1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 
5 #include "libgccjit.h"
6 
7 #include "harness.h"
8 
9 void
create_code(gcc_jit_context * ctxt,void * user_data)10 create_code (gcc_jit_context *ctxt, void *user_data)
11 {
12   /* Let's try to inject the equivalent of:
13 
14       int
15       my_factorial_must_tail_call (int x)
16       {
17         if (x < 2)
18           return x;
19         else
20           return x * my_factorial_must_tail_call (x - 1);
21       }
22 
23      and mark the call as requiring tail-call-optimization.
24    */
25   gcc_jit_type *the_type =
26     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
27   gcc_jit_type *return_type = the_type;
28 
29   gcc_jit_param *x =
30     gcc_jit_context_new_param (ctxt, NULL, the_type, "x");
31   gcc_jit_param *params[1] = {x};
32   gcc_jit_function *func =
33     gcc_jit_context_new_function (ctxt, NULL,
34 				  GCC_JIT_FUNCTION_EXPORTED,
35 				  return_type,
36 				  "my_factorial_must_tail_call",
37 				  1, params, 0);
38 
39   gcc_jit_block *initial =
40     gcc_jit_function_new_block (func, "initial");
41   gcc_jit_block *on_true =
42     gcc_jit_function_new_block (func, "on_true");
43   gcc_jit_block *on_false =
44     gcc_jit_function_new_block (func, "on_false");
45 
46  /* if (x < 2) */
47   gcc_jit_block_end_with_conditional (
48     initial, NULL,
49     gcc_jit_context_new_comparison (
50       ctxt, NULL,
51       GCC_JIT_COMPARISON_LT,
52       gcc_jit_param_as_rvalue (x),
53       gcc_jit_context_new_rvalue_from_int (
54 	ctxt,
55 	the_type,
56 	2)),
57     on_true,
58     on_false);
59 
60   /* true branch: */
61   /* return x */
62   gcc_jit_block_end_with_return (
63     on_true,
64     NULL,
65     gcc_jit_param_as_rvalue (x));
66 
67   /* false branch: */
68   gcc_jit_rvalue *x_minus_1 =
69     gcc_jit_context_new_binary_op (
70       ctxt, NULL,
71       GCC_JIT_BINARY_OP_MINUS, the_type,
72       gcc_jit_param_as_rvalue (x),
73       gcc_jit_context_new_rvalue_from_int (
74 	ctxt,
75 	the_type,
76 	1));
77   /* my_factorial_must_tail_call (x - 1) */
78   gcc_jit_rvalue *call =
79       gcc_jit_context_new_call (
80         ctxt, NULL,
81         func,
82         1, &x_minus_1);
83 
84   /* Mark the call as requiring tail-call optimization.  */
85   gcc_jit_rvalue_set_bool_require_tail_call (call, 1);
86 
87   gcc_jit_block_end_with_return (
88     on_false,
89     NULL,
90     gcc_jit_context_new_binary_op (
91       ctxt, NULL,
92       GCC_JIT_BINARY_OP_MULT, the_type,
93       gcc_jit_param_as_rvalue (x),
94       call));
95 }
96 
97 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)98 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
99 {
100   typedef int (*my_factorial_fn_type) (int);
101   CHECK_NON_NULL (result);
102   my_factorial_fn_type my_factorial_must_tail_call =
103     (my_factorial_fn_type)gcc_jit_result_get_code (result, "my_factorial_must_tail_call");
104   CHECK_NON_NULL (my_factorial_must_tail_call);
105   int val = my_factorial_must_tail_call (10);
106   note ("my_factorial_must_tail_call returned: %d", val);
107   CHECK_VALUE (val, 3628800);
108 }
109 
110