1 /* Structure layout test generator.
2    Copyright (C) 2004, 2005, 2007, 2010, 2011 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 3, 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 COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 /* Compile with gcc -o struct-layout-1_generate{,.c} generate_random{,_r}.c */
22 
23 /* N.B. -- This program cannot use libiberty as that will not work
24    when testing an installed compiler.  */
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stddef.h>
30 /* We use our own pseudo-random number generator, so that it gives the same
31    values on all hosts.  */
32 #include "generate-random.h"
33 
34 #if LLONG_MAX != 9223372036854775807LL && __LONG_LONG_MAX__ != 9223372036854775807LL
35 # error Need 64-bit long long
36 #endif
37 
38 typedef unsigned int hashval_t;
39 
40 enum TYPE
41 {
42   TYPE_INT,
43   TYPE_UINT,
44   TYPE_CINT,
45   TYPE_CUINT,
46   TYPE_FLOAT,
47   TYPE_CFLOAT,
48   TYPE_SENUM,
49   TYPE_UENUM,
50   TYPE_PTR,
51   TYPE_FNPTR,
52   TYPE_OTHER
53 };
54 
55 struct types
56 {
57   const char *name;
58   enum TYPE type;
59   unsigned long long int maxval;
60   char bitfld;
61 };
62 
63 struct types base_types[] = {
64 /* As we don't know whether char will be signed or not, just limit ourselves
65    to unsigned values less than maximum signed char value.  */
66 { "char", TYPE_UINT, 127, 'C' },
67 { "signed char", TYPE_INT, 127, 'C' },
68 { "unsigned char", TYPE_UINT, 255, 'C' },
69 { "short int", TYPE_INT, 32767, 'S' },
70 { "unsigned short int", TYPE_UINT, 65535, 'S' },
71 { "int", TYPE_INT, 2147483647, 'I' },
72 { "unsigned int", TYPE_UINT, 4294967295U, 'I' },
73 { "long int", TYPE_INT, 9223372036854775807LL, 'L' },
74 { "unsigned long int", TYPE_UINT, 18446744073709551615ULL, 'L' },
75 { "long long int", TYPE_INT, 9223372036854775807LL, 'Q' },
76 { "unsigned long long int", TYPE_UINT, 18446744073709551615ULL, 'Q' },
77 { "bool", TYPE_UINT, 1, 'B' },
78 { "void *", TYPE_PTR, 0, 0 },
79 { "char *", TYPE_PTR, 0, 0 },
80 { "int *", TYPE_PTR, 0, 0 },
81 { "float", TYPE_FLOAT, 0, 0 },
82 { "double", TYPE_FLOAT, 0, 0 },
83 /*{ "long double", TYPE_FLOAT, 0, 0 },*/
84 /* Disabled as double and long double
85    are encoded thee same, currently  */
86 #define NTYPES1 16
87 #if 0
88 /* enums are disabled for now as it seems like their encoding is broken, we should
89    just encode them using their underlaying type but we don't.   */
90 { "enum E0", TYPE_UENUM, 0, ' ' },
91 { "enum E1", TYPE_UENUM, 1, ' ' },
92 { "enum E2", TYPE_SENUM, 3, ' ' },
93 { "enum E3", TYPE_SENUM, 127, ' ' },
94 { "enum E4", TYPE_UENUM, 255, ' ' },
95 { "enum E5", TYPE_SENUM, 32767, ' ' },
96 { "enum E6", TYPE_UENUM, 65535, ' ' },
97 { "enum E7", TYPE_SENUM, 2147483647, ' ' },
98 { "enum E8", TYPE_UENUM, 4294967295U, ' ' },
99 { "enum E9", TYPE_SENUM, 1099511627775LL, ' ' },
100 #endif
101 #define NTYPES2 (sizeof (base_types) / sizeof (base_types[0]))
102 };
103 struct types complex_types[] = {
104 { "_Complex char", TYPE_CUINT, 127, 0 },
105 { "_Complex signed char", TYPE_CINT, 127, 0 },
106 { "_Complex unsigned char", TYPE_CUINT, 255, 0 },
107 { "_Complex short int", TYPE_CINT, 32767, 0 },
108 { "_Complex unsigned short int", TYPE_CUINT, 65535, 0 },
109 { "_Complex int", TYPE_CINT, 2147483647, 0 },
110 { "_Complex unsigned int", TYPE_CUINT, 4294967295U, 0 },
111 { "_Complex long int", TYPE_CINT, 9223372036854775807LL, 0 },
112 { "_Complex unsigned long int", TYPE_CUINT, 18446744073709551615ULL, 0 },
113 { "_Complex long long int", TYPE_CINT, 9223372036854775807LL, 0 },
114 { "_Complex unsigned long long int", TYPE_CUINT, 18446744073709551615ULL, 0 },
115 { "_Complex float", TYPE_CFLOAT, 0, 0 },
116 { "_Complex double", TYPE_CFLOAT, 0, 0 },
117 /*{ "_Complex long double", TYPE_CFLOAT, 0, 0 }, */
118 /* Disable until long doubles are encoded correctly.   */
119 #define NCTYPES2 (sizeof (complex_types) / sizeof (complex_types[0]))
120 };
121 struct types vector_types[] = {
122 /* vector-defs.h typedefs */
123 { "v8qi", TYPE_OTHER, 0, 0 },
124 { "v16qi", TYPE_OTHER, 0, 0 },
125 { "v2hi", TYPE_OTHER, 0, 0 },
126 { "v4hi", TYPE_OTHER, 0, 0 },
127 { "v8hi", TYPE_OTHER, 0, 0 },
128 { "v2si", TYPE_OTHER, 0, 0 },
129 { "v4si", TYPE_OTHER, 0, 0 },
130 { "v1di", TYPE_OTHER, 0, 0 },
131 { "v2di", TYPE_OTHER, 0, 0 },
132 { "v2sf", TYPE_OTHER, 0, 0 },
133 { "v4sf", TYPE_OTHER, 0, 0 },
134 { "v16sf", TYPE_OTHER, 0, 0 },
135 { "v2df", TYPE_OTHER, 0, 0 },
136 { "u8qi", TYPE_OTHER, 0, 0 },
137 { "u16qi", TYPE_OTHER, 0, 0 },
138 { "u2hi", TYPE_OTHER, 0, 0 },
139 { "u4hi", TYPE_OTHER, 0, 0 },
140 { "u8hi", TYPE_OTHER, 0, 0 },
141 { "u2si", TYPE_OTHER, 0, 0 },
142 { "u4si", TYPE_OTHER, 0, 0 },
143 { "u1di", TYPE_OTHER, 0, 0 },
144 { "u2di", TYPE_OTHER, 0, 0 },
145 { "u2sf", TYPE_OTHER, 0, 0 },
146 { "u4sf", TYPE_OTHER, 0, 0 },
147 { "u16sf", TYPE_OTHER, 0, 0 },
148 { "u2df", TYPE_OTHER, 0, 0 },
149 { "__m64", TYPE_OTHER, 0, 0 },
150 { "__m128", TYPE_OTHER, 0, 0 }
151 #define NVTYPES2 (sizeof (vector_types) / sizeof (vector_types[0]))
152 };
153 
154 struct types bitfld_types[NTYPES2];
155 int n_bitfld_types;
156 
157 enum ETYPE
158 {
159   ETYPE_TYPE,
160   ETYPE_ARRAY,
161   ETYPE_BITFLD,
162   ETYPE_STRUCT,
163   ETYPE_UNION,
164   ETYPE_STRUCT_ARRAY,
165   ETYPE_UNION_ARRAY
166 };
167 
168 struct entry
169 {
170 #ifdef __GNUC__
171   enum ETYPE etype : 8;
172 #else
173   unsigned char etype;
174 #endif
175   unsigned short len;
176   unsigned char arr_len;
177   struct types *type;
178   const char *attrib;
179   /* Used to chain together entries in the hash table.  */
180   struct entry *next;
181 };
182 
183 /* A prime number giving the number of slots in the hash table.  */
184 #define HASH_SIZE 32749
185 static struct entry *hash_table[HASH_SIZE];
186 
187 static int idx, limidx, output_one;
188 static const char *destdir;
189 static const char *srcdir;
190 FILE *outfile;
191 
192 void
switchfiles(int fields)193 switchfiles (int fields)
194 {
195   static int filecnt;
196   static char *destbuf, *destptr;
197   ++filecnt;
198   if (outfile)
199     fclose (outfile);
200   if (output_one)
201     {
202       outfile = stdout;
203       return;
204     }
205   if (destbuf == NULL)
206     {
207       size_t len = strlen (destdir);
208       destbuf = malloc (len + 20);
209       if (!destbuf)
210 	abort ();
211       memcpy (destbuf, destdir, len);
212       if (!len || destbuf[len - 1] != '/')
213 	destbuf[len++] = '/';
214       destptr = destbuf + len;
215     }
216   sprintf (destptr, "t%03d_main.m", filecnt);
217   outfile = fopen (destbuf, "w");
218   if (outfile == NULL)
219     {
220     fail:
221       fputs ("failed to create test files\n", stderr);
222       exit (1);
223     }
224   /* FIXME: these tests should not be xfailed on aix but they are because
225      libobjc uses GCC's headers for trying to find the struct layout but it
226      gets it wrong.  */
227   if (filecnt == 2
228       || filecnt == 3
229       || filecnt == 4
230       || filecnt == 6
231       || filecnt == 7
232       || filecnt == 8
233       || filecnt == 11
234       || filecnt == 12
235       || filecnt == 15
236       || filecnt == 22)
237      {
238       fprintf (outfile, "\
239 /* { dg-do run { xfail { powerpc*-*-aix* } } } */\n\
240 /* { dg-options \"-w -I%s -fgnu-runtime\" } */\n", srcdir);
241      }
242   /* FIXME: these should not be xfailed but they are because
243      of bugs in libobjc and the objc front-end.  25 is because
244      vectors are not encoded.  The rest are because or zero sized
245      arrays are encoded as pointers.  See PR objc/25361.  */
246   else if (filecnt == 25 || (filecnt >= 27 && filecnt <= 29))
247     {
248       fprintf (outfile, "\
249 /* { dg-do run { xfail { { i?86-*-* x86_64-*-* } || { powerpc*-apple-darwin* && ilp32 } } } } */\n\
250 /* { dg-options \"-w -I%s -fgnu-runtime\" } */\n", srcdir);
251     }
252   else if (filecnt >= 30)
253     {
254       fprintf (outfile, "\
255 /* { dg-do run { xfail { i?86-*-* x86_64-*-* } } } */\n\
256 /* { dg-options \"-w -I%s -fgnu-runtime\" } */\n", srcdir);
257     }
258   else
259     {
260       fprintf (outfile, "\
261 /* { dg-do run } */\n\
262 /* { dg-options \"-w -I%s -fgnu-runtime\" } */\n", srcdir);
263     }
264   fprintf(outfile, "#include <objc/runtime.h> \n\
265 #include \"struct-layout-1.h\"\n\
266 \n\
267 int fails; \n\
268 #define TX(n, type, attrs, fields, ops)                         \\\n\
269 type S##n { fields } attrs;                                     \\\n\
270 void test##n (void)                                             \\\n\
271 {                                                               \\\n\
272   char *encoding = @encode (type S##n);				\\\n\
273   if (objc_sizeof_type (encoding) != sizeof(type S##n)) 	\\\n\
274     {   	                                                \\\n\
275       fails ++;                                                 \\\n\
276       printf(#type \" { \" #fields \"} size is %%u, but is calulated as %%u\\n\", \\\n\
277       	      sizeof(type S##n), objc_sizeof_type (encoding));  \\\n\
278     }           	                                        \\\n\
279   if (objc_alignof_type (encoding) != __alignof__ (type S##n)) 	\\\n\
280     {   	                                                \\\n\
281       fails ++;                                                 \\\n\
282       printf(#type \" { \" #fields \"} align is %%u, but is calulated as %%u\\n\", \\\n\
283       	      __alignof__ (type S##n), objc_alignof_type (encoding));  \\\n\
284     }           	                                        \\\n\
285 }\n\
286 #include \"t%03d_test.h\"\n\
287 #undef TX\n\
288 \n\
289 int main (void)\n\
290 {\n\
291 #define TX(n, type, attrs, fields, ops)   test##n ();\n\
292 #include \"t%03d_test.h\"\n\
293 #undef TX\n\
294   if (fails)\n\
295     {\n\
296       fflush (stdout);\n\
297       abort ();\n\
298     }\n\
299   exit (0);\n\
300 }\n", filecnt, filecnt);
301   fclose (outfile);
302   sprintf (destptr, "t%03d_test.h", filecnt);
303   outfile = fopen (destbuf, "w");
304   if (outfile == NULL)
305     goto fail;
306   if (fields <= 2)
307     limidx = idx + 300;
308   else if (fields <= 4)
309     limidx = idx + 200;
310   else if (fields <= 6)
311     limidx = idx + 100;
312   else
313     limidx = idx + 50;
314 }
315 
316 unsigned long long int
getrandll(void)317 getrandll (void)
318 {
319   unsigned long long int ret;
320   ret = generate_random () & 0xffffff;
321   ret |= (generate_random () & 0xffffffLL) << 24;
322   ret |= ((unsigned long long int) generate_random ()) << 48;
323   return ret;
324 }
325 
326 int
subfield(struct entry * e,char * letter)327 subfield (struct entry *e, char *letter)
328 {
329   int i, type;
330   char buf[20];
331   const char *p;
332   switch (e[0].etype)
333     {
334     case ETYPE_STRUCT:
335     case ETYPE_UNION:
336     case ETYPE_STRUCT_ARRAY:
337     case ETYPE_UNION_ARRAY:
338       type = e[0].attrib ? 1 + (generate_random () & 3) : 0;
339       if (e[0].etype == ETYPE_STRUCT || e[0].etype == ETYPE_STRUCT_ARRAY)
340 	p = "struct";
341       else
342 	p = "union";
343       if (e[0].etype == ETYPE_STRUCT_ARRAY || e[0].etype == ETYPE_UNION_ARRAY)
344 	{
345 	  if (e[0].arr_len == 255)
346 	    snprintf (buf, 20, "%c[]", *letter);
347 	  else
348 	    snprintf (buf, 20, "%c[%d]", *letter, e[0].arr_len);
349 	}
350       else
351         {
352           buf[0] = *letter;
353           buf[1] = '\0';
354         }
355       ++*letter;
356       switch (type)
357         {
358         case 0:
359         case 3:
360         case 4:
361           fprintf (outfile, "%s{", p);
362           break;
363         case 1:
364           fprintf (outfile, "%s %s{", e[0].attrib, p);
365           break;
366         case 2:
367           fprintf (outfile, "%s %s{", p, e[0].attrib);
368           break;
369         }
370 
371       for (i = 1; i <= e[0].len; )
372 	i += subfield (e + i, letter);
373 
374       switch (type)
375         {
376         case 0:
377         case 1:
378         case 2:
379           fprintf (outfile, "}%s;", buf);
380           break;
381 	case 3:
382 	  fprintf (outfile, "}%s %s;", e[0].attrib, buf);
383 	  break;
384 	case 4:
385 	  fprintf (outfile, "}%s %s;", buf, e[0].attrib);
386 	  break;
387         }
388       return 1 + e[0].len;
389     case ETYPE_TYPE:
390     case ETYPE_ARRAY:
391       if (e[0].etype == ETYPE_ARRAY)
392 	{
393 	  if (e[0].arr_len == 255)
394 	    snprintf (buf, 20, "%c[]", *letter);
395 	  else
396 	    snprintf (buf, 20, "%c[%d]", *letter, e[0].arr_len);
397 	}
398       else
399         {
400           buf[0] = *letter;
401           buf[1] = '\0';
402         }
403       ++*letter;
404       if (e[0].attrib)
405 	switch (generate_random () % 3)
406           {
407           case 0:
408             fprintf (outfile, "%s %s %s;", e[0].attrib, e[0].type->name, buf);
409             break;
410           case 1:
411             fprintf (outfile, "%s %s %s;", e[0].type->name, e[0].attrib, buf);
412             break;
413           case 2:
414             fprintf (outfile, "%s %s %s;", e[0].type->name, buf, e[0].attrib);
415             break;
416           }
417       else
418 	fprintf (outfile, "%s %s;", e[0].type->name, buf);
419       return 1;
420     case ETYPE_BITFLD:
421       if (e[0].len == 0)
422 	{
423 	  if (e[0].attrib)
424 	    switch (generate_random () % 3)
425 	      {
426 	      case 0:
427 		fprintf (outfile, "%s %s:0;", e[0].attrib, e[0].type->name);
428 		break;
429 	      case 1:
430 		fprintf (outfile, "%s %s:0;", e[0].type->name, e[0].attrib);
431 		break;
432 	      case 2:
433 		fprintf (outfile, "%s:0 %s;", e[0].type->name, e[0].attrib);
434 		break;
435 	      }
436 	  else
437 	    fprintf (outfile, "%s:0;", e[0].type->name);
438 	  ++*letter;
439 	  return 1;
440 	}
441       switch (e[0].type->bitfld)
442 	{
443 	case 'C':
444 	case 'S':
445 	case 'I':
446 	case 'L':
447 	case 'Q':
448 	  snprintf (buf, 20, "B%cN(%d)", e[0].type->bitfld, e[0].len);
449 	  break;
450 	case 'B':
451 	case ' ':
452 	  snprintf (buf, 20, "%d", e[0].len);
453 	  break;
454 	default:
455 	  abort ();
456 	}
457       if (e[0].attrib)
458 	switch (generate_random () % 3)
459 	  {
460 	  case 0:
461 	    fprintf (outfile, "%s %s %c:%s;", e[0].attrib, e[0].type->name,
462 		     *letter, buf);
463 	    break;
464 	  case 1:
465 	    fprintf (outfile, "%s %s %c:%s;", e[0].type->name, e[0].attrib,
466 		     *letter, buf);
467 	    break;
468 	  case 2:
469 	    fprintf (outfile, "%s %c:%s %s;", e[0].type->name, *letter,
470 		     buf, e[0].attrib);
471 	    break;
472 	  }
473       else
474 	fprintf (outfile, "%s %c:%s;", e[0].type->name, *letter, buf);
475       ++*letter;
476       return 1;
477     default:
478       abort ();
479   }
480 }
481 
482 char namebuf[1024];
483 
484 void
output_FNB(char mode,struct entry * e)485 output_FNB (char mode, struct entry *e)
486 {
487   unsigned long long int l1, l2, m;
488   int signs = 0;
489   const char *p, *q;
490 
491   if (e->type->type == TYPE_OTHER)
492     {
493       if (mode == 'B')
494         abort ();
495       fprintf (outfile, "N(%d,%s)", idx, namebuf);
496       return;
497     }
498   fprintf (outfile, "%c(%d,%s,", mode, idx, namebuf);
499   l1 = getrandll ();
500   l2 = getrandll ();
501   switch (e->type->type)
502     {
503     case TYPE_INT:
504       signs = generate_random () & 3;
505       m = e->type->maxval;
506       if (mode == 'B')
507 	m &= e->len > 1 ? (1ULL << (e->len - 1)) - 1 : 1;
508       l1 &= m;
509       l2 &= m;
510       fprintf (outfile, "%s%llu%s,%s%llu%s",
511 	       (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "",
512 	       (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : "");
513       break;
514     case TYPE_UINT:
515       m = e->type->maxval;
516       if (mode == 'B')
517 	m &= (1ULL << e->len) - 1;
518       l1 &= m;
519       l2 &= m;
520       fprintf (outfile, "%lluU%s,%lluU%s", l1, l1 > 4294967295U ? "LL" : "",
521 	       l2, l2 > 4294967295U ? "LL" : "");
522       break;
523     case TYPE_FLOAT:
524       l1 &= 0xffffff;
525       l2 &= 0xffffff;
526       signs = generate_random () & 3;
527       fprintf (outfile, "%s%f,%s%f", (signs & 1) ? "-" : "",
528 	       ((double) l1) / 64, (signs & 2) ? "-" : "", ((double) l2) / 64);
529       break;
530     case TYPE_CINT:
531       signs = generate_random () & 3;
532       l1 &= e->type->maxval;
533       l2 &= e->type->maxval;
534       fprintf (outfile, "CINT(%s%llu%s,%s%llu%s),",
535 	       (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "",
536 	       (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : "");
537       signs = generate_random () & 3;
538       l1 = getrandll ();
539       l2 = getrandll ();
540       l1 &= e->type->maxval;
541       l2 &= e->type->maxval;
542       fprintf (outfile, "CINT(%s%llu%s,%s%llu%s)",
543 	       (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "",
544 	       (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : "");
545       break;
546     case TYPE_CUINT:
547       l1 &= e->type->maxval;
548       l2 &= e->type->maxval;
549       fprintf (outfile, "CINT(%lluU%s,%lluU%s),",
550 	       l1, l1 > 4294967295U ? "LL" : "",
551 	       l2, l2 > 4294967295U ? "LL" : "");
552       l1 = getrandll ();
553       l2 = getrandll ();
554       l1 &= e->type->maxval;
555       l2 &= e->type->maxval;
556       fprintf (outfile, "CINT(%lluU%s,%lluU%s)",
557 	       l1, l1 > 4294967295U ? "LL" : "",
558 	       l2, l2 > 4294967295U ? "LL" : "");
559       break;
560     case TYPE_CFLOAT:
561       l1 &= 0xffffff;
562       l2 &= 0xffffff;
563       signs = generate_random () & 3;
564       fprintf (outfile, "CDBL(%s%f,%s%f),",
565 	       (signs & 1) ? "-" : "", ((double) l1) / 64,
566 	       (signs & 2) ? "-" : "", ((double) l2) / 64);
567       l1 = getrandll ();
568       l2 = getrandll ();
569       l1 &= 0xffffff;
570       l2 &= 0xffffff;
571       signs = generate_random () & 3;
572       fprintf (outfile, "CDBL(%s%f,%s%f)",
573 	       (signs & 1) ? "-" : "", ((double) l1) / 64,
574 	       (signs & 2) ? "-" : "", ((double) l2) / 64);
575       break;
576     case TYPE_UENUM:
577       if (e->type->maxval == 0)
578 	fputs ("e0_0,e0_0", outfile);
579       else if (e->type->maxval == 1)
580         fprintf (outfile, "e1_%lld,e1_%lld", l1 & 1, l2 & 1);
581       else
582         {
583 	  p = strchr (e->type->name, '\0');
584 	  while (--p >= e->type->name && *p >= '0' && *p <= '9');
585 	  p++;
586           l1 %= 7;
587           l2 %= 7;
588           if (l1 > 3)
589             l1 += e->type->maxval - 6;
590           if (l2 > 3)
591             l2 += e->type->maxval - 6;
592 	  fprintf (outfile, "e%s_%lld,e%s_%lld", p, l1, p, l2);
593         }
594       break;
595     case TYPE_SENUM:
596       p = strchr (e->type->name, '\0');
597       while (--p >= e->type->name && *p >= '0' && *p <= '9');
598       p++;
599       l1 %= 7;
600       l2 %= 7;
601       fprintf (outfile, "e%s_%s%lld,e%s_%s%lld",
602 	       p, l1 < 3 ? "m" : "",
603 	       l1 == 3 ? 0LL : e->type->maxval - (l1 & 3),
604 	       p, l2 < 3 ? "m" : "",
605 	       l2 == 3 ? 0LL : e->type->maxval - (l2 & 3));
606       break;
607     case TYPE_PTR:
608       l1 %= 256;
609       l2 %= 256;
610       fprintf (outfile, "(%s)&intarray[%lld],(%s)&intarray[%lld]",
611 	       e->type->name, l1, e->type->name, l2);
612       break;
613     case TYPE_FNPTR:
614       l1 %= 10;
615       l2 %= 10;
616       fprintf (outfile, "fn%lld,fn%lld", l1, l2);
617       break;
618     default:
619       abort ();
620     }
621   fputs (")", outfile);
622 }
623 
624 int
subvalues(struct entry * e,char * p,char * letter)625 subvalues (struct entry *e, char *p, char *letter)
626 {
627   int i, j;
628   char *q;
629   if (p >= namebuf + sizeof (namebuf) - 32)
630     abort ();
631   p[0] = *letter;
632   p[1] = '\0';
633   q = p + 1;
634   switch (e[0].etype)
635     {
636     case ETYPE_STRUCT_ARRAY:
637     case ETYPE_UNION_ARRAY:
638       if (e[0].arr_len == 0 || e[0].arr_len == 255)
639 	{
640 	  *letter += 1 + e[0].len;
641 	  return 1 + e[0].len;
642 	}
643       i = generate_random () % e[0].arr_len;
644       snprintf (p, sizeof (namebuf) - (p - namebuf) - 1,
645 		"%c[%d]", *letter, i);
646       q = strchr (p, '\0');
647       /* FALLTHROUGH */
648     case ETYPE_STRUCT:
649     case ETYPE_UNION:
650       *q++ = '.';
651       ++*letter;
652       for (i = 1; i <= e[0].len; )
653 	{
654 	  i += subvalues (e + i, q, letter);
655 	  if (e[0].etype == ETYPE_UNION || e[0].etype == ETYPE_UNION_ARRAY)
656 	    {
657 	      *letter += e[0].len - i + 1;
658 	      break;
659 	    }
660 	}
661       return 1 + e[0].len;
662     case ETYPE_TYPE:
663       ++*letter;
664       output_FNB ('F', e);
665       return 1;
666     case ETYPE_ARRAY:
667       if (e[0].arr_len == 0 || e[0].arr_len == 255)
668 	{
669 	  ++*letter;
670 	  return 1;
671 	}
672       i = generate_random () % e[0].arr_len;
673       snprintf (p, sizeof (namebuf) - (p - namebuf),
674 		"%c[%d]", *letter, i);
675       output_FNB ('F', e);
676       if ((generate_random () & 7) == 0)
677 	{
678 	  j = generate_random () % e[0].arr_len;
679 	  if (i != j)
680 	    {
681 	      snprintf (p, sizeof (namebuf) - (p - namebuf),
682 			"%c[%d]", *letter, j);
683 	      output_FNB ('F', e);
684 	    }
685 	}
686       ++*letter;
687       return 1;
688     case ETYPE_BITFLD:
689       ++*letter;
690       if (e[0].len != 0)
691 	output_FNB ('B', e);
692       return 1;
693     }
694 }
695 
696 /* DERIVED FROM:
697 --------------------------------------------------------------------
698 lookup2.c, by Bob Jenkins, December 1996, Public Domain.
699 hash(), hash2(), hash3, and mix() are externally useful functions.
700 Routines to test the hash are included if SELF_TEST is defined.
701 You can use this free for any purpose.  It has no warranty.
702 --------------------------------------------------------------------
703 */
704 
705 /*
706 --------------------------------------------------------------------
707 mix -- mix 3 32-bit values reversibly.
708 For every delta with one or two bit set, and the deltas of all three
709   high bits or all three low bits, whether the original value of a,b,c
710   is almost all zero or is uniformly distributed,
711 * If mix() is run forward or backward, at least 32 bits in a,b,c
712   have at least 1/4 probability of changing.
713 * If mix() is run forward, every bit of c will change between 1/3 and
714   2/3 of the time.  (Well, 22/100 and 78/100 for some 2-bit deltas.)
715 mix() was built out of 36 single-cycle latency instructions in a
716   structure that could supported 2x parallelism, like so:
717       a -= b;
718       a -= c; x = (c>>13);
719       b -= c; a ^= x;
720       b -= a; x = (a<<8);
721       c -= a; b ^= x;
722       c -= b; x = (b>>13);
723       ...
724   Unfortunately, superscalar Pentiums and Sparcs can't take advantage
725   of that parallelism.  They've also turned some of those single-cycle
726   latency instructions into multi-cycle latency instructions.  Still,
727   this is the fastest good hash I could find.  There were about 2^^68
728   to choose from.  I only looked at a billion or so.
729 --------------------------------------------------------------------
730 */
731 /* same, but slower, works on systems that might have 8 byte hashval_t's */
732 #define mix(a,b,c) \
733 { \
734   a -= b; a -= c; a ^= (c>>13); \
735   b -= c; b -= a; b ^= (a<< 8); \
736   c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
737   a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
738   b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
739   c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
740   a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
741   b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
742   c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
743 }
744 
745 /*
746 --------------------------------------------------------------------
747 hash() -- hash a variable-length key into a 32-bit value
748   k     : the key (the unaligned variable-length array of bytes)
749   len   : the length of the key, counting by bytes
750   level : can be any 4-byte value
751 Returns a 32-bit value.  Every bit of the key affects every bit of
752 the return value.  Every 1-bit and 2-bit delta achieves avalanche.
753 About 36+6len instructions.
754 
755 The best hash table sizes are powers of 2.  There is no need to do
756 mod a prime (mod is sooo slow!).  If you need less than 32 bits,
757 use a bitmask.  For example, if you need only 10 bits, do
758   h = (h & hashmask(10));
759 In which case, the hash table should have hashsize(10) elements.
760 
761 If you are hashing n strings (ub1 **)k, do it like this:
762   for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
763 
764 By Bob Jenkins, 1996.  bob_jenkins@burtleburtle.net.  You may use this
765 code any way you wish, private, educational, or commercial.  It's free.
766 
767 See http://burtleburtle.net/bob/hash/evahash.html
768 Use for hash table lookup, or anything where one collision in 2^32 is
769 acceptable.  Do NOT use for cryptographic purposes.
770 --------------------------------------------------------------------
771 */
772 
773 static hashval_t
iterative_hash(const void * k_in,register size_t length,register hashval_t initval)774 iterative_hash (const void *k_in /* the key */,
775                 register size_t  length /* the length of the key */,
776                 register hashval_t initval /* the previous hash, or
777                                               an arbitrary value */)
778 {
779   register const unsigned char *k = (const unsigned char *)k_in;
780   register hashval_t a,b,c,len;
781 
782   /* Set up the internal state */
783   len = length;
784   a = b = 0x9e3779b9;  /* the golden ratio; an arbitrary value */
785   c = initval;           /* the previous hash value */
786 
787   /*---------------------------------------- handle most of the key */
788     while (len >= 12)
789       {
790 	a += (k[0] +((hashval_t)k[1]<<8) +((hashval_t)k[2]<<16) +((hashval_t)k[3]<<24));
791 	b += (k[4] +((hashval_t)k[5]<<8) +((hashval_t)k[6]<<16) +((hashval_t)k[7]<<24));
792 	c += (k[8] +((hashval_t)k[9]<<8) +((hashval_t)k[10]<<16)+((hashval_t)k[11]<<24));
793 	mix(a,b,c);
794 	k += 12; len -= 12;
795       }
796 
797   /*------------------------------------- handle the last 11 bytes */
798   c += length;
799   switch(len)              /* all the case statements fall through */
800     {
801     case 11: c+=((hashval_t)k[10]<<24);
802     case 10: c+=((hashval_t)k[9]<<16);
803     case 9 : c+=((hashval_t)k[8]<<8);
804       /* the first byte of c is reserved for the length */
805     case 8 : b+=((hashval_t)k[7]<<24);
806     case 7 : b+=((hashval_t)k[6]<<16);
807     case 6 : b+=((hashval_t)k[5]<<8);
808     case 5 : b+=k[4];
809     case 4 : a+=((hashval_t)k[3]<<24);
810     case 3 : a+=((hashval_t)k[2]<<16);
811     case 2 : a+=((hashval_t)k[1]<<8);
812     case 1 : a+=k[0];
813       /* case 0: nothing left to add */
814     }
815   mix(a,b,c);
816   /*-------------------------------------------- report the result */
817   return c;
818 }
819 
820 hashval_t
e_hash(const void * a)821 e_hash (const void *a)
822 {
823   const struct entry *e = a;
824   hashval_t ret = 0;
825   int i;
826 
827   if (e[0].etype != ETYPE_STRUCT && e[0].etype != ETYPE_UNION)
828     abort ();
829   for (i = 0; i <= e[0].len; ++i)
830     {
831       int attriblen;
832       ret = iterative_hash (&e[i], offsetof (struct entry, attrib), ret);
833       attriblen = e[i].attrib ? strlen (e[i].attrib) : -1;
834       ret = iterative_hash (&attriblen, sizeof (int), ret);
835       if (e[i].attrib)
836         ret = iterative_hash (e[i].attrib, attriblen, ret);
837     }
838   return ret;
839 }
840 
841 int
e_eq(const void * a,const void * b)842 e_eq (const void *a, const void *b)
843 {
844   const struct entry *ea = a, *eb = b;
845   int i;
846   if (ea[0].etype != ETYPE_STRUCT && ea[0].etype != ETYPE_UNION)
847     abort ();
848   if (ea[0].len != eb[0].len)
849     return 0;
850   for (i = 0; i <= ea[0].len; ++i)
851     {
852       if (ea[i].etype != eb[i].etype
853 	  || ea[i].len != eb[i].len
854 	  || ea[i].arr_len != eb[i].arr_len
855 	  || ea[i].type != eb[i].type)
856 	return 0;
857       if ((ea[i].attrib == NULL) ^ (eb[i].attrib == NULL))
858 	return 0;
859       if (ea[i].attrib && strcmp (ea[i].attrib, eb[i].attrib) != 0)
860 	return 0;
861     }
862   return 1;
863 }
864 
865 static int
e_exists(const struct entry * e)866 e_exists (const struct entry *e)
867 {
868   struct entry *h;
869   hashval_t hval;
870 
871   hval = e_hash (e);
872   for (h = hash_table[hval % HASH_SIZE]; h; h = h->next)
873     if (e_eq (e, h))
874       return 1;
875   return 0;
876 }
877 
878 static void
e_insert(struct entry * e)879 e_insert (struct entry *e)
880 {
881   hashval_t hval;
882 
883   hval = e_hash (e);
884   e->next = hash_table[hval % HASH_SIZE];
885   hash_table[hval % HASH_SIZE] = e;
886 }
887 
888 void
output(struct entry * e)889 output (struct entry *e)
890 {
891   int i;
892   char c;
893   struct entry *n;
894   const char *skip_cint = "";
895 
896   if (e[0].etype != ETYPE_STRUCT && e[0].etype != ETYPE_UNION)
897     abort ();
898 
899   if (e_exists (e))
900     return;
901 
902   n = (struct entry *) malloc ((e[0].len + 1) * sizeof (struct entry));
903   memcpy (n, e, (e[0].len + 1) * sizeof (struct entry));
904   e_insert (n);
905 
906   if (idx == limidx)
907     switchfiles (e[0].len);
908 
909   for (i = 1; i <= e[0].len; ++i)
910     if ((e[i].etype == ETYPE_TYPE || e[i].etype == ETYPE_ARRAY)
911 	&& (e[i].type->type == TYPE_CINT || e[i].type->type == TYPE_CUINT))
912       break;
913   if (i <= e[0].len)
914     skip_cint = "CI";
915   if (e[0].attrib)
916     fprintf (outfile, (generate_random () & 1)
917 	     ? "TX%s(%d,%s %s,," : "TX%s(%d,%s,%s,", skip_cint,
918 	     idx, e[0].etype == ETYPE_STRUCT ? "struct" : "union",
919 	     e[0].attrib);
920   else if (e[0].etype == ETYPE_STRUCT)
921     fprintf (outfile, "T%s(%d,", skip_cint, idx);
922   else
923     fprintf (outfile, "U%s(%d,", skip_cint, idx);
924   c = 'a';
925   for (i = 1; i <= e[0].len; )
926     i += subfield (e + i, &c);
927   fputs (",", outfile);
928   c = 'a';
929   for (i = 1; i <= e[0].len; )
930     {
931       i += subvalues (e + i, namebuf, &c);
932       if (e[0].etype == ETYPE_UNION)
933         break;
934     }
935   fputs (")\n", outfile);
936   if (output_one && idx == limidx)
937     exit (0);
938   ++idx;
939 }
940 
941 enum FEATURE
942 {
943   FEATURE_VECTOR = 1,
944   FEATURE_COMPLEX = 2,
945   FEATURE_ZEROARRAY = 8,
946   FEATURE_ZEROBITFLD = 16,
947   ALL_FEATURES = FEATURE_COMPLEX | FEATURE_VECTOR | FEATURE_ZEROARRAY
948 		 | FEATURE_ZEROBITFLD
949 };
950 
951 void
singles(enum FEATURE features)952 singles (enum FEATURE features)
953 {
954   struct entry e[2];
955   int i;
956   memset (e, 0, sizeof (e));
957   e[0].etype = ETYPE_STRUCT;
958   output (e);
959   e[0].etype = ETYPE_UNION;
960   output (e);
961   e[0].len = 1;
962   e[0].attrib = NULL;
963   for (i = 0; i < NTYPES2; ++i)
964     {
965       e[0].etype = ETYPE_STRUCT;
966       e[1].etype = ETYPE_TYPE;
967       e[1].type = &base_types[i];
968       output (e);
969       e[0].etype = ETYPE_UNION;
970       output (e);
971     }
972   if (features & FEATURE_COMPLEX)
973     for (i = 0; i < NCTYPES2; ++i)
974       {
975 	e[0].etype = ETYPE_STRUCT;
976 	e[1].etype = ETYPE_TYPE;
977 	e[1].type = &complex_types[i];
978 	output (e);
979 	e[0].etype = ETYPE_UNION;
980 	output (e);
981       }
982   if (features & FEATURE_VECTOR)
983     for (i = 0; i < NVTYPES2; ++i)
984       {
985 	e[0].etype = ETYPE_STRUCT;
986 	e[1].etype = ETYPE_TYPE;
987 	e[1].type = &vector_types[i];
988 	output (e);
989 	e[0].etype = ETYPE_UNION;
990 	output (e);
991       }
992 }
993 
994 void
choose_type(enum FEATURE features,struct entry * e,int r,int in_array)995 choose_type (enum FEATURE features, struct entry *e, int r, int in_array)
996 {
997   int i;
998 
999   i = NTYPES2 - NTYPES1;
1000   if (features & FEATURE_COMPLEX)
1001     i += NCTYPES2;
1002   if (features & FEATURE_VECTOR)
1003     i += NVTYPES2;
1004   r >>= 2;
1005   r %= i;
1006   if (r < NTYPES2 - NTYPES1)
1007     e->type = &base_types[r + NTYPES1];
1008   r -= NTYPES2 - NTYPES1;
1009   if (e->type == NULL && (features & FEATURE_COMPLEX))
1010     {
1011       if (r < NCTYPES2)
1012 	e->type = &complex_types[r];
1013       r -= NCTYPES2;
1014     }
1015   if (e->type == NULL && (features & FEATURE_VECTOR))
1016     {
1017       if (r < NVTYPES2)
1018 	e->type = &vector_types[r];
1019       r -= NVTYPES2;
1020     }
1021     if (e->type == NULL)
1022     abort ();
1023 }
1024 
1025 /* This is from gcc.c-torture/execute/builtin-bitops-1.c.  */
1026 static int
my_ffsll(unsigned long long x)1027 my_ffsll (unsigned long long x)
1028 {
1029   int i;
1030   if (x == 0)
1031     return 0;
1032   /* We've tested LLONG_MAX for 64 bits so this should be safe.  */
1033   for (i = 0; i < 64; i++)
1034     if (x & (1ULL << i))
1035       break;
1036   return i + 1;
1037 }
1038 
1039 void
generate_fields(enum FEATURE features,struct entry * e,struct entry * parent,int len)1040 generate_fields (enum FEATURE features, struct entry *e, struct entry *parent,
1041 		 int len)
1042 {
1043   int r, i, j, ret = 1, n, incr, sametype;
1044 
1045   for (n = 0; n < len; n += incr)
1046     {
1047       r = generate_random ();
1048       /* 50% ETYPE_TYPE base_types NTYPES1
1049 	 12.5% ETYPE_TYPE other
1050 	 12.5% ETYPE_ARRAY
1051 	 12.5% ETYPE_BITFLD
1052 	 12.5% ETYPE_STRUCT|ETYPE_UNION|ETYPE_STRUCT_ARRAY|ETYPE_UNION_ARRAY */
1053       i = (r & 7);
1054       r >>= 3;
1055       incr = 1;
1056       switch (i)
1057 	{
1058 	case 6: /* BITfields disabled for now as _Bool bitfields are broken. */
1059 	case 0:
1060 	case 1:
1061 	case 2:
1062 	case 3:
1063 	  e[n].etype = ETYPE_TYPE;
1064 	  e[n].type = &base_types[r % NTYPES1];
1065 	  break;
1066 	case 4:
1067 	  e[n].etype = ETYPE_TYPE;
1068 	  choose_type (features, &e[n], r, 0);
1069 	  break;
1070 	case 5:
1071 	  e[n].etype = ETYPE_ARRAY;
1072 	  i = r & 1;
1073 	  r >>= 1;
1074 	  if (i)
1075 	    e[n].type = &base_types[r % NTYPES1];
1076 	  else
1077 	    choose_type (features, &e[n], r, 1);
1078 	  r = generate_random ();
1079 	  if ((features & FEATURE_ZEROARRAY) && (r & 3) == 0)
1080 	    {
1081 	      e[n].arr_len = 0;
1082 	      if (n == len - 1 && (r & 4)
1083 		  && (parent->etype == ETYPE_STRUCT
1084 		      || parent->etype == ETYPE_STRUCT_ARRAY))
1085 		{
1086 		  int k;
1087 		  for (k = 0; k < n; ++k)
1088 		    if (e[k].etype != ETYPE_BITFLD || e[k].len)
1089 		      {
1090 			e[n].arr_len = 255;
1091 			break;
1092 		      }
1093 		}
1094 	    }
1095 	  else if ((r & 3) != 3)
1096 	    e[n].arr_len = (r >> 2) & 7;
1097 	  else
1098 	    e[n].arr_len = (r >> 2) & 31;
1099 	  break;
1100 #if 0
1101 	case 6:
1102 	  sametype = 1;
1103 	  switch (r & 7)
1104 	    {
1105 	    case 0:
1106 	    case 1:
1107 	    case 2:
1108 	      break;
1109 	    case 3:
1110 	    case 4:
1111 	    case 5:
1112 	      incr = 1 + (r >> 3) % (len - n);
1113 	      break;
1114 	    case 6:
1115 	    case 7:
1116 	      sametype = 0;
1117 	      incr = 1 + (r >> 3) % (len - n);
1118 	      break;
1119 	    }
1120 	  for (j = n; j < n + incr; ++j)
1121 	    {
1122 	      int mi, ma;
1123 
1124 	      e[j].etype = ETYPE_BITFLD;
1125 	      if (j == n || !sametype)
1126 		{
1127 		  r = generate_random ();
1128 		  r >>= 2;
1129 		  e[j].type
1130 		      = &bitfld_types[r % n_bitfld_types];
1131 		}
1132 	      else
1133 		e[j].type = e[n].type;
1134 	      r = generate_random ();
1135 	      mi = 0;
1136 	      ma = 0;
1137 	      switch (e[j].type->bitfld)
1138 	        {
1139 	        case 'C': ma = 8; break;
1140 	        case 'S': ma = 16; break;
1141 	        case 'I': ma = 32; break;
1142 	        case 'L':
1143 	        case 'Q': ma = 64; break;
1144 	        case 'B': ma = 1; break;
1145 	        case ' ':
1146 		  if (e[j].type->type == TYPE_UENUM)
1147 		    mi = my_ffsll (e[j].type->maxval + 1) - 1;
1148 		  else if (e[j].type->type == TYPE_SENUM)
1149 		    mi = my_ffsll (e[j].type->maxval + 1);
1150 		  else
1151 		    abort ();
1152 		  if (!mi)
1153 		    mi = 1;
1154 		  if (mi <= 32)
1155 		    ma = 32;
1156 		  else
1157 		    ma = 64;
1158 		  break;
1159 		default:
1160 		  abort ();
1161 	        }
1162 	      e[j].len = ma + 1;
1163 	      if (sametype && (r & 3) == 0 && ma > 1)
1164 		{
1165 		  int sum = 0, k;
1166 		  for (k = n; k < j; ++k)
1167 		    sum += e[k].len;
1168 		  sum %= ma;
1169 		  e[j].len = sum ? ma - sum : ma;
1170 		}
1171 	      r >>= 2;
1172 	      if (! (features & FEATURE_ZEROBITFLD) && mi == 0)
1173 		mi = 1;
1174 	      if (e[j].len < mi || e[j].len > ma)
1175 		e[j].len = mi + (r % (ma + 1 - mi));
1176 	      r >>= 6;
1177 	      if ((features & FEATURE_ZEROBITFLD) && (r & 3) == 0
1178 		  && mi == 0)
1179 		e[j].len = 0;
1180 	    }
1181 	  break;
1182 #endif
1183 	case 7:
1184 	  switch (r & 7)
1185 	    {
1186 	    case 0:
1187 	    case 1:
1188 	    case 2:
1189 	      e[n].etype = ETYPE_STRUCT;
1190 	      break;
1191 	    case 3:
1192 	    case 4:
1193 	      e[n].etype = ETYPE_UNION;
1194 	      break;
1195 	    case 5:
1196 	    case 6:
1197 	      e[n].etype = ETYPE_STRUCT_ARRAY;
1198 	      break;
1199 	    case 7:
1200 	      e[n].etype = ETYPE_UNION_ARRAY;
1201 	      break;
1202 	    }
1203 	  r >>= 3;
1204 	  e[n].len = r % (len - n);
1205 	  incr = 1 + e[n].len;
1206 	  generate_fields (features, &e[n + 1], &e[n], e[n].len);
1207 	  if (e[n].etype == ETYPE_STRUCT_ARRAY
1208 	      || e[n].etype == ETYPE_UNION_ARRAY)
1209 	    {
1210 	      r = generate_random ();
1211 	      if ((features & FEATURE_ZEROARRAY) && (r & 3) == 0)
1212 		{
1213 		  e[n].arr_len = 0;
1214 		  if (n + incr == len && (r & 4)
1215 		      && (parent->etype == ETYPE_STRUCT
1216 			  || parent->etype == ETYPE_STRUCT_ARRAY))
1217 		    {
1218 		      int k;
1219 		      for (k = 0; k < n; ++k)
1220 			if (e[k].etype != ETYPE_BITFLD || e[k].len)
1221 			  {
1222 			    e[n].arr_len = 255;
1223 			    break;
1224 			  }
1225 		    }
1226 		}
1227 	      else if ((r & 3) != 3)
1228 		e[n].arr_len = (r >> 2) & 7;
1229 	      else
1230 		e[n].arr_len = (r >> 2) & 31;
1231 	    }
1232 	  break;
1233 	}
1234     }
1235 }
1236 
1237 void
generate_random_tests(enum FEATURE features,int len)1238 generate_random_tests (enum FEATURE features, int len)
1239 {
1240   struct entry e[len + 1];
1241   int i, r;
1242   if (len > 'z' - 'a' + 1)
1243     abort ();
1244   memset (e, 0, sizeof (e));
1245   r = generate_random ();
1246   if ((r & 7) == 0)
1247     e[0].etype = ETYPE_UNION;
1248   else
1249     e[0].etype = ETYPE_STRUCT;
1250   r >>= 3;
1251   e[0].len = len;
1252   generate_fields (features, &e[1], &e[0], len);
1253   output (e);
1254 }
1255 
1256 struct { const char *name; enum FEATURE f; }
1257 features[] = {
1258 { "normal", 0 },
1259 { "complex", FEATURE_COMPLEX },
1260 { "vector", FEATURE_VECTOR },
1261 { "[0] :0", FEATURE_ZEROARRAY | FEATURE_ZEROBITFLD },
1262 { "complex vector [0]",
1263   FEATURE_COMPLEX | FEATURE_VECTOR | FEATURE_ZEROARRAY }
1264 };
1265 
1266 int
main(int argc,char ** argv)1267 main (int argc, char **argv)
1268 {
1269   int i, j, count, c, n = 3000;
1270   char *optarg;
1271 
1272   if (sizeof (int) != 4 || sizeof (long long) != 8)
1273     return 1;
1274 
1275   i = 1;
1276   while (i < argc)
1277     {
1278       c = '\0';
1279       if (argv[i][0] == '-' && argv[i][2] == '\0')
1280 	c = argv[i][1];
1281       optarg = argv[i + 1];
1282       if (!optarg)
1283 	goto usage;
1284       switch (c)
1285 	{
1286 	case 'n':
1287 	  n = atoi (optarg);
1288 	  break;
1289 	case 'd':
1290 	  destdir = optarg;
1291 	  break;
1292 	case 's':
1293 	  srcdir = optarg;
1294 	  break;
1295 	case 'i':
1296 	  output_one = 1;
1297 	  limidx = atoi (optarg);
1298 	  break;
1299 	default:
1300 	  fprintf (stderr, "unrecognized option %s\n", argv[i]);
1301 	  goto usage;
1302       }
1303       i += 2;
1304     }
1305 
1306   if (output_one)
1307     {
1308       outfile = fopen ("/dev/null", "w");
1309       if (outfile == NULL)
1310         {
1311 	  fputs ("could not open /dev/null", stderr);
1312 	  return 1;
1313         }
1314       n = limidx + 1;
1315     }
1316 
1317   if (destdir == NULL && !output_one)
1318     {
1319     usage:
1320       fprintf (stderr, "Usage:\n\
1321 %s [-s srcdir -d destdir] [-n count] [-i idx]\n\
1322 Either -s srcdir -d destdir or -i idx must be used\n", argv[0]);
1323       return 1;
1324     }
1325 
1326   if (srcdir == NULL && !output_one)
1327     goto usage;
1328 
1329   for (i = 0; i < NTYPES2; ++i)
1330     if (base_types[i].bitfld)
1331       bitfld_types[n_bitfld_types++] = base_types[i];
1332   for (i = 0; i < sizeof (features) / sizeof (features[0]); ++i)
1333     {
1334       int startidx = idx;
1335       if (! output_one)
1336 	limidx = idx;
1337       if (!i)
1338         count = 200;
1339       else
1340         count = 20;
1341       for (j = 1; j <= 9; ++j)
1342         while (idx < startidx + j * count)
1343 	  generate_random_tests (features[i].f, j);
1344       while (idx < startidx + count * 10)
1345 	generate_random_tests (features[i].f, 10 + (generate_random () % 16));
1346     }
1347   for (i = 0; n > 3000 && i < sizeof (features) / sizeof (features[0]); ++i)
1348     {
1349       int startidx;
1350       startidx = idx;
1351       if (! output_one)
1352 	limidx = idx;
1353       singles (features[i].f);
1354       if (!i)
1355 	{
1356 	  count = 1000;
1357 	  while (idx < startidx + 1000)
1358 	    generate_random_tests (features[i].f, 1);
1359 	}
1360       else
1361 	{
1362 	  startidx = idx;
1363 	  count = 100;
1364 	  while (idx < startidx + 100)
1365 	    generate_random_tests (features[i].f, 1);
1366 	}
1367       startidx = idx;
1368       for (j = 2; j <= 9; ++j)
1369 	while (idx < startidx + (j - 1) * count)
1370 	  generate_random_tests (features[i].f, j);
1371       while (idx < startidx + count * 9)
1372         generate_random_tests (features[i].f, 10 + (generate_random () % 16));
1373     }
1374   if (! output_one)
1375     limidx = idx;
1376   while (idx < n)
1377     generate_random_tests (ALL_FEATURES, 1 + (generate_random () % 25));
1378   fclose (outfile);
1379   return 0;
1380 }
1381