Imported File#ftype spec from rubyspecs.
[rbx.git] / shotgun / external_libs / libffi / testsuite / libffi.call / nested_struct10.c
blob8d9aba2acdc75f17204a19ec71b4e8d752faf400
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: <andreast@gcc.gnu.org> 20051010 */
9 /* { dg-do run } */
10 #include "ffitest.h"
12 typedef struct A {
13 unsigned long long a;
14 unsigned char b;
15 } A;
17 typedef struct B {
18 unsigned char y;
19 struct A x;
20 unsigned int z;
21 } B;
23 typedef struct C {
24 unsigned long long d;
25 unsigned char e;
26 } C;
28 static B B_fn(struct A b2, struct B b3, struct C b4)
30 struct B result;
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;
36 printf("%d %d %d %d %d %d %d %d: %d %d %d\n", (int)b2.a, b2.b,
37 (int)b3.x.a, b3.x.b, b3.y, b3.z, (int)b4.d, b4.e,
38 (int)result.x.a, result.x.b, result.y);
40 return result;
43 static void
44 B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
45 void* userdata __UNUSED__)
47 struct A b0;
48 struct B b1;
49 struct C b2;
51 b0 = *(struct A*)(args[0]);
52 b1 = *(struct B*)(args[1]);
53 b2 = *(struct C*)(args[2]);
55 *(B*)resp = B_fn(b0, b1, b2);
58 int main (void)
60 ffi_cif cif;
61 #ifndef USING_MMAP
62 static ffi_closure cl;
63 #endif
64 ffi_closure *pcl;
65 void* args_dbl[4];
66 ffi_type* cls_struct_fields[3];
67 ffi_type* cls_struct_fields1[4];
68 ffi_type* cls_struct_fields2[3];
69 ffi_type cls_struct_type, cls_struct_type1, cls_struct_type2;
70 ffi_type* dbl_arg_types[4];
72 #ifdef USING_MMAP
73 pcl = allocate_mmap (sizeof(ffi_closure));
74 #else
75 pcl = &cl;
76 #endif
78 cls_struct_type.size = 0;
79 cls_struct_type.alignment = 0;
80 cls_struct_type.type = FFI_TYPE_STRUCT;
81 cls_struct_type.elements = cls_struct_fields;
83 cls_struct_type1.size = 0;
84 cls_struct_type1.alignment = 0;
85 cls_struct_type1.type = FFI_TYPE_STRUCT;
86 cls_struct_type1.elements = cls_struct_fields1;
88 cls_struct_type2.size = 0;
89 cls_struct_type2.alignment = 0;
90 cls_struct_type2.type = FFI_TYPE_STRUCT;
91 cls_struct_type2.elements = cls_struct_fields2;
93 struct A e_dbl = { 1LL, 7};
94 struct B f_dbl = { 99, {12LL , 127}, 255};
95 struct C g_dbl = { 2LL, 9};
97 struct B res_dbl;
99 cls_struct_fields[0] = &ffi_type_uint64;
100 cls_struct_fields[1] = &ffi_type_uchar;
101 cls_struct_fields[2] = NULL;
103 cls_struct_fields1[0] = &ffi_type_uchar;
104 cls_struct_fields1[1] = &cls_struct_type;
105 cls_struct_fields1[2] = &ffi_type_uint;
106 cls_struct_fields1[3] = NULL;
108 cls_struct_fields2[0] = &ffi_type_uint64;
109 cls_struct_fields2[1] = &ffi_type_uchar;
110 cls_struct_fields2[2] = NULL;
113 dbl_arg_types[0] = &cls_struct_type;
114 dbl_arg_types[1] = &cls_struct_type1;
115 dbl_arg_types[2] = &cls_struct_type2;
116 dbl_arg_types[3] = NULL;
118 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 3, &cls_struct_type1,
119 dbl_arg_types) == FFI_OK);
121 args_dbl[0] = &e_dbl;
122 args_dbl[1] = &f_dbl;
123 args_dbl[2] = &g_dbl;
124 args_dbl[3] = NULL;
126 ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
127 /* { dg-output "1 7 12 127 99 255 2 9: 270 242 143" } */
128 CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a + f_dbl.z + g_dbl.d));
129 CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y + g_dbl.e));
130 CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b + g_dbl.e));
132 CHECK(ffi_prep_closure(pcl, &cif, B_gn, NULL) == FFI_OK);
134 res_dbl = ((B(*)(A, B, C))(pcl))(e_dbl, f_dbl, g_dbl);
135 /* { dg-output "\n1 7 12 127 99 255 2 9: 270 242 143" } */
136 CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a + f_dbl.z + g_dbl.d));
137 CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y + g_dbl.e));
138 CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b + g_dbl.e));
140 exit(0);