Eclipse Applications: Lisp Function Calls in C


General Form

A typical function call in Lisp appears as:
  (setq value (list arg0 arg1 arg2))
where argn may be any lisp expression, including variable references, other function calls, etc. In the most general case, this appears in code translated to C as:
 { clObject L_0, L_1, L_2;
   clSetq(L_0, arg0);
   clSetq(L_1, arg1);
   clSetq(L_2, arg2);
   clSetq(value, clList(L_0, L_1, L_2, clEOA)); }
The temporary variables may be necessary, in general, because:
  1. The order of evaluation of arguments to functions is defined as being left-to-right in ANSI Common Lisp, but is not defined at all in C. The use of temporary variables ensures the proper order of evaluation.
  2. The arguments to functions in Lisp may be an arbitrarily complex expression, involving iteration, introduction of new lexical variables, and other control structure. The translation of many of these into C involves the use of C statements (i.e. not simply expressions) which cannot appear as arguments in a C function call. The use of temporary variables is then required.
In many cases, such as when the arguments are simply variable references, the temporary variables are eliminated by compile-file, leaving the simpler expression:
   clSetq(value, clList(arg1, arg2, arg3, clEOA));

Argument Termination

Note, that the last argument to clList, above is the meta symbol clEOA. This marker is defined in eclipse.h and is used to denote the End Of Arguments to all Lisp function calls. This is used by Lisp functions to do argument parsing and checking.

Note that the symbol EOA does not appear in the Lisp source code. It is added automatically by compile-file.

Function Reference

In the example above, there is a C function clList that implements the behavior of the Lisp function LIST. This function is called directly.

In general, the calling mechanism may go through other levels of indirection, such as through the symbol-function of a symbol. See "Implementation of Lisp" for more information. The current version of Eclipse does not provide user control over when this additional indirection is performed for Lisp functions.

Calling C Functions and Macros

A user`s Lisp function can be translated to C using compile-file, and all of the functions defined by Eclipse are implemented in C in the library. However, we still refer to these as Lisp functions because they follow Lisp argument parsing semantics -- ie. they might take optional or named (keyword) arguments. The argument in C calls to such functions must be terminated with clEOA, as describe above.

When an ordinary C function is to be called from Lisp code that has been translated to C by compile-file, compile-file must not add the clEOA marker or use any function reference indirection.

This can be avoided by providing a c:function or c:macro compound type specifier ftype declaration in the Lisp source code. Just as C functions must be declared before they are used in C source code, they must be declared before they are used in Lisp source code that is translated to C by compile-file. Of course, it is not necessary to declare Lisp functions before use, regardless of whether those Lisp functions are implemented in C.

Note that a C macro referenced by Lisp source code must be a function-like macro in that it must expand into a C expression, not one or more statements. At this time, compile-file makes no special provision to avoid multiple evaluation of arguments to a c:macro.

At this time, there is no special mechanism to call a C function from code that has not been processed by compile-file (eg. interpreted code in the Lisp top level). For example, a programmer may define a function in C that is compiled and linked to the library to create a new application -- possibly even an application (such as the Eclipse top-level) that calls clLispTopLevel. If it is desired that this hand-written C function should provide such Lisp features as being fbound and callable by interpreted Lisp code, then the programmer will also have to write code to create a symbol identifying the function, and (setf symbol-value) of the symbol to a closure referencing the C function. Details are discussed in "Implementation of Lisp".


[Previous] [Next] [Up] [Documentation Contents] [Eclipse Home Page]