PR lto/84212 - -Wno-* does not disable warnings from -flto link stage
[official-gcc.git] / gcc / testsuite / jit.dg / test-accessing-union.c
blob15656e9bd4c972b0116a1a826a37baf4d7eab532
1 #include <stdlib.h>
2 #include <stdio.h>
4 #include "libgccjit.h"
6 #include "harness.h"
8 /* Quote from here in docs/topics/types.rst. */
10 union int_or_float
12 int as_int;
13 float as_float;
16 void
17 create_code (gcc_jit_context *ctxt, void *user_data)
19 /* Let's try to inject the equivalent of:
20 float
21 test_union (int i)
23 union int_or_float u;
24 u.as_int = i;
25 return u.as_float;
28 gcc_jit_type *int_type =
29 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
30 gcc_jit_type *float_type =
31 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT);
32 gcc_jit_field *as_int =
33 gcc_jit_context_new_field (ctxt,
34 NULL,
35 int_type,
36 "as_int");
37 gcc_jit_field *as_float =
38 gcc_jit_context_new_field (ctxt,
39 NULL,
40 float_type,
41 "as_float");
42 gcc_jit_field *fields[] = {as_int, as_float};
43 gcc_jit_type *union_type =
44 gcc_jit_context_new_union_type (ctxt, NULL,
45 "int_or_float", 2, fields);
47 /* Build the test function. */
48 gcc_jit_param *param_i =
49 gcc_jit_context_new_param (ctxt, NULL, int_type, "i");
50 gcc_jit_function *test_fn =
51 gcc_jit_context_new_function (ctxt, NULL,
52 GCC_JIT_FUNCTION_EXPORTED,
53 float_type,
54 "test_union",
55 1, &param_i,
56 0);
58 gcc_jit_lvalue *u =
59 gcc_jit_function_new_local (test_fn, NULL,
60 union_type, "u");
62 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
64 /* u.as_int = i; */
65 gcc_jit_block_add_assignment (
66 block,
67 NULL,
68 /* "u.as_int = ..." */
69 gcc_jit_lvalue_access_field (u,
70 NULL,
71 as_int),
72 gcc_jit_param_as_rvalue (param_i));
74 /* return u.as_float; */
75 gcc_jit_block_end_with_return (
76 block, NULL,
77 gcc_jit_rvalue_access_field (gcc_jit_lvalue_as_rvalue (u),
78 NULL,
79 as_float));
82 /* Quote up to here in docs/topics/types.rst. */
84 void
85 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
87 typedef float (*fn_type) (int i);
88 CHECK_NON_NULL (result);
90 fn_type test_union =
91 (fn_type)gcc_jit_result_get_code (result, "test_union");
92 CHECK_NON_NULL (test_union);
94 /* Call the JIT-generated function. */
95 float f_result = test_union (42);
97 union int_or_float u;
98 u.as_float = f_result;
100 CHECK_VALUE (u.as_int, 42);