| 1 |
#include <stdlib.h> |
| 2 |
#include <stdio.h> |
| 3 |
|
| 4 |
#include "libgccjit.h" |
| 5 |
|
| 6 |
#include "harness.h" |
| 7 |
|
| 8 |
void |
| 9 |
create_code (gcc_jit_context *ctxt, void *user_data) |
| 10 |
{ |
| 11 |
/* Let's try to inject the equivalent of: |
| 12 |
|
| 13 |
void |
| 14 |
test_calling_function_ptr (void (*fn_ptr) (int, int, int) fn_ptr, |
| 15 |
int a) |
| 16 |
{ |
| 17 |
fn_ptr (a * 3, a * 4, a * 5); |
| 18 |
} |
| 19 |
*/ |
| 20 |
|
| 21 |
int i; |
| 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); |
| 26 |
|
| 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; |
| 32 |
|
| 33 |
gcc_jit_type *fn_ptr_type = |
| 34 |
gcc_jit_context_new_function_ptr_type (ctxt, NULL, |
| 35 |
void_type, |
| 36 |
3, param_types, 0); |
| 37 |
|
| 38 |
/* Ensure that function ptr types have sane debug strings. */ |
| 39 |
|
| 40 |
CHECK_STRING_VALUE ( |
| 41 |
gcc_jit_object_get_debug_string (gcc_jit_type_as_object (fn_ptr_type)), |
| 42 |
"void (*) (int, int, int)"); |
| 43 |
|
| 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"); |
| 49 |
|
| 50 |
gcc_jit_param *params[2]; |
| 51 |
params[0] = param_fn_ptr; |
| 52 |
params[1] = param_a; |
| 53 |
|
| 54 |
gcc_jit_function *test_fn = |
| 55 |
gcc_jit_context_new_function (ctxt, NULL, |
| 56 |
GCC_JIT_FUNCTION_EXPORTED, |
| 57 |
void_type, |
| 58 |
"test_calling_function_ptr", |
| 59 |
2, params, |
| 60 |
0); |
| 61 |
/* "a * 3, a * 4, a * 5" */ |
| 62 |
gcc_jit_rvalue *args[3]; |
| 63 |
for (i = 0; i < 3; i++) |
| 64 |
args[i] = |
| 65 |
gcc_jit_context_new_binary_op ( |
| 66 |
ctxt, NULL, |
| 67 |
GCC_JIT_BINARY_OP_MULT, |
| 68 |
int_type, |
| 69 |
gcc_jit_param_as_rvalue (param_a), |
| 70 |
gcc_jit_context_new_rvalue_from_int ( |
| 71 |
ctxt, |
| 72 |
int_type, |
| 73 |
(i + 3) )); |
| 74 |
gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL); |
| 75 |
gcc_jit_block_add_eval ( |
| 76 |
block, NULL, |
| 77 |
gcc_jit_context_new_call_through_ptr ( |
| 78 |
ctxt, |
| 79 |
NULL, |
| 80 |
gcc_jit_param_as_rvalue (param_fn_ptr), |
| 81 |
3, args)); |
| 82 |
gcc_jit_block_end_with_void_return (block, NULL); |
| 83 |
} |
| 84 |
|
| 85 |
static int called_through_ptr_with[3]; |
| 86 |
|
| 87 |
static void |
| 88 |
function_called_through_fn_ptr (int i, int j, int k) |
| 89 |
{ |
| 90 |
called_through_ptr_with[0] = i; |
| 91 |
called_through_ptr_with[1] = j; |
| 92 |
called_through_ptr_with[2] = k; |
| 93 |
} |
| 94 |
|
| 95 |
void |
| 96 |
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) |
| 97 |
{ |
| 98 |
typedef void (*fn_type) (void (*fn_ptr) (int, int, int), |
| 99 |
int); |
| 100 |
CHECK_NON_NULL (result); |
| 101 |
|
| 102 |
fn_type test_caller = |
| 103 |
(fn_type)gcc_jit_result_get_code (result, "test_calling_function_ptr"); |
| 104 |
CHECK_NON_NULL (test_caller); |
| 105 |
|
| 106 |
called_through_ptr_with[0] = 0; |
| 107 |
called_through_ptr_with[1] = 0; |
| 108 |
called_through_ptr_with[2] = 0; |
| 109 |
|
| 110 |
/* Call the JIT-generated function. */ |
| 111 |
test_caller (function_called_through_fn_ptr, 5); |
| 112 |
|
| 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); |
| 117 |
} |
| 118 |
|