test-constants.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include <limits.h>
  2. #include <float.h>
  3. #include "libgccjit.h"
  4. #include "harness.h"
  5. static void
  6. make_test_of_constant (gcc_jit_context *ctxt,
  7. gcc_jit_type *type,
  8. gcc_jit_rvalue *rvalue,
  9. const char *funcname)
  10. {
  11. /* Make a test function of the form:
  12. T funcname (void)
  13. {
  14. return VALUE;
  15. }
  16. and return a debug dump of VALUE so that
  17. the caller can sanity-check the debug dump implementation.
  18. */
  19. gcc_jit_function *test_fn =
  20. gcc_jit_context_new_function (ctxt, NULL,
  21. GCC_JIT_FUNCTION_EXPORTED,
  22. type,
  23. funcname,
  24. 0, NULL,
  25. 0);
  26. gcc_jit_block *initial = gcc_jit_function_new_block (test_fn, "initial");
  27. gcc_jit_block_end_with_return (initial, NULL, rvalue);
  28. }
  29. /**********************************************************************
  30. Tests of gcc_jit_context_new_rvalue_from_int.
  31. **********************************************************************/
  32. static const char *
  33. make_test_of_int_constant (gcc_jit_context *ctxt,
  34. gcc_jit_type *type,
  35. int value,
  36. const char *funcname)
  37. {
  38. /* Make a test function of the form:
  39. int funcname (void)
  40. {
  41. return VALUE;
  42. }
  43. and return a debug dump of VALUE so that
  44. the caller can sanity-check the debug dump implementation.
  45. */
  46. gcc_jit_rvalue *rvalue =
  47. gcc_jit_context_new_rvalue_from_int (ctxt, type, value);
  48. make_test_of_constant (ctxt, type, rvalue, funcname);
  49. return gcc_jit_object_get_debug_string (
  50. gcc_jit_rvalue_as_object (rvalue));
  51. }
  52. static void
  53. make_tests_of_int_constants (gcc_jit_context *ctxt)
  54. {
  55. gcc_jit_type *int_type =
  56. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  57. CHECK_STRING_VALUE (
  58. make_test_of_int_constant (ctxt,
  59. int_type,
  60. 0,
  61. "test_int_constant_0"),
  62. "(int)0");
  63. make_test_of_int_constant (ctxt,
  64. int_type,
  65. INT_MAX,
  66. "test_int_constant_INT_MAX");
  67. make_test_of_int_constant (ctxt,
  68. int_type,
  69. INT_MIN,
  70. "test_int_constant_INT_MIN");
  71. }
  72. static void
  73. verify_int_constants (gcc_jit_result *result)
  74. {
  75. typedef int (*test_fn) (void);
  76. test_fn test_int_constant_0 =
  77. (test_fn)gcc_jit_result_get_code (result,
  78. "test_int_constant_0");
  79. CHECK_NON_NULL (test_int_constant_0);
  80. CHECK_VALUE (test_int_constant_0 (), 0);
  81. test_fn test_int_constant_INT_MAX =
  82. (test_fn)gcc_jit_result_get_code (result,
  83. "test_int_constant_INT_MAX");
  84. CHECK_NON_NULL (test_int_constant_INT_MAX);
  85. CHECK_VALUE (test_int_constant_INT_MAX (), INT_MAX);
  86. test_fn test_int_constant_INT_MIN =
  87. (test_fn)gcc_jit_result_get_code (result,
  88. "test_int_constant_INT_MIN");
  89. CHECK_NON_NULL (test_int_constant_INT_MIN);
  90. CHECK_VALUE (test_int_constant_INT_MIN (), INT_MIN);
  91. }
  92. /**********************************************************************
  93. Tests of gcc_jit_context_new_rvalue_from_long.
  94. **********************************************************************/
  95. static const char *
  96. make_test_of_long_constant (gcc_jit_context *ctxt,
  97. gcc_jit_type *type,
  98. long value,
  99. const char *funcname)
  100. {
  101. /* Make a test function of the form:
  102. long funcname (void)
  103. {
  104. return VALUE;
  105. }
  106. and return a debug dump of VALUE so that
  107. the caller can sanity-check the debug dump implementation.
  108. */
  109. gcc_jit_rvalue *rvalue =
  110. gcc_jit_context_new_rvalue_from_long (ctxt, type, value);
  111. make_test_of_constant (ctxt, type, rvalue, funcname);
  112. return gcc_jit_object_get_debug_string (
  113. gcc_jit_rvalue_as_object (rvalue));
  114. }
  115. static void
  116. make_tests_of_long_constants (gcc_jit_context *ctxt)
  117. {
  118. gcc_jit_type *long_type =
  119. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG);
  120. CHECK_STRING_VALUE (
  121. make_test_of_long_constant (ctxt,
  122. long_type,
  123. 0,
  124. "test_long_constant_0"),
  125. "(long)0");
  126. make_test_of_long_constant (ctxt,
  127. long_type,
  128. LONG_MAX,
  129. "test_long_constant_LONG_MAX");
  130. make_test_of_long_constant (ctxt,
  131. long_type,
  132. LONG_MIN,
  133. "test_long_constant_LONG_MIN");
  134. }
  135. static void
  136. verify_long_constants (gcc_jit_result *result)
  137. {
  138. typedef long (*test_fn) (void);
  139. test_fn test_long_constant_0 =
  140. (test_fn)gcc_jit_result_get_code (result,
  141. "test_long_constant_0");
  142. CHECK_NON_NULL (test_long_constant_0);
  143. CHECK_VALUE (test_long_constant_0 (), 0);
  144. test_fn test_long_constant_LONG_MAX =
  145. (test_fn)gcc_jit_result_get_code (result,
  146. "test_long_constant_LONG_MAX");
  147. CHECK_NON_NULL (test_long_constant_LONG_MAX);
  148. CHECK_VALUE (test_long_constant_LONG_MAX (), LONG_MAX);
  149. test_fn test_long_constant_LONG_MIN =
  150. (test_fn)gcc_jit_result_get_code (result,
  151. "test_long_constant_LONG_MIN");
  152. CHECK_NON_NULL (test_long_constant_LONG_MIN);
  153. CHECK_VALUE (test_long_constant_LONG_MIN (), LONG_MIN);
  154. }
  155. /**********************************************************************
  156. Tests of gcc_jit_context_new_rvalue_from_double.
  157. **********************************************************************/
  158. static const char *
  159. make_test_of_double_constant (gcc_jit_context *ctxt,
  160. gcc_jit_type *type,
  161. double value,
  162. const char *funcname)
  163. {
  164. /* Make a test function of the form:
  165. double funcname (void)
  166. {
  167. return VALUE;
  168. }
  169. and return a debug dump of VALUE so that
  170. the caller can sanity-check the debug dump implementation.
  171. */
  172. gcc_jit_rvalue *rvalue =
  173. gcc_jit_context_new_rvalue_from_double (ctxt, type, value);
  174. make_test_of_constant (ctxt, type, rvalue, funcname);
  175. return gcc_jit_object_get_debug_string (
  176. gcc_jit_rvalue_as_object (rvalue));
  177. }
  178. static void
  179. make_tests_of_double_constants (gcc_jit_context *ctxt)
  180. {
  181. gcc_jit_type *double_type =
  182. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE);
  183. make_test_of_double_constant (ctxt,
  184. double_type,
  185. 0.5,
  186. "test_double_constant_0_5");
  187. make_test_of_double_constant (ctxt,
  188. double_type,
  189. 1e100,
  190. "test_double_constant_1e100");
  191. make_test_of_double_constant (ctxt,
  192. double_type,
  193. DBL_MIN,
  194. "test_double_constant_DBL_MIN");
  195. make_test_of_double_constant (ctxt,
  196. double_type,
  197. DBL_MAX,
  198. "test_double_constant_DBL_MAX");
  199. }
  200. static void
  201. verify_double_constants (gcc_jit_result *result)
  202. {
  203. typedef double (*test_fn) (void);
  204. test_fn test_double_constant_0_5 =
  205. (test_fn)gcc_jit_result_get_code (result,
  206. "test_double_constant_0_5");
  207. CHECK_NON_NULL (test_double_constant_0_5);
  208. CHECK_VALUE (test_double_constant_0_5 (), 0.5);
  209. test_fn test_double_constant_1e100 =
  210. (test_fn)gcc_jit_result_get_code (result,
  211. "test_double_constant_1e100");
  212. CHECK_NON_NULL (test_double_constant_1e100);
  213. CHECK_VALUE (test_double_constant_1e100 (), 1e100);
  214. test_fn test_double_constant_DBL_MIN =
  215. (test_fn)gcc_jit_result_get_code (result,
  216. "test_double_constant_DBL_MIN");
  217. CHECK_NON_NULL (test_double_constant_DBL_MIN);
  218. CHECK_VALUE (test_double_constant_DBL_MIN (), DBL_MIN);
  219. test_fn test_double_constant_DBL_MAX =
  220. (test_fn)gcc_jit_result_get_code (result,
  221. "test_double_constant_DBL_MAX");
  222. CHECK_NON_NULL (test_double_constant_DBL_MAX);
  223. CHECK_VALUE (test_double_constant_DBL_MAX (), DBL_MAX);
  224. }
  225. /**********************************************************************
  226. Tests of gcc_jit_context_new_rvalue_from_ptr.
  227. **********************************************************************/
  228. static const char *
  229. make_test_of_ptr_constant (gcc_jit_context *ctxt,
  230. gcc_jit_type *type,
  231. void *value,
  232. const char *funcname)
  233. {
  234. /* Make a test function of the form:
  235. void *funcname (void)
  236. {
  237. return VALUE;
  238. }
  239. and return a debug dump of VALUE so that
  240. the caller can sanity-check the debug dump implementation.
  241. */
  242. gcc_jit_rvalue *rvalue =
  243. gcc_jit_context_new_rvalue_from_ptr (ctxt, type, value);
  244. make_test_of_constant (ctxt, type, rvalue, funcname);
  245. return gcc_jit_object_get_debug_string (
  246. gcc_jit_rvalue_as_object (rvalue));
  247. }
  248. static void
  249. make_tests_of_ptr_constants (gcc_jit_context *ctxt)
  250. {
  251. gcc_jit_type *ptr_type =
  252. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
  253. CHECK_STRING_VALUE (
  254. make_test_of_ptr_constant (ctxt,
  255. ptr_type,
  256. 0,
  257. "test_ptr_constant_0"),
  258. "(void *)NULL");
  259. CHECK_STRING_VALUE (
  260. make_test_of_ptr_constant (ctxt,
  261. ptr_type,
  262. (void *)0xdeadbeef,
  263. "test_ptr_constant_0xdeadbeef"),
  264. "(void *)0xdeadbeef");
  265. }
  266. static void
  267. verify_ptr_constants (gcc_jit_result *result)
  268. {
  269. typedef void *(*test_fn) (void);
  270. test_fn test_ptr_constant_0 =
  271. (test_fn)gcc_jit_result_get_code (result,
  272. "test_ptr_constant_0");
  273. CHECK_NON_NULL (test_ptr_constant_0);
  274. CHECK_VALUE (test_ptr_constant_0 (), 0);
  275. test_fn test_ptr_constant_0xdeadbeef =
  276. (test_fn)gcc_jit_result_get_code (result,
  277. "test_ptr_constant_0xdeadbeef");
  278. CHECK_NON_NULL (test_ptr_constant_0xdeadbeef);
  279. CHECK_VALUE (test_ptr_constant_0xdeadbeef (), (void *)0xdeadbeef);
  280. }
  281. /**********************************************************************
  282. Code for harness
  283. **********************************************************************/
  284. void
  285. create_code (gcc_jit_context *ctxt, void *user_data)
  286. {
  287. make_tests_of_int_constants (ctxt);
  288. make_tests_of_long_constants (ctxt);
  289. make_tests_of_double_constants (ctxt);
  290. make_tests_of_ptr_constants (ctxt);
  291. }
  292. void
  293. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  294. {
  295. CHECK_NON_NULL (result);
  296. verify_int_constants (result);
  297. verify_long_constants (result);
  298. verify_double_constants (result);
  299. verify_ptr_constants (result);
  300. }