4 Working Beyond the Lisp Environment

4.1 The Foreign Function Interface

Unlike data in languages such as C and FORTRAN, data objects in Liquid Common Lisp are typed objects; that is, the bit patterns for Lisp objects indicate the type of the object. For example, the hexadecimal form of the ASCII characterA is 41 in C; in Liquid Common Lisp, the hexadecimal form of the same character is 4116 (in LCL/RS6000, it is 4138). Thus, to communicate with code compiled in another language, the Lisp data must be converted to a representation that the other language can understand. A similar conversion must take place when foreign code communicates with compiled Lisp code.

Liquid Common Lisp provides a Foreign Function Interface that allows you to link compiled C, Pascal, and FORTRAN files with a Lisp program and to link Lisp programs into running C, Pascal, and FORTRAN code. In this chapter, functions that are written in computer languages other than Lisp are called foreign functions.

The following features are provided by the Foreign Function Interface:

The macrodef-foreign-function defines a Lisp routine that coerces Lisp arguments to foreign data types, calls the foreign code, and coerces the values returned from the foreign code into Lisp objects.

The macrodef-foreign-callable defines a routine that converts foreign arguments to Lisp objects, calls a Lisp routine, and converts the returned Lisp value into a foreign object.

The macrosdef-foreign-synonym-type anddef-foreign-struct manipulate a type system that is much like the Lisp type system but is applied to foreign data structures. Using this foreign type system, you can specify the layout of foreign data structures and write functions that convert between foreign data structures and Lisp objects.

The functionsload-foreign-files andload-foreign-libraries read binary object files into the Lisp image and build a symbol table of entry points.Unlike many linking loaders, all relocation information is preserved; thus, foreign code can be dynamically linked and relinked.

These features are described in detail in the sections that follow.

4.1.1 - Defining and calling foreign functions
4.1.2 - Creating foreign structure types
4.1.3 - Loading foreign language files
4.1.4 - Backward compatible constructs
4.1.5 - Examples
4.1.6 - Predefined data types for foreign structures
4.1.7 - Passing foreign and Lisp data types
4.1.8 - Reference pages

The Advanced User's Guide - 9 SEP 1996

Generated with Harlequin WebMaker