cgraph: A COMDAT decl always has non-zero address.
[gcc.git] / gcc / testsuite / jit.dg / test-accessing-bitfield.c
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include "libgccjit.h"
5
6 #include "harness.h"
7
8 struct bit_foo
9 {
10 int i:3;
11 int x:5;
12 int y:5;
13 int z:10;
14 int j:3;
15 };
16
17 void
18 create_code (gcc_jit_context *ctxt, void *user_data)
19 {
20 /* Let's try to inject the equivalent of:
21 void
22 test_bitfield_access (struct bit_foo *f)
23 {
24 f->z = f->x + f->y;
25 }
26 */
27 gcc_jit_type *void_type =
28 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
29 gcc_jit_type *int_type =
30 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
31 gcc_jit_field *i =
32 gcc_jit_context_new_bitfield (ctxt,
33 NULL,
34 int_type,
35 3,
36 "i");
37 gcc_jit_field *x =
38 gcc_jit_context_new_bitfield (ctxt,
39 NULL,
40 int_type,
41 5,
42 "x");
43 gcc_jit_field *y =
44 gcc_jit_context_new_bitfield (ctxt,
45 NULL,
46 int_type,
47 5,
48 "y");
49 gcc_jit_field *z =
50 gcc_jit_context_new_bitfield (ctxt,
51 NULL,
52 int_type,
53 10,
54 "z");
55 gcc_jit_field *j =
56 gcc_jit_context_new_bitfield (ctxt,
57 NULL,
58 int_type,
59 3,
60 "j");
61 gcc_jit_field *fields[] = {i, x, y, z, j};
62 gcc_jit_struct *struct_type =
63 gcc_jit_context_new_struct_type (ctxt, NULL, "bit_foo", 5, fields);
64 gcc_jit_type *ptr_type =
65 gcc_jit_type_get_pointer (gcc_jit_struct_as_type (struct_type));
66
67 /* Build the test function. */
68 gcc_jit_param *param_f =
69 gcc_jit_context_new_param (ctxt, NULL, ptr_type, "f");
70 gcc_jit_function *test_fn =
71 gcc_jit_context_new_function (ctxt, NULL,
72 GCC_JIT_FUNCTION_EXPORTED,
73 void_type,
74 "test_bitfield_access",
75 1, &param_f,
76 0);
77
78 /* f->x + f->y */
79 gcc_jit_rvalue *sum =
80 gcc_jit_context_new_binary_op (
81 ctxt, NULL,
82 GCC_JIT_BINARY_OP_PLUS,
83 int_type,
84 gcc_jit_lvalue_as_rvalue (
85 gcc_jit_rvalue_dereference_field (
86 gcc_jit_param_as_rvalue (param_f),
87 NULL,
88 x)),
89 gcc_jit_lvalue_as_rvalue (
90 gcc_jit_rvalue_dereference_field (
91 gcc_jit_param_as_rvalue (param_f),
92 NULL,
93 y)));
94
95 /* f->z = ... */
96 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
97 gcc_jit_block_add_assignment (
98 block,
99 NULL,
100 gcc_jit_rvalue_dereference_field (
101 gcc_jit_param_as_rvalue (param_f),
102 NULL,
103 z),
104 sum);
105 gcc_jit_block_end_with_void_return (block, NULL);
106 }
107
108 void
109 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
110 {
111 typedef void (*fn_type) (struct bit_foo *);
112 CHECK_NON_NULL (result);
113
114 fn_type test_bitfield_access =
115 (fn_type)gcc_jit_result_get_code (result, "test_bitfield_access");
116 CHECK_NON_NULL (test_bitfield_access);
117
118 struct bit_foo tmp;
119 tmp.i = 3;
120 tmp.x = 5;
121 tmp.y = 7;
122 tmp.z = 0;
123 tmp.j = 3;
124
125 /* Call the JIT-generated function. */
126 test_bitfield_access (&tmp);
127
128 /* Verify that the code correctly modified the field "z". */
129 CHECK_VALUE (tmp.z, 12);
130 }