DEFC


NOTE: This refers to the behaviour of the DEFC statement in jBASE 4.1 and later. For the corresponding statement in jBASE 3.4, see DEFC.

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

The DEFC statement assumes that the C functions will need to manipulate jBASE BASIC variables and hence will also require the thread data pointer. As such, all C functions require recoding to include the data pointer as an argument to the C function. The location of the data pointer argument depends upon the function return type.

 

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

#include <jsystem.h>

#include <assert.h>

#ifdef DPSTRUCT_DEF

#define JBASEDP DPSTRUCT *dp,

#else

#define JBASEDP

#endif

VAR *MyString(VAR *Result, JBASEDP VAR *VarPtr)

{

    char *Ptr;

    assert(dp != NULL);

    Ptr = (char *) CONV_SFB(VarPtr);

    printf("MyString: %s - %d\n", Ptr, strlen(Ptr) );

    STORE_VBI(Result, strlen(Ptr) );

    return(Result);

}

INT32 MyCalc(INT32 Value1, INT32 Value2)

{

INT32 Result;

    Result = (Value1 / Value2);

    printf("MyCalc: %d\n", Result);

    return(Result);

}

 


jBC