1/* Structure layout test generator.
2   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3   Contributed by Jakub Jelinek <jakub@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING.  If not, write to the Free
19Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2002110-1301, USA.  */
21
22/* Compile with gcc -o struct-layout-1_generate{,.c} generate_random{,_r}.c */
23
24/* N.B. -- This program cannot use libiberty as that will not work
25   when testing an installed compiler.  */
26#include <limits.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stddef.h>
31/* We use our own pseudo-random number generator, so that it gives the same
32   values on all hosts.  */
33#include "generate-random.h"
34
35#if LLONG_MAX != 9223372036854775807LL && __LONG_LONG_MAX__ != 9223372036854775807LL
36# error Need 64-bit long long
37#endif
38
39typedef unsigned int hashval_t;
40
41enum TYPE
42{
43  TYPE_INT,
44  TYPE_UINT,
45  TYPE_CINT,
46  TYPE_CUINT,
47  TYPE_FLOAT,
48  TYPE_CFLOAT,
49  TYPE_SENUM,
50  TYPE_UENUM,
51  TYPE_PTR,
52  TYPE_FNPTR,
53  TYPE_OTHER
54};
55
56struct types
57{
58  const char *name;
59  enum TYPE type;
60  unsigned long long int maxval;
61  char bitfld;
62};
63
64struct types base_types[] = {
65/* As we don't know whether char will be signed or not, just limit ourselves
66   to unsigned values less than maximum signed char value.  */
67{ "char", TYPE_UINT, 127, 'C' },
68{ "signed char", TYPE_INT, 127, 'C' },
69{ "unsigned char", TYPE_UINT, 255, 'C' },
70{ "short int", TYPE_INT, 32767, 'S' },
71{ "unsigned short int", TYPE_UINT, 65535, 'S' },
72{ "int", TYPE_INT, 2147483647, 'I' },
73{ "unsigned int", TYPE_UINT, 4294967295U, 'I' },
74{ "long int", TYPE_INT, 9223372036854775807LL, 'L' },
75{ "unsigned long int", TYPE_UINT, 18446744073709551615ULL, 'L' },
76{ "long long int", TYPE_INT, 9223372036854775807LL, 'Q' },
77{ "unsigned long long int", TYPE_UINT, 18446744073709551615ULL, 'Q' },
78{ "bool", TYPE_UINT, 1, 'B' },
79{ "void *", TYPE_PTR, 0, 0 },
80{ "char *", TYPE_PTR, 0, 0 },
81{ "int *", TYPE_PTR, 0, 0 },
82{ "float", TYPE_FLOAT, 0, 0 },
83{ "double", TYPE_FLOAT, 0, 0 },
84{ "long double", TYPE_FLOAT, 0, 0 },
85#define NTYPES1 18
86{ "Tchar", TYPE_UINT, 127, 'C' },
87{ "Tschar", TYPE_INT, 127, 'C' },
88{ "Tuchar", TYPE_UINT, 255, 'C' },
89{ "Tshort", TYPE_INT, 32767, 'S' },
90{ "Tushort", TYPE_UINT, 65535, 'S' },
91{ "Tint", TYPE_INT, 2147483647, 'I' },
92{ "Tuint", TYPE_UINT, 4294967295U, 'I' },
93{ "Tlong", TYPE_INT, 9223372036854775807LL, 'L' },
94{ "Tulong", TYPE_UINT, 18446744073709551615ULL, 'L' },
95{ "Tllong", TYPE_INT, 9223372036854775807LL, 'Q' },
96{ "Tullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },
97{ "Tbool", TYPE_UINT, 1, 'B' },
98{ "size_t", TYPE_UINT, 18446744073709551615ULL, 0 },
99{ "Tptr", TYPE_PTR, 0, 0 },
100{ "Tcptr", TYPE_PTR, 0, 0 },
101{ "Tiptr", TYPE_PTR, 0, 0 },
102{ "Tfnptr", TYPE_FNPTR, 0, 0 },
103{ "Tfloat", TYPE_FLOAT, 0, 0 },
104{ "Tdouble", TYPE_FLOAT, 0, 0 },
105{ "Tldouble", TYPE_FLOAT, 0, 0 },
106{ "enum E0", TYPE_UENUM, 0, ' ' },
107{ "enum E1", TYPE_UENUM, 1, ' ' },
108{ "enum E2", TYPE_SENUM, 3, ' ' },
109{ "enum E3", TYPE_SENUM, 127, ' ' },
110{ "enum E4", TYPE_UENUM, 255, ' ' },
111{ "enum E5", TYPE_SENUM, 32767, ' ' },
112{ "enum E6", TYPE_UENUM, 65535, ' ' },
113{ "enum E7", TYPE_SENUM, 2147483647, ' ' },
114{ "enum E8", TYPE_UENUM, 4294967295U, ' ' },
115{ "enum E9", TYPE_SENUM, 1099511627775LL, ' ' },
116{ "TE0", TYPE_UENUM, 0, ' ' },
117{ "TE1", TYPE_UENUM, 1, ' ' },
118{ "TE2", TYPE_SENUM, 3, ' ' },
119{ "TE3", TYPE_SENUM, 127, ' ' },
120{ "TE4", TYPE_UENUM, 255, ' ' },
121{ "TE5", TYPE_SENUM, 32767, ' ' },
122{ "TE6", TYPE_UENUM, 65535, ' ' },
123{ "TE7", TYPE_SENUM, 2147483647, ' ' },
124{ "TE8", TYPE_UENUM, 4294967295U, ' ' },
125{ "TE9", TYPE_SENUM, 1099511627775LL, ' ' },
126/* vector-defs.h typedefs */
127{ "qi", TYPE_INT, 127, 0 },
128{ "hi", TYPE_INT, 32767, 0 },
129{ "si", TYPE_INT, 2147483647, 0 },
130{ "di", TYPE_INT, 9223372036854775807LL, 0 },
131{ "sf", TYPE_FLOAT, 0, 0 },
132{ "df", TYPE_FLOAT, 0, 0 }
133#define NTYPES2 (sizeof (base_types) / sizeof (base_types[0]))
134};
135struct types complex_types[] = {
136{ "_Complex char", TYPE_CUINT, 127, 0 },
137{ "_Complex signed char", TYPE_CINT, 127, 0 },
138{ "_Complex unsigned char", TYPE_CUINT, 255, 0 },
139{ "_Complex short int", TYPE_CINT, 32767, 0 },
140{ "_Complex unsigned short int", TYPE_CUINT, 65535, 0 },
141{ "_Complex int", TYPE_CINT, 2147483647, 0 },
142{ "_Complex unsigned int", TYPE_CUINT, 4294967295U, 0 },
143{ "_Complex long int", TYPE_CINT, 9223372036854775807LL, 0 },
144{ "_Complex unsigned long int", TYPE_CUINT, 18446744073709551615ULL, 0 },
145{ "_Complex long long int", TYPE_CINT, 9223372036854775807LL, 0 },
146{ "_Complex unsigned long long int", TYPE_CUINT, 18446744073709551615ULL, 0 },
147{ "_Complex float", TYPE_CFLOAT, 0, 0 },
148{ "_Complex double", TYPE_CFLOAT, 0, 0 },
149{ "_Complex long double", TYPE_CFLOAT, 0, 0 },
150{ "Tcchar", TYPE_CUINT, 127, 0 },
151{ "Tcschar", TYPE_CINT, 127, 0 },
152{ "Tcuchar", TYPE_CUINT, 255, 0 },
153{ "Tcshort", TYPE_CINT, 32767, 0 },
154{ "Tcushort", TYPE_CUINT, 65535, 0 },
155{ "Tcint", TYPE_CINT, 2147483647, 0 },
156{ "Tcuint", TYPE_CUINT, 4294967295U, 0 },
157{ "Tclong", TYPE_CINT, 9223372036854775807LL, 0 },
158{ "Tculong", TYPE_CUINT, 18446744073709551615ULL, 0 },
159{ "Tcllong", TYPE_CINT, 9223372036854775807LL, 0 },
160{ "Tcullong", TYPE_CUINT, 18446744073709551615ULL, 0 },
161{ "Tcfloat", TYPE_CFLOAT, 0, 0 },
162{ "Tcdouble", TYPE_CFLOAT, 0, 0 },
163{ "Tcldouble", TYPE_CFLOAT, 0, 0 }
164#define NCTYPES2 (sizeof (complex_types) / sizeof (complex_types[0]))
165};
166struct types vector_types[] = {
167/* vector-defs.h typedefs */
168{ "v8qi", TYPE_OTHER, 0, 0 },
169{ "v16qi", TYPE_OTHER, 0, 0 },
170{ "v2hi", TYPE_OTHER, 0, 0 },
171{ "v4hi", TYPE_OTHER, 0, 0 },
172{ "v8hi", TYPE_OTHER, 0, 0 },
173{ "v2si", TYPE_OTHER, 0, 0 },
174{ "v4si", TYPE_OTHER, 0, 0 },
175{ "v1di", TYPE_OTHER, 0, 0 },
176{ "v2di", TYPE_OTHER, 0, 0 },
177{ "v2sf", TYPE_OTHER, 0, 0 },
178{ "v4sf", TYPE_OTHER, 0, 0 },
179{ "v16sf", TYPE_OTHER, 0, 0 },
180{ "v2df", TYPE_OTHER, 0, 0 },
181{ "u8qi", TYPE_OTHER, 0, 0 },
182{ "u16qi", TYPE_OTHER, 0, 0 },
183{ "u2hi", TYPE_OTHER, 0, 0 },
184{ "u4hi", TYPE_OTHER, 0, 0 },
185{ "u8hi", TYPE_OTHER, 0, 0 },
186{ "u2si", TYPE_OTHER, 0, 0 },
187{ "u4si", TYPE_OTHER, 0, 0 },
188{ "u1di", TYPE_OTHER, 0, 0 },
189{ "u2di", TYPE_OTHER, 0, 0 },
190{ "u2sf", TYPE_OTHER, 0, 0 },
191{ "u4sf", TYPE_OTHER, 0, 0 },
192{ "u16sf", TYPE_OTHER, 0, 0 },
193{ "u2df", TYPE_OTHER, 0, 0 },
194{ "__m64", TYPE_OTHER, 0, 0 },
195{ "__m128", TYPE_OTHER, 0, 0 }
196#define NVTYPES2 (sizeof (vector_types) / sizeof (vector_types[0]))
197};
198struct types attrib_types[] = {
199{ "Talchar", TYPE_UINT, 127, 'C' },
200{ "Talschar", TYPE_INT, 127, 'C' },
201{ "Taluchar", TYPE_UINT, 255, 'C' },
202{ "Talshort", TYPE_INT, 32767, 'S' },
203{ "Talushort", TYPE_UINT, 65535, 'S' },
204{ "Talint", TYPE_INT, 2147483647, 'I' },
205{ "Taluint", TYPE_UINT, 4294967295U, 'I' },
206{ "Tallong", TYPE_INT, 9223372036854775807LL, 'L' },
207{ "Talulong", TYPE_UINT, 18446744073709551615ULL, 'L' },
208{ "Talllong", TYPE_INT, 9223372036854775807LL, 'Q' },
209{ "Talullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },
210{ "Talbool", TYPE_UINT, 1, 'B' },
211{ "Talptr", TYPE_PTR, 0, 0 },
212{ "Talcptr", TYPE_PTR, 0, 0 },
213{ "Taliptr", TYPE_PTR, 0, 0 },
214{ "Talfloat", TYPE_FLOAT, 0, 0 },
215{ "Taldouble", TYPE_FLOAT, 0, 0 },
216{ "Talldouble", TYPE_FLOAT, 0, 0 },
217{ "TalE0", TYPE_UENUM, 0, ' ' },
218{ "TalE1", TYPE_UENUM, 1, ' ' },
219{ "TalE2", TYPE_SENUM, 3, ' ' },
220{ "TalE3", TYPE_SENUM, 127, ' ' },
221{ "TalE4", TYPE_UENUM, 255, ' ' },
222{ "TalE5", TYPE_SENUM, 32767, ' ' },
223{ "TalE6", TYPE_UENUM, 65535, ' ' },
224{ "TalE7", TYPE_SENUM, 2147483647, ' ' },
225{ "TalE8", TYPE_UENUM, 4294967295U, ' ' },
226{ "TalE9", TYPE_SENUM, 1099511627775LL, ' ' },
227{ "Tal1char", TYPE_UINT, 127, 'C' },
228{ "Tal1schar", TYPE_INT, 127, 'C' },
229{ "Tal1uchar", TYPE_UINT, 255, 'C' },
230{ "Tal1short", TYPE_INT, 32767, 'S' },
231{ "Tal1ushort", TYPE_UINT, 65535, 'S' },
232{ "Tal1int", TYPE_INT, 2147483647, 'I' },
233{ "Tal1uint", TYPE_UINT, 4294967295U, 'I' },
234{ "Tal1long", TYPE_INT, 9223372036854775807LL, 'L' },
235{ "Tal1ulong", TYPE_UINT, 18446744073709551615ULL, 'L' },
236{ "Tal1llong", TYPE_INT, 9223372036854775807LL, 'Q' },
237{ "Tal1ullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },
238{ "Tal1bool", TYPE_UINT, 1, 'B' },
239{ "Tal1ptr", TYPE_PTR, 0, 0 },
240{ "Tal1cptr", TYPE_PTR, 0, 0 },
241{ "Tal1iptr", TYPE_PTR, 0, 0 },
242{ "Tal1float", TYPE_FLOAT, 0, 0 },
243{ "Tal1double", TYPE_FLOAT, 0, 0 },
244{ "Tal1ldouble", TYPE_FLOAT, 0, 0 },
245{ "Tal1E0", TYPE_UENUM, 0, ' ' },
246{ "Tal1E1", TYPE_UENUM, 1, ' ' },
247{ "Tal1E2", TYPE_SENUM, 3, ' ' },
248{ "Tal1E3", TYPE_SENUM, 127, ' ' },
249{ "Tal1E4", TYPE_UENUM, 255, ' ' },
250{ "Tal1E5", TYPE_SENUM, 32767, ' ' },
251{ "Tal1E6", TYPE_UENUM, 65535, ' ' },
252{ "Tal1E7", TYPE_SENUM, 2147483647, ' ' },
253{ "Tal1E8", TYPE_UENUM, 4294967295U, ' ' },
254{ "Tal1E9", TYPE_SENUM, 1099511627775LL, ' ' },
255{ "Tal2char", TYPE_UINT, 127, 'C' },
256{ "Tal2schar", TYPE_INT, 127, 'C' },
257{ "Tal2uchar", TYPE_UINT, 255, 'C' },
258{ "Tal2short", TYPE_INT, 32767, 'S' },
259{ "Tal2ushort", TYPE_UINT, 65535, 'S' },
260{ "Tal2int", TYPE_INT, 2147483647, 'I' },
261{ "Tal2uint", TYPE_UINT, 4294967295U, 'I' },
262{ "Tal2long", TYPE_INT, 9223372036854775807LL, 'L' },
263{ "Tal2ulong", TYPE_UINT, 18446744073709551615ULL, 'L' },
264{ "Tal2llong", TYPE_INT, 9223372036854775807LL, 'Q' },
265{ "Tal2ullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },
266{ "Tal2bool", TYPE_UINT, 1, 'B' },
267{ "Tal2ptr", TYPE_PTR, 0, 0 },
268{ "Tal2cptr", TYPE_PTR, 0, 0 },
269{ "Tal2iptr", TYPE_PTR, 0, 0 },
270{ "Tal2float", TYPE_FLOAT, 0, 0 },
271{ "Tal2double", TYPE_FLOAT, 0, 0 },
272{ "Tal2ldouble", TYPE_FLOAT, 0, 0 },
273{ "Tal2E0", TYPE_UENUM, 0, ' ' },
274{ "Tal2E1", TYPE_UENUM, 1, ' ' },
275{ "Tal2E2", TYPE_SENUM, 3, ' ' },
276{ "Tal2E3", TYPE_SENUM, 127, ' ' },
277{ "Tal2E4", TYPE_UENUM, 255, ' ' },
278{ "Tal2E5", TYPE_SENUM, 32767, ' ' },
279{ "Tal2E6", TYPE_UENUM, 65535, ' ' },
280{ "Tal2E7", TYPE_SENUM, 2147483647, ' ' },
281{ "Tal2E8", TYPE_UENUM, 4294967295U, ' ' },
282{ "Tal2E9", TYPE_SENUM, 1099511627775LL, ' ' },
283{ "Tal4char", TYPE_UINT, 127, 'C' },
284{ "Tal4schar", TYPE_INT, 127, 'C' },
285{ "Tal4uchar", TYPE_UINT, 255, 'C' },
286{ "Tal4short", TYPE_INT, 32767, 'S' },
287{ "Tal4ushort", TYPE_UINT, 65535, 'S' },
288{ "Tal4int", TYPE_INT, 2147483647, 'I' },
289{ "Tal4uint", TYPE_UINT, 4294967295U, 'I' },
290{ "Tal4long", TYPE_INT, 9223372036854775807LL, 'L' },
291{ "Tal4ulong", TYPE_UINT, 18446744073709551615ULL, 'L' },
292{ "Tal4llong", TYPE_INT, 9223372036854775807LL, 'Q' },
293{ "Tal4ullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },
294{ "Tal4bool", TYPE_UINT, 1, 'B' },
295{ "Tal4ptr", TYPE_PTR, 0, 0 },
296{ "Tal4cptr", TYPE_PTR, 0, 0 },
297{ "Tal4iptr", TYPE_PTR, 0, 0 },
298{ "Tal4float", TYPE_FLOAT, 0, 0 },
299{ "Tal4double", TYPE_FLOAT, 0, 0 },
300{ "Tal4ldouble", TYPE_FLOAT, 0, 0 },
301{ "Tal4E0", TYPE_UENUM, 0, ' ' },
302{ "Tal4E1", TYPE_UENUM, 1, ' ' },
303{ "Tal4E2", TYPE_SENUM, 3, ' ' },
304{ "Tal4E3", TYPE_SENUM, 127, ' ' },
305{ "Tal4E4", TYPE_UENUM, 255, ' ' },
306{ "Tal4E5", TYPE_SENUM, 32767, ' ' },
307{ "Tal4E6", TYPE_UENUM, 65535, ' ' },
308{ "Tal4E7", TYPE_SENUM, 2147483647, ' ' },
309{ "Tal4E8", TYPE_UENUM, 4294967295U, ' ' },
310{ "Tal4E9", TYPE_SENUM, 1099511627775LL, ' ' },
311{ "Tal8char", TYPE_UINT, 127, 'C' },
312{ "Tal8schar", TYPE_INT, 127, 'C' },
313{ "Tal8uchar", TYPE_UINT, 255, 'C' },
314{ "Tal8short", TYPE_INT, 32767, 'S' },
315{ "Tal8ushort", TYPE_UINT, 65535, 'S' },
316{ "Tal8int", TYPE_INT, 2147483647, 'I' },
317{ "Tal8uint", TYPE_UINT, 4294967295U, 'I' },
318{ "Tal8long", TYPE_INT, 9223372036854775807LL, 'L' },
319{ "Tal8ulong", TYPE_UINT, 18446744073709551615ULL, 'L' },
320{ "Tal8llong", TYPE_INT, 9223372036854775807LL, 'Q' },
321{ "Tal8ullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },
322{ "Tal8bool", TYPE_UINT, 1, 'B' },
323{ "Tal8ptr", TYPE_PTR, 0, 0 },
324{ "Tal8cptr", TYPE_PTR, 0, 0 },
325{ "Tal8iptr", TYPE_PTR, 0, 0 },
326{ "Tal8float", TYPE_FLOAT, 0, 0 },
327{ "Tal8double", TYPE_FLOAT, 0, 0 },
328{ "Tal8ldouble", TYPE_FLOAT, 0, 0 },
329{ "Tal8E0", TYPE_UENUM, 0, ' ' },
330{ "Tal8E1", TYPE_UENUM, 1, ' ' },
331{ "Tal8E2", TYPE_SENUM, 3, ' ' },
332{ "Tal8E3", TYPE_SENUM, 127, ' ' },
333{ "Tal8E4", TYPE_UENUM, 255, ' ' },
334{ "Tal8E5", TYPE_SENUM, 32767, ' ' },
335{ "Tal8E6", TYPE_UENUM, 65535, ' ' },
336{ "Tal8E7", TYPE_SENUM, 2147483647, ' ' },
337{ "Tal8E8", TYPE_UENUM, 4294967295U, ' ' },
338{ "Tal8E9", TYPE_SENUM, 1099511627775LL, ' ' },
339{ "Tal16char", TYPE_UINT, 127, 'C' },
340{ "Tal16schar", TYPE_INT, 127, 'C' },
341{ "Tal16uchar", TYPE_UINT, 255, 'C' },
342{ "Tal16short", TYPE_INT, 32767, 'S' },
343{ "Tal16ushort", TYPE_UINT, 65535, 'S' },
344{ "Tal16int", TYPE_INT, 2147483647, 'I' },
345{ "Tal16uint", TYPE_UINT, 4294967295U, 'I' },
346{ "Tal16long", TYPE_INT, 9223372036854775807LL, 'L' },
347{ "Tal16ulong", TYPE_UINT, 18446744073709551615ULL, 'L' },
348{ "Tal16llong", TYPE_INT, 9223372036854775807LL, 'Q' },
349{ "Tal16ullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },
350{ "Tal16bool", TYPE_UINT, 1, 'B' },
351{ "Tal16ptr", TYPE_PTR, 0, 0 },
352{ "Tal16cptr", TYPE_PTR, 0, 0 },
353{ "Tal16iptr", TYPE_PTR, 0, 0 },
354{ "Tal16float", TYPE_FLOAT, 0, 0 },
355{ "Tal16double", TYPE_FLOAT, 0, 0 },
356{ "Tal16ldouble", TYPE_FLOAT, 0, 0 },
357{ "Tal16E0", TYPE_UENUM, 0, ' ' },
358{ "Tal16E1", TYPE_UENUM, 1, ' ' },
359{ "Tal16E2", TYPE_SENUM, 3, ' ' },
360{ "Tal16E3", TYPE_SENUM, 127, ' ' },
361{ "Tal16E4", TYPE_UENUM, 255, ' ' },
362{ "Tal16E5", TYPE_SENUM, 32767, ' ' },
363{ "Tal16E6", TYPE_UENUM, 65535, ' ' },
364{ "Tal16E7", TYPE_SENUM, 2147483647, ' ' },
365{ "Tal16E8", TYPE_UENUM, 4294967295U, ' ' },
366{ "Tal16E9", TYPE_SENUM, 1099511627775LL, ' ' }
367#define NATYPES2 (sizeof (attrib_types) / sizeof (attrib_types[0]))
368};
369struct types complex_attrib_types[] = {
370{ "Talcchar", TYPE_CUINT, 127, 0 },
371{ "Talcschar", TYPE_CINT, 127, 0 },
372{ "Talcuchar", TYPE_CUINT, 255, 0 },
373{ "Talcshort", TYPE_CINT, 32767, 0 },
374{ "Talcushort", TYPE_CUINT, 65535, 0 },
375{ "Talcint", TYPE_CINT, 2147483647, 0 },
376{ "Talcuint", TYPE_CUINT, 4294967295U, 0 },
377{ "Talclong", TYPE_CINT, 9223372036854775807LL, 0 },
378{ "Talculong", TYPE_CUINT, 18446744073709551615ULL, 0 },
379{ "Talcllong", TYPE_CINT, 9223372036854775807LL, 0 },
380{ "Talcullong", TYPE_CUINT, 18446744073709551615ULL, 0 },
381{ "Talcfloat", TYPE_CFLOAT, 0, 0 },
382{ "Talcdouble", TYPE_CFLOAT, 0, 0 },
383{ "Talcldouble", TYPE_CFLOAT, 0, 0 },
384{ "Tal1cchar", TYPE_CUINT, 127, 0 },
385{ "Tal1cschar", TYPE_CINT, 127, 0 },
386{ "Tal1cuchar", TYPE_CUINT, 255, 0 },
387{ "Tal1cshort", TYPE_CINT, 32767, 0 },
388{ "Tal1cushort", TYPE_CUINT, 65535, 0 },
389{ "Tal1cint", TYPE_CINT, 2147483647, 0 },
390{ "Tal1cuint", TYPE_CUINT, 4294967295U, 0 },
391{ "Tal1clong", TYPE_CINT, 9223372036854775807LL, 0 },
392{ "Tal1culong", TYPE_CUINT, 18446744073709551615ULL, 0 },
393{ "Tal1cllong", TYPE_CINT, 9223372036854775807LL, 0 },
394{ "Tal1cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },
395{ "Tal1cfloat", TYPE_CFLOAT, 0, 0 },
396{ "Tal1cdouble", TYPE_CFLOAT, 0, 0 },
397{ "Tal1cldouble", TYPE_CFLOAT, 0, 0 },
398{ "Tal2cchar", TYPE_CUINT, 127, 0 },
399{ "Tal2cschar", TYPE_CINT, 127, 0 },
400{ "Tal2cuchar", TYPE_CUINT, 255, 0 },
401{ "Tal2cshort", TYPE_CINT, 32767, 0 },
402{ "Tal2cushort", TYPE_CUINT, 65535, 0 },
403{ "Tal2cint", TYPE_CINT, 2147483647, 0 },
404{ "Tal2cuint", TYPE_CUINT, 4294967295U, 0 },
405{ "Tal2clong", TYPE_CINT, 9223372036854775807LL, 0 },
406{ "Tal2culong", TYPE_CUINT, 18446744073709551615ULL, 0 },
407{ "Tal2cllong", TYPE_CINT, 9223372036854775807LL, 0 },
408{ "Tal2cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },
409{ "Tal2cfloat", TYPE_CFLOAT, 0, 0 },
410{ "Tal2cdouble", TYPE_CFLOAT, 0, 0 },
411{ "Tal2cldouble", TYPE_CFLOAT, 0, 0 },
412{ "Tal4cchar", TYPE_CUINT, 127, 0 },
413{ "Tal4cschar", TYPE_CINT, 127, 0 },
414{ "Tal4cuchar", TYPE_CUINT, 255, 0 },
415{ "Tal4cshort", TYPE_CINT, 32767, 0 },
416{ "Tal4cushort", TYPE_CUINT, 65535, 0 },
417{ "Tal4cint", TYPE_CINT, 2147483647, 0 },
418{ "Tal4cuint", TYPE_CUINT, 4294967295U, 0 },
419{ "Tal4clong", TYPE_CINT, 9223372036854775807LL, 0 },
420{ "Tal4culong", TYPE_CUINT, 18446744073709551615ULL, 0 },
421{ "Tal4cllong", TYPE_CINT, 9223372036854775807LL, 0 },
422{ "Tal4cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },
423{ "Tal4cfloat", TYPE_CFLOAT, 0, 0 },
424{ "Tal4cdouble", TYPE_CFLOAT, 0, 0 },
425{ "Tal4cldouble", TYPE_CFLOAT, 0, 0 },
426{ "Tal8cchar", TYPE_CUINT, 127, 0 },
427{ "Tal8cschar", TYPE_CINT, 127, 0 },
428{ "Tal8cuchar", TYPE_CUINT, 255, 0 },
429{ "Tal8cshort", TYPE_CINT, 32767, 0 },
430{ "Tal8cushort", TYPE_CUINT, 65535, 0 },
431{ "Tal8cint", TYPE_CINT, 2147483647, 0 },
432{ "Tal8cuint", TYPE_CUINT, 4294967295U, 0 },
433{ "Tal8clong", TYPE_CINT, 9223372036854775807LL, 0 },
434{ "Tal8culong", TYPE_CUINT, 18446744073709551615ULL, 0 },
435{ "Tal8cllong", TYPE_CINT, 9223372036854775807LL, 0 },
436{ "Tal8cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },
437{ "Tal8cfloat", TYPE_CFLOAT, 0, 0 },
438{ "Tal8cdouble", TYPE_CFLOAT, 0, 0 },
439{ "Tal8cldouble", TYPE_CFLOAT, 0, 0 },
440{ "Tal16cchar", TYPE_CUINT, 127, 0 },
441{ "Tal16cschar", TYPE_CINT, 127, 0 },
442{ "Tal16cuchar", TYPE_CUINT, 255, 0 },
443{ "Tal16cshort", TYPE_CINT, 32767, 0 },
444{ "Tal16cushort", TYPE_CUINT, 65535, 0 },
445{ "Tal16cint", TYPE_CINT, 2147483647, 0 },
446{ "Tal16cuint", TYPE_CUINT, 4294967295U, 0 },
447{ "Tal16clong", TYPE_CINT, 9223372036854775807LL, 0 },
448{ "Tal16culong", TYPE_CUINT, 18446744073709551615ULL, 0 },
449{ "Tal16cllong", TYPE_CINT, 9223372036854775807LL, 0 },
450{ "Tal16cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },
451{ "Tal16cfloat", TYPE_CFLOAT, 0, 0 },
452{ "Tal16cdouble", TYPE_CFLOAT, 0, 0 },
453{ "Tal16cldouble", TYPE_CFLOAT, 0, 0 }
454#define NCATYPES2 (sizeof (complex_attrib_types) / sizeof (complex_attrib_types[0]))
455};
456struct types attrib_array_types[] = {
457{ "Talx1char", TYPE_UINT, 127, 'C' },
458{ "Talx1schar", TYPE_INT, 127, 'C' },
459{ "Talx1uchar", TYPE_UINT, 255, 'C' },
460{ "Talx1short", TYPE_INT, 32767, 'S' },
461{ "Talx1ushort", TYPE_UINT, 65535, 'S' },
462{ "Talx1int", TYPE_INT, 2147483647, 'I' },
463{ "Talx1uint", TYPE_UINT, 4294967295U, 'I' },
464{ "Talx1long", TYPE_INT, 9223372036854775807LL, 'L' },
465{ "Talx1ulong", TYPE_UINT, 18446744073709551615ULL, 'L' },
466{ "Talx1llong", TYPE_INT, 9223372036854775807LL, 'Q' },
467{ "Talx1ullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },
468{ "Talx1bool", TYPE_UINT, 1, 'B' },
469{ "Talx1ptr", TYPE_PTR, 0, 0 },
470{ "Talx1cptr", TYPE_PTR, 0, 0 },
471{ "Talx1iptr", TYPE_PTR, 0, 0 },
472{ "Talx1float", TYPE_FLOAT, 0, 0 },
473{ "Talx1double", TYPE_FLOAT, 0, 0 },
474{ "Talx1ldouble", TYPE_FLOAT, 0, 0 },
475{ "Talx1E0", TYPE_UENUM, 0, ' ' },
476{ "Talx1E1", TYPE_UENUM, 1, ' ' },
477{ "Talx1E2", TYPE_SENUM, 3, ' ' },
478{ "Talx1E3", TYPE_SENUM, 127, ' ' },
479{ "Talx1E4", TYPE_UENUM, 255, ' ' },
480{ "Talx1E5", TYPE_SENUM, 32767, ' ' },
481{ "Talx1E6", TYPE_UENUM, 65535, ' ' },
482{ "Talx1E7", TYPE_SENUM, 2147483647, ' ' },
483{ "Talx1E8", TYPE_UENUM, 4294967295U, ' ' },
484{ "Talx1E9", TYPE_SENUM, 1099511627775LL, ' ' },
485{ "Talx2short", TYPE_INT, 32767, 'S' },
486{ "Talx2ushort", TYPE_UINT, 65535, 'S' },
487{ "Talx2int", TYPE_INT, 2147483647, 'I' },
488{ "Talx2uint", TYPE_UINT, 4294967295U, 'I' },
489{ "Talx2long", TYPE_INT, 9223372036854775807LL, 'L' },
490{ "Talx2ulong", TYPE_UINT, 18446744073709551615ULL, 'L' },
491{ "Talx2llong", TYPE_INT, 9223372036854775807LL, 'Q' },
492{ "Talx2ullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },
493{ "Talx2ptr", TYPE_PTR, 0, 0 },
494{ "Talx2cptr", TYPE_PTR, 0, 0 },
495{ "Talx2iptr", TYPE_PTR, 0, 0 },
496{ "Talx2float", TYPE_FLOAT, 0, 0 },
497{ "Talx2double", TYPE_FLOAT, 0, 0 },
498{ "Talx2ldouble", TYPE_FLOAT, 0, 0 },
499{ "Talx2E0", TYPE_UENUM, 0, ' ' },
500{ "Talx2E1", TYPE_UENUM, 1, ' ' },
501{ "Talx2E2", TYPE_SENUM, 3, ' ' },
502{ "Talx2E3", TYPE_SENUM, 127, ' ' },
503{ "Talx2E4", TYPE_UENUM, 255, ' ' },
504{ "Talx2E5", TYPE_SENUM, 32767, ' ' },
505{ "Talx2E6", TYPE_UENUM, 65535, ' ' },
506{ "Talx2E7", TYPE_SENUM, 2147483647, ' ' },
507{ "Talx2E8", TYPE_UENUM, 4294967295U, ' ' },
508{ "Talx2E9", TYPE_SENUM, 1099511627775LL, ' ' },
509{ "Talx4int", TYPE_INT, 2147483647, 'I' },
510{ "Talx4uint", TYPE_UINT, 4294967295U, 'I' },
511{ "Talx4long", TYPE_INT, 9223372036854775807LL, 'L' },
512{ "Talx4ulong", TYPE_UINT, 18446744073709551615ULL, 'L' },
513{ "Talx4llong", TYPE_INT, 9223372036854775807LL, 'Q' },
514{ "Talx4ullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },
515{ "Talx4ptr", TYPE_PTR, 0, 0 },
516{ "Talx4cptr", TYPE_PTR, 0, 0 },
517{ "Talx4iptr", TYPE_PTR, 0, 0 },
518{ "Talx4float", TYPE_FLOAT, 0, 0 },
519{ "Talx4double", TYPE_FLOAT, 0, 0 },
520{ "Talx4ldouble", TYPE_FLOAT, 0, 0 },
521{ "Talx4E0", TYPE_UENUM, 0, ' ' },
522{ "Talx4E1", TYPE_UENUM, 1, ' ' },
523{ "Talx4E2", TYPE_SENUM, 3, ' ' },
524{ "Talx4E3", TYPE_SENUM, 127, ' ' },
525{ "Talx4E4", TYPE_UENUM, 255, ' ' },
526{ "Talx4E5", TYPE_SENUM, 32767, ' ' },
527{ "Talx4E6", TYPE_UENUM, 65535, ' ' },
528{ "Talx4E7", TYPE_SENUM, 2147483647, ' ' },
529{ "Talx4E8", TYPE_UENUM, 4294967295U, ' ' },
530{ "Talx4E9", TYPE_SENUM, 1099511627775LL, ' ' },
531{ "Taly8long", TYPE_INT, 9223372036854775807LL, 'L' },
532{ "Taly8ulong", TYPE_UINT, 18446744073709551615ULL, 'L' },
533{ "Talx8llong", TYPE_INT, 9223372036854775807LL, 'Q' },
534{ "Talx8ullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },
535{ "Taly8ptr", TYPE_PTR, 0, 0 },
536{ "Taly8cptr", TYPE_PTR, 0, 0 },
537{ "Taly8iptr", TYPE_PTR, 0, 0 },
538{ "Talx8double", TYPE_FLOAT, 0, 0 },
539{ "Talx8ldouble", TYPE_FLOAT, 0, 0 }
540#define NAATYPES2 (sizeof (attrib_array_types) / sizeof (attrib_array_types[0]))
541};
542struct types complex_attrib_array_types[] = {
543{ "Talx1cchar", TYPE_CUINT, 127, 0 },
544{ "Talx1cschar", TYPE_CINT, 127, 0 },
545{ "Talx1cuchar", TYPE_CUINT, 255, 0 },
546{ "Talx1cshort", TYPE_CINT, 32767, 0 },
547{ "Talx1cushort", TYPE_CUINT, 65535, 0 },
548{ "Talx1cint", TYPE_CINT, 2147483647, 0 },
549{ "Talx1cuint", TYPE_CUINT, 4294967295U, 0 },
550{ "Talx1clong", TYPE_CINT, 9223372036854775807LL, 0 },
551{ "Talx1culong", TYPE_CUINT, 18446744073709551615ULL, 0 },
552{ "Talx1cllong", TYPE_CINT, 9223372036854775807LL, 0 },
553{ "Talx1cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },
554{ "Talx1cfloat", TYPE_CFLOAT, 0, 0 },
555{ "Talx1cdouble", TYPE_CFLOAT, 0, 0 },
556{ "Talx1cldouble", TYPE_CFLOAT, 0, 0 },
557{ "Talx2cchar", TYPE_CUINT, 127, 0 },
558{ "Talx2cschar", TYPE_CINT, 127, 0 },
559{ "Talx2cuchar", TYPE_CUINT, 255, 0 },
560{ "Talx2cshort", TYPE_CINT, 32767, 0 },
561{ "Talx2cushort", TYPE_CUINT, 65535, 0 },
562{ "Talx2cint", TYPE_CINT, 2147483647, 0 },
563{ "Talx2cuint", TYPE_CUINT, 4294967295U, 0 },
564{ "Talx2clong", TYPE_CINT, 9223372036854775807LL, 0 },
565{ "Talx2culong", TYPE_CUINT, 18446744073709551615ULL, 0 },
566{ "Talx2cllong", TYPE_CINT, 9223372036854775807LL, 0 },
567{ "Talx2cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },
568{ "Talx2cfloat", TYPE_CFLOAT, 0, 0 },
569{ "Talx2cdouble", TYPE_CFLOAT, 0, 0 },
570{ "Talx2cldouble", TYPE_CFLOAT, 0, 0 },
571{ "Talx4cshort", TYPE_CINT, 32767, 0 },
572{ "Talx4cushort", TYPE_CUINT, 65535, 0 },
573{ "Talx4cint", TYPE_CINT, 2147483647, 0 },
574{ "Talx4cuint", TYPE_CUINT, 4294967295U, 0 },
575{ "Talx4clong", TYPE_CINT, 9223372036854775807LL, 0 },
576{ "Talx4culong", TYPE_CUINT, 18446744073709551615ULL, 0 },
577{ "Talx4cllong", TYPE_CINT, 9223372036854775807LL, 0 },
578{ "Talx4cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },
579{ "Talx4cfloat", TYPE_CFLOAT, 0, 0 },
580{ "Talx4cdouble", TYPE_CFLOAT, 0, 0 },
581{ "Talx4cldouble", TYPE_CFLOAT, 0, 0 },
582{ "Talx8cint", TYPE_CINT, 2147483647, 0 },
583{ "Talx8cuint", TYPE_CUINT, 4294967295U, 0 },
584{ "Talx8clong", TYPE_CINT, 9223372036854775807LL, 0 },
585{ "Talx8culong", TYPE_CUINT, 18446744073709551615ULL, 0 },
586{ "Talx8cllong", TYPE_CINT, 9223372036854775807LL, 0 },
587{ "Talx8cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },
588{ "Talx8cfloat", TYPE_CFLOAT, 0, 0 },
589{ "Talx8cdouble", TYPE_CFLOAT, 0, 0 },
590{ "Talx8cldouble", TYPE_CFLOAT, 0, 0 },
591{ "Taly16clong", TYPE_CINT, 9223372036854775807LL, 0 },
592{ "Taly16culong", TYPE_CUINT, 18446744073709551615ULL, 0 },
593{ "Talx16cllong", TYPE_CINT, 9223372036854775807LL, 0 },
594{ "Talx16cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },
595{ "Talx16cdouble", TYPE_CFLOAT, 0, 0 },
596{ "Talx16cldouble", TYPE_CFLOAT, 0, 0 }
597#define NCAATYPES2 (sizeof (complex_attrib_array_types) / sizeof (complex_attrib_array_types[0]))
598};
599
600struct types bitfld_types[NTYPES2];
601int n_bitfld_types;
602struct types aligned_bitfld_types[NATYPES2];
603int n_aligned_bitfld_types;
604
605const char *attributes[] = {
606"atal",
607"atpa",
608"atal1",
609"atal2",
610"atal4",
611"atal8",
612"atal16",
613#define NATTRIBS1 7
614"atalpa",
615"atpaal",
616"atal1pa",
617"atal2pa",
618"atal4pa",
619"atal8pa",
620"atal16pa",
621"atpaal1",
622"atpaal2",
623"atpaal4",
624"atpaal8",
625"atpaal16"
626#define NATTRIBS2 (sizeof (attributes) / sizeof (attributes[0]))
627};
628
629enum ETYPE
630{
631  ETYPE_TYPE,
632  ETYPE_ARRAY,
633  ETYPE_BITFLD,
634  ETYPE_STRUCT,
635  ETYPE_UNION,
636  ETYPE_STRUCT_ARRAY,
637  ETYPE_UNION_ARRAY
638};
639
640struct entry
641{
642#ifdef __GNUC__
643  enum ETYPE etype : 8;
644#else
645  unsigned char etype;
646#endif
647  unsigned short len;
648  unsigned char arr_len;
649  struct types *type;
650  const char *attrib;
651  /* Used to chain together entries in the hash table.  */
652  struct entry *next;
653};
654
655/* A prime number giving the number of slots in the hash table.  */
656#define HASH_SIZE 32749
657static struct entry *hash_table[HASH_SIZE];
658
659static int idx, limidx, output_one, short_enums;
660static const char *destdir;
661static const char *srcdir;
662FILE *outfile;
663
664void
665switchfiles (int fields)
666{
667  static int filecnt;
668  static char *destbuf, *destptr;
669  ++filecnt;
670  if (outfile)
671    fclose (outfile);
672  if (output_one)
673    {
674      outfile = stdout;
675      return;
676    }
677  if (destbuf == NULL)
678    {
679      size_t len = strlen (destdir);
680      destbuf = malloc (len + 20);
681      if (!destbuf)
682	abort ();
683      memcpy (destbuf, destdir, len);
684      if (!len || destbuf[len - 1] != '/')
685	destbuf[len++] = '/';
686      destptr = destbuf + len;
687    }
688  sprintf (destptr, "t%03d_main.c", filecnt);
689  outfile = fopen (destbuf, "w");
690  if (outfile == NULL)
691    {
692    fail:
693      fputs ("failed to create test files\n", stderr);
694      exit (1);
695    }
696  fprintf (outfile, "\
697/* { dg-options \"-I%s\" } */\n\
698/* { dg-options \"-I%s -fno-common\" { target hppa*-*-hpux* } } */\n\
699/* { dg-options \"-I%s -mno-base-addresses\" { target mmix-*-* } } */\n\
700#include \"struct-layout-1.h\"\n\
701\n\
702#define TX(n, type, attrs, fields, ops) extern void test##n (void);\n\
703#include \"t%03d_test.h\"\n\
704#undef TX\n\
705\n\
706int main (void)\n\
707{\n\
708#define TX(n, type, attrs, fields, ops)   test##n ();\n\
709#include \"t%03d_test.h\"\n\
710#undef TX\n\
711  if (fails)\n\
712    {\n\
713      fflush (stdout);\n\
714      abort ();\n\
715    }\n\
716  exit (0);\n\
717}\n", srcdir, srcdir, srcdir, filecnt, filecnt);
718  fclose (outfile);
719  sprintf (destptr, "t%03d_x.c", filecnt);
720  outfile = fopen (destbuf, "w");
721  if (outfile == NULL)
722    goto fail;
723  fprintf (outfile, "\
724/* { dg-options \"-w -I%s\" } */\n\
725/* { dg-options \"-w -I%s -fno-common\" { target hppa*-*-hpux* } } */\n\
726/* { dg-options \"-w -I%s -mno-base-addresses\" { target mmix-*-* } } */\n\
727#include \"struct-layout-1_x1.h\"\n\
728#include \"t%03d_test.h\"\n\
729#include \"struct-layout-1_x2.h\"\n\
730#include \"t%03d_test.h\"\n", srcdir, srcdir, srcdir, filecnt, filecnt);
731  fclose (outfile);
732  sprintf (destptr, "t%03d_y.c", filecnt);
733  outfile = fopen (destbuf, "w");
734  if (outfile == NULL)
735    goto fail;
736  fprintf (outfile, "\
737/* { dg-options \"-w -I%s\" } */\n\
738/* { dg-options \"-w -I%s -fno-common\" { target hppa*-*-hpux* } } */\n\
739/* { dg-options \"-w -I%s -mno-base-addresses\" { target mmix-*-* } } */\n\
740#include \"struct-layout-1_y1.h\"\n\
741#include \"t%03d_test.h\"\n\
742#include \"struct-layout-1_y2.h\"\n\
743#include \"t%03d_test.h\"\n", srcdir, srcdir, srcdir, filecnt, filecnt);
744  fclose (outfile);
745  sprintf (destptr, "t%03d_test.h", filecnt);
746  outfile = fopen (destbuf, "w");
747  if (outfile == NULL)
748    goto fail;
749  if (fields <= 2)
750    limidx = idx + 300;
751  else if (fields <= 4)
752    limidx = idx + 200;
753  else if (fields <= 6)
754    limidx = idx + 100;
755  else
756    limidx = idx + 50;
757}
758
759unsigned long long int
760getrandll (void)
761{
762  unsigned long long int ret;
763  ret = generate_random () & 0xffffff;
764  ret |= (generate_random () & 0xffffffLL) << 24;
765  ret |= ((unsigned long long int) generate_random ()) << 48;
766  return ret;
767}
768
769int
770subfield (struct entry *e, char *letter)
771{
772  int i, type;
773  char buf[20];
774  const char *p;
775  switch (e[0].etype)
776    {
777    case ETYPE_STRUCT:
778    case ETYPE_UNION:
779    case ETYPE_STRUCT_ARRAY:
780    case ETYPE_UNION_ARRAY:
781      type = e[0].attrib ? 1 + (generate_random () & 3) : 0;
782      if (e[0].etype == ETYPE_STRUCT || e[0].etype == ETYPE_STRUCT_ARRAY)
783	p = "struct";
784      else
785	p = "union";
786      if (e[0].etype == ETYPE_STRUCT_ARRAY || e[0].etype == ETYPE_UNION_ARRAY)
787	{
788	  if (e[0].arr_len == 255)
789	    snprintf (buf, 20, "%c[]", *letter);
790	  else
791	    snprintf (buf, 20, "%c[%d]", *letter, e[0].arr_len);
792	  /* If this is an array type, do not put aligned attributes on
793	     elements.  Aligning elements to a value greater than their
794	     size will result in a compiler error.  */
795	  if (type == 1
796	      && ((strncmp (e[0].attrib, "atal", 4) == 0)
797		   || strncmp (e[0].attrib, "atpaal", 6) == 0))
798	    type = 2;
799	}
800      else
801        {
802          buf[0] = *letter;
803          buf[1] = '\0';
804        }
805      ++*letter;
806      switch (type)
807        {
808        case 0:
809        case 3:
810        case 4:
811          fprintf (outfile, "%s{", p);
812          break;
813        case 1:
814          fprintf (outfile, "%s %s{", e[0].attrib, p);
815          break;
816        case 2:
817          fprintf (outfile, "%s %s{", p, e[0].attrib);
818          break;
819        }
820
821      for (i = 1; i <= e[0].len; )
822	i += subfield (e + i, letter);
823
824      switch (type)
825        {
826        case 0:
827        case 1:
828        case 2:
829          fprintf (outfile, "}%s;", buf);
830          break;
831	case 3:
832	  fprintf (outfile, "}%s %s;", e[0].attrib, buf);
833	  break;
834	case 4:
835	  fprintf (outfile, "}%s %s;", buf, e[0].attrib);
836	  break;
837        }
838      return 1 + e[0].len;
839    case ETYPE_TYPE:
840    case ETYPE_ARRAY:
841      if (e[0].etype == ETYPE_ARRAY)
842	{
843	  if (e[0].arr_len == 255)
844	    snprintf (buf, 20, "%c[]", *letter);
845	  else
846	    snprintf (buf, 20, "%c[%d]", *letter, e[0].arr_len);
847	}
848      else
849        {
850          buf[0] = *letter;
851          buf[1] = '\0';
852        }
853      ++*letter;
854      if (e[0].attrib)
855	{
856	  /* If this is an array type, do not put aligned attributes on
857	     elements.  Aligning elements to a value greater than their
858	     size will result in a compiler error.  */
859	  if (e[0].etype == ETYPE_ARRAY
860              && ((strncmp (e[0].attrib, "atal", 4) == 0)
861                   || strncmp (e[0].attrib, "atpaal", 6) == 0))
862	    type = 2;
863	  else
864            type = generate_random () % 3;
865	  switch (type)
866	    {
867	    case 0:
868	      fprintf (outfile, "%s %s %s;", e[0].attrib, e[0].type->name,
869		       buf);
870	      break;
871	    case 1:
872	      fprintf (outfile, "%s %s %s;", e[0].type->name, e[0].attrib,
873		       buf);
874	      break;
875	    case 2:
876	      fprintf (outfile, "%s %s %s;", e[0].type->name, buf,
877		       e[0].attrib);
878	      break;
879	    }
880	}
881      else
882	fprintf (outfile, "%s %s;", e[0].type->name, buf);
883      return 1;
884    case ETYPE_BITFLD:
885      if (e[0].len == 0)
886	{
887	  if (e[0].attrib)
888	    switch (generate_random () % 3)
889	      {
890	      case 0:
891		fprintf (outfile, "%s %s:0;", e[0].attrib, e[0].type->name);
892		break;
893	      case 1:
894		fprintf (outfile, "%s %s:0;", e[0].type->name, e[0].attrib);
895		break;
896	      case 2:
897		fprintf (outfile, "%s:0 %s;", e[0].type->name, e[0].attrib);
898		break;
899	      }
900	  else
901	    fprintf (outfile, "%s:0;", e[0].type->name);
902	  ++*letter;
903	  return 1;
904	}
905      switch (e[0].type->bitfld)
906	{
907	case 'C':
908	case 'S':
909	case 'I':
910	case 'L':
911	case 'Q':
912	  snprintf (buf, 20, "B%cN(%d)", e[0].type->bitfld, e[0].len);
913	  break;
914	case 'B':
915	case ' ':
916	  snprintf (buf, 20, "%d", e[0].len);
917	  break;
918	default:
919	  abort ();
920	}
921      if (e[0].attrib)
922	switch (generate_random () % 3)
923	  {
924	  case 0:
925	    fprintf (outfile, "%s %s %c:%s;", e[0].attrib, e[0].type->name,
926		     *letter, buf);
927	    break;
928	  case 1:
929	    fprintf (outfile, "%s %s %c:%s;", e[0].type->name, e[0].attrib,
930		     *letter, buf);
931	    break;
932	  case 2:
933	    fprintf (outfile, "%s %c:%s %s;", e[0].type->name, *letter,
934		     buf, e[0].attrib);
935	    break;
936	  }
937      else
938	fprintf (outfile, "%s %c:%s;", e[0].type->name, *letter, buf);
939      ++*letter;
940      return 1;
941    default:
942      abort ();
943  }
944}
945
946char namebuf[1024];
947
948void
949output_FNB (char mode, struct entry *e)
950{
951  unsigned long long int l1, l2, m;
952  int signs = 0;
953  const char *p, *q;
954
955  if (e->type->type == TYPE_OTHER)
956    {
957      if (mode == 'B')
958        abort ();
959      fprintf (outfile, "N(%d,%s)", idx, namebuf);
960      return;
961    }
962  fprintf (outfile, "%c(%d,%s,", mode, idx, namebuf);
963  l1 = getrandll ();
964  l2 = getrandll ();
965  switch (e->type->type)
966    {
967    case TYPE_INT:
968      signs = generate_random () & 3;
969      m = e->type->maxval;
970      if (mode == 'B')
971	m &= e->len > 1 ? (1ULL << (e->len - 1)) - 1 : 1;
972      l1 &= m;
973      l2 &= m;
974      fprintf (outfile, "%s%llu%s,%s%llu%s",
975	       (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "",
976	       (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : "");
977      break;
978    case TYPE_UINT:
979      m = e->type->maxval;
980      if (mode == 'B')
981	m &= (1ULL << e->len) - 1;
982      l1 &= m;
983      l2 &= m;
984      fprintf (outfile, "%lluU%s,%lluU%s", l1, l1 > 4294967295U ? "LL" : "",
985	       l2, l2 > 4294967295U ? "LL" : "");
986      break;
987    case TYPE_FLOAT:
988      l1 &= 0xffffff;
989      l2 &= 0xffffff;
990      signs = generate_random () & 3;
991      fprintf (outfile, "%s%f,%s%f", (signs & 1) ? "-" : "",
992	       ((double) l1) / 64, (signs & 2) ? "-" : "", ((double) l2) / 64);
993      break;
994    case TYPE_CINT:
995      signs = generate_random () & 3;
996      l1 &= e->type->maxval;
997      l2 &= e->type->maxval;
998      fprintf (outfile, "CINT(%s%llu%s,%s%llu%s),",
999	       (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "",
1000	       (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : "");
1001      signs = generate_random () & 3;
1002      l1 = getrandll ();
1003      l2 = getrandll ();
1004      l1 &= e->type->maxval;
1005      l2 &= e->type->maxval;
1006      fprintf (outfile, "CINT(%s%llu%s,%s%llu%s)",
1007	       (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "",
1008	       (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : "");
1009      break;
1010    case TYPE_CUINT:
1011      l1 &= e->type->maxval;
1012      l2 &= e->type->maxval;
1013      fprintf (outfile, "CINT(%lluU%s,%lluU%s),",
1014	       l1, l1 > 4294967295U ? "LL" : "",
1015	       l2, l2 > 4294967295U ? "LL" : "");
1016      l1 = getrandll ();
1017      l2 = getrandll ();
1018      l1 &= e->type->maxval;
1019      l2 &= e->type->maxval;
1020      fprintf (outfile, "CINT(%lluU%s,%lluU%s)",
1021	       l1, l1 > 4294967295U ? "LL" : "",
1022	       l2, l2 > 4294967295U ? "LL" : "");
1023      break;
1024    case TYPE_CFLOAT:
1025      l1 &= 0xffffff;
1026      l2 &= 0xffffff;
1027      signs = generate_random () & 3;
1028      fprintf (outfile, "CDBL(%s%f,%s%f),",
1029	       (signs & 1) ? "-" : "", ((double) l1) / 64,
1030	       (signs & 2) ? "-" : "", ((double) l2) / 64);
1031      l1 = getrandll ();
1032      l2 = getrandll ();
1033      l1 &= 0xffffff;
1034      l2 &= 0xffffff;
1035      signs = generate_random () & 3;
1036      fprintf (outfile, "CDBL(%s%f,%s%f)",
1037	       (signs & 1) ? "-" : "", ((double) l1) / 64,
1038	       (signs & 2) ? "-" : "", ((double) l2) / 64);
1039      break;
1040    case TYPE_UENUM:
1041      if (e->type->maxval == 0)
1042	fputs ("e0_0,e0_0", outfile);
1043      else if (e->type->maxval == 1)
1044        fprintf (outfile, "e1_%lld,e1_%lld", l1 & 1, l2 & 1);
1045      else
1046        {
1047	  p = strchr (e->type->name, '\0');
1048	  while (--p >= e->type->name && *p >= '0' && *p <= '9');
1049	  p++;
1050          l1 %= 7;
1051          l2 %= 7;
1052          if (l1 > 3)
1053            l1 += e->type->maxval - 6;
1054          if (l2 > 3)
1055            l2 += e->type->maxval - 6;
1056	  fprintf (outfile, "e%s_%lld,e%s_%lld", p, l1, p, l2);
1057        }
1058      break;
1059    case TYPE_SENUM:
1060      p = strchr (e->type->name, '\0');
1061      while (--p >= e->type->name && *p >= '0' && *p <= '9');
1062      p++;
1063      l1 %= 7;
1064      l2 %= 7;
1065      fprintf (outfile, "e%s_%s%lld,e%s_%s%lld",
1066	       p, l1 < 3 ? "m" : "",
1067	       l1 == 3 ? 0LL : e->type->maxval - (l1 & 3),
1068	       p, l2 < 3 ? "m" : "",
1069	       l2 == 3 ? 0LL : e->type->maxval - (l2 & 3));
1070      break;
1071    case TYPE_PTR:
1072      l1 %= 256;
1073      l2 %= 256;
1074      fprintf (outfile, "(%s)&intarray[%lld],(%s)&intarray[%lld]",
1075	       e->type->name, l1, e->type->name, l2);
1076      break;
1077    case TYPE_FNPTR:
1078      l1 %= 10;
1079      l2 %= 10;
1080      fprintf (outfile, "fn%lld,fn%lld", l1, l2);
1081      break;
1082    default:
1083      abort ();
1084    }
1085  fputs (")", outfile);
1086}
1087
1088int
1089subvalues (struct entry *e, char *p, char *letter)
1090{
1091  int i, j;
1092  char *q;
1093  if (p >= namebuf + sizeof (namebuf) - 32)
1094    abort ();
1095  p[0] = *letter;
1096  p[1] = '\0';
1097  q = p + 1;
1098  switch (e[0].etype)
1099    {
1100    case ETYPE_STRUCT_ARRAY:
1101    case ETYPE_UNION_ARRAY:
1102      if (e[0].arr_len == 0 || e[0].arr_len == 255)
1103	{
1104	  *letter += 1 + e[0].len;
1105	  return 1 + e[0].len;
1106	}
1107      i = generate_random () % e[0].arr_len;
1108      snprintf (p, sizeof (namebuf) - (p - namebuf) - 1,
1109		"%c[%d]", *letter, i);
1110      q = strchr (p, '\0');
1111      /* FALLTHROUGH */
1112    case ETYPE_STRUCT:
1113    case ETYPE_UNION:
1114      *q++ = '.';
1115      ++*letter;
1116      for (i = 1; i <= e[0].len; )
1117	{
1118	  i += subvalues (e + i, q, letter);
1119	  if (e[0].etype == ETYPE_UNION || e[0].etype == ETYPE_UNION_ARRAY)
1120	    {
1121	      *letter += e[0].len - i + 1;
1122	      break;
1123	    }
1124	}
1125      return 1 + e[0].len;
1126    case ETYPE_TYPE:
1127      ++*letter;
1128      output_FNB ('F', e);
1129      return 1;
1130    case ETYPE_ARRAY:
1131      if (e[0].arr_len == 0 || e[0].arr_len == 255)
1132	{
1133	  ++*letter;
1134	  return 1;
1135	}
1136      i = generate_random () % e[0].arr_len;
1137      snprintf (p, sizeof (namebuf) - (p - namebuf),
1138		"%c[%d]", *letter, i);
1139      output_FNB ('F', e);
1140      if ((generate_random () & 7) == 0)
1141	{
1142	  j = generate_random () % e[0].arr_len;
1143	  if (i != j)
1144	    {
1145	      snprintf (p, sizeof (namebuf) - (p - namebuf),
1146			"%c[%d]", *letter, j);
1147	      output_FNB ('F', e);
1148	    }
1149	}
1150      ++*letter;
1151      return 1;
1152    case ETYPE_BITFLD:
1153      ++*letter;
1154      if (e[0].len != 0)
1155	output_FNB ('B', e);
1156      return 1;
1157    }
1158}
1159
1160/* DERIVED FROM:
1161--------------------------------------------------------------------
1162lookup2.c, by Bob Jenkins, December 1996, Public Domain.
1163hash(), hash2(), hash3, and mix() are externally useful functions.
1164Routines to test the hash are included if SELF_TEST is defined.
1165You can use this free for any purpose.  It has no warranty.
1166--------------------------------------------------------------------
1167*/
1168
1169/*
1170--------------------------------------------------------------------
1171mix -- mix 3 32-bit values reversibly.
1172For every delta with one or two bit set, and the deltas of all three
1173  high bits or all three low bits, whether the original value of a,b,c
1174  is almost all zero or is uniformly distributed,
1175* If mix() is run forward or backward, at least 32 bits in a,b,c
1176  have at least 1/4 probability of changing.
1177* If mix() is run forward, every bit of c will change between 1/3 and
1178  2/3 of the time.  (Well, 22/100 and 78/100 for some 2-bit deltas.)
1179mix() was built out of 36 single-cycle latency instructions in a
1180  structure that could supported 2x parallelism, like so:
1181      a -= b;
1182      a -= c; x = (c>>13);
1183      b -= c; a ^= x;
1184      b -= a; x = (a<<8);
1185      c -= a; b ^= x;
1186      c -= b; x = (b>>13);
1187      ...
1188  Unfortunately, superscalar Pentiums and Sparcs can't take advantage
1189  of that parallelism.  They've also turned some of those single-cycle
1190  latency instructions into multi-cycle latency instructions.  Still,
1191  this is the fastest good hash I could find.  There were about 2^^68
1192  to choose from.  I only looked at a billion or so.
1193--------------------------------------------------------------------
1194*/
1195/* same, but slower, works on systems that might have 8 byte hashval_t's */
1196#define mix(a,b,c) \
1197{ \
1198  a -= b; a -= c; a ^= (c>>13); \
1199  b -= c; b -= a; b ^= (a<< 8); \
1200  c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
1201  a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
1202  b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
1203  c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
1204  a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
1205  b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
1206  c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
1207}
1208
1209/*
1210--------------------------------------------------------------------
1211hash() -- hash a variable-length key into a 32-bit value
1212  k     : the key (the unaligned variable-length array of bytes)
1213  len   : the length of the key, counting by bytes
1214  level : can be any 4-byte value
1215Returns a 32-bit value.  Every bit of the key affects every bit of
1216the return value.  Every 1-bit and 2-bit delta achieves avalanche.
1217About 36+6len instructions.
1218
1219The best hash table sizes are powers of 2.  There is no need to do
1220mod a prime (mod is sooo slow!).  If you need less than 32 bits,
1221use a bitmask.  For example, if you need only 10 bits, do
1222  h = (h & hashmask(10));
1223In which case, the hash table should have hashsize(10) elements.
1224
1225If you are hashing n strings (ub1 **)k, do it like this:
1226  for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
1227
1228By Bob Jenkins, 1996.  bob_jenkins@burtleburtle.net.  You may use this
1229code any way you wish, private, educational, or commercial.  It's free.
1230
1231See http://burtleburtle.net/bob/hash/evahash.html
1232Use for hash table lookup, or anything where one collision in 2^32 is
1233acceptable.  Do NOT use for cryptographic purposes.
1234--------------------------------------------------------------------
1235*/
1236
1237static hashval_t
1238iterative_hash (const void *k_in /* the key */,
1239                register size_t  length /* the length of the key */,
1240                register hashval_t initval /* the previous hash, or
1241                                              an arbitrary value */)
1242{
1243  register const unsigned char *k = (const unsigned char *)k_in;
1244  register hashval_t a,b,c,len;
1245
1246  /* Set up the internal state */
1247  len = length;
1248  a = b = 0x9e3779b9;  /* the golden ratio; an arbitrary value */
1249  c = initval;           /* the previous hash value */
1250
1251  /*---------------------------------------- handle most of the key */
1252    while (len >= 12)
1253      {
1254	a += (k[0] +((hashval_t)k[1]<<8) +((hashval_t)k[2]<<16) +((hashval_t)k[3]<<24));
1255	b += (k[4] +((hashval_t)k[5]<<8) +((hashval_t)k[6]<<16) +((hashval_t)k[7]<<24));
1256	c += (k[8] +((hashval_t)k[9]<<8) +((hashval_t)k[10]<<16)+((hashval_t)k[11]<<24));
1257	mix(a,b,c);
1258	k += 12; len -= 12;
1259      }
1260
1261  /*------------------------------------- handle the last 11 bytes */
1262  c += length;
1263  switch(len)              /* all the case statements fall through */
1264    {
1265    case 11: c+=((hashval_t)k[10]<<24);
1266    case 10: c+=((hashval_t)k[9]<<16);
1267    case 9 : c+=((hashval_t)k[8]<<8);
1268      /* the first byte of c is reserved for the length */
1269    case 8 : b+=((hashval_t)k[7]<<24);
1270    case 7 : b+=((hashval_t)k[6]<<16);
1271    case 6 : b+=((hashval_t)k[5]<<8);
1272    case 5 : b+=k[4];
1273    case 4 : a+=((hashval_t)k[3]<<24);
1274    case 3 : a+=((hashval_t)k[2]<<16);
1275    case 2 : a+=((hashval_t)k[1]<<8);
1276    case 1 : a+=k[0];
1277      /* case 0: nothing left to add */
1278    }
1279  mix(a,b,c);
1280  /*-------------------------------------------- report the result */
1281  return c;
1282}
1283
1284hashval_t
1285e_hash (const void *a)
1286{
1287  const struct entry *e = a;
1288  hashval_t ret = 0;
1289  int i;
1290
1291  if (e[0].etype != ETYPE_STRUCT && e[0].etype != ETYPE_UNION)
1292    abort ();
1293  for (i = 0; i <= e[0].len; ++i)
1294    {
1295      int attriblen;
1296      ret = iterative_hash (&e[i], offsetof (struct entry, attrib), ret);
1297      attriblen = e[i].attrib ? strlen (e[i].attrib) : -1;
1298      ret = iterative_hash (&attriblen, sizeof (int), ret);
1299      if (e[i].attrib)
1300        ret = iterative_hash (e[i].attrib, attriblen, ret);
1301    }
1302  return ret;
1303}
1304
1305int
1306e_eq (const void *a, const void *b)
1307{
1308  const struct entry *ea = a, *eb = b;
1309  int i;
1310  if (ea[0].etype != ETYPE_STRUCT && ea[0].etype != ETYPE_UNION)
1311    abort ();
1312  if (ea[0].len != eb[0].len)
1313    return 0;
1314  for (i = 0; i <= ea[0].len; ++i)
1315    {
1316      if (ea[i].etype != eb[i].etype
1317	  || ea[i].len != eb[i].len
1318	  || ea[i].arr_len != eb[i].arr_len
1319	  || ea[i].type != eb[i].type)
1320	return 0;
1321      if ((ea[i].attrib == NULL) ^ (eb[i].attrib == NULL))
1322	return 0;
1323      if (ea[i].attrib && strcmp (ea[i].attrib, eb[i].attrib) != 0)
1324	return 0;
1325    }
1326  return 1;
1327}
1328
1329static int
1330e_exists (const struct entry *e)
1331{
1332  struct entry *h;
1333  hashval_t hval;
1334
1335  hval = e_hash (e);
1336  for (h = hash_table[hval % HASH_SIZE]; h; h = h->next)
1337    if (e_eq (e, h))
1338      return 1;
1339  return 0;
1340}
1341
1342static void
1343e_insert (struct entry *e)
1344{
1345  hashval_t hval;
1346
1347  hval = e_hash (e);
1348  e->next = hash_table[hval % HASH_SIZE];
1349  hash_table[hval % HASH_SIZE] = e;
1350}
1351
1352void
1353output (struct entry *e)
1354{
1355  int i;
1356  char c;
1357  struct entry *n;
1358  const char *skip_cint = "";
1359
1360  if (e[0].etype != ETYPE_STRUCT && e[0].etype != ETYPE_UNION)
1361    abort ();
1362
1363  if (e_exists (e))
1364    return;
1365
1366  n = (struct entry *) malloc ((e[0].len + 1) * sizeof (struct entry));
1367  memcpy (n, e, (e[0].len + 1) * sizeof (struct entry));
1368  e_insert (n);
1369
1370  if (idx == limidx)
1371    switchfiles (e[0].len);
1372
1373  for (i = 1; i <= e[0].len; ++i)
1374    if ((e[i].etype == ETYPE_TYPE || e[i].etype == ETYPE_ARRAY)
1375	&& (e[i].type->type == TYPE_CINT || e[i].type->type == TYPE_CUINT))
1376      break;
1377  if (i <= e[0].len)
1378    skip_cint = "CI";
1379  if (e[0].attrib)
1380    fprintf (outfile, (generate_random () & 1)
1381	     ? "TX%s(%d,%s %s,," : "TX%s(%d,%s,%s,", skip_cint,
1382	     idx, e[0].etype == ETYPE_STRUCT ? "struct" : "union",
1383	     e[0].attrib);
1384  else if (e[0].etype == ETYPE_STRUCT)
1385    fprintf (outfile, "T%s(%d,", skip_cint, idx);
1386  else
1387    fprintf (outfile, "U%s(%d,", skip_cint, idx);
1388  c = 'a';
1389  for (i = 1; i <= e[0].len; )
1390    i += subfield (e + i, &c);
1391  fputs (",", outfile);
1392  c = 'a';
1393  for (i = 1; i <= e[0].len; )
1394    {
1395      i += subvalues (e + i, namebuf, &c);
1396      if (e[0].etype == ETYPE_UNION)
1397        break;
1398    }
1399  fputs (")\n", outfile);
1400  if (output_one && idx == limidx)
1401    exit (0);
1402  ++idx;
1403}
1404
1405enum FEATURE
1406{
1407  FEATURE_VECTOR = 1,
1408  FEATURE_COMPLEX = 2,
1409  FEATURE_ALIGNEDPACKED = 4,
1410  FEATURE_ZEROARRAY = 8,
1411  FEATURE_ZEROBITFLD = 16,
1412  ALL_FEATURES = FEATURE_COMPLEX | FEATURE_VECTOR | FEATURE_ZEROARRAY
1413		 | FEATURE_ALIGNEDPACKED | FEATURE_ZEROBITFLD
1414};
1415
1416void
1417singles (enum FEATURE features)
1418{
1419  struct entry e[2];
1420  int i;
1421  memset (e, 0, sizeof (e));
1422  e[0].etype = ETYPE_STRUCT;
1423  output (e);
1424  e[0].etype = ETYPE_UNION;
1425  output (e);
1426  for (i = 0;
1427       i < ((features & FEATURE_ALIGNEDPACKED) ? NATTRIBS2 : NATTRIBS1);
1428       ++i)
1429    {
1430      e[0].attrib = attributes[i];
1431      e[0].etype = ETYPE_STRUCT;
1432      output (e);
1433      e[0].etype = ETYPE_UNION;
1434      output (e);
1435    }
1436  e[0].len = 1;
1437  e[0].attrib = NULL;
1438  for (i = 0; i < NTYPES2; ++i)
1439    {
1440      e[0].etype = ETYPE_STRUCT;
1441      e[1].etype = ETYPE_TYPE;
1442      e[1].type = &base_types[i];
1443      output (e);
1444      e[0].etype = ETYPE_UNION;
1445      output (e);
1446    }
1447  if (features & FEATURE_COMPLEX)
1448    for (i = 0; i < NCTYPES2; ++i)
1449      {
1450	e[0].etype = ETYPE_STRUCT;
1451	e[1].etype = ETYPE_TYPE;
1452	e[1].type = &complex_types[i];
1453	output (e);
1454	e[0].etype = ETYPE_UNION;
1455	output (e);
1456      }
1457  if (features & FEATURE_VECTOR)
1458    for (i = 0; i < NVTYPES2; ++i)
1459      {
1460	e[0].etype = ETYPE_STRUCT;
1461	e[1].etype = ETYPE_TYPE;
1462	e[1].type = &vector_types[i];
1463	output (e);
1464	e[0].etype = ETYPE_UNION;
1465	output (e);
1466      }
1467}
1468
1469void
1470choose_type (enum FEATURE features, struct entry *e, int r, int in_array)
1471{
1472  int i;
1473
1474  i = NTYPES2 - NTYPES1;
1475  if (features & FEATURE_COMPLEX)
1476    i += NCTYPES2;
1477  if (features & FEATURE_VECTOR)
1478    i += NVTYPES2;
1479  if ((r & 3) == 0)
1480    {
1481      if (in_array)
1482	{
1483	  i += NAATYPES2;
1484	  if (features & FEATURE_COMPLEX)
1485	    i += NCAATYPES2;
1486	}
1487      else
1488	{
1489	  i += NATYPES2;
1490	  if (features & FEATURE_COMPLEX)
1491	    i += NCATYPES2;
1492	}
1493    }
1494  r >>= 2;
1495  r %= i;
1496  if (r < NTYPES2 - NTYPES1)
1497    e->type = &base_types[r + NTYPES1];
1498  r -= NTYPES2 - NTYPES1;
1499  if (e->type == NULL && (features & FEATURE_COMPLEX))
1500    {
1501      if (r < NCTYPES2)
1502	e->type = &complex_types[r];
1503      r -= NCTYPES2;
1504    }
1505  if (e->type == NULL && (features & FEATURE_VECTOR))
1506    {
1507      if (r < NVTYPES2)
1508	e->type = &vector_types[r];
1509      r -= NVTYPES2;
1510    }
1511  if (e->type == NULL && !in_array)
1512    {
1513      if (r < NATYPES2)
1514	e->type = &attrib_types[r];
1515      r -= NATYPES2;
1516    }
1517  if (e->type == NULL && !in_array && (features & FEATURE_COMPLEX))
1518    {
1519      if (r < NCATYPES2)
1520	e->type = &complex_attrib_types[r];
1521      r -= NCATYPES2;
1522    }
1523  if (e->type == NULL && in_array)
1524    {
1525      if (r < NAATYPES2)
1526	e->type = &attrib_array_types[r];
1527      r -= NAATYPES2;
1528    }
1529  if (e->type == NULL && in_array && (features & FEATURE_COMPLEX))
1530    {
1531      if (r < NCAATYPES2)
1532	e->type = &complex_attrib_array_types[r];
1533      r -= NCAATYPES2;
1534    }
1535  if (e->type == NULL)
1536    abort ();
1537}
1538
1539/* This is from gcc.c-torture/execute/builtin-bitops-1.c.  */
1540static int
1541my_ffsll (unsigned long long x)
1542{
1543  int i;
1544  if (x == 0)
1545    return 0;
1546  /* We've tested LLONG_MAX for 64 bits so this should be safe.  */
1547  for (i = 0; i < 64; i++)
1548    if (x & (1ULL << i))
1549      break;
1550  return i + 1;
1551}
1552
1553void
1554generate_fields (enum FEATURE features, struct entry *e, struct entry *parent,
1555		 int len)
1556{
1557  int r, i, j, ret = 1, n, incr, sametype;
1558
1559  for (n = 0; n < len; n += incr)
1560    {
1561      r = generate_random ();
1562      /* 50% ETYPE_TYPE base_types NTYPES1
1563	 12.5% ETYPE_TYPE other
1564	 12.5% ETYPE_ARRAY
1565	 12.5% ETYPE_BITFLD
1566	 12.5% ETYPE_STRUCT|ETYPE_UNION|ETYPE_STRUCT_ARRAY|ETYPE_UNION_ARRAY */
1567      i = (r & 7);
1568      r >>= 3;
1569      incr = 1;
1570      switch (i)
1571	{
1572	case 0:
1573	case 1:
1574	case 2:
1575	case 3:
1576	  e[n].etype = ETYPE_TYPE;
1577	  e[n].type = &base_types[r % NTYPES1];
1578	  break;
1579	case 4:
1580	  e[n].etype = ETYPE_TYPE;
1581	  choose_type (features, &e[n], r, 0);
1582	  break;
1583	case 5:
1584	  e[n].etype = ETYPE_ARRAY;
1585	  i = r & 1;
1586	  r >>= 1;
1587	  if (i)
1588	    e[n].type = &base_types[r % NTYPES1];
1589	  else
1590	    choose_type (features, &e[n], r, 1);
1591	  r = generate_random ();
1592	  if ((features & FEATURE_ZEROARRAY) && (r & 3) == 0)
1593	    {
1594	      e[n].arr_len = 0;
1595	      if (n == len - 1 && (r & 4)
1596		  && (parent->etype == ETYPE_STRUCT
1597		      || parent->etype == ETYPE_STRUCT_ARRAY))
1598		{
1599		  int k;
1600		  for (k = 0; k < n; ++k)
1601		    if (e[k].etype != ETYPE_BITFLD || e[k].len)
1602		      {
1603			e[n].arr_len = 255;
1604			break;
1605		      }
1606		}
1607	    }
1608	  else if ((r & 3) != 3)
1609	    e[n].arr_len = (r >> 2) & 7;
1610	  else
1611	    e[n].arr_len = (r >> 2) & 31;
1612	  break;
1613	case 6:
1614	  sametype = 1;
1615	  switch (r & 7)
1616	    {
1617	    case 0:
1618	    case 1:
1619	    case 2:
1620	      break;
1621	    case 3:
1622	    case 4:
1623	    case 5:
1624	      incr = 1 + (r >> 3) % (len - n);
1625	      break;
1626	    case 6:
1627	    case 7:
1628	      sametype = 0;
1629	      incr = 1 + (r >> 3) % (len - n);
1630	      break;
1631	    }
1632	  for (j = n; j < n + incr; ++j)
1633	    {
1634	      int mi, ma;
1635
1636	      e[j].etype = ETYPE_BITFLD;
1637	      if (j == n || !sametype)
1638		{
1639		  int k;
1640		  r = generate_random ();
1641		  k = r & 3;
1642		  r >>= 2;
1643		  if (!k)
1644		    e[j].type
1645		      = &aligned_bitfld_types[r % n_aligned_bitfld_types];
1646		  else
1647		    e[j].type
1648		      = &bitfld_types[r % n_bitfld_types];
1649		}
1650	      else
1651		e[j].type = e[n].type;
1652	      r = generate_random ();
1653	      mi = 0;
1654	      ma = 0;
1655	      switch (e[j].type->bitfld)
1656	        {
1657	        case 'C': ma = 8; break;
1658	        case 'S': ma = 16; break;
1659	        case 'I': ma = 32; break;
1660	        case 'L':
1661	        case 'Q': ma = 64; break;
1662	        case 'B': ma = 1; break;
1663	        case ' ':
1664		  if (e[j].type->type == TYPE_UENUM)
1665		    mi = my_ffsll (e[j].type->maxval + 1) - 1;
1666		  else if (e[j].type->type == TYPE_SENUM)
1667		    mi = my_ffsll (e[j].type->maxval + 1);
1668		  else
1669		    abort ();
1670		  if (!mi)
1671		    mi = 1;
1672		  if (mi > 32)
1673		    ma = 64;
1674		  else if (mi > 16 || !short_enums)
1675		    ma = 32;
1676		  else if (mi > 8)
1677		    ma = 16;
1678		  else
1679		    ma = 8;
1680		  break;
1681		default:
1682		  abort ();
1683	        }
1684	      e[j].len = ma + 1;
1685	      if (sametype && (r & 3) == 0 && ma > 1)
1686		{
1687		  int sum = 0, k;
1688		  for (k = n; k < j; ++k)
1689		    sum += e[k].len;
1690		  sum %= ma;
1691		  e[j].len = sum ? ma - sum : ma;
1692		}
1693	      r >>= 2;
1694	      if (! (features & FEATURE_ZEROBITFLD) && mi == 0)
1695		mi = 1;
1696	      if (e[j].len < mi || e[j].len > ma)
1697		e[j].len = mi + (r % (ma + 1 - mi));
1698	      r >>= 6;
1699	      if ((features & FEATURE_ZEROBITFLD) && (r & 3) == 0
1700		  && mi == 0)
1701		e[j].len = 0;
1702	    }
1703	  break;
1704	case 7:
1705	  switch (r & 7)
1706	    {
1707	    case 0:
1708	    case 1:
1709	    case 2:
1710	      e[n].etype = ETYPE_STRUCT;
1711	      break;
1712	    case 3:
1713	    case 4:
1714	      e[n].etype = ETYPE_UNION;
1715	      break;
1716	    case 5:
1717	    case 6:
1718	      e[n].etype = ETYPE_STRUCT_ARRAY;
1719	      break;
1720	    case 7:
1721	      e[n].etype = ETYPE_UNION_ARRAY;
1722	      break;
1723	    }
1724	  r >>= 3;
1725	  e[n].len = r % (len - n);
1726	  incr = 1 + e[n].len;
1727	  generate_fields (features, &e[n + 1], &e[n], e[n].len);
1728	  if (e[n].etype == ETYPE_STRUCT_ARRAY
1729	      || e[n].etype == ETYPE_UNION_ARRAY)
1730	    {
1731	      r = generate_random ();
1732	      if ((features & FEATURE_ZEROARRAY) && (r & 3) == 0)
1733		{
1734		  e[n].arr_len = 0;
1735		  if (n + incr == len && (r & 4)
1736		      && (parent->etype == ETYPE_STRUCT
1737			  || parent->etype == ETYPE_STRUCT_ARRAY))
1738		    {
1739		      int k;
1740		      for (k = 0; k < n; ++k)
1741			if (e[k].etype != ETYPE_BITFLD || e[k].len)
1742			  {
1743			    e[n].arr_len = 255;
1744			    break;
1745			  }
1746		    }
1747		}
1748	      else if ((r & 3) != 3)
1749		e[n].arr_len = (r >> 2) & 7;
1750	      else
1751		e[n].arr_len = (r >> 2) & 31;
1752	    }
1753	  break;
1754	}
1755      r = generate_random ();
1756      if ((r & 7) == 0)
1757	{
1758	  r >>= 3;
1759	  i = (features & FEATURE_ALIGNEDPACKED) ? NATTRIBS2 : NATTRIBS1;
1760	  e[n].attrib = attributes[r % i];
1761	  if (! (features & FEATURE_ALIGNEDPACKED)
1762	      && strcmp (e[n].attrib, "atpa") == 0
1763	      && ((e[n].type >= &attrib_types[0]
1764		   && e[n].type < &attrib_types[NATYPES2])
1765		  || (e[n].type >= &complex_attrib_types[0]
1766		      && e[n].type < &complex_attrib_types[NCATYPES2])
1767		  || (e[n].type >= &attrib_array_types[0]
1768		      && e[n].type < &attrib_array_types[NAATYPES2])
1769		  || (e[n].type >= &complex_attrib_array_types[0]
1770		      && e[n].type < &complex_attrib_array_types[NAATYPES2])
1771		  || (e[n].type >= &aligned_bitfld_types[0]
1772		      && e[n].type < &aligned_bitfld_types[n_aligned_bitfld_types])))
1773	    e[n].attrib = NULL;
1774	}
1775    }
1776}
1777
1778void
1779generate_random_tests (enum FEATURE features, int len)
1780{
1781  struct entry e[len + 1];
1782  int i, r;
1783  if (len > 'z' - 'a' + 1)
1784    abort ();
1785  memset (e, 0, sizeof (e));
1786  r = generate_random ();
1787  if ((r & 7) == 0)
1788    e[0].etype = ETYPE_UNION;
1789  else
1790    e[0].etype = ETYPE_STRUCT;
1791  r >>= 3;
1792  e[0].len = len;
1793  if ((r & 31) == 0)
1794    {
1795      r >>= 5;
1796      if (features & FEATURE_ALIGNEDPACKED)
1797	r %= NATTRIBS2;
1798      else
1799	r %= NATTRIBS1;
1800      e[0].attrib = attributes[r];
1801    }
1802  generate_fields (features, &e[1], &e[0], len);
1803  output (e);
1804}
1805
1806struct { const char *name; enum FEATURE f; }
1807features[] = {
1808{ "normal", 0 },
1809{ "complex", FEATURE_COMPLEX },
1810{ "vector", FEATURE_VECTOR },
1811{ "[0] :0", FEATURE_ZEROARRAY | FEATURE_ZEROBITFLD },
1812{ "complex vector [0]",
1813  FEATURE_COMPLEX | FEATURE_VECTOR | FEATURE_ZEROARRAY },
1814{ "aligned packed complex vector [0] :0",
1815  FEATURE_COMPLEX | FEATURE_VECTOR | FEATURE_ZEROARRAY
1816  | FEATURE_ALIGNEDPACKED | FEATURE_ZEROBITFLD },
1817};
1818
1819int
1820main (int argc, char **argv)
1821{
1822  int i, j, count, c, n = 3000;
1823  char *optarg;
1824
1825  if (sizeof (int) != 4 || sizeof (long long) != 8)
1826    return 1;
1827
1828  i = 1;
1829  while (i < argc)
1830    {
1831      c = '\0';
1832      if (argv[i][0] == '-' && argv[i][2] == '\0')
1833	c = argv[i][1];
1834      optarg = argv[i + 1];
1835      if (!optarg)
1836	goto usage;
1837      switch (c)
1838	{
1839	case 'n':
1840	  n = atoi (optarg);
1841	  break;
1842	case 'd':
1843	  destdir = optarg;
1844	  break;
1845	case 's':
1846	  srcdir = optarg;
1847	  break;
1848	case 'i':
1849	  output_one = 1;
1850	  limidx = atoi (optarg);
1851	  break;
1852	case 'e':
1853	  short_enums = 1;
1854	  i--;
1855	  break;
1856	default:
1857	  fprintf (stderr, "unrecognized option %s\n", argv[i]);
1858	  goto usage;
1859      }
1860      i += 2;
1861    }
1862
1863  if (output_one)
1864    {
1865      outfile = fopen ("/dev/null", "w");
1866      if (outfile == NULL)
1867        {
1868	  fputs ("could not open /dev/null", stderr);
1869	  return 1;
1870        }
1871      n = limidx + 1;
1872    }
1873
1874  if (destdir == NULL && !output_one)
1875    {
1876    usage:
1877      fprintf (stderr, "Usage:\n\
1878%s [-e] [-s srcdir -d destdir] [-n count] [-i idx]\n\
1879Either -s srcdir -d destdir or -i idx must be used\n", argv[0]);
1880      return 1;
1881    }
1882
1883  if (srcdir == NULL && !output_one)
1884    goto usage;
1885
1886  for (i = 0; i < NTYPES2; ++i)
1887    if (base_types[i].bitfld)
1888      bitfld_types[n_bitfld_types++] = base_types[i];
1889  for (i = 0; i < NATYPES2; ++i)
1890    if (attrib_types[i].bitfld)
1891      aligned_bitfld_types[n_aligned_bitfld_types++] = attrib_types[i];
1892  for (i = 0; i < sizeof (features) / sizeof (features[0]); ++i)
1893    {
1894      int startidx = idx;
1895      if (! output_one)
1896	limidx = idx;
1897      if (!i)
1898        count = 200;
1899      else
1900        count = 20;
1901      for (j = 1; j <= 9; ++j)
1902        while (idx < startidx + j * count)
1903	  generate_random_tests (features[i].f, j);
1904      while (idx < startidx + count * 10)
1905	generate_random_tests (features[i].f, 10 + (generate_random () % 16));
1906    }
1907  for (i = 0; n > 3000 && i < sizeof (features) / sizeof (features[0]); ++i)
1908    {
1909      int startidx;
1910      startidx = idx;
1911      if (! output_one)
1912	limidx = idx;
1913      singles (features[i].f);
1914      if (!i)
1915	{
1916	  count = 1000;
1917	  while (idx < startidx + 1000)
1918	    generate_random_tests (features[i].f, 1);
1919	}
1920      else
1921	{
1922	  startidx = idx;
1923	  count = 100;
1924	  while (idx < startidx + 100)
1925	    generate_random_tests (features[i].f, 1);
1926	}
1927      startidx = idx;
1928      for (j = 2; j <= 9; ++j)
1929	while (idx < startidx + (j - 1) * count)
1930	  generate_random_tests (features[i].f, j);
1931      while (idx < startidx + count * 9)
1932        generate_random_tests (features[i].f, 10 + (generate_random () % 16));
1933    }
1934  if (! output_one)
1935    limidx = idx;
1936  while (idx < n)
1937    generate_random_tests (ALL_FEATURES, 1 + (generate_random () % 25));
1938  fclose (outfile);
1939  return 0;
1940}
1941