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