* cfgloopmanip.c (duplicate_loop_to_header_edge): Cleanup profile
[official-gcc.git] / gcc / testsuite / jit.dg / test-types.c
blob8debcd7eb82c898e08cc79e1618f721205fb381f
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stddef.h>
4 #include <stdbool.h>
6 #include "libgccjit.h"
8 #include "harness.h"
10 struct zoo
12 void *m_void_ptr;
14 bool m_bool;
16 char m_char;
17 signed char m_signed_char;
18 unsigned char m_unsigned_char;
20 short m_short;
21 unsigned short m_unsigned_short;
23 int m_int;
24 unsigned int m_unsigned_int;
26 long m_long;
27 unsigned long m_unsigned_long;
29 long long m_long_long;
30 unsigned long long m_unsigned_long_long;
32 int m_sized_int_type;
34 float m_float;
35 double m_double;
36 long double m_long_double;
38 const char *m_const_char_ptr;
40 size_t m_size_t;
42 FILE *m_FILE_ptr;
45 int test_int = 42;
46 int *test_ptr = &test_int;
48 const char *test_string = "test_string";
50 void
51 create_code (gcc_jit_context *ctxt, void *user_data)
53 /* Let's try to inject the equivalent of:
55 void
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 ( \
67 ctxt, NULL, \
68 gcc_jit_context_get_type (ctxt, TYPE), \
69 NAME)
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");
127 #undef CREATE_FIELD
129 gcc_jit_field *zoo_fields[] = {
130 field_m_void_ptr,
132 field_m_bool,
134 field_m_char,
135 field_m_signed_char,
136 field_m_unsigned_char,
138 field_m_short,
139 field_m_unsigned_short,
141 field_m_int,
142 field_m_unsigned_int,
144 field_m_long,
145 field_m_unsigned_long,
147 field_m_long_long,
148 field_m_unsigned_long_long,
150 field_m_sized_int_type,
152 field_m_float,
153 field_m_double,
154 field_m_long_double,
156 field_m_const_char_ptr,
158 field_m_size_t,
160 field_m_FILE_ptr
163 gcc_jit_type *zoo_type =
164 gcc_jit_struct_as_type (
165 gcc_jit_context_new_struct_type (
166 ctxt,
167 NULL,
168 "zoo",
169 sizeof (zoo_fields) / sizeof (zoo_fields[0]),
170 zoo_fields));
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,
181 void_type,
182 "test_types",
183 1, &param_z,
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 ( \
190 block, NULL, \
191 gcc_jit_rvalue_dereference_field ( \
192 gcc_jit_param_as_rvalue (param_z), \
193 NULL, \
194 (FIELD)), \
195 (EXPR));
197 ASSIGN(
198 field_m_void_ptr,
199 gcc_jit_context_new_rvalue_from_ptr (
200 ctxt,
201 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR),
202 test_ptr))
204 ASSIGN(field_m_bool,
205 gcc_jit_context_new_rvalue_from_int (
206 ctxt,
207 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BOOL), 1))
209 ASSIGN(field_m_char,
210 gcc_jit_context_new_rvalue_from_int (
211 ctxt,
212 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR),
213 'V'))
214 ASSIGN(field_m_signed_char,
215 gcc_jit_context_new_rvalue_from_int (
216 ctxt,
217 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIGNED_CHAR),
218 -37))
219 ASSIGN(field_m_unsigned_char,
220 gcc_jit_context_new_rvalue_from_int (
221 ctxt,
222 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_CHAR),
223 200))
225 ASSIGN(field_m_short,
226 gcc_jit_context_new_rvalue_from_int (
227 ctxt,
228 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SHORT),
229 -900))
230 ASSIGN(field_m_unsigned_short,
231 gcc_jit_context_new_rvalue_from_int (
232 ctxt,
233 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_SHORT),
234 0x3000))
236 ASSIGN(field_m_int,
237 gcc_jit_context_new_rvalue_from_int (
238 ctxt,
239 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT),
240 -0x2000))
241 ASSIGN(field_m_unsigned_int,
242 gcc_jit_context_new_rvalue_from_int (
243 ctxt,
244 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_INT),
245 1234567))
247 ASSIGN(field_m_long,
248 gcc_jit_context_new_rvalue_from_int (
249 ctxt,
250 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG),
251 -5))
252 ASSIGN(field_m_unsigned_long,
253 gcc_jit_context_new_rvalue_from_int (
254 ctxt,
255 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_LONG),
256 12345678))
258 ASSIGN(field_m_long_long,
259 gcc_jit_context_new_rvalue_from_int (
260 ctxt,
261 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG_LONG),
262 -42))
263 ASSIGN(field_m_unsigned_long_long,
264 gcc_jit_context_new_rvalue_from_int (
265 ctxt,
266 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_LONG_LONG),
267 123456789))
269 ASSIGN(field_m_sized_int_type,
270 gcc_jit_context_new_rvalue_from_int (
271 ctxt,
272 sized_int_type, 500))
274 ASSIGN(field_m_float,
275 gcc_jit_context_new_rvalue_from_double (
276 ctxt,
277 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT),
278 3.141))
279 ASSIGN(field_m_double,
280 gcc_jit_context_new_rvalue_from_double (
281 ctxt,
282 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE),
283 3.141))
284 ASSIGN(field_m_long_double,
285 gcc_jit_context_new_rvalue_from_double (
286 ctxt,
287 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG_DOUBLE),
288 3.141))
290 ASSIGN(field_m_const_char_ptr,
291 gcc_jit_context_new_rvalue_from_ptr (
292 ctxt,
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 (
298 ctxt,
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 (
304 ctxt,
305 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FILE_PTR),
306 stderr))
308 #undef ASSIGN
310 gcc_jit_block_end_with_void_return (block, NULL);
313 void
314 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
316 typedef void (*fn_type) (struct zoo *);
317 CHECK_NON_NULL (result);
319 fn_type test_types =
320 (fn_type)gcc_jit_result_get_code (result, "test_types");
321 CHECK_NON_NULL (test_types);
323 struct zoo z;
324 memset (&z, 0xf0, sizeof (z));
326 /* Call the JIT-generated function. */
327 test_types (&z);
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);