17 signed char m_signed_char
;
18 unsigned char m_unsigned_char
;
21 unsigned short m_unsigned_short
;
24 unsigned int m_unsigned_int
;
27 unsigned long m_unsigned_long
;
29 long long m_long_long
;
30 unsigned long long m_unsigned_long_long
;
36 long double m_long_double
;
38 const char *m_const_char_ptr
;
46 int *test_ptr
= &test_int
;
48 const char *test_string
= "test_string";
51 create_code (gcc_jit_context
*ctxt
, void *user_data
)
53 /* Let's try to inject the equivalent of:
56 test_caller (struct zoo *z)
58 for each fields "m_field":
59 z->m_field = ...some data;
62 gcc_jit_type
*void_type
=
63 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_VOID
);
65 #define CREATE_FIELD(TYPE, NAME) \
66 gcc_jit_context_new_field ( \
68 gcc_jit_context_get_type (ctxt, TYPE), \
71 gcc_jit_field
*field_m_void_ptr
=
72 CREATE_FIELD (GCC_JIT_TYPE_VOID_PTR
, "m_void_ptr");
74 gcc_jit_field
*field_m_bool
=
75 CREATE_FIELD (GCC_JIT_TYPE_BOOL
, "m_bool");
77 gcc_jit_field
*field_m_char
=
78 CREATE_FIELD (GCC_JIT_TYPE_CHAR
, "m_char");
79 gcc_jit_field
*field_m_signed_char
=
80 CREATE_FIELD (GCC_JIT_TYPE_SIGNED_CHAR
, "m_signed_char");
81 gcc_jit_field
*field_m_unsigned_char
=
82 CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_CHAR
, "m_unsigned_char");
84 gcc_jit_field
*field_m_short
=
85 CREATE_FIELD (GCC_JIT_TYPE_SHORT
, "m_short");
86 gcc_jit_field
*field_m_unsigned_short
=
87 CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_SHORT
, "m_unsigned_short");
89 gcc_jit_field
*field_m_int
=
90 CREATE_FIELD (GCC_JIT_TYPE_INT
, "m_int");
91 gcc_jit_field
*field_m_unsigned_int
=
92 CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_INT
, "m_unsigned_int");
94 gcc_jit_field
*field_m_long
=
95 CREATE_FIELD (GCC_JIT_TYPE_LONG
, "m_long");
96 gcc_jit_field
*field_m_unsigned_long
=
97 CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_LONG
, "m_unsigned_long");
99 gcc_jit_field
*field_m_long_long
=
100 CREATE_FIELD (GCC_JIT_TYPE_LONG_LONG
, "m_long_long");
101 gcc_jit_field
*field_m_unsigned_long_long
=
102 CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_LONG_LONG
, "m_unsigned_long_long");
104 /* Signed int type with sizeof (int): */
105 gcc_jit_type
*sized_int_type
=
106 gcc_jit_context_get_int_type (ctxt
, sizeof (int), 1);
107 gcc_jit_field
*field_m_sized_int_type
=
108 gcc_jit_context_new_field (
109 ctxt
, NULL
, sized_int_type
, "m_sized_int_type");
111 gcc_jit_field
*field_m_float
=
112 CREATE_FIELD (GCC_JIT_TYPE_FLOAT
, "m_float");
113 gcc_jit_field
*field_m_double
=
114 CREATE_FIELD (GCC_JIT_TYPE_DOUBLE
, "m_double");
115 gcc_jit_field
*field_m_long_double
=
116 CREATE_FIELD (GCC_JIT_TYPE_LONG_DOUBLE
, "m_long_double");
118 gcc_jit_field
*field_m_const_char_ptr
=
119 CREATE_FIELD (GCC_JIT_TYPE_CONST_CHAR_PTR
, "m_const_char_ptr");
121 gcc_jit_field
*field_m_size_t
=
122 CREATE_FIELD (GCC_JIT_TYPE_SIZE_T
, "m_size_t");
124 gcc_jit_field
*field_m_FILE_ptr
=
125 CREATE_FIELD (GCC_JIT_TYPE_FILE_PTR
, "m_FILE_ptr");
129 gcc_jit_field
*zoo_fields
[] = {
136 field_m_unsigned_char
,
139 field_m_unsigned_short
,
142 field_m_unsigned_int
,
145 field_m_unsigned_long
,
148 field_m_unsigned_long_long
,
150 field_m_sized_int_type
,
156 field_m_const_char_ptr
,
163 gcc_jit_type
*zoo_type
=
164 gcc_jit_struct_as_type (
165 gcc_jit_context_new_struct_type (
169 sizeof (zoo_fields
) / sizeof (zoo_fields
[0]),
172 gcc_jit_type
*zoo_ptr_type
=
173 gcc_jit_type_get_pointer (zoo_type
);
175 /* Build the test_fn. */
176 gcc_jit_param
*param_z
=
177 gcc_jit_context_new_param (ctxt
, NULL
, zoo_ptr_type
, "z");
178 gcc_jit_function
*test_fn
=
179 gcc_jit_context_new_function (ctxt
, NULL
,
180 GCC_JIT_FUNCTION_EXPORTED
,
185 gcc_jit_block
*block
= gcc_jit_function_new_block (test_fn
, NULL
);
187 /* Write to the various fields of param "z". */
188 #define ASSIGN(FIELD, EXPR) \
189 gcc_jit_block_add_assignment ( \
191 gcc_jit_rvalue_dereference_field ( \
192 gcc_jit_param_as_rvalue (param_z), \
199 gcc_jit_context_new_rvalue_from_ptr (
201 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_VOID_PTR
),
205 gcc_jit_context_new_rvalue_from_int (
207 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_BOOL
), 1))
210 gcc_jit_context_new_rvalue_from_int (
212 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_CHAR
),
214 ASSIGN(field_m_signed_char
,
215 gcc_jit_context_new_rvalue_from_int (
217 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_SIGNED_CHAR
),
219 ASSIGN(field_m_unsigned_char
,
220 gcc_jit_context_new_rvalue_from_int (
222 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_UNSIGNED_CHAR
),
225 ASSIGN(field_m_short
,
226 gcc_jit_context_new_rvalue_from_int (
228 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_SHORT
),
230 ASSIGN(field_m_unsigned_short
,
231 gcc_jit_context_new_rvalue_from_int (
233 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_UNSIGNED_SHORT
),
237 gcc_jit_context_new_rvalue_from_int (
239 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
),
241 ASSIGN(field_m_unsigned_int
,
242 gcc_jit_context_new_rvalue_from_int (
244 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_UNSIGNED_INT
),
248 gcc_jit_context_new_rvalue_from_int (
250 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_LONG
),
252 ASSIGN(field_m_unsigned_long
,
253 gcc_jit_context_new_rvalue_from_int (
255 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_UNSIGNED_LONG
),
258 ASSIGN(field_m_long_long
,
259 gcc_jit_context_new_rvalue_from_int (
261 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_LONG_LONG
),
263 ASSIGN(field_m_unsigned_long_long
,
264 gcc_jit_context_new_rvalue_from_int (
266 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_UNSIGNED_LONG_LONG
),
269 ASSIGN(field_m_sized_int_type
,
270 gcc_jit_context_new_rvalue_from_int (
272 sized_int_type
, 500))
274 ASSIGN(field_m_float
,
275 gcc_jit_context_new_rvalue_from_double (
277 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_FLOAT
),
279 ASSIGN(field_m_double
,
280 gcc_jit_context_new_rvalue_from_double (
282 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_DOUBLE
),
284 ASSIGN(field_m_long_double
,
285 gcc_jit_context_new_rvalue_from_double (
287 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_LONG_DOUBLE
),
290 ASSIGN(field_m_const_char_ptr
,
291 gcc_jit_context_new_rvalue_from_ptr (
293 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_CONST_CHAR_PTR
),
294 (char *)test_string
))
296 ASSIGN(field_m_size_t
,
297 gcc_jit_context_new_rvalue_from_int (
299 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_SIZE_T
),
300 sizeof (struct zoo
)))
302 ASSIGN(field_m_FILE_ptr
,
303 gcc_jit_context_new_rvalue_from_ptr (
305 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_FILE_PTR
),
310 gcc_jit_block_end_with_void_return (block
, NULL
);
314 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
316 typedef void (*fn_type
) (struct zoo
*);
317 CHECK_NON_NULL (result
);
320 (fn_type
)gcc_jit_result_get_code (result
, "test_types");
321 CHECK_NON_NULL (test_types
);
324 memset (&z
, 0xf0, sizeof (z
));
326 /* Call the JIT-generated function. */
329 /* Verify that it correctly wrote to the various fields. */
330 CHECK_VALUE (z
.m_void_ptr
, test_ptr
);
332 CHECK_VALUE (z
.m_bool
, true);
334 CHECK_VALUE (z
.m_char
, 'V');
335 CHECK_VALUE (z
.m_signed_char
, -37);
336 CHECK_VALUE (z
.m_unsigned_char
, 200);
338 CHECK_VALUE (z
.m_short
, -900);
339 CHECK_VALUE (z
.m_unsigned_short
, 0x3000);
341 CHECK_VALUE (z
.m_int
, -0x2000);
342 CHECK_VALUE (z
.m_unsigned_int
, 1234567);
344 CHECK_VALUE (z
.m_long
, -5);
345 CHECK_VALUE (z
.m_unsigned_long
, 12345678);
347 CHECK_VALUE (z
.m_long_long
, -42);
348 CHECK_VALUE (z
.m_unsigned_long_long
, 123456789);
350 CHECK_VALUE (z
.m_sized_int_type
, 500);
352 CHECK_VALUE (z
.m_float
, 3.141f
);
353 CHECK_VALUE (z
.m_double
, 3.141);
354 CHECK_VALUE (z
.m_long_double
, 3.141);
356 CHECK_VALUE (z
.m_const_char_ptr
, test_string
);
358 CHECK_VALUE (z
.m_size_t
, sizeof (struct zoo
));
360 CHECK_VALUE (z
.m_FILE_ptr
, stderr
);