• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef USING_MMAP
58   static ffi_closure cl;
59 #endif
60   ffi_closure *pcl;
61   void* args_dbl[5];
62   ffi_type* cls_struct_fields[5];
63   ffi_type cls_struct_type;
64   ffi_type* dbl_arg_types[5];
65 
66 #ifdef USING_MMAP
67   pcl = allocate_mmap (sizeof(ffi_closure));
68 #else
69   pcl = &cl;
70 #endif
71 
72   cls_struct_type.size = 0;
73   cls_struct_type.alignment = 0;
74   cls_struct_type.type = FFI_TYPE_STRUCT;
75   cls_struct_type.elements = cls_struct_fields;
76 
77   struct cls_struct_24byte e_dbl = { 9.0, 2.0, 6, 5.0 };
78   struct cls_struct_24byte f_dbl = { 1.0, 2.0, 3, 7.0 };
79   struct cls_struct_24byte g_dbl = { 4.0, 5.0, 7, 9.0 };
80   struct cls_struct_24byte h_dbl = { 8.0, 6.0, 1, 4.0 };
81   struct cls_struct_24byte res_dbl;
82 
83   cls_struct_fields[0] = &ffi_type_double;
84   cls_struct_fields[1] = &ffi_type_double;
85   cls_struct_fields[2] = &ffi_type_sint;
86   cls_struct_fields[3] = &ffi_type_float;
87   cls_struct_fields[4] = NULL;
88 
89   dbl_arg_types[0] = &cls_struct_type;
90   dbl_arg_types[1] = &cls_struct_type;
91   dbl_arg_types[2] = &cls_struct_type;
92   dbl_arg_types[3] = &cls_struct_type;
93   dbl_arg_types[4] = NULL;
94 
95   CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 4, &cls_struct_type,
96 		     dbl_arg_types) == FFI_OK);
97 
98   args_dbl[0] = &e_dbl;
99   args_dbl[1] = &f_dbl;
100   args_dbl[2] = &g_dbl;
101   args_dbl[3] = &h_dbl;
102   args_dbl[4] = NULL;
103 
104   ffi_call(&cif, FFI_FN(cls_struct_24byte_fn), &res_dbl, args_dbl);
105   /* { dg-output "9 2 6 5 1 2 3 7 4 5 7 9 8 6 1 9: 22 15 17 25" } */
106   printf("res: %g %g %d %g\n", res_dbl.a, res_dbl.b, res_dbl.c, res_dbl.d);
107   /* { dg-output "\nres: 22 15 17 25" } */
108 
109   CHECK(ffi_prep_closure(pcl, &cif, cls_struct_24byte_gn, NULL) == FFI_OK);
110 
111   res_dbl = ((cls_struct_24byte(*)(cls_struct_24byte,
112 				   cls_struct_24byte,
113 				   cls_struct_24byte,
114 				   cls_struct_24byte))
115 	     (pcl))(e_dbl, f_dbl, g_dbl, h_dbl);
116   /* { dg-output "\n9 2 6 5 1 2 3 7 4 5 7 9 8 6 1 9: 22 15 17 25" } */
117   printf("res: %g %g %d %g\n", res_dbl.a, res_dbl.b, res_dbl.c, res_dbl.d);
118   /* { dg-output "\nres: 22 15 17 25" } */
119 
120   exit(0);
121 }
122