Table Of Contents

Previous topic

Objects

Next topic

Expressions

This Page

Types

gcc_jit_object *gcc_jit_type_as_object (gcc_jit_type *type)
Upcast a type to an object.

Types can be created in several ways:

  • fundamental types can be accessed using gcc_jit_context_get_type():

    gcc_jit_type *int_type = gcc_jit_context_get_type (GCC_JIT_TYPE_INT);
    

    See gcc_jit_context_get_type() for the available types.

  • derived types can be accessed by using functions such as gcc_jit_type_get_pointer() and gcc_jit_type_get_const():

    gcc_jit_type *const_int_star = gcc_jit_type_get_pointer (gcc_jit_type_get_const (int_type));
    gcc_jit_type *int_const_star = gcc_jit_type_get_const (gcc_jit_type_get_pointer (int_type));
    
  • by creating structures (see below).

Standard types

gcc_jit_type *gcc_jit_context_get_type (gcc_jit_context *ctxt, \
enum gcc_jit_types type_)

Access a specific type. The available types are:

enum gcc_jit_types value Meaning
:c:data:`GCC_JIT_TYPE_VOID` C’s void type.
:c:data:`GCC_JIT_TYPE_VOID_PTR` C’s void *.
:c:data:`GCC_JIT_TYPE_BOOL` C++’s bool type; also C99’s _Bool type, aka bool if using stdbool.h.
:c:data:`GCC_JIT_TYPE_CHAR` C’s char (of some signedness)
:c:data:`GCC_JIT_TYPE_SIGNED_CHAR` C’s signed char
:c:data:`GCC_JIT_TYPE_UNSIGNED_CHAR` C’s unsigned char
:c:data:`GCC_JIT_TYPE_SHORT` C’s short (signed)
:c:data:`GCC_JIT_TYPE_UNSIGNED_SHORT` C’s unsigned short
:c:data:`GCC_JIT_TYPE_INT` C’s int (signed)
:c:data:`GCC_JIT_TYPE_UNSIGNED_INT` C’s unsigned int
:c:data:`GCC_JIT_TYPE_LONG` C’s long (signed)
:c:data:`GCC_JIT_TYPE_UNSIGNED_LONG` C’s unsigned long
:c:data:`GCC_JIT_TYPE_LONG_LONG` C99’s long long (signed)
:c:data:`GCC_JIT_TYPE_UNSIGNED_LONG_LONG` C99’s unsigned long long
:c:data:`GCC_JIT_TYPE_FLOAT`  
:c:data:`GCC_JIT_TYPE_DOUBLE`  
:c:data:`GCC_JIT_TYPE_LONG_DOUBLE`  
:c:data:`GCC_JIT_TYPE_CONST_CHAR_PTR` C type: (const char *)
:c:data:`GCC_JIT_TYPE_SIZE_T` C’s size_t type
:c:data:`GCC_JIT_TYPE_FILE_PTR` C type: (FILE *)
:c:data:`GCC_JIT_TYPE_COMPLEX_FLOAT` C99’s _Complex float
:c:data:`GCC_JIT_TYPE_COMPLEX_DOUBLE` C99’s _Complex double
:c:data:`GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE` C99’s _Complex long double
gcc_jit_type *\
gcc_jit_context_get_int_type (gcc_jit_context *ctxt, \
int num_bytes, int is_signed)
Access the integer type of the given size.

Pointers, const, and volatile

gcc_jit_type *gcc_jit_type_get_pointer (gcc_jit_type *type)
Given type “T”, get type “T*”.
gcc_jit_type *gcc_jit_type_get_const (gcc_jit_type *type)
Given type “T”, get type “const T”.
gcc_jit_type *gcc_jit_type_get_volatile (gcc_jit_type *type)
Given type “T”, get type “volatile T”.
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 “T[N]” (for a constant N).

Structures and unions

A compound type analagous to a C struct.

A field within a :c:type:`gcc_jit_struct`.

You can model C struct types by creating :c:type:`gcc_jit_struct *` and :c:type:`gcc_jit_field` instances, in either order:

  • by creating the fields, then the structure. For example, to model:

    struct coord {double x; double y; };
    

    you could call:

    gcc_jit_field *field_x =
      gcc_jit_context_new_field (ctxt, NULL, double_type, "x");
    gcc_jit_field *field_y =
      gcc_jit_context_new_field (ctxt, NULL, double_type, "y");
    gcc_jit_field *fields[2] = {field_x, field_y};
    gcc_jit_struct *coord =
      gcc_jit_context_new_struct_type (ctxt, NULL, "coord", 2, fields);
    
  • by creating the structure, then populating it with fields, typically to allow modelling self-referential structs such as:

    struct node { int m_hash; struct node *m_next; };
    

    like this:

    gcc_jit_type *node =
      gcc_jit_context_new_opaque_struct (ctxt, NULL, "node");
    gcc_jit_type *node_ptr =
      gcc_jit_type_get_pointer (node);
    gcc_jit_field *field_hash =
      gcc_jit_context_new_field (ctxt, NULL, int_type, "m_hash");
    gcc_jit_field *field_next =
      gcc_jit_context_new_field (ctxt, NULL, node_ptr, "m_next");
    gcc_jit_field *fields[2] = {field_hash, field_next};
    gcc_jit_struct_set_fields (node, NULL, 2, fields);
    
gcc_jit_field *\
gcc_jit_context_new_field (gcc_jit_context *ctxt,\
gcc_jit_location *loc,\
gcc_jit_type *type,\
const char *name)

Construct a new field, with the given type and name.

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.

gcc_jit_object *\
gcc_jit_field_as_object(gcc_jit_field *field)
Upcast from field to object.
gcc_jit_struct *\
gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,\
gcc_jit_location *loc,\
const char *name,\
int num_fields,\
gcc_jit_field **fields)

Construct a new struct type, with the given name and fields.

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.

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

Construct a new struct type, with the given name, but without specifying the fields. The fields can be omitted (in which case the size of the struct is not known), or later specified using :c:func:`gcc_jit_struct_set_fields`.

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.

gcc_jit_type *\
gcc_jit_struct_as_type(gcc_jit_struct *struct_type)
Upcast from struct to type.
void\
gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,\
gcc_jit_location *loc,\
int num_fields,\
gcc_jit_field **fields)

Populate the fields of a formerly-opaque struct type.

This can only be called once on a given struct type.

gcc_jit_type *\
gcc_jit_context_new_union_type (gcc_jit_context *ctxt,\
gcc_jit_location *loc,\
const char *name,\
int num_fields,\
gcc_jit_field **fields)

Construct a new union type, with the given name and fields.

The parameter name must be non-NULL. It is copied, so the input buffer does not need to outlive the call.

Example of use:

union int_or_float
{
  int as_int;
  float as_float;
};

void
create_code (gcc_jit_context *ctxt, void *user_data)
{
  /* Let's try to inject the equivalent of:
     float
     test_union (int i)
     {
        union int_or_float u;
	u.as_int = i;
	return u.as_float;
     }
  */
  gcc_jit_type *int_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  gcc_jit_type *float_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT);
  gcc_jit_field *as_int =
    gcc_jit_context_new_field (ctxt,
                               NULL,
                               int_type,
                               "as_int");
  gcc_jit_field *as_float =
    gcc_jit_context_new_field (ctxt,
                               NULL,
                               float_type,
                               "as_float");
  gcc_jit_field *fields[] = {as_int, as_float};
  gcc_jit_type *union_type =
    gcc_jit_context_new_union_type (ctxt, NULL,
				    "int_or_float", 2, fields);

  /* Build the test function.  */
  gcc_jit_param *param_i =
    gcc_jit_context_new_param (ctxt, NULL, int_type, "i");
  gcc_jit_function *test_fn =
    gcc_jit_context_new_function (ctxt, NULL,
                                  GCC_JIT_FUNCTION_EXPORTED,
                                  float_type,
                                  "test_union",
                                  1, &param_i,
                                  0);

  gcc_jit_lvalue *u =
    gcc_jit_function_new_local (test_fn, NULL,
				union_type, "u");

  gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);

  /* u.as_int = i; */
  gcc_jit_block_add_assignment (
    block,
    NULL,
    /* "u.as_int = ..." */
    gcc_jit_lvalue_access_field (u,
				 NULL,
				 as_int),
    gcc_jit_param_as_rvalue (param_i));

  /* return u.as_float; */
  gcc_jit_block_end_with_return (
    block, NULL,
    gcc_jit_rvalue_access_field (gcc_jit_lvalue_as_rvalue (u),
				 NULL,
				 as_float));
}