Debian Codegraph @

testsuite/libffi.closures/cls_9byte2.c

raw
1 /* Area: ffi_call, closure_call
2 Purpose: Check structure passing with different structure size.
3 Depending on the ABI. Darwin/AIX do double-word
4 alignment of the struct if the first element is a double.
5 Check that it does here.
6 Limitations: none.
7 PR: none.
8 Originator: <[email protected]> 20030914 */
9
10 /* { dg-do run } */
11 #include "ffitest.h"
12
13 typedef struct cls_struct_9byte {
14 double a;
15 int b;
16 } cls_struct_9byte;
17
18 static cls_struct_9byte cls_struct_9byte_fn(struct cls_struct_9byte b1,
19 struct cls_struct_9byte b2)
20 {
21 struct cls_struct_9byte result;
22
23 result.a = b1.a + b2.a;
24 result.b = b1.b + b2.b;
25
26 printf("%g %d %g %d: %g %d\n", b1.a, b1.b, b2.a, b2.b,
27 result.a, result.b);
28
29 CHECK(b1.a == 7);
30 CHECK(b1.b == 8);
31
32 CHECK(b2.a == 1);
33 CHECK(b2.b == 9);
34
35 CHECK(result.a == 8);
36 CHECK(result.b == 17);
37
38 return result;
39 }
40
41 static void cls_struct_9byte_gn(ffi_cif* cif __UNUSED__, void* resp,
42 void** args, void* userdata __UNUSED__)
43 {
44 struct cls_struct_9byte b1, b2;
45
46 b1 = *(struct cls_struct_9byte*)(args[0]);
47 b2 = *(struct cls_struct_9byte*)(args[1]);
48
49 *(cls_struct_9byte*)resp = cls_struct_9byte_fn(b1, b2);
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[3];
58 ffi_type* cls_struct_fields[3];
59 ffi_type cls_struct_type;
60 ffi_type* dbl_arg_types[3];
61
62 struct cls_struct_9byte h_dbl = { 7.0, 8};
63 struct cls_struct_9byte j_dbl = { 1.0, 9};
64 struct cls_struct_9byte 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_double;
72 cls_struct_fields[1] = &ffi_type_sint;
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] = &h_dbl;
83 args_dbl[1] = &j_dbl;
84 args_dbl[2] = NULL;
85
86 ffi_call(&cif, FFI_FN(cls_struct_9byte_fn), &res_dbl, args_dbl);
87 /* { dg-output "7 8 1 9: 8 17" } */
88 printf("res: %g %d\n", res_dbl.a, res_dbl.b);
89 /* { dg-output "\nres: 8 17" } */
90 CHECK(res_dbl.a == 8);
91 CHECK(res_dbl.b == 17);
92
93 CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_9byte_gn, NULL, code) == FFI_OK);
94
95 res_dbl = ((cls_struct_9byte(*)(cls_struct_9byte, cls_struct_9byte))(code))(h_dbl, j_dbl);
96 /* { dg-output "\n7 8 1 9: 8 17" } */
97 printf("res: %g %d\n", res_dbl.a, res_dbl.b);
98 /* { dg-output "\nres: 8 17" } */
99 CHECK(res_dbl.a == 8);
100 CHECK(res_dbl.b == 17);
101
102 exit(0);
103 }