10 /**********************************************************************
12 **********************************************************************/
15 make_test_of_unary_op (gcc_jit_context
*ctxt
,
17 enum gcc_jit_unary_op op
,
20 /* Make a test function of the form:
25 and return a debug dump of the OP so that
26 the caller can sanity-check the debug dump implementation.
28 gcc_jit_param
*param_a
=
29 gcc_jit_context_new_param (ctxt
, NULL
, type
, "a");
30 gcc_jit_function
*test_fn
=
31 gcc_jit_context_new_function (ctxt
, NULL
,
32 GCC_JIT_FUNCTION_EXPORTED
,
37 gcc_jit_rvalue
*unary_op
= gcc_jit_context_new_unary_op (
42 gcc_jit_param_as_rvalue (param_a
));
44 gcc_jit_block
*initial
= gcc_jit_function_new_block (test_fn
, "initial");
45 gcc_jit_block_end_with_return (initial
, NULL
, unary_op
);
47 return gcc_jit_object_get_debug_string (
48 gcc_jit_rvalue_as_object (unary_op
));
53 make_tests_of_unary_ops (gcc_jit_context
*ctxt
)
55 gcc_jit_type
*int_type
=
56 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
59 make_test_of_unary_op (ctxt
,
61 GCC_JIT_UNARY_OP_MINUS
,
62 "test_UNARY_OP_MINUS_on_int"),
65 make_test_of_unary_op (ctxt
,
67 GCC_JIT_UNARY_OP_BITWISE_NEGATE
,
68 "test_UNARY_OP_BITWISE_NEGATE_on_int"),
71 make_test_of_unary_op (ctxt
,
73 GCC_JIT_UNARY_OP_LOGICAL_NEGATE
,
74 "test_UNARY_OP_LOGICAL_NEGATE_on_int"),
77 make_test_of_unary_op (ctxt
,
80 "test_UNARY_OP_ABS_on_int"),
85 verify_unary_ops (gcc_jit_result
*result
)
87 typedef int (*test_fn
) (int);
89 test_fn test_UNARY_OP_MINUS_on_int
=
90 (test_fn
)gcc_jit_result_get_code (result
,
91 "test_UNARY_OP_MINUS_on_int");
92 CHECK_NON_NULL (test_UNARY_OP_MINUS_on_int
);
93 CHECK_VALUE (test_UNARY_OP_MINUS_on_int (0), 0);
94 CHECK_VALUE (test_UNARY_OP_MINUS_on_int (42), -42);
95 CHECK_VALUE (test_UNARY_OP_MINUS_on_int (-5), 5);
97 test_fn test_UNARY_OP_BITWISE_NEGATE_on_int
=
98 (test_fn
)gcc_jit_result_get_code (result
,
99 "test_UNARY_OP_BITWISE_NEGATE_on_int");
100 CHECK_NON_NULL (test_UNARY_OP_BITWISE_NEGATE_on_int
);
101 CHECK_VALUE (test_UNARY_OP_BITWISE_NEGATE_on_int (0), ~0);
102 CHECK_VALUE (test_UNARY_OP_BITWISE_NEGATE_on_int (42), ~42);
103 CHECK_VALUE (test_UNARY_OP_BITWISE_NEGATE_on_int (-5), ~-5);
105 test_fn test_UNARY_OP_LOGICAL_NEGATE_on_int
=
106 (test_fn
)gcc_jit_result_get_code (result
,
107 "test_UNARY_OP_LOGICAL_NEGATE_on_int");
108 CHECK_NON_NULL (test_UNARY_OP_LOGICAL_NEGATE_on_int
);
109 CHECK_VALUE (test_UNARY_OP_LOGICAL_NEGATE_on_int (0), 1);
110 CHECK_VALUE (test_UNARY_OP_LOGICAL_NEGATE_on_int (42), 0);
111 CHECK_VALUE (test_UNARY_OP_LOGICAL_NEGATE_on_int (-5), 0);
113 test_fn test_UNARY_OP_ABS_on_int
=
114 (test_fn
)gcc_jit_result_get_code (result
,
115 "test_UNARY_OP_ABS_on_int");
116 CHECK_NON_NULL (test_UNARY_OP_ABS_on_int
);
117 CHECK_VALUE (test_UNARY_OP_ABS_on_int (0), 0);
118 CHECK_VALUE (test_UNARY_OP_ABS_on_int (42), 42);
119 CHECK_VALUE (test_UNARY_OP_ABS_on_int (-5), 5);
122 /**********************************************************************
124 **********************************************************************/
127 make_test_of_binary_op (gcc_jit_context
*ctxt
,
129 enum gcc_jit_binary_op op
,
130 const char *funcname
)
132 /* Make a test function of the form:
133 T test_binary_op (T a, T b)
138 gcc_jit_param
*param_a
=
139 gcc_jit_context_new_param (ctxt
, NULL
, type
, "a");
140 gcc_jit_param
*param_b
=
141 gcc_jit_context_new_param (ctxt
, NULL
, type
, "b");
142 gcc_jit_param
*params
[] = {param_a
, param_b
};
143 gcc_jit_function
*test_fn
=
144 gcc_jit_context_new_function (ctxt
, NULL
,
145 GCC_JIT_FUNCTION_EXPORTED
,
150 gcc_jit_rvalue
*binary_op
=
151 gcc_jit_context_new_binary_op (
156 gcc_jit_param_as_rvalue (param_a
),
157 gcc_jit_param_as_rvalue (param_b
));
159 gcc_jit_block
*initial
= gcc_jit_function_new_block (test_fn
, "initial");
160 gcc_jit_block_end_with_return (initial
, NULL
, binary_op
);
162 return gcc_jit_object_get_debug_string (
163 gcc_jit_rvalue_as_object (binary_op
));
168 make_tests_of_binary_ops (gcc_jit_context
*ctxt
)
170 gcc_jit_type
*int_type
=
171 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
173 /* Test binary ops. */
175 make_test_of_binary_op (ctxt
,
177 GCC_JIT_BINARY_OP_PLUS
,
178 "test_BINARY_OP_PLUS_on_int"),
181 make_test_of_binary_op (ctxt
,
183 GCC_JIT_BINARY_OP_MINUS
,
184 "test_BINARY_OP_MINUS_on_int"),
187 make_test_of_binary_op (ctxt
,
189 GCC_JIT_BINARY_OP_MULT
,
190 "test_BINARY_OP_MULT_on_int"),
193 make_test_of_binary_op (ctxt
,
195 GCC_JIT_BINARY_OP_DIVIDE
,
196 "test_BINARY_OP_DIVIDE_on_int"),
198 /* TODO: test for DIVIDE on float or double */
200 make_test_of_binary_op (ctxt
,
202 GCC_JIT_BINARY_OP_MODULO
,
203 "test_BINARY_OP_MODULO_on_int"),
206 make_test_of_binary_op (ctxt
,
208 GCC_JIT_BINARY_OP_BITWISE_AND
,
209 "test_BINARY_OP_BITWISE_AND_on_int"),
212 make_test_of_binary_op (ctxt
,
214 GCC_JIT_BINARY_OP_BITWISE_XOR
,
215 "test_BINARY_OP_BITWISE_XOR_on_int"),
218 make_test_of_binary_op (ctxt
,
220 GCC_JIT_BINARY_OP_BITWISE_OR
,
221 "test_BINARY_OP_BITWISE_OR_on_int"),
224 make_test_of_binary_op (ctxt
,
226 GCC_JIT_BINARY_OP_LOGICAL_AND
,
227 "test_BINARY_OP_LOGICAL_AND_on_int"),
230 make_test_of_binary_op (ctxt
,
232 GCC_JIT_BINARY_OP_LOGICAL_OR
,
233 "test_BINARY_OP_LOGICAL_OR_on_int"),
236 make_test_of_binary_op (ctxt
,
238 GCC_JIT_BINARY_OP_LSHIFT
,
239 "test_BINARY_OP_LSHIFT_on_int"),
242 make_test_of_binary_op (ctxt
,
244 GCC_JIT_BINARY_OP_RSHIFT
,
245 "test_BINARY_OP_RSHIFT_on_int"),
250 verify_binary_ops (gcc_jit_result
*result
)
252 typedef int (*test_fn
) (int, int);
254 test_fn test_BINARY_OP_PLUS_on_int
=
255 (test_fn
)gcc_jit_result_get_code (result
,
256 "test_BINARY_OP_PLUS_on_int");
257 CHECK_NON_NULL (test_BINARY_OP_PLUS_on_int
);
258 CHECK_VALUE (test_BINARY_OP_PLUS_on_int (0, 0), 0);
259 CHECK_VALUE (test_BINARY_OP_PLUS_on_int (1, 2), 3);
260 CHECK_VALUE (test_BINARY_OP_PLUS_on_int (100, -1), 99);
261 CHECK_VALUE (test_BINARY_OP_PLUS_on_int (-1, -4), -5);
263 test_fn test_BINARY_OP_MINUS_on_int
=
264 (test_fn
)gcc_jit_result_get_code (result
,
265 "test_BINARY_OP_MINUS_on_int");
266 CHECK_NON_NULL (test_BINARY_OP_MINUS_on_int
);
267 CHECK_VALUE (test_BINARY_OP_MINUS_on_int (0, 0), 0);
268 CHECK_VALUE (test_BINARY_OP_MINUS_on_int (1, 2), -1);
269 CHECK_VALUE (test_BINARY_OP_MINUS_on_int (100, -1), 101);
270 CHECK_VALUE (test_BINARY_OP_MINUS_on_int (-1, -4), 3);
272 test_fn test_BINARY_OP_MULT_on_int
=
273 (test_fn
)gcc_jit_result_get_code (result
,
274 "test_BINARY_OP_MULT_on_int");
275 CHECK_NON_NULL (test_BINARY_OP_MULT_on_int
);
276 CHECK_VALUE (test_BINARY_OP_MULT_on_int (0, 0), 0);
277 CHECK_VALUE (test_BINARY_OP_MULT_on_int (1, 2), 2);
278 CHECK_VALUE (test_BINARY_OP_MULT_on_int (100, -1), -100);
279 CHECK_VALUE (test_BINARY_OP_MULT_on_int (-1, -4), 4);
280 CHECK_VALUE (test_BINARY_OP_MULT_on_int (7, 10), 70);
282 test_fn test_BINARY_OP_DIVIDE_on_int
=
283 (test_fn
)gcc_jit_result_get_code (result
,
284 "test_BINARY_OP_DIVIDE_on_int");
285 CHECK_NON_NULL (test_BINARY_OP_DIVIDE_on_int
);
286 CHECK_VALUE (test_BINARY_OP_DIVIDE_on_int (7, 2), 3);
287 CHECK_VALUE (test_BINARY_OP_DIVIDE_on_int (100, -1), (100 / -1));
288 CHECK_VALUE (test_BINARY_OP_DIVIDE_on_int (-1, -4), (-1 / -4));
289 CHECK_VALUE (test_BINARY_OP_DIVIDE_on_int (60, 5), 12);
291 /* TODO: test for DIVIDE on float or double */
293 test_fn test_BINARY_OP_MODULO_on_int
=
294 (test_fn
)gcc_jit_result_get_code (result
,
295 "test_BINARY_OP_MODULO_on_int");
296 CHECK_NON_NULL (test_BINARY_OP_MODULO_on_int
);
297 CHECK_VALUE (test_BINARY_OP_MODULO_on_int (7, 2), 1);
298 CHECK_VALUE (test_BINARY_OP_MODULO_on_int (100, -1), (100 % -1));
299 CHECK_VALUE (test_BINARY_OP_MODULO_on_int (-1, -4), (-1 % -4));
300 CHECK_VALUE (test_BINARY_OP_MODULO_on_int (60, 5), 0);
302 test_fn test_BINARY_OP_BITWISE_AND_on_int
=
303 (test_fn
)gcc_jit_result_get_code (result
,
304 "test_BINARY_OP_BITWISE_AND_on_int");
305 CHECK_NON_NULL (test_BINARY_OP_BITWISE_AND_on_int
);
306 CHECK_VALUE (test_BINARY_OP_BITWISE_AND_on_int (0xf0f0, 0x7777), 0x7070);
308 test_fn test_BINARY_OP_BITWISE_XOR_on_int
=
309 (test_fn
)gcc_jit_result_get_code (result
,
310 "test_BINARY_OP_BITWISE_XOR_on_int");
311 CHECK_NON_NULL (test_BINARY_OP_BITWISE_XOR_on_int
);
312 CHECK_VALUE (test_BINARY_OP_BITWISE_XOR_on_int (0xf0f0, 0x7777), 0x8787);
314 test_fn test_BINARY_OP_BITWISE_OR_on_int
=
315 (test_fn
)gcc_jit_result_get_code (result
,
316 "test_BINARY_OP_BITWISE_OR_on_int");
317 CHECK_NON_NULL (test_BINARY_OP_BITWISE_OR_on_int
);
318 CHECK_VALUE (test_BINARY_OP_BITWISE_OR_on_int (0xf0f0, 0x7777), 0xf7f7);
320 test_fn test_BINARY_OP_LOGICAL_AND_on_int
=
321 (test_fn
)gcc_jit_result_get_code (result
,
322 "test_BINARY_OP_LOGICAL_AND_on_int");
323 CHECK_NON_NULL (test_BINARY_OP_LOGICAL_AND_on_int
);
324 CHECK_VALUE (test_BINARY_OP_LOGICAL_AND_on_int (0, 0), 0);
325 CHECK_VALUE (test_BINARY_OP_LOGICAL_AND_on_int (42, 0), 0);
326 CHECK_VALUE (test_BINARY_OP_LOGICAL_AND_on_int (0, -13), 0);
327 CHECK_VALUE (test_BINARY_OP_LOGICAL_AND_on_int (1997, 1998), 1);
329 test_fn test_BINARY_OP_LOGICAL_OR_on_int
=
330 (test_fn
)gcc_jit_result_get_code (result
,
331 "test_BINARY_OP_LOGICAL_OR_on_int");
332 CHECK_NON_NULL (test_BINARY_OP_LOGICAL_OR_on_int
);
333 CHECK_VALUE (test_BINARY_OP_LOGICAL_OR_on_int (0, 0), 0);
334 CHECK_VALUE (test_BINARY_OP_LOGICAL_OR_on_int (42, 0), 1);
335 CHECK_VALUE (test_BINARY_OP_LOGICAL_OR_on_int (0, -13), 1);
336 CHECK_VALUE (test_BINARY_OP_LOGICAL_OR_on_int (1997, 1998), 1);
338 test_fn test_BINARY_OP_LSHIFT_on_int
=
339 (test_fn
)gcc_jit_result_get_code (result
,
340 "test_BINARY_OP_LSHIFT_on_int");
341 CHECK_NON_NULL (test_BINARY_OP_LSHIFT_on_int
);
342 CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (0, 0), 0);
343 CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (0, 1), 0);
344 CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (0, 2), 0);
345 CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (1, 0), 1);
346 CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (1, 1), 2);
347 CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (1, 2), 4);
348 CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (1, 3), 8);
349 CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (3, 0), 3);
350 CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (3, 1), 6);
351 CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (3, 5), 3 * 32);
352 CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (42, 0), 42);
353 CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (42, 1), 84);
355 test_fn test_BINARY_OP_RSHIFT_on_int
=
356 (test_fn
)gcc_jit_result_get_code (result
,
357 "test_BINARY_OP_RSHIFT_on_int");
358 CHECK_NON_NULL (test_BINARY_OP_RSHIFT_on_int
);
359 CHECK_VALUE (test_BINARY_OP_RSHIFT_on_int (0, 0), 0);
360 CHECK_VALUE (test_BINARY_OP_RSHIFT_on_int (42, 0), 42);
361 CHECK_VALUE (test_BINARY_OP_RSHIFT_on_int (42, 1), 21);
362 CHECK_VALUE (test_BINARY_OP_RSHIFT_on_int (42, 2), 10);
365 /**********************************************************************
367 **********************************************************************/
370 make_test_of_comparison (gcc_jit_context
*ctxt
,
372 enum gcc_jit_comparison op
,
373 const char *funcname
)
375 /* Make a test function of the form:
376 bool test_comparison_op (T a, T b)
381 gcc_jit_param
*param_a
=
382 gcc_jit_context_new_param (ctxt
, NULL
, type
, "a");
383 gcc_jit_param
*param_b
=
384 gcc_jit_context_new_param (ctxt
, NULL
, type
, "b");
385 gcc_jit_param
*params
[] = {param_a
, param_b
};
386 gcc_jit_type
*bool_type
=
387 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_BOOL
);
388 gcc_jit_function
*test_fn
=
389 gcc_jit_context_new_function (ctxt
, NULL
,
390 GCC_JIT_FUNCTION_EXPORTED
,
395 gcc_jit_rvalue
*comparison
=
396 gcc_jit_context_new_comparison (
400 gcc_jit_param_as_rvalue (param_a
),
401 gcc_jit_param_as_rvalue (param_b
));
403 gcc_jit_block
*initial
= gcc_jit_function_new_block (test_fn
, "initial");
404 gcc_jit_block_end_with_return (initial
, NULL
, comparison
);
406 return gcc_jit_object_get_debug_string (
407 gcc_jit_rvalue_as_object (comparison
));
411 make_tests_of_comparisons (gcc_jit_context
*ctxt
)
413 gcc_jit_type
*int_type
=
414 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
417 make_test_of_comparison (ctxt
,
419 GCC_JIT_COMPARISON_EQ
,
420 "test_COMPARISON_EQ_on_int"),
423 make_test_of_comparison (ctxt
,
425 GCC_JIT_COMPARISON_NE
,
426 "test_COMPARISON_NE_on_int"),
429 make_test_of_comparison (ctxt
,
431 GCC_JIT_COMPARISON_LT
,
432 "test_COMPARISON_LT_on_int"),
435 make_test_of_comparison (ctxt
,
437 GCC_JIT_COMPARISON_LE
,
438 "test_COMPARISON_LE_on_int"),
441 make_test_of_comparison (ctxt
,
443 GCC_JIT_COMPARISON_GT
,
444 "test_COMPARISON_GT_on_int"),
447 make_test_of_comparison (ctxt
,
449 GCC_JIT_COMPARISON_GE
,
450 "test_COMPARISON_GE_on_int"),
455 verify_comparisons (gcc_jit_result
*result
)
457 typedef bool (*test_fn
) (int, int);
459 test_fn test_COMPARISON_EQ_on_int
=
460 (test_fn
)gcc_jit_result_get_code (result
,
461 "test_COMPARISON_EQ_on_int");
462 CHECK_NON_NULL (test_COMPARISON_EQ_on_int
);
463 CHECK_VALUE (test_COMPARISON_EQ_on_int (0, 0), 1);
464 CHECK_VALUE (test_COMPARISON_EQ_on_int (1, 2), 0);
466 test_fn test_COMPARISON_NE_on_int
=
467 (test_fn
)gcc_jit_result_get_code (result
,
468 "test_COMPARISON_NE_on_int");
469 CHECK_NON_NULL (test_COMPARISON_NE_on_int
);
470 CHECK_VALUE (test_COMPARISON_NE_on_int (0, 0), 0);
471 CHECK_VALUE (test_COMPARISON_NE_on_int (1, 2), 1);
473 test_fn test_COMPARISON_LT_on_int
=
474 (test_fn
)gcc_jit_result_get_code (result
,
475 "test_COMPARISON_LT_on_int");
476 CHECK_NON_NULL (test_COMPARISON_LT_on_int
);
477 CHECK_VALUE (test_COMPARISON_LT_on_int (0, 0), 0);
478 CHECK_VALUE (test_COMPARISON_LT_on_int (1, 2), 1);
479 CHECK_VALUE (test_COMPARISON_LT_on_int (2, 1), 0);
480 CHECK_VALUE (test_COMPARISON_LT_on_int (-2, 1), 1);
482 test_fn test_COMPARISON_LE_on_int
=
483 (test_fn
)gcc_jit_result_get_code (result
,
484 "test_COMPARISON_LE_on_int");
485 CHECK_NON_NULL (test_COMPARISON_LE_on_int
);
486 CHECK_VALUE (test_COMPARISON_LE_on_int (0, 0), 1);
487 CHECK_VALUE (test_COMPARISON_LE_on_int (1, 2), 1);
488 CHECK_VALUE (test_COMPARISON_LE_on_int (2, 1), 0);
490 test_fn test_COMPARISON_GT_on_int
=
491 (test_fn
)gcc_jit_result_get_code (result
,
492 "test_COMPARISON_GT_on_int");
493 CHECK_NON_NULL (test_COMPARISON_GT_on_int
);
494 CHECK_VALUE (test_COMPARISON_GT_on_int (0, 0), 0);
495 CHECK_VALUE (test_COMPARISON_GT_on_int (1, 2), 0);
496 CHECK_VALUE (test_COMPARISON_GT_on_int (2, 1), 1);
498 test_fn test_COMPARISON_GE_on_int
=
499 (test_fn
)gcc_jit_result_get_code (result
,
500 "test_COMPARISON_GE_on_int");
501 CHECK_NON_NULL (test_COMPARISON_GE_on_int
);
502 CHECK_VALUE (test_COMPARISON_GE_on_int (0, 0), 1);
503 CHECK_VALUE (test_COMPARISON_GE_on_int (1, 2), 0);
504 CHECK_VALUE (test_COMPARISON_GE_on_int (2, 1), 1);
507 /**********************************************************************
509 **********************************************************************/
512 make_test_of_cast (gcc_jit_context
*ctxt
,
513 gcc_jit_type
*input_type
,
514 gcc_jit_type
*output_type
,
515 const char *funcname
)
517 /* Make a test function of the form:
518 OUTPUT_TYPE test_cast_* (INPUT_TYPE a)
520 return (OUTPUT_TYPE)a;
523 gcc_jit_param
*param_a
=
524 gcc_jit_context_new_param (ctxt
, NULL
, input_type
, "a");
525 gcc_jit_param
*params
[] = {param_a
};
526 gcc_jit_function
*test_fn
=
527 gcc_jit_context_new_function (ctxt
, NULL
,
528 GCC_JIT_FUNCTION_EXPORTED
,
533 gcc_jit_rvalue
*cast
=
534 gcc_jit_context_new_cast (
537 gcc_jit_param_as_rvalue (param_a
),
539 gcc_jit_block
*initial
= gcc_jit_function_new_block (test_fn
, "initial");
540 gcc_jit_block_end_with_return (initial
, NULL
, cast
);
542 return gcc_jit_object_get_debug_string (
543 gcc_jit_rvalue_as_object (cast
));
546 /* For use by test_cast_from_array_of_ints_to_int_ptr. */
547 extern int called_pointer_checking_function (int *ints
)
549 CHECK_VALUE (ints
[0], 10);
550 CHECK_VALUE (ints
[1], 4);
551 return ints
[0] * ints
[1];
555 make_tests_of_casts (gcc_jit_context
*ctxt
)
557 gcc_jit_type
*int_type
=
558 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
559 gcc_jit_type
*long_type
=
560 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_LONG
);
561 gcc_jit_type
*float_type
=
562 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_FLOAT
);
563 gcc_jit_type
*bool_type
=
564 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_BOOL
);
565 gcc_jit_type
*array_int_type
=
566 gcc_jit_context_new_array_type (ctxt
, NULL
,
569 gcc_jit_type
*int_ptr_type
=
570 gcc_jit_type_get_pointer (int_type
);
572 /* float/int conversions */
574 make_test_of_cast (ctxt
,
577 "test_cast_from_float_to_int"),
580 make_test_of_cast (ctxt
,
583 "test_cast_from_int_to_float"),
586 /* bool/int conversions */
588 make_test_of_cast (ctxt
,
591 "test_cast_from_bool_to_int"),
594 make_test_of_cast (ctxt
,
597 "test_cast_from_int_to_bool"),
600 /* bool/long conversions */
602 make_test_of_cast (ctxt
,
605 "test_cast_from_bool_to_long"),
608 make_test_of_cast (ctxt
,
611 "test_cast_from_long_to_bool"),
614 /* array/ptr conversions */
616 gcc_jit_function
*test_fn
=
617 gcc_jit_context_new_function (
619 GCC_JIT_FUNCTION_EXPORTED
,
621 "test_cast_from_array_of_ints_to_int_ptr",
625 int test_cast_from_array_of_ints_to_int_ptr (void)
630 return called_pointer_checking_function (array);
634 gcc_jit_param
*param_ints
=
635 gcc_jit_context_new_param (ctxt
, NULL
, int_ptr_type
, "ints");
636 gcc_jit_function
*called_fn
=
637 gcc_jit_context_new_function (
639 GCC_JIT_FUNCTION_IMPORTED
,
641 "called_pointer_checking_function",
645 gcc_jit_lvalue
*array
=
646 gcc_jit_function_new_local (test_fn
, NULL
,
649 gcc_jit_block
*block
=
650 gcc_jit_function_new_block (test_fn
, "block");
652 gcc_jit_block_add_assignment (
654 gcc_jit_context_new_array_access (
656 gcc_jit_lvalue_as_rvalue (array
),
657 gcc_jit_context_new_rvalue_from_int (ctxt
, int_type
, 0)),
658 gcc_jit_context_new_rvalue_from_int (ctxt
, int_type
, 10));
660 gcc_jit_block_add_assignment (
662 gcc_jit_context_new_array_access (
664 gcc_jit_lvalue_as_rvalue (array
),
665 gcc_jit_context_new_rvalue_from_int (ctxt
, int_type
, 1)),
666 gcc_jit_context_new_rvalue_from_int (ctxt
, int_type
, 4));
667 gcc_jit_rvalue
*cast
=
668 gcc_jit_context_new_cast (
671 /* We need a get_address here. */
672 gcc_jit_lvalue_get_address (array
, NULL
),
674 gcc_jit_block_end_with_return (
676 gcc_jit_context_new_call (
682 gcc_jit_object_get_debug_string (
683 gcc_jit_rvalue_as_object (cast
)),
689 verify_casts (gcc_jit_result
*result
)
693 typedef int (*fn_type
) (float);
694 fn_type test_cast_from_float_to_int
=
695 (fn_type
)gcc_jit_result_get_code (result
,
696 "test_cast_from_float_to_int");
697 CHECK_NON_NULL (test_cast_from_float_to_int
);
698 CHECK_VALUE (test_cast_from_float_to_int (4.2), 4);
703 typedef float (*fn_type
) (int);
704 fn_type test_cast_from_int_to_float
=
705 (fn_type
)gcc_jit_result_get_code (result
,
706 "test_cast_from_int_to_float");
707 CHECK_NON_NULL (test_cast_from_int_to_float
);
708 CHECK_VALUE (test_cast_from_int_to_float (4), 4.0);
713 typedef int (*fn_type
) (bool);
714 fn_type test_cast_from_bool_to_int
=
715 (fn_type
)gcc_jit_result_get_code (result
,
716 "test_cast_from_bool_to_int");
717 CHECK_NON_NULL (test_cast_from_bool_to_int
);
718 CHECK_VALUE (test_cast_from_bool_to_int (0), 0);
719 CHECK_VALUE (test_cast_from_bool_to_int (1), 1);
724 typedef bool (*fn_type
) (int);
725 fn_type test_cast_from_int_to_bool
=
726 (fn_type
)gcc_jit_result_get_code (result
,
727 "test_cast_from_int_to_bool");
728 CHECK_NON_NULL (test_cast_from_int_to_bool
);
729 CHECK_VALUE (test_cast_from_int_to_bool (0), 0);
730 CHECK_VALUE (test_cast_from_int_to_bool (1), 1);
735 typedef long (*fn_type
) (bool);
736 fn_type test_cast_from_bool_to_long
=
737 (fn_type
)gcc_jit_result_get_code (result
,
738 "test_cast_from_bool_to_long");
739 CHECK_NON_NULL (test_cast_from_bool_to_long
);
740 CHECK_VALUE (test_cast_from_bool_to_long (0), 0);
741 CHECK_VALUE (test_cast_from_bool_to_long (1), 1);
746 typedef bool (*fn_type
) (long);
747 fn_type test_cast_from_long_to_bool
=
748 (fn_type
)gcc_jit_result_get_code (result
,
749 "test_cast_from_long_to_bool");
750 CHECK_NON_NULL (test_cast_from_long_to_bool
);
751 CHECK_VALUE (test_cast_from_long_to_bool (0), 0);
752 CHECK_VALUE (test_cast_from_long_to_bool (1), 1);
757 typedef int (*fn_type
) (void);
758 fn_type test_cast_from_array_of_ints_to_int_ptr
=
759 (fn_type
)gcc_jit_result_get_code (
761 "test_cast_from_array_of_ints_to_int_ptr");
762 CHECK_NON_NULL (test_cast_from_array_of_ints_to_int_ptr
);
763 CHECK_VALUE (test_cast_from_array_of_ints_to_int_ptr (), 40);
767 /**********************************************************************
769 **********************************************************************/
772 make_tests_of_dereferences (gcc_jit_context
*ctxt
)
775 int test_dereference_read (int *ptr)
779 void test_dereference_write (int *ptr, int i)
784 gcc_jit_type
*void_type
=
785 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_VOID
);
786 gcc_jit_type
*int_type
=
787 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
788 gcc_jit_type
*int_ptr_type
=
789 gcc_jit_type_get_pointer (int_type
);
791 gcc_jit_param
*param_ptr
=
792 gcc_jit_context_new_param (ctxt
, NULL
, int_ptr_type
, "ptr");
793 gcc_jit_function
*test_dereference_read
=
794 gcc_jit_context_new_function (ctxt
, NULL
,
795 GCC_JIT_FUNCTION_EXPORTED
,
797 "test_dereference_read",
800 gcc_jit_block
*initial
=
801 gcc_jit_function_new_block (test_dereference_read
, "initial");
802 gcc_jit_block_end_with_return (
805 gcc_jit_lvalue_as_rvalue (
806 gcc_jit_rvalue_dereference (
807 gcc_jit_param_as_rvalue (param_ptr
),
812 gcc_jit_param
*param_ptr
=
813 gcc_jit_context_new_param (ctxt
, NULL
, int_ptr_type
, "ptr");
814 gcc_jit_param
*param_i
=
815 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "i");
816 gcc_jit_param
*params
[] = {param_ptr
, param_i
};
817 gcc_jit_function
*test_dereference_write
=
818 gcc_jit_context_new_function (ctxt
, NULL
,
819 GCC_JIT_FUNCTION_EXPORTED
,
821 "test_dereference_write",
824 gcc_jit_block
*initial
=
825 gcc_jit_function_new_block (test_dereference_write
, "initial");
826 gcc_jit_block_add_assignment (
829 gcc_jit_rvalue_dereference (
830 gcc_jit_param_as_rvalue (param_ptr
),
832 gcc_jit_param_as_rvalue (param_i
));
833 gcc_jit_block_end_with_void_return (initial
, NULL
);
838 verify_dereferences (gcc_jit_result
*result
)
844 typedef int (*test_read
) (int *);
845 test_read test_dereference_read
=
846 (test_read
)gcc_jit_result_get_code (result
,
847 "test_dereference_read");
848 CHECK_NON_NULL (test_dereference_read
);
849 CHECK_VALUE (test_dereference_read (&a
), 42);
850 CHECK_VALUE (test_dereference_read (&b
), -99);
854 typedef void (*test_write
) (int *, int);
855 test_write test_dereference_write
=
856 (test_write
)gcc_jit_result_get_code (result
,
857 "test_dereference_write");
858 CHECK_NON_NULL (test_dereference_write
);
859 test_dereference_write (&a
, -55);
860 CHECK_VALUE (a
, -55);
862 test_dereference_write (&b
, 404);
863 CHECK_VALUE (b
, 404);
867 /**********************************************************************
868 gcc_jit_lvalue_get_address
869 **********************************************************************/
873 make_test_of_get_address (gcc_jit_context
*ctxt
)
876 void *test_get_address (void)
881 gcc_jit_type
*int_type
=
882 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
883 gcc_jit_lvalue
*test_global
=
884 gcc_jit_context_new_global (
887 GCC_JIT_GLOBAL_IMPORTED
,
891 gcc_jit_type
*void_ptr_type
=
892 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_VOID_PTR
);
894 gcc_jit_function
*test_get_address
=
895 gcc_jit_context_new_function (ctxt
, NULL
,
896 GCC_JIT_FUNCTION_EXPORTED
,
901 gcc_jit_block
*initial
=
902 gcc_jit_function_new_block (test_get_address
, "initial");
903 gcc_jit_block_end_with_return (
906 gcc_jit_lvalue_get_address (
912 verify_get_address (gcc_jit_result
*result
)
914 typedef void *(*test_fn
) (void);
915 test_fn test_get_address
=
916 (test_fn
)gcc_jit_result_get_code (result
,
918 CHECK_NON_NULL (test_get_address
);
919 CHECK_VALUE (test_get_address (), &test_global
);
922 /**********************************************************************
924 **********************************************************************/
927 create_code (gcc_jit_context
*ctxt
, void *user_data
)
929 make_tests_of_unary_ops (ctxt
);
930 make_tests_of_binary_ops (ctxt
);
931 make_tests_of_comparisons (ctxt
);
932 make_tests_of_casts (ctxt
);
933 make_tests_of_dereferences (ctxt
);
934 make_test_of_get_address (ctxt
);
938 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
940 CHECK_NON_NULL (result
);
942 verify_unary_ops (result
);
943 verify_binary_ops (result
);
944 verify_comparisons (result
);
945 verify_casts (result
);
946 verify_dereferences (result
);
947 verify_get_address (result
);