fix libobjc ppc64 abi for darwin.
[official-gcc.git] / gcc / testsuite / objc.dg / gnu-encoding / struct-layout-encoding-1_generate.c
blob3ac4179c04761828142ec8538a02f44c01b004c6
1 /* Structure layout test generator.
2 Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <jakub@redhat.com>.
5 This file is part of GCC.
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.
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.
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/>. */
21 /* Compile with gcc -o struct-layout-1_generate{,.c} generate_random{,_r}.c */
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"
34 #if LLONG_MAX != 9223372036854775807LL && __LONG_LONG_MAX__ != 9223372036854775807LL
35 # error Need 64-bit long long
36 #endif
38 typedef unsigned int hashval_t;
40 enum TYPE
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
55 struct types
57 const char *name;
58 enum TYPE type;
59 unsigned long long int maxval;
60 char bitfld;
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]))
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]))
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]))
154 struct types bitfld_types[NTYPES2];
155 int n_bitfld_types;
157 enum ETYPE
159 ETYPE_TYPE,
160 ETYPE_ARRAY,
161 ETYPE_BITFLD,
162 ETYPE_STRUCT,
163 ETYPE_UNION,
164 ETYPE_STRUCT_ARRAY,
165 ETYPE_UNION_ARRAY
168 struct entry
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;
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];
187 static int idx, limidx, output_one;
188 static const char *destdir;
189 static const char *srcdir;
190 FILE *outfile;
192 void
193 switchfiles (int fields)
195 static int filecnt;
196 static char *destbuf, *destptr;
197 ++filecnt;
198 if (outfile)
199 fclose (outfile);
200 if (output_one)
202 outfile = stdout;
203 return;
205 if (destbuf == NULL)
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;
216 sprintf (destptr, "t%03d_main.m", filecnt);
217 outfile = fopen (destbuf, "w");
218 if (outfile == NULL)
220 fail:
221 fputs ("failed to create test files\n", stderr);
222 exit (1);
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)
238 fprintf (outfile, "\
239 /* { dg-do run { xfail { { \"powerpc*-*-darwin*\" && { ! lp64 } } || { \"powerpc*-*-aix*\" } } } } */\n\
240 /* { dg-options \"-w -I%s -fgnu-runtime\" } */\n", srcdir);
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)
248 fprintf (outfile, "\
249 /* { dg-do run { xfail *-*-* } } */\n\
250 /* { dg-options \"-w -I%s -fgnu-runtime\" } */\n", srcdir);
252 else
254 fprintf (outfile, "\
255 /* { dg-do run } */\n\
256 /* { dg-options \"-w -I%s -fgnu-runtime\" } */\n", srcdir);
258 fprintf(outfile, "#include <objc/encoding.h> \n\
259 #include \"struct-layout-1.h\"\n\
261 int fails; \n\
262 #define TX(n, type, attrs, fields, ops) \\\n\
263 type S##n { fields } attrs; \\\n\
264 void 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\
283 int 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;
310 unsigned long long int
311 getrandll (void)
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;
321 subfield (struct entry *e, char *letter)
323 int i, type;
324 char buf[20];
325 const char *p;
326 switch (e[0].etype)
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)
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);
344 else
346 buf[0] = *letter;
347 buf[1] = '\0';
349 ++*letter;
350 switch (type)
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;
365 for (i = 1; i <= e[0].len; )
366 i += subfield (e + i, letter);
368 switch (type)
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;
382 return 1 + e[0].len;
383 case ETYPE_TYPE:
384 case ETYPE_ARRAY:
385 if (e[0].etype == ETYPE_ARRAY)
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);
392 else
394 buf[0] = *letter;
395 buf[1] = '\0';
397 ++*letter;
398 if (e[0].attrib)
399 switch (generate_random () % 3)
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;
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)
417 if (e[0].attrib)
418 switch (generate_random () % 3)
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;
430 else
431 fprintf (outfile, "%s:0;", e[0].type->name);
432 ++*letter;
433 return 1;
435 switch (e[0].type->bitfld)
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 ();
451 if (e[0].attrib)
452 switch (generate_random () % 3)
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;
467 else
468 fprintf (outfile, "%s %c:%s;", e[0].type->name, *letter, buf);
469 ++*letter;
470 return 1;
471 default:
472 abort ();
476 char namebuf[1024];
478 void
479 output_FNB (char mode, struct entry *e)
481 unsigned long long int l1, l2, m;
482 int signs = 0;
483 const char *p, *q;
485 if (e->type->type == TYPE_OTHER)
487 if (mode == 'B')
488 abort ();
489 fprintf (outfile, "N(%d,%s)", idx, namebuf);
490 return;
492 fprintf (outfile, "%c(%d,%s,", mode, idx, namebuf);
493 l1 = getrandll ();
494 l2 = getrandll ();
495 switch (e->type->type)
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
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);
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 ();
615 fputs (")", outfile);
619 subvalues (struct entry *e, char *p, char *letter)
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)
630 case ETYPE_STRUCT_ARRAY:
631 case ETYPE_UNION_ARRAY:
632 if (e[0].arr_len == 0 || e[0].arr_len == 255)
634 *letter += 1 + e[0].len;
635 return 1 + e[0].len;
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; )
648 i += subvalues (e + i, q, letter);
649 if (e[0].etype == ETYPE_UNION || e[0].etype == ETYPE_UNION_ARRAY)
651 *letter += e[0].len - i + 1;
652 break;
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)
663 ++*letter;
664 return 1;
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)
672 j = generate_random () % e[0].arr_len;
673 if (i != j)
675 snprintf (p, sizeof (namebuf) - (p - namebuf),
676 "%c[%d]", *letter, j);
677 output_FNB ('F', e);
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;
690 /* DERIVED FROM:
691 --------------------------------------------------------------------
692 lookup2.c, by Bob Jenkins, December 1996, Public Domain.
693 hash(), hash2(), hash3, and mix() are externally useful functions.
694 Routines to test the hash are included if SELF_TEST is defined.
695 You can use this free for any purpose. It has no warranty.
696 --------------------------------------------------------------------
700 --------------------------------------------------------------------
701 mix -- mix 3 32-bit values reversibly.
702 For 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.)
709 mix() 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);
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 --------------------------------------------------------------------
725 /* same, but slower, works on systems that might have 8 byte hashval_t's */
726 #define mix(a,b,c) \
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; \
740 --------------------------------------------------------------------
741 hash() -- 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
745 Returns a 32-bit value. Every bit of the key affects every bit of
746 the return value. Every 1-bit and 2-bit delta achieves avalanche.
747 About 36+6len instructions.
749 The best hash table sizes are powers of 2. There is no need to do
750 mod a prime (mod is sooo slow!). If you need less than 32 bits,
751 use a bitmask. For example, if you need only 10 bits, do
752 h = (h & hashmask(10));
753 In which case, the hash table should have hashsize(10) elements.
755 If 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);
758 By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
759 code any way you wish, private, educational, or commercial. It's free.
761 See http://burtleburtle.net/bob/hash/evahash.html
762 Use for hash table lookup, or anything where one collision in 2^32 is
763 acceptable. Do NOT use for cryptographic purposes.
764 --------------------------------------------------------------------
767 static hashval_t
768 iterative_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 */)
773 register const unsigned char *k = (const unsigned char *)k_in;
774 register hashval_t a,b,c,len;
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 */
781 /*---------------------------------------- handle most of the key */
782 while (len >= 12)
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;
791 /*------------------------------------- handle the last 11 bytes */
792 c += length;
793 switch(len) /* all the case statements fall through */
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 */
809 mix(a,b,c);
810 /*-------------------------------------------- report the result */
811 return c;
814 hashval_t
815 e_hash (const void *a)
817 const struct entry *e = a;
818 hashval_t ret = 0;
819 int i;
821 if (e[0].etype != ETYPE_STRUCT && e[0].etype != ETYPE_UNION)
822 abort ();
823 for (i = 0; i <= e[0].len; ++i)
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);
832 return ret;
836 e_eq (const void *a, const void *b)
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)
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;
856 return 1;
859 static int
860 e_exists (const struct entry *e)
862 struct entry *h;
863 hashval_t hval;
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;
872 static void
873 e_insert (struct entry *e)
875 hashval_t hval;
877 hval = e_hash (e);
878 e->next = hash_table[hval % HASH_SIZE];
879 hash_table[hval % HASH_SIZE] = e;
882 void
883 output (struct entry *e)
885 int i;
886 char c;
887 struct entry *n;
888 const char *skip_cint = "";
890 if (e[0].etype != ETYPE_STRUCT && e[0].etype != ETYPE_UNION)
891 abort ();
893 if (e_exists (e))
894 return;
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);
900 if (idx == limidx)
901 switchfiles (e[0].len);
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; )
925 i += subvalues (e + i, namebuf, &c);
926 if (e[0].etype == ETYPE_UNION)
927 break;
929 fputs (")\n", outfile);
930 if (output_one && idx == limidx)
931 exit (0);
932 ++idx;
935 enum FEATURE
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
945 void
946 singles (enum FEATURE features)
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)
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);
966 if (features & FEATURE_COMPLEX)
967 for (i = 0; i < NCTYPES2; ++i)
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);
976 if (features & FEATURE_VECTOR)
977 for (i = 0; i < NVTYPES2; ++i)
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);
988 void
989 choose_type (enum FEATURE features, struct entry *e, int r, int in_array)
991 int i;
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))
1005 if (r < NCTYPES2)
1006 e->type = &complex_types[r];
1007 r -= NCTYPES2;
1009 if (e->type == NULL && (features & FEATURE_VECTOR))
1011 if (r < NVTYPES2)
1012 e->type = &vector_types[r];
1013 r -= NVTYPES2;
1015 if (e->type == NULL)
1016 abort ();
1019 /* This is from gcc.c-torture/execute/builtin-bitops-1.c. */
1020 static int
1021 my_ffsll (unsigned long long x)
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;
1033 void
1034 generate_fields (enum FEATURE features, struct entry *e, struct entry *parent,
1035 int len)
1037 int r, i, j, ret = 1, n, incr, sametype;
1039 for (n = 0; n < len; n += incr)
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)
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)
1075 e[n].arr_len = 0;
1076 if (n == len - 1 && (r & 4)
1077 && (parent->etype == ETYPE_STRUCT
1078 || parent->etype == ETYPE_STRUCT_ARRAY))
1080 int k;
1081 for (k = 0; k < n; ++k)
1082 if (e[k].etype != ETYPE_BITFLD || e[k].len)
1084 e[n].arr_len = 255;
1085 break;
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)
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;
1114 for (j = n; j < n + incr; ++j)
1116 int mi, ma;
1118 e[j].etype = ETYPE_BITFLD;
1119 if (j == n || !sametype)
1121 r = generate_random ();
1122 r >>= 2;
1123 e[j].type
1124 = &bitfld_types[r % n_bitfld_types];
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)
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 ();
1156 e[j].len = ma + 1;
1157 if (sametype && (r & 3) == 0 && ma > 1)
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;
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;
1175 break;
1176 #endif
1177 case 7:
1178 switch (r & 7)
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;
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)
1204 r = generate_random ();
1205 if ((features & FEATURE_ZEROARRAY) && (r & 3) == 0)
1207 e[n].arr_len = 0;
1208 if (n + incr == len && (r & 4)
1209 && (parent->etype == ETYPE_STRUCT
1210 || parent->etype == ETYPE_STRUCT_ARRAY))
1212 int k;
1213 for (k = 0; k < n; ++k)
1214 if (e[k].etype != ETYPE_BITFLD || e[k].len)
1216 e[n].arr_len = 255;
1217 break;
1221 else if ((r & 3) != 3)
1222 e[n].arr_len = (r >> 2) & 7;
1223 else
1224 e[n].arr_len = (r >> 2) & 31;
1226 break;
1231 void
1232 generate_random_tests (enum FEATURE features, int len)
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);
1250 struct { const char *name; enum FEATURE f; }
1251 features[] = {
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 }
1261 main (int argc, char **argv)
1263 int i, j, count, c, n = 3000;
1264 char *optarg;
1266 if (sizeof (int) != 4 || sizeof (long long) != 8)
1267 return 1;
1269 i = 1;
1270 while (i < argc)
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)
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;
1297 i += 2;
1300 if (output_one)
1302 outfile = fopen ("/dev/null", "w");
1303 if (outfile == NULL)
1305 fputs ("could not open /dev/null", stderr);
1306 return 1;
1308 n = limidx + 1;
1311 if (destdir == NULL && !output_one)
1313 usage:
1314 fprintf (stderr, "Usage:\n\
1315 %s [-s srcdir -d destdir] [-n count] [-i idx]\n\
1316 Either -s srcdir -d destdir or -i idx must be used\n", argv[0]);
1317 return 1;
1320 if (srcdir == NULL && !output_one)
1321 goto usage;
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)
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));
1341 for (i = 0; n > 3000 && i < sizeof (features) / sizeof (features[0]); ++i)
1343 int startidx;
1344 startidx = idx;
1345 if (! output_one)
1346 limidx = idx;
1347 singles (features[i].f);
1348 if (!i)
1350 count = 1000;
1351 while (idx < startidx + 1000)
1352 generate_random_tests (features[i].f, 1);
1354 else
1356 startidx = idx;
1357 count = 100;
1358 while (idx < startidx + 100)
1359 generate_random_tests (features[i].f, 1);
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));
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;