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