Debian Codegraph @

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