Debian Codegraph @

testsuite/libffi.closures/cls_19byte.c

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