1/* Area:	ffi_call, closure_call
2   Purpose:	Check structure passing with different structure size.
3		Contains structs as parameter of the struct itself.
4		Sample taken from Alan Modras patch to src/prep_cif.c.
5   Limitations:	none.
6   PR:		none.
7   Originator:	<andreast@gcc.gnu.org> 20030911	 */
8
9/* { dg-do run } */
10#include "ffitest.h"
11
12typedef struct A {
13  unsigned long a;
14  unsigned char b;
15} A;
16
17typedef struct B {
18  struct A x;
19  unsigned char y;
20} B;
21
22B B_fn(struct A b0, struct B b1)
23{
24  struct B result;
25
26  result.x.a = b0.a + b1.x.a;
27  result.x.b = b0.b + b1.x.b + b1.y;
28  result.y = b0.b + b1.x.b;
29
30  printf("%lu %d %lu %d %d: %lu %d %d\n", b0.a, b0.b, b1.x.a, b1.x.b, b1.y,
31	 result.x.a, result.x.b, result.y);
32
33  return result;
34}
35
36static void
37B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
38     void* userdata __UNUSED__)
39{
40  struct A b0;
41  struct B b1;
42
43  b0 = *(struct A*)(args[0]);
44  b1 = *(struct B*)(args[1]);
45
46  *(B*)resp = B_fn(b0, b1);
47}
48
49int main (void)
50{
51  ffi_cif cif;
52#ifndef USING_MMAP
53  static ffi_closure cl;
54#endif
55  ffi_closure *pcl;
56  void* args_dbl[3];
57  ffi_type* cls_struct_fields[3];
58  ffi_type* cls_struct_fields1[3];
59  ffi_type cls_struct_type, cls_struct_type1;
60  ffi_type* dbl_arg_types[3];
61
62#ifdef USING_MMAP
63  pcl = allocate_mmap (sizeof(ffi_closure));
64#else
65  pcl = &cl;
66#endif
67
68  cls_struct_type.size = 0;
69  cls_struct_type.alignment = 0;
70  cls_struct_type.type = FFI_TYPE_STRUCT;
71  cls_struct_type.elements = cls_struct_fields;
72
73  cls_struct_type1.size = 0;
74  cls_struct_type1.alignment = 0;
75  cls_struct_type1.type = FFI_TYPE_STRUCT;
76  cls_struct_type1.elements = cls_struct_fields1;
77
78  struct A e_dbl = { 1, 7};
79  struct B f_dbl = {{12 , 127}, 99};
80
81  struct B res_dbl;
82
83  cls_struct_fields[0] = &ffi_type_ulong;
84  cls_struct_fields[1] = &ffi_type_uchar;
85  cls_struct_fields[2] = NULL;
86
87  cls_struct_fields1[0] = &cls_struct_type;
88  cls_struct_fields1[1] = &ffi_type_uchar;
89  cls_struct_fields1[2] = NULL;
90
91
92  dbl_arg_types[0] = &cls_struct_type;
93  dbl_arg_types[1] = &cls_struct_type1;
94  dbl_arg_types[2] = NULL;
95
96  CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type1,
97		     dbl_arg_types) == FFI_OK);
98
99  args_dbl[0] = &e_dbl;
100  args_dbl[1] = &f_dbl;
101  args_dbl[2] = NULL;
102
103  ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
104  /* { dg-output "1 7 12 127 99: 13 233 134" } */
105  CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a));
106  CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y));
107  CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b));
108
109  CHECK(ffi_prep_closure(pcl, &cif, B_gn, NULL) == FFI_OK);
110
111  res_dbl = ((B(*)(A, B))(pcl))(e_dbl, f_dbl);
112  /* { dg-output "\n1 7 12 127 99: 13 233 134" } */
113  CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a));
114  CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y));
115  CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b));
116
117  exit(0);
118}
119