| 1 |
/* Area: ffi_call, closure_call |
| 2 |
Purpose: Check structure passing with different structure size. |
| 3 |
Contains structs as parameter of the struct itself. |
| 4 |
Sample taken from Alan Modras patch to src/prep_cif.c. |
| 5 |
Limitations: none. |
| 6 |
PR: PR 25630. |
| 7 |
Originator: <[email protected]> 20051010 */ |
| 8 |
|
| 9 |
/* { dg-do run } */ |
| 10 |
#include "ffitest.h" |
| 11 |
|
| 12 |
typedef struct A { |
| 13 |
double a; |
| 14 |
unsigned char b; |
| 15 |
} A; |
| 16 |
|
| 17 |
typedef struct B { |
| 18 |
struct A x; |
| 19 |
unsigned char y; |
| 20 |
} B; |
| 21 |
|
| 22 |
static B B_fn(struct A b2, struct B b3) |
| 23 |
{ |
| 24 |
struct B result; |
| 25 |
|
| 26 |
result.x.a = b2.a + b3.x.a; |
| 27 |
result.x.b = b2.b + b3.x.b + b3.y; |
| 28 |
result.y = b2.b + b3.x.b; |
| 29 |
|
| 30 |
printf("%d %d %d %d %d: %d %d %d\n", (int)b2.a, b2.b, |
| 31 |
(int)b3.x.a, b3.x.b, b3.y, |
| 32 |
(int)result.x.a, result.x.b, result.y); |
| 33 |
|
| 34 |
CHECK((int)b2.a == 1); |
| 35 |
CHECK(b2.b == 7); |
| 36 |
CHECK((int)b3.x.a == 12); |
| 37 |
CHECK(b3.x.b == 127); |
| 38 |
CHECK(b3.y == 99); |
| 39 |
CHECK((int)result.x.a == 13); |
| 40 |
CHECK(result.x.b == 233); |
| 41 |
CHECK(result.y == 134); |
| 42 |
|
| 43 |
return result; |
| 44 |
} |
| 45 |
|
| 46 |
static void |
| 47 |
B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args, |
| 48 |
void* userdata __UNUSED__) |
| 49 |
{ |
| 50 |
struct A b0; |
| 51 |
struct B b1; |
| 52 |
|
| 53 |
b0 = *(struct A*)(args[0]); |
| 54 |
b1 = *(struct B*)(args[1]); |
| 55 |
|
| 56 |
*(B*)resp = B_fn(b0, b1); |
| 57 |
} |
| 58 |
|
| 59 |
int main (void) |
| 60 |
{ |
| 61 |
ffi_cif cif; |
| 62 |
void *code; |
| 63 |
ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code); |
| 64 |
void* args_dbl[3]; |
| 65 |
ffi_type* cls_struct_fields[3]; |
| 66 |
ffi_type* cls_struct_fields1[3]; |
| 67 |
ffi_type cls_struct_type, cls_struct_type1; |
| 68 |
ffi_type* dbl_arg_types[3]; |
| 69 |
|
| 70 |
struct A e_dbl = { 1.0, 7}; |
| 71 |
struct B f_dbl = {{12.0 , 127}, 99}; |
| 72 |
|
| 73 |
struct B res_dbl; |
| 74 |
|
| 75 |
cls_struct_type.size = 0; |
| 76 |
cls_struct_type.alignment = 0; |
| 77 |
cls_struct_type.type = FFI_TYPE_STRUCT; |
| 78 |
cls_struct_type.elements = cls_struct_fields; |
| 79 |
|
| 80 |
cls_struct_type1.size = 0; |
| 81 |
cls_struct_type1.alignment = 0; |
| 82 |
cls_struct_type1.type = FFI_TYPE_STRUCT; |
| 83 |
cls_struct_type1.elements = cls_struct_fields1; |
| 84 |
|
| 85 |
cls_struct_fields[0] = &ffi_type_double; |
| 86 |
cls_struct_fields[1] = &ffi_type_uchar; |
| 87 |
cls_struct_fields[2] = NULL; |
| 88 |
|
| 89 |
cls_struct_fields1[0] = &cls_struct_type; |
| 90 |
cls_struct_fields1[1] = &ffi_type_uchar; |
| 91 |
cls_struct_fields1[2] = NULL; |
| 92 |
|
| 93 |
|
| 94 |
dbl_arg_types[0] = &cls_struct_type; |
| 95 |
dbl_arg_types[1] = &cls_struct_type1; |
| 96 |
dbl_arg_types[2] = NULL; |
| 97 |
|
| 98 |
CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type1, |
| 99 |
dbl_arg_types) == FFI_OK); |
| 100 |
|
| 101 |
args_dbl[0] = &e_dbl; |
| 102 |
args_dbl[1] = &f_dbl; |
| 103 |
args_dbl[2] = NULL; |
| 104 |
|
| 105 |
ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl); |
| 106 |
/* { dg-output "1 7 12 127 99: 13 233 134" } */ |
| 107 |
CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a)); |
| 108 |
CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y)); |
| 109 |
CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b)); |
| 110 |
|
| 111 |
CHECK(ffi_prep_closure_loc(pcl, &cif, B_gn, NULL, code) == FFI_OK); |
| 112 |
|
| 113 |
res_dbl = ((B(*)(A, B))(code))(e_dbl, f_dbl); |
| 114 |
/* { dg-output "\n1 7 12 127 99: 13 233 134" } */ |
| 115 |
CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a)); |
| 116 |
CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y)); |
| 117 |
CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b)); |
| 118 |
|
| 119 |
exit(0); |
| 120 |
} |