Debian Codegraph @

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