Debian Codegraph @

testsuite/libffi.call/va_struct3.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*-*-* } } */
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 (void) n;
37 va_start (ap, n);
38 s1 = va_arg (ap, struct small_tag);
39 l = va_arg (ap, struct large_tag);
40 s2 = va_arg (ap, struct small_tag);
41 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,
42 s2.a, s2.b);
43
44 CHECK(s1.a == 5);
45 CHECK(s1.b == 6);
46 CHECK(l.a == 10);
47 CHECK(l.b == 11);
48 CHECK(l.c == 12);
49 CHECK(l.d == 13);
50 CHECK(l.e == 14);
51 CHECK(s2.a == 7);
52 CHECK(s2.b == 8);
53 va_end (ap);
54 l.a += s1.a;
55 l.b += s1.b;
56 l.c += s2.a;
57 l.d += s2.b;
58 return l;
59 }
60
61 int
62 main (void)
63 {
64 ffi_cif cif;
65 void* args[5];
66 ffi_type* arg_types[5];
67
68 ffi_type s_type;
69 ffi_type *s_type_elements[3];
70
71 ffi_type l_type;
72 ffi_type *l_type_elements[6];
73
74 struct small_tag s1;
75 struct small_tag s2;
76 struct large_tag l1;
77
78 int n;
79 struct large_tag res;
80
81 s_type.size = 0;
82 s_type.alignment = 0;
83 s_type.type = FFI_TYPE_STRUCT;
84 s_type.elements = s_type_elements;
85
86 s_type_elements[0] = &ffi_type_uchar;
87 s_type_elements[1] = &ffi_type_uchar;
88 s_type_elements[2] = NULL;
89
90 l_type.size = 0;
91 l_type.alignment = 0;
92 l_type.type = FFI_TYPE_STRUCT;
93 l_type.elements = l_type_elements;
94
95 l_type_elements[0] = &ffi_type_uint;
96 l_type_elements[1] = &ffi_type_uint;
97 l_type_elements[2] = &ffi_type_uint;
98 l_type_elements[3] = &ffi_type_uint;
99 l_type_elements[4] = &ffi_type_uint;
100 l_type_elements[5] = NULL;
101
102 arg_types[0] = &ffi_type_sint;
103 arg_types[1] = &s_type;
104 arg_types[2] = &l_type;
105 arg_types[3] = &s_type;
106 arg_types[4] = NULL;
107
108 CHECK(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 4, &l_type, arg_types) == FFI_OK);
109
110 s1.a = 5;
111 s1.b = 6;
112
113 l1.a = 10;
114 l1.b = 11;
115 l1.c = 12;
116 l1.d = 13;
117 l1.e = 14;
118
119 s2.a = 7;
120 s2.b = 8;
121
122 n = 41;
123
124 args[0] = &n;
125 args[1] = &s1;
126 args[2] = &l1;
127 args[3] = &s2;
128 args[4] = NULL;
129
130 ffi_call(&cif, FFI_FN(test_fn), &res, args);
131 /* { dg-output "5 6 10 11 12 13 14 7 8" } */
132 printf("res: %d %d %d %d %d\n", res.a, res.b, res.c, res.d, res.e);
133 /* { dg-output "\nres: 15 17 19 21 14" } */
134 CHECK(res.a == 15);
135 CHECK(res.b == 17);
136 CHECK(res.c == 19);
137 CHECK(res.d == 21);
138 CHECK(res.e == 14);
139
140 return 0;
141 }