13 internally_called_function (int i
, int j
, int k
);
20 create_code (gcc_jit_context
*ctxt
, void *user_data
)
22 /* Let's try to inject the equivalent of:
23 extern void internally_called_function (int i, int j, int k);
26 internal_test_caller (int a)
28 internally_called_function (a * 3, a * 4, a * 5);
31 typedef void (*fn_ptr_type) (int);
34 get_test_caller (void)
36 // Verify that we can assign function pointers to variables
38 p = internal_test_caller;
43 gcc_jit_type
*void_type
=
44 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_VOID
);
45 gcc_jit_type
*int_type
=
46 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
48 /* Declare the imported function. */
49 gcc_jit_param
*params
[3];
51 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "i");
53 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "j");
55 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "k");
56 gcc_jit_function
*called_fn
=
57 gcc_jit_context_new_function (ctxt
, NULL
,
58 GCC_JIT_FUNCTION_IMPORTED
,
60 "internally_called_function",
64 /* Build the test_caller fn. */
65 gcc_jit_param
*param_a
=
66 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "a");
67 gcc_jit_function
*test_caller
=
68 gcc_jit_context_new_function (ctxt
, NULL
,
69 GCC_JIT_FUNCTION_EXPORTED
,
71 "internal_test_caller",
74 /* "a * 3, a * 4, a * 5" */
75 gcc_jit_rvalue
*args
[3];
76 for (i
= 0; i
< 3; i
++)
78 = gcc_jit_context_new_binary_op
80 GCC_JIT_BINARY_OP_MULT
,
82 gcc_jit_param_as_rvalue (param_a
),
83 gcc_jit_context_new_rvalue_from_int (ctxt
,
86 gcc_jit_block
*block
= gcc_jit_function_new_block (test_caller
, NULL
);
87 gcc_jit_block_add_eval (
89 gcc_jit_context_new_call (ctxt
,
93 gcc_jit_block_end_with_void_return (block
, NULL
);
95 gcc_jit_type
*fn_ptr_type
96 = gcc_jit_context_new_function_ptr_type (ctxt
, NULL
,
101 /* Build the get_test_caller fn. */
102 gcc_jit_function
*get_test_caller
=
103 gcc_jit_context_new_function (ctxt
, NULL
,
104 GCC_JIT_FUNCTION_EXPORTED
,
109 block
= gcc_jit_function_new_block (get_test_caller
, NULL
);
112 gcc_jit_lvalue
*local_p
113 = gcc_jit_function_new_local (get_test_caller
, NULL
,
116 /* p = internal_test_caller; */
117 gcc_jit_block_add_assignment (block
, NULL
,
119 gcc_jit_function_get_address (test_caller
,
123 gcc_jit_block_end_with_return (block
, NULL
,
124 gcc_jit_lvalue_as_rvalue (local_p
));
127 static int called_with
[3];
130 internally_called_function (int i
, int j
, int k
)
138 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
140 typedef void (*test_caller_type
) (int);
141 typedef test_caller_type (*get_test_caller_type
) (void);
142 CHECK_NON_NULL (result
);
144 get_test_caller_type get_test_caller
=
145 (get_test_caller_type
)gcc_jit_result_get_code (result
, "get_test_caller");
146 CHECK_NON_NULL (get_test_caller
);
148 test_caller_type test_caller
= (test_caller_type
)get_test_caller ();
149 CHECK_NON_NULL (test_caller
);
155 /* Call the JIT-generated function. */
158 /* Verify that it correctly called "internally_called_function". */
159 CHECK_VALUE (called_with
[0], 15);
160 CHECK_VALUE (called_with
[1], 20);
161 CHECK_VALUE (called_with
[2], 25);