Debian Codegraph @

testsuite/libffi.call/va_2.c

raw
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*-*-* m68k-*-* } } */
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
29 static int
30 test_fn (int n, ...)
31 {
32 va_list ap;
33 struct small_tag s1;
34 struct small_tag s2;
35 struct large_tag l;
36 unsigned char uc;
37 signed char sc;
38 unsigned short us;
39 signed short ss;
40 unsigned int ui;
41 signed int si;
42 unsigned long ul;
43 signed long sl;
44 float f;
45 double d;
46
47 va_start (ap, n);
48 s1 = va_arg (ap, struct small_tag);
49 l = va_arg (ap, struct large_tag);
50 s2 = va_arg (ap, struct small_tag);
51
52 uc = va_arg (ap, unsigned);
53 sc = va_arg (ap, signed);
54
55 us = va_arg (ap, unsigned);
56 ss = va_arg (ap, signed);
57
58 ui = va_arg (ap, unsigned int);
59 si = va_arg (ap, signed int);
60
61 ul = va_arg (ap, unsigned long);
62 sl = va_arg (ap, signed long);
63
64 f = va_arg (ap, double); /* C standard promotes float->double
65 when anonymous */
66 d = va_arg (ap, double);
67
68 printf ("%u %u %u %u %u %u %u %u %u uc=%u sc=%d %u %d %u %d %lu %ld %f %f\n",
69 s1.a, s1.b, l.a, l.b, l.c, l.d, l.e,
70 s2.a, s2.b,
71 uc, sc,
72 us, ss,
73 ui, si,
74 ul, sl,
75 f, d);
76
77 va_end (ap);
78
79 CHECK(s1.a == 5);
80 CHECK(s1.b == 6);
81 CHECK(l.a == 10);
82 CHECK(l.b == 11);
83 CHECK(l.c == 12);
84 CHECK(l.d == 13);
85 CHECK(l.e == 14);
86 CHECK(s2.a == 7);
87 CHECK(s2.b == 8);
88 CHECK(uc == 9);
89 CHECK(sc == 10);
90 CHECK(us == 11);
91 CHECK(ss == 12);
92 CHECK(ui == 13);
93 CHECK(si == 14);
94 CHECK(ul == 15);
95 CHECK(sl == 16);
96 CHECK((int)f == 2);
97 CHECK((int)d == 3);
98
99 return n + 1;
100 }
101
102 int
103 main (void)
104 {
105 ffi_cif cif;
106 void* args[15];
107 ffi_type* arg_types[15];
108
109 ffi_type s_type;
110 ffi_type *s_type_elements[3];
111
112 ffi_type l_type;
113 ffi_type *l_type_elements[6];
114
115 struct small_tag s1;
116 struct small_tag s2;
117 struct large_tag l1;
118
119 int n;
120 ffi_arg res;
121
122 unsigned int uc;
123 signed int sc;
124 unsigned int us;
125 signed int ss;
126 unsigned int ui;
127 signed int si;
128 unsigned long ul;
129 signed long sl;
130 double d1;
131 double f1;
132
133 s_type.size = 0;
134 s_type.alignment = 0;
135 s_type.type = FFI_TYPE_STRUCT;
136 s_type.elements = s_type_elements;
137
138 s_type_elements[0] = &ffi_type_uchar;
139 s_type_elements[1] = &ffi_type_uchar;
140 s_type_elements[2] = NULL;
141
142 l_type.size = 0;
143 l_type.alignment = 0;
144 l_type.type = FFI_TYPE_STRUCT;
145 l_type.elements = l_type_elements;
146
147 l_type_elements[0] = &ffi_type_uint;
148 l_type_elements[1] = &ffi_type_uint;
149 l_type_elements[2] = &ffi_type_uint;
150 l_type_elements[3] = &ffi_type_uint;
151 l_type_elements[4] = &ffi_type_uint;
152 l_type_elements[5] = NULL;
153
154 arg_types[0] = &ffi_type_sint;
155 arg_types[1] = &s_type;
156 arg_types[2] = &l_type;
157 arg_types[3] = &s_type;
158 arg_types[4] = &ffi_type_uint;
159 arg_types[5] = &ffi_type_sint;
160 arg_types[6] = &ffi_type_uint;
161 arg_types[7] = &ffi_type_sint;
162 arg_types[8] = &ffi_type_uint;
163 arg_types[9] = &ffi_type_sint;
164 arg_types[10] = &ffi_type_ulong;
165 arg_types[11] = &ffi_type_slong;
166 arg_types[12] = &ffi_type_double;
167 arg_types[13] = &ffi_type_double;
168 arg_types[14] = NULL;
169
170 CHECK(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 14, &ffi_type_sint, arg_types) == FFI_OK);
171
172 s1.a = 5;
173 s1.b = 6;
174
175 l1.a = 10;
176 l1.b = 11;
177 l1.c = 12;
178 l1.d = 13;
179 l1.e = 14;
180
181 s2.a = 7;
182 s2.b = 8;
183
184 n = 41;
185
186 uc = 9;
187 sc = 10;
188 us = 11;
189 ss = 12;
190 ui = 13;
191 si = 14;
192 ul = 15;
193 sl = 16;
194 f1 = 2.12;
195 d1 = 3.13;
196
197 args[0] = &n;
198 args[1] = &s1;
199 args[2] = &l1;
200 args[3] = &s2;
201 args[4] = &uc;
202 args[5] = &sc;
203 args[6] = &us;
204 args[7] = &ss;
205 args[8] = &ui;
206 args[9] = &si;
207 args[10] = &ul;
208 args[11] = &sl;
209 args[12] = &f1;
210 args[13] = &d1;
211 args[14] = NULL;
212
213 ffi_call(&cif, FFI_FN(test_fn), &res, args);
214 /* { dg-output "5 6 10 11 12 13 14 7 8 uc=9 sc=10 11 12 13 14 15 16 2.120000 3.130000" } */
215 printf("res: %d\n", (int) res);
216 /* { dg-output "\nres: 42" } */
217 CHECK(res == 42);
218
219 return 0;
220 }