10 create_code (gcc_jit_context
*ctxt
, void *user_data
)
12 const int FIRST_LINE
= __LINE__
+ 4;
13 /* Let's try to inject the equivalent of:
14 0000000001111111111222222222233333333334444444444555555555566666666667
15 1234567890123456789012345678901234567890123456789012345678901234567890
17 FIRST_LINE + 1: my_fibonacci (int x)
19 FIRST_LINE + 3: if (x < 2)
20 FIRST_LINE + 4: return x;
22 FIRST_LINE + 6: return my_fibonacci (x - 1) + my_fibonacci (x - 2);
24 0000000001111111111222222222233333333334444444444555555555566666666667
25 1234567890123456789012345678901234567890123456789012345678901234567890
27 where the source locations are set up to point to the commented-out
29 It should therefore be possible to step through the generated code
30 in the debugger, stepping through the above commented-out code
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
;
38 gcc_jit_context_new_param (
40 gcc_jit_context_new_location (
41 ctxt
, __FILE__
, FIRST_LINE
+ 1, 35),
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
,
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");
61 gcc_jit_block_end_with_conditional (
63 gcc_jit_context_new_location (ctxt
, __FILE__
, FIRST_LINE
+ 3, 19),
64 gcc_jit_context_new_comparison (
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 (
78 gcc_jit_block_end_with_return (
80 gcc_jit_context_new_location (ctxt
, __FILE__
, FIRST_LINE
+ 4, 21),
81 gcc_jit_param_as_rvalue (x
));
84 gcc_jit_rvalue
*x_minus_1
=
85 gcc_jit_context_new_binary_op (
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 (
94 gcc_jit_rvalue
*x_minus_2
=
95 gcc_jit_context_new_binary_op (
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 (
104 gcc_jit_block_end_with_return (
106 gcc_jit_context_new_location (ctxt
, __FILE__
, FIRST_LINE
+ 6, 21),
107 gcc_jit_context_new_binary_op (
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 (
114 gcc_jit_context_new_location (ctxt
, __FILE__
, FIRST_LINE
+ 6, 28),
117 /* my_fibonacci (x - 2) */
118 gcc_jit_context_new_call (
120 gcc_jit_context_new_location (ctxt
, __FILE__
, FIRST_LINE
+ 6, 51),
126 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
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);