15 create_code (gcc_jit_context
*ctxt
, void *user_data
)
17 /* Let's try to inject the equivalent of:
20 test_reading (const struct bar *f)
31 return test_reading (&tmp);
34 gcc_jit_type
*int_type
=
35 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
37 gcc_jit_context_new_field (ctxt
,
42 gcc_jit_context_new_field (ctxt
,
46 gcc_jit_field
*fields
[] = {x
, y
};
47 gcc_jit_type
*struct_type
=
48 gcc_jit_struct_as_type (
49 gcc_jit_context_new_struct_type (ctxt
, NULL
, "bar", 2, fields
));
50 gcc_jit_type
*const_struct_type
= gcc_jit_type_get_const (struct_type
);
51 gcc_jit_type
*ptr_type
= gcc_jit_type_get_pointer (const_struct_type
);
53 /* Build "test_reading". */
54 gcc_jit_param
*param_f
=
55 gcc_jit_context_new_param (ctxt
, NULL
, ptr_type
, "f");
56 gcc_jit_function
*fn_test_reading
=
57 gcc_jit_context_new_function (ctxt
, NULL
,
58 GCC_JIT_FUNCTION_EXPORTED
,
64 /* return f->x * f->y; */
65 gcc_jit_block
*reading_block
= gcc_jit_function_new_block (fn_test_reading
, NULL
);
66 gcc_jit_block_end_with_return (
69 gcc_jit_context_new_binary_op (
71 GCC_JIT_BINARY_OP_MULT
,
73 gcc_jit_lvalue_as_rvalue (
74 gcc_jit_rvalue_dereference_field (
75 gcc_jit_param_as_rvalue (param_f
),
78 gcc_jit_lvalue_as_rvalue (
79 gcc_jit_rvalue_dereference_field (
80 gcc_jit_param_as_rvalue (param_f
),
84 /* Build "test_writing". */
85 gcc_jit_function
*fn_test_writing
=
86 gcc_jit_context_new_function (ctxt
, NULL
,
87 GCC_JIT_FUNCTION_EXPORTED
,
94 gcc_jit_lvalue
*local_tmp
=
95 gcc_jit_function_new_local (fn_test_writing
, NULL
,
99 gcc_jit_block
*writing_block
= gcc_jit_function_new_block (fn_test_writing
, NULL
);
100 gcc_jit_block_add_assignment (
102 gcc_jit_lvalue_access_field (local_tmp
, NULL
, x
),
103 gcc_jit_context_new_rvalue_from_int (ctxt
, int_type
, 5));
106 gcc_jit_block_add_assignment (
108 gcc_jit_lvalue_access_field (local_tmp
, NULL
, y
),
109 gcc_jit_context_new_rvalue_from_int (ctxt
, int_type
, 7));
111 /* return test_reading (&tmp); */
112 gcc_jit_rvalue
*arg
= gcc_jit_lvalue_get_address (local_tmp
, NULL
);
113 gcc_jit_block_end_with_return (
116 gcc_jit_context_new_call (
123 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
125 typedef int (*fn_type
) (void);
126 CHECK_NON_NULL (result
);
128 fn_type test_writing
=
129 (fn_type
)gcc_jit_result_get_code (result
, "test_writing");
130 CHECK_NON_NULL (test_writing
);
132 /* Verify that the code correctly returns the product of the fields. */
133 CHECK_VALUE (test_writing (), 35);