| 1 |
/* Area: ffi_call, closure_call |
| 2 |
Purpose: Check structure passing with different structure size. |
| 3 |
Depending on the ABI. Check overlapping. |
| 4 |
Limitations: none. |
| 5 |
PR: none. |
| 6 |
Originator: <[email protected]> 20050708 */ |
| 7 |
|
| 8 |
/* { dg-do run } */ |
| 9 |
#include "ffitest.h" |
| 10 |
|
| 11 |
typedef struct cls_struct_7byte { |
| 12 |
unsigned char a; |
| 13 |
unsigned char b; |
| 14 |
unsigned char c; |
| 15 |
unsigned char d; |
| 16 |
unsigned char e; |
| 17 |
unsigned char f; |
| 18 |
unsigned char g; |
| 19 |
} cls_struct_7byte; |
| 20 |
|
| 21 |
static cls_struct_7byte cls_struct_7byte_fn(struct cls_struct_7byte a1, |
| 22 |
struct cls_struct_7byte a2) |
| 23 |
{ |
| 24 |
struct cls_struct_7byte result; |
| 25 |
|
| 26 |
result.a = a1.a + a2.a; |
| 27 |
result.b = a1.b + a2.b; |
| 28 |
result.c = a1.c + a2.c; |
| 29 |
result.d = a1.d + a2.d; |
| 30 |
result.e = a1.e + a2.e; |
| 31 |
result.f = a1.f + a2.f; |
| 32 |
result.g = a1.g + a2.g; |
| 33 |
|
| 34 |
printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d: %d %d %d %d %d %d %d\n", |
| 35 |
a1.a, a1.b, a1.c, a1.d, a1.e, a1.f, a1.g, |
| 36 |
a2.a, a2.b, a2.c, a2.d, a2.e, a2.f, a2.g, |
| 37 |
result.a, result.b, result.c, result.d, result.e, result.f, result.g); |
| 38 |
|
| 39 |
CHECK(a1.a == 127); |
| 40 |
CHECK(a1.b == 120); |
| 41 |
CHECK(a1.c == 1); |
| 42 |
CHECK(a1.d == 3); |
| 43 |
CHECK(a1.e == 4); |
| 44 |
CHECK(a1.f == 5); |
| 45 |
CHECK(a1.g == 6); |
| 46 |
|
| 47 |
CHECK(a2.a == 12); |
| 48 |
CHECK(a2.b == 128); |
| 49 |
CHECK(a2.c == 9); |
| 50 |
CHECK(a2.d == 3); |
| 51 |
CHECK(a2.e == 4); |
| 52 |
CHECK(a2.f == 5); |
| 53 |
CHECK(a2.g == 6); |
| 54 |
|
| 55 |
CHECK(result.a == 139); |
| 56 |
CHECK(result.b == 248); |
| 57 |
CHECK(result.c == 10); |
| 58 |
CHECK(result.d == 6); |
| 59 |
CHECK(result.e == 8); |
| 60 |
CHECK(result.f == 10); |
| 61 |
CHECK(result.g == 12); |
| 62 |
|
| 63 |
return result; |
| 64 |
} |
| 65 |
|
| 66 |
static void |
| 67 |
cls_struct_7byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args, |
| 68 |
void* userdata __UNUSED__) |
| 69 |
{ |
| 70 |
|
| 71 |
struct cls_struct_7byte a1, a2; |
| 72 |
|
| 73 |
a1 = *(struct cls_struct_7byte*)(args[0]); |
| 74 |
a2 = *(struct cls_struct_7byte*)(args[1]); |
| 75 |
|
| 76 |
*(cls_struct_7byte*)resp = cls_struct_7byte_fn(a1, a2); |
| 77 |
} |
| 78 |
|
| 79 |
int main (void) |
| 80 |
{ |
| 81 |
ffi_cif cif; |
| 82 |
void *code; |
| 83 |
ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code); |
| 84 |
void* args_dbl[5]; |
| 85 |
ffi_type* cls_struct_fields[8]; |
| 86 |
ffi_type cls_struct_type; |
| 87 |
ffi_type* dbl_arg_types[5]; |
| 88 |
|
| 89 |
struct cls_struct_7byte g_dbl = { 127, 120, 1, 3, 4, 5, 6 }; |
| 90 |
struct cls_struct_7byte f_dbl = { 12, 128, 9, 3, 4, 5, 6 }; |
| 91 |
struct cls_struct_7byte res_dbl = { 0, 0, 0, 0, 0, 0, 0 }; |
| 92 |
|
| 93 |
cls_struct_type.size = 0; |
| 94 |
cls_struct_type.alignment = 0; |
| 95 |
cls_struct_type.type = FFI_TYPE_STRUCT; |
| 96 |
cls_struct_type.elements = cls_struct_fields; |
| 97 |
|
| 98 |
cls_struct_fields[0] = &ffi_type_uchar; |
| 99 |
cls_struct_fields[1] = &ffi_type_uchar; |
| 100 |
cls_struct_fields[2] = &ffi_type_uchar; |
| 101 |
cls_struct_fields[3] = &ffi_type_uchar; |
| 102 |
cls_struct_fields[4] = &ffi_type_uchar; |
| 103 |
cls_struct_fields[5] = &ffi_type_uchar; |
| 104 |
cls_struct_fields[6] = &ffi_type_uchar; |
| 105 |
cls_struct_fields[7] = NULL; |
| 106 |
|
| 107 |
dbl_arg_types[0] = &cls_struct_type; |
| 108 |
dbl_arg_types[1] = &cls_struct_type; |
| 109 |
dbl_arg_types[2] = NULL; |
| 110 |
|
| 111 |
CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type, |
| 112 |
dbl_arg_types) == FFI_OK); |
| 113 |
|
| 114 |
args_dbl[0] = &g_dbl; |
| 115 |
args_dbl[1] = &f_dbl; |
| 116 |
args_dbl[2] = NULL; |
| 117 |
|
| 118 |
ffi_call(&cif, FFI_FN(cls_struct_7byte_fn), &res_dbl, args_dbl); |
| 119 |
/* { dg-output "127 120 1 3 4 5 6 12 128 9 3 4 5 6: 139 248 10 6 8 10 12" } */ |
| 120 |
printf("res: %d %d %d %d %d %d %d\n", res_dbl.a, res_dbl.b, res_dbl.c, |
| 121 |
res_dbl.d, res_dbl.e, res_dbl.f, res_dbl.g); |
| 122 |
/* { dg-output "\nres: 139 248 10 6 8 10 12" } */ |
| 123 |
CHECK(res_dbl.a == 139); |
| 124 |
CHECK(res_dbl.b == 248); |
| 125 |
CHECK(res_dbl.c == 10); |
| 126 |
CHECK(res_dbl.d == 6); |
| 127 |
CHECK(res_dbl.e == 8); |
| 128 |
CHECK(res_dbl.f == 10); |
| 129 |
CHECK(res_dbl.g == 12); |
| 130 |
|
| 131 |
res_dbl.a = 0; |
| 132 |
res_dbl.b = 0; |
| 133 |
res_dbl.c = 0; |
| 134 |
res_dbl.d = 0; |
| 135 |
res_dbl.e = 0; |
| 136 |
res_dbl.f = 0; |
| 137 |
res_dbl.g = 0; |
| 138 |
|
| 139 |
CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_7byte_gn, NULL, code) == FFI_OK); |
| 140 |
|
| 141 |
res_dbl = ((cls_struct_7byte(*)(cls_struct_7byte, cls_struct_7byte))(code))(g_dbl, f_dbl); |
| 142 |
/* { dg-output "\n127 120 1 3 4 5 6 12 128 9 3 4 5 6: 139 248 10 6 8 10 12" } */ |
| 143 |
printf("res: %d %d %d %d %d %d %d\n", res_dbl.a, res_dbl.b, res_dbl.c, |
| 144 |
res_dbl.d, res_dbl.e, res_dbl.f, res_dbl.g); |
| 145 |
/* { dg-output "\nres: 139 248 10 6 8 10 12" } */ |
| 146 |
CHECK(res_dbl.a == 139); |
| 147 |
CHECK(res_dbl.b == 248); |
| 148 |
CHECK(res_dbl.c == 10); |
| 149 |
CHECK(res_dbl.d == 6); |
| 150 |
CHECK(res_dbl.e == 8); |
| 151 |
CHECK(res_dbl.f == 10); |
| 152 |
CHECK(res_dbl.g == 12); |
| 153 |
|
| 154 |
exit(0); |
| 155 |
} |