gccjit.c

A pure C API to enable client code to embed GCC as a JIT-compiler.

This file has been modified from the libgccjit.h header to work with the D compiler. The original file is part of the GCC distribution and is licensed under the following terms.

Copyright (C) 2013-2015 Free Software Foundation, Inc.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Members

Aliases

gcc_jit_binary_op
alias gcc_jit_binary_op = uint
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_bool_option
alias gcc_jit_bool_option = uint

Options taking boolean values. These all default to "false".

gcc_jit_comparison
alias gcc_jit_comparison = uint

(Comparisons are treated as separate from "binary_op" to save you having to specify the result_type).

gcc_jit_function_kind
alias gcc_jit_function_kind = uint

Kinds of function.

gcc_jit_global_kind
alias gcc_jit_global_kind = uint

lvalues, rvalues and expressions.

gcc_jit_int_option
alias gcc_jit_int_option = uint

Options taking int values.

gcc_jit_output_kind
alias gcc_jit_output_kind = uint

Kinds of ahead-of-time compilation, for use with gcc_jit_context_compile_to_file.

gcc_jit_str_option
alias gcc_jit_str_option = uint

Options taking string values.

gcc_jit_types
alias gcc_jit_types = uint

Access to specific types.

gcc_jit_unary_op
alias gcc_jit_unary_op = uint
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.

Enums

GCC_JIT_BINARY_OP_PLUS
anonymousenum GCC_JIT_BINARY_OP_PLUS
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
GCC_JIT_BOOL_OPTION_DEBUGINFO
anonymousenum GCC_JIT_BOOL_OPTION_DEBUGINFO
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
GCC_JIT_COMPARISON_EQ
anonymousenum GCC_JIT_COMPARISON_EQ
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
GCC_JIT_FUNCTION_EXPORTED
anonymousenum GCC_JIT_FUNCTION_EXPORTED
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
GCC_JIT_GLOBAL_EXPORTED
anonymousenum GCC_JIT_GLOBAL_EXPORTED
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL
anonymousenum GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
GCC_JIT_OUTPUT_KIND_ASSEMBLER
anonymousenum GCC_JIT_OUTPUT_KIND_ASSEMBLER
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
GCC_JIT_STR_OPTION_PROGNAME
anonymousenum GCC_JIT_STR_OPTION_PROGNAME
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
GCC_JIT_TYPE_VOID
anonymousenum GCC_JIT_TYPE_VOID
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
GCC_JIT_UNARY_OP_MINUS
anonymousenum GCC_JIT_UNARY_OP_MINUS
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.

Functions

gcc_jit_block_add_assignment
void gcc_jit_block_add_assignment(gcc_jit_block* block, gcc_jit_location* loc, gcc_jit_lvalue* lvalue, gcc_jit_rvalue* rvalue)

Add evaluation of an rvalue, assigning the result to the given lvalue.

gcc_jit_block_add_assignment_op
void gcc_jit_block_add_assignment_op(gcc_jit_block* block, gcc_jit_location* loc, gcc_jit_lvalue* lvalue, gcc_jit_binary_op op, gcc_jit_rvalue* rvalue)

Add evaluation of an rvalue, using the result to modify an lvalue.

gcc_jit_block_add_comment
void gcc_jit_block_add_comment(gcc_jit_block* block, gcc_jit_location* loc, char* text)

Add a no-op textual comment to the internal representation of the code. It will be optimized away, but will be visible in the dumps seen via GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE and GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE, and thus may be of use when debugging how your project's internal representation gets converted to the libgccjit IR.

gcc_jit_block_add_eval
void gcc_jit_block_add_eval(gcc_jit_block* block, gcc_jit_location* loc, gcc_jit_rvalue* rvalue)

Add evaluation of an rvalue, discarding the result (e.g. a function call that "returns" void).

gcc_jit_block_as_object
gcc_jit_object* gcc_jit_block_as_object(gcc_jit_block* block)

Upcasting from block to object.

gcc_jit_block_end_with_conditional
void gcc_jit_block_end_with_conditional(gcc_jit_block* block, gcc_jit_location* loc, gcc_jit_rvalue* boolval, gcc_jit_block* on_true, gcc_jit_block* on_false)

Terminate a block by adding evaluation of an rvalue, branching on the result to the appropriate successor block.

gcc_jit_block_end_with_jump
void gcc_jit_block_end_with_jump(gcc_jit_block* block, gcc_jit_location* loc, gcc_jit_block* target)

Terminate a block by adding a jump to the given target block.

gcc_jit_block_end_with_return
void gcc_jit_block_end_with_return(gcc_jit_block* block, gcc_jit_location* loc, gcc_jit_rvalue* rvalue)

Terminate a block by adding evaluation of an rvalue, returning the value.

gcc_jit_block_end_with_void_return
void gcc_jit_block_end_with_void_return(gcc_jit_block* block, gcc_jit_location* loc)

Terminate a block by adding a valueless return, for use within a function with "void" return type.

gcc_jit_block_get_function
gcc_jit_function* gcc_jit_block_get_function(gcc_jit_block* block)

Which function is this block within?

gcc_jit_context_acquire
gcc_jit_context* gcc_jit_context_acquire()

Acquire a JIT-compilation context.

gcc_jit_context_compile
gcc_jit_result* gcc_jit_context_compile(gcc_jit_context* ctxt)

Compile the context to in-memory machine code.

gcc_jit_context_compile_to_file
void gcc_jit_context_compile_to_file(gcc_jit_context* ctxt, gcc_jit_output_kind output_kind, char* output_path)

Compile the context to a file of the given kind.

gcc_jit_context_dump_to_file
void gcc_jit_context_dump_to_file(gcc_jit_context* ctxt, char* path, int update_locations)

To help with debugging: dump a C-like representation to the given path, describing what's been set up on the context.

gcc_jit_context_get_builtin_function
gcc_jit_function* gcc_jit_context_get_builtin_function(gcc_jit_context* ctxt, char* name)

Create a reference to a builtin function (sometimes called intrinsic functions).

gcc_jit_context_get_first_error
const(char)* gcc_jit_context_get_first_error(gcc_jit_context* ctxt)

To be called after any API call, this gives the first error message that occurred on the context.

gcc_jit_context_get_int_type
gcc_jit_type* gcc_jit_context_get_int_type(gcc_jit_context* ctxt, int num_bytes, int is_signed)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_context_get_last_error
const(char)* gcc_jit_context_get_last_error(gcc_jit_context* ctxt)

To be called after any API call, this gives the last error message that occurred on the context.

gcc_jit_context_get_type
gcc_jit_type* gcc_jit_context_get_type(gcc_jit_context* ctxt, gcc_jit_types type_)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_context_new_array_access
gcc_jit_lvalue* gcc_jit_context_new_array_access(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_rvalue* ptr, gcc_jit_rvalue* index)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_context_new_array_type
gcc_jit_type* gcc_jit_context_new_array_type(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_type* element_type, int num_elements)

Given type "T", get type "TN" (for a constant N).

gcc_jit_context_new_binary_op
gcc_jit_rvalue* gcc_jit_context_new_binary_op(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_binary_op op, gcc_jit_type* result_type, gcc_jit_rvalue* a, gcc_jit_rvalue* b)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_context_new_call
gcc_jit_rvalue* gcc_jit_context_new_call(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_function* func, int numargs, gcc_jit_rvalue** args)

Call of a specific function.

gcc_jit_context_new_call_through_ptr
gcc_jit_rvalue* gcc_jit_context_new_call_through_ptr(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_rvalue* fn_ptr, int numargs, gcc_jit_rvalue** args)

Call through a function pointer.

gcc_jit_context_new_cast
gcc_jit_rvalue* gcc_jit_context_new_cast(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_rvalue* rvalue, gcc_jit_type* type)

Type-coercion.

gcc_jit_context_new_child_context
gcc_jit_context* gcc_jit_context_new_child_context(gcc_jit_context* parent_ctxt)

Given an existing JIT context, create a child context.

gcc_jit_context_new_comparison
gcc_jit_rvalue* gcc_jit_context_new_comparison(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_comparison op, gcc_jit_rvalue* a, gcc_jit_rvalue* b)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_context_new_field
gcc_jit_field* gcc_jit_context_new_field(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_type* type, char* name)

Struct-handling.

gcc_jit_context_new_function
gcc_jit_function* gcc_jit_context_new_function(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_function_kind kind, gcc_jit_type* return_type, char* name, int num_params, gcc_jit_param** params, int is_variadic)

Create a function.

gcc_jit_context_new_function_ptr_type
gcc_jit_type* gcc_jit_context_new_function_ptr_type(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_type* return_type, int num_params, gcc_jit_type** param_types, int is_variadic)

Function pointers.

gcc_jit_context_new_global
gcc_jit_lvalue* gcc_jit_context_new_global(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_global_kind kind, gcc_jit_type* type, char* name)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_context_new_location
gcc_jit_location* gcc_jit_context_new_location(gcc_jit_context* ctxt, char* filename, int line, int column)

Creating source code locations for use by the debugger. Line and column numbers are 1-based.

gcc_jit_context_new_opaque_struct
gcc_jit_struct* gcc_jit_context_new_opaque_struct(gcc_jit_context* ctxt, gcc_jit_location* loc, char* name)

Create an opaque struct type.

gcc_jit_context_new_param
gcc_jit_param* gcc_jit_context_new_param(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_type* type, char* name)

Create a function param.

gcc_jit_context_new_rvalue_from_double
gcc_jit_rvalue* gcc_jit_context_new_rvalue_from_double(gcc_jit_context* ctxt, gcc_jit_type* numeric_type, double value)

Floating-point constants.

gcc_jit_context_new_rvalue_from_int
gcc_jit_rvalue* gcc_jit_context_new_rvalue_from_int(gcc_jit_context* ctxt, gcc_jit_type* numeric_type, int value)

Integer constants.

gcc_jit_context_new_rvalue_from_long
gcc_jit_rvalue* gcc_jit_context_new_rvalue_from_long(gcc_jit_context* ctxt, gcc_jit_type* numeric_type, long value)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_context_new_rvalue_from_ptr
gcc_jit_rvalue* gcc_jit_context_new_rvalue_from_ptr(gcc_jit_context* ctxt, gcc_jit_type* pointer_type, void* value)

Pointers.

gcc_jit_context_new_string_literal
gcc_jit_rvalue* gcc_jit_context_new_string_literal(gcc_jit_context* ctxt, char* value)

String literals.

gcc_jit_context_new_struct_type
gcc_jit_struct* gcc_jit_context_new_struct_type(gcc_jit_context* ctxt, gcc_jit_location* loc, char* name, int num_fields, gcc_jit_field** fields)

Create a struct type from an array of fields.

gcc_jit_context_new_unary_op
gcc_jit_rvalue* gcc_jit_context_new_unary_op(gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_unary_op op, gcc_jit_type* result_type, gcc_jit_rvalue* rvalue)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_context_new_union_type
gcc_jit_type* gcc_jit_context_new_union_type(gcc_jit_context* ctxt, gcc_jit_location* loc, char* name, int num_fields, gcc_jit_field** fields)

Unions work similarly to structs.

gcc_jit_context_null
gcc_jit_rvalue* gcc_jit_context_null(gcc_jit_context* ctxt, gcc_jit_type* pointer_type)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_context_one
gcc_jit_rvalue* gcc_jit_context_one(gcc_jit_context* ctxt, gcc_jit_type* numeric_type)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_context_release
void gcc_jit_context_release(gcc_jit_context* ctxt)

Release the context. After this call, it's no longer valid to use the ctxt.

gcc_jit_context_set_bool_option
void gcc_jit_context_set_bool_option(gcc_jit_context* ctxt, gcc_jit_bool_option opt, int value)

Set a boolean option on the given context.

gcc_jit_context_set_int_option
void gcc_jit_context_set_int_option(gcc_jit_context* ctxt, gcc_jit_int_option opt, int value)

Set an int option on the given context.

gcc_jit_context_set_logfile
void gcc_jit_context_set_logfile(gcc_jit_context* ctxt, FILE* logfile, int flags, int verbosity)

To help with debugging; enable ongoing logging of the context's activity to the given FILE *.

gcc_jit_context_set_str_option
void gcc_jit_context_set_str_option(gcc_jit_context* ctxt, gcc_jit_str_option opt, char* value)

Set a string option on the given context.

gcc_jit_context_zero
gcc_jit_rvalue* gcc_jit_context_zero(gcc_jit_context* ctxt, gcc_jit_type* numeric_type)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_field_as_object
gcc_jit_object* gcc_jit_field_as_object(gcc_jit_field* field)

Upcasting from field to object.

gcc_jit_function_as_object
gcc_jit_object* gcc_jit_function_as_object(gcc_jit_function* func)

Upcasting from function to object.

gcc_jit_function_dump_to_dot
void gcc_jit_function_dump_to_dot(gcc_jit_function* func, char* path)

Emit the function in graphviz format.

gcc_jit_function_get_param
gcc_jit_param* gcc_jit_function_get_param(gcc_jit_function* func, int index)

Get a specific param of a function by index.

gcc_jit_function_new_block
gcc_jit_block* gcc_jit_function_new_block(gcc_jit_function* func, char* name)

Create a block.

gcc_jit_function_new_local
gcc_jit_lvalue* gcc_jit_function_new_local(gcc_jit_function* func, gcc_jit_location* loc, gcc_jit_type* type, char* name)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_location_as_object
gcc_jit_object* gcc_jit_location_as_object(gcc_jit_location* loc)

Upcasting from location to object.

gcc_jit_lvalue_access_field
gcc_jit_lvalue* gcc_jit_lvalue_access_field(gcc_jit_lvalue* struct_or_union, gcc_jit_location* loc, gcc_jit_field* field)

Accessing a field of an lvalue of struct type, analogous to: (EXPR).field = ...; in C.

gcc_jit_lvalue_as_object
gcc_jit_object* gcc_jit_lvalue_as_object(gcc_jit_lvalue* lvalue)

Upcasting.

gcc_jit_lvalue_as_rvalue
gcc_jit_rvalue* gcc_jit_lvalue_as_rvalue(gcc_jit_lvalue* lvalue)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_lvalue_get_address
gcc_jit_rvalue* gcc_jit_lvalue_get_address(gcc_jit_lvalue* lvalue, gcc_jit_location* loc)

Taking the address of an lvalue; analogous to: &(EXPR) in C.

gcc_jit_object_get_context
gcc_jit_context* gcc_jit_object_get_context(gcc_jit_object* obj)

Which context is "obj" within?

gcc_jit_object_get_debug_string
const(char)* gcc_jit_object_get_debug_string(gcc_jit_object* obj)

Get a human-readable description of this object. The string buffer is created the first time this is called on a given object, and persists until the object's context is released.

gcc_jit_param_as_lvalue
gcc_jit_lvalue* gcc_jit_param_as_lvalue(gcc_jit_param* param)

Upcasting from param to lvalue.

gcc_jit_param_as_object
gcc_jit_object* gcc_jit_param_as_object(gcc_jit_param* param)

Upcasting from param to object.

gcc_jit_param_as_rvalue
gcc_jit_rvalue* gcc_jit_param_as_rvalue(gcc_jit_param* param)

Upcasting from param to rvalue.

gcc_jit_result_get_code
void* gcc_jit_result_get_code(gcc_jit_result* result, char* funcname)

Locate a given function within the built machine code. This will need to be cast to a function pointer of the correct type before it can be called.

gcc_jit_result_get_global
void* gcc_jit_result_get_global(gcc_jit_result* result, char* name)

Locate a given global within the built machine code. It must have been created using GCC_JIT_GLOBAL_EXPORTED. This is a ptr to the global, so e.g. for an int this is an int *.

gcc_jit_result_release
void gcc_jit_result_release(gcc_jit_result* result)

Once we're done with the code, this unloads the built .so file. This cleans up the result; after calling this, it's no longer valid to use the result.

gcc_jit_rvalue_access_field
gcc_jit_rvalue* gcc_jit_rvalue_access_field(gcc_jit_rvalue* struct_or_union, gcc_jit_location* loc, gcc_jit_field* field)

Accessing a field of an rvalue of struct type, analogous to: (EXPR).field in C.

gcc_jit_rvalue_as_object
gcc_jit_object* gcc_jit_rvalue_as_object(gcc_jit_rvalue* rvalue)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_rvalue_dereference
gcc_jit_lvalue* gcc_jit_rvalue_dereference(gcc_jit_rvalue* rvalue, gcc_jit_location* loc)

Dereferencing a pointer; analogous to: *(EXPR)

gcc_jit_rvalue_dereference_field
gcc_jit_lvalue* gcc_jit_rvalue_dereference_field(gcc_jit_rvalue* ptr, gcc_jit_location* loc, gcc_jit_field* field)

Accessing a field of an rvalue of pointer type, analogous to: (EXPR)->field in C, itself equivalent to (*EXPR).FIELD

gcc_jit_rvalue_get_type
gcc_jit_type* gcc_jit_rvalue_get_type(gcc_jit_rvalue* rvalue)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
gcc_jit_struct_as_type
gcc_jit_type* gcc_jit_struct_as_type(gcc_jit_struct* struct_type)

Upcast a struct to a type.

gcc_jit_struct_set_fields
void gcc_jit_struct_set_fields(gcc_jit_struct* struct_type, gcc_jit_location* loc, int num_fields, gcc_jit_field** fields)

Populating the fields of a formerly-opaque struct type. This can only be called once on a given struct type.

gcc_jit_type_as_object
gcc_jit_object* gcc_jit_type_as_object(gcc_jit_type* type)

Upcasting from type to object.

gcc_jit_type_get_const
gcc_jit_type* gcc_jit_type_get_const(gcc_jit_type* type)

Given type "T", get type "const T".

gcc_jit_type_get_pointer
gcc_jit_type* gcc_jit_type_get_pointer(gcc_jit_type* type)

Given type "T", get type "T*".

gcc_jit_type_get_volatile
gcc_jit_type* gcc_jit_type_get_volatile(gcc_jit_type* type)

Given type "T", get type "volatile T".

Structs

gcc_jit_block
struct gcc_jit_block

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

gcc_jit_context
struct gcc_jit_context

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

gcc_jit_field
struct gcc_jit_field

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

gcc_jit_function
struct gcc_jit_function

A gcc_jit_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.

gcc_jit_location
struct gcc_jit_location

A gcc_jit_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.

gcc_jit_lvalue
struct gcc_jit_lvalue

A gcc_jit_lvalue is a storage location within your code (e.g. a variable, a parameter, etc). It is also a gcc_jit_rvalue; use gcc_jit_lvalue_as_rvalue to cast.

gcc_jit_object
struct gcc_jit_object

An object created within a context. Such objects are automatically cleaned up when the context is released.

gcc_jit_param
struct gcc_jit_param

A gcc_jit_param is a function parameter, used when creating a gcc_jit_function. It is also a gcc_jit_lvalue (and thus also an rvalue); use gcc_jit_param_as_lvalue to convert.

gcc_jit_result
struct gcc_jit_result

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

gcc_jit_rvalue
struct gcc_jit_rvalue

A gcc_jit_rvalue is an expression within your code, with some type.

gcc_jit_struct
struct gcc_jit_struct

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

gcc_jit_type
struct gcc_jit_type

A gcc_jit_type encapsulates a type e.g. "int" or a "struct foo*".

Meta