Preserve SSA info for more propagated copy
[official-gcc.git] / gcc / testsuite / jit.dg / test-global-init-rvalue.c
blob4866462ff4ab01fde8e4d0d955df7bfdfd70b927
1 /* This testcase checks that gcc_jit_global_set_initializer_rvalue() works
2 with rvalues, especially with gcc_jit_context_new_*_constructor() for
3 struct, unions and arrays. */
5 #include <stdio.h>
6 #include <string.h>
8 #include "libgccjit.h"
9 #include "harness.h"
11 void
12 create_code (gcc_jit_context *ctxt, void *user_data)
14 gcc_jit_type *int_type = gcc_jit_context_get_type (ctxt,
15 GCC_JIT_TYPE_INT);
16 gcc_jit_type *short_type = gcc_jit_context_get_type (ctxt,
17 GCC_JIT_TYPE_SHORT);
18 gcc_jit_type *pint_type = gcc_jit_type_get_pointer (int_type);
19 gcc_jit_type *double_type = gcc_jit_context_get_type (ctxt,
20 GCC_JIT_TYPE_DOUBLE);
21 gcc_jit_type *float_type = gcc_jit_context_get_type (ctxt,
22 GCC_JIT_TYPE_FLOAT);
23 gcc_jit_type *bool_type = gcc_jit_context_get_type (ctxt,
24 GCC_JIT_TYPE_BOOL);
25 gcc_jit_type *char_type = gcc_jit_context_get_type (ctxt,
26 GCC_JIT_TYPE_CHAR);
27 gcc_jit_type *cpchar_type = gcc_jit_context_get_type (ctxt,
28 GCC_JIT_TYPE_CONST_CHAR_PTR);
29 gcc_jit_type *size_type = gcc_jit_context_get_type (ctxt,
30 GCC_JIT_TYPE_SIZE_T);
32 /* Make a struct: struct fi { float f; int i;} */
33 gcc_jit_field *fi_f = gcc_jit_context_new_field (ctxt,
35 float_type,
36 "f");
37 gcc_jit_field *fi_i = gcc_jit_context_new_field (ctxt,
39 int_type,
40 "i");
41 gcc_jit_field *fields[] = {fi_f, fi_i};
43 gcc_jit_type *struct_fi_type =
44 gcc_jit_struct_as_type (
45 gcc_jit_context_new_struct_type (ctxt,
47 "fi",
49 fields));
51 /* Make a struct:
53 struct bar {
54 int ii;
55 struct fi fi;
56 float ff;
59 gcc_jit_field *bar_ff = gcc_jit_context_new_field (ctxt,
61 float_type,
62 "ff");
63 gcc_jit_field *bar_ii = gcc_jit_context_new_field (ctxt,
65 int_type,
66 "ii");
67 gcc_jit_field *bar_fi = gcc_jit_context_new_field (ctxt,
69 struct_fi_type,
70 "fi");
71 gcc_jit_field *fields2[] = {bar_ff, bar_fi, bar_ii};
73 gcc_jit_type *struct_bar_type =
74 gcc_jit_struct_as_type (
75 gcc_jit_context_new_struct_type (ctxt,
77 "bar",
79 fields2));
81 /* Make an union:
83 union ubar {
84 float ff;
85 int ii;
88 gcc_jit_field *ubar_ff = gcc_jit_context_new_field (ctxt,
90 float_type,
91 "ff");
92 gcc_jit_field *ubar_ii = gcc_jit_context_new_field (ctxt,
94 int_type,
95 "ii");
96 gcc_jit_field *fields3[] = {ubar_ff, ubar_ii};
98 gcc_jit_type *ubar = gcc_jit_context_new_union_type (ctxt,
100 "ubar",
102 fields3);
104 { /* struct bar bar = {.ff=1, .fi={.f=2, .i=3}, .ii=4};
105 I.e. nested ctors and with fields specified
107 gcc_jit_lvalue *bar = gcc_jit_context_new_global (
108 ctxt, NULL,
109 GCC_JIT_GLOBAL_EXPORTED,
110 struct_bar_type,
111 "global_struct_bar_1234_1");
113 gcc_jit_rvalue *fval = gcc_jit_context_new_rvalue_from_int (
114 ctxt, float_type, 2);
115 gcc_jit_rvalue *ival = gcc_jit_context_new_rvalue_from_int (
116 ctxt, int_type, 3);
118 gcc_jit_rvalue *vals[] = { fval, ival};
119 gcc_jit_field *fields[] = {fi_f, fi_i};
121 gcc_jit_rvalue *ctor = gcc_jit_context_new_struct_constructor
122 (ctxt, 0,
123 struct_fi_type,
125 fields,
126 vals);
128 ival = gcc_jit_context_new_rvalue_from_int (
129 ctxt, int_type, 4);
130 fval = gcc_jit_context_new_rvalue_from_int (
131 ctxt, float_type, 1);
133 gcc_jit_rvalue *vals2[] = {fval, ctor, ival};
134 gcc_jit_field *fields2[] = {bar_ff, bar_fi, bar_ii};
136 gcc_jit_rvalue *ctor_bar = gcc_jit_context_new_struct_constructor
137 (ctxt, 0,
138 struct_bar_type,
140 fields2,
141 vals2);
143 gcc_jit_global_set_initializer_rvalue (bar, ctor_bar);
145 { /* struct bar bar = {1, {2, 3}, 4};
146 I.e. nested ctors and fields implicit in definition order (fields=NULL)
148 gcc_jit_lvalue *bar = gcc_jit_context_new_global (
149 ctxt, NULL,
150 GCC_JIT_GLOBAL_EXPORTED,
151 struct_bar_type,
152 "global_struct_bar_1234_2");
154 gcc_jit_rvalue *fval = gcc_jit_context_new_rvalue_from_int (
155 ctxt, float_type, 2);
156 gcc_jit_rvalue *ival = gcc_jit_context_new_rvalue_from_int (
157 ctxt, int_type, 3);
159 gcc_jit_rvalue *vals[] = { fval, ival};
161 gcc_jit_rvalue *ctor = gcc_jit_context_new_struct_constructor
162 (ctxt, 0,
163 struct_fi_type,
166 vals);
168 ival = gcc_jit_context_new_rvalue_from_int (
169 ctxt, int_type, 4);
170 fval = gcc_jit_context_new_rvalue_from_int (
171 ctxt, float_type, 1);
173 gcc_jit_rvalue *vals2[] = {fval, ctor, ival};
175 gcc_jit_rvalue *ctor_bar = gcc_jit_context_new_struct_constructor
176 (ctxt, 0,
177 struct_bar_type,
180 vals2);
182 gcc_jit_global_set_initializer_rvalue (bar, ctor_bar);
184 { /* struct fi foo = {.f=2, .i=3}; */
185 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
186 ctxt, NULL,
187 GCC_JIT_GLOBAL_EXPORTED,
188 struct_fi_type,
189 "global_struct_fi_23_1");
191 gcc_jit_rvalue *fval = gcc_jit_context_new_rvalue_from_int (
192 ctxt, float_type, 2);
193 gcc_jit_rvalue *ival = gcc_jit_context_new_rvalue_from_int (
194 ctxt, int_type, 3);
196 gcc_jit_rvalue *vals[] = { fval, ival};
197 gcc_jit_field *fields[] = {fi_f, fi_i};
199 gcc_jit_rvalue *ctor = gcc_jit_context_new_struct_constructor
200 (ctxt, 0,
201 struct_fi_type,
203 fields,
204 vals);
206 gcc_jit_global_set_initializer_rvalue (foo, ctor);
208 { /* struct fi foo = {2, 3}; */
209 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
210 ctxt, NULL,
211 GCC_JIT_GLOBAL_EXPORTED,
212 struct_fi_type,
213 "global_struct_fi_23_2");
215 gcc_jit_rvalue *fval = gcc_jit_context_new_rvalue_from_int (
216 ctxt, float_type, 2);
217 gcc_jit_rvalue *ival = gcc_jit_context_new_rvalue_from_int (
218 ctxt, int_type, 3);
220 gcc_jit_rvalue *vals[] = { fval, ival};
222 gcc_jit_rvalue *ctor = gcc_jit_context_new_struct_constructor
223 (ctxt, 0,
224 struct_fi_type,
227 vals);
229 gcc_jit_global_set_initializer_rvalue (foo, ctor);
231 { /* struct fi foo = {.i=0, .f=0}; (null init) */
232 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
233 ctxt, NULL,
234 GCC_JIT_GLOBAL_EXPORTED,
235 struct_fi_type,
236 "global_struct_fi_00_1");
238 gcc_jit_rvalue *vals[] = { 0, 0};
239 gcc_jit_field *fields[] = {fi_f, fi_i};
241 gcc_jit_rvalue *ctor = gcc_jit_context_new_struct_constructor
242 (ctxt, 0,
243 struct_fi_type,
245 fields,
246 vals);
248 gcc_jit_global_set_initializer_rvalue (foo, ctor);
250 { /* struct fi foo = {0, 0}; (null fields, null elements in values) */
251 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
252 ctxt, NULL,
253 GCC_JIT_GLOBAL_EXPORTED,
254 struct_fi_type,
255 "global_struct_fi_00_2");
257 gcc_jit_rvalue *vals[] = { 0, 0};
259 gcc_jit_rvalue *ctor = gcc_jit_context_new_struct_constructor
260 (ctxt, 0,
261 struct_fi_type,
264 vals);
266 gcc_jit_global_set_initializer_rvalue (foo, ctor);
268 { /* struct fi foo = {.i = 0} (null init);
270 Null init values. */
271 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
272 ctxt, NULL,
273 GCC_JIT_GLOBAL_EXPORTED,
274 struct_fi_type,
275 "global_struct_fi_0_1");
277 gcc_jit_rvalue *vals[] = {0};
278 gcc_jit_field *fields[] = {fi_i};
280 gcc_jit_rvalue *ctor = gcc_jit_context_new_struct_constructor
281 (ctxt, 0,
282 struct_fi_type,
284 fields,
285 vals);
286 gcc_jit_global_set_initializer_rvalue (foo, ctor);
288 { /* struct fi foo = {0};
290 Null init values. */
291 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
292 ctxt, NULL,
293 GCC_JIT_GLOBAL_EXPORTED,
294 struct_fi_type,
295 "global_struct_fi_0_2");
297 gcc_jit_rvalue *vals[] = {0};
299 gcc_jit_rvalue *ctor = gcc_jit_context_new_struct_constructor
300 (ctxt, 0,
301 struct_fi_type,
304 vals);
305 gcc_jit_global_set_initializer_rvalue (foo, ctor);
307 { /* struct fi foo = {}; (null init)
309 Null fields and values. */
310 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
311 ctxt, NULL,
312 GCC_JIT_GLOBAL_EXPORTED,
313 struct_fi_type,
314 "global_struct_fi_6");
316 gcc_jit_rvalue *ctor = gcc_jit_context_new_struct_constructor
317 (ctxt, 0,
318 struct_fi_type,
322 gcc_jit_global_set_initializer_rvalue (foo, ctor);
324 { /* struct fi foo = {2 * 2, 3}; */
325 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
326 ctxt, NULL,
327 GCC_JIT_GLOBAL_EXPORTED,
328 struct_fi_type,
329 "global_struct_fi_3");
331 gcc_jit_rvalue *fval = gcc_jit_context_new_rvalue_from_int (
332 ctxt, float_type, 2);
333 gcc_jit_rvalue *fval2 = gcc_jit_context_new_rvalue_from_int (
334 ctxt, float_type, 2);
335 gcc_jit_rvalue *ival = gcc_jit_context_new_rvalue_from_int (
336 ctxt, int_type, 3);
337 gcc_jit_rvalue *rval_mul = gcc_jit_context_new_binary_op (ctxt, 0,
338 GCC_JIT_BINARY_OP_MULT,
339 float_type,
340 fval,
341 fval2);
343 gcc_jit_rvalue *vals[] = { rval_mul, ival};
344 gcc_jit_field *fields[] = {fi_f, fi_i};
346 gcc_jit_rvalue *ctor = gcc_jit_context_new_struct_constructor
347 (ctxt, 0,
348 struct_fi_type,
350 fields,
351 vals);
353 gcc_jit_global_set_initializer_rvalue (foo, ctor);
355 { /* union ubar foo = {.ff = 3}; */
356 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
357 ctxt, NULL,
358 GCC_JIT_GLOBAL_EXPORTED,
359 ubar,
360 "global_union_ufoo_ff3");
362 gcc_jit_rvalue *fval = gcc_jit_context_new_rvalue_from_int (
363 ctxt, float_type, 3);
365 gcc_jit_rvalue *ctor = gcc_jit_context_new_union_constructor (
366 ctxt,
368 ubar,
369 ubar_ff,
370 fval);
372 gcc_jit_global_set_initializer_rvalue (foo, ctor);
374 { /* union ubar foo = {.ii = 2}; */
375 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
376 ctxt, NULL,
377 GCC_JIT_GLOBAL_EXPORTED,
378 ubar,
379 "global_union_ufoo_ii2");
381 gcc_jit_rvalue *ival = gcc_jit_context_new_rvalue_from_int (
382 ctxt, int_type, 2);
384 gcc_jit_rvalue *ctor = gcc_jit_context_new_union_constructor (
385 ctxt,
387 ubar,
388 ubar_ii,
389 ival);
391 gcc_jit_global_set_initializer_rvalue (foo, ctor);
393 { /* union ubar foo = {1.1f}; should init first field */
394 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
395 ctxt, NULL,
396 GCC_JIT_GLOBAL_EXPORTED,
397 ubar,
398 "global_union_ufoo_ff1c1");
400 gcc_jit_rvalue *fval = gcc_jit_context_new_rvalue_from_double (
401 ctxt, float_type, 1.1);
403 gcc_jit_rvalue *ctor = gcc_jit_context_new_union_constructor (
404 ctxt,
406 ubar,
408 fval);
410 gcc_jit_global_set_initializer_rvalue (foo, ctor);
412 { /* union ubar foo = (union ubar){}; */
413 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
414 ctxt, NULL,
415 GCC_JIT_GLOBAL_EXPORTED,
416 ubar,
417 "global_union_ufoo_0");
419 gcc_jit_rvalue *ctor = gcc_jit_context_new_union_constructor (
420 ctxt,
422 ubar,
426 gcc_jit_global_set_initializer_rvalue (foo, ctor);
428 { /* int foo = 3; */
429 gcc_jit_rvalue *rval = gcc_jit_context_new_rvalue_from_int (
430 ctxt, int_type, 3);
431 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
432 ctxt, NULL,
433 GCC_JIT_GLOBAL_EXPORTED,
434 int_type,
435 "global_int1_3");
436 gcc_jit_global_set_initializer_rvalue (foo,
437 rval);
439 { /* const volatile int foo = 3; */
440 gcc_jit_rvalue *rval = gcc_jit_context_new_rvalue_from_int (
441 ctxt, int_type, 3);
442 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
443 ctxt, NULL,
444 GCC_JIT_GLOBAL_EXPORTED,
445 gcc_jit_type_get_const (gcc_jit_type_get_volatile (int_type)),
446 "global_cvint1_3");
447 gcc_jit_global_set_initializer_rvalue (foo,
448 rval);
450 { /* Try the above, but with opposite order of global and literal calls */
451 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
452 ctxt, NULL,
453 GCC_JIT_GLOBAL_EXPORTED,
454 int_type,
455 "global_int2_3");
456 gcc_jit_rvalue *rval = gcc_jit_context_new_rvalue_from_int (
457 ctxt, int_type, 3);
458 gcc_jit_global_set_initializer_rvalue (foo,
459 rval);
461 { /* int foo = 3 * (3 + 3) */
462 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
463 ctxt, NULL,
464 GCC_JIT_GLOBAL_EXPORTED,
465 int_type,
466 "global_int3_18");
467 gcc_jit_rvalue *rval3_0 = gcc_jit_context_new_rvalue_from_int (
468 ctxt, int_type, 3);
469 gcc_jit_rvalue *rval3_1 = gcc_jit_context_new_rvalue_from_int (
470 ctxt, int_type, 3);
471 gcc_jit_rvalue *rval3_2 = gcc_jit_context_new_rvalue_from_int (
472 ctxt, int_type, 3);
473 gcc_jit_rvalue *rval_plus = gcc_jit_context_new_binary_op (ctxt, 0,
474 GCC_JIT_BINARY_OP_PLUS,
475 int_type,
476 rval3_0,
477 rval3_1);
478 gcc_jit_rvalue *rval_mul = gcc_jit_context_new_binary_op (ctxt, 0,
479 GCC_JIT_BINARY_OP_MULT,
480 int_type,
481 rval_plus,
482 rval3_2);
484 gcc_jit_global_set_initializer_rvalue (foo,
485 rval_mul);
487 { /* int foo = ~(-(((((2 | 8) & 15) ^ 0) << 3 >> 2 - 1) / 2)); */
488 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
489 ctxt, NULL,
490 GCC_JIT_GLOBAL_EXPORTED,
491 int_type,
492 "global_int_alotofoperators");
493 gcc_jit_rvalue *rval_0 = gcc_jit_context_new_rvalue_from_int (
494 ctxt, int_type, 2);
495 gcc_jit_rvalue *rval_1 = gcc_jit_context_new_rvalue_from_int (
496 ctxt, int_type, 8);
497 gcc_jit_rvalue *rval_2 = gcc_jit_context_new_rvalue_from_int (
498 ctxt, int_type, 15);
499 gcc_jit_rvalue *rval_3 = gcc_jit_context_new_rvalue_from_int (
500 ctxt, int_type, 0);
501 gcc_jit_rvalue *rval_4 = gcc_jit_context_new_rvalue_from_int (
502 ctxt, int_type, 3);
503 gcc_jit_rvalue *rval_5 = gcc_jit_context_new_rvalue_from_int (
504 ctxt, int_type, 2);
505 gcc_jit_rvalue *rval_6 = gcc_jit_context_new_rvalue_from_int (
506 ctxt, int_type, 1);
507 gcc_jit_rvalue *rval_7 = gcc_jit_context_new_rvalue_from_int (
508 ctxt, int_type, 2);
510 gcc_jit_rvalue *rval_or = gcc_jit_context_new_binary_op (ctxt, 0,
511 GCC_JIT_BINARY_OP_BITWISE_OR,
512 int_type,
513 rval_0,
514 rval_1);
515 gcc_jit_rvalue *rval_and = gcc_jit_context_new_binary_op (ctxt, 0,
516 GCC_JIT_BINARY_OP_BITWISE_AND,
517 int_type,
518 rval_or,
519 rval_2);
520 gcc_jit_rvalue *rval_xor = gcc_jit_context_new_binary_op (ctxt, 0,
521 GCC_JIT_BINARY_OP_BITWISE_XOR,
522 int_type,
523 rval_and,
524 rval_3);
525 gcc_jit_rvalue *rval_lsh = gcc_jit_context_new_binary_op (ctxt, 0,
526 GCC_JIT_BINARY_OP_LSHIFT,
527 int_type,
528 rval_xor,
529 rval_4);
530 gcc_jit_rvalue *rval_rsh = gcc_jit_context_new_binary_op (ctxt, 0,
531 GCC_JIT_BINARY_OP_RSHIFT,
532 int_type,
533 rval_lsh,
534 rval_5);
535 gcc_jit_rvalue *rval_min = gcc_jit_context_new_binary_op (ctxt, 0,
536 GCC_JIT_BINARY_OP_MINUS,
537 int_type,
538 rval_rsh,
539 rval_6);
540 gcc_jit_rvalue *rval_div = gcc_jit_context_new_binary_op (ctxt, 0,
541 GCC_JIT_BINARY_OP_DIVIDE,
542 int_type,
543 rval_min,
544 rval_7);
545 gcc_jit_rvalue *rval_umin = gcc_jit_context_new_unary_op (ctxt, 0,
546 GCC_JIT_UNARY_OP_MINUS,
547 int_type,
548 rval_div);
549 gcc_jit_rvalue *rval_neg = gcc_jit_context_new_unary_op (ctxt, 0,
550 GCC_JIT_UNARY_OP_BITWISE_NEGATE,
551 int_type,
552 rval_umin);
554 gcc_jit_global_set_initializer_rvalue (foo,
555 rval_neg);
557 { /* int foo = 3; int *pfoo = &foo; */
558 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
559 ctxt, NULL,
560 GCC_JIT_GLOBAL_EXPORTED,
561 int_type,
562 "global_int4_3");
563 gcc_jit_rvalue *rval = gcc_jit_context_new_rvalue_from_int (
564 ctxt, int_type, 3);
565 gcc_jit_global_set_initializer_rvalue (foo,
566 rval);
568 gcc_jit_lvalue *pfoo = gcc_jit_context_new_global (
569 ctxt, NULL,
570 GCC_JIT_GLOBAL_EXPORTED,
571 pint_type,
572 "global_pint5");
573 gcc_jit_global_set_initializer_rvalue (pfoo,
574 gcc_jit_lvalue_get_address (foo, 0));
576 { /* static int foo; int *pfoo = &foo; */
577 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
578 ctxt, NULL,
579 GCC_JIT_GLOBAL_INTERNAL,
580 int_type,
581 "global_int5_3");
583 gcc_jit_lvalue *pfoo = gcc_jit_context_new_global (
584 ctxt, NULL,
585 GCC_JIT_GLOBAL_EXPORTED,
586 pint_type,
587 "global_pint6");
588 gcc_jit_global_set_initializer_rvalue (pfoo,
589 gcc_jit_lvalue_get_address (foo, 0));
591 gcc_jit_function *fn =
592 gcc_jit_context_new_function (ctxt,
594 GCC_JIT_FUNCTION_EXPORTED,
595 gcc_jit_type_get_pointer(int_type),
596 "fn_pint_0",
601 gcc_jit_block *block = gcc_jit_function_new_block (fn, "start");
603 gcc_jit_block_end_with_return (block,
605 gcc_jit_lvalue_get_address (foo, 0));
607 { /* int foo = 3; int *pfoo = &foo + 1; */
608 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
609 ctxt, NULL,
610 GCC_JIT_GLOBAL_EXPORTED,
611 int_type,
612 "global_int6_3");
613 gcc_jit_rvalue *rval = gcc_jit_context_new_rvalue_from_int (
614 ctxt, int_type, 3);
615 gcc_jit_global_set_initializer_rvalue (foo,
616 rval);
618 gcc_jit_lvalue *pfoo = gcc_jit_context_new_global (
619 ctxt, NULL,
620 GCC_JIT_GLOBAL_EXPORTED,
621 pint_type,
622 "global_pint7");
623 gcc_jit_global_set_initializer_rvalue (pfoo,
624 gcc_jit_lvalue_get_address (
625 gcc_jit_context_new_array_access(
626 ctxt,
628 gcc_jit_lvalue_get_address(foo, 0),
629 gcc_jit_context_one(ctxt, int_type)),
630 0));
632 { /* double foo = 3; */
633 gcc_jit_lvalue *double1 = gcc_jit_context_new_global (
634 ctxt, NULL,
635 GCC_JIT_GLOBAL_EXPORTED,
636 double_type,
637 "global_double1_3");
638 gcc_jit_rvalue *rval = gcc_jit_context_new_rvalue_from_int (
639 ctxt, double_type, 3);
640 gcc_jit_global_set_initializer_rvalue (double1,
641 rval);
643 { /* double foo = 3 * 3 + 3 */
644 gcc_jit_lvalue *double1 = gcc_jit_context_new_global (
645 ctxt, NULL,
646 GCC_JIT_GLOBAL_EXPORTED,
647 double_type,
648 "global_double2_12");
649 gcc_jit_rvalue *rval = gcc_jit_context_new_rvalue_from_int (
650 ctxt, double_type, 3);
651 gcc_jit_rvalue *rval_mul = gcc_jit_context_new_binary_op (ctxt, 0,
652 GCC_JIT_BINARY_OP_MULT,
653 double_type,
654 rval,
655 rval);
656 gcc_jit_rvalue *rval_plus = gcc_jit_context_new_binary_op (ctxt, 0,
657 GCC_JIT_BINARY_OP_PLUS,
658 double_type,
659 rval_mul,
660 rval);
661 gcc_jit_global_set_initializer_rvalue (double1,
662 rval_plus);
664 { /* bool foo = 3 + 3 <= 6; */
665 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
666 ctxt, NULL,
667 GCC_JIT_GLOBAL_EXPORTED,
668 bool_type,
669 "global_bool1_1");
670 gcc_jit_rvalue *rval3_0 = gcc_jit_context_new_rvalue_from_int (
671 ctxt, int_type, 3);
672 gcc_jit_rvalue *rval3_1 = gcc_jit_context_new_rvalue_from_int (
673 ctxt, int_type, 3);
674 gcc_jit_rvalue *rval6 = gcc_jit_context_new_rvalue_from_int (
675 ctxt, int_type, 6);
676 gcc_jit_rvalue *rval_plus = gcc_jit_context_new_binary_op (ctxt,
678 GCC_JIT_BINARY_OP_PLUS,
679 int_type,
680 rval3_0,
681 rval3_1);
682 gcc_jit_rvalue *rval_le = gcc_jit_context_new_comparison (ctxt,
684 GCC_JIT_COMPARISON_LE,
685 rval_plus,
686 rval6);
688 gcc_jit_global_set_initializer_rvalue (foo,
689 rval_le);
691 gcc_jit_lvalue *global_intarr_1234;
692 { /* int foo[] = {1,2,3,4}; */
694 gcc_jit_type *arr_type = gcc_jit_context_new_array_type (ctxt,
696 int_type,
698 gcc_jit_rvalue *rval_1 = gcc_jit_context_new_rvalue_from_int (
699 ctxt, int_type, 1);
700 gcc_jit_rvalue *rval_2 = gcc_jit_context_new_rvalue_from_int (
701 ctxt, int_type, 2);
702 gcc_jit_rvalue *rval_3 = gcc_jit_context_new_rvalue_from_int (
703 ctxt, int_type, 3);
704 gcc_jit_rvalue *rval_4 = gcc_jit_context_new_rvalue_from_int (
705 ctxt, int_type, 4);
707 gcc_jit_rvalue *values[] = {rval_1, rval_2, rval_3, rval_4};
709 gcc_jit_rvalue *ctor = gcc_jit_context_new_array_constructor (ctxt,
711 arr_type,
713 values);
714 global_intarr_1234 = gcc_jit_context_new_global (
715 ctxt, NULL,
716 GCC_JIT_GLOBAL_EXPORTED,
717 arr_type,
718 "global_intarr_1234");
719 gcc_jit_global_set_initializer_rvalue (global_intarr_1234, ctor);
721 { /* float foo[4] = {1,2}; */
723 gcc_jit_type *arr_type = gcc_jit_context_new_array_type (ctxt,
725 float_type,
727 gcc_jit_rvalue *rval_1 = gcc_jit_context_new_rvalue_from_int (
728 ctxt, float_type, 1);
729 gcc_jit_rvalue *rval_2 = gcc_jit_context_new_rvalue_from_int (
730 ctxt, float_type, 2);
732 gcc_jit_rvalue *values[] = {rval_1, rval_2};
734 gcc_jit_rvalue *ctor = gcc_jit_context_new_array_constructor (ctxt,
736 arr_type,
738 values);
739 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
740 ctxt, NULL,
741 GCC_JIT_GLOBAL_EXPORTED,
742 arr_type,
743 "global_floatarr_12");
744 gcc_jit_global_set_initializer_rvalue (foo, ctor);
746 { /* float foo[4] = {1,2};
747 With different array objects of same size and type. */
749 gcc_jit_type *arr_type = gcc_jit_context_new_array_type (ctxt,
751 float_type,
753 gcc_jit_type *arr_type1 = gcc_jit_context_new_array_type (ctxt,
755 float_type,
757 gcc_jit_rvalue *rval_1 = gcc_jit_context_new_rvalue_from_int (
758 ctxt, float_type, 1);
759 gcc_jit_rvalue *rval_2 = gcc_jit_context_new_rvalue_from_int (
760 ctxt, float_type, 2);
762 gcc_jit_rvalue *values[] = {rval_1, rval_2};
764 gcc_jit_rvalue *ctor = gcc_jit_context_new_array_constructor (ctxt,
766 arr_type1,
768 values);
769 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
770 ctxt, NULL,
771 GCC_JIT_GLOBAL_EXPORTED,
772 arr_type,
773 "global_floatarr_12_2");
774 gcc_jit_global_set_initializer_rvalue (foo, ctor);
776 { /* float foo[4] = {1,2,0}; (null init) */
778 gcc_jit_type *arr_type = gcc_jit_context_new_array_type (ctxt,
780 float_type,
782 gcc_jit_rvalue *rval_1 = gcc_jit_context_new_rvalue_from_int (
783 ctxt, float_type, 1);
784 gcc_jit_rvalue *rval_2 = gcc_jit_context_new_rvalue_from_int (
785 ctxt, float_type, 2);
787 gcc_jit_rvalue *values[] = {rval_1, rval_2, 0};
789 gcc_jit_rvalue *ctor = gcc_jit_context_new_array_constructor (ctxt,
791 arr_type,
793 values);
794 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
795 ctxt, NULL,
796 GCC_JIT_GLOBAL_EXPORTED,
797 arr_type,
798 "global_floatarr_120");
799 gcc_jit_global_set_initializer_rvalue (foo, ctor);
801 { /* float foo[4] = {}; (null init) */
803 gcc_jit_type *arr_type = gcc_jit_context_new_array_type (ctxt,
805 float_type,
808 gcc_jit_rvalue *ctor = gcc_jit_context_new_array_constructor (ctxt,
810 arr_type,
813 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
814 ctxt, NULL,
815 GCC_JIT_GLOBAL_EXPORTED,
816 arr_type,
817 "global_floatarr_0000");
818 gcc_jit_global_set_initializer_rvalue (foo, ctor);
820 { /* float foo[4] = {NULL , NULL, 3, NULL, 5, 6}; (null init) */
822 gcc_jit_type *arr_type = gcc_jit_context_new_array_type (ctxt,
824 float_type,
826 gcc_jit_rvalue *rval3 = gcc_jit_context_new_rvalue_from_int (
827 ctxt, float_type, 3);
828 gcc_jit_rvalue *rval5 = gcc_jit_context_new_rvalue_from_int (
829 ctxt, float_type, 5);
830 gcc_jit_rvalue *rval6 = gcc_jit_context_new_rvalue_from_int (
831 ctxt, float_type, 6);
833 gcc_jit_rvalue *values[] = {0, 0, rval3, 0, rval5, rval6, 0 };
835 gcc_jit_rvalue *ctor = gcc_jit_context_new_array_constructor (ctxt,
837 arr_type,
839 values);
840 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
841 ctxt, NULL,
842 GCC_JIT_GLOBAL_EXPORTED,
843 arr_type,
844 "global_floatarr_00305600");
845 gcc_jit_global_set_initializer_rvalue (foo, ctor);
847 { /* int *foo[4] = {0, &global_intarr_1234[1], 0}; */
849 gcc_jit_type *arr_type = gcc_jit_context_new_array_type (ctxt,
851 pint_type,
853 gcc_jit_rvalue *rval_1 = gcc_jit_context_new_rvalue_from_int (
854 ctxt, int_type, 1);
855 gcc_jit_lvalue *arr_access = gcc_jit_context_new_array_access (
856 ctxt,
858 gcc_jit_lvalue_as_rvalue (global_intarr_1234),
859 rval_1);
860 gcc_jit_rvalue *rval_2 = gcc_jit_lvalue_get_address (arr_access, 0);
861 gcc_jit_rvalue *rval_3 = gcc_jit_context_null (ctxt, pint_type);
863 gcc_jit_rvalue *values[] = {0, rval_2, rval_3};
865 gcc_jit_rvalue *ctor = gcc_jit_context_new_array_constructor (ctxt,
867 arr_type,
869 values);
870 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
871 ctxt, NULL,
872 GCC_JIT_GLOBAL_EXPORTED,
873 arr_type,
874 "global_pintarr_x2xx");
875 gcc_jit_global_set_initializer_rvalue (foo, ctor);
877 { /* char foo[4] = {'q','w','e',0}; */
879 gcc_jit_type *arr_type = gcc_jit_context_new_array_type (ctxt,
881 char_type,
885 gcc_jit_rvalue *rvals[] = {
886 gcc_jit_context_new_rvalue_from_int ( ctxt, char_type, 'q'),
887 gcc_jit_context_new_rvalue_from_int ( ctxt, char_type, 'w'),
888 gcc_jit_context_new_rvalue_from_int ( ctxt, char_type, 'e'),
889 gcc_jit_context_new_rvalue_from_int ( ctxt, char_type, 0)
892 gcc_jit_rvalue *ctor = gcc_jit_context_new_array_constructor (ctxt,
894 arr_type,
896 rvals);
897 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
898 ctxt, NULL,
899 GCC_JIT_GLOBAL_EXPORTED,
900 arr_type,
901 "global_chararr_qwe");
902 gcc_jit_global_set_initializer_rvalue (foo, ctor);
904 { /* int foo[2][2] = {{1,2},{3,4}}; */
906 gcc_jit_type *row_type = gcc_jit_context_new_array_type (ctxt,
908 int_type,
911 gcc_jit_type *arr_type = gcc_jit_context_new_array_type (ctxt,
913 row_type,
915 gcc_jit_rvalue *rvals_row0[] = {
916 gcc_jit_context_new_rvalue_from_int ( ctxt, int_type, 1),
917 gcc_jit_context_new_rvalue_from_int ( ctxt, int_type, 2)
919 gcc_jit_rvalue *rvals_row1[] = {
920 gcc_jit_context_new_rvalue_from_int ( ctxt, int_type, 3),
921 gcc_jit_context_new_rvalue_from_int ( ctxt, int_type, 4)
924 gcc_jit_rvalue *ctor_row0 =
925 gcc_jit_context_new_array_constructor (ctxt,
927 row_type,
929 rvals_row0);
930 gcc_jit_rvalue *ctor_row1 =
931 gcc_jit_context_new_array_constructor (ctxt,
933 row_type,
935 rvals_row1);
936 gcc_jit_rvalue *ctors_row[] = {ctor_row0, ctor_row1};
938 gcc_jit_rvalue *ctor_arr =
939 gcc_jit_context_new_array_constructor (ctxt,
941 arr_type,
943 ctors_row);
945 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
946 ctxt, NULL,
947 GCC_JIT_GLOBAL_EXPORTED,
948 arr_type,
949 "global_int2x2matrix_1234");
951 gcc_jit_global_set_initializer_rvalue (foo, ctor_arr);
953 { /* const char *foo[4] = {"qwe", "asd"}; */
955 gcc_jit_type *arr_type = gcc_jit_context_new_array_type (ctxt,
957 cpchar_type,
961 gcc_jit_rvalue *rvals[] = {
962 gcc_jit_context_new_string_literal (ctxt, "qwe"),
963 gcc_jit_context_new_string_literal (ctxt, "asd")
966 gcc_jit_rvalue *ctor = gcc_jit_context_new_array_constructor (ctxt,
968 arr_type,
970 rvals);
971 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
972 ctxt, NULL,
973 GCC_JIT_GLOBAL_EXPORTED,
974 arr_type,
975 "global_cpchararr_qwe_asd");
976 gcc_jit_global_set_initializer_rvalue (foo, ctor);
978 { /* const int foo = 3;
979 int bar = foo;
981 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
982 ctxt, NULL,
983 GCC_JIT_GLOBAL_EXPORTED,
984 gcc_jit_type_get_const (int_type),
985 "global_const_int_3");
986 gcc_jit_rvalue *rval3 = gcc_jit_context_new_rvalue_from_int (
987 ctxt, int_type, 3);
988 gcc_jit_global_set_initializer_rvalue (foo,
989 rval3);
990 gcc_jit_lvalue *bar = gcc_jit_context_new_global (
991 ctxt, NULL,
992 GCC_JIT_GLOBAL_EXPORTED,
993 int_type,
994 "global_lvalueinit_int_3");
995 gcc_jit_global_set_initializer_rvalue (bar,
996 gcc_jit_lvalue_as_rvalue (foo));
998 { /* int foo = 3 * 2;
999 int arr[] = {1,2,3,4};
1000 int *bar = &arr[2] + 1
1002 Example in the docs.
1005 gcc_jit_type *arr_type = gcc_jit_context_new_array_type (ctxt,
1007 int_type,
1009 gcc_jit_rvalue *rval_1 = gcc_jit_context_new_rvalue_from_int (
1010 ctxt, int_type, 1);
1011 gcc_jit_rvalue *rval_2 = gcc_jit_context_new_rvalue_from_int (
1012 ctxt, int_type, 2);
1013 gcc_jit_rvalue *rval_3 = gcc_jit_context_new_rvalue_from_int (
1014 ctxt, int_type, 3);
1015 gcc_jit_rvalue *rval_4 = gcc_jit_context_new_rvalue_from_int (
1016 ctxt, int_type, 4);
1018 gcc_jit_rvalue *values[] = {rval_1, rval_2, rval_3, rval_4};
1020 gcc_jit_rvalue *ctor = gcc_jit_context_new_array_constructor (ctxt,
1022 arr_type,
1024 values);
1025 gcc_jit_lvalue *global_intarr_1234 =
1026 gcc_jit_context_new_global (ctxt, NULL,
1027 GCC_JIT_GLOBAL_EXPORTED,
1028 arr_type,
1029 "global_intarr_1234_2");
1031 gcc_jit_global_set_initializer_rvalue (global_intarr_1234, ctor);
1033 gcc_jit_lvalue *bar =
1034 gcc_jit_context_new_global (ctxt, NULL,
1035 GCC_JIT_GLOBAL_EXPORTED,
1036 int_type,
1037 "global_int_6");
1038 gcc_jit_global_set_initializer_rvalue
1039 (bar,
1040 gcc_jit_context_new_binary_op
1041 (ctxt, 0, GCC_JIT_BINARY_OP_MULT,
1042 int_type,
1043 gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 3),
1044 gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 2)));
1046 gcc_jit_lvalue *pfoo =
1047 gcc_jit_context_new_global (ctxt, NULL,
1048 GCC_JIT_GLOBAL_EXPORTED,
1049 gcc_jit_type_get_pointer (int_type),
1050 "global_pint_4");
1051 /* int *bar = &arr[2] + 1;
1053 In practice we could just do &foo[3]
1054 but just prove folding this works. */
1055 gcc_jit_global_set_initializer_rvalue (
1056 pfoo,
1057 gcc_jit_lvalue_get_address (
1058 gcc_jit_context_new_array_access (
1059 ctxt, 0,
1060 gcc_jit_lvalue_get_address (
1061 gcc_jit_context_new_array_access (
1062 ctxt, 0,
1063 gcc_jit_lvalue_as_rvalue (global_intarr_1234),
1064 gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 2)),
1066 gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 1)),
1067 0));
1069 { /* static int bar = 11;
1070 int foo () { return bar; } */
1072 gcc_jit_lvalue *bar = gcc_jit_context_new_global (
1073 ctxt, NULL,
1074 GCC_JIT_GLOBAL_INTERNAL,
1075 int_type,
1076 "global_static_int_11");
1077 gcc_jit_rvalue *rval1 = gcc_jit_context_new_rvalue_from_int (
1078 ctxt, int_type, 11);
1079 gcc_jit_global_set_initializer_rvalue (bar,
1080 rval1);
1082 gcc_jit_function *fn11 =
1083 gcc_jit_context_new_function (ctxt,
1085 GCC_JIT_FUNCTION_EXPORTED,
1086 int_type,
1087 "fn_int_11",
1091 gcc_jit_block *block = gcc_jit_function_new_block (fn11, "start");
1093 gcc_jit_block_end_with_return (block,
1095 gcc_jit_lvalue_as_rvalue(bar));
1097 { /* static const int cbar = 11;
1098 int cfoo () { return cbar; } */
1100 gcc_jit_lvalue *bar = gcc_jit_context_new_global (
1101 ctxt, NULL,
1102 GCC_JIT_GLOBAL_INTERNAL,
1103 gcc_jit_type_get_const (int_type),
1104 "global_static_cint_11");
1105 gcc_jit_rvalue *rval1 = gcc_jit_context_new_rvalue_from_int (
1106 ctxt, int_type, 11);
1107 gcc_jit_global_set_initializer_rvalue (bar,
1108 rval1);
1110 gcc_jit_function *fn11 =
1111 gcc_jit_context_new_function (ctxt,
1113 GCC_JIT_FUNCTION_EXPORTED,
1114 int_type,
1115 "fn_cint_11",
1119 gcc_jit_block *block = gcc_jit_function_new_block (fn11, "start");
1121 gcc_jit_block_end_with_return (block,
1123 gcc_jit_lvalue_as_rvalue(bar));
1125 { /* static const int cbar = 12;
1126 const int* cfoo () { return &cbar; } */
1128 gcc_jit_lvalue *bar = gcc_jit_context_new_global (
1129 ctxt, NULL,
1130 GCC_JIT_GLOBAL_INTERNAL,
1131 gcc_jit_type_get_const (int_type),
1132 "global_static_cint_12");
1133 gcc_jit_rvalue *rval1 = gcc_jit_context_new_rvalue_from_int (
1134 ctxt, int_type, 12);
1135 gcc_jit_global_set_initializer_rvalue (bar,
1136 rval1);
1138 gcc_jit_function *fn11 =
1139 gcc_jit_context_new_function (ctxt,
1141 GCC_JIT_FUNCTION_EXPORTED,
1142 gcc_jit_type_get_pointer(int_type),
1143 "fn_cint_12",
1148 gcc_jit_block *block = gcc_jit_function_new_block (fn11, "start");
1150 gcc_jit_block_end_with_return (block,
1152 gcc_jit_lvalue_get_address (bar, 0));
1154 { /* const int foo = 3;
1155 short bar = (short)foo;
1157 Assure casts fold
1159 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
1160 ctxt, NULL,
1161 GCC_JIT_GLOBAL_EXPORTED,
1162 gcc_jit_type_get_const (int_type),
1163 "global_const_int_4");
1164 gcc_jit_rvalue *rval3 = gcc_jit_context_new_rvalue_from_int (
1165 ctxt, int_type, 3);
1166 gcc_jit_global_set_initializer_rvalue (foo,
1167 rval3);
1168 gcc_jit_lvalue *bar = gcc_jit_context_new_global (
1169 ctxt, NULL,
1170 GCC_JIT_GLOBAL_EXPORTED,
1171 short_type,
1172 "global_lvalueinit_short_3");
1173 gcc_jit_global_set_initializer_rvalue (
1174 bar,
1175 gcc_jit_context_new_cast( ctxt, 0,
1176 gcc_jit_lvalue_as_rvalue (foo),
1177 short_type));
1179 { /* const int foo = 3;
1180 const int const *bar = &foo; */
1181 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
1182 ctxt, NULL,
1183 GCC_JIT_GLOBAL_EXPORTED,
1184 gcc_jit_type_get_const (int_type),
1185 "global_const_int_6");
1186 gcc_jit_rvalue *rval3 = gcc_jit_context_new_rvalue_from_int (
1187 ctxt, int_type, 3);
1188 gcc_jit_global_set_initializer_rvalue (foo,
1189 rval3);
1190 gcc_jit_lvalue *bar = gcc_jit_context_new_global (
1191 ctxt, NULL,
1192 GCC_JIT_GLOBAL_EXPORTED,
1193 gcc_jit_type_get_const (
1194 gcc_jit_type_get_pointer (
1195 gcc_jit_type_get_const (
1196 int_type))),
1197 "global_lvalueinit_cpcint_3");
1198 gcc_jit_global_set_initializer_rvalue (
1199 bar,
1200 gcc_jit_lvalue_get_address (foo, 0));
1202 { /* const int __attribute__ ((aligned (64))) foo = 3;
1203 int bar = foo;
1205 Assure alignement does not make the constant "miss"
1206 or something strange.
1208 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
1209 ctxt, NULL,
1210 GCC_JIT_GLOBAL_EXPORTED,
1211 gcc_jit_type_get_const (gcc_jit_type_get_aligned (int_type, 64)),
1212 "global_const_int_7");
1213 gcc_jit_rvalue *rval = gcc_jit_context_new_rvalue_from_int (
1214 ctxt, int_type, 4);
1215 gcc_jit_global_set_initializer_rvalue (foo,
1216 rval);
1217 gcc_jit_lvalue *bar = gcc_jit_context_new_global (
1218 ctxt, NULL,
1219 GCC_JIT_GLOBAL_EXPORTED,
1220 int_type,
1221 "global_lvalueinit_int_4");
1222 gcc_jit_global_set_initializer_rvalue (bar,
1223 gcc_jit_lvalue_as_rvalue (foo));
1226 /* union upintsize { size_t s; int *p } u = {.s = 0xEEEFBEEF}; */
1227 gcc_jit_field *f1 = gcc_jit_context_new_field (ctxt,
1229 size_type,
1230 "s");
1231 gcc_jit_field *f2 = gcc_jit_context_new_field (ctxt,
1233 pint_type,
1234 "p");
1235 gcc_jit_field *fields1[] = {f1, f2};
1237 gcc_jit_type *ubar = gcc_jit_context_new_union_type (ctxt,
1239 "upintsize",
1241 fields1);
1242 gcc_jit_lvalue *foo = gcc_jit_context_new_global (
1243 ctxt, NULL,
1244 GCC_JIT_GLOBAL_EXPORTED,
1245 gcc_jit_type_get_const (ubar),
1246 "global_const_upintsize_1");
1248 gcc_jit_rvalue *val = gcc_jit_context_new_rvalue_from_long (
1249 ctxt, size_type, 0xEEEFBEEF);
1251 gcc_jit_rvalue *ctor =
1252 gcc_jit_context_new_union_constructor (ctxt,
1254 ubar,
1256 val);
1258 gcc_jit_global_set_initializer_rvalue (foo, ctor);
1261 struct B;
1262 struct A { B* b; };
1263 struct B { A* a; };
1264 extern struct B b;
1265 struct A a = {.b = b};
1266 struct B b = {.a = a};
1268 See that opaque structs and circular pointers works.
1271 gcc_jit_struct *struct_B =
1272 gcc_jit_context_new_opaque_struct(ctxt,
1273 0, "B");
1275 gcc_jit_field *fields_A[] =
1277 gcc_jit_context_new_field (ctxt, 0,
1278 gcc_jit_type_get_pointer (
1279 gcc_jit_struct_as_type (struct_B)),
1280 "b")
1283 gcc_jit_struct *struct_A =
1284 gcc_jit_context_new_struct_type(ctxt, 0, "A", 1, fields_A);
1286 gcc_jit_field *fields_B[] =
1288 gcc_jit_context_new_field (ctxt, 0,
1289 gcc_jit_type_get_pointer (
1290 gcc_jit_struct_as_type (struct_A)),
1291 "a")
1294 gcc_jit_struct_set_fields (struct_B, 0, 1, fields_B);
1296 gcc_jit_lvalue *a =
1297 gcc_jit_context_new_global (ctxt, 0, GCC_JIT_GLOBAL_EXPORTED,
1298 gcc_jit_struct_as_type (struct_A),
1299 "a_glb");
1300 gcc_jit_lvalue *b =
1301 gcc_jit_context_new_global (ctxt, 0, GCC_JIT_GLOBAL_EXPORTED,
1302 gcc_jit_struct_as_type (struct_B),
1303 "b_glb");
1304 gcc_jit_rvalue *b_addr = gcc_jit_lvalue_get_address( b, 0);
1305 gcc_jit_rvalue *a_ctor =
1306 gcc_jit_context_new_struct_constructor (ctxt, 0,
1307 gcc_jit_struct_as_type (struct_A),
1308 1, 0,
1309 &b_addr);
1310 gcc_jit_rvalue *a_addr = gcc_jit_lvalue_get_address( a, 0);
1311 gcc_jit_rvalue *b_ctor =
1312 gcc_jit_context_new_struct_constructor (ctxt, 0,
1313 gcc_jit_struct_as_type (struct_B),
1314 1, 0,
1315 &a_addr);
1317 gcc_jit_global_set_initializer_rvalue(a, a_ctor);
1318 gcc_jit_global_set_initializer_rvalue(b, b_ctor);
1322 struct fi {
1323 float f;
1324 int i;
1327 struct bar1 {
1328 float ff;
1329 struct fi fi;
1330 int ii;
1333 union ubar1 {
1334 float ff;
1335 int ii;
1338 union upintsize {
1339 size_t s;
1340 int *p;
1343 struct B_glb;
1344 struct A_glb {
1345 struct B_glb *b;
1347 struct B_glb {
1348 struct A_glb *a;
1351 int test_aligned64_works_in_linker_1 __attribute__ ((aligned (64))) = 0;
1352 int test_aligned64_works_in_linker_2 __attribute__ ((aligned (64))) = 0;
1354 void
1355 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
1357 CHECK_NON_NULL (result);
1360 struct bar1 *bar =
1361 gcc_jit_result_get_global (result, "global_struct_bar_1234_1");
1363 CHECK_VALUE (bar->ff, 1);
1364 CHECK_VALUE (bar->fi.f, 2);
1365 CHECK_VALUE (bar->fi.i, 3);
1366 CHECK_VALUE (bar->ii, 4);
1369 struct bar1 *bar =
1370 gcc_jit_result_get_global (result, "global_struct_bar_1234_2");
1372 CHECK_VALUE (bar->ff, 1);
1373 CHECK_VALUE (bar->fi.f, 2);
1374 CHECK_VALUE (bar->fi.i, 3);
1375 CHECK_VALUE (bar->ii, 4);
1378 struct fi *fi = gcc_jit_result_get_global (result, "global_struct_fi_23_1");
1380 CHECK_VALUE (fi->f, 2);
1381 CHECK_VALUE (fi->i, 3);
1384 struct fi *fi = gcc_jit_result_get_global (result, "global_struct_fi_23_2");
1386 CHECK_VALUE (fi->f, 2);
1387 CHECK_VALUE (fi->i, 3);
1390 struct fi *fi = gcc_jit_result_get_global (result, "global_struct_fi_00_1");
1392 CHECK_VALUE (fi->f, 0);
1393 CHECK_VALUE (fi->i, 0);
1396 struct fi *fi = gcc_jit_result_get_global (result, "global_struct_fi_00_2");
1398 CHECK_VALUE (fi->f, 0);
1399 CHECK_VALUE (fi->i, 0);
1402 struct fi *fi = gcc_jit_result_get_global (result, "global_struct_fi_0_1");
1404 CHECK_VALUE (fi->f, 0);
1405 CHECK_VALUE (fi->i, 0);
1408 struct fi *fi = gcc_jit_result_get_global (result, "global_struct_fi_0_2");
1410 CHECK_VALUE (fi->f, 0);
1411 CHECK_VALUE (fi->i, 0);
1414 struct fi *fi = gcc_jit_result_get_global (result, "global_struct_fi_6");
1416 CHECK_VALUE (fi->f, 0);
1417 CHECK_VALUE (fi->i, 0);
1420 struct fi *fi = gcc_jit_result_get_global (result, "global_struct_fi_3");
1422 CHECK_VALUE (fi->f, 2 * 2);
1423 CHECK_VALUE (fi->i, 3);
1426 union ubar1 *foo = gcc_jit_result_get_global (result,
1427 "global_union_ufoo_ff3");
1428 CHECK_VALUE (foo->ff, 3);
1431 union ubar1 *foo = gcc_jit_result_get_global (result,
1432 "global_union_ufoo_ii2");
1433 CHECK_VALUE (foo->ii, 2);
1436 union ubar1 *foo = gcc_jit_result_get_global (result,
1437 "global_union_ufoo_ff1c1");
1438 CHECK_VALUE (foo->ff, 1.1f);
1441 union ubar1 *foo = gcc_jit_result_get_global (result,
1442 "global_union_ufoo_0");
1443 CHECK_VALUE (foo->ii, 0);
1446 int *foo = gcc_jit_result_get_global (result, "global_int1_3");
1448 CHECK_VALUE (*foo, 3);
1451 int *foo = gcc_jit_result_get_global (result, "global_cvint1_3");
1453 CHECK_VALUE (*foo, 3);
1456 int *foo = gcc_jit_result_get_global (result, "global_int2_3");
1458 CHECK_VALUE (*foo, 3);
1461 int *foo = gcc_jit_result_get_global (result, "global_int3_18");
1463 CHECK_VALUE (*foo, 18);
1466 int *foo = gcc_jit_result_get_global (result, "global_int_alotofoperators");
1468 CHECK_VALUE (*foo, ~(-((((((2 | 8) & 15) ^ 0) << 3 >> 2) - 1) / 2)));
1471 int *foo = gcc_jit_result_get_global (result, "global_int4_3");
1472 int **pfoo = gcc_jit_result_get_global (result, "global_pint5");
1474 CHECK_VALUE (*foo, 3);
1475 CHECK_VALUE (foo, *pfoo);
1476 CHECK_VALUE (**pfoo, 3);
1479 int * (*foo) (void) = gcc_jit_result_get_code (result, "fn_pint_0");
1480 int **pfoo = gcc_jit_result_get_global (result, "global_pint6");
1482 CHECK_VALUE (*foo (), 0);
1483 CHECK_VALUE (foo (), *pfoo);
1484 CHECK_VALUE (**pfoo, 0);
1487 int *foo = gcc_jit_result_get_global (result, "global_int6_3");
1488 int **pfoo = gcc_jit_result_get_global (result, "global_pint7");
1490 CHECK_VALUE (*foo, 3);
1491 CHECK_VALUE (foo + 1, *pfoo);
1492 CHECK_VALUE (*(*pfoo - 1), 3);
1495 double *foo = gcc_jit_result_get_global (result, "global_double1_3");
1497 CHECK_VALUE (*foo, 3);
1500 double *foo = gcc_jit_result_get_global (result, "global_double2_12");
1502 CHECK_VALUE (*foo, 12);
1505 _Bool *foo = gcc_jit_result_get_global (result, "global_bool1_1");
1507 CHECK_VALUE (*foo, 1);
1510 int *foo = gcc_jit_result_get_global (result, "global_intarr_1234");
1512 CHECK_VALUE (foo[0], 1);
1513 CHECK_VALUE (foo[1], 2);
1514 CHECK_VALUE (foo[2], 3);
1515 CHECK_VALUE (foo[3], 4);
1518 float *foo = gcc_jit_result_get_global (result, "global_floatarr_12");
1520 CHECK_VALUE (foo[0], 1);
1521 CHECK_VALUE (foo[1], 2);
1522 CHECK_VALUE (foo[2], 0);
1523 CHECK_VALUE (foo[3], 0);
1526 float *foo = gcc_jit_result_get_global (result, "global_floatarr_12_2");
1528 CHECK_VALUE (foo[0], 1);
1529 CHECK_VALUE (foo[1], 2);
1530 CHECK_VALUE (foo[2], 0);
1531 CHECK_VALUE (foo[3], 0);
1534 float *foo = gcc_jit_result_get_global (result, "global_floatarr_120");
1536 CHECK_VALUE (foo[0], 1);
1537 CHECK_VALUE (foo[1], 2);
1538 CHECK_VALUE (foo[2], 0);
1539 CHECK_VALUE (foo[3], 0);
1542 float *foo = gcc_jit_result_get_global (result, "global_floatarr_0000");
1544 CHECK_VALUE (foo[0], 0);
1545 CHECK_VALUE (foo[1], 0);
1546 CHECK_VALUE (foo[2], 0);
1547 CHECK_VALUE (foo[3], 0);
1550 float *foo = gcc_jit_result_get_global (result, "global_floatarr_00305600");
1552 float key[] = {0,0,3,0,5,6,0,0};
1554 CHECK_VALUE (memcmp (foo, key, sizeof key), 0);
1557 int **foo = gcc_jit_result_get_global (result, "global_pintarr_x2xx");
1559 CHECK_VALUE (foo[0], 0);
1560 CHECK_VALUE (*foo[1], 2);
1563 char *foo = gcc_jit_result_get_global (result, "global_chararr_qwe");
1564 const char *key = "qwe";
1565 CHECK_VALUE (strcmp (foo, key), 0);
1568 int *foo = gcc_jit_result_get_global (result, "global_int2x2matrix_1234");
1570 for (int i = 0; i < 4; i++)
1571 CHECK_VALUE (foo[i], i + 1);
1574 const char **foo =
1575 gcc_jit_result_get_global (result, "global_cpchararr_qwe_asd");
1577 CHECK_VALUE (strcmp (foo[0], "qwe"), 0);
1578 CHECK_VALUE (strcmp (foo[1], "asd"), 0);
1581 int *foo = gcc_jit_result_get_global (result, "global_lvalueinit_int_3");
1583 CHECK_VALUE (*foo, 3);
1586 int **pint =
1587 gcc_jit_result_get_global (result, "global_pint_4");
1588 int *foo =
1589 gcc_jit_result_get_global (result, "global_int_6");
1590 CHECK_VALUE (**pint, 4);
1591 CHECK_VALUE (*foo, 6);
1594 int (*fn)(void) = gcc_jit_result_get_code (result, "fn_int_11");
1595 CHECK_VALUE (fn (), 11);
1598 int (*fn)(void) = gcc_jit_result_get_code (result, "fn_cint_11");
1599 CHECK_VALUE (fn (), 11);
1602 int *(*fn)(void) = gcc_jit_result_get_code (result, "fn_cint_12");
1603 CHECK_VALUE (*fn (), 12);
1606 short *foo =
1607 gcc_jit_result_get_code (result, "global_lvalueinit_short_3");
1608 CHECK_VALUE (*foo, 3);
1611 int **foo =
1612 gcc_jit_result_get_code (result, "global_lvalueinit_cpcint_3");
1613 CHECK_VALUE (**foo, 3);
1616 int *foo =
1617 gcc_jit_result_get_code (result, "global_lvalueinit_int_4");
1618 CHECK_VALUE (*foo, 4);
1620 int *bar =
1621 gcc_jit_result_get_code (result, "global_const_int_7");
1622 CHECK_VALUE (*bar, 4);
1623 /* The linker does not have to support up to 64 alignment, so test that
1624 it does before testing that it works in libgccjit. */
1625 if ((size_t) &test_aligned64_works_in_linker_1 % 64 == 0 &&
1626 (size_t) &test_aligned64_works_in_linker_2 % 64 == 0)
1627 CHECK_VALUE ((size_t) bar % 64, 0); /* __attribute__ ((aligned (64))) */
1630 union upintsize *foo =
1631 gcc_jit_result_get_code (result, "global_const_upintsize_1");
1632 CHECK_VALUE (foo->p, (void*)0xEEEFBEEF);
1635 struct A_glb *a =
1636 gcc_jit_result_get_code (result, "a_glb");
1637 struct B_glb *b =
1638 gcc_jit_result_get_code (result, "b_glb");
1640 CHECK_VALUE (a->b, b);
1641 CHECK_VALUE (b->a, a);