test-switch.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "libgccjit.h"
  5. #include "harness.h"
  6. /* Quote from here in docs/topics/functions.rst. */
  7. void
  8. create_code (gcc_jit_context *ctxt, void *user_data)
  9. {
  10. /* Let's try to inject the equivalent of:
  11. int
  12. test_switch (int x)
  13. {
  14. switch (x)
  15. {
  16. case 0 ... 5:
  17. return 3;
  18. case 25 ... 27:
  19. return 4;
  20. case -42 ... -17:
  21. return 83;
  22. case 40:
  23. return 8;
  24. default:
  25. return 10;
  26. }
  27. }
  28. */
  29. gcc_jit_type *t_int =
  30. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  31. gcc_jit_type *return_type = t_int;
  32. gcc_jit_param *x =
  33. gcc_jit_context_new_param (ctxt, NULL, t_int, "x");
  34. gcc_jit_param *params[1] = {x};
  35. gcc_jit_function *func =
  36. gcc_jit_context_new_function (ctxt, NULL,
  37. GCC_JIT_FUNCTION_EXPORTED,
  38. return_type,
  39. "test_switch",
  40. 1, params, 0);
  41. gcc_jit_block *b_initial =
  42. gcc_jit_function_new_block (func, "initial");
  43. gcc_jit_block *b_default =
  44. gcc_jit_function_new_block (func, "default");
  45. gcc_jit_block *b_case_0_5 =
  46. gcc_jit_function_new_block (func, "case_0_5");
  47. gcc_jit_block *b_case_25_27 =
  48. gcc_jit_function_new_block (func, "case_25_27");
  49. gcc_jit_block *b_case_m42_m17 =
  50. gcc_jit_function_new_block (func, "case_m42_m17");
  51. gcc_jit_block *b_case_40 =
  52. gcc_jit_function_new_block (func, "case_40");
  53. gcc_jit_case *cases[4] = {
  54. gcc_jit_context_new_case (
  55. ctxt,
  56. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 0),
  57. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 5),
  58. b_case_0_5),
  59. gcc_jit_context_new_case (
  60. ctxt,
  61. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 25),
  62. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 27),
  63. b_case_25_27),
  64. gcc_jit_context_new_case (
  65. ctxt,
  66. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, -42),
  67. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, -17),
  68. b_case_m42_m17),
  69. gcc_jit_context_new_case (
  70. ctxt,
  71. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 40),
  72. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 40),
  73. b_case_40)
  74. };
  75. gcc_jit_block_end_with_switch (
  76. b_initial, NULL,
  77. gcc_jit_param_as_rvalue (x),
  78. b_default,
  79. 4, cases);
  80. gcc_jit_block_end_with_return (
  81. b_case_0_5, NULL,
  82. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 3));
  83. gcc_jit_block_end_with_return (
  84. b_case_25_27, NULL,
  85. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 4));
  86. gcc_jit_block_end_with_return (
  87. b_case_m42_m17, NULL,
  88. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 83));
  89. gcc_jit_block_end_with_return (
  90. b_case_40, NULL,
  91. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 8));
  92. gcc_jit_block_end_with_return (
  93. b_default, NULL,
  94. gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 10));
  95. }
  96. /* Quote up to here in docs/topics/functions.rst. */
  97. static int
  98. c_test_switch (int x)
  99. {
  100. switch (x)
  101. {
  102. case 0 ... 5:
  103. return 3;
  104. case 25 ... 27:
  105. return 4;
  106. case -42 ... -17:
  107. return 83;
  108. case 40:
  109. return 8;
  110. default:
  111. return 10;
  112. }
  113. }
  114. void
  115. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  116. {
  117. typedef int (*test_switch_type) (int);
  118. CHECK_NON_NULL (result);
  119. test_switch_type test_switch =
  120. (test_switch_type)gcc_jit_result_get_code (result, "test_switch");
  121. CHECK_NON_NULL (test_switch);
  122. int i;
  123. for (i = -255; i < 255; i++)
  124. {
  125. int val = test_switch (i);
  126. int exp = c_test_switch (i);
  127. if (val != exp)
  128. fail ("test_switch (%i) returned: %i; expected; %i", i, val, exp);
  129. }
  130. }