next up previous contents index
Next: Compiler Options Up: The Compiler Previous: The Compiler   Contents   Index


Invoking the Compiler

The compiler is invoked directly at the interpreter level (or in a program) through the Prolog predicates compile/[1,2].

The general forms of predicate compile/2 are:

compile(+File, +OptionList)
compile(+FileList, +OptionList)
and at the time of the call both of its arguments should be ground.

The second form allows the user to supply a proper list of file names as the parameter for compile/[1,2]. In this case the compiler will compile all the files in FileList with the compiler options specified in OptionList (but see Section 3.8.2 below for the precise details.)

$\vert$ ?- compile(Files).

is just a notational shorthand for the query:

$\vert$ ?- compile(Files, []).

The standard predicates consult/[1,2] call compile/1 (if necessary). Argument File can be any syntactically valid UNIX or Windows file name (in the form of a Prolog atom), but the user can also supply a module name.

The list of compiler options OptionList, if specified, should be a proper Prolog list, i.e. a term of the form:

[ $option_1$, $option_2$, $\ldots$, $option_n$ ].
where $option_i$ is one of the options described in Section 3.8.2.

The source file name corresponding to a given module is obtained by concatenating a directory prefix and the extension .P (or .c) to the module name. The directory prefix must be in the dynamic loader path (see Section 3.4). Note that these directories are searched in a predetermined order (see Section 3.4), so if a module with the same name appears in more than one of the directories searched, the compiler will compile the first one it encounters. In such a case, the user can override the search order by providing an absolute path name.

If File contains no extension, an attempt is made to compile the file File.P (or File.c) before trying compiling the file with name File.

We recommend use of the extension .P for Prolog source file to avoid ambiguity. Optionally, users can also provide a header file for a module (denoted by the module name suffixed by .H). In such a case, the XSB compiler will first read the header file (if it exists), and then the source file. Currently the compiler makes no special treatment of header files. They are simply included in the beginning of the corresponding source files, and code can, in principle, be placed in either. In future versions of XSB the header files may be used to check interfaces across modules, hence it is a good programming practice to restrict header files to declarations alone.

The result of the compilation (an SLG-WAM object code file) is stored in a ($\langle$filename$\rangle$.O), but compile/[1,2] does not load the object file it creates. (The standard predicates consult/[1,2] and reconsult/[1,2] both recompile the source file, if needed, and load the object file into the system.) The object file created is always written into the directory where the source file resides (the user should therefore have write permission in that directory).

If desired, when compiling a module (file), clauses and directives can be transformed as they are read. This is indeed the case for definite clause grammar rules (see Chapter 8), but it can also be done for clauses of any form by providing a definition for predicate term_expansion/2 (see Section 8.3).

Predicates compile/[1,2] can also be used to compile foreign language modules. In this case, the names of the source files should have the extension .c and a .P file must not exist. A header file (with extension .H) must be present for a foreign language module (see the chapter Foreign Language Interface in Volume 2.


next up previous contents index
Next: Compiler Options Up: The Compiler Previous: The Compiler   Contents   Index
Baoqiu Cui
2000-04-23