libstdc++: Avoid conflicting declaration in eh_call.cc [PR112997]
[official-gcc.git] / libffi / testsuite / libffi.closures / nested_struct13.c
blob6c139b9526882b7d3bd1a6fdb7055727eaa43996
1 /* Area: ffi_call, closure_call
2 Purpose: Check structure passing.
3 Limitations: none.
4 PR: none.
5 Originator: <jincheng@ca.ibm.com> and <jakub@redhat.com> 20210609 */
7 /* { dg-do run } */
8 #include "ffitest.h"
10 typedef struct A {
11 float a, b;
12 } A;
14 typedef struct B {
15 float x;
16 struct A y;
17 } B;
19 B B_fn(float b0, struct B b1)
21 struct B result;
23 result.x = b0 + b1.x;
24 result.y.a = b0 + b1.y.a;
25 result.y.b = b0 + b1.y.b;
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);
30 return result;
33 static void
34 B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
35 void* userdata __UNUSED__)
37 float b0;
38 struct B b1;
40 b0 = *(float*)(args[0]);
41 b1 = *(struct B*)(args[1]);
43 *(B*)resp = B_fn(b0, b1);
46 int main (void)
48 ffi_cif cif;
49 void *code;
50 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
51 void* args_dbl[3];
52 ffi_type* cls_struct_fields[3];
53 ffi_type* cls_struct_fields1[3];
54 ffi_type cls_struct_type, cls_struct_type1;
55 ffi_type* dbl_arg_types[3];
57 float e_dbl = 12.125f;
58 struct B f_dbl = { 24.75f, { 31.625f, 32.25f } };
60 struct B res_dbl;
62 cls_struct_type.size = 0;
63 cls_struct_type.alignment = 0;
64 cls_struct_type.type = FFI_TYPE_STRUCT;
65 cls_struct_type.elements = cls_struct_fields;
67 cls_struct_type1.size = 0;
68 cls_struct_type1.alignment = 0;
69 cls_struct_type1.type = FFI_TYPE_STRUCT;
70 cls_struct_type1.elements = cls_struct_fields1;
72 cls_struct_fields[0] = &ffi_type_float;
73 cls_struct_fields[1] = &ffi_type_float;
74 cls_struct_fields[2] = NULL;
76 cls_struct_fields1[0] = &ffi_type_float;
77 cls_struct_fields1[1] = &cls_struct_type;
78 cls_struct_fields1[2] = NULL;
81 dbl_arg_types[0] = &ffi_type_float;
82 dbl_arg_types[1] = &cls_struct_type1;
83 dbl_arg_types[2] = NULL;
85 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type1,
86 dbl_arg_types) == FFI_OK);
88 args_dbl[0] = &e_dbl;
89 args_dbl[1] = &f_dbl;
90 args_dbl[2] = NULL;
92 ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
93 /* { dg-output "12.125 24.75 31.625 32.25: 36.875 43.75 44.375" } */
94 CHECK( res_dbl.x == (e_dbl + f_dbl.x));
95 CHECK( res_dbl.y.a == (e_dbl + f_dbl.y.a));
96 CHECK( res_dbl.y.b == (e_dbl + f_dbl.y.b));
98 CHECK(ffi_prep_closure_loc(pcl, &cif, B_gn, NULL, code) == FFI_OK);
100 res_dbl = ((B(*)(float, B))(code))(e_dbl, f_dbl);
101 /* { dg-output "\n12.125 24.75 31.625 32.25: 36.875 43.75 44.375" } */
102 CHECK( res_dbl.x == (e_dbl + f_dbl.x));
103 CHECK( res_dbl.y.a == (e_dbl + f_dbl.y.a));
104 CHECK( res_dbl.y.b == (e_dbl + f_dbl.y.b));
106 exit(0);