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:	<andreast@gcc.gnu.org> 20030828	 */
7 
8 /* { dg-do run } */
9 #include "ffitest.h"
10 
11 typedef struct cls_struct_24byte {
12   double a;
13   double b;
14   int c;
15   float d;
16 } cls_struct_24byte;
17 
cls_struct_24byte_fn(struct cls_struct_24byte b0,struct cls_struct_24byte b1,struct cls_struct_24byte b2,struct cls_struct_24byte b3)18 cls_struct_24byte cls_struct_24byte_fn(struct cls_struct_24byte b0,
19 			    struct cls_struct_24byte b1,
20 			    struct cls_struct_24byte b2,
21 			    struct cls_struct_24byte b3)
22 {
23   struct cls_struct_24byte result;
24 
25   result.a = b0.a + b1.a + b2.a + b3.a;
26   result.b = b0.b + b1.b + b2.b + b3.b;
27   result.c = b0.c + b1.c + b2.c + b3.c;
28   result.d = b0.d + b1.d + b2.d + b3.d;
29 
30   printf("%g %g %d %g %g %g %d %g %g %g %d %g %g %g %d %g: %g %g %d %g\n",
31 	 b0.a, b0.b, b0.c, b0.d,
32 	 b1.a, b1.b, b1.c, b1.d,
33 	 b2.a, b2.b, b2.c, b2.d,
34 	 b3.a, b3.b, b3.c, b2.d,
35 	 result.a, result.b, result.c, result.d);
36 
37   return result;
38 }
39 
40 static void
cls_struct_24byte_gn(ffi_cif * cif __UNUSED__,void * resp,void ** args,void * userdata __UNUSED__)41 cls_struct_24byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
42 		     void* userdata __UNUSED__)
43 {
44   struct cls_struct_24byte b0, b1, b2, b3;
45 
46   b0 = *(struct cls_struct_24byte*)(args[0]);
47   b1 = *(struct cls_struct_24byte*)(args[1]);
48   b2 = *(struct cls_struct_24byte*)(args[2]);
49   b3 = *(struct cls_struct_24byte*)(args[3]);
50 
51   *(cls_struct_24byte*)resp = cls_struct_24byte_fn(b0, b1, b2, b3);
52 }
53 
main(void)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[5];
60   ffi_type* cls_struct_fields[5];
61   ffi_type cls_struct_type;
62   ffi_type* dbl_arg_types[5];
63 
64   struct cls_struct_24byte e_dbl = { 9.0, 2.0, 6, 5.0 };
65   struct cls_struct_24byte f_dbl = { 1.0, 2.0, 3, 7.0 };
66   struct cls_struct_24byte g_dbl = { 4.0, 5.0, 7, 9.0 };
67   struct cls_struct_24byte h_dbl = { 8.0, 6.0, 1, 4.0 };
68   struct cls_struct_24byte 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_double;
76   cls_struct_fields[1] = &ffi_type_double;
77   cls_struct_fields[2] = &ffi_type_sint;
78   cls_struct_fields[3] = &ffi_type_float;
79   cls_struct_fields[4] = NULL;
80 
81   dbl_arg_types[0] = &cls_struct_type;
82   dbl_arg_types[1] = &cls_struct_type;
83   dbl_arg_types[2] = &cls_struct_type;
84   dbl_arg_types[3] = &cls_struct_type;
85   dbl_arg_types[4] = NULL;
86 
87   CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 4, &cls_struct_type,
88 		     dbl_arg_types) == FFI_OK);
89 
90   args_dbl[0] = &e_dbl;
91   args_dbl[1] = &f_dbl;
92   args_dbl[2] = &g_dbl;
93   args_dbl[3] = &h_dbl;
94   args_dbl[4] = NULL;
95 
96   ffi_call(&cif, FFI_FN(cls_struct_24byte_fn), &res_dbl, args_dbl);
97   /* { dg-output "9 2 6 5 1 2 3 7 4 5 7 9 8 6 1 9: 22 15 17 25" } */
98   printf("res: %g %g %d %g\n", res_dbl.a, res_dbl.b, res_dbl.c, res_dbl.d);
99   /* { dg-output "\nres: 22 15 17 25" } */
100 
101   CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_24byte_gn, NULL, code) == FFI_OK);
102 
103   res_dbl = ((cls_struct_24byte(*)(cls_struct_24byte,
104 				   cls_struct_24byte,
105 				   cls_struct_24byte,
106 				   cls_struct_24byte))
107 	     (code))(e_dbl, f_dbl, g_dbl, h_dbl);
108   /* { dg-output "\n9 2 6 5 1 2 3 7 4 5 7 9 8 6 1 9: 22 15 17 25" } */
109   printf("res: %g %g %d %g\n", res_dbl.a, res_dbl.b, res_dbl.c, res_dbl.d);
110   /* { dg-output "\nres: 22 15 17 25" } */
111 
112   exit(0);
113 }
114