An Improved C Calling Interface for Unicon Language



















![Icon/Unicon Stubs #myaverage. c procedure myaverage(a[]) #calculates myaverage return(myaverage: =pathload("ctest 1. so", "myaveragewrap"))!a; end Icon/Unicon Stubs #myaverage. c procedure myaverage(a[]) #calculates myaverage return(myaverage: =pathload("ctest 1. so", "myaveragewrap"))!a; end](https://slidetodoc.com/presentation_image_h2/f9d336acb4d8dc3d8d6d93c16c6c1068/image-20.jpg)









- Slides: 29
An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery
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 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; Arg. Integer(1); /* validate type */ v = Integer. Val(argv[1]); /* convert to C type */ n = fibonacci(v); Ret. Integer(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 Automatic Wrapper Code generation make_library icon_stub 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 procedure main() local a, b Beginning place for a : =4 external C code b : = fib(a) write(“the fibonacci value for”, a, “ is: ”, b) end Name of the object file to look for the external C function $libc { fibonacci. o {int fib(int)} } $libcend Signature of the C function with return types and function parameter types End of external C code
Improved Mechanism in Detail e. g. test 1. icn – Unicon program calling C functions procedure main() local i, j, str Calling the external C functions inside Unicon 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 Start of the external C function declarations $libc { fact. o { int factorialfunc (int ) }, classavg. o { double myaverage(double, double)}, checkpalin. o { int palindrome( char) } } $libcend 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 "My. Icall. h" double myaverage(double, double); int myaveragewrap(int argc, descriptor *argv) { double return. Value; double arg 1; double arg 2; Arg. Real( 1 ); arg 1 = Real. Val( argv[ 1 ] ); Arg. Real( 2 ); arg 2 = Real. Val( argv[ 2 ] ); return. Value = myaverage( arg 1, arg 2 ); Ret. Real( return. Value ); }
Icon/Unicon Stubs #myaverage. c procedure myaverage(a[]) #calculates myaverage return(myaverage: =pathload("ctest 1. so", "myaveragewrap"))!a; end About the Shared Library - ctest 1. so • Library contains the entries for all external C functions used inside a particular Unicon program, here it is test 1. 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 # test 2. 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) bubble. Sort(elements, sorted, size) avg : = Caverage(dblelements, size) Calling external C functions inside Unicon Start of the write("the list after getting sorted is: ") external C function every write(!elements) declaration write("the average of the list of doubles is--> ", avg) End $libc { bubblesort. o { void bubble. Sort(int[], int) }, arrayavg. o { double Caverage(double[], int) } } $libcend End of the external C function declarations
Wrapper code for Arrays - 1 #bubble. Sortwrap. c – Wrapper code generated for the C function bubble. Sort #include <stdio. h> #include "My. Icall. h" int bubble. Sort(int[], int); int bubble. Sortwrap(int argc, descriptor *argv) { int *arg 1; int arg 2; Arg. List( 1 ); arg 1 = (int *) malloc((int) List. Len( argv[ 1 ] )* sizeof(int)); IList. Val( argv[ 1 ], arg 1 ); Arg. Integer( 2 ); arg 2 = Integer. Val( argv[ 2 ] ); bubble. Sort( arg 1, arg 2 ); IVal. List( arg 1, argv[ 1 ] ); return 0; }
Wrapper code for Arrays - 2 #arrayavgwrap. c – Wrapper code for the C function Caverage #include <stdio. h> #include "My. Icall. h" double Caverage(double[], int); int Caveragewrap(int argc, descriptor *argv) { double return. Value; double *arg 1; int arg 2; Arg. List( 1 ); arg 1 = (double *) malloc((int) List. Len( argv[ 1 ] )* sizeof(double)); RList. Val( argv[ 1 ], arg 1 ); Arg. Integer( 2 ); arg 2 = Integer. Val( argv[ 2 ] ); return. Value = Caverage( arg 1, arg 2 ); Ret. Real( return. Value ); }
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 – Arg. List for validating List data type – IList. Val and RList. Val: takes a Unicon list descriptor and convert them to an array of integers or reals – IVal. List and RVal. List: 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
Thank You