8 /* Quote from here in docs/topics/types.rst. */
17 create_code (gcc_jit_context
*ctxt
, void *user_data
)
19 /* Let's try to inject the equivalent of:
28 gcc_jit_type
*int_type
=
29 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
30 gcc_jit_type
*float_type
=
31 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_FLOAT
);
32 gcc_jit_field
*as_int
=
33 gcc_jit_context_new_field (ctxt
,
37 gcc_jit_field
*as_float
=
38 gcc_jit_context_new_field (ctxt
,
42 gcc_jit_field
*fields
[] = {as_int
, as_float
};
43 gcc_jit_type
*union_type
=
44 gcc_jit_context_new_union_type (ctxt
, NULL
,
45 "int_or_float", 2, fields
);
47 /* Build the test function. */
48 gcc_jit_param
*param_i
=
49 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "i");
50 gcc_jit_function
*test_fn
=
51 gcc_jit_context_new_function (ctxt
, NULL
,
52 GCC_JIT_FUNCTION_EXPORTED
,
59 gcc_jit_function_new_local (test_fn
, NULL
,
62 gcc_jit_block
*block
= gcc_jit_function_new_block (test_fn
, NULL
);
65 gcc_jit_block_add_assignment (
68 /* "u.as_int = ..." */
69 gcc_jit_lvalue_access_field (u
,
72 gcc_jit_param_as_rvalue (param_i
));
74 /* return u.as_float; */
75 gcc_jit_block_end_with_return (
77 gcc_jit_rvalue_access_field (gcc_jit_lvalue_as_rvalue (u
),
82 /* Quote up to here in docs/topics/types.rst. */
85 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
87 typedef float (*fn_type
) (int i
);
88 CHECK_NON_NULL (result
);
91 (fn_type
)gcc_jit_result_get_code (result
, "test_union");
92 CHECK_NON_NULL (test_union
);
94 /* Call the JIT-generated function. */
95 float f_result
= test_union (42);
98 u
.as_float
= f_result
;
100 CHECK_VALUE (u
.as_int
, 42);