9 create_code (gcc_jit_context
*ctxt
, void *user_data
)
11 /* Let's try to inject the equivalent of:
14 test_calling_function_ptr (void (*fn_ptr) (int, int, int) fn_ptr,
17 fn_ptr (a * 3, a * 4, a * 5);
22 gcc_jit_type
*void_type
=
23 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_VOID
);
24 gcc_jit_type
*int_type
=
25 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
27 /* Build the function ptr type. */
28 gcc_jit_type
*param_types
[3];
29 param_types
[0] = int_type
;
30 param_types
[1] = int_type
;
31 param_types
[2] = int_type
;
33 gcc_jit_type
*fn_ptr_type
=
34 gcc_jit_context_new_function_ptr_type (ctxt
, NULL
,
38 /* Ensure that function ptr types have sane debug strings. */
41 gcc_jit_object_get_debug_string (gcc_jit_type_as_object (fn_ptr_type
)),
42 "void (*) (int, int, int)");
44 /* Build the test_fn. */
45 gcc_jit_param
*param_fn_ptr
=
46 gcc_jit_context_new_param (ctxt
, NULL
, fn_ptr_type
, "fn_ptr");
47 gcc_jit_param
*param_a
=
48 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "a");
50 gcc_jit_param
*params
[2];
51 params
[0] = param_fn_ptr
;
54 gcc_jit_function
*test_fn
=
55 gcc_jit_context_new_function (ctxt
, NULL
,
56 GCC_JIT_FUNCTION_EXPORTED
,
58 "test_calling_function_ptr",
61 /* "a * 3, a * 4, a * 5" */
62 gcc_jit_rvalue
*args
[3];
63 for (i
= 0; i
< 3; i
++)
65 gcc_jit_context_new_binary_op (
67 GCC_JIT_BINARY_OP_MULT
,
69 gcc_jit_param_as_rvalue (param_a
),
70 gcc_jit_context_new_rvalue_from_int (
74 gcc_jit_block
*block
= gcc_jit_function_new_block (test_fn
, NULL
);
75 gcc_jit_block_add_eval (
77 gcc_jit_context_new_call_through_ptr (
80 gcc_jit_param_as_rvalue (param_fn_ptr
),
82 gcc_jit_block_end_with_void_return (block
, NULL
);
85 static int called_through_ptr_with
[3];
88 function_called_through_fn_ptr (int i
, int j
, int k
)
90 called_through_ptr_with
[0] = i
;
91 called_through_ptr_with
[1] = j
;
92 called_through_ptr_with
[2] = k
;
96 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
98 typedef void (*fn_type
) (void (*fn_ptr
) (int, int, int),
100 CHECK_NON_NULL (result
);
102 fn_type test_caller
=
103 (fn_type
)gcc_jit_result_get_code (result
, "test_calling_function_ptr");
104 CHECK_NON_NULL (test_caller
);
106 called_through_ptr_with
[0] = 0;
107 called_through_ptr_with
[1] = 0;
108 called_through_ptr_with
[2] = 0;
110 /* Call the JIT-generated function. */
111 test_caller (function_called_through_fn_ptr
, 5);
113 /* Verify that it correctly called "function_called_through_fn_ptr". */
114 CHECK_VALUE (called_through_ptr_with
[0], 15);
115 CHECK_VALUE (called_through_ptr_with
[1], 20);
116 CHECK_VALUE (called_through_ptr_with
[2], 25);