Debian Codegraph @

testsuite/libffi.closures/nested_struct10.c

raw
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: none.
7 Originator: <[email protected]> 20051010 */
8
9 /* { dg-do run } */
10 #include "ffitest.h"
11
12 typedef struct A {
13 unsigned long long a;
14 unsigned char b;
15 } A;
16
17 typedef struct B {
18 unsigned char y;
19 struct A x;
20 unsigned int z;
21 } B;
22
23 typedef struct C {
24 unsigned long long d;
25 unsigned char e;
26 } C;
27
28 static B B_fn(struct A b2, struct B b3, struct C b4)
29 {
30 struct B result;
31
32 result.x.a = b2.a + b3.x.a + b3.z + b4.d;
33 result.x.b = b2.b + b3.x.b + b3.y + b4.e;
34 result.y = b2.b + b3.x.b + b4.e;
35 result.z = 0;
36
37 printf("%d %d %d %d %d %d %d %d: %d %d %d\n", (int)b2.a, b2.b,
38 (int)b3.x.a, b3.x.b, b3.y, b3.z, (int)b4.d, b4.e,
39 (int)result.x.a, result.x.b, result.y);
40
41 CHECK((int)b2.a == 1);
42 CHECK(b2.b == 7);
43 CHECK((int)b3.x.a == 12);
44 CHECK(b3.x.b == 127);
45 CHECK(b3.y == 99);
46 CHECK(b3.z == 255);
47 CHECK((int)b4.d == 2);
48 CHECK(b4.e == 9);
49 CHECK((int)result.x.a == 270);
50 CHECK(result.x.b == 242);
51 CHECK(result.y == 143);
52
53 return result;
54 }
55
56 static void
57 B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
58 void* userdata __UNUSED__)
59 {
60 struct A b0;
61 struct B b1;
62 struct C b2;
63
64 b0 = *(struct A*)(args[0]);
65 b1 = *(struct B*)(args[1]);
66 b2 = *(struct C*)(args[2]);
67
68 *(B*)resp = B_fn(b0, b1, b2);
69 }
70
71 int main (void)
72 {
73 ffi_cif cif;
74 void *code;
75 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
76 void* args_dbl[4];
77 ffi_type* cls_struct_fields[3];
78 ffi_type* cls_struct_fields1[4];
79 ffi_type* cls_struct_fields2[3];
80 ffi_type cls_struct_type, cls_struct_type1, cls_struct_type2;
81 ffi_type* dbl_arg_types[4];
82
83 struct A e_dbl = { 1LL, 7};
84 struct B f_dbl = { 99, {12LL , 127}, 255};
85 struct C g_dbl = { 2LL, 9};
86
87 struct B res_dbl;
88
89 cls_struct_type.size = 0;
90 cls_struct_type.alignment = 0;
91 cls_struct_type.type = FFI_TYPE_STRUCT;
92 cls_struct_type.elements = cls_struct_fields;
93
94 cls_struct_type1.size = 0;
95 cls_struct_type1.alignment = 0;
96 cls_struct_type1.type = FFI_TYPE_STRUCT;
97 cls_struct_type1.elements = cls_struct_fields1;
98
99 cls_struct_type2.size = 0;
100 cls_struct_type2.alignment = 0;
101 cls_struct_type2.type = FFI_TYPE_STRUCT;
102 cls_struct_type2.elements = cls_struct_fields2;
103
104 cls_struct_fields[0] = &ffi_type_uint64;
105 cls_struct_fields[1] = &ffi_type_uchar;
106 cls_struct_fields[2] = NULL;
107
108 cls_struct_fields1[0] = &ffi_type_uchar;
109 cls_struct_fields1[1] = &cls_struct_type;
110 cls_struct_fields1[2] = &ffi_type_uint;
111 cls_struct_fields1[3] = NULL;
112
113 cls_struct_fields2[0] = &ffi_type_uint64;
114 cls_struct_fields2[1] = &ffi_type_uchar;
115 cls_struct_fields2[2] = NULL;
116
117
118 dbl_arg_types[0] = &cls_struct_type;
119 dbl_arg_types[1] = &cls_struct_type1;
120 dbl_arg_types[2] = &cls_struct_type2;
121 dbl_arg_types[3] = NULL;
122
123 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 3, &cls_struct_type1,
124 dbl_arg_types) == FFI_OK);
125
126 args_dbl[0] = &e_dbl;
127 args_dbl[1] = &f_dbl;
128 args_dbl[2] = &g_dbl;
129 args_dbl[3] = NULL;
130
131 ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
132 /* { dg-output "1 7 12 127 99 255 2 9: 270 242 143" } */
133 CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a + f_dbl.z + g_dbl.d));
134 CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y + g_dbl.e));
135 CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b + g_dbl.e));
136
137 CHECK(ffi_prep_closure_loc(pcl, &cif, B_gn, NULL, code) == FFI_OK);
138
139 res_dbl = ((B(*)(A, B, C))(code))(e_dbl, f_dbl, g_dbl);
140 /* { dg-output "\n1 7 12 127 99 255 2 9: 270 242 143" } */
141 CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a + f_dbl.z + g_dbl.d));
142 CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y + g_dbl.e));
143 CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b + g_dbl.e));
144
145 exit(0);
146 }