Debian Codegraph @

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