310 likes | 10.09k Views
Udaykumar Batchu Advisor: Dr. Clinton Jeffery. An Improved C Calling Interface for Unicon Language. A Brief Introduction to the Unicon Language. Is a high level programming language Derives Its name from Icon, originated from University of Arizona
E N D
Udaykumar Batchu Advisor: Dr. Clinton Jeffery An Improved C Calling Interface for Unicon Language
A Brief Introduction to the Unicon Language • Is a high level programming language • Derives Its name from Icon, originated from University of Arizona • Offers support for various data types, structures, procedures, high-level graphics functions and object oriented facilities • Easy for application development involving objects, networks and databases and simpler syntax than C and many other high level languages • Is highly portable
Basic Data Types of Unicon All the basic data types are Immutable • Integers: size of long int; are of arbitrary precision • Real: double-precision floating point values • String: similar to C; includes escape sequences and special characters • Cset: character sets represented within single quotes
Unicon Structures Unicon provides four different types of structures. All can hold heterogeneous values • Lists: useful for representing arrays, queues, linked lists and stacks; declared with list() function • Tables: similar to associative arrays; declared with table() function • Records: similar C structures; declared with record() function • Sets: used for holding unordered collection of values; declared with set() function • Classes: similar to classes found in C++
String Scanning in Unicon str ? expression • str is the subject string used for string scanning operations • expression is/are the set of operations to be performed on str • nested string scanning expressions are possible • can design flexible and readable string scanning expressions • easier regular expressions and pattern matching actions • Unicon provides a variety functions for use with string scanning e.g. str ? { while tab(upto(&letters))do write(tab(many(&letters))) } writes out all the set of words found in the string str
Procedures in Unicon • Procedures in Unicon are declared with the keyword procedure • procedure main()is the start of any Unicon program • All parameters are passed by value except for structures • parameter types are not declared; they are intuitive e.g. sample Unicon procedure procedure array(i, j, x) local L L := list(i) every !L := list(j, x) return L end
Introduction to the Problem Goal: Using the existing code written in C language, inside Unicon Programs Earlier Mechanism: • Support was available only for few data types • Manual Updating of Makefile and shared library • Wrapper code need to be hand written • Building and linking the shared library for calling C functions Mission: • Automate the whole process mentioned above • Make it easier to call external C functions inside Unicon programs
Earlier Mechanism • C functions were called with • pathload (filename, funcname) filename – shared library name containing the C function funcname – name of the C function • pathload uses loadfunc which in turn uses dynamic loading and loads the external C function dynamically at runtime • Making entries into the Makefile; building it for Shared library creation or updating the existing one
Earlier Mechanism contd… Supplying files to Makefile: FUNCS = bitcount.o files.o fpoll.o internal.o lgconv.o osf.o \ pack.o ppm.o process.o tconnect.o fibonacci.o CSRC = bitcount.c files.c fpoll.c internal.c lgconv.c osf.c \ pack.c ppm.c process.c tconnect.c fibonacci.c FUNCS: Object files CSRC: C language Source files • UNIX Shell scripts were used for building the shared library and Icon/Unicon stubs
Earlier Mechanism - Steps followed • Write or locate the C code to be called • For the C function(s) to be called, the programmer needs to write wrapper code using macros present in “icall.h” header file • Build (Update) the shared library with an entry for the new C function • Write a Unicon/Icon stub procedure to call the wrapper function: which is used for data type conversions • Link the external C function inside the Unicon program
Steps to be Followed Step 1)Example: C code for Fibonacci Series using recursion: fibonacci.c int fibonacci(int n) { if(n<2)return 1; else return (fibonacci(n-1) + fibonacci(n-2)); } Step 2) Wrapper Code for Fibonacci: fibonacciwrap.c int fibonacciwrap(int argc, descriptor *argv) { unsigned long v; int n; ArgInteger(1); /* validate type */ v = IntegerVal(argv[1]); /* convert to C type */ n = fibonacci(v); RetInteger(n); /* return the value to Unicon */ }
Steps to be Followed - contd… Step 3) Building the Shared Library • Compile the above two files and update the makefile to create a shared called “libcfunc.so” Step 4) Creation of Icon/Unicon Stub: procedure fibonacci(a[]) #:Fibonacci if fibonacci := pathload("libcfunc.so", "fibonacciwrap") then return fibonacci(n) end • The above entry is created in “cfunc.icn” under /unicon/ipl/cfuncs/ during the updation of shared library Step 5) Linking the external C Program • Adding the statement ‘link cfunc’ inside the Unicon program for calling the external C function
Calling C function from Unicon Example Unicon Program: “foo.icn” using “fibonacci” from C function link cfunc procedure main() local i write("Enter value for fibonacci") i := read() write("Value of Fibonacci is:", fibonacci(i) ) end • “libcfunc.so” is the shared library file, where we can find the C function ‘fibonacci’ to be loaded dynamically at runtime using loadfunc
Improved Mechanism - Overview New Preprocessor Directives and more • “$libc” and “$libcend” preprocessor directives within which external C functions can be declared • Providing room for function signatures for the external C functions being used in Unicon with these new preprocessor directives • External C function signatures are grabbed as a string and processed within the preprocessor
Improved Mechanism contd… • Automatic wrapper code generation along with Icon/Unicon stubs takes place during preprocessing stage of compilation • Unicon translator also builds the Shared library for loading the external C functions used inside the Unicon program • Each Unicon (*.icn) program has its own shared library built when C functions are used in it. • Support is provided for basic data types such as: Integers, Real’s and Characters along with Integer and Real Arrays
Implementation: Changes to the Unicon Language Unicon Source Program Unicon Preprocessor cincludesparser make_library icon_stub Automatic Wrapper Code generation Building the shared library Generation of Icon/Unicon stubs • Preprocessor code is modified at /unicon/uni/preproce.icn • Macros used for handling arrays are tweaked at /unicon/src/runtime/rstruct.r • All the work is developed under 32-bit Linux (Kernel Version 2.6.11.4) environment and is easily portable to other platforms • Existing Macros for handling Integers, Reals and Strings are used
Improved Mechanism – example Beginning place for external C code procedure main() local a, b a :=4 b := fib(a) write(“the fibonacci value for”, a, “ is:”, b) end $libc { fibonacci.o {int fib(int)} } $libcend Name of the object file to look for the external C function Signature of the C function with return types and function parameter types End of external C code
Improved Mechanism in Detail Calling the external C functions inside Unicon e.g. test1.icn – Unicon program calling C functions procedure main() local i, j, str write("enter two variables i and j :") i := read() j := read() write("enter the string for palindrome check: ") str := read() write("the avg of i and j is***********: ", myaverage(i, j)) write("the factorial of number i is****: ", factorialfunc(i)) palindrome(str) end $libc { fact.o { int factorialfunc (int ) }, classavg.o { double myaverage(double, double)}, checkpalin.o { int palindrome( char) } } $libcend Start of the external C function declarations C function signatures End of the external C function declarations
Wrapper code generation Wrapper Code for the functions present classavg.o library #include <stdio.h> #include "MyIcall.h" double myaverage(double, double); int myaveragewrap(int argc, descriptor *argv) { double returnValue; double arg1; double arg2; ArgReal( 1 ); arg1 = RealVal( argv[ 1 ] ); ArgReal( 2 ); arg2 = RealVal( argv[ 2 ] ); returnValue = myaverage( arg1, arg2 ); RetReal( returnValue ); }
Icon/Unicon Stubs #myaverage.c procedure myaverage(a[]) #calculates myaverage return(myaverage:=pathload("ctest1.so","myaveragewrap"))!a;end About the Shared Library -ctest1.so • Library contains the entries for all external C functions used inside a particular Unicon program, here it is test1.icn • pathload () in turn calls loadfunc() to load the external C funtion dynamically at run time • The stubs become written procedures of a Unicon program written for the compiler • Programmers’ burden is heavily reduced
Parameter Declaration for Arrays • format for declaring a function parameter to be a single dimensional array is • data type followed by array indices ‘[]’ • look like: int[] or double[] • When array indices are found for a function parameter type • wrapper code for handling arrays is generated by the Unicon translator • support is provided for both integer and real arrays.
Example involving Arrays # test2.icn – Program using C functions having single dimensional arrays as parameters procedure main() local elements, sorted, size, loop, avg write("enter the size of the array: ") size := integer(read()) elements := list(size, 0) dblelements := list(size, 0) - - bubbleSort(elements, sorted, size) avg := Caverage(dblelements, size) write("the list after getting sorted is:") every write(!elements) write("the average of the list of doubles is--> ", avg) End $libc { bubblesort.o { void bubbleSort(int[], int[], int) }, arrayavg.o { double Caverage(double[], int) } } $libcend Calling external C functions inside Unicon Start of the external C function declaration End of the external C function declarations
Wrapper code for Arrays - 1 #bubbleSortwrap.c – Wrapper code generated for the C function bubbleSort #include <stdio.h> #include "MyIcall.h" int bubbleSort(int[], int); int bubbleSortwrap(int argc, descriptor *argv) { int *arg1; int arg2; ArgList( 1 ); arg1 = (int *) malloc((int) ListLen( argv[ 1 ] )* sizeof(int)); IListVal( argv[ 1 ],arg1 ); ArgInteger( 2 ); arg2 = IntegerVal( argv[ 2 ] ); bubbleSort( arg1, arg2 ); IValList( arg1, argv[ 1 ] ); return 0; }
Wrapper code for Arrays - 2 #arrayavgwrap.c – Wrapper code for the C function Caverage #include <stdio.h> #include "MyIcall.h" double Caverage(double[], int); int Caveragewrap(int argc, descriptor *argv) { double returnValue; double *arg1; int arg2; ArgList( 1 ); arg1 = (double *) malloc((int) ListLen( argv[ 1 ] )* sizeof(double)); RListVal( argv[ 1 ],arg1 ); ArgInteger( 2 ); arg2 = IntegerVal( argv[ 2 ] ); returnValue = Caverage( arg1, arg2 ); RetReal( returnValue ); }
Macros Used for Wrapper Code Generation • Macros help in converting Unicon descriptors to equivalent C data types and vice versa • Basic descriptor looks as: • typedef long word; • typedef struct { word dword, vword; } descriptor; • dword has the information about its type • vword stores the actual value • Macros are provided are for handling Various Unicon data types: Integer, Real, Character and Arrays • ArgList for validating List data type • IListVal and RListVal: takes a Unicon list descriptor and convert them to an array of integers or reals • IValList and RValList: copy back the contents of C array of integers or reals into Unicon list structure
Limitations • Unicon strings are Immutable: modifying values of the string variables when passed to C functions is not possible • Programmer should take care of wrapper code when using string manipulation functions inside Unicon • Support is provided only for Integer and Real arrays
Future Work • Making the C function calling inside Unicon into a Inline capability • Extending the support for C structures (struct *) and Union data types inside Unicon
Conclusion • New preprocessor directives make it easier for calling external C functions inside Unicon • Different data types such as Integer, Real, Character and single dimensional Array types for Integers and Reals are supported • New interface eliminates writing the wrapper code • Shared library is automatically built by the Preprocessor • Icon/Unicon stubs generated automatically are passed to the Unicon program