Debian Codegraph @

testsuite/libffi.closures/nested_struct13.c

raw
1 /* Area: ffi_call, closure_call
2 Purpose: Check structure passing.
3 Limitations: none.
4 PR: none.
5 Originator: <[email protected]> and <[email protected]> 20210609 */
6
7 /* { dg-do run } */
8 #include "ffitest.h"
9
10 typedef struct A {
11 float a, b;
12 } A;
13
14 typedef struct B {
15 float x;
16 struct A y;
17 } B;
18
19 B B_fn(float b0, struct B b1)
20 {
21 struct B result;
22
23 result.x = b0 + b1.x;
24 result.y.a = b0 + b1.y.a;
25 result.y.b = b0 + b1.y.b;
26
27 printf("%g %g %g %g: %g %g %g\n", b0, b1.x, b1.y.a, b1.y.b,
28 result.x, result.y.a, result.y.b);
29
30 CHECK_FLOAT_EQ(b0, 12.125);
31 CHECK_FLOAT_EQ(b1.x, 24.75);
32 CHECK_FLOAT_EQ(b1.y.a, 31.625);
33 CHECK_FLOAT_EQ(b1.y.b, 32.25);
34 CHECK_FLOAT_EQ(result.x, 36.875);
35 CHECK_FLOAT_EQ(result.y.a, 43.75);
36 CHECK_FLOAT_EQ(result.y.b, 44.375);
37
38 return result;
39 }
40
41 static void
42 B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
43 void* userdata __UNUSED__)
44 {
45 float b0;
46 struct B b1;
47
48 b0 = *(float*)(args[0]);
49 b1 = *(struct B*)(args[1]);
50
51 *(B*)resp = B_fn(b0, b1);
52 }
53
54 int main (void)
55 {
56 ffi_cif cif;
57 void *code;
58 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
59 void* args_dbl[3];
60 ffi_type* cls_struct_fields[3];
61 ffi_type* cls_struct_fields1[3];
62 ffi_type cls_struct_type, cls_struct_type1;
63 ffi_type* dbl_arg_types[3];
64
65 float e_dbl = 12.125f;
66 struct B f_dbl = { 24.75f, { 31.625f, 32.25f } };
67
68 struct B 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_type1.size = 0;
76 cls_struct_type1.alignment = 0;
77 cls_struct_type1.type = FFI_TYPE_STRUCT;
78 cls_struct_type1.elements = cls_struct_fields1;
79
80 cls_struct_fields[0] = &ffi_type_float;
81 cls_struct_fields[1] = &ffi_type_float;
82 cls_struct_fields[2] = NULL;
83
84 cls_struct_fields1[0] = &ffi_type_float;
85 cls_struct_fields1[1] = &cls_struct_type;
86 cls_struct_fields1[2] = NULL;
87
88
89 dbl_arg_types[0] = &ffi_type_float;
90 dbl_arg_types[1] = &cls_struct_type1;
91 dbl_arg_types[2] = NULL;
92
93 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type1,
94 dbl_arg_types) == FFI_OK);
95
96 args_dbl[0] = &e_dbl;
97 args_dbl[1] = &f_dbl;
98 args_dbl[2] = NULL;
99
100 ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
101 /* { dg-output "12.125 24.75 31.625 32.25: 36.875 43.75 44.375" } */
102 CHECK_FLOAT_EQ( res_dbl.x, (e_dbl + f_dbl.x));
103 CHECK_FLOAT_EQ( res_dbl.y.a, (e_dbl + f_dbl.y.a));
104 CHECK_FLOAT_EQ( res_dbl.y.b, (e_dbl + f_dbl.y.b));
105
106 CHECK(ffi_prep_closure_loc(pcl, &cif, B_gn, NULL, code) == FFI_OK);
107
108 res_dbl = ((B(*)(float, B))(code))(e_dbl, f_dbl);
109 /* { dg-output "\n12.125 24.75 31.625 32.25: 36.875 43.75 44.375" } */
110 CHECK_FLOAT_EQ( res_dbl.x, (e_dbl + f_dbl.x));
111 CHECK_FLOAT_EQ( res_dbl.y.a, (e_dbl + f_dbl.y.a));
112 CHECK_FLOAT_EQ( res_dbl.y.b, (e_dbl + f_dbl.y.b));
113
114 exit(0);
115 }