Debian Codegraph @

testsuite/libffi.closures/nested_struct.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 Limitations: none.
5 PR: none.
6 Originator: <[email protected]> 20030828 */
7
8 /* { dg-do run } */
9 #include "ffitest.h"
10
11 typedef struct cls_struct_16byte1 {
12 double a;
13 float b;
14 int c;
15 } cls_struct_16byte1;
16
17 typedef struct cls_struct_16byte2 {
18 int ii;
19 double dd;
20 float ff;
21 } cls_struct_16byte2;
22
23 typedef struct cls_struct_combined {
24 cls_struct_16byte1 d;
25 cls_struct_16byte2 e;
26 } cls_struct_combined;
27
28 static cls_struct_combined cls_struct_combined_fn(struct cls_struct_16byte1 b0,
29 struct cls_struct_16byte2 b1,
30 struct cls_struct_combined b2)
31 {
32 struct cls_struct_combined result;
33
34 result.d.a = b0.a + b1.dd + b2.d.a;
35 result.d.b = b0.b + b1.ff + b2.d.b;
36 result.d.c = b0.c + b1.ii + b2.d.c;
37 result.e.ii = b0.c + b1.ii + b2.e.ii;
38 result.e.dd = b0.a + b1.dd + b2.e.dd;
39 result.e.ff = b0.b + b1.ff + b2.e.ff;
40
41 printf("%g %g %d %d %g %g %g %g %d %d %g %g: %g %g %d %d %g %g\n",
42 b0.a, b0.b, b0.c,
43 b1.ii, b1.dd, b1.ff,
44 b2.d.a, b2.d.b, b2.d.c,
45 b2.e.ii, b2.e.dd, b2.e.ff,
46 result.d.a, result.d.b, result.d.c,
47 result.e.ii, result.e.dd, result.e.ff);
48
49 CHECK_DOUBLE_EQ(b0.a, 9);
50 CHECK_FLOAT_EQ(b0.b, 2);
51 CHECK(b0.c == 6);
52
53 CHECK(b1.ii == 1);
54 CHECK_DOUBLE_EQ(b1.dd, 2);
55 CHECK_FLOAT_EQ(b1.ff, 3);
56
57 CHECK_DOUBLE_EQ(b2.d.a, 4);
58 CHECK_FLOAT_EQ(b2.d.b, 5);
59 CHECK(b2.d.c == 6);
60
61 CHECK(b2.e.ii == 3);
62 CHECK_DOUBLE_EQ(b2.e.dd, 1);
63 CHECK_FLOAT_EQ(b2.e.ff, 8);
64
65 CHECK_DOUBLE_EQ(result.d.a, 15);
66 CHECK_FLOAT_EQ(result.d.b, 10);
67 CHECK(result.d.c == 13);
68 CHECK(result.e.ii == 10);
69 CHECK_DOUBLE_EQ(result.e.dd, 12);
70 CHECK_FLOAT_EQ(result.e.ff, 13);
71
72 return result;
73 }
74
75 static void
76 cls_struct_combined_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
77 void* userdata __UNUSED__)
78 {
79 struct cls_struct_16byte1 b0;
80 struct cls_struct_16byte2 b1;
81 struct cls_struct_combined b2;
82
83 b0 = *(struct cls_struct_16byte1*)(args[0]);
84 b1 = *(struct cls_struct_16byte2*)(args[1]);
85 b2 = *(struct cls_struct_combined*)(args[2]);
86
87
88 *(cls_struct_combined*)resp = cls_struct_combined_fn(b0, b1, b2);
89 }
90
91 int main (void)
92 {
93 ffi_cif cif;
94 void *code;
95 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
96 void* args_dbl[5];
97 ffi_type* cls_struct_fields[5];
98 ffi_type* cls_struct_fields1[5];
99 ffi_type* cls_struct_fields2[5];
100 ffi_type cls_struct_type, cls_struct_type1, cls_struct_type2;
101 ffi_type* dbl_arg_types[5];
102
103 struct cls_struct_16byte1 e_dbl = { 9.0, 2.0, 6};
104 struct cls_struct_16byte2 f_dbl = { 1, 2.0, 3.0};
105 struct cls_struct_combined g_dbl = {{4.0, 5.0, 6},
106 {3, 1.0, 8.0}};
107 struct cls_struct_combined res_dbl;
108
109 cls_struct_type.size = 0;
110 cls_struct_type.alignment = 0;
111 cls_struct_type.type = FFI_TYPE_STRUCT;
112 cls_struct_type.elements = cls_struct_fields;
113
114 cls_struct_type1.size = 0;
115 cls_struct_type1.alignment = 0;
116 cls_struct_type1.type = FFI_TYPE_STRUCT;
117 cls_struct_type1.elements = cls_struct_fields1;
118
119 cls_struct_type2.size = 0;
120 cls_struct_type2.alignment = 0;
121 cls_struct_type2.type = FFI_TYPE_STRUCT;
122 cls_struct_type2.elements = cls_struct_fields2;
123
124 cls_struct_fields[0] = &ffi_type_double;
125 cls_struct_fields[1] = &ffi_type_float;
126 cls_struct_fields[2] = &ffi_type_sint;
127 cls_struct_fields[3] = NULL;
128
129 cls_struct_fields1[0] = &ffi_type_sint;
130 cls_struct_fields1[1] = &ffi_type_double;
131 cls_struct_fields1[2] = &ffi_type_float;
132 cls_struct_fields1[3] = NULL;
133
134 cls_struct_fields2[0] = &cls_struct_type;
135 cls_struct_fields2[1] = &cls_struct_type1;
136 cls_struct_fields2[2] = NULL;
137
138
139 dbl_arg_types[0] = &cls_struct_type;
140 dbl_arg_types[1] = &cls_struct_type1;
141 dbl_arg_types[2] = &cls_struct_type2;
142 dbl_arg_types[3] = NULL;
143
144 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 3, &cls_struct_type2,
145 dbl_arg_types) == FFI_OK);
146
147 args_dbl[0] = &e_dbl;
148 args_dbl[1] = &f_dbl;
149 args_dbl[2] = &g_dbl;
150 args_dbl[3] = NULL;
151
152 ffi_call(&cif, FFI_FN(cls_struct_combined_fn), &res_dbl, args_dbl);
153 /* { dg-output "9 2 6 1 2 3 4 5 6 3 1 8: 15 10 13 10 12 13" } */
154 CHECK_DOUBLE_EQ( res_dbl.d.a, (e_dbl.a + f_dbl.dd + g_dbl.d.a));
155 CHECK_FLOAT_EQ( res_dbl.d.b, (e_dbl.b + f_dbl.ff + g_dbl.d.b));
156 CHECK( res_dbl.d.c == (e_dbl.c + f_dbl.ii + g_dbl.d.c));
157 CHECK( res_dbl.e.ii == (e_dbl.c + f_dbl.ii + g_dbl.e.ii));
158 CHECK_DOUBLE_EQ( res_dbl.e.dd, (e_dbl.a + f_dbl.dd + g_dbl.e.dd));
159 CHECK_FLOAT_EQ( res_dbl.e.ff, (e_dbl.b + f_dbl.ff + g_dbl.e.ff));
160
161 CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_combined_gn, NULL, code) == FFI_OK);
162
163 res_dbl = ((cls_struct_combined(*)(cls_struct_16byte1,
164 cls_struct_16byte2,
165 cls_struct_combined))
166 (code))(e_dbl, f_dbl, g_dbl);
167 /* { dg-output "\n9 2 6 1 2 3 4 5 6 3 1 8: 15 10 13 10 12 13" } */
168 CHECK_DOUBLE_EQ( res_dbl.d.a, (e_dbl.a + f_dbl.dd + g_dbl.d.a));
169 CHECK_FLOAT_EQ( res_dbl.d.b, (e_dbl.b + f_dbl.ff + g_dbl.d.b));
170 CHECK( res_dbl.d.c == (e_dbl.c + f_dbl.ii + g_dbl.d.c));
171 CHECK( res_dbl.e.ii == (e_dbl.c + f_dbl.ii + g_dbl.e.ii));
172 CHECK_DOUBLE_EQ( res_dbl.e.dd, (e_dbl.a + f_dbl.dd + g_dbl.e.dd));
173 CHECK_FLOAT_EQ( res_dbl.e.ff, (e_dbl.b + f_dbl.ff + g_dbl.e.ff));
174 exit(0);
175 }