The OpenMCL interface translator

Overview

OpenMCL uses an interface translation system based on the FFIGEN system (described here and here) to make the constant, type, structure, and function definitions in a set of .h files available to lisp code.

The basic idea of the FFIGEN scheme is to use the C compiler's frontend and parser to translate .h files into semantically equivalent .ffi files, which use an S-expression - based syntax to represent the definitions contained in those headers. Lisp code can then concentrate on the .ffi representation, without having to concern itself with the semantics of header file inclusion or the arcana of C parsing.

The original FFIGEN system used a modified version of the LCC C compiler to produce .ffi files. Since many LinuxPPC header files contain GCC-specific constructs, OpenMCL's translation system uses a modified version of GCC (called, somewhat confusingly, ffigen.)


A LinuxPPC binary is available at ftp://clozure.com/pub/ffigen.tar.gz, and LinuxPPC source differences are at ftp://clozure.com/pub/ffigen-src.tar.gz

For Darwin, the binary is available at ftp://clozure.com/pub/ffigen-darwin.tar.gz, and the source differences are at ftp://clozure.com/pub/ffigen-darwin-src.tar.gz


A shell script (distributed with the source and binary packages) called h-to-ffi.sh reads a specified .h file (and optional preprocessor arguments) and writes a (hopefully) equivalent .ffi file to standard output, calling the installed C preprocessor and the ffigen program with appropriate arguments.

For each interface directory subdir distributed with OpenMCL, a shell script (distributed with OpenMCL as "ccl:headers;subdir;C;populate.sh" ("ccl:darwin-headers;subdir;C;populate.sh" for Darwin) calls h-to-ffi.sh on a large number of the header files in /usr/include (or some other system header path) and creates a parallel directory tree in "ccl:headers;subdir;C;system;header;path;" (or "ccl:darwin-headers;subdir;C;system;header;path;"), populating that directory with .ffi files.

A lisp function defined in "ccl:library;parse-ffi.lisp" reads the .ffi files in a specified interface directory subdir and generates new versions of the databases ("ccl:headers;subdir;constants.cdb", "ccl:headers;subdir;functions.cdb", "ccl:headers;subdir;records.cdb", and "ccl:headers;subdir;types.cdb"); it can optionally produce a parallel direcory of .lisp files (in "ccl:headers;subdir;usr;include;"). Such .lisp files aren't used directly by OpenMCL, but may be interesting as reference material: the information in the .cdb files is an encoded version of the union of the information in the .lisp files.

The CDB databases are used by the #$ and #_ reader macros and are used in the expansion of RREF, RLET, and related macros.

Details

Rebuilding the CDB databases, step by step

  1. Ensure that the FFIGEN program is installed. See the "README" file in the source or binary archive for specific installation instructions.
  2. Edit the "ccl:headers;subdir;C;populate.sh" shell script. When you're confident that the files and preprocessor options match your environment, cd to the "ccl:headers;subdir;C;" directory and invoke ./populate.sh. Repeat this step until you're able to cleanly translate all files refrenced in the shell script.
  3. Run OpenMCL:
    ? (require "PARSE-FFI")
    PARSE-FFI
    ? (parse-standard-ffi-files :SUBDIR)
    ;;; lots of output ... after a while, shiny new .cdb files should
    ;;; appear in "ccl:headers;subdir;" 
    ;;; (or "ccl:darwin-headers;subdir;" under Darwin)
    	

    PARSE-STANDARD-FFI-FILES accepts a :PREPEND-UNDERSCORES keyword argument. Darwin (and some other platforms) use a convention wherein the symbols associated with C-visible external function and variables have underscore characters prepended to their names. When this argument is true, PARSE-STANDARD-FFI-FILES will prepend underscores to all foreign function names written to the database, so that (#_foo ...) expands into an EXTERNAL-CALL to "_foo".


Last modified: Tue Apr 30 13:38:33 MDT 2002