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