DEFC


The DEFC statement is used to declare an external C function to the jBC compiler and define its arguments and return types.

 

COMMAND SYNTAX

DEFC {FuncType} FuncName ({ArgType {, ArgType ...}})

 

SYNTAX ELEMENTS

FuncType 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.

 

NOTES

A 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

DEFC INT cfunc( INT, FLOAT, VAR)
Var1 = cfunc( A, 45, B)
cfunc( 34, C, J)

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

DEFC INT getpid()
CRT "Process id =":getpid()

 

EXAMPLE 3

Compiling and calling a C function on Unix. Given the following C function square.c:

#include <jsystem.h>

INT32 square(INT32 number)
{
    return(number*number);
}

and the jBC program calcsuqare.b

PROMPT ""
DEFC square(INT)

LOOP
    CRT "Enter an integer :":
    INPUT n:
UNTIL NOT(NUM(n)) OR n = "" DO
    CRT n:" squared is ":square(n)
REPEAT

END

Create these source files in an empty directory. First compile c function

jbc -c square.c

This creates the output file square.o

Now create the shared object that is loaded when the c function is called.

jBuildSLib -o sample.so square.o

Then compile the jBC program:

jbc -Jo calcsquare.b square.o

As everything is in the same directory, you can now run the program.

./calcsquare
Enter a number :46
46 squared is 2116

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 4

Compiling and calling a C function on Windows. Given the following C function square.c:

#include <jsystem.h>

INT32 square(INT32 number)
{
    return(number*number);
}

and the jBC program calcsuqare.b

PROMPT ""
DEFC square(INT)

LOOP
    CRT "Enter an integer :":
    INPUT n:
UNTIL NOT(NUM(n)) OR n = "" DO
    CRT n:" squared is ":square(n)
REPEAT

END

First compile c function

jbc -c square.c

This creates the output file square.obj

Now create the shared object that is loaded when the c function is called.

jBuildSLib -o sample.dll square.obj

This creates the files sample.def, sample.dll, sample.exp and sample.lib

Then compile the jBC program:

jbc -Jo calcsquare.b sample.lib

As everything is in the same directory, you can now run the program.

.\calcsquare
Enter a number :36
36 squared is 1296

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.


jBC