1/* This testcase is part of GDB, the GNU debugger.
2
3   Copyright 2018-2020 Free Software Foundation, Inc.
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 3 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18/* This file is used for testing GDBs ability to pass structures to, and
19   return structures from, functions.  All of the structures in this test
20   are special in that they are small structures containing from 1 up to 5
21   scalar fields, the fields can be inside nested structures, and there can
22   be empty structures around too.
23
24   When compiled for C++ this file also tests structures containing static
25   members (which live in global memory).  In addition, empty structures in C++
26   have a size of 1 (compared to 0 in GNU C), which can effect structure
27   padding.
28
29   This test is specifically written for RiscV and Aarch64, which both have
30   special ABI rules for structures like these, however, there should be no harm
31   in running these tests on other targets, though in many cases the
32   structures will treated no differently to the structures already covered
33   in the structs.exp test script.  */
34
35#include <string.h>
36
37/* Useful abreviations.  */
38typedef char tc;
39typedef short ts;
40typedef int ti;
41typedef long tl;
42typedef long long tll;
43typedef float tf;
44typedef double td;
45typedef long double tld;
46
47#ifdef TEST_COMPLEX
48typedef float _Complex tfc;
49typedef double _Complex tdc;
50typedef long double _Complex tldc;
51#endif /* TEST_COMPLEX */
52
53#define MAKE_CHECK_FUNCS(TYPE)					\
54  int __attribute__((noinline,noclone))				\
55  check_arg_ ## TYPE (struct TYPE arg)				\
56  {								\
57    return cmp_ ## TYPE (arg, ref_val_ ## TYPE);		\
58  }								\
59								\
60  struct TYPE __attribute__((noinline,noclone))			\
61  rtn_str_ ## TYPE (void)					\
62  {								\
63    return (ref_val_ ## TYPE);					\
64  }
65
66#define REF_VAL(NAME) struct NAME ref_val_ ## NAME
67#define ES(NAME) struct { } NAME
68
69/* Test is either for a single type or two differing types.  */
70#if defined tA && ! defined tB
71#define tB tA
72#endif
73#if ! defined tB
74#error "Incorrect configuration of tA and tB defines"
75#endif
76
77/* Structures with a single field nested to various depths, along with
78   some empty structures.  */
79struct struct_01_01 { ES(es1); struct { struct { tA a; } s1; } s2; };
80struct struct_01_02 { tA a; struct { struct { ES(es1); } s1; } s2; };
81struct struct_01_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4;};
82struct struct_01_04 { ES(es1); ES(es2); tA a; ES(es3); };
83
84/* Structures with two fields nested to various depths, along with
85   some empty structures.  */
86struct struct_02_01 { ES(es1); struct { struct { tA a; tB b; } s1; } s2; };
87struct struct_02_02 { tA a; struct { struct { ES(es1); } s1; } s2; tB b; };
88struct struct_02_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4; struct { struct { tB b; } s5; } s6;};
89struct struct_02_04 { ES(es1); ES(es2); tA a; ES(es3); tB b; };
90
91/* Structures with four fields nested to various depths, along with
92   some empty structures.  */
93struct struct_04_01 { ES(es1); struct { struct { tA a; tB b; tA c; tB d; } s1; } s2; };
94struct struct_04_02 { tA a; struct { struct { ES(es1); } s1; } s2; tB b; struct { struct { ES(es1); } s2; } s3; tA c; struct { struct { ES(es2); } s4; } s5; tB d;};
95struct struct_04_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4; struct { struct { tB b; } s5; } s6; struct { struct { tA c; } s7; } s8; struct { struct { tB d; } s9; } s10;};
96struct struct_04_04 { ES(es1); ES(es2); tA a; ES(es3); tB b; ES(es4); tA c; ES(es5); tB d; };
97
98/* Structures with five fields nested to various depths, along with
99   some empty structures.  */
100struct struct_05_01 { ES(es1); struct { struct { tA a; tB b; tA c; tB d; tA e; } s1; } s2; };
101struct struct_05_02 { tA a; struct { struct { ES(es1); } s1; } s2; tB b; struct { struct { ES(es1); } s2; } s3; tA c; struct { struct { ES(es2); } s4; } s5; tB d; struct { struct { ES(es2); } s6; } s7; tB e;};
102struct struct_05_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4; struct { struct { tB b; } s5; } s6; struct { struct { tA c; } s7; } s8; struct { struct { tB d; } s9; } s10; struct { struct { tA e; } s11; } s12;};
103struct struct_05_04 { ES(es1); ES(es2); tA a; ES(es3); tB b; ES(es4); tA c; ES(es5); tB d; ES(es6); tA e; };
104
105/* Only C++ allows structures to have static members.  */
106#ifdef __cplusplus
107
108/* Structures with two fields nested to various depths, one of which is static.
109   Some include empty structures.  */
110struct struct_static_02_01 { struct sa { struct sb { tA a; static tB b; } s1; } s2; };
111struct struct_static_02_02 { static tA a; struct { struct { ES(es1); } s1; } s2; tB b; };
112struct struct_static_02_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4; struct sa { struct sb { static tB b; } s5; } s6;};
113struct struct_static_02_04 { static tA a; tB b; };
114
115/* Structures with four fields nested to various depths, some of which are
116   static.  Some include empty structures.  */
117struct struct_static_04_01 { struct sa { struct sb { static tA a; tB b; tA c; tB d; } s1; } s2; };
118struct struct_static_04_02 { tA a; struct { struct { ES(es1); } s1; } s2; tB b; struct { struct { ES(es1); } s2; } s3; static tA c; struct { struct { ES(es2); } s4; } s5; static tB d;};
119struct struct_static_04_03 { struct sa { struct sb { static tA a; } s3; } s4; struct sc { struct sd { static tB b; } s5; } s6; struct se { struct sf { static tA c; } s7; } s8; struct sg { struct sh { static tB d; } s9; } s10;};
120struct struct_static_04_04 { ES(es1); ES(es2); tA a; ES(es3); tB b; ES(es4); tA c; ES(es5); static tB d; };
121
122/* Structures with six fields nested to various depths, some of which are
123   static.  Some include empty structures.  */
124struct struct_static_06_01 { struct sa { struct sb { tA a; static tB b; tA c; tB d; tA e; } s1; } s2; tB f; };
125struct struct_static_06_02 { tA a; static tB b; static tA c; tB d; tB e; tA f;};
126struct struct_static_06_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct sa { struct sb { static tA a; } s3; } s4; struct sc { struct sd { tB b; } s5; } s6; struct se { struct sf { static tA c; } s7; } s8; struct sg { struct sh { static tB d; } s9; } s10; struct { struct { tA e; tB f; } s11; } s12;};
127struct struct_static_06_04 { ES(es1); ES(es2); static tA a; ES(es3); static tB b; ES(es4); static tA c; ES(es5); static tB d; ES(es6); static tA e; ES(es7); tB f; };
128
129#endif
130
131int __attribute__((noinline,noclone))
132cmp_struct_01_01 (struct struct_01_01 a, struct struct_01_01 b)
133{ return a.s2.s1.a == b.s2.s1.a; }
134
135int __attribute__((noinline,noclone))
136cmp_struct_01_02 (struct struct_01_02 a, struct struct_01_02 b)
137{ return a.a == b.a; }
138
139int __attribute__((noinline,noclone))
140cmp_struct_01_03 (struct struct_01_03 a, struct struct_01_03 b)
141{ return a.s4.s3.a == b.s4.s3.a; }
142
143int __attribute__((noinline,noclone))
144cmp_struct_01_04 (struct struct_01_04 a, struct struct_01_04 b)
145{ return a.a == b.a; }
146
147int __attribute__((noinline,noclone))
148cmp_struct_02_01 (struct struct_02_01 a, struct struct_02_01 b)
149{ return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b; }
150
151int __attribute__((noinline,noclone))
152cmp_struct_02_02 (struct struct_02_02 a, struct struct_02_02 b)
153{ return a.a == b.a && a.b == b.b; }
154
155int __attribute__((noinline,noclone))
156cmp_struct_02_03 (struct struct_02_03 a, struct struct_02_03 b)
157{ return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b; }
158
159int __attribute__((noinline,noclone))
160cmp_struct_02_04 (struct struct_02_04 a, struct struct_02_04 b)
161{ return a.a == b.a && a.b == b.b; }
162
163int __attribute__((noinline,noclone))
164cmp_struct_04_01 (struct struct_04_01 a, struct struct_04_01 b)
165{ return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b
166	 && a.s2.s1.c == b.s2.s1.c && a.s2.s1.d == b.s2.s1.d; }
167
168int __attribute__((noinline,noclone))
169cmp_struct_04_02 (struct struct_04_02 a, struct struct_04_02 b)
170{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d; }
171
172int __attribute__((noinline,noclone))
173cmp_struct_04_03 (struct struct_04_03 a, struct struct_04_03 b)
174{ return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b
175	 && a.s8.s7.c == b.s8.s7.c && a.s10.s9.d == b.s10.s9.d; }
176
177int __attribute__((noinline,noclone))
178cmp_struct_04_04 (struct struct_04_04 a, struct struct_04_04 b)
179{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d; }
180
181int __attribute__((noinline,noclone))
182cmp_struct_05_01 (struct struct_05_01 a, struct struct_05_01 b)
183{ return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b
184	 && a.s2.s1.c == b.s2.s1.c && a.s2.s1.d == b.s2.s1.d
185	 && a.s2.s1.e == b.s2.s1.e; }
186
187int __attribute__((noinline,noclone))
188cmp_struct_05_02 (struct struct_05_02 a, struct struct_05_02 b)
189{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e; }
190
191int __attribute__((noinline,noclone))
192cmp_struct_05_03 (struct struct_05_03 a, struct struct_05_03 b)
193{ return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b
194	 && a.s8.s7.c == b.s8.s7.c && a.s10.s9.d == b.s10.s9.d
195	 && a.s12.s11.e == b.s12.s11.e; }
196
197int __attribute__((noinline,noclone))
198cmp_struct_05_04 (struct struct_05_04 a, struct struct_05_04 b)
199{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e; }
200
201#ifdef __cplusplus
202
203int __attribute__((noinline,noclone))
204cmp_struct_static_02_01 (struct struct_static_02_01 a,
205			 struct struct_static_02_01 b)
206{ return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b; }
207
208int __attribute__((noinline,noclone))
209cmp_struct_static_02_02 (struct struct_static_02_02 a,
210			 struct struct_static_02_02 b)
211{ return a.a == b.a && a.b == b.b; }
212
213int __attribute__((noinline,noclone))
214cmp_struct_static_02_03 (struct struct_static_02_03 a,
215			 struct struct_static_02_03 b)
216{ return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b; }
217
218int __attribute__((noinline,noclone))
219cmp_struct_static_02_04 (struct struct_static_02_04 a,
220			     struct struct_static_02_04 b)
221{ return a.a == b.a && a.b == b.b; }
222
223int __attribute__((noinline,noclone))
224cmp_struct_static_04_01 (struct struct_static_04_01 a,
225			 struct struct_static_04_01 b)
226{ return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b
227	 && a.s2.s1.c == b.s2.s1.c && a.s2.s1.d == b.s2.s1.d; }
228
229int __attribute__((noinline,noclone))
230cmp_struct_static_04_02 (struct struct_static_04_02 a,
231			 struct struct_static_04_02 b)
232{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d; }
233
234int __attribute__((noinline,noclone))
235cmp_struct_static_04_03 (struct struct_static_04_03 a,
236			 struct struct_static_04_03 b)
237{ return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b
238	 && a.s8.s7.c == b.s8.s7.c && a.s10.s9.d == b.s10.s9.d; }
239
240int __attribute__((noinline,noclone))
241cmp_struct_static_04_04 (struct struct_static_04_04 a,
242			 struct struct_static_04_04 b)
243{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d; }
244
245int __attribute__((noinline,noclone))
246cmp_struct_static_06_01 (struct struct_static_06_01 a,
247			 struct struct_static_06_01 b)
248{ return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b
249	 && a.s2.s1.c == b.s2.s1.c && a.s2.s1.d == b.s2.s1.d
250	 && a.s2.s1.e == b.s2.s1.e && a.f == b.f; }
251
252int __attribute__((noinline,noclone))
253cmp_struct_static_06_02 (struct struct_static_06_02 a,
254			 struct struct_static_06_02 b)
255{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e
256	 && a.f == b.f; }
257
258int __attribute__((noinline,noclone))
259cmp_struct_static_06_03 (struct struct_static_06_03 a,
260			 struct struct_static_06_03 b)
261{ return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b
262	 && a.s8.s7.c == b.s8.s7.c && a.s10.s9.d == b.s10.s9.d
263	 && a.s12.s11.e == b.s12.s11.e && a.s12.s11.f == b.s12.s11.f; }
264
265int __attribute__((noinline,noclone))
266cmp_struct_static_06_04 (struct struct_static_06_04 a,
267			 struct struct_static_06_04 b)
268{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e
269	 && a.f == b.f; }
270
271#endif
272
273REF_VAL(struct_01_01) = { {}, { { 'a' } } };
274REF_VAL(struct_01_02) = { 'a', { { {} } } };
275REF_VAL(struct_01_03) = { { { {} } }, {}, { { 'a' } } };
276REF_VAL(struct_01_04) = { {}, {}, 'a', {} };
277
278REF_VAL(struct_02_01) = { {}, { { 'a', 'b' } } };
279REF_VAL(struct_02_02) = { 'a', { { {} } }, 'b' };
280REF_VAL(struct_02_03) = { { { {} } }, {}, { { 'a' } }, { { 'b' } } };
281REF_VAL(struct_02_04) = { {}, {}, 'a', {}, 'b' };
282
283REF_VAL(struct_04_01) = { {}, { { 'a', 'b', 'c', 'd' } } };
284REF_VAL(struct_04_02) = { 'a', { { {} } }, 'b', { { {} } }, 'c', { { {} } }, 'd' };
285REF_VAL(struct_04_03) = { { { {} } }, {}, { { 'a' } }, { { 'b' } }, { { 'c' } }, { { 'd' } } };
286REF_VAL(struct_04_04) = { {}, {}, 'a', {}, 'b', {}, 'c', {}, 'd' };
287
288REF_VAL(struct_05_01) = { {}, { { 'a', 'b', 'c', 'd', 'e' } } };
289REF_VAL(struct_05_02) = { 'a', { { {} } }, 'b', { { {} } }, 'c', { { {} } }, 'd', { { {} } }, 'e' };
290REF_VAL(struct_05_03) = { { { {} } }, {}, { { 'a' } }, { { 'b' } }, { { 'c' } }, { { 'd' } }, { { 'e' } } };
291REF_VAL(struct_05_04) = { {}, {}, 'a', {}, 'b', {}, 'c', {}, 'd', {}, 'e' };
292
293#ifdef __cplusplus
294
295/* Initialise static members.  */
296tB struct_static_02_01::sa::sb::b = '1';
297tA struct_static_02_02::a = '2';
298tB struct_static_02_03::sa::sb::b = '3';
299tA struct_static_02_04::a = '4';
300tA struct_static_04_01::sa::sb::a = '5';
301tA struct_static_04_02::c = '6';
302tB struct_static_04_02::d = '7';
303tA struct_static_04_03::sa::sb::a = '8';
304tB struct_static_04_03::sc::sd::b = '9';
305tA struct_static_04_03::se::sf::c = '0';
306tB struct_static_04_03::sg::sh::d = 'A';
307tB struct_static_04_04::d = 'B';
308tB struct_static_06_01::sa::sb::b = 'C';
309tB struct_static_06_02::b = 'D';
310tA struct_static_06_02::c = 'E';
311tA struct_static_06_03::sa::sb::a = 'F';
312tA struct_static_06_03::se::sf::c = 'G';
313tB struct_static_06_03::sg::sh::d = 'H';
314tA struct_static_06_04::a = 'I';
315tB struct_static_06_04::b = 'J';
316tA struct_static_06_04::c = 'K';
317tB struct_static_06_04::d = 'L';
318tA struct_static_06_04::e = 'M';
319
320REF_VAL(struct_static_02_01) = { { { 'a' } } };
321REF_VAL(struct_static_02_02) = { { { {} } }, 'b' };
322REF_VAL(struct_static_02_03) = { { { {} } }, {}, { { 'a' } }, { { } } };
323REF_VAL(struct_static_02_04) = { 'b' };
324REF_VAL(struct_static_04_01) = { { { 'b', 'c', 'd' } } };
325REF_VAL(struct_static_04_02) = { 'a', { { {} } }, 'b', { { {} } }, { { {} } } };
326REF_VAL(struct_static_04_03) = {};
327REF_VAL(struct_static_04_04) = { {}, {}, 'a', {}, 'b', {}, 'c', {} };
328REF_VAL(struct_static_06_01) = { { { 'a', 'c', 'd', 'e' } }, 'f' };
329REF_VAL(struct_static_06_02) = { 'a', 'd', 'e', 'f' };
330REF_VAL(struct_static_06_03) = { { { {} } }, {}, {}, { { 'b' } }, {}, /*{ { 'e', 'f' } }*/ };
331REF_VAL(struct_static_06_04) = { {}, {}, {}, {}, {}, {}, {}, 'f' };
332
333#endif
334
335/* Create all of the functions GDB will call to check functionality.  */
336MAKE_CHECK_FUNCS(struct_01_01)
337MAKE_CHECK_FUNCS(struct_01_02)
338MAKE_CHECK_FUNCS(struct_01_03)
339MAKE_CHECK_FUNCS(struct_01_04)
340MAKE_CHECK_FUNCS(struct_02_01)
341MAKE_CHECK_FUNCS(struct_02_02)
342MAKE_CHECK_FUNCS(struct_02_03)
343MAKE_CHECK_FUNCS(struct_02_04)
344MAKE_CHECK_FUNCS(struct_04_01)
345MAKE_CHECK_FUNCS(struct_04_02)
346MAKE_CHECK_FUNCS(struct_04_03)
347MAKE_CHECK_FUNCS(struct_04_04)
348MAKE_CHECK_FUNCS(struct_05_01)
349MAKE_CHECK_FUNCS(struct_05_02)
350MAKE_CHECK_FUNCS(struct_05_03)
351MAKE_CHECK_FUNCS(struct_05_04)
352#ifdef __cplusplus
353MAKE_CHECK_FUNCS(struct_static_02_01)
354MAKE_CHECK_FUNCS(struct_static_02_02)
355MAKE_CHECK_FUNCS(struct_static_02_03)
356MAKE_CHECK_FUNCS(struct_static_02_04)
357MAKE_CHECK_FUNCS(struct_static_04_01)
358MAKE_CHECK_FUNCS(struct_static_04_02)
359MAKE_CHECK_FUNCS(struct_static_04_03)
360MAKE_CHECK_FUNCS(struct_static_04_04)
361MAKE_CHECK_FUNCS(struct_static_06_01)
362MAKE_CHECK_FUNCS(struct_static_06_02)
363MAKE_CHECK_FUNCS(struct_static_06_03)
364MAKE_CHECK_FUNCS(struct_static_06_04)
365#endif
366
367#define CALL_LINE(NAME) val += check_arg_ ## NAME (rtn_str_ ## NAME ())
368
369int __attribute__((noinline,noclone))
370call_all ()
371{
372  int val = 0;
373
374  CALL_LINE(struct_01_01);
375  CALL_LINE(struct_01_02);
376  CALL_LINE(struct_01_03);
377  CALL_LINE(struct_01_04);
378  CALL_LINE(struct_02_01);
379  CALL_LINE(struct_02_02);
380  CALL_LINE(struct_02_03);
381  CALL_LINE(struct_02_04);
382  CALL_LINE(struct_04_01);
383  CALL_LINE(struct_04_02);
384  CALL_LINE(struct_04_03);
385  CALL_LINE(struct_04_04);
386  CALL_LINE(struct_05_01);
387  CALL_LINE(struct_05_02);
388  CALL_LINE(struct_05_03);
389  CALL_LINE(struct_05_04);
390#ifdef __cplusplus
391  CALL_LINE(struct_static_02_01);
392  CALL_LINE(struct_static_02_02);
393  CALL_LINE(struct_static_02_03);
394  CALL_LINE(struct_static_02_04);
395  CALL_LINE(struct_static_04_01);
396  CALL_LINE(struct_static_04_02);
397  CALL_LINE(struct_static_04_03);
398  CALL_LINE(struct_static_04_04);
399  CALL_LINE(struct_static_06_01);
400  CALL_LINE(struct_static_06_02);
401  CALL_LINE(struct_static_06_03);
402  CALL_LINE(struct_static_06_04);
403#endif
404
405  return val;
406}
407
408int volatile v = 1;
409
410void __attribute__((noinline, noclone))
411breakpt (void)
412{
413  v++;
414}
415
416int
417main ()
418{
419  int res;
420
421  res = call_all ();
422  breakpt (); /* Break Here.  */
423  return res;
424}
425