![]() ![]() |
|
DEFCThe DEFC statement is used to declare an external C function to the jBC compiler and define its arguments and return types.
COMMAND SYNTAXDEFC {FuncType} FuncName ({ArgType {, ArgType ...}})
SYNTAX ELEMENTSFuncType and ArgType are selected from one of INT, FLOAT or VAR. FuncType specifies the type of result that the function will return. If FuncType is omitted then INT will be assumed. The optional list of ArgTypes specifies the argument types that the C function will expect. The compiler must know this in advance as it will automatically perform type conversions on these arguments.
NOTESA DEFC must be compiled for each C function before any reference is made to it or the compiler will not recognize the function name. The function is called in the same manner as it would be in a C program. This means it can be used as if it was an intrinsic function of the jBC language and therefore returns a value. However it can also be specified as a standalone function call, which causes the compiler to generate code that ignores any returned values. When jBC variables are passed to a C function you must utilize predefined macros to access the various data types it contains. These macros are fully documented in the Advanced Programmer's manual - in the section C Extensions, Supported Functions and Macros. This manual also gives examples of working C functions and documents other interfaces available to the jBC programmer. C functions are particularly useful for increasing the performance of tight loops that perform specific functions. The jBC compiler must cater for any eventuality within a loop (such as the controlling variable changing from integer to floating point). A dedicated C function can ignore such events, if they are guaranteed not to happen. The jBC programmer may freely ignore the type of argument used when the C function is invoked as the jBC compiler will automatically perform type conversion.
EXAMPLE 1
Standard UNIX functions may be called directly be declaring them with the DEFC statement according to their parameter requirements. However, they may only be called directly providing they return one of the types int or float/double or that the return type may be ignored.
EXAMPLE 2
EXAMPLE 3Compiling and calling a C function on Unix. Given the following C function square.c:
INT32 square(INT32 number) and the jBC program calcsuqare.b
LOOP END Create these source files in an empty directory. First compile c function
This creates the output file square.o Now create the shared object that is loaded when the c function is called.
Then compile the jBC program:
As everything is in the same directory, you can now run the program.
Enter a number : When incorporating the object code into an application, the shared object that contains the C function and the .el file must be in a directory that is pointed to by JBCOBJECTLIST.
EXAMPLE 4Compiling and calling a C function on Windows. Given the following C function square.c:
INT32 square(INT32 number) and the jBC program calcsuqare.b
LOOP END First compile c function
This creates the output file square.obj Now create the shared object that is loaded when the c function is called.
This creates the files sample.def, sample.dll, sample.exp and sample.lib Then compile the jBC program:
As everything is in the same directory, you can now run the program.
Enter a number : When incorporating the object code into an application, the shared object that contains the C function and the .el file must be in a directory that is pointed to by JBCOBJECTLIST. |