Debian Codegraph @

testsuite/libffi.closures/cls_16byte.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 #include "ffitest.h"
10
11 typedef struct cls_struct_16byte {
12 int a;
13 double b;
14 int c;
15 } cls_struct_16byte;
16
17 cls_struct_16byte cls_struct_16byte_fn(struct cls_struct_16byte b1,
18 struct cls_struct_16byte b2)
19 {
20 struct cls_struct_16byte result;
21
22 result.a = b1.a + b2.a;
23 result.b = b1.b + b2.b;
24 result.c = b1.c + b2.c;
25
26 printf("%d %g %d %d %g %d: %d %g %d\n", b1.a, b1.b, b1.c, b2.a, b2.b, b2.c,
27 result.a, result.b, result.c);
28
29 CHECK(b1.a == 7);
30 CHECK(b1.b == 8);
31 CHECK(b1.c == 9);
32
33 CHECK(b2.a == 1);
34 CHECK(b2.b == 9);
35 CHECK(b2.c == 3);
36
37 CHECK(result.a == 8);
38 CHECK(result.b == 17);
39 CHECK(result.c == 12);
40
41
42 return result;
43 }
44
45 static void cls_struct_16byte_gn(ffi_cif* cif __UNUSED__, void* resp,
46 void** args, void* userdata __UNUSED__)
47 {
48 struct cls_struct_16byte b1, b2;
49
50 b1 = *(struct cls_struct_16byte*)(args[0]);
51 b2 = *(struct cls_struct_16byte*)(args[1]);
52
53 *(cls_struct_16byte*)resp = cls_struct_16byte_fn(b1, b2);
54 }
55
56 int main (void)
57 {
58 ffi_cif cif;
59 void *code;
60 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
61 void* args_dbl[5];
62 ffi_type* cls_struct_fields[4];
63 ffi_type cls_struct_type;
64 ffi_type* dbl_arg_types[5];
65
66 struct cls_struct_16byte h_dbl = { 7, 8.0, 9 };
67 struct cls_struct_16byte j_dbl = { 1, 9.0, 3 };
68 struct cls_struct_16byte res_dbl;
69
70 cls_struct_type.size = 0;
71 cls_struct_type.alignment = 0;
72 cls_struct_type.type = FFI_TYPE_STRUCT;
73 cls_struct_type.elements = cls_struct_fields;
74
75 cls_struct_fields[0] = &ffi_type_sint;
76 cls_struct_fields[1] = &ffi_type_double;
77 cls_struct_fields[2] = &ffi_type_sint;
78 cls_struct_fields[3] = NULL;
79
80 dbl_arg_types[0] = &cls_struct_type;
81 dbl_arg_types[1] = &cls_struct_type;
82 dbl_arg_types[2] = NULL;
83
84 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
85 dbl_arg_types) == FFI_OK);
86
87 args_dbl[0] = &h_dbl;
88 args_dbl[1] = &j_dbl;
89 args_dbl[2] = NULL;
90
91 ffi_call(&cif, FFI_FN(cls_struct_16byte_fn), &res_dbl, args_dbl);
92 /* { dg-output "7 8 9 1 9 3: 8 17 12" } */
93 printf("res: %d %g %d\n", res_dbl.a, res_dbl.b, res_dbl.c);
94 /* { dg-output "\nres: 8 17 12" } */
95
96 CHECK(res_dbl.a == 8);
97 CHECK(res_dbl.b == 17);
98 CHECK(res_dbl.c == 12);
99
100 res_dbl.a = 0;
101 res_dbl.b = 0.0;
102 res_dbl.c = 0;
103
104 CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_16byte_gn, NULL, code) == FFI_OK);
105
106 res_dbl = ((cls_struct_16byte(*)(cls_struct_16byte, cls_struct_16byte))(code))(h_dbl, j_dbl);
107 /* { dg-output "\n7 8 9 1 9 3: 8 17 12" } */
108 printf("res: %d %g %d\n", res_dbl.a, res_dbl.b, res_dbl.c);
109 /* { dg-output "\nres: 8 17 12" } */
110
111 CHECK(res_dbl.a == 8);
112 CHECK(res_dbl.b == 17);
113 CHECK(res_dbl.c == 12);
114
115
116 exit(0);
117 }