11 /* Verify that struct layout works properly when adding an array field. */
15 int m_ints
[ARRAY_SIZE
];
20 create_code (gcc_jit_context
*ctxt
, void *user_data
)
22 /* Let's try to inject the equivalent of:
25 test_array (struct array_holder *ah)
28 for i in 0 to (ARRAY_SIZE - 1):
29 ah->m_ints[i] = (i * i);
33 gcc_jit_type
*void_type
=
34 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_VOID
);
35 gcc_jit_type
*float_type
=
36 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_FLOAT
);
37 gcc_jit_type
*int_type
=
38 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
40 gcc_jit_field
*field_m_before
=
41 gcc_jit_context_new_field (ctxt
, NULL
, float_type
, "m_before");
42 gcc_jit_field
*field_m_ints
=
43 gcc_jit_context_new_field (
45 gcc_jit_context_new_array_type (ctxt
, NULL
, int_type
, ARRAY_SIZE
),
47 gcc_jit_field
*field_m_after
=
48 gcc_jit_context_new_field (ctxt
, NULL
, float_type
, "m_after");
50 gcc_jit_field
*fields
[] = {
56 gcc_jit_struct
*struct_type
=
57 gcc_jit_context_new_struct_type (
63 gcc_jit_type
*struct_ptr_type
=
64 gcc_jit_type_get_pointer (gcc_jit_struct_as_type (struct_type
));
66 /* Build the test_fn. */
67 gcc_jit_param
*param_ah
=
68 gcc_jit_context_new_param (ctxt
, NULL
, struct_ptr_type
, "ah");
69 gcc_jit_function
*func
=
70 gcc_jit_context_new_function (ctxt
, NULL
,
71 GCC_JIT_FUNCTION_EXPORTED
,
77 gcc_jit_block
*initial
= gcc_jit_function_new_block (func
, "initial");
78 gcc_jit_block
*loop_test
= gcc_jit_function_new_block (func
, "loop_test");
79 gcc_jit_block
*loop_body
= gcc_jit_function_new_block (func
, "loop_body");
80 gcc_jit_block
*final
= gcc_jit_function_new_block (func
, "final");
82 /* "ah->m_before = 4.0f;" */
83 gcc_jit_block_add_assignment (
85 gcc_jit_rvalue_dereference_field (
86 gcc_jit_param_as_rvalue (param_ah
), NULL
, field_m_before
),
87 gcc_jit_context_new_rvalue_from_int (ctxt
, float_type
, 4));
89 gcc_jit_block_add_comment (initial
, NULL
,
90 "for i in 0 to (ARRAY_SIZE - 1):");
92 gcc_jit_function_new_local (func
, NULL
, int_type
, "i");
93 gcc_jit_block_add_assignment (initial
, NULL
,
95 gcc_jit_context_zero (ctxt
, int_type
));
97 gcc_jit_block_end_with_jump (initial
, NULL
, loop_test
);
99 gcc_jit_block_end_with_conditional (loop_test
, NULL
,
100 gcc_jit_context_new_comparison (
102 GCC_JIT_COMPARISON_LT
,
103 gcc_jit_lvalue_as_rvalue (i
),
104 gcc_jit_context_new_rvalue_from_int (ctxt
, int_type
, ARRAY_SIZE
)),
108 gcc_jit_block_add_comment (loop_body
, NULL
, "ah->m_ints[i] = (i * i);");
109 gcc_jit_block_add_assignment (
111 gcc_jit_context_new_array_access (
113 gcc_jit_lvalue_as_rvalue (gcc_jit_rvalue_dereference_field (
114 gcc_jit_param_as_rvalue (param_ah
),
117 gcc_jit_lvalue_as_rvalue (i
)),
118 gcc_jit_context_new_binary_op (
120 GCC_JIT_BINARY_OP_MULT
,
122 gcc_jit_lvalue_as_rvalue (i
),
123 gcc_jit_lvalue_as_rvalue (i
)));
126 gcc_jit_block_add_assignment_op (
129 GCC_JIT_BINARY_OP_PLUS
,
130 gcc_jit_context_one (ctxt
, int_type
));
132 gcc_jit_block_end_with_jump (loop_body
, NULL
, loop_test
);
134 /* ah->m_after = 2.0f; */
135 gcc_jit_block_add_assignment (
137 gcc_jit_rvalue_dereference_field (
138 gcc_jit_param_as_rvalue (param_ah
), NULL
, field_m_after
),
139 gcc_jit_context_new_rvalue_from_int (ctxt
, float_type
, 2));
140 gcc_jit_block_end_with_void_return (final
, NULL
);
145 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
147 typedef void (*fn_type
) (struct array_holder
*ah
);
149 CHECK_NON_NULL (result
);
151 (fn_type
)gcc_jit_result_get_code (result
, "test_array");
152 CHECK_NON_NULL (test_array
);
154 struct array_holder ah
;
155 memset (&ah
, 0xf0, sizeof (ah
));
158 CHECK_VALUE (ah
.m_before
, 4.0f
);
159 CHECK_VALUE (ah
.m_ints
[0], 0);
160 CHECK_VALUE (ah
.m_ints
[1], 1);
161 CHECK_VALUE (ah
.m_ints
[2], 4);
162 CHECK_VALUE (ah
.m_ints
[3], 9);
163 CHECK_VALUE (ah
.m_after
, 2.0f
);