Compiling a context¶
You can compile a context multiple times (using either form of compilation), although any errors that occur on the context will prevent any future compilation of that context.
This calls into GCC and builds the code, returning a
Locate the given function within the built machine code.
Functions are looked up by name. For this to succeed, a function with a name matching funcname must have been created on result‘s context (or a parent context) via a call to
The returned value is an int, actually a pointer to the machine code within the address space of the process. This will need to be wrapped up with ctypes to be callable:
import ctypes # "[int] -> int" functype: int_int_func_type = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int) code = int_int_func_type(jit_result.get_code(b"square")) assert code(5) == 25
The code has the same lifetime as the
gccjit.Resultinstance; the pointer becomes invalid when the result instance is cleaned up.
Although libgccjit is primarily aimed at just-in-time compilation, it
can also be used for implementing more traditional ahead-of-time
compilers, via the
compile_to_file(self, kind, path)¶
Compile the context to a file of the given kind:
gccjit.Context.compile_to_file()ignores the suffix of
path, and insteads uses kind to decide what to do.
This is different from the
gccprogram, which does make use of the suffix of the output file when determining what to do.
The available kinds of output are:
Output kind Typical suffix
.so or .dll
None, or .exe
Compile the context to an assembler file.
Compile the context to an object file.
Compile the context to a dynamic library.
There is currently no support for specifying other libraries to link against.
Compile the context to an executable.
There is currently no support for specifying libraries to link against.