Debian Codegraph @

gcc/testsuite/jit.dg/test-types.c

raw
1 #include <immintrin.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stddef.h>
6 #include <stdbool.h>
7
8 #include "libgccjit.h"
9
10 #include "harness.h"
11
12 struct zoo
13 {
14 void *m_void_ptr;
15
16 bool m_bool;
17
18 char m_char;
19 signed char m_signed_char;
20 unsigned char m_unsigned_char;
21
22 short m_short;
23 unsigned short m_unsigned_short;
24
25 int m_int;
26 unsigned int m_unsigned_int;
27
28 long m_long;
29 unsigned long m_unsigned_long;
30
31 long long m_long_long;
32 unsigned long long m_unsigned_long_long;
33
34 uint8_t m_u8;
35 uint16_t m_u16;
36 uint32_t m_u32;
37 uint64_t m_u64;
38
39 int8_t m_i8;
40 int16_t m_i16;
41 int32_t m_i32;
42 int64_t m_i64;
43
44 #ifdef __SIZEOF_INT128__
45 __uint128_t m_u128;
46 __int128_t m_i128;
47 #endif
48
49 int m_sized_int_type;
50
51 float m_float;
52 double m_double;
53 long double m_long_double;
54
55 const char *m_const_char_ptr;
56
57 size_t m_size_t;
58
59 FILE *m_FILE_ptr;
60 };
61
62 int test_int = 42;
63 int *test_ptr = &test_int;
64
65 const char *test_string = "test_string";
66
67 void
68 create_code (gcc_jit_context *ctxt, void *user_data)
69 {
70 /* Let's try to inject the equivalent of:
71
72 void
73 test_caller (struct zoo *z)
74 {
75 for each fields "m_field":
76 z->m_field = ...some data;
77 }
78 */
79 gcc_jit_type *void_type =
80 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
81
82 #define CREATE_FIELD(TYPE, NAME) \
83 gcc_jit_context_new_field ( \
84 ctxt, NULL, \
85 gcc_jit_context_get_type (ctxt, TYPE), \
86 NAME)
87
88 gcc_jit_field *field_m_void_ptr =
89 CREATE_FIELD (GCC_JIT_TYPE_VOID_PTR, "m_void_ptr");
90
91 gcc_jit_field *field_m_bool =
92 CREATE_FIELD (GCC_JIT_TYPE_BOOL, "m_bool");
93
94 gcc_jit_field *field_m_char =
95 CREATE_FIELD (GCC_JIT_TYPE_CHAR, "m_char");
96 gcc_jit_field *field_m_signed_char =
97 CREATE_FIELD (GCC_JIT_TYPE_SIGNED_CHAR, "m_signed_char");
98 gcc_jit_field *field_m_unsigned_char =
99 CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_CHAR, "m_unsigned_char");
100
101 gcc_jit_field *field_m_short =
102 CREATE_FIELD (GCC_JIT_TYPE_SHORT, "m_short");
103 gcc_jit_field *field_m_unsigned_short =
104 CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_SHORT, "m_unsigned_short");
105
106 gcc_jit_field *field_m_int =
107 CREATE_FIELD (GCC_JIT_TYPE_INT, "m_int");
108 gcc_jit_field *field_m_unsigned_int =
109 CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_INT, "m_unsigned_int");
110
111 gcc_jit_field *field_m_long =
112 CREATE_FIELD (GCC_JIT_TYPE_LONG, "m_long");
113 gcc_jit_field *field_m_unsigned_long =
114 CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_LONG, "m_unsigned_long");
115
116 gcc_jit_field *field_m_long_long =
117 CREATE_FIELD (GCC_JIT_TYPE_LONG_LONG, "m_long_long");
118 gcc_jit_field *field_m_unsigned_long_long =
119 CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_LONG_LONG, "m_unsigned_long_long");
120
121 gcc_jit_field *field_m_u8 =
122 CREATE_FIELD (GCC_JIT_TYPE_UINT8_T, "m_u8");
123 gcc_jit_field *field_m_u16 =
124 CREATE_FIELD (GCC_JIT_TYPE_UINT16_T, "m_u16");
125 gcc_jit_field *field_m_u32 =
126 CREATE_FIELD (GCC_JIT_TYPE_UINT32_T, "m_u32");
127 gcc_jit_field *field_m_u64 =
128 CREATE_FIELD (GCC_JIT_TYPE_UINT64_T, "m_u64");
129
130 gcc_jit_field *field_m_i8 =
131 CREATE_FIELD (GCC_JIT_TYPE_INT8_T, "m_i8");
132 gcc_jit_field *field_m_i16 =
133 CREATE_FIELD (GCC_JIT_TYPE_INT16_T, "m_i16");
134 gcc_jit_field *field_m_i32 =
135 CREATE_FIELD (GCC_JIT_TYPE_INT32_T, "m_i32");
136 gcc_jit_field *field_m_i64 =
137 CREATE_FIELD (GCC_JIT_TYPE_INT64_T, "m_i64");
138
139 #ifdef __SIZEOF_INT128__
140 gcc_jit_field *field_m_u128 =
141 CREATE_FIELD (GCC_JIT_TYPE_UINT128_T, "m_u128");
142 gcc_jit_field *field_m_i128 =
143 CREATE_FIELD (GCC_JIT_TYPE_INT128_T, "m_i128");
144 #endif
145
146 /* Signed int type with sizeof (int): */
147 gcc_jit_type *sized_int_type =
148 gcc_jit_context_get_int_type (ctxt, sizeof (int), 1);
149 gcc_jit_field *field_m_sized_int_type =
150 gcc_jit_context_new_field (
151 ctxt, NULL, sized_int_type, "m_sized_int_type");
152
153 gcc_jit_field *field_m_float =
154 CREATE_FIELD (GCC_JIT_TYPE_FLOAT, "m_float");
155 gcc_jit_field *field_m_double =
156 CREATE_FIELD (GCC_JIT_TYPE_DOUBLE, "m_double");
157 gcc_jit_field *field_m_long_double =
158 CREATE_FIELD (GCC_JIT_TYPE_LONG_DOUBLE, "m_long_double");
159
160 gcc_jit_field *field_m_const_char_ptr =
161 CREATE_FIELD (GCC_JIT_TYPE_CONST_CHAR_PTR, "m_const_char_ptr");
162
163 gcc_jit_field *field_m_size_t =
164 CREATE_FIELD (GCC_JIT_TYPE_SIZE_T, "m_size_t");
165
166 gcc_jit_field *field_m_FILE_ptr =
167 CREATE_FIELD (GCC_JIT_TYPE_FILE_PTR, "m_FILE_ptr");
168
169 #undef CREATE_FIELD
170
171 gcc_jit_field *zoo_fields[] = {
172 field_m_void_ptr,
173
174 field_m_bool,
175
176 field_m_char,
177 field_m_signed_char,
178 field_m_unsigned_char,
179
180 field_m_short,
181 field_m_unsigned_short,
182
183 field_m_int,
184 field_m_unsigned_int,
185
186 field_m_long,
187 field_m_unsigned_long,
188
189 field_m_long_long,
190 field_m_unsigned_long_long,
191
192 field_m_u8,
193 field_m_u16,
194 field_m_u32,
195 field_m_u64,
196
197 field_m_i8,
198 field_m_i16,
199 field_m_i32,
200 field_m_i64,
201
202 #ifdef __SIZEOF_INT128__
203 field_m_u128,
204 field_m_i128,
205 #endif
206
207 field_m_sized_int_type,
208
209 field_m_float,
210 field_m_double,
211 field_m_long_double,
212
213 field_m_const_char_ptr,
214
215 field_m_size_t,
216
217 field_m_FILE_ptr
218 };
219
220 gcc_jit_type *zoo_type =
221 gcc_jit_struct_as_type (
222 gcc_jit_context_new_struct_type (
223 ctxt,
224 NULL,
225 "zoo",
226 sizeof (zoo_fields) / sizeof (zoo_fields[0]),
227 zoo_fields));
228
229 gcc_jit_type *zoo_ptr_type =
230 gcc_jit_type_get_pointer (zoo_type);
231
232 /* Build the test_fn. */
233 gcc_jit_param *param_z =
234 gcc_jit_context_new_param (ctxt, NULL, zoo_ptr_type, "z");
235 gcc_jit_function *test_fn =
236 gcc_jit_context_new_function (ctxt, NULL,
237 GCC_JIT_FUNCTION_EXPORTED,
238 void_type,
239 "test_types",
240 1, &param_z,
241 0);
242 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
243
244 /* Write to the various fields of param "z". */
245 #define ASSIGN(FIELD, EXPR) \
246 gcc_jit_block_add_assignment ( \
247 block, NULL, \
248 gcc_jit_rvalue_dereference_field ( \
249 gcc_jit_param_as_rvalue (param_z), \
250 NULL, \
251 (FIELD)), \
252 (EXPR));
253
254 ASSIGN(
255 field_m_void_ptr,
256 gcc_jit_context_new_rvalue_from_ptr (
257 ctxt,
258 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR),
259 test_ptr))
260
261 ASSIGN(field_m_bool,
262 gcc_jit_context_new_rvalue_from_int (
263 ctxt,
264 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BOOL), 1))
265
266 ASSIGN(field_m_char,
267 gcc_jit_context_new_rvalue_from_int (
268 ctxt,
269 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR),
270 'V'))
271 ASSIGN(field_m_signed_char,
272 gcc_jit_context_new_rvalue_from_int (
273 ctxt,
274 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIGNED_CHAR),
275 -37))
276 ASSIGN(field_m_unsigned_char,
277 gcc_jit_context_new_rvalue_from_int (
278 ctxt,
279 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_CHAR),
280 200))
281
282 ASSIGN(field_m_short,
283 gcc_jit_context_new_rvalue_from_int (
284 ctxt,
285 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SHORT),
286 -900))
287 ASSIGN(field_m_unsigned_short,
288 gcc_jit_context_new_rvalue_from_int (
289 ctxt,
290 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_SHORT),
291 0x3000))
292
293 ASSIGN(field_m_int,
294 gcc_jit_context_new_rvalue_from_int (
295 ctxt,
296 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT),
297 -0x2000))
298 ASSIGN(field_m_unsigned_int,
299 gcc_jit_context_new_rvalue_from_int (
300 ctxt,
301 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_INT),
302 1234567))
303
304 ASSIGN(field_m_long,
305 gcc_jit_context_new_rvalue_from_int (
306 ctxt,
307 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG),
308 -5))
309 ASSIGN(field_m_unsigned_long,
310 gcc_jit_context_new_rvalue_from_int (
311 ctxt,
312 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_LONG),
313 12345678))
314
315 ASSIGN(field_m_long_long,
316 gcc_jit_context_new_rvalue_from_int (
317 ctxt,
318 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG_LONG),
319 -42))
320 ASSIGN(field_m_unsigned_long_long,
321 gcc_jit_context_new_rvalue_from_int (
322 ctxt,
323 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_LONG_LONG),
324 123456789))
325
326 ASSIGN(field_m_u8,
327 gcc_jit_context_new_rvalue_from_int (
328 ctxt,
329 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UINT8_T),
330 123))
331 ASSIGN(field_m_u16,
332 gcc_jit_context_new_rvalue_from_int (
333 ctxt,
334 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UINT16_T),
335 12345))
336 ASSIGN(field_m_u32,
337 gcc_jit_context_new_rvalue_from_int (
338 ctxt,
339 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UINT32_T),
340 123456789))
341 ASSIGN(field_m_u64,
342 gcc_jit_context_new_rvalue_from_int (
343 ctxt,
344 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UINT64_T),
345 123456789))
346
347 ASSIGN(field_m_i8,
348 gcc_jit_context_new_rvalue_from_int (
349 ctxt,
350 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT8_T),
351 -1))
352 ASSIGN(field_m_i16,
353 gcc_jit_context_new_rvalue_from_int (
354 ctxt,
355 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT16_T),
356 -2))
357 ASSIGN(field_m_i32,
358 gcc_jit_context_new_rvalue_from_int (
359 ctxt,
360 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT32_T),
361 -3))
362 ASSIGN(field_m_i64,
363 gcc_jit_context_new_rvalue_from_int (
364 ctxt,
365 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT64_T),
366 -4))
367
368 #ifdef __SIZEOF_INT128__
369 ASSIGN(field_m_u128,
370 gcc_jit_context_new_rvalue_from_int (
371 ctxt,
372 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UINT128_T),
373 123456789))
374 ASSIGN(field_m_i128,
375 gcc_jit_context_new_rvalue_from_int (
376 ctxt,
377 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT128_T),
378 -5))
379 #endif
380
381 ASSIGN(field_m_sized_int_type,
382 gcc_jit_context_new_rvalue_from_int (
383 ctxt,
384 sized_int_type, 500))
385
386 ASSIGN(field_m_float,
387 gcc_jit_context_new_rvalue_from_double (
388 ctxt,
389 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT),
390 3.141))
391 ASSIGN(field_m_double,
392 gcc_jit_context_new_rvalue_from_double (
393 ctxt,
394 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE),
395 3.141))
396 ASSIGN(field_m_long_double,
397 gcc_jit_context_new_rvalue_from_double (
398 ctxt,
399 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG_DOUBLE),
400 3.141))
401
402 ASSIGN(field_m_const_char_ptr,
403 gcc_jit_context_new_rvalue_from_ptr (
404 ctxt,
405 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR),
406 (char *)test_string))
407
408 ASSIGN(field_m_size_t,
409 gcc_jit_context_new_rvalue_from_int (
410 ctxt,
411 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIZE_T),
412 sizeof (struct zoo)))
413
414 ASSIGN(field_m_FILE_ptr,
415 gcc_jit_context_new_rvalue_from_ptr (
416 ctxt,
417 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FILE_PTR),
418 stderr))
419
420 #undef ASSIGN
421
422 gcc_jit_block_end_with_void_return (block, NULL);
423 }
424
425 void
426 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
427 {
428 typedef void (*fn_type) (struct zoo *);
429 CHECK_NON_NULL (result);
430
431 fn_type test_types =
432 (fn_type)gcc_jit_result_get_code (result, "test_types");
433 CHECK_NON_NULL (test_types);
434
435 struct zoo z;
436 memset (&z, 0xf0, sizeof (z));
437
438 /* Call the JIT-generated function. */
439 test_types (&z);
440
441 /* Verify that it correctly wrote to the various fields. */
442 CHECK_VALUE (z.m_void_ptr, test_ptr);
443
444 CHECK_VALUE (z.m_bool, true);
445
446 CHECK_VALUE (z.m_char, 'V');
447 CHECK_VALUE (z.m_signed_char, -37);
448 CHECK_VALUE (z.m_unsigned_char, 200);
449
450 CHECK_VALUE (z.m_short, -900);
451 CHECK_VALUE (z.m_unsigned_short, 0x3000);
452
453 CHECK_VALUE (z.m_int, -0x2000);
454 CHECK_VALUE (z.m_unsigned_int, 1234567);
455
456 CHECK_VALUE (z.m_long, -5);
457 CHECK_VALUE (z.m_unsigned_long, 12345678);
458
459 CHECK_VALUE (z.m_long_long, -42);
460 CHECK_VALUE (z.m_unsigned_long_long, 123456789);
461
462 CHECK_VALUE (z.m_u8, 123);
463 CHECK_VALUE (z.m_u16, 12345);
464 CHECK_VALUE (z.m_u32, 123456789);
465 CHECK_VALUE (z.m_u64, 123456789);
466
467 CHECK_VALUE (z.m_i8, -1);
468 CHECK_VALUE (z.m_i16, -2);
469 CHECK_VALUE (z.m_i32, -3);
470 CHECK_VALUE (z.m_i64, -4);
471
472 #ifdef __SIZEOF_INT128__
473 CHECK_VALUE (z.m_u128, 123456789);
474 CHECK_VALUE (z.m_i128, -5);
475 #endif
476
477 CHECK_VALUE (z.m_sized_int_type, 500);
478
479 CHECK_VALUE (z.m_float, 3.141f);
480 CHECK_VALUE (z.m_double, 3.141);
481 CHECK_VALUE (z.m_long_double, 3.141);
482
483 CHECK_VALUE (z.m_const_char_ptr, test_string);
484
485 CHECK_VALUE (z.m_size_t, sizeof (struct zoo));
486
487 CHECK_VALUE (z.m_FILE_ptr, stderr);
488
489 gcc_jit_type *long_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG);
490 gcc_jit_type *int64_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT64_T);
491 if (sizeof(long) == 8)
492 CHECK (gcc_jit_compatible_types (long_type, int64_type));
493
494 CHECK_VALUE (gcc_jit_type_get_size (gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT)), sizeof (float));
495 CHECK_VALUE (gcc_jit_type_get_size (gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE)), sizeof (double));
496 #ifdef HAVE_BFmode
497 CHECK_VALUE (gcc_jit_type_get_size (gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BFLOAT16)), sizeof (__bfloat16));
498 #endif
499
500 gcc_jit_type *int_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
501 gcc_jit_type *array_type1 = gcc_jit_context_new_array_type (ctxt, NULL, int_type, 2);
502 gcc_jit_type *array_type2 = gcc_jit_context_new_array_type (ctxt, NULL, int_type, 2);
503 CHECK (gcc_jit_compatible_types (array_type1, array_type2));
504
505 gcc_jit_type *aligned_long = gcc_jit_type_get_aligned (long_type, 4);
506 gcc_jit_type *aligned_int64 = gcc_jit_type_get_aligned (int64_type, 4);
507 if (sizeof(long) == 8)
508 CHECK (gcc_jit_compatible_types (aligned_long, aligned_int64));
509 }