13 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 called_function (int i, int j, int k);
28 called_function (a * 3, a * 4, a * 5);
32 gcc_jit_type
*void_type
=
33 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_VOID
);
34 gcc_jit_type
*int_type
=
35 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
37 /* Declare the imported function. */
38 gcc_jit_param
*params
[3];
40 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "i");
42 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "j");
44 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "k");
45 gcc_jit_function
*called_fn
=
46 gcc_jit_context_new_function (ctxt
, NULL
,
47 GCC_JIT_FUNCTION_IMPORTED
,
53 /* Build the test_fn. */
54 gcc_jit_param
*param_a
=
55 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "a");
56 gcc_jit_function
*test_fn
=
57 gcc_jit_context_new_function (ctxt
, NULL
,
58 GCC_JIT_FUNCTION_EXPORTED
,
63 /* "a * 3, a * 4, a * 5" */
64 gcc_jit_rvalue
*args
[3];
65 for (i
= 0; i
< 3; i
++)
67 gcc_jit_context_new_binary_op (
69 GCC_JIT_BINARY_OP_MULT
,
71 gcc_jit_param_as_rvalue (param_a
),
72 gcc_jit_context_new_rvalue_from_int (
76 gcc_jit_block
*block
= gcc_jit_function_new_block (test_fn
, NULL
);
77 gcc_jit_block_add_eval (
79 gcc_jit_context_new_call (ctxt
,
83 gcc_jit_block_end_with_void_return (block
, NULL
);
86 static int called_with
[3];
89 called_function (int i
, int j
, int k
)
97 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
99 typedef void (*fn_type
) (int);
100 CHECK_NON_NULL (result
);
102 fn_type test_caller
=
103 (fn_type
)gcc_jit_result_get_code (result
, "test_caller");
104 CHECK_NON_NULL (test_caller
);
110 /* Call the JIT-generated function. */
113 /* Verify that it correctly called "called_function". */
114 CHECK_VALUE (called_with
[0], 15);
115 CHECK_VALUE (called_with
[1], 20);
116 CHECK_VALUE (called_with
[2], 25);