Module Gccjit

OCaml bindings for libgccjit.

See GCC wiki page for more information.

exception Error of string

This exception (containing an explanatory string) is raised if an error occurs.

type context

The type of compilation contexts. See Compilation Contexts.

type result

A result encapsulates the result of an in-memory compilation.

type location

A location encapsulates a source code location, so that you can (optionally) associate locations in your languages with statements in the JIT-compiled code, alowing the debugger to single-step through your language. See Source locations.

type param

A param is a function parameter. See Parameters.

type lvalue

An lvalue is something that can be the left-hand side of an assignment. See Lvalues.

type rvalue

A rvalue is an expression within your code, with some type. See RValues.

type field

The type of fields of structs and unions. See Fields.

type struct_

The type of structure types. See Structure Types.

type type_

The type of C types, e.g. int or a struct foo*. See Types.

type function_

The type of functios. See Functions.

type block

The type of basic blocks. See Basic Blocks.

type unary_op =
  1. | Negate
  2. | Bitwise_negate
  3. | Logical_negate
type binary_op =
  1. | Plus
  2. | Minus
  3. | Mult
  4. | Divide
  5. | Modulo
  6. | Bitwise_and
  7. | Bitwise_xor
  8. | Bitwise_or
  9. | Logical_and
  10. | Logical_or
type comparison =
  1. | Eq
  2. | Ne
  3. | Lt
  4. | Le
  5. | Gt
  6. | Ge

Compilation Contexts

A context encapsulates the state of a compilation. You can set up options on it, add types, functions and code, using the API below.

Invoking Context.compile on it gives you a result, representing in-memory machine-code.

You can call Context.compile repeatedly on one context, giving multiple independent results.

Similarly, you can call Context.compile_to_file on a context to compile to disk.

Eventually you can call Context.release to clean up the context; any in-memory results created from it are still usable.

val get_first_error : context -> string option
module Context : sig ... end

Fields

A field encapsulates a field within a struct; it is used when creating a struct type (using Struct.create). Fields cannot be shared between structs.

module Field : sig ... end

Structure Types

A struct_ encapsulates a struct type, either one that we have the layout for, or an opaque type.

You can model C struct types by creating struct_ and field instances, in either order:

module Struct : sig ... end

Types

module Type : sig ... end

Rvalues

A rvalue is an expression that can be computed.

It can be simple, e.g.:

or compound e.g.:

Every rvalue has an associated type, and the API will check to ensure that types match up correctly (otherwise the context will emit an error).

module RValue : sig ... end

Lvalues

An lvalue is something that can of the left-hand side of an assignment: a storage area (such as a variable). It is also usable as an rvalue, where the rvalue is computed by reading from the storage area.

module LValue : sig ... end

Parameters

A value of type param represents a parameter to a function.

module Param : sig ... end

Functions

A values of type function_ encapsulates a function: either one that you're creating yourself, or a reference to one that you're dynamically linking to within the rest of the process.

module Function : sig ... end

Basic Blocks

A block encapsulates a basic block of statements within a function (i.e. with one entry point and one exit point).

module Block : sig ... end

Source Locations

A location encapsulates a source code location, so that you can (optionally) associate locations in your language with statements in the JIT-compiled code, allowing the debugger to single-step through your language.

Faking it

If you don't have source code for your internal representation, but need to debug, you can generate a C-like representation of the functions in your context using Context.dump_to_file. This will dump C-like code to the given path. If the update_locations argument is true, this will also set up location information throughout the context, pointing at the dump file as if it were a source file, giving you something you can step through in the debugger.

module Location : sig ... end

In-memory compilation

module Result : sig ... end