Ruby: ext/fiddle/libffi-3.2.1/testsuite/libffi.call/nested_struct10.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

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