| 1 |
#include <stdlib.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <stddef.h> |
| 4 |
#include <stdbool.h> |
| 5 |
|
| 6 |
#include "libgccjit.h" |
| 7 |
|
| 8 |
#include "harness.h" |
| 9 |
|
| 10 |
/********************************************************************** |
| 11 |
Unary ops |
| 12 |
**********************************************************************/ |
| 13 |
|
| 14 |
static const char * |
| 15 |
make_test_of_unary_op (gcc_jit_context *ctxt, |
| 16 |
gcc_jit_type *type, |
| 17 |
enum gcc_jit_unary_op op, |
| 18 |
const char *funcname) |
| 19 |
{ |
| 20 |
/* Make a test function of the form: |
| 21 |
T test_unary_op (T a) |
| 22 |
{ |
| 23 |
return OP a; |
| 24 |
} |
| 25 |
and return a debug dump of the OP so that |
| 26 |
the caller can sanity-check the debug dump implementation. |
| 27 |
*/ |
| 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, |
| 33 |
type, |
| 34 |
funcname, |
| 35 |
1, ¶m_a, |
| 36 |
0); |
| 37 |
gcc_jit_rvalue *unary_op = gcc_jit_context_new_unary_op ( |
| 38 |
ctxt, |
| 39 |
NULL, |
| 40 |
op, |
| 41 |
type, |
| 42 |
gcc_jit_param_as_rvalue (param_a)); |
| 43 |
|
| 44 |
gcc_jit_block *initial = gcc_jit_function_new_block (test_fn, "initial"); |
| 45 |
gcc_jit_block_end_with_return (initial, NULL, unary_op); |
| 46 |
|
| 47 |
return gcc_jit_object_get_debug_string ( |
| 48 |
gcc_jit_rvalue_as_object (unary_op)); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
static void |
| 53 |
make_tests_of_unary_ops (gcc_jit_context *ctxt) |
| 54 |
{ |
| 55 |
gcc_jit_type *int_type = |
| 56 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); |
| 57 |
|
| 58 |
CHECK_STRING_VALUE ( |
| 59 |
make_test_of_unary_op (ctxt, |
| 60 |
int_type, |
| 61 |
GCC_JIT_UNARY_OP_MINUS, |
| 62 |
"test_UNARY_OP_MINUS_on_int"), |
| 63 |
"-(a)"); |
| 64 |
CHECK_STRING_VALUE ( |
| 65 |
make_test_of_unary_op (ctxt, |
| 66 |
int_type, |
| 67 |
GCC_JIT_UNARY_OP_BITWISE_NEGATE, |
| 68 |
"test_UNARY_OP_BITWISE_NEGATE_on_int"), |
| 69 |
"~(a)"); |
| 70 |
CHECK_STRING_VALUE ( |
| 71 |
make_test_of_unary_op (ctxt, |
| 72 |
int_type, |
| 73 |
GCC_JIT_UNARY_OP_LOGICAL_NEGATE, |
| 74 |
"test_UNARY_OP_LOGICAL_NEGATE_on_int"), |
| 75 |
"!(a)"); |
| 76 |
CHECK_STRING_VALUE ( |
| 77 |
make_test_of_unary_op (ctxt, |
| 78 |
int_type, |
| 79 |
GCC_JIT_UNARY_OP_ABS, |
| 80 |
"test_UNARY_OP_ABS_on_int"), |
| 81 |
"abs (a)"); |
| 82 |
} |
| 83 |
|
| 84 |
static void |
| 85 |
verify_unary_ops (gcc_jit_result *result) |
| 86 |
{ |
| 87 |
typedef int (*test_fn) (int); |
| 88 |
|
| 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); |
| 96 |
|
| 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); |
| 104 |
|
| 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); |
| 112 |
|
| 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); |
| 120 |
} |
| 121 |
|
| 122 |
/********************************************************************** |
| 123 |
Binary ops |
| 124 |
**********************************************************************/ |
| 125 |
|
| 126 |
static const char * |
| 127 |
make_test_of_binary_op (gcc_jit_context *ctxt, |
| 128 |
gcc_jit_type *type, |
| 129 |
enum gcc_jit_binary_op op, |
| 130 |
const char *funcname) |
| 131 |
{ |
| 132 |
/* Make a test function of the form: |
| 133 |
T test_binary_op (T a, T b) |
| 134 |
{ |
| 135 |
return a OP b; |
| 136 |
} |
| 137 |
*/ |
| 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, |
| 146 |
type, |
| 147 |
funcname, |
| 148 |
2, params, |
| 149 |
0); |
| 150 |
gcc_jit_rvalue *binary_op = |
| 151 |
gcc_jit_context_new_binary_op ( |
| 152 |
ctxt, |
| 153 |
NULL, |
| 154 |
op, |
| 155 |
type, |
| 156 |
gcc_jit_param_as_rvalue (param_a), |
| 157 |
gcc_jit_param_as_rvalue (param_b)); |
| 158 |
|
| 159 |
gcc_jit_block *initial = gcc_jit_function_new_block (test_fn, "initial"); |
| 160 |
gcc_jit_block_end_with_return (initial, NULL, binary_op); |
| 161 |
|
| 162 |
return gcc_jit_object_get_debug_string ( |
| 163 |
gcc_jit_rvalue_as_object (binary_op)); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
static void |
| 168 |
make_tests_of_binary_ops (gcc_jit_context *ctxt) |
| 169 |
{ |
| 170 |
gcc_jit_type *int_type = |
| 171 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); |
| 172 |
|
| 173 |
/* Test binary ops. */ |
| 174 |
CHECK_STRING_VALUE ( |
| 175 |
make_test_of_binary_op (ctxt, |
| 176 |
int_type, |
| 177 |
GCC_JIT_BINARY_OP_PLUS, |
| 178 |
"test_BINARY_OP_PLUS_on_int"), |
| 179 |
"a + b"); |
| 180 |
CHECK_STRING_VALUE ( |
| 181 |
make_test_of_binary_op (ctxt, |
| 182 |
int_type, |
| 183 |
GCC_JIT_BINARY_OP_MINUS, |
| 184 |
"test_BINARY_OP_MINUS_on_int"), |
| 185 |
"a - b"); |
| 186 |
CHECK_STRING_VALUE ( |
| 187 |
make_test_of_binary_op (ctxt, |
| 188 |
int_type, |
| 189 |
GCC_JIT_BINARY_OP_MULT, |
| 190 |
"test_BINARY_OP_MULT_on_int"), |
| 191 |
"a * b"); |
| 192 |
CHECK_STRING_VALUE ( |
| 193 |
make_test_of_binary_op (ctxt, |
| 194 |
int_type, |
| 195 |
GCC_JIT_BINARY_OP_DIVIDE, |
| 196 |
"test_BINARY_OP_DIVIDE_on_int"), |
| 197 |
"a / b"); |
| 198 |
/* TODO: test for DIVIDE on float or double */ |
| 199 |
CHECK_STRING_VALUE ( |
| 200 |
make_test_of_binary_op (ctxt, |
| 201 |
int_type, |
| 202 |
GCC_JIT_BINARY_OP_MODULO, |
| 203 |
"test_BINARY_OP_MODULO_on_int"), |
| 204 |
"a % b"); |
| 205 |
CHECK_STRING_VALUE ( |
| 206 |
make_test_of_binary_op (ctxt, |
| 207 |
int_type, |
| 208 |
GCC_JIT_BINARY_OP_BITWISE_AND, |
| 209 |
"test_BINARY_OP_BITWISE_AND_on_int"), |
| 210 |
"a & b"); |
| 211 |
CHECK_STRING_VALUE ( |
| 212 |
make_test_of_binary_op (ctxt, |
| 213 |
int_type, |
| 214 |
GCC_JIT_BINARY_OP_BITWISE_XOR, |
| 215 |
"test_BINARY_OP_BITWISE_XOR_on_int"), |
| 216 |
"a ^ b"); |
| 217 |
CHECK_STRING_VALUE ( |
| 218 |
make_test_of_binary_op (ctxt, |
| 219 |
int_type, |
| 220 |
GCC_JIT_BINARY_OP_BITWISE_OR, |
| 221 |
"test_BINARY_OP_BITWISE_OR_on_int"), |
| 222 |
"a | b"); |
| 223 |
CHECK_STRING_VALUE ( |
| 224 |
make_test_of_binary_op (ctxt, |
| 225 |
int_type, |
| 226 |
GCC_JIT_BINARY_OP_LOGICAL_AND, |
| 227 |
"test_BINARY_OP_LOGICAL_AND_on_int"), |
| 228 |
"a && b"); |
| 229 |
CHECK_STRING_VALUE ( |
| 230 |
make_test_of_binary_op (ctxt, |
| 231 |
int_type, |
| 232 |
GCC_JIT_BINARY_OP_LOGICAL_OR, |
| 233 |
"test_BINARY_OP_LOGICAL_OR_on_int"), |
| 234 |
"a || b"); |
| 235 |
CHECK_STRING_VALUE ( |
| 236 |
make_test_of_binary_op (ctxt, |
| 237 |
int_type, |
| 238 |
GCC_JIT_BINARY_OP_LSHIFT, |
| 239 |
"test_BINARY_OP_LSHIFT_on_int"), |
| 240 |
"a << b"); |
| 241 |
CHECK_STRING_VALUE ( |
| 242 |
make_test_of_binary_op (ctxt, |
| 243 |
int_type, |
| 244 |
GCC_JIT_BINARY_OP_RSHIFT, |
| 245 |
"test_BINARY_OP_RSHIFT_on_int"), |
| 246 |
"a >> b"); |
| 247 |
} |
| 248 |
|
| 249 |
static void |
| 250 |
verify_binary_ops (gcc_jit_result *result) |
| 251 |
{ |
| 252 |
typedef int (*test_fn) (int, int); |
| 253 |
|
| 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); |
| 262 |
|
| 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); |
| 271 |
|
| 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); |
| 281 |
|
| 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); |
| 290 |
|
| 291 |
/* TODO: test for DIVIDE on float or double */ |
| 292 |
|
| 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); |
| 301 |
|
| 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); |
| 307 |
|
| 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); |
| 313 |
|
| 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); |
| 319 |
|
| 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); |
| 328 |
|
| 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); |
| 337 |
|
| 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); |
| 354 |
|
| 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); |
| 363 |
} |
| 364 |
|
| 365 |
/********************************************************************** |
| 366 |
Comparisons |
| 367 |
**********************************************************************/ |
| 368 |
|
| 369 |
static const char * |
| 370 |
make_test_of_comparison (gcc_jit_context *ctxt, |
| 371 |
gcc_jit_type *type, |
| 372 |
enum gcc_jit_comparison op, |
| 373 |
const char *funcname) |
| 374 |
{ |
| 375 |
/* Make a test function of the form: |
| 376 |
bool test_comparison_op (T a, T b) |
| 377 |
{ |
| 378 |
return a OP b; |
| 379 |
} |
| 380 |
*/ |
| 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 |
|
| 387 |
gcc_jit_rvalue *comparison = |
| 388 |
gcc_jit_context_new_comparison ( |
| 389 |
ctxt, |
| 390 |
NULL, |
| 391 |
op, |
| 392 |
gcc_jit_param_as_rvalue (param_a), |
| 393 |
gcc_jit_param_as_rvalue (param_b)); |
| 394 |
|
| 395 |
gcc_jit_type *comparison_type = gcc_jit_rvalue_get_type(comparison); |
| 396 |
|
| 397 |
gcc_jit_function *test_fn = |
| 398 |
gcc_jit_context_new_function (ctxt, NULL, |
| 399 |
GCC_JIT_FUNCTION_EXPORTED, |
| 400 |
comparison_type, |
| 401 |
funcname, |
| 402 |
2, params, |
| 403 |
0); |
| 404 |
|
| 405 |
gcc_jit_block *initial = gcc_jit_function_new_block (test_fn, "initial"); |
| 406 |
gcc_jit_block_end_with_return (initial, NULL, comparison); |
| 407 |
|
| 408 |
return gcc_jit_object_get_debug_string ( |
| 409 |
gcc_jit_rvalue_as_object (comparison)); |
| 410 |
} |
| 411 |
|
| 412 |
static void run_test_of_comparison(gcc_jit_context *ctxt, |
| 413 |
gcc_jit_type *type, |
| 414 |
enum gcc_jit_comparison op, |
| 415 |
const char *funcname, |
| 416 |
const char *vec_funcname, |
| 417 |
const char *expected) |
| 418 |
{ |
| 419 |
gcc_jit_type *vec_type = |
| 420 |
gcc_jit_type_get_vector (type, 2); |
| 421 |
|
| 422 |
CHECK_STRING_VALUE ( |
| 423 |
make_test_of_comparison (ctxt, |
| 424 |
type, |
| 425 |
op, |
| 426 |
funcname), |
| 427 |
expected); |
| 428 |
CHECK_STRING_VALUE ( |
| 429 |
make_test_of_comparison (ctxt, |
| 430 |
vec_type, |
| 431 |
op, |
| 432 |
vec_funcname), |
| 433 |
expected); |
| 434 |
} |
| 435 |
|
| 436 |
static void |
| 437 |
make_tests_of_comparisons (gcc_jit_context *ctxt) |
| 438 |
{ |
| 439 |
gcc_jit_type *int_type = |
| 440 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); |
| 441 |
gcc_jit_type *float_type = |
| 442 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT); |
| 443 |
|
| 444 |
run_test_of_comparison( |
| 445 |
ctxt, |
| 446 |
int_type, |
| 447 |
GCC_JIT_COMPARISON_EQ, |
| 448 |
"test_COMPARISON_EQ_on_int", |
| 449 |
"test_COMPARISON_EQ_on_vec_int", |
| 450 |
"a == b"); |
| 451 |
run_test_of_comparison( |
| 452 |
ctxt, |
| 453 |
int_type, |
| 454 |
GCC_JIT_COMPARISON_NE, |
| 455 |
"test_COMPARISON_NE_on_int", |
| 456 |
"test_COMPARISON_NE_on_vec_int", |
| 457 |
"a != b"); |
| 458 |
run_test_of_comparison( |
| 459 |
ctxt, |
| 460 |
int_type, |
| 461 |
GCC_JIT_COMPARISON_LT, |
| 462 |
"test_COMPARISON_LT_on_int", |
| 463 |
"test_COMPARISON_LT_on_vec_int", |
| 464 |
"a < b"); |
| 465 |
run_test_of_comparison( |
| 466 |
ctxt, |
| 467 |
int_type, |
| 468 |
GCC_JIT_COMPARISON_LE, |
| 469 |
"test_COMPARISON_LE_on_int", |
| 470 |
"test_COMPARISON_LE_on_vec_int", |
| 471 |
"a <= b"); |
| 472 |
run_test_of_comparison( |
| 473 |
ctxt, |
| 474 |
int_type, |
| 475 |
GCC_JIT_COMPARISON_GT, |
| 476 |
"test_COMPARISON_GT_on_int", |
| 477 |
"test_COMPARISON_GT_on_vec_int", |
| 478 |
"a > b"); |
| 479 |
run_test_of_comparison( |
| 480 |
ctxt, |
| 481 |
int_type, |
| 482 |
GCC_JIT_COMPARISON_GE, |
| 483 |
"test_COMPARISON_GE_on_int", |
| 484 |
"test_COMPARISON_GE_on_vec_int", |
| 485 |
"a >= b"); |
| 486 |
|
| 487 |
// Float tests |
| 488 |
run_test_of_comparison( |
| 489 |
ctxt, |
| 490 |
float_type, |
| 491 |
GCC_JIT_COMPARISON_NE, |
| 492 |
"test_COMPARISON_NE_on_float", |
| 493 |
"test_COMPARISON_NE_on_vec_float", |
| 494 |
"a != b"); |
| 495 |
run_test_of_comparison( |
| 496 |
ctxt, |
| 497 |
float_type, |
| 498 |
GCC_JIT_COMPARISON_LT, |
| 499 |
"test_COMPARISON_LT_on_float", |
| 500 |
"test_COMPARISON_LT_on_vec_float", |
| 501 |
"a < b"); |
| 502 |
run_test_of_comparison( |
| 503 |
ctxt, |
| 504 |
float_type, |
| 505 |
GCC_JIT_COMPARISON_GT, |
| 506 |
"test_COMPARISON_GT_on_float", |
| 507 |
"test_COMPARISON_GT_on_vec_float", |
| 508 |
"a > b"); |
| 509 |
} |
| 510 |
|
| 511 |
static void |
| 512 |
verify_comparisons (gcc_jit_result *result) |
| 513 |
{ |
| 514 |
typedef bool (*test_fn) (int, int); |
| 515 |
|
| 516 |
test_fn test_COMPARISON_EQ_on_int = |
| 517 |
(test_fn)gcc_jit_result_get_code (result, |
| 518 |
"test_COMPARISON_EQ_on_int"); |
| 519 |
CHECK_NON_NULL (test_COMPARISON_EQ_on_int); |
| 520 |
CHECK_VALUE (test_COMPARISON_EQ_on_int (0, 0), 1); |
| 521 |
CHECK_VALUE (test_COMPARISON_EQ_on_int (1, 2), 0); |
| 522 |
|
| 523 |
test_fn test_COMPARISON_NE_on_int = |
| 524 |
(test_fn)gcc_jit_result_get_code (result, |
| 525 |
"test_COMPARISON_NE_on_int"); |
| 526 |
CHECK_NON_NULL (test_COMPARISON_NE_on_int); |
| 527 |
CHECK_VALUE (test_COMPARISON_NE_on_int (0, 0), 0); |
| 528 |
CHECK_VALUE (test_COMPARISON_NE_on_int (1, 2), 1); |
| 529 |
|
| 530 |
test_fn test_COMPARISON_LT_on_int = |
| 531 |
(test_fn)gcc_jit_result_get_code (result, |
| 532 |
"test_COMPARISON_LT_on_int"); |
| 533 |
CHECK_NON_NULL (test_COMPARISON_LT_on_int); |
| 534 |
CHECK_VALUE (test_COMPARISON_LT_on_int (0, 0), 0); |
| 535 |
CHECK_VALUE (test_COMPARISON_LT_on_int (1, 2), 1); |
| 536 |
CHECK_VALUE (test_COMPARISON_LT_on_int (2, 1), 0); |
| 537 |
CHECK_VALUE (test_COMPARISON_LT_on_int (-2, 1), 1); |
| 538 |
|
| 539 |
test_fn test_COMPARISON_LE_on_int = |
| 540 |
(test_fn)gcc_jit_result_get_code (result, |
| 541 |
"test_COMPARISON_LE_on_int"); |
| 542 |
CHECK_NON_NULL (test_COMPARISON_LE_on_int); |
| 543 |
CHECK_VALUE (test_COMPARISON_LE_on_int (0, 0), 1); |
| 544 |
CHECK_VALUE (test_COMPARISON_LE_on_int (1, 2), 1); |
| 545 |
CHECK_VALUE (test_COMPARISON_LE_on_int (2, 1), 0); |
| 546 |
|
| 547 |
test_fn test_COMPARISON_GT_on_int = |
| 548 |
(test_fn)gcc_jit_result_get_code (result, |
| 549 |
"test_COMPARISON_GT_on_int"); |
| 550 |
CHECK_NON_NULL (test_COMPARISON_GT_on_int); |
| 551 |
CHECK_VALUE (test_COMPARISON_GT_on_int (0, 0), 0); |
| 552 |
CHECK_VALUE (test_COMPARISON_GT_on_int (1, 2), 0); |
| 553 |
CHECK_VALUE (test_COMPARISON_GT_on_int (2, 1), 1); |
| 554 |
|
| 555 |
test_fn test_COMPARISON_GE_on_int = |
| 556 |
(test_fn)gcc_jit_result_get_code (result, |
| 557 |
"test_COMPARISON_GE_on_int"); |
| 558 |
CHECK_NON_NULL (test_COMPARISON_GE_on_int); |
| 559 |
CHECK_VALUE (test_COMPARISON_GE_on_int (0, 0), 1); |
| 560 |
CHECK_VALUE (test_COMPARISON_GE_on_int (1, 2), 0); |
| 561 |
CHECK_VALUE (test_COMPARISON_GE_on_int (2, 1), 1); |
| 562 |
|
| 563 |
typedef int v2si __attribute__ ((__vector_size__ (sizeof(int) * 2))); |
| 564 |
typedef v2si (*test_vec_fn) (v2si, v2si); |
| 565 |
|
| 566 |
v2si zero_zero = {0, 0}; |
| 567 |
v2si zero_one = {0, 1}; |
| 568 |
v2si one_zero = {1, 0}; |
| 569 |
|
| 570 |
v2si true_true = {-1, -1}; |
| 571 |
v2si false_true = {0, -1}; |
| 572 |
v2si true_false = {-1, 0}; |
| 573 |
v2si false_false = {0, 0}; |
| 574 |
|
| 575 |
test_vec_fn test_COMPARISON_EQ_on_vec_int = |
| 576 |
(test_vec_fn)gcc_jit_result_get_code (result, |
| 577 |
"test_COMPARISON_EQ_on_vec_int"); |
| 578 |
CHECK_NON_NULL (test_COMPARISON_EQ_on_vec_int); |
| 579 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_EQ_on_vec_int (zero_zero, zero_zero), true_true); |
| 580 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_EQ_on_vec_int (zero_one, one_zero), false_false); |
| 581 |
|
| 582 |
test_vec_fn test_COMPARISON_NE_on_vec_int = |
| 583 |
(test_vec_fn)gcc_jit_result_get_code (result, |
| 584 |
"test_COMPARISON_NE_on_vec_int"); |
| 585 |
CHECK_NON_NULL (test_COMPARISON_NE_on_vec_int); |
| 586 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_NE_on_vec_int (zero_zero, zero_zero), false_false); |
| 587 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_NE_on_vec_int (zero_one, one_zero), true_true); |
| 588 |
|
| 589 |
test_vec_fn test_COMPARISON_LT_on_vec_int = |
| 590 |
(test_vec_fn)gcc_jit_result_get_code (result, |
| 591 |
"test_COMPARISON_LT_on_vec_int"); |
| 592 |
CHECK_NON_NULL (test_COMPARISON_LT_on_vec_int); |
| 593 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_LT_on_vec_int (zero_zero, zero_zero), false_false); |
| 594 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_LT_on_vec_int (zero_one, one_zero), true_false); |
| 595 |
|
| 596 |
test_vec_fn test_COMPARISON_LE_on_vec_int = |
| 597 |
(test_vec_fn)gcc_jit_result_get_code (result, |
| 598 |
"test_COMPARISON_LE_on_vec_int"); |
| 599 |
CHECK_NON_NULL (test_COMPARISON_LE_on_vec_int); |
| 600 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_LE_on_vec_int (zero_zero, zero_zero), true_true); |
| 601 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_LE_on_vec_int (zero_one, one_zero), true_false); |
| 602 |
|
| 603 |
test_vec_fn test_COMPARISON_GT_on_vec_int = |
| 604 |
(test_vec_fn)gcc_jit_result_get_code (result, |
| 605 |
"test_COMPARISON_GT_on_vec_int"); |
| 606 |
CHECK_NON_NULL (test_COMPARISON_GT_on_vec_int); |
| 607 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_GT_on_vec_int (zero_zero, zero_zero), false_false); |
| 608 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_GT_on_vec_int (zero_one, one_zero), false_true); |
| 609 |
|
| 610 |
test_vec_fn test_COMPARISON_GE_on_vec_int = |
| 611 |
(test_vec_fn)gcc_jit_result_get_code (result, |
| 612 |
"test_COMPARISON_GE_on_vec_int"); |
| 613 |
CHECK_NON_NULL (test_COMPARISON_GE_on_vec_int); |
| 614 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_GE_on_vec_int (zero_zero, zero_zero), true_true); |
| 615 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_GE_on_vec_int (zero_one, one_zero), false_true); |
| 616 |
|
| 617 |
typedef float __vector_f __attribute__ ((__vector_size__ (sizeof(float) * 2))); |
| 618 |
typedef v2si (*test_vec_f_fn) (__vector_f, __vector_f); |
| 619 |
|
| 620 |
__vector_f zero_zero_f = {0, 0}; |
| 621 |
__vector_f zero_one_f = {0, 1}; |
| 622 |
__vector_f one_zero_f = {1, 0}; |
| 623 |
|
| 624 |
__vector_f true_true_f = {-1, -1}; |
| 625 |
__vector_f false_true_f = {0, -1}; |
| 626 |
__vector_f true_false_f = {-1, 0}; |
| 627 |
__vector_f false_false_f = {0, 0}; |
| 628 |
|
| 629 |
test_vec_f_fn test_COMPARISON_NE_on_vec_float = |
| 630 |
(test_vec_f_fn)gcc_jit_result_get_code (result, |
| 631 |
"test_COMPARISON_NE_on_vec_float"); |
| 632 |
CHECK_NON_NULL (test_COMPARISON_NE_on_vec_float); |
| 633 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_NE_on_vec_float (zero_zero_f, zero_zero_f), false_false_f); |
| 634 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_NE_on_vec_float (zero_one_f, one_zero_f), true_true_f); |
| 635 |
|
| 636 |
test_vec_f_fn test_COMPARISON_LT_on_vec_float = |
| 637 |
(test_vec_f_fn)gcc_jit_result_get_code (result, |
| 638 |
"test_COMPARISON_LT_on_vec_float"); |
| 639 |
CHECK_NON_NULL (test_COMPARISON_LT_on_vec_float); |
| 640 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_LT_on_vec_float (zero_zero_f, zero_zero_f), false_false_f); |
| 641 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_LT_on_vec_float (zero_one_f, one_zero_f), true_false_f); |
| 642 |
|
| 643 |
test_vec_f_fn test_COMPARISON_GT_on_vec_float = |
| 644 |
(test_vec_f_fn)gcc_jit_result_get_code (result, |
| 645 |
"test_COMPARISON_GT_on_vec_float"); |
| 646 |
CHECK_NON_NULL (test_COMPARISON_GT_on_vec_float); |
| 647 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_GT_on_vec_float (zero_zero_f, zero_zero_f), false_false_f); |
| 648 |
CHECK_VECTOR_VALUE (2, test_COMPARISON_GT_on_vec_float (zero_one_f, one_zero_f), false_true_f); |
| 649 |
} |
| 650 |
|
| 651 |
/********************************************************************** |
| 652 |
Casts |
| 653 |
**********************************************************************/ |
| 654 |
|
| 655 |
static const char* |
| 656 |
make_test_of_cast (gcc_jit_context *ctxt, |
| 657 |
gcc_jit_type *input_type, |
| 658 |
gcc_jit_type *output_type, |
| 659 |
const char *funcname) |
| 660 |
{ |
| 661 |
/* Make a test function of the form: |
| 662 |
OUTPUT_TYPE test_cast_* (INPUT_TYPE a) |
| 663 |
{ |
| 664 |
return (OUTPUT_TYPE)a; |
| 665 |
} |
| 666 |
*/ |
| 667 |
gcc_jit_param *param_a = |
| 668 |
gcc_jit_context_new_param (ctxt, NULL, input_type, "a"); |
| 669 |
gcc_jit_param *params[] = {param_a}; |
| 670 |
gcc_jit_function *test_fn = |
| 671 |
gcc_jit_context_new_function (ctxt, NULL, |
| 672 |
GCC_JIT_FUNCTION_EXPORTED, |
| 673 |
output_type, |
| 674 |
funcname, |
| 675 |
1, params, |
| 676 |
0); |
| 677 |
gcc_jit_rvalue *cast = |
| 678 |
gcc_jit_context_new_cast ( |
| 679 |
ctxt, |
| 680 |
NULL, |
| 681 |
gcc_jit_param_as_rvalue (param_a), |
| 682 |
output_type); |
| 683 |
gcc_jit_block *initial = gcc_jit_function_new_block (test_fn, "initial"); |
| 684 |
gcc_jit_block_end_with_return (initial, NULL, cast); |
| 685 |
|
| 686 |
return gcc_jit_object_get_debug_string ( |
| 687 |
gcc_jit_rvalue_as_object (cast)); |
| 688 |
} |
| 689 |
|
| 690 |
/* For use by test_cast_from_array_of_ints_to_int_ptr. */ |
| 691 |
extern int called_pointer_checking_function (int *ints) |
| 692 |
{ |
| 693 |
CHECK_VALUE (ints[0], 10); |
| 694 |
CHECK_VALUE (ints[1], 4); |
| 695 |
return ints[0] * ints[1]; |
| 696 |
} |
| 697 |
|
| 698 |
static void |
| 699 |
make_tests_of_casts (gcc_jit_context *ctxt) |
| 700 |
{ |
| 701 |
gcc_jit_type *int_type = |
| 702 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); |
| 703 |
gcc_jit_type *long_type = |
| 704 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG); |
| 705 |
gcc_jit_type *float_type = |
| 706 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT); |
| 707 |
gcc_jit_type *bool_type = |
| 708 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BOOL); |
| 709 |
gcc_jit_type *array_int_type = |
| 710 |
gcc_jit_context_new_array_type (ctxt, NULL, |
| 711 |
int_type, |
| 712 |
2); |
| 713 |
gcc_jit_type *int_ptr_type = |
| 714 |
gcc_jit_type_get_pointer (int_type); |
| 715 |
|
| 716 |
/* float/int conversions */ |
| 717 |
CHECK_STRING_VALUE ( |
| 718 |
make_test_of_cast (ctxt, |
| 719 |
float_type, |
| 720 |
int_type, |
| 721 |
"test_cast_from_float_to_int"), |
| 722 |
"(int)a"); |
| 723 |
CHECK_STRING_VALUE ( |
| 724 |
make_test_of_cast (ctxt, |
| 725 |
int_type, |
| 726 |
float_type, |
| 727 |
"test_cast_from_int_to_float"), |
| 728 |
"(float)a"); |
| 729 |
|
| 730 |
/* bool/int conversions */ |
| 731 |
CHECK_STRING_VALUE ( |
| 732 |
make_test_of_cast (ctxt, |
| 733 |
bool_type, |
| 734 |
int_type, |
| 735 |
"test_cast_from_bool_to_int"), |
| 736 |
"(int)a"); |
| 737 |
CHECK_STRING_VALUE ( |
| 738 |
make_test_of_cast (ctxt, |
| 739 |
int_type, |
| 740 |
bool_type, |
| 741 |
"test_cast_from_int_to_bool"), |
| 742 |
"(bool)a"); |
| 743 |
|
| 744 |
/* bool/long conversions */ |
| 745 |
CHECK_STRING_VALUE ( |
| 746 |
make_test_of_cast (ctxt, |
| 747 |
bool_type, |
| 748 |
long_type, |
| 749 |
"test_cast_from_bool_to_long"), |
| 750 |
"(long)a"); |
| 751 |
CHECK_STRING_VALUE ( |
| 752 |
make_test_of_cast (ctxt, |
| 753 |
long_type, |
| 754 |
bool_type, |
| 755 |
"test_cast_from_long_to_bool"), |
| 756 |
"(bool)a"); |
| 757 |
|
| 758 |
/* array/ptr conversions */ |
| 759 |
{ |
| 760 |
gcc_jit_function *test_fn = |
| 761 |
gcc_jit_context_new_function ( |
| 762 |
ctxt, NULL, |
| 763 |
GCC_JIT_FUNCTION_EXPORTED, |
| 764 |
int_type, |
| 765 |
"test_cast_from_array_of_ints_to_int_ptr", |
| 766 |
0, NULL, |
| 767 |
0); |
| 768 |
/* Equivalent to: |
| 769 |
int test_cast_from_array_of_ints_to_int_ptr (void) |
| 770 |
{ |
| 771 |
int array[2]; |
| 772 |
array[0] = 10; |
| 773 |
array[1] = 4; |
| 774 |
return called_pointer_checking_function (array); |
| 775 |
} |
| 776 |
*/ |
| 777 |
|
| 778 |
gcc_jit_param *param_ints = |
| 779 |
gcc_jit_context_new_param (ctxt, NULL, int_ptr_type, "ints"); |
| 780 |
gcc_jit_function *called_fn = |
| 781 |
gcc_jit_context_new_function ( |
| 782 |
ctxt, NULL, |
| 783 |
GCC_JIT_FUNCTION_IMPORTED, |
| 784 |
int_type, |
| 785 |
"called_pointer_checking_function", |
| 786 |
1, ¶m_ints, |
| 787 |
0); |
| 788 |
|
| 789 |
gcc_jit_lvalue *array = |
| 790 |
gcc_jit_function_new_local (test_fn, NULL, |
| 791 |
array_int_type, |
| 792 |
"array"); |
| 793 |
gcc_jit_block *block = |
| 794 |
gcc_jit_function_new_block (test_fn, "block"); |
| 795 |
/* array[0] = 10; */ |
| 796 |
gcc_jit_block_add_assignment ( |
| 797 |
block, NULL, |
| 798 |
gcc_jit_context_new_array_access ( |
| 799 |
ctxt, NULL, |
| 800 |
gcc_jit_lvalue_as_rvalue (array), |
| 801 |
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 0)), |
| 802 |
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 10)); |
| 803 |
/* array[1] = 4; */ |
| 804 |
gcc_jit_block_add_assignment ( |
| 805 |
block, NULL, |
| 806 |
gcc_jit_context_new_array_access ( |
| 807 |
ctxt, NULL, |
| 808 |
gcc_jit_lvalue_as_rvalue (array), |
| 809 |
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 1)), |
| 810 |
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 4)); |
| 811 |
gcc_jit_rvalue *cast = |
| 812 |
gcc_jit_context_new_cast ( |
| 813 |
ctxt, |
| 814 |
NULL, |
| 815 |
/* We need a get_address here. */ |
| 816 |
gcc_jit_lvalue_get_address (array, NULL), |
| 817 |
int_ptr_type); |
| 818 |
gcc_jit_block_end_with_return ( |
| 819 |
block, NULL, |
| 820 |
gcc_jit_context_new_call ( |
| 821 |
ctxt, NULL, |
| 822 |
called_fn, |
| 823 |
1, &cast)); |
| 824 |
|
| 825 |
CHECK_STRING_VALUE ( |
| 826 |
gcc_jit_object_get_debug_string ( |
| 827 |
gcc_jit_rvalue_as_object (cast)), |
| 828 |
"(int *)&array"); |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
static void |
| 833 |
verify_casts (gcc_jit_result *result) |
| 834 |
{ |
| 835 |
/* float to int */ |
| 836 |
{ |
| 837 |
typedef int (*fn_type) (float); |
| 838 |
fn_type test_cast_from_float_to_int = |
| 839 |
(fn_type)gcc_jit_result_get_code (result, |
| 840 |
"test_cast_from_float_to_int"); |
| 841 |
CHECK_NON_NULL (test_cast_from_float_to_int); |
| 842 |
CHECK_VALUE (test_cast_from_float_to_int (4.2), 4); |
| 843 |
} |
| 844 |
|
| 845 |
/* int to float */ |
| 846 |
{ |
| 847 |
typedef float (*fn_type) (int); |
| 848 |
fn_type test_cast_from_int_to_float = |
| 849 |
(fn_type)gcc_jit_result_get_code (result, |
| 850 |
"test_cast_from_int_to_float"); |
| 851 |
CHECK_NON_NULL (test_cast_from_int_to_float); |
| 852 |
CHECK_VALUE (test_cast_from_int_to_float (4), 4.0); |
| 853 |
} |
| 854 |
|
| 855 |
/* bool to int */ |
| 856 |
{ |
| 857 |
typedef int (*fn_type) (bool); |
| 858 |
fn_type test_cast_from_bool_to_int = |
| 859 |
(fn_type)gcc_jit_result_get_code (result, |
| 860 |
"test_cast_from_bool_to_int"); |
| 861 |
CHECK_NON_NULL (test_cast_from_bool_to_int); |
| 862 |
CHECK_VALUE (test_cast_from_bool_to_int (0), 0); |
| 863 |
CHECK_VALUE (test_cast_from_bool_to_int (1), 1); |
| 864 |
} |
| 865 |
|
| 866 |
/* int to bool */ |
| 867 |
{ |
| 868 |
typedef bool (*fn_type) (int); |
| 869 |
fn_type test_cast_from_int_to_bool = |
| 870 |
(fn_type)gcc_jit_result_get_code (result, |
| 871 |
"test_cast_from_int_to_bool"); |
| 872 |
CHECK_NON_NULL (test_cast_from_int_to_bool); |
| 873 |
CHECK_VALUE (test_cast_from_int_to_bool (0), 0); |
| 874 |
CHECK_VALUE (test_cast_from_int_to_bool (1), 1); |
| 875 |
} |
| 876 |
|
| 877 |
/* bool to long */ |
| 878 |
{ |
| 879 |
typedef long (*fn_type) (bool); |
| 880 |
fn_type test_cast_from_bool_to_long = |
| 881 |
(fn_type)gcc_jit_result_get_code (result, |
| 882 |
"test_cast_from_bool_to_long"); |
| 883 |
CHECK_NON_NULL (test_cast_from_bool_to_long); |
| 884 |
CHECK_VALUE (test_cast_from_bool_to_long (0), 0); |
| 885 |
CHECK_VALUE (test_cast_from_bool_to_long (1), 1); |
| 886 |
} |
| 887 |
|
| 888 |
/* long to bool */ |
| 889 |
{ |
| 890 |
typedef bool (*fn_type) (long); |
| 891 |
fn_type test_cast_from_long_to_bool = |
| 892 |
(fn_type)gcc_jit_result_get_code (result, |
| 893 |
"test_cast_from_long_to_bool"); |
| 894 |
CHECK_NON_NULL (test_cast_from_long_to_bool); |
| 895 |
CHECK_VALUE (test_cast_from_long_to_bool (0), 0); |
| 896 |
CHECK_VALUE (test_cast_from_long_to_bool (1), 1); |
| 897 |
} |
| 898 |
|
| 899 |
/* array to ptr */ |
| 900 |
{ |
| 901 |
typedef int (*fn_type) (void); |
| 902 |
fn_type test_cast_from_array_of_ints_to_int_ptr = |
| 903 |
(fn_type)gcc_jit_result_get_code ( |
| 904 |
result, |
| 905 |
"test_cast_from_array_of_ints_to_int_ptr"); |
| 906 |
CHECK_NON_NULL (test_cast_from_array_of_ints_to_int_ptr); |
| 907 |
CHECK_VALUE (test_cast_from_array_of_ints_to_int_ptr (), 40); |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
/********************************************************************** |
| 912 |
Dereferences |
| 913 |
**********************************************************************/ |
| 914 |
|
| 915 |
static void |
| 916 |
make_tests_of_dereferences (gcc_jit_context *ctxt) |
| 917 |
{ |
| 918 |
/* |
| 919 |
int test_dereference_read (int *ptr) |
| 920 |
{ |
| 921 |
return *ptr; |
| 922 |
} |
| 923 |
void test_dereference_write (int *ptr, int i) |
| 924 |
{ |
| 925 |
*ptr = i; |
| 926 |
} |
| 927 |
*/ |
| 928 |
gcc_jit_type *void_type = |
| 929 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID); |
| 930 |
gcc_jit_type *int_type = |
| 931 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); |
| 932 |
gcc_jit_type *int_ptr_type = |
| 933 |
gcc_jit_type_get_pointer (int_type); |
| 934 |
{ |
| 935 |
gcc_jit_param *param_ptr = |
| 936 |
gcc_jit_context_new_param (ctxt, NULL, int_ptr_type, "ptr"); |
| 937 |
gcc_jit_function *test_dereference_read = |
| 938 |
gcc_jit_context_new_function (ctxt, NULL, |
| 939 |
GCC_JIT_FUNCTION_EXPORTED, |
| 940 |
int_type, |
| 941 |
"test_dereference_read", |
| 942 |
1, ¶m_ptr, |
| 943 |
0); |
| 944 |
gcc_jit_block *initial = |
| 945 |
gcc_jit_function_new_block (test_dereference_read, "initial"); |
| 946 |
gcc_jit_block_end_with_return ( |
| 947 |
initial, |
| 948 |
NULL, |
| 949 |
gcc_jit_lvalue_as_rvalue ( |
| 950 |
gcc_jit_rvalue_dereference ( |
| 951 |
gcc_jit_param_as_rvalue (param_ptr), |
| 952 |
NULL))); |
| 953 |
} |
| 954 |
|
| 955 |
{ |
| 956 |
gcc_jit_param *param_ptr = |
| 957 |
gcc_jit_context_new_param (ctxt, NULL, int_ptr_type, "ptr"); |
| 958 |
gcc_jit_param *param_i = |
| 959 |
gcc_jit_context_new_param (ctxt, NULL, int_type, "i"); |
| 960 |
gcc_jit_param *params[] = {param_ptr, param_i}; |
| 961 |
gcc_jit_function *test_dereference_write = |
| 962 |
gcc_jit_context_new_function (ctxt, NULL, |
| 963 |
GCC_JIT_FUNCTION_EXPORTED, |
| 964 |
void_type, |
| 965 |
"test_dereference_write", |
| 966 |
2, params, |
| 967 |
0); |
| 968 |
gcc_jit_block *initial = |
| 969 |
gcc_jit_function_new_block (test_dereference_write, "initial"); |
| 970 |
gcc_jit_block_add_assignment ( |
| 971 |
initial, |
| 972 |
NULL, |
| 973 |
gcc_jit_rvalue_dereference ( |
| 974 |
gcc_jit_param_as_rvalue (param_ptr), |
| 975 |
NULL), |
| 976 |
gcc_jit_param_as_rvalue (param_i)); |
| 977 |
gcc_jit_block_end_with_void_return (initial, NULL); |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
static void |
| 982 |
verify_dereferences (gcc_jit_result *result) |
| 983 |
{ |
| 984 |
int a = 42; |
| 985 |
int b = -99; |
| 986 |
|
| 987 |
{ |
| 988 |
typedef int (*test_read) (int *); |
| 989 |
test_read test_dereference_read = |
| 990 |
(test_read)gcc_jit_result_get_code (result, |
| 991 |
"test_dereference_read"); |
| 992 |
CHECK_NON_NULL (test_dereference_read); |
| 993 |
CHECK_VALUE (test_dereference_read (&a), 42); |
| 994 |
CHECK_VALUE (test_dereference_read (&b), -99); |
| 995 |
} |
| 996 |
|
| 997 |
{ |
| 998 |
typedef void (*test_write) (int *, int); |
| 999 |
test_write test_dereference_write = |
| 1000 |
(test_write)gcc_jit_result_get_code (result, |
| 1001 |
"test_dereference_write"); |
| 1002 |
CHECK_NON_NULL (test_dereference_write); |
| 1003 |
test_dereference_write (&a, -55); |
| 1004 |
CHECK_VALUE (a, -55); |
| 1005 |
|
| 1006 |
test_dereference_write (&b, 404); |
| 1007 |
CHECK_VALUE (b, 404); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
/********************************************************************** |
| 1012 |
gcc_jit_lvalue_get_address |
| 1013 |
**********************************************************************/ |
| 1014 |
|
| 1015 |
int test_global; |
| 1016 |
static void |
| 1017 |
make_test_of_get_address (gcc_jit_context *ctxt) |
| 1018 |
{ |
| 1019 |
/* |
| 1020 |
void *test_get_address (void) |
| 1021 |
{ |
| 1022 |
return &test_global; |
| 1023 |
} |
| 1024 |
*/ |
| 1025 |
gcc_jit_type *int_type = |
| 1026 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); |
| 1027 |
gcc_jit_lvalue *test_global = |
| 1028 |
gcc_jit_context_new_global ( |
| 1029 |
ctxt, |
| 1030 |
NULL, |
| 1031 |
GCC_JIT_GLOBAL_IMPORTED, |
| 1032 |
int_type, |
| 1033 |
"test_global"); |
| 1034 |
|
| 1035 |
gcc_jit_type *void_ptr_type = |
| 1036 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR); |
| 1037 |
|
| 1038 |
gcc_jit_function *test_get_address = |
| 1039 |
gcc_jit_context_new_function (ctxt, NULL, |
| 1040 |
GCC_JIT_FUNCTION_EXPORTED, |
| 1041 |
void_ptr_type, |
| 1042 |
"test_get_address", |
| 1043 |
0, NULL, |
| 1044 |
0); |
| 1045 |
gcc_jit_block *initial = |
| 1046 |
gcc_jit_function_new_block (test_get_address, "initial"); |
| 1047 |
gcc_jit_block_end_with_return ( |
| 1048 |
initial, |
| 1049 |
NULL, |
| 1050 |
gcc_jit_lvalue_get_address ( |
| 1051 |
test_global, |
| 1052 |
NULL)); |
| 1053 |
} |
| 1054 |
|
| 1055 |
static void |
| 1056 |
verify_get_address (gcc_jit_result *result) |
| 1057 |
{ |
| 1058 |
typedef void *(*test_fn) (void); |
| 1059 |
test_fn test_get_address = |
| 1060 |
(test_fn)gcc_jit_result_get_code (result, |
| 1061 |
"test_get_address"); |
| 1062 |
CHECK_NON_NULL (test_get_address); |
| 1063 |
CHECK_VALUE (test_get_address (), &test_global); |
| 1064 |
} |
| 1065 |
|
| 1066 |
/********************************************************************** |
| 1067 |
Vector values |
| 1068 |
**********************************************************************/ |
| 1069 |
|
| 1070 |
static void |
| 1071 |
make_test_of_vectors (gcc_jit_context *ctxt) |
| 1072 |
{ |
| 1073 |
gcc_jit_type *scalar_type; |
| 1074 |
gcc_jit_type *vec_type; |
| 1075 |
gcc_jit_rvalue *elements[4]; |
| 1076 |
|
| 1077 |
scalar_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); |
| 1078 |
|
| 1079 |
vec_type = gcc_jit_type_get_vector (scalar_type, 4); |
| 1080 |
|
| 1081 |
elements[0] = gcc_jit_context_new_rvalue_from_int (ctxt, scalar_type, 1); |
| 1082 |
elements[1] = gcc_jit_context_new_rvalue_from_int (ctxt, scalar_type, -2); |
| 1083 |
elements[2] = gcc_jit_context_new_rvalue_from_int (ctxt, scalar_type, 3); |
| 1084 |
elements[3] = gcc_jit_context_new_rvalue_from_int (ctxt, scalar_type, -4); |
| 1085 |
|
| 1086 |
gcc_jit_rvalue *vec_rvalue |
| 1087 |
= gcc_jit_context_new_rvalue_from_vector (ctxt, NULL, vec_type, |
| 1088 |
4, elements); |
| 1089 |
CHECK_STRING_VALUE ( |
| 1090 |
gcc_jit_object_get_debug_string ( |
| 1091 |
gcc_jit_rvalue_as_object (vec_rvalue)), |
| 1092 |
"{(int)1, (int)-2, (int)3, (int)-4}"); |
| 1093 |
} |
| 1094 |
|
| 1095 |
/********************************************************************** |
| 1096 |
Code for harness |
| 1097 |
**********************************************************************/ |
| 1098 |
|
| 1099 |
void |
| 1100 |
create_code (gcc_jit_context *ctxt, void *user_data) |
| 1101 |
{ |
| 1102 |
make_tests_of_unary_ops (ctxt); |
| 1103 |
make_tests_of_binary_ops (ctxt); |
| 1104 |
make_tests_of_comparisons (ctxt); |
| 1105 |
make_tests_of_casts (ctxt); |
| 1106 |
make_tests_of_dereferences (ctxt); |
| 1107 |
make_test_of_get_address (ctxt); |
| 1108 |
make_test_of_vectors (ctxt); |
| 1109 |
} |
| 1110 |
|
| 1111 |
void |
| 1112 |
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) |
| 1113 |
{ |
| 1114 |
CHECK_NON_NULL (result); |
| 1115 |
|
| 1116 |
verify_unary_ops (result); |
| 1117 |
verify_binary_ops (result); |
| 1118 |
verify_comparisons (result); |
| 1119 |
verify_casts (result); |
| 1120 |
verify_dereferences (result); |
| 1121 |
verify_get_address (result); |
| 1122 |
} |