test-expressions.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stddef.h>
  4. #include <stdbool.h>
  5. #include "libgccjit.h"
  6. #include "harness.h"
  7. /**********************************************************************
  8. Unary ops
  9. **********************************************************************/
  10. static const char *
  11. make_test_of_unary_op (gcc_jit_context *ctxt,
  12. gcc_jit_type *type,
  13. enum gcc_jit_unary_op op,
  14. const char *funcname)
  15. {
  16. /* Make a test function of the form:
  17. T test_unary_op (T a)
  18. {
  19. return OP a;
  20. }
  21. and return a debug dump of the OP so that
  22. the caller can sanity-check the debug dump implementation.
  23. */
  24. gcc_jit_param *param_a =
  25. gcc_jit_context_new_param (ctxt, NULL, type, "a");
  26. gcc_jit_function *test_fn =
  27. gcc_jit_context_new_function (ctxt, NULL,
  28. GCC_JIT_FUNCTION_EXPORTED,
  29. type,
  30. funcname,
  31. 1, &param_a,
  32. 0);
  33. gcc_jit_rvalue *unary_op = gcc_jit_context_new_unary_op (
  34. ctxt,
  35. NULL,
  36. op,
  37. type,
  38. gcc_jit_param_as_rvalue (param_a));
  39. gcc_jit_block *initial = gcc_jit_function_new_block (test_fn, "initial");
  40. gcc_jit_block_end_with_return (initial, NULL, unary_op);
  41. return gcc_jit_object_get_debug_string (
  42. gcc_jit_rvalue_as_object (unary_op));
  43. }
  44. static void
  45. make_tests_of_unary_ops (gcc_jit_context *ctxt)
  46. {
  47. gcc_jit_type *int_type =
  48. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  49. CHECK_STRING_VALUE (
  50. make_test_of_unary_op (ctxt,
  51. int_type,
  52. GCC_JIT_UNARY_OP_MINUS,
  53. "test_UNARY_OP_MINUS_on_int"),
  54. "-(a)");
  55. CHECK_STRING_VALUE (
  56. make_test_of_unary_op (ctxt,
  57. int_type,
  58. GCC_JIT_UNARY_OP_BITWISE_NEGATE,
  59. "test_UNARY_OP_BITWISE_NEGATE_on_int"),
  60. "~(a)");
  61. CHECK_STRING_VALUE (
  62. make_test_of_unary_op (ctxt,
  63. int_type,
  64. GCC_JIT_UNARY_OP_LOGICAL_NEGATE,
  65. "test_UNARY_OP_LOGICAL_NEGATE_on_int"),
  66. "!(a)");
  67. CHECK_STRING_VALUE (
  68. make_test_of_unary_op (ctxt,
  69. int_type,
  70. GCC_JIT_UNARY_OP_ABS,
  71. "test_UNARY_OP_ABS_on_int"),
  72. "abs (a)");
  73. }
  74. static void
  75. verify_unary_ops (gcc_jit_result *result)
  76. {
  77. typedef int (*test_fn) (int);
  78. test_fn test_UNARY_OP_MINUS_on_int =
  79. (test_fn)gcc_jit_result_get_code (result,
  80. "test_UNARY_OP_MINUS_on_int");
  81. CHECK_NON_NULL (test_UNARY_OP_MINUS_on_int);
  82. CHECK_VALUE (test_UNARY_OP_MINUS_on_int (0), 0);
  83. CHECK_VALUE (test_UNARY_OP_MINUS_on_int (42), -42);
  84. CHECK_VALUE (test_UNARY_OP_MINUS_on_int (-5), 5);
  85. test_fn test_UNARY_OP_BITWISE_NEGATE_on_int =
  86. (test_fn)gcc_jit_result_get_code (result,
  87. "test_UNARY_OP_BITWISE_NEGATE_on_int");
  88. CHECK_NON_NULL (test_UNARY_OP_BITWISE_NEGATE_on_int);
  89. CHECK_VALUE (test_UNARY_OP_BITWISE_NEGATE_on_int (0), ~0);
  90. CHECK_VALUE (test_UNARY_OP_BITWISE_NEGATE_on_int (42), ~42);
  91. CHECK_VALUE (test_UNARY_OP_BITWISE_NEGATE_on_int (-5), ~-5);
  92. test_fn test_UNARY_OP_LOGICAL_NEGATE_on_int =
  93. (test_fn)gcc_jit_result_get_code (result,
  94. "test_UNARY_OP_LOGICAL_NEGATE_on_int");
  95. CHECK_NON_NULL (test_UNARY_OP_LOGICAL_NEGATE_on_int);
  96. CHECK_VALUE (test_UNARY_OP_LOGICAL_NEGATE_on_int (0), 1);
  97. CHECK_VALUE (test_UNARY_OP_LOGICAL_NEGATE_on_int (42), 0);
  98. CHECK_VALUE (test_UNARY_OP_LOGICAL_NEGATE_on_int (-5), 0);
  99. test_fn test_UNARY_OP_ABS_on_int =
  100. (test_fn)gcc_jit_result_get_code (result,
  101. "test_UNARY_OP_ABS_on_int");
  102. CHECK_NON_NULL (test_UNARY_OP_ABS_on_int);
  103. CHECK_VALUE (test_UNARY_OP_ABS_on_int (0), 0);
  104. CHECK_VALUE (test_UNARY_OP_ABS_on_int (42), 42);
  105. CHECK_VALUE (test_UNARY_OP_ABS_on_int (-5), 5);
  106. }
  107. /**********************************************************************
  108. Binary ops
  109. **********************************************************************/
  110. static const char *
  111. make_test_of_binary_op (gcc_jit_context *ctxt,
  112. gcc_jit_type *type,
  113. enum gcc_jit_binary_op op,
  114. const char *funcname)
  115. {
  116. /* Make a test function of the form:
  117. T test_binary_op (T a, T b)
  118. {
  119. return a OP b;
  120. }
  121. */
  122. gcc_jit_param *param_a =
  123. gcc_jit_context_new_param (ctxt, NULL, type, "a");
  124. gcc_jit_param *param_b =
  125. gcc_jit_context_new_param (ctxt, NULL, type, "b");
  126. gcc_jit_param *params[] = {param_a, param_b};
  127. gcc_jit_function *test_fn =
  128. gcc_jit_context_new_function (ctxt, NULL,
  129. GCC_JIT_FUNCTION_EXPORTED,
  130. type,
  131. funcname,
  132. 2, params,
  133. 0);
  134. gcc_jit_rvalue *binary_op =
  135. gcc_jit_context_new_binary_op (
  136. ctxt,
  137. NULL,
  138. op,
  139. type,
  140. gcc_jit_param_as_rvalue (param_a),
  141. gcc_jit_param_as_rvalue (param_b));
  142. gcc_jit_block *initial = gcc_jit_function_new_block (test_fn, "initial");
  143. gcc_jit_block_end_with_return (initial, NULL, binary_op);
  144. return gcc_jit_object_get_debug_string (
  145. gcc_jit_rvalue_as_object (binary_op));
  146. }
  147. static void
  148. make_tests_of_binary_ops (gcc_jit_context *ctxt)
  149. {
  150. gcc_jit_type *int_type =
  151. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  152. /* Test binary ops. */
  153. CHECK_STRING_VALUE (
  154. make_test_of_binary_op (ctxt,
  155. int_type,
  156. GCC_JIT_BINARY_OP_PLUS,
  157. "test_BINARY_OP_PLUS_on_int"),
  158. "a + b");
  159. CHECK_STRING_VALUE (
  160. make_test_of_binary_op (ctxt,
  161. int_type,
  162. GCC_JIT_BINARY_OP_MINUS,
  163. "test_BINARY_OP_MINUS_on_int"),
  164. "a - b");
  165. CHECK_STRING_VALUE (
  166. make_test_of_binary_op (ctxt,
  167. int_type,
  168. GCC_JIT_BINARY_OP_MULT,
  169. "test_BINARY_OP_MULT_on_int"),
  170. "a * b");
  171. CHECK_STRING_VALUE (
  172. make_test_of_binary_op (ctxt,
  173. int_type,
  174. GCC_JIT_BINARY_OP_DIVIDE,
  175. "test_BINARY_OP_DIVIDE_on_int"),
  176. "a / b");
  177. /* TODO: test for DIVIDE on float or double */
  178. CHECK_STRING_VALUE (
  179. make_test_of_binary_op (ctxt,
  180. int_type,
  181. GCC_JIT_BINARY_OP_MODULO,
  182. "test_BINARY_OP_MODULO_on_int"),
  183. "a % b");
  184. CHECK_STRING_VALUE (
  185. make_test_of_binary_op (ctxt,
  186. int_type,
  187. GCC_JIT_BINARY_OP_BITWISE_AND,
  188. "test_BINARY_OP_BITWISE_AND_on_int"),
  189. "a & b");
  190. CHECK_STRING_VALUE (
  191. make_test_of_binary_op (ctxt,
  192. int_type,
  193. GCC_JIT_BINARY_OP_BITWISE_XOR,
  194. "test_BINARY_OP_BITWISE_XOR_on_int"),
  195. "a ^ b");
  196. CHECK_STRING_VALUE (
  197. make_test_of_binary_op (ctxt,
  198. int_type,
  199. GCC_JIT_BINARY_OP_BITWISE_OR,
  200. "test_BINARY_OP_BITWISE_OR_on_int"),
  201. "a | b");
  202. CHECK_STRING_VALUE (
  203. make_test_of_binary_op (ctxt,
  204. int_type,
  205. GCC_JIT_BINARY_OP_LOGICAL_AND,
  206. "test_BINARY_OP_LOGICAL_AND_on_int"),
  207. "a && b");
  208. CHECK_STRING_VALUE (
  209. make_test_of_binary_op (ctxt,
  210. int_type,
  211. GCC_JIT_BINARY_OP_LOGICAL_OR,
  212. "test_BINARY_OP_LOGICAL_OR_on_int"),
  213. "a || b");
  214. CHECK_STRING_VALUE (
  215. make_test_of_binary_op (ctxt,
  216. int_type,
  217. GCC_JIT_BINARY_OP_LSHIFT,
  218. "test_BINARY_OP_LSHIFT_on_int"),
  219. "a << b");
  220. CHECK_STRING_VALUE (
  221. make_test_of_binary_op (ctxt,
  222. int_type,
  223. GCC_JIT_BINARY_OP_RSHIFT,
  224. "test_BINARY_OP_RSHIFT_on_int"),
  225. "a >> b");
  226. }
  227. static void
  228. verify_binary_ops (gcc_jit_result *result)
  229. {
  230. typedef int (*test_fn) (int, int);
  231. test_fn test_BINARY_OP_PLUS_on_int =
  232. (test_fn)gcc_jit_result_get_code (result,
  233. "test_BINARY_OP_PLUS_on_int");
  234. CHECK_NON_NULL (test_BINARY_OP_PLUS_on_int);
  235. CHECK_VALUE (test_BINARY_OP_PLUS_on_int (0, 0), 0);
  236. CHECK_VALUE (test_BINARY_OP_PLUS_on_int (1, 2), 3);
  237. CHECK_VALUE (test_BINARY_OP_PLUS_on_int (100, -1), 99);
  238. CHECK_VALUE (test_BINARY_OP_PLUS_on_int (-1, -4), -5);
  239. test_fn test_BINARY_OP_MINUS_on_int =
  240. (test_fn)gcc_jit_result_get_code (result,
  241. "test_BINARY_OP_MINUS_on_int");
  242. CHECK_NON_NULL (test_BINARY_OP_MINUS_on_int);
  243. CHECK_VALUE (test_BINARY_OP_MINUS_on_int (0, 0), 0);
  244. CHECK_VALUE (test_BINARY_OP_MINUS_on_int (1, 2), -1);
  245. CHECK_VALUE (test_BINARY_OP_MINUS_on_int (100, -1), 101);
  246. CHECK_VALUE (test_BINARY_OP_MINUS_on_int (-1, -4), 3);
  247. test_fn test_BINARY_OP_MULT_on_int =
  248. (test_fn)gcc_jit_result_get_code (result,
  249. "test_BINARY_OP_MULT_on_int");
  250. CHECK_NON_NULL (test_BINARY_OP_MULT_on_int);
  251. CHECK_VALUE (test_BINARY_OP_MULT_on_int (0, 0), 0);
  252. CHECK_VALUE (test_BINARY_OP_MULT_on_int (1, 2), 2);
  253. CHECK_VALUE (test_BINARY_OP_MULT_on_int (100, -1), -100);
  254. CHECK_VALUE (test_BINARY_OP_MULT_on_int (-1, -4), 4);
  255. CHECK_VALUE (test_BINARY_OP_MULT_on_int (7, 10), 70);
  256. test_fn test_BINARY_OP_DIVIDE_on_int =
  257. (test_fn)gcc_jit_result_get_code (result,
  258. "test_BINARY_OP_DIVIDE_on_int");
  259. CHECK_NON_NULL (test_BINARY_OP_DIVIDE_on_int);
  260. CHECK_VALUE (test_BINARY_OP_DIVIDE_on_int (7, 2), 3);
  261. CHECK_VALUE (test_BINARY_OP_DIVIDE_on_int (100, -1), (100 / -1));
  262. CHECK_VALUE (test_BINARY_OP_DIVIDE_on_int (-1, -4), (-1 / -4));
  263. CHECK_VALUE (test_BINARY_OP_DIVIDE_on_int (60, 5), 12);
  264. /* TODO: test for DIVIDE on float or double */
  265. test_fn test_BINARY_OP_MODULO_on_int =
  266. (test_fn)gcc_jit_result_get_code (result,
  267. "test_BINARY_OP_MODULO_on_int");
  268. CHECK_NON_NULL (test_BINARY_OP_MODULO_on_int);
  269. CHECK_VALUE (test_BINARY_OP_MODULO_on_int (7, 2), 1);
  270. CHECK_VALUE (test_BINARY_OP_MODULO_on_int (100, -1), (100 % -1));
  271. CHECK_VALUE (test_BINARY_OP_MODULO_on_int (-1, -4), (-1 % -4));
  272. CHECK_VALUE (test_BINARY_OP_MODULO_on_int (60, 5), 0);
  273. test_fn test_BINARY_OP_BITWISE_AND_on_int =
  274. (test_fn)gcc_jit_result_get_code (result,
  275. "test_BINARY_OP_BITWISE_AND_on_int");
  276. CHECK_NON_NULL (test_BINARY_OP_BITWISE_AND_on_int);
  277. CHECK_VALUE (test_BINARY_OP_BITWISE_AND_on_int (0xf0f0, 0x7777), 0x7070);
  278. test_fn test_BINARY_OP_BITWISE_XOR_on_int =
  279. (test_fn)gcc_jit_result_get_code (result,
  280. "test_BINARY_OP_BITWISE_XOR_on_int");
  281. CHECK_NON_NULL (test_BINARY_OP_BITWISE_XOR_on_int);
  282. CHECK_VALUE (test_BINARY_OP_BITWISE_XOR_on_int (0xf0f0, 0x7777), 0x8787);
  283. test_fn test_BINARY_OP_BITWISE_OR_on_int =
  284. (test_fn)gcc_jit_result_get_code (result,
  285. "test_BINARY_OP_BITWISE_OR_on_int");
  286. CHECK_NON_NULL (test_BINARY_OP_BITWISE_OR_on_int);
  287. CHECK_VALUE (test_BINARY_OP_BITWISE_OR_on_int (0xf0f0, 0x7777), 0xf7f7);
  288. test_fn test_BINARY_OP_LOGICAL_AND_on_int =
  289. (test_fn)gcc_jit_result_get_code (result,
  290. "test_BINARY_OP_LOGICAL_AND_on_int");
  291. CHECK_NON_NULL (test_BINARY_OP_LOGICAL_AND_on_int);
  292. CHECK_VALUE (test_BINARY_OP_LOGICAL_AND_on_int (0, 0), 0);
  293. CHECK_VALUE (test_BINARY_OP_LOGICAL_AND_on_int (42, 0), 0);
  294. CHECK_VALUE (test_BINARY_OP_LOGICAL_AND_on_int (0, -13), 0);
  295. CHECK_VALUE (test_BINARY_OP_LOGICAL_AND_on_int (1997, 1998), 1);
  296. test_fn test_BINARY_OP_LOGICAL_OR_on_int =
  297. (test_fn)gcc_jit_result_get_code (result,
  298. "test_BINARY_OP_LOGICAL_OR_on_int");
  299. CHECK_NON_NULL (test_BINARY_OP_LOGICAL_OR_on_int);
  300. CHECK_VALUE (test_BINARY_OP_LOGICAL_OR_on_int (0, 0), 0);
  301. CHECK_VALUE (test_BINARY_OP_LOGICAL_OR_on_int (42, 0), 1);
  302. CHECK_VALUE (test_BINARY_OP_LOGICAL_OR_on_int (0, -13), 1);
  303. CHECK_VALUE (test_BINARY_OP_LOGICAL_OR_on_int (1997, 1998), 1);
  304. test_fn test_BINARY_OP_LSHIFT_on_int =
  305. (test_fn)gcc_jit_result_get_code (result,
  306. "test_BINARY_OP_LSHIFT_on_int");
  307. CHECK_NON_NULL (test_BINARY_OP_LSHIFT_on_int);
  308. CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (0, 0), 0);
  309. CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (0, 1), 0);
  310. CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (0, 2), 0);
  311. CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (1, 0), 1);
  312. CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (1, 1), 2);
  313. CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (1, 2), 4);
  314. CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (1, 3), 8);
  315. CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (3, 0), 3);
  316. CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (3, 1), 6);
  317. CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (3, 5), 3 * 32);
  318. CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (42, 0), 42);
  319. CHECK_VALUE (test_BINARY_OP_LSHIFT_on_int (42, 1), 84);
  320. test_fn test_BINARY_OP_RSHIFT_on_int =
  321. (test_fn)gcc_jit_result_get_code (result,
  322. "test_BINARY_OP_RSHIFT_on_int");
  323. CHECK_NON_NULL (test_BINARY_OP_RSHIFT_on_int);
  324. CHECK_VALUE (test_BINARY_OP_RSHIFT_on_int (0, 0), 0);
  325. CHECK_VALUE (test_BINARY_OP_RSHIFT_on_int (42, 0), 42);
  326. CHECK_VALUE (test_BINARY_OP_RSHIFT_on_int (42, 1), 21);
  327. CHECK_VALUE (test_BINARY_OP_RSHIFT_on_int (42, 2), 10);
  328. }
  329. /**********************************************************************
  330. Comparisons
  331. **********************************************************************/
  332. static const char *
  333. make_test_of_comparison (gcc_jit_context *ctxt,
  334. gcc_jit_type *type,
  335. enum gcc_jit_comparison op,
  336. const char *funcname)
  337. {
  338. /* Make a test function of the form:
  339. bool test_comparison_op (T a, T b)
  340. {
  341. return a OP b;
  342. }
  343. */
  344. gcc_jit_param *param_a =
  345. gcc_jit_context_new_param (ctxt, NULL, type, "a");
  346. gcc_jit_param *param_b =
  347. gcc_jit_context_new_param (ctxt, NULL, type, "b");
  348. gcc_jit_param *params[] = {param_a, param_b};
  349. gcc_jit_type *bool_type =
  350. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BOOL);
  351. gcc_jit_function *test_fn =
  352. gcc_jit_context_new_function (ctxt, NULL,
  353. GCC_JIT_FUNCTION_EXPORTED,
  354. bool_type,
  355. funcname,
  356. 2, params,
  357. 0);
  358. gcc_jit_rvalue *comparison =
  359. gcc_jit_context_new_comparison (
  360. ctxt,
  361. NULL,
  362. op,
  363. gcc_jit_param_as_rvalue (param_a),
  364. gcc_jit_param_as_rvalue (param_b));
  365. gcc_jit_block *initial = gcc_jit_function_new_block (test_fn, "initial");
  366. gcc_jit_block_end_with_return (initial, NULL, comparison);
  367. return gcc_jit_object_get_debug_string (
  368. gcc_jit_rvalue_as_object (comparison));
  369. }
  370. static void
  371. make_tests_of_comparisons (gcc_jit_context *ctxt)
  372. {
  373. gcc_jit_type *int_type =
  374. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  375. CHECK_STRING_VALUE (
  376. make_test_of_comparison (ctxt,
  377. int_type,
  378. GCC_JIT_COMPARISON_EQ,
  379. "test_COMPARISON_EQ_on_int"),
  380. "a == b");
  381. CHECK_STRING_VALUE (
  382. make_test_of_comparison (ctxt,
  383. int_type,
  384. GCC_JIT_COMPARISON_NE,
  385. "test_COMPARISON_NE_on_int"),
  386. "a != b");
  387. CHECK_STRING_VALUE (
  388. make_test_of_comparison (ctxt,
  389. int_type,
  390. GCC_JIT_COMPARISON_LT,
  391. "test_COMPARISON_LT_on_int"),
  392. "a < b");
  393. CHECK_STRING_VALUE (
  394. make_test_of_comparison (ctxt,
  395. int_type,
  396. GCC_JIT_COMPARISON_LE,
  397. "test_COMPARISON_LE_on_int"),
  398. "a <= b");
  399. CHECK_STRING_VALUE (
  400. make_test_of_comparison (ctxt,
  401. int_type,
  402. GCC_JIT_COMPARISON_GT,
  403. "test_COMPARISON_GT_on_int"),
  404. "a > b");
  405. CHECK_STRING_VALUE (
  406. make_test_of_comparison (ctxt,
  407. int_type,
  408. GCC_JIT_COMPARISON_GE,
  409. "test_COMPARISON_GE_on_int"),
  410. "a >= b");
  411. }
  412. static void
  413. verify_comparisons (gcc_jit_result *result)
  414. {
  415. typedef bool (*test_fn) (int, int);
  416. test_fn test_COMPARISON_EQ_on_int =
  417. (test_fn)gcc_jit_result_get_code (result,
  418. "test_COMPARISON_EQ_on_int");
  419. CHECK_NON_NULL (test_COMPARISON_EQ_on_int);
  420. CHECK_VALUE (test_COMPARISON_EQ_on_int (0, 0), 1);
  421. CHECK_VALUE (test_COMPARISON_EQ_on_int (1, 2), 0);
  422. test_fn test_COMPARISON_NE_on_int =
  423. (test_fn)gcc_jit_result_get_code (result,
  424. "test_COMPARISON_NE_on_int");
  425. CHECK_NON_NULL (test_COMPARISON_NE_on_int);
  426. CHECK_VALUE (test_COMPARISON_NE_on_int (0, 0), 0);
  427. CHECK_VALUE (test_COMPARISON_NE_on_int (1, 2), 1);
  428. test_fn test_COMPARISON_LT_on_int =
  429. (test_fn)gcc_jit_result_get_code (result,
  430. "test_COMPARISON_LT_on_int");
  431. CHECK_NON_NULL (test_COMPARISON_LT_on_int);
  432. CHECK_VALUE (test_COMPARISON_LT_on_int (0, 0), 0);
  433. CHECK_VALUE (test_COMPARISON_LT_on_int (1, 2), 1);
  434. CHECK_VALUE (test_COMPARISON_LT_on_int (2, 1), 0);
  435. CHECK_VALUE (test_COMPARISON_LT_on_int (-2, 1), 1);
  436. test_fn test_COMPARISON_LE_on_int =
  437. (test_fn)gcc_jit_result_get_code (result,
  438. "test_COMPARISON_LE_on_int");
  439. CHECK_NON_NULL (test_COMPARISON_LE_on_int);
  440. CHECK_VALUE (test_COMPARISON_LE_on_int (0, 0), 1);
  441. CHECK_VALUE (test_COMPARISON_LE_on_int (1, 2), 1);
  442. CHECK_VALUE (test_COMPARISON_LE_on_int (2, 1), 0);
  443. test_fn test_COMPARISON_GT_on_int =
  444. (test_fn)gcc_jit_result_get_code (result,
  445. "test_COMPARISON_GT_on_int");
  446. CHECK_NON_NULL (test_COMPARISON_GT_on_int);
  447. CHECK_VALUE (test_COMPARISON_GT_on_int (0, 0), 0);
  448. CHECK_VALUE (test_COMPARISON_GT_on_int (1, 2), 0);
  449. CHECK_VALUE (test_COMPARISON_GT_on_int (2, 1), 1);
  450. test_fn test_COMPARISON_GE_on_int =
  451. (test_fn)gcc_jit_result_get_code (result,
  452. "test_COMPARISON_GE_on_int");
  453. CHECK_NON_NULL (test_COMPARISON_GE_on_int);
  454. CHECK_VALUE (test_COMPARISON_GE_on_int (0, 0), 1);
  455. CHECK_VALUE (test_COMPARISON_GE_on_int (1, 2), 0);
  456. CHECK_VALUE (test_COMPARISON_GE_on_int (2, 1), 1);
  457. }
  458. /**********************************************************************
  459. Casts
  460. **********************************************************************/
  461. static const char*
  462. make_test_of_cast (gcc_jit_context *ctxt,
  463. gcc_jit_type *input_type,
  464. gcc_jit_type *output_type,
  465. const char *funcname)
  466. {
  467. /* Make a test function of the form:
  468. OUTPUT_TYPE test_cast_* (INPUT_TYPE a)
  469. {
  470. return (OUTPUT_TYPE)a;
  471. }
  472. */
  473. gcc_jit_param *param_a =
  474. gcc_jit_context_new_param (ctxt, NULL, input_type, "a");
  475. gcc_jit_param *params[] = {param_a};
  476. gcc_jit_function *test_fn =
  477. gcc_jit_context_new_function (ctxt, NULL,
  478. GCC_JIT_FUNCTION_EXPORTED,
  479. output_type,
  480. funcname,
  481. 1, params,
  482. 0);
  483. gcc_jit_rvalue *cast =
  484. gcc_jit_context_new_cast (
  485. ctxt,
  486. NULL,
  487. gcc_jit_param_as_rvalue (param_a),
  488. output_type);
  489. gcc_jit_block *initial = gcc_jit_function_new_block (test_fn, "initial");
  490. gcc_jit_block_end_with_return (initial, NULL, cast);
  491. return gcc_jit_object_get_debug_string (
  492. gcc_jit_rvalue_as_object (cast));
  493. }
  494. /* For use by test_cast_from_array_of_ints_to_int_ptr. */
  495. extern int called_pointer_checking_function (int *ints)
  496. {
  497. CHECK_VALUE (ints[0], 10);
  498. CHECK_VALUE (ints[1], 4);
  499. return ints[0] * ints[1];
  500. }
  501. static void
  502. make_tests_of_casts (gcc_jit_context *ctxt)
  503. {
  504. gcc_jit_type *int_type =
  505. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  506. gcc_jit_type *long_type =
  507. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG);
  508. gcc_jit_type *float_type =
  509. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT);
  510. gcc_jit_type *bool_type =
  511. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BOOL);
  512. gcc_jit_type *array_int_type =
  513. gcc_jit_context_new_array_type (ctxt, NULL,
  514. int_type,
  515. 2);
  516. gcc_jit_type *int_ptr_type =
  517. gcc_jit_type_get_pointer (int_type);
  518. /* float/int conversions */
  519. CHECK_STRING_VALUE (
  520. make_test_of_cast (ctxt,
  521. float_type,
  522. int_type,
  523. "test_cast_from_float_to_int"),
  524. "(int)a");
  525. CHECK_STRING_VALUE (
  526. make_test_of_cast (ctxt,
  527. int_type,
  528. float_type,
  529. "test_cast_from_int_to_float"),
  530. "(float)a");
  531. /* bool/int conversions */
  532. CHECK_STRING_VALUE (
  533. make_test_of_cast (ctxt,
  534. bool_type,
  535. int_type,
  536. "test_cast_from_bool_to_int"),
  537. "(int)a");
  538. CHECK_STRING_VALUE (
  539. make_test_of_cast (ctxt,
  540. int_type,
  541. bool_type,
  542. "test_cast_from_int_to_bool"),
  543. "(bool)a");
  544. /* bool/long conversions */
  545. CHECK_STRING_VALUE (
  546. make_test_of_cast (ctxt,
  547. bool_type,
  548. long_type,
  549. "test_cast_from_bool_to_long"),
  550. "(long)a");
  551. CHECK_STRING_VALUE (
  552. make_test_of_cast (ctxt,
  553. long_type,
  554. bool_type,
  555. "test_cast_from_long_to_bool"),
  556. "(bool)a");
  557. /* array/ptr conversions */
  558. {
  559. gcc_jit_function *test_fn =
  560. gcc_jit_context_new_function (
  561. ctxt, NULL,
  562. GCC_JIT_FUNCTION_EXPORTED,
  563. int_type,
  564. "test_cast_from_array_of_ints_to_int_ptr",
  565. 0, NULL,
  566. 0);
  567. /* Equivalent to:
  568. int test_cast_from_array_of_ints_to_int_ptr (void)
  569. {
  570. int array[2];
  571. array[0] = 10;
  572. array[1] = 4;
  573. return called_pointer_checking_function (array);
  574. }
  575. */
  576. gcc_jit_param *param_ints =
  577. gcc_jit_context_new_param (ctxt, NULL, int_ptr_type, "ints");
  578. gcc_jit_function *called_fn =
  579. gcc_jit_context_new_function (
  580. ctxt, NULL,
  581. GCC_JIT_FUNCTION_IMPORTED,
  582. int_type,
  583. "called_pointer_checking_function",
  584. 1, &param_ints,
  585. 0);
  586. gcc_jit_lvalue *array =
  587. gcc_jit_function_new_local (test_fn, NULL,
  588. array_int_type,
  589. "array");
  590. gcc_jit_block *block =
  591. gcc_jit_function_new_block (test_fn, "block");
  592. /* array[0] = 10; */
  593. gcc_jit_block_add_assignment (
  594. block, NULL,
  595. gcc_jit_context_new_array_access (
  596. ctxt, NULL,
  597. gcc_jit_lvalue_as_rvalue (array),
  598. gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 0)),
  599. gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 10));
  600. /* array[1] = 4; */
  601. gcc_jit_block_add_assignment (
  602. block, NULL,
  603. gcc_jit_context_new_array_access (
  604. ctxt, NULL,
  605. gcc_jit_lvalue_as_rvalue (array),
  606. gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 1)),
  607. gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 4));
  608. gcc_jit_rvalue *cast =
  609. gcc_jit_context_new_cast (
  610. ctxt,
  611. NULL,
  612. /* We need a get_address here. */
  613. gcc_jit_lvalue_get_address (array, NULL),
  614. int_ptr_type);
  615. gcc_jit_block_end_with_return (
  616. block, NULL,
  617. gcc_jit_context_new_call (
  618. ctxt, NULL,
  619. called_fn,
  620. 1, &cast));
  621. CHECK_STRING_VALUE (
  622. gcc_jit_object_get_debug_string (
  623. gcc_jit_rvalue_as_object (cast)),
  624. "(int *)&array");
  625. }
  626. }
  627. static void
  628. verify_casts (gcc_jit_result *result)
  629. {
  630. /* float to int */
  631. {
  632. typedef int (*fn_type) (float);
  633. fn_type test_cast_from_float_to_int =
  634. (fn_type)gcc_jit_result_get_code (result,
  635. "test_cast_from_float_to_int");
  636. CHECK_NON_NULL (test_cast_from_float_to_int);
  637. CHECK_VALUE (test_cast_from_float_to_int (4.2), 4);
  638. }
  639. /* int to float */
  640. {
  641. typedef float (*fn_type) (int);
  642. fn_type test_cast_from_int_to_float =
  643. (fn_type)gcc_jit_result_get_code (result,
  644. "test_cast_from_int_to_float");
  645. CHECK_NON_NULL (test_cast_from_int_to_float);
  646. CHECK_VALUE (test_cast_from_int_to_float (4), 4.0);
  647. }
  648. /* bool to int */
  649. {
  650. typedef int (*fn_type) (bool);
  651. fn_type test_cast_from_bool_to_int =
  652. (fn_type)gcc_jit_result_get_code (result,
  653. "test_cast_from_bool_to_int");
  654. CHECK_NON_NULL (test_cast_from_bool_to_int);
  655. CHECK_VALUE (test_cast_from_bool_to_int (0), 0);
  656. CHECK_VALUE (test_cast_from_bool_to_int (1), 1);
  657. }
  658. /* int to bool */
  659. {
  660. typedef bool (*fn_type) (int);
  661. fn_type test_cast_from_int_to_bool =
  662. (fn_type)gcc_jit_result_get_code (result,
  663. "test_cast_from_int_to_bool");
  664. CHECK_NON_NULL (test_cast_from_int_to_bool);
  665. CHECK_VALUE (test_cast_from_int_to_bool (0), 0);
  666. CHECK_VALUE (test_cast_from_int_to_bool (1), 1);
  667. }
  668. /* bool to long */
  669. {
  670. typedef long (*fn_type) (bool);
  671. fn_type test_cast_from_bool_to_long =
  672. (fn_type)gcc_jit_result_get_code (result,
  673. "test_cast_from_bool_to_long");
  674. CHECK_NON_NULL (test_cast_from_bool_to_long);
  675. CHECK_VALUE (test_cast_from_bool_to_long (0), 0);
  676. CHECK_VALUE (test_cast_from_bool_to_long (1), 1);
  677. }
  678. /* long to bool */
  679. {
  680. typedef bool (*fn_type) (long);
  681. fn_type test_cast_from_long_to_bool =
  682. (fn_type)gcc_jit_result_get_code (result,
  683. "test_cast_from_long_to_bool");
  684. CHECK_NON_NULL (test_cast_from_long_to_bool);
  685. CHECK_VALUE (test_cast_from_long_to_bool (0), 0);
  686. CHECK_VALUE (test_cast_from_long_to_bool (1), 1);
  687. }
  688. /* array to ptr */
  689. {
  690. typedef int (*fn_type) (void);
  691. fn_type test_cast_from_array_of_ints_to_int_ptr =
  692. (fn_type)gcc_jit_result_get_code (
  693. result,
  694. "test_cast_from_array_of_ints_to_int_ptr");
  695. CHECK_NON_NULL (test_cast_from_array_of_ints_to_int_ptr);
  696. CHECK_VALUE (test_cast_from_array_of_ints_to_int_ptr (), 40);
  697. }
  698. }
  699. /**********************************************************************
  700. Dereferences
  701. **********************************************************************/
  702. static void
  703. make_tests_of_dereferences (gcc_jit_context *ctxt)
  704. {
  705. /*
  706. int test_dereference_read (int *ptr)
  707. {
  708. return *ptr;
  709. }
  710. void test_dereference_write (int *ptr, int i)
  711. {
  712. *ptr = i;
  713. }
  714. */
  715. gcc_jit_type *void_type =
  716. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  717. gcc_jit_type *int_type =
  718. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  719. gcc_jit_type *int_ptr_type =
  720. gcc_jit_type_get_pointer (int_type);
  721. {
  722. gcc_jit_param *param_ptr =
  723. gcc_jit_context_new_param (ctxt, NULL, int_ptr_type, "ptr");
  724. gcc_jit_function *test_dereference_read =
  725. gcc_jit_context_new_function (ctxt, NULL,
  726. GCC_JIT_FUNCTION_EXPORTED,
  727. int_type,
  728. "test_dereference_read",
  729. 1, &param_ptr,
  730. 0);
  731. gcc_jit_block *initial =
  732. gcc_jit_function_new_block (test_dereference_read, "initial");
  733. gcc_jit_block_end_with_return (
  734. initial,
  735. NULL,
  736. gcc_jit_lvalue_as_rvalue (
  737. gcc_jit_rvalue_dereference (
  738. gcc_jit_param_as_rvalue (param_ptr),
  739. NULL)));
  740. }
  741. {
  742. gcc_jit_param *param_ptr =
  743. gcc_jit_context_new_param (ctxt, NULL, int_ptr_type, "ptr");
  744. gcc_jit_param *param_i =
  745. gcc_jit_context_new_param (ctxt, NULL, int_type, "i");
  746. gcc_jit_param *params[] = {param_ptr, param_i};
  747. gcc_jit_function *test_dereference_write =
  748. gcc_jit_context_new_function (ctxt, NULL,
  749. GCC_JIT_FUNCTION_EXPORTED,
  750. void_type,
  751. "test_dereference_write",
  752. 2, params,
  753. 0);
  754. gcc_jit_block *initial =
  755. gcc_jit_function_new_block (test_dereference_write, "initial");
  756. gcc_jit_block_add_assignment (
  757. initial,
  758. NULL,
  759. gcc_jit_rvalue_dereference (
  760. gcc_jit_param_as_rvalue (param_ptr),
  761. NULL),
  762. gcc_jit_param_as_rvalue (param_i));
  763. gcc_jit_block_end_with_void_return (initial, NULL);
  764. }
  765. }
  766. static void
  767. verify_dereferences (gcc_jit_result *result)
  768. {
  769. int a = 42;
  770. int b = -99;
  771. {
  772. typedef int (*test_read) (int *);
  773. test_read test_dereference_read =
  774. (test_read)gcc_jit_result_get_code (result,
  775. "test_dereference_read");
  776. CHECK_NON_NULL (test_dereference_read);
  777. CHECK_VALUE (test_dereference_read (&a), 42);
  778. CHECK_VALUE (test_dereference_read (&b), -99);
  779. }
  780. {
  781. typedef void (*test_write) (int *, int);
  782. test_write test_dereference_write =
  783. (test_write)gcc_jit_result_get_code (result,
  784. "test_dereference_write");
  785. CHECK_NON_NULL (test_dereference_write);
  786. test_dereference_write (&a, -55);
  787. CHECK_VALUE (a, -55);
  788. test_dereference_write (&b, 404);
  789. CHECK_VALUE (b, 404);
  790. }
  791. }
  792. /**********************************************************************
  793. gcc_jit_lvalue_get_address
  794. **********************************************************************/
  795. int test_global;
  796. static void
  797. make_test_of_get_address (gcc_jit_context *ctxt)
  798. {
  799. /*
  800. void *test_get_address (void)
  801. {
  802. return &test_global;
  803. }
  804. */
  805. gcc_jit_type *int_type =
  806. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  807. gcc_jit_lvalue *test_global =
  808. gcc_jit_context_new_global (
  809. ctxt,
  810. NULL,
  811. GCC_JIT_GLOBAL_IMPORTED,
  812. int_type,
  813. "test_global");
  814. gcc_jit_type *void_ptr_type =
  815. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
  816. gcc_jit_function *test_get_address =
  817. gcc_jit_context_new_function (ctxt, NULL,
  818. GCC_JIT_FUNCTION_EXPORTED,
  819. void_ptr_type,
  820. "test_get_address",
  821. 0, NULL,
  822. 0);
  823. gcc_jit_block *initial =
  824. gcc_jit_function_new_block (test_get_address, "initial");
  825. gcc_jit_block_end_with_return (
  826. initial,
  827. NULL,
  828. gcc_jit_lvalue_get_address (
  829. test_global,
  830. NULL));
  831. }
  832. static void
  833. verify_get_address (gcc_jit_result *result)
  834. {
  835. typedef void *(*test_fn) (void);
  836. test_fn test_get_address =
  837. (test_fn)gcc_jit_result_get_code (result,
  838. "test_get_address");
  839. CHECK_NON_NULL (test_get_address);
  840. CHECK_VALUE (test_get_address (), &test_global);
  841. }
  842. /**********************************************************************
  843. Code for harness
  844. **********************************************************************/
  845. void
  846. create_code (gcc_jit_context *ctxt, void *user_data)
  847. {
  848. make_tests_of_unary_ops (ctxt);
  849. make_tests_of_binary_ops (ctxt);
  850. make_tests_of_comparisons (ctxt);
  851. make_tests_of_casts (ctxt);
  852. make_tests_of_dereferences (ctxt);
  853. make_test_of_get_address (ctxt);
  854. }
  855. void
  856. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  857. {
  858. CHECK_NON_NULL (result);
  859. verify_unary_ops (result);
  860. verify_binary_ops (result);
  861. verify_comparisons (result);
  862. verify_casts (result);
  863. verify_dereferences (result);
  864. verify_get_address (result);
  865. }