Debian Codegraph @

testsuite/libffi.closures/cls_4byte.c

raw
1 /* Area: ffi_call, closure_call
2 Purpose: Check structure passing with different structure size.
3 Depending on the ABI. Check overlapping.
4 Limitations: none.
5 PR: none.
6 Originator: <[email protected]> 20030828 */
7
8 /* { dg-do run } */
9
10 #include "ffitest.h"
11
12 typedef struct cls_struct_4byte {
13 unsigned short a;
14 unsigned short b;
15 } cls_struct_4byte;
16
17 cls_struct_4byte cls_struct_4byte_fn(struct cls_struct_4byte a1,
18 struct cls_struct_4byte a2)
19 {
20 struct cls_struct_4byte result;
21
22 result.a = a1.a + a2.a;
23 result.b = a1.b + a2.b;
24
25 printf("%d %d %d %d: %d %d\n", a1.a, a1.b, a2.a, a2.b, result.a, result.b);
26
27 CHECK(a1.a == 127);
28 CHECK(a1.b == 120);
29
30 CHECK(a2.a == 12);
31 CHECK(a2.b == 128);
32
33 CHECK(result.a == 139);
34 CHECK(result.b == 248);
35
36 return result;
37 }
38
39 static void
40 cls_struct_4byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
41 void* userdata __UNUSED__)
42 {
43
44 struct cls_struct_4byte a1, a2;
45
46 a1 = *(struct cls_struct_4byte*)(args[0]);
47 a2 = *(struct cls_struct_4byte*)(args[1]);
48
49 *(cls_struct_4byte*)resp = cls_struct_4byte_fn(a1, a2);
50 }
51
52 int main (void)
53 {
54 ffi_cif cif;
55 void *code;
56 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
57 void* args_dbl[5];
58 ffi_type* cls_struct_fields[4];
59 ffi_type cls_struct_type;
60 ffi_type* dbl_arg_types[5];
61
62 struct cls_struct_4byte g_dbl = { 127, 120 };
63 struct cls_struct_4byte f_dbl = { 12, 128 };
64 struct cls_struct_4byte res_dbl;
65
66 cls_struct_type.size = 0;
67 cls_struct_type.alignment = 0;
68 cls_struct_type.type = FFI_TYPE_STRUCT;
69 cls_struct_type.elements = cls_struct_fields;
70
71 cls_struct_fields[0] = &ffi_type_ushort;
72 cls_struct_fields[1] = &ffi_type_ushort;
73 cls_struct_fields[2] = NULL;
74
75 dbl_arg_types[0] = &cls_struct_type;
76 dbl_arg_types[1] = &cls_struct_type;
77 dbl_arg_types[2] = NULL;
78
79 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
80 dbl_arg_types) == FFI_OK);
81
82 args_dbl[0] = &g_dbl;
83 args_dbl[1] = &f_dbl;
84 args_dbl[2] = NULL;
85
86 ffi_call(&cif, FFI_FN(cls_struct_4byte_fn), &res_dbl, args_dbl);
87 /* { dg-output "127 120 12 128: 139 248" } */
88 printf("res: %d %d\n", res_dbl.a, res_dbl.b);
89 /* { dg-output "\nres: 139 248" } */
90 CHECK(res_dbl.a == 139);
91 CHECK(res_dbl.b == 248);
92
93 CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_4byte_gn, NULL, code) == FFI_OK);
94
95 res_dbl = ((cls_struct_4byte(*)(cls_struct_4byte, cls_struct_4byte))(code))(g_dbl, f_dbl);
96 /* { dg-output "\n127 120 12 128: 139 248" } */
97 printf("res: %d %d\n", res_dbl.a, res_dbl.b);
98 /* { dg-output "\nres: 139 248" } */
99 CHECK(res_dbl.a == 139);
100 CHECK(res_dbl.b == 248);
101
102 exit(0);
103 }