Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
nested_struct9.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
12typedef struct A {
13 unsigned char a;
14 unsigned long long b;
15} A;
16
17typedef struct B {
18 struct A x;
19 unsigned char y;
20} B;
21
22typedef struct C {
23 unsigned long d;
24 unsigned char e;
25} C;
26
27static B B_fn(struct A b2, struct B b3, struct C b4)
28{
29 struct B result;
30
31 result.x.a = b2.a + b3.x.a + b4.d;
32 result.x.b = b2.b + b3.x.b + b3.y + b4.e;
33 result.y = b2.b + b3.x.b + b4.e;
34
35 printf("%d %d %d %d %d %d %d: %d %d %d\n", b2.a, (int)b2.b,
36 b3.x.a, (int)b3.x.b, b3.y, (int)b4.d, b4.e,
37 result.x.a, (int)result.x.b, result.y);
38
39 return result;
40}
41
42static void
43B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
44 void* userdata __UNUSED__)
45{
46 struct A b0;
47 struct B b1;
48 struct C b2;
49
50 b0 = *(struct A*)(args[0]);
51 b1 = *(struct B*)(args[1]);
52 b2 = *(struct C*)(args[2]);
53
54 *(B*)resp = B_fn(b0, b1, b2);
55}
56
57int main (void)
58{
59 ffi_cif cif;
60 void *code;
61 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
62 void* args_dbl[4];
63 ffi_type* cls_struct_fields[3];
64 ffi_type* cls_struct_fields1[3];
65 ffi_type* cls_struct_fields2[3];
66 ffi_type cls_struct_type, cls_struct_type1, cls_struct_type2;
67 ffi_type* dbl_arg_types[4];
68
69 struct A e_dbl = { 1, 7LL};
70 struct B f_dbl = {{12.0 , 127}, 99};
71 struct C g_dbl = { 2, 9};
72
73 struct B res_dbl;
74
75 cls_struct_type.size = 0;
76 cls_struct_type.alignment = 0;
77 cls_struct_type.type = FFI_TYPE_STRUCT;
78 cls_struct_type.elements = cls_struct_fields;
79
80 cls_struct_type1.size = 0;
81 cls_struct_type1.alignment = 0;
82 cls_struct_type1.type = FFI_TYPE_STRUCT;
83 cls_struct_type1.elements = cls_struct_fields1;
84
85 cls_struct_type2.size = 0;
86 cls_struct_type2.alignment = 0;
87 cls_struct_type2.type = FFI_TYPE_STRUCT;
88 cls_struct_type2.elements = cls_struct_fields2;
89
90 cls_struct_fields[0] = &ffi_type_uchar;
91 cls_struct_fields[1] = &ffi_type_uint64;
92 cls_struct_fields[2] = NULL;
93
94 cls_struct_fields1[0] = &cls_struct_type;
95 cls_struct_fields1[1] = &ffi_type_uchar;
96 cls_struct_fields1[2] = NULL;
97
98 cls_struct_fields2[0] = &ffi_type_ulong;
99 cls_struct_fields2[1] = &ffi_type_uchar;
100 cls_struct_fields2[2] = NULL;
101
102
103 dbl_arg_types[0] = &cls_struct_type;
104 dbl_arg_types[1] = &cls_struct_type1;
105 dbl_arg_types[2] = &cls_struct_type2;
106 dbl_arg_types[3] = NULL;
107
108 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 3, &cls_struct_type1,
109 dbl_arg_types) == FFI_OK);
110
111 args_dbl[0] = &e_dbl;
112 args_dbl[1] = &f_dbl;
113 args_dbl[2] = &g_dbl;
114 args_dbl[3] = NULL;
115
116 ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
117 /* { dg-output "1 7 12 127 99 2 9: 15 242 143" } */
118 CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a + g_dbl.d));
119 CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y + g_dbl.e));
120 CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b + g_dbl.e));
121
122 CHECK(ffi_prep_closure_loc(pcl, &cif, B_gn, NULL, code) == FFI_OK);
123
124 res_dbl = ((B(*)(A, B, C))(code))(e_dbl, f_dbl, g_dbl);
125 /* { dg-output "\n1 7 12 127 99 2 9: 15 242 143" } */
126 CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a + g_dbl.d));
127 CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y + g_dbl.e));
128 CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b + g_dbl.e));
129
130 exit(0);
131}
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
void ffi_call(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
Definition: ffi.c:813
@ FFI_DEFAULT_ABI
Definition: ffitarget.h:38
#define CHECK(sub)
Definition: compile.c:448
#define __UNUSED__
Definition: ffitest.h:28
#define ffi_type_uchar
Definition: fiddle.h:55
B B_fn(struct A b0, struct B b1)
struct B B
int main(void)
struct A A
struct C C
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
#define NULL
int int int printf(const char *__restrict__,...) __attribute__((__format__(__printf__
void exit(int __status) __attribute__((__noreturn__))
unsigned long long a
unsigned long long b
unsigned char a
unsigned char b
unsigned char y
struct A x
unsigned char e
unsigned long d
unsigned long long d