5 Header Compilation
This library provides facilities for extracting and compiling C header
information to generate architecture-specific binary layout information.
This is particularly useful for writing code that interacts with the PLT
Scheme foreign library—see FFI: Racket Foreign Interface.
Specifically, the foreign library’s pointer-manipulation procedures such as
ptr-ref and ptr-set! can be used to read and write to arbitrary
addresses, which can be computed using layout information.
5.1 Headers
Determines whether x is a header.
Constructs a header.
Constructs a header.
Examples: |
| > (define time.h | (header | (struct tm ([int tm_sec] | [int tm_min] | [int tm_hour] | [int tm_mday] | [int tm_mon] | [int tm_year] | [int tm_wday] | [int tm_yday] | [int tm_isdst])))) |
|
|
Using the Scribble @-reader,
the latter example could be rewritten with embedded C syntax as:
(define time.h |
(make-header |
@program{ |
struct tm { |
int tm_sec; |
int tm_min; |
int tm_hour; |
int tm_mday; |
int tm_mon; |
int tm_year; |
int tm_wday; |
int tm_yday; |
int tm_isdst; |
}; |
})) |
5.2 Compilation
A header compiler must recognize the following types of queries.
Determines whether x is a query
A query to determine the size of the type represented by
type.
A query to determine the offset of
field in
type.
A query to determine the value of the integer expression
expr.
A header compiler that delegates to an external C compiler, presumably installed on the
current system. The queries are converted into a series of C statements which are assembled
into a C program that produces the answers. The
include<> list is used to generate
the list of system C headers to be included in the generated C program, and the
include
list is the list of path strings for local headers included in the generated C program.
The
gcc compiler is used by default, but this can be overridden by providing an
alternative system compiler for the
exe argument.
5.3 Layouts
5.4 Application Binary Interfaces (ABI’s)
An Application Binary Interface (ABI) provides information about the binary representation
of C datatypes on a particular architecture.
An ABI. The typedefs are a table of type definitions and the tags are a table
of struct, union, and enum tag definitions.
As a convenience, ABI structures can be used as procedures, which is equivalent to calling
abi-lookup with the ABI structure as the first argument.
Looks up the definition of name in abi, searching
the type definitions first, and failing that, the tag definitions.
Looks up the type definition of name in abi.
Looks up the type tag definition of name in abi.
Serializes abi as an S-expression.
Deserializes input as a representation of an ABI.
Reads a serialized ABI from in.
Serializes abi and writes it to out.