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