test-fibonacci.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "libgccjit.h"
  5. #include "harness.h"
  6. void
  7. create_code (gcc_jit_context *ctxt, void *user_data)
  8. {
  9. const int FIRST_LINE = __LINE__ + 4;
  10. /* Let's try to inject the equivalent of:
  11. 0000000001111111111222222222233333333334444444444555555555566666666667
  12. 1234567890123456789012345678901234567890123456789012345678901234567890
  13. FIRST_LINE + 0: int
  14. FIRST_LINE + 1: my_fibonacci (int x)
  15. FIRST_LINE + 2: {
  16. FIRST_LINE + 3: if (x < 2)
  17. FIRST_LINE + 4: return x;
  18. FIRST_LINE + 5: else
  19. FIRST_LINE + 6: return my_fibonacci (x - 1) + my_fibonacci (x - 2);
  20. FIRST_LINE + 7: }
  21. 0000000001111111111222222222233333333334444444444555555555566666666667
  22. 1234567890123456789012345678901234567890123456789012345678901234567890
  23. where the source locations are set up to point to the commented-out
  24. code above.
  25. It should therefore be possible to step through the generated code
  26. in the debugger, stepping through the above commented-out code
  27. fragement.
  28. */
  29. gcc_jit_type *the_type =
  30. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  31. gcc_jit_type *return_type = the_type;
  32. gcc_jit_param *x =
  33. gcc_jit_context_new_param (
  34. ctxt,
  35. gcc_jit_context_new_location (
  36. ctxt, __FILE__, FIRST_LINE + 1, 35),
  37. the_type, "x");
  38. gcc_jit_param *params[1] = {x};
  39. gcc_jit_function *func =
  40. gcc_jit_context_new_function (ctxt,
  41. gcc_jit_context_new_location (
  42. ctxt, __FILE__, FIRST_LINE, 17),
  43. GCC_JIT_FUNCTION_EXPORTED,
  44. return_type,
  45. "my_fibonacci",
  46. 1, params, 0);
  47. gcc_jit_block *initial =
  48. gcc_jit_function_new_block (func, "initial");
  49. gcc_jit_block *on_true =
  50. gcc_jit_function_new_block (func, "on_true");
  51. gcc_jit_block *on_false =
  52. gcc_jit_function_new_block (func, "on_false");
  53. /* if (x < 2) */
  54. gcc_jit_block_end_with_conditional (
  55. initial,
  56. gcc_jit_context_new_location (ctxt, __FILE__, FIRST_LINE + 3, 19),
  57. gcc_jit_context_new_comparison (
  58. ctxt,
  59. gcc_jit_context_new_location (ctxt, __FILE__, FIRST_LINE + 3, 25),
  60. GCC_JIT_COMPARISON_LT,
  61. gcc_jit_param_as_rvalue (x),
  62. gcc_jit_context_new_rvalue_from_int (
  63. ctxt,
  64. the_type,
  65. 2)),
  66. on_true,
  67. on_false);
  68. /* true branch: */
  69. /* return x */
  70. gcc_jit_block_end_with_return (
  71. on_true,
  72. gcc_jit_context_new_location (ctxt, __FILE__, FIRST_LINE + 4, 21),
  73. gcc_jit_param_as_rvalue (x));
  74. /* false branch: */
  75. gcc_jit_rvalue *x_minus_1 =
  76. gcc_jit_context_new_binary_op (
  77. ctxt,
  78. gcc_jit_context_new_location (ctxt, __FILE__, FIRST_LINE + 6, 44),
  79. GCC_JIT_BINARY_OP_MINUS, the_type,
  80. gcc_jit_param_as_rvalue (x),
  81. gcc_jit_context_new_rvalue_from_int (
  82. ctxt,
  83. the_type,
  84. 1));
  85. gcc_jit_rvalue *x_minus_2 =
  86. gcc_jit_context_new_binary_op (
  87. ctxt,
  88. gcc_jit_context_new_location (ctxt, __FILE__, FIRST_LINE + 6, 67),
  89. GCC_JIT_BINARY_OP_MINUS, the_type,
  90. gcc_jit_param_as_rvalue (x),
  91. gcc_jit_context_new_rvalue_from_int (
  92. ctxt,
  93. the_type,
  94. 2));
  95. gcc_jit_block_end_with_return (
  96. on_false,
  97. gcc_jit_context_new_location (ctxt, __FILE__, FIRST_LINE + 6, 21),
  98. gcc_jit_context_new_binary_op (
  99. ctxt,
  100. gcc_jit_context_new_location (ctxt, __FILE__, FIRST_LINE + 6, 49),
  101. GCC_JIT_BINARY_OP_PLUS, the_type,
  102. /* my_fibonacci (x - 1) */
  103. gcc_jit_context_new_call (
  104. ctxt,
  105. gcc_jit_context_new_location (ctxt, __FILE__, FIRST_LINE + 6, 28),
  106. func,
  107. 1, &x_minus_1),
  108. /* my_fibonacci (x - 2) */
  109. gcc_jit_context_new_call (
  110. ctxt,
  111. gcc_jit_context_new_location (ctxt, __FILE__, FIRST_LINE + 6, 51),
  112. func,
  113. 1, &x_minus_2)));
  114. }
  115. void
  116. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  117. {
  118. typedef int (*my_fibonacci_fn_type) (int);
  119. CHECK_NON_NULL (result);
  120. my_fibonacci_fn_type my_fibonacci =
  121. (my_fibonacci_fn_type)gcc_jit_result_get_code (result, "my_fibonacci");
  122. CHECK_NON_NULL (my_fibonacci);
  123. int val = my_fibonacci (10);
  124. note ("my_fibonacci returned: %d", val);
  125. CHECK_VALUE (val, 55);
  126. }