OSDN Git Service

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