Ruby  2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
va_struct3.c
Go to the documentation of this file.
1 /* Area: ffi_call
2  Purpose: Test passing struct in variable argument lists.
3  Limitations: none.
4  PR: none.
5  Originator: ARM Ltd. */
6 
7 /* { dg-do run } */
8 /* { dg-output "" { xfail avr32*-*-* } } */
9 
10 #include "ffitest.h"
11 #include <stdarg.h>
12 
13 struct small_tag
14 {
15  unsigned char a;
16  unsigned char b;
17 };
18 
19 struct large_tag
20 {
21  unsigned a;
22  unsigned b;
23  unsigned c;
24  unsigned d;
25  unsigned e;
26 };
27 
28 static struct large_tag
29 test_fn (int n, ...)
30 {
31  va_list ap;
32  struct small_tag s1;
33  struct small_tag s2;
34  struct large_tag l;
35 
36  va_start (ap, n);
37  s1 = va_arg (ap, struct small_tag);
38  l = va_arg (ap, struct large_tag);
39  s2 = va_arg (ap, struct small_tag);
40  printf ("%u %u %u %u %u %u %u %u %u\n", s1.a, s1.b, l.a, l.b, l.c, l.d, l.e,
41  s2.a, s2.b);
42  va_end (ap);
43  l.a += s1.a;
44  l.b += s1.b;
45  l.c += s2.a;
46  l.d += s2.b;
47  return l;
48 }
49 
50 int
51 main (void)
52 {
53  ffi_cif cif;
54  void* args[5];
55  ffi_type* arg_types[5];
56 
57  ffi_type s_type;
58  ffi_type *s_type_elements[3];
59 
60  ffi_type l_type;
61  ffi_type *l_type_elements[6];
62 
63  struct small_tag s1;
64  struct small_tag s2;
65  struct large_tag l1;
66 
67  int n;
68  struct large_tag res;
69 
70  s_type.size = 0;
71  s_type.alignment = 0;
72  s_type.type = FFI_TYPE_STRUCT;
73  s_type.elements = s_type_elements;
74 
75  s_type_elements[0] = &ffi_type_uchar;
76  s_type_elements[1] = &ffi_type_uchar;
77  s_type_elements[2] = NULL;
78 
79  l_type.size = 0;
80  l_type.alignment = 0;
81  l_type.type = FFI_TYPE_STRUCT;
82  l_type.elements = l_type_elements;
83 
84  l_type_elements[0] = &ffi_type_uint;
85  l_type_elements[1] = &ffi_type_uint;
86  l_type_elements[2] = &ffi_type_uint;
87  l_type_elements[3] = &ffi_type_uint;
88  l_type_elements[4] = &ffi_type_uint;
89  l_type_elements[5] = NULL;
90 
91  arg_types[0] = &ffi_type_sint;
92  arg_types[1] = &s_type;
93  arg_types[2] = &l_type;
94  arg_types[3] = &s_type;
95  arg_types[4] = NULL;
96 
97  CHECK(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 4, &l_type, arg_types) == FFI_OK);
98 
99  s1.a = 5;
100  s1.b = 6;
101 
102  l1.a = 10;
103  l1.b = 11;
104  l1.c = 12;
105  l1.d = 13;
106  l1.e = 14;
107 
108  s2.a = 7;
109  s2.b = 8;
110 
111  n = 41;
112 
113  args[0] = &n;
114  args[1] = &s1;
115  args[2] = &l1;
116  args[3] = &s2;
117  args[4] = NULL;
118 
119  ffi_call(&cif, FFI_FN(test_fn), &res, args);
120  /* { dg-output "5 6 10 11 12 13 14 7 8" } */
121  printf("res: %d %d %d %d %d\n", res.a, res.b, res.c, res.d, res.e);
122  /* { dg-output "\nres: 15 17 19 21 14" } */
123 
124  return 0;
125 }
main
int main(void)
Definition: va_struct3.c:51
small_tag
Definition: cls_struct_va1.c:11
large_tag
Definition: cls_struct_va1.c:17
small_tag::b
unsigned char b
Definition: cls_struct_va1.c:14
n
const char size_t n
Definition: rb_mjit_min_header-2.7.1.h:5417
large_tag::b
unsigned b
Definition: cls_struct_va1.c:20
large_tag::c
unsigned c
Definition: cls_struct_va1.c:21
NULL
#define NULL
Definition: _sdbm.c:101
ffitest.h
large_tag::e
unsigned e
Definition: cls_struct_va1.c:23
va_start
#define va_start(v, l)
Definition: rb_mjit_min_header-2.7.1.h:3946
s2
const char * s2
Definition: rb_mjit_min_header-2.7.1.h:5415
va_list
__gnuc_va_list va_list
Definition: rb_mjit_min_header-2.7.1.h:831
va_arg
#define va_arg(v, l)
Definition: rb_mjit_min_header-2.7.1.h:3948
CHECK
#define CHECK(sub)
Definition: compile.c:448
FFI_DEFAULT_ABI
@ FFI_DEFAULT_ABI
Definition: ffitarget.h:38
ffi_prep_cif_var
ffi_status ffi_prep_cif_var(ffi_cif *cif, ffi_abi abi, unsigned int nfixedargs, unsigned int ntotalargs, ffi_type *rtype, ffi_type **atypes)
Definition: prep_cif.c:232
small_tag::a
unsigned char a
Definition: cls_struct_va1.c:13
ffi_type_uchar
#define ffi_type_uchar
Definition: fiddle.h:55
large_tag::d
unsigned d
Definition: cls_struct_va1.c:22
int
__inline__ int
Definition: rb_mjit_min_header-2.7.1.h:2807
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
large_tag::a
unsigned a
Definition: cls_struct_va1.c:19
va_end
#define va_end(v)
Definition: rb_mjit_min_header-2.7.1.h:3947