2.4.2.1 Global variables

C Function: gcc_jit_lvalue * gcc_jit_context_new_global (gcc_jit_context *ctxt, gcc_jit_location *loc, enum gcc_jit_global_kind kind, gcc_jit_type *type, const char *name)

Add a new global variable of the given type and name to the context.

The parameter type must be non-void.

The parameter name must be non-NULL. The call takes a copy of the underlying string, so it is valid to pass in a pointer to an on-stack buffer.

The “kind” parameter determines the visibility of the “global” outside of the gcc_jit_result:

C Type: enum gcc_jit_global_kind
C Macro: GCC_JIT_GLOBAL_EXPORTED

Global is defined by the client code and is visible by name outside of this JIT context via gcc_jit_result_get_global() (and this value is required for the global to be accessible via that entrypoint).

C Macro: GCC_JIT_GLOBAL_INTERNAL

Global is defined by the client code, but is invisible outside of it. Analogous to a “static” global within a .c file. Specifically, the variable will only be visible within this context and within child contexts.

C Macro: GCC_JIT_GLOBAL_IMPORTED

Global is not defined by the client code; we’re merely referring to it. Analogous to using an “extern” global from a header file.

C Function: gcc_jit_lvalue * gcc_jit_global_set_initializer (gcc_jit_lvalue *global, const void *blob, size_t num_bytes)

Set an initializer for global using the memory content pointed by blob for num_bytes. global must be an array of an integral type. Return the global itself.

The parameter blob must be non-NULL. The call copies the memory pointed by blob for num_bytes bytes, so it is valid to pass in a pointer to an on-stack buffer. The content will be stored in the compilation unit and used as initialization value of the array.

This entrypoint was added in LIBGCCJIT_ABI_14; you can test for its presence using

#ifdef LIBGCCJIT_HAVE_gcc_jit_global_set_initializer
C Function: gcc_jit_lvalue * gcc_jit_global_set_initializer_rvalue (gcc_jit_lvalue *global, gcc_jit_rvalue *init_value)

Set the initial value of a global with an rvalue.

The rvalue needs to be a constant expression, e.g. no function calls.

The global can’t have the kind GCC_JIT_GLOBAL_IMPORTED.

As a non-comprehensive example it is OK to do the equivalent of:

int foo = 3 * 2; /* rvalue from gcc_jit_context_new_binary_op.  */
int arr[] = {1,2,3,4}; /* rvalue from gcc_jit_context_new_constructor.  */
int *bar = &arr[2] + 1; /* rvalue from nested "get address" of "array access".  */
const int baz = 3; /* rvalue from gcc_jit_context_rvalue_from_int.  */
int boz = baz; /* rvalue from gcc_jit_lvalue_as_rvalue.  */

Use together with gcc_jit_context_new_struct_constructor(), gcc_jit_context_new_union_constructor(), gcc_jit_context_new_array_constructor() to initialize structs, unions and arrays.

On success, returns the global parameter unchanged. Otherwise, NULL.

This entrypoint was added in LIBGCCJIT_ABI_19; you can test for its presence using:

#ifdef LIBGCCJIT_HAVE_CTORS