Ruby  2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
nested_struct5.c
Go to the documentation of this file.
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> 20051010 */
8 
9 /* { dg-do run } */
10 #include "ffitest.h"
11 
12 typedef struct A {
13  long double a;
14  unsigned char b;
15 } A;
16 
17 typedef struct B {
18  struct A x;
19  unsigned char y;
20 } B;
21 
22 static B B_fn(struct A b2, struct B b3)
23 {
24  struct B result;
25 
26  result.x.a = b2.a + b3.x.a;
27  result.x.b = b2.b + b3.x.b + b3.y;
28  result.y = b2.b + b3.x.b;
29 
30  printf("%d %d %d %d %d: %d %d %d\n", (int)b2.a, b2.b,
31  (int)b3.x.a, b3.x.b, b3.y,
32  (int)result.x.a, result.x.b, result.y);
33 
34  return result;
35 }
36 
37 static void
38 B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
39  void* userdata __UNUSED__)
40 {
41  struct A b0;
42  struct B b1;
43 
44  b0 = *(struct A*)(args[0]);
45  b1 = *(struct B*)(args[1]);
46 
47  *(B*)resp = B_fn(b0, b1);
48 }
49 
50 int main (void)
51 {
52  ffi_cif cif;
53  void *code;
54  ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
55  void* args_dbl[3];
56  ffi_type* cls_struct_fields[3];
57  ffi_type* cls_struct_fields1[3];
58  ffi_type cls_struct_type, cls_struct_type1;
59  ffi_type* dbl_arg_types[3];
60 
61  struct A e_dbl = { 1.0, 7};
62  struct B f_dbl = {{12.0 , 127}, 99};
63 
64  struct B res_dbl;
65 
66  cls_struct_type.size = 0;
67  cls_struct_type.alignment = 0;
68  cls_struct_type.type = FFI_TYPE_STRUCT;
69  cls_struct_type.elements = cls_struct_fields;
70 
71  cls_struct_type1.size = 0;
72  cls_struct_type1.alignment = 0;
73  cls_struct_type1.type = FFI_TYPE_STRUCT;
74  cls_struct_type1.elements = cls_struct_fields1;
75 
76  cls_struct_fields[0] = &ffi_type_longdouble;
77  cls_struct_fields[1] = &ffi_type_uchar;
78  cls_struct_fields[2] = NULL;
79 
80  cls_struct_fields1[0] = &cls_struct_type;
81  cls_struct_fields1[1] = &ffi_type_uchar;
82  cls_struct_fields1[2] = NULL;
83 
84 
85  dbl_arg_types[0] = &cls_struct_type;
86  dbl_arg_types[1] = &cls_struct_type1;
87  dbl_arg_types[2] = NULL;
88 
89  CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type1,
90  dbl_arg_types) == FFI_OK);
91 
92  args_dbl[0] = &e_dbl;
93  args_dbl[1] = &f_dbl;
94  args_dbl[2] = NULL;
95 
96  ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
97  /* { dg-output "1 7 12 127 99: 13 233 134" } */
98  CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a));
99  CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y));
100  CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b));
101 
102 
103  CHECK(ffi_prep_closure_loc(pcl, &cif, B_gn, NULL, code) == FFI_OK);
104 
105  res_dbl = ((B(*)(A, B))(code))(e_dbl, f_dbl);
106  /* { dg-output "\n1 7 12 127 99: 13 233 134" } */
107  CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a));
108  CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y));
109  CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b));
110 
111  exit(0);
112 }
A
struct A A
B::x
struct A x
Definition: nested_struct10.c:19
NULL
#define NULL
Definition: _sdbm.c:101
ffitest.h
main
int main(void)
Definition: nested_struct5.c:50
A::a
unsigned long long a
Definition: nested_struct10.c:13
B_fn
B B_fn(struct A b0, struct B b1)
Definition: nested_struct2.c:22
B::y
unsigned char y
Definition: nested_struct10.c:18
__UNUSED__
#define __UNUSED__
Definition: ffitest.h:28
CHECK
#define CHECK(sub)
Definition: compile.c:448
B
struct B B
A
Definition: nested_struct10.c:12
FFI_DEFAULT_ABI
@ FFI_DEFAULT_ABI
Definition: ffitarget.h:38
A::a
long double a
Definition: nested_struct5.c:13
A::b
unsigned char b
Definition: nested_struct10.c:14
ffi_type_uchar
#define ffi_type_uchar
Definition: fiddle.h:55
ffi_prep_cif
ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs, ffi_type *rtype, ffi_type **atypes)
Definition: prep_cif.c:226
B
Definition: nested_struct10.c:17
exit
void exit(int __status) __attribute__((__noreturn__))
printf
int int int printf(const char *__restrict,...) __attribute__((__format__(__printf__
ffi_call
void ffi_call(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
Definition: ffi.c:813
ffi_prep_closure_loc
ffi_status ffi_prep_closure_loc(ffi_closure *closure, ffi_cif *cif, void(*fun)(ffi_cif *, void *, void **, void *), void *user_data, void *codeloc)
Definition: ffi.c:928