an improved c calling interface for unicon language udaykumar batchu advisor: dr. clinton jeffery

29
An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

Upload: britney-copeland

Post on 30-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

An Improved C Calling Interface for Unicon Language

Udaykumar Batchu

Advisor: Dr. Clinton Jeffery

Page 2: 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

Page 3: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 4: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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++

Page 5: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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))dowrite(tab(many(&letters)))

}writes out all the set of words found in the string str

Page 6: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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 procedureprocedure array(i, j, x)

local L

L := list(i)

every !L := list(j, x)

return L

end

Page 7: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

functionsMission:• Automate the whole process mentioned above• Make it easier to call external C functions inside

Unicon programs

Page 8: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

Earlier Mechanism

• C functions were called with – pathload (filename, funcname)

filename – shared library name containing the C functionfuncname – 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

Page 9: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 10: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 11: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 12: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 13: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 14: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 15: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 16: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 17: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

Improved Mechanism – example

procedure main()local a, ba :=4b := 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

Beginning place for external C code

Signature of the C function with return types and function parameter types

End of external C code

Page 18: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

Improved Mechanism in Detaile.g. test1.icn – Unicon program calling C functionsprocedure 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

Calling the external C functions inside

Unicon

Start of the external C function declarations

End of the external C function declarations

C function signatures

Page 19: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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 ); }

Page 20: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

Icon/Unicon Stubs

#myaverage.cprocedure 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

Page 21: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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.

Page 22: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

Example involving Arrays# test2.icn – Program using C functions having single dimensional arrays

as parametersprocedure 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

Page 23: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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;

}

Page 24: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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 ); }

Page 25: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 26: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 27: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 28: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

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

Page 29: An Improved C Calling Interface for Unicon Language Udaykumar Batchu Advisor: Dr. Clinton Jeffery

Thank You