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:	<compnerd@compnerd.org> 20171026	 */
7 
8 /* { dg-do run } */
9 
10 #include "ffitest.h"
11 
12 typedef struct cls_struct_3float {
13   float f;
14   float g;
15   float h;
16 } cls_struct_3float;
17 
cls_struct_3float_fn(struct cls_struct_3float a1,struct cls_struct_3float a2)18 cls_struct_3float cls_struct_3float_fn(struct cls_struct_3float a1,
19 				       struct cls_struct_3float a2)
20 {
21   struct cls_struct_3float result;
22 
23   result.f = a1.f + a2.f;
24   result.g = a1.g + a2.g;
25   result.h = a1.h + a2.h;
26 
27   printf("%g %g %g %g %g %g: %g %g %g\n", a1.f, a1.g, a1.h,
28 	 a2.f, a2.g, a2.h, result.f, result.g, result.h);
29 
30   return result;
31 }
32 
33 static void
cls_struct_3float_gn(ffi_cif * cif __UNUSED__,void * resp,void ** args,void * userdata __UNUSED__)34 cls_struct_3float_gn(ffi_cif *cif __UNUSED__, void* resp, void **args,
35 		     void* userdata __UNUSED__)
36 {
37   struct cls_struct_3float a1, a2;
38 
39   a1 = *(struct cls_struct_3float*)(args[0]);
40   a2 = *(struct cls_struct_3float*)(args[1]);
41 
42   *(cls_struct_3float*)resp = cls_struct_3float_fn(a1, a2);
43 }
44 
main(void)45 int main (void)
46 {
47   ffi_cif cif;
48   void *code;
49   ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
50   void *args_dbl[3];
51   ffi_type* cls_struct_fields[4];
52   ffi_type cls_struct_type;
53   ffi_type* dbl_arg_types[3];
54 
55   struct cls_struct_3float g_dbl = { 1.0f, 2.0f, 3.0f };
56   struct cls_struct_3float f_dbl = { 1.0f, 2.0f, 3.0f };
57   struct cls_struct_3float res_dbl;
58 
59   cls_struct_fields[0] = &ffi_type_float;
60   cls_struct_fields[1] = &ffi_type_float;
61   cls_struct_fields[2] = &ffi_type_float;
62   cls_struct_fields[3] = NULL;
63 
64   cls_struct_type.size = 0;
65   cls_struct_type.alignment = 0;
66   cls_struct_type.type = FFI_TYPE_STRUCT;
67   cls_struct_type.elements = cls_struct_fields;
68 
69   dbl_arg_types[0] = &cls_struct_type;
70   dbl_arg_types[1] = &cls_struct_type;
71   dbl_arg_types[2] = NULL;
72 
73   CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
74 		     dbl_arg_types) == FFI_OK);
75 
76   args_dbl[0] = &g_dbl;
77   args_dbl[1] = &f_dbl;
78   args_dbl[2] = NULL;
79 
80   ffi_call(&cif, FFI_FN(cls_struct_3float_fn), &res_dbl, args_dbl);
81   /* { dg-output "1 2 3 1 2 3: 2 4 6" } */
82   printf("res: %g %g %g\n", res_dbl.f, res_dbl.g, res_dbl.h);
83   /* { dg-output "\nres: 2 4 6" } */
84 
85   CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_3float_gn, NULL, code) ==
86 	FFI_OK);
87 
88   res_dbl = ((cls_struct_3float(*)(cls_struct_3float,
89 				   cls_struct_3float))(code))(g_dbl, f_dbl);
90   /* { dg-output "\n1 2 3 1 2 3: 2 4 6" } */
91   printf("res: %g %g %g\n", res_dbl.f, res_dbl.g, res_dbl.h);
92   /* { dg-output "\nres: 2 4 6" } */
93 
94   exit(0);
95 }
96