1 /* Structure layout test generator.
2 Copyright (C) 2004, 2005 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 2, or (at your option) any later
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
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22 /* Compile with gcc -o struct-layout-1_generate{,.c} generate_random{,_r}.c */
24 /* N.B. -- This program cannot use libiberty as that will not work
25 when testing an installed compiler. */
31 /* We use our own pseudo-random number generator, so that it gives the same
32 values on all hosts. */
33 #include "generate-random.h"
35 #if LLONG_MAX != 9223372036854775807LL && __LONG_LONG_MAX__ != 9223372036854775807LL
36 # error Need 64-bit long long
39 typedef unsigned int hashval_t
;
60 unsigned long long int maxval
;
64 struct types base_types
[] = {
65 /* As we don't know whether char will be signed or not, just limit ourselves
66 to unsigned values less than maximum signed char value. */
67 { "char", TYPE_UINT
, 127, 'C' },
68 { "signed char", TYPE_INT
, 127, 'C' },
69 { "unsigned char", TYPE_UINT
, 255, 'C' },
70 { "short int", TYPE_INT
, 32767, 'S' },
71 { "unsigned short int", TYPE_UINT
, 65535, 'S' },
72 { "int", TYPE_INT
, 2147483647, 'I' },
73 { "unsigned int", TYPE_UINT
, 4294967295U, 'I' },
74 { "long int", TYPE_INT
, 9223372036854775807LL, 'L' },
75 { "unsigned long int", TYPE_UINT
, 18446744073709551615ULL, 'L' },
76 { "long long int", TYPE_INT
, 9223372036854775807LL, 'Q' },
77 { "unsigned long long int", TYPE_UINT
, 18446744073709551615ULL, 'Q' },
78 { "bool", TYPE_UINT
, 1, 'B' },
79 { "void *", TYPE_PTR
, 0, 0 },
80 { "char *", TYPE_PTR
, 0, 0 },
81 { "int *", TYPE_PTR
, 0, 0 },
82 { "float", TYPE_FLOAT
, 0, 0 },
83 { "double", TYPE_FLOAT
, 0, 0 },
84 /*{ "long double", TYPE_FLOAT, 0, 0 },*/
85 /* Disabled as double and long double
86 are encoded thee same, currently */
89 /* enums are disabled for now as it seems like their encoding is broken, we should
90 just encode them using their underlaying type but we don't. */
91 { "enum E0", TYPE_UENUM
, 0, ' ' },
92 { "enum E1", TYPE_UENUM
, 1, ' ' },
93 { "enum E2", TYPE_SENUM
, 3, ' ' },
94 { "enum E3", TYPE_SENUM
, 127, ' ' },
95 { "enum E4", TYPE_UENUM
, 255, ' ' },
96 { "enum E5", TYPE_SENUM
, 32767, ' ' },
97 { "enum E6", TYPE_UENUM
, 65535, ' ' },
98 { "enum E7", TYPE_SENUM
, 2147483647, ' ' },
99 { "enum E8", TYPE_UENUM
, 4294967295U, ' ' },
100 { "enum E9", TYPE_SENUM
, 1099511627775LL, ' ' },
102 #define NTYPES2 (sizeof (base_types) / sizeof (base_types[0]))
104 struct types complex_types
[] = {
105 { "_Complex char", TYPE_CUINT
, 127, 0 },
106 { "_Complex signed char", TYPE_CINT
, 127, 0 },
107 { "_Complex unsigned char", TYPE_CUINT
, 255, 0 },
108 { "_Complex short int", TYPE_CINT
, 32767, 0 },
109 { "_Complex unsigned short int", TYPE_CUINT
, 65535, 0 },
110 { "_Complex int", TYPE_CINT
, 2147483647, 0 },
111 { "_Complex unsigned int", TYPE_CUINT
, 4294967295U, 0 },
112 { "_Complex long int", TYPE_CINT
, 9223372036854775807LL, 0 },
113 { "_Complex unsigned long int", TYPE_CUINT
, 18446744073709551615ULL, 0 },
114 { "_Complex long long int", TYPE_CINT
, 9223372036854775807LL, 0 },
115 { "_Complex unsigned long long int", TYPE_CUINT
, 18446744073709551615ULL, 0 },
116 { "_Complex float", TYPE_CFLOAT
, 0, 0 },
117 { "_Complex double", TYPE_CFLOAT
, 0, 0 },
118 /*{ "_Complex long double", TYPE_CFLOAT, 0, 0 }, */
119 /* Disable until long doubles are encoded correctly. */
120 #define NCTYPES2 (sizeof (complex_types) / sizeof (complex_types[0]))
122 struct types vector_types
[] = {
123 /* vector-defs.h typedefs */
124 { "v8qi", TYPE_OTHER
, 0, 0 },
125 { "v16qi", TYPE_OTHER
, 0, 0 },
126 { "v2hi", TYPE_OTHER
, 0, 0 },
127 { "v4hi", TYPE_OTHER
, 0, 0 },
128 { "v8hi", TYPE_OTHER
, 0, 0 },
129 { "v2si", TYPE_OTHER
, 0, 0 },
130 { "v4si", TYPE_OTHER
, 0, 0 },
131 { "v1di", TYPE_OTHER
, 0, 0 },
132 { "v2di", TYPE_OTHER
, 0, 0 },
133 { "v2sf", TYPE_OTHER
, 0, 0 },
134 { "v4sf", TYPE_OTHER
, 0, 0 },
135 { "v16sf", TYPE_OTHER
, 0, 0 },
136 { "v2df", TYPE_OTHER
, 0, 0 },
137 { "u8qi", TYPE_OTHER
, 0, 0 },
138 { "u16qi", TYPE_OTHER
, 0, 0 },
139 { "u2hi", TYPE_OTHER
, 0, 0 },
140 { "u4hi", TYPE_OTHER
, 0, 0 },
141 { "u8hi", TYPE_OTHER
, 0, 0 },
142 { "u2si", TYPE_OTHER
, 0, 0 },
143 { "u4si", TYPE_OTHER
, 0, 0 },
144 { "u1di", TYPE_OTHER
, 0, 0 },
145 { "u2di", TYPE_OTHER
, 0, 0 },
146 { "u2sf", TYPE_OTHER
, 0, 0 },
147 { "u4sf", TYPE_OTHER
, 0, 0 },
148 { "u16sf", TYPE_OTHER
, 0, 0 },
149 { "u2df", TYPE_OTHER
, 0, 0 },
150 { "__m64", TYPE_OTHER
, 0, 0 },
151 { "__m128", TYPE_OTHER
, 0, 0 }
152 #define NVTYPES2 (sizeof (vector_types) / sizeof (vector_types[0]))
155 struct types bitfld_types
[NTYPES2
];
172 enum ETYPE etype
: 8;
177 unsigned char arr_len
;
180 /* Used to chain together entries in the hash table. */
184 /* A prime number giving the number of slots in the hash table. */
185 #define HASH_SIZE 32749
186 static struct entry
*hash_table
[HASH_SIZE
];
188 static int idx
, limidx
, output_one
;
189 static const char *destdir
;
190 static const char *srcdir
;
194 switchfiles (int fields
)
197 static char *destbuf
, *destptr
;
208 size_t len
= strlen (destdir
);
209 destbuf
= malloc (len
+ 20);
212 memcpy (destbuf
, destdir
, len
);
213 if (!len
|| destbuf
[len
- 1] != '/')
214 destbuf
[len
++] = '/';
215 destptr
= destbuf
+ len
;
217 sprintf (destptr
, "t%03d_main.m", filecnt
);
218 outfile
= fopen (destbuf
, "w");
222 fputs ("failed to create test files\n", stderr
);
226 /* { dg-do run } */\n\
227 /* { dg-options \"-w -I%s -fgnu-runtime\" } */\n\
228 #include <objc/encoding.h> \n\
229 #include \"struct-layout-1.h\"\n\
232 #define TX(n, type, attrs, fields, ops) \\\n\
233 type S##n { fields } attrs; \\\n\
234 void test##n (void) \\\n\
236 char *encoding = @encode (type S##n); \\\n\
237 if (objc_sizeof_type (encoding) != sizeof(type S##n)) \\\n\
240 printf(#type \" { \" #fields \"} size is %%u, but is calulated as %%u\\n\", \\\n\
241 sizeof(type S##n), objc_sizeof_type (encoding)); \\\n\
243 if (objc_alignof_type (encoding) != __alignof__ (type S##n)) \\\n\
246 printf(#type \" { \" #fields \"} align is %%u, but is calulated as %%u\\n\", \\\n\
247 __alignof__ (type S##n), objc_alignof_type (encoding)); \\\n\
250 #include \"t%03d_test.h\"\n\
255 #define TX(n, type, attrs, fields, ops) test##n ();\n\
256 #include \"t%03d_test.h\"\n\
264 }\n", srcdir
, filecnt
, filecnt
);
266 sprintf (destptr
, "t%03d_test.h", filecnt
);
267 outfile
= fopen (destbuf
, "w");
272 else if (fields
<= 4)
274 else if (fields
<= 6)
280 unsigned long long int
283 unsigned long long int ret
;
284 ret
= generate_random () & 0xffffff;
285 ret
|= (generate_random () & 0xffffffLL
) << 24;
286 ret
|= ((unsigned long long int) generate_random ()) << 48;
291 subfield (struct entry
*e
, char *letter
)
300 case ETYPE_STRUCT_ARRAY
:
301 case ETYPE_UNION_ARRAY
:
302 type
= e
[0].attrib
? 1 + (generate_random () & 3) : 0;
303 if (e
[0].etype
== ETYPE_STRUCT
|| e
[0].etype
== ETYPE_STRUCT_ARRAY
)
307 if (e
[0].etype
== ETYPE_STRUCT_ARRAY
|| e
[0].etype
== ETYPE_UNION_ARRAY
)
309 if (e
[0].arr_len
== 255)
310 snprintf (buf
, 20, "%c[]", *letter
);
312 snprintf (buf
, 20, "%c[%d]", *letter
, e
[0].arr_len
);
325 fprintf (outfile
, "%s{", p
);
328 fprintf (outfile
, "%s %s{", e
[0].attrib
, p
);
331 fprintf (outfile
, "%s %s{", p
, e
[0].attrib
);
335 for (i
= 1; i
<= e
[0].len
; )
336 i
+= subfield (e
+ i
, letter
);
343 fprintf (outfile
, "}%s;", buf
);
346 fprintf (outfile
, "}%s %s;", e
[0].attrib
, buf
);
349 fprintf (outfile
, "}%s %s;", buf
, e
[0].attrib
);
355 if (e
[0].etype
== ETYPE_ARRAY
)
357 if (e
[0].arr_len
== 255)
358 snprintf (buf
, 20, "%c[]", *letter
);
360 snprintf (buf
, 20, "%c[%d]", *letter
, e
[0].arr_len
);
369 switch (generate_random () % 3)
372 fprintf (outfile
, "%s %s %s;", e
[0].attrib
, e
[0].type
->name
, buf
);
375 fprintf (outfile
, "%s %s %s;", e
[0].type
->name
, e
[0].attrib
, buf
);
378 fprintf (outfile
, "%s %s %s;", e
[0].type
->name
, buf
, e
[0].attrib
);
382 fprintf (outfile
, "%s %s;", e
[0].type
->name
, buf
);
388 switch (generate_random () % 3)
391 fprintf (outfile
, "%s %s:0;", e
[0].attrib
, e
[0].type
->name
);
394 fprintf (outfile
, "%s %s:0;", e
[0].type
->name
, e
[0].attrib
);
397 fprintf (outfile
, "%s:0 %s;", e
[0].type
->name
, e
[0].attrib
);
401 fprintf (outfile
, "%s:0;", e
[0].type
->name
);
405 switch (e
[0].type
->bitfld
)
412 snprintf (buf
, 20, "B%cN(%d)", e
[0].type
->bitfld
, e
[0].len
);
416 snprintf (buf
, 20, "%d", e
[0].len
);
422 switch (generate_random () % 3)
425 fprintf (outfile
, "%s %s %c:%s;", e
[0].attrib
, e
[0].type
->name
,
429 fprintf (outfile
, "%s %s %c:%s;", e
[0].type
->name
, e
[0].attrib
,
433 fprintf (outfile
, "%s %c:%s %s;", e
[0].type
->name
, *letter
,
438 fprintf (outfile
, "%s %c:%s;", e
[0].type
->name
, *letter
, buf
);
449 output_FNB (char mode
, struct entry
*e
)
451 unsigned long long int l1
, l2
, m
;
455 if (e
->type
->type
== TYPE_OTHER
)
459 fprintf (outfile
, "N(%d,%s)", idx
, namebuf
);
462 fprintf (outfile
, "%c(%d,%s,", mode
, idx
, namebuf
);
465 switch (e
->type
->type
)
468 signs
= generate_random () & 3;
471 m
&= e
->len
> 1 ? (1ULL << (e
->len
- 1)) - 1 : 1;
474 fprintf (outfile
, "%s%llu%s,%s%llu%s",
475 (signs
& 1) ? "-" : "", l1
, l1
> 2147483647 ? "LL" : "",
476 (signs
& 2) ? "-" : "", l2
, l2
> 2147483647 ? "LL" : "");
481 m
&= (1ULL << e
->len
) - 1;
484 fprintf (outfile
, "%lluU%s,%lluU%s", l1
, l1
> 4294967295U ? "LL" : "",
485 l2
, l2
> 4294967295U ? "LL" : "");
490 signs
= generate_random () & 3;
491 fprintf (outfile
, "%s%f,%s%f", (signs
& 1) ? "-" : "",
492 ((double) l1
) / 64, (signs
& 2) ? "-" : "", ((double) l2
) / 64);
495 signs
= generate_random () & 3;
496 l1
&= e
->type
->maxval
;
497 l2
&= e
->type
->maxval
;
498 fprintf (outfile
, "CINT(%s%llu%s,%s%llu%s),",
499 (signs
& 1) ? "-" : "", l1
, l1
> 2147483647 ? "LL" : "",
500 (signs
& 2) ? "-" : "", l2
, l2
> 2147483647 ? "LL" : "");
501 signs
= generate_random () & 3;
504 l1
&= e
->type
->maxval
;
505 l2
&= e
->type
->maxval
;
506 fprintf (outfile
, "CINT(%s%llu%s,%s%llu%s)",
507 (signs
& 1) ? "-" : "", l1
, l1
> 2147483647 ? "LL" : "",
508 (signs
& 2) ? "-" : "", l2
, l2
> 2147483647 ? "LL" : "");
511 l1
&= e
->type
->maxval
;
512 l2
&= e
->type
->maxval
;
513 fprintf (outfile
, "CINT(%lluU%s,%lluU%s),",
514 l1
, l1
> 4294967295U ? "LL" : "",
515 l2
, l2
> 4294967295U ? "LL" : "");
518 l1
&= e
->type
->maxval
;
519 l2
&= e
->type
->maxval
;
520 fprintf (outfile
, "CINT(%lluU%s,%lluU%s)",
521 l1
, l1
> 4294967295U ? "LL" : "",
522 l2
, l2
> 4294967295U ? "LL" : "");
527 signs
= generate_random () & 3;
528 fprintf (outfile
, "CDBL(%s%f,%s%f),",
529 (signs
& 1) ? "-" : "", ((double) l1
) / 64,
530 (signs
& 2) ? "-" : "", ((double) l2
) / 64);
535 signs
= generate_random () & 3;
536 fprintf (outfile
, "CDBL(%s%f,%s%f)",
537 (signs
& 1) ? "-" : "", ((double) l1
) / 64,
538 (signs
& 2) ? "-" : "", ((double) l2
) / 64);
541 if (e
->type
->maxval
== 0)
542 fputs ("e0_0,e0_0", outfile
);
543 else if (e
->type
->maxval
== 1)
544 fprintf (outfile
, "e1_%lld,e1_%lld", l1
& 1, l2
& 1);
547 p
= strchr (e
->type
->name
, '\0');
548 while (--p
>= e
->type
->name
&& *p
>= '0' && *p
<= '9');
553 l1
+= e
->type
->maxval
- 6;
555 l2
+= e
->type
->maxval
- 6;
556 fprintf (outfile
, "e%s_%lld,e%s_%lld", p
, l1
, p
, l2
);
560 p
= strchr (e
->type
->name
, '\0');
561 while (--p
>= e
->type
->name
&& *p
>= '0' && *p
<= '9');
565 fprintf (outfile
, "e%s_%s%lld,e%s_%s%lld",
566 p
, l1
< 3 ? "m" : "",
567 l1
== 3 ? 0LL : e
->type
->maxval
- (l1
& 3),
568 p
, l2
< 3 ? "m" : "",
569 l2
== 3 ? 0LL : e
->type
->maxval
- (l2
& 3));
574 fprintf (outfile
, "(%s)&intarray[%lld],(%s)&intarray[%lld]",
575 e
->type
->name
, l1
, e
->type
->name
, l2
);
580 fprintf (outfile
, "fn%lld,fn%lld", l1
, l2
);
585 fputs (")", outfile
);
589 subvalues (struct entry
*e
, char *p
, char *letter
)
593 if (p
>= namebuf
+ sizeof (namebuf
) - 32)
600 case ETYPE_STRUCT_ARRAY
:
601 case ETYPE_UNION_ARRAY
:
602 if (e
[0].arr_len
== 0 || e
[0].arr_len
== 255)
604 *letter
+= 1 + e
[0].len
;
607 i
= generate_random () % e
[0].arr_len
;
608 snprintf (p
, sizeof (namebuf
) - (p
- namebuf
) - 1,
609 "%c[%d]", *letter
, i
);
610 q
= strchr (p
, '\0');
616 for (i
= 1; i
<= e
[0].len
; )
618 i
+= subvalues (e
+ i
, q
, letter
);
619 if (e
[0].etype
== ETYPE_UNION
|| e
[0].etype
== ETYPE_UNION_ARRAY
)
621 *letter
+= e
[0].len
- i
+ 1;
631 if (e
[0].arr_len
== 0 || e
[0].arr_len
== 255)
636 i
= generate_random () % e
[0].arr_len
;
637 snprintf (p
, sizeof (namebuf
) - (p
- namebuf
),
638 "%c[%d]", *letter
, i
);
640 if ((generate_random () & 7) == 0)
642 j
= generate_random () % e
[0].arr_len
;
645 snprintf (p
, sizeof (namebuf
) - (p
- namebuf
),
646 "%c[%d]", *letter
, j
);
661 --------------------------------------------------------------------
662 lookup2.c, by Bob Jenkins, December 1996, Public Domain.
663 hash(), hash2(), hash3, and mix() are externally useful functions.
664 Routines to test the hash are included if SELF_TEST is defined.
665 You can use this free for any purpose. It has no warranty.
666 --------------------------------------------------------------------
670 --------------------------------------------------------------------
671 mix -- mix 3 32-bit values reversibly.
672 For every delta with one or two bit set, and the deltas of all three
673 high bits or all three low bits, whether the original value of a,b,c
674 is almost all zero or is uniformly distributed,
675 * If mix() is run forward or backward, at least 32 bits in a,b,c
676 have at least 1/4 probability of changing.
677 * If mix() is run forward, every bit of c will change between 1/3 and
678 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
679 mix() was built out of 36 single-cycle latency instructions in a
680 structure that could supported 2x parallelism, like so:
688 Unfortunately, superscalar Pentiums and Sparcs can't take advantage
689 of that parallelism. They've also turned some of those single-cycle
690 latency instructions into multi-cycle latency instructions. Still,
691 this is the fastest good hash I could find. There were about 2^^68
692 to choose from. I only looked at a billion or so.
693 --------------------------------------------------------------------
695 /* same, but slower, works on systems that might have 8 byte hashval_t's */
698 a -= b; a -= c; a ^= (c>>13); \
699 b -= c; b -= a; b ^= (a<< 8); \
700 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
701 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
702 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
703 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
704 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
705 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
706 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
710 --------------------------------------------------------------------
711 hash() -- hash a variable-length key into a 32-bit value
712 k : the key (the unaligned variable-length array of bytes)
713 len : the length of the key, counting by bytes
714 level : can be any 4-byte value
715 Returns a 32-bit value. Every bit of the key affects every bit of
716 the return value. Every 1-bit and 2-bit delta achieves avalanche.
717 About 36+6len instructions.
719 The best hash table sizes are powers of 2. There is no need to do
720 mod a prime (mod is sooo slow!). If you need less than 32 bits,
721 use a bitmask. For example, if you need only 10 bits, do
722 h = (h & hashmask(10));
723 In which case, the hash table should have hashsize(10) elements.
725 If you are hashing n strings (ub1 **)k, do it like this:
726 for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
728 By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
729 code any way you wish, private, educational, or commercial. It's free.
731 See http://burtleburtle.net/bob/hash/evahash.html
732 Use for hash table lookup, or anything where one collision in 2^32 is
733 acceptable. Do NOT use for cryptographic purposes.
734 --------------------------------------------------------------------
738 iterative_hash (const void *k_in
/* the key */,
739 register size_t length
/* the length of the key */,
740 register hashval_t initval
/* the previous hash, or
741 an arbitrary value */)
743 register const unsigned char *k
= (const unsigned char *)k_in
;
744 register hashval_t a
,b
,c
,len
;
746 /* Set up the internal state */
748 a
= b
= 0x9e3779b9; /* the golden ratio; an arbitrary value */
749 c
= initval
; /* the previous hash value */
751 /*---------------------------------------- handle most of the key */
754 a
+= (k
[0] +((hashval_t
)k
[1]<<8) +((hashval_t
)k
[2]<<16) +((hashval_t
)k
[3]<<24));
755 b
+= (k
[4] +((hashval_t
)k
[5]<<8) +((hashval_t
)k
[6]<<16) +((hashval_t
)k
[7]<<24));
756 c
+= (k
[8] +((hashval_t
)k
[9]<<8) +((hashval_t
)k
[10]<<16)+((hashval_t
)k
[11]<<24));
761 /*------------------------------------- handle the last 11 bytes */
763 switch(len
) /* all the case statements fall through */
765 case 11: c
+=((hashval_t
)k
[10]<<24);
766 case 10: c
+=((hashval_t
)k
[9]<<16);
767 case 9 : c
+=((hashval_t
)k
[8]<<8);
768 /* the first byte of c is reserved for the length */
769 case 8 : b
+=((hashval_t
)k
[7]<<24);
770 case 7 : b
+=((hashval_t
)k
[6]<<16);
771 case 6 : b
+=((hashval_t
)k
[5]<<8);
773 case 4 : a
+=((hashval_t
)k
[3]<<24);
774 case 3 : a
+=((hashval_t
)k
[2]<<16);
775 case 2 : a
+=((hashval_t
)k
[1]<<8);
777 /* case 0: nothing left to add */
780 /*-------------------------------------------- report the result */
785 e_hash (const void *a
)
787 const struct entry
*e
= a
;
791 if (e
[0].etype
!= ETYPE_STRUCT
&& e
[0].etype
!= ETYPE_UNION
)
793 for (i
= 0; i
<= e
[0].len
; ++i
)
796 ret
= iterative_hash (&e
[i
], offsetof (struct entry
, attrib
), ret
);
797 attriblen
= e
[i
].attrib
? strlen (e
[i
].attrib
) : -1;
798 ret
= iterative_hash (&attriblen
, sizeof (int), ret
);
800 ret
= iterative_hash (e
[i
].attrib
, attriblen
, ret
);
806 e_eq (const void *a
, const void *b
)
808 const struct entry
*ea
= a
, *eb
= b
;
810 if (ea
[0].etype
!= ETYPE_STRUCT
&& ea
[0].etype
!= ETYPE_UNION
)
812 if (ea
[0].len
!= eb
[0].len
)
814 for (i
= 0; i
<= ea
[0].len
; ++i
)
816 if (ea
[i
].etype
!= eb
[i
].etype
817 || ea
[i
].len
!= eb
[i
].len
818 || ea
[i
].arr_len
!= eb
[i
].arr_len
819 || ea
[i
].type
!= eb
[i
].type
)
821 if ((ea
[i
].attrib
== NULL
) ^ (eb
[i
].attrib
== NULL
))
823 if (ea
[i
].attrib
&& strcmp (ea
[i
].attrib
, eb
[i
].attrib
) != 0)
830 e_exists (const struct entry
*e
)
836 for (h
= hash_table
[hval
% HASH_SIZE
]; h
; h
= h
->next
)
843 e_insert (struct entry
*e
)
848 e
->next
= hash_table
[hval
% HASH_SIZE
];
849 hash_table
[hval
% HASH_SIZE
] = e
;
853 output (struct entry
*e
)
858 const char *skip_cint
= "";
860 if (e
[0].etype
!= ETYPE_STRUCT
&& e
[0].etype
!= ETYPE_UNION
)
866 n
= (struct entry
*) malloc ((e
[0].len
+ 1) * sizeof (struct entry
));
867 memcpy (n
, e
, (e
[0].len
+ 1) * sizeof (struct entry
));
871 switchfiles (e
[0].len
);
873 for (i
= 1; i
<= e
[0].len
; ++i
)
874 if ((e
[i
].etype
== ETYPE_TYPE
|| e
[i
].etype
== ETYPE_ARRAY
)
875 && (e
[i
].type
->type
== TYPE_CINT
|| e
[i
].type
->type
== TYPE_CUINT
))
880 fprintf (outfile
, (generate_random () & 1)
881 ? "TX%s(%d,%s %s,," : "TX%s(%d,%s,%s,", skip_cint
,
882 idx
, e
[0].etype
== ETYPE_STRUCT
? "struct" : "union",
884 else if (e
[0].etype
== ETYPE_STRUCT
)
885 fprintf (outfile
, "T%s(%d,", skip_cint
, idx
);
887 fprintf (outfile
, "U%s(%d,", skip_cint
, idx
);
889 for (i
= 1; i
<= e
[0].len
; )
890 i
+= subfield (e
+ i
, &c
);
891 fputs (",", outfile
);
893 for (i
= 1; i
<= e
[0].len
; )
895 i
+= subvalues (e
+ i
, namebuf
, &c
);
896 if (e
[0].etype
== ETYPE_UNION
)
899 fputs (")\n", outfile
);
900 if (output_one
&& idx
== limidx
)
909 FEATURE_ZEROARRAY
= 8,
910 FEATURE_ZEROBITFLD
= 16,
911 ALL_FEATURES
= FEATURE_COMPLEX
| FEATURE_VECTOR
| FEATURE_ZEROARRAY
916 singles (enum FEATURE features
)
920 memset (e
, 0, sizeof (e
));
921 e
[0].etype
= ETYPE_STRUCT
;
923 e
[0].etype
= ETYPE_UNION
;
927 for (i
= 0; i
< NTYPES2
; ++i
)
929 e
[0].etype
= ETYPE_STRUCT
;
930 e
[1].etype
= ETYPE_TYPE
;
931 e
[1].type
= &base_types
[i
];
933 e
[0].etype
= ETYPE_UNION
;
936 if (features
& FEATURE_COMPLEX
)
937 for (i
= 0; i
< NCTYPES2
; ++i
)
939 e
[0].etype
= ETYPE_STRUCT
;
940 e
[1].etype
= ETYPE_TYPE
;
941 e
[1].type
= &complex_types
[i
];
943 e
[0].etype
= ETYPE_UNION
;
946 if (features
& FEATURE_VECTOR
)
947 for (i
= 0; i
< NVTYPES2
; ++i
)
949 e
[0].etype
= ETYPE_STRUCT
;
950 e
[1].etype
= ETYPE_TYPE
;
951 e
[1].type
= &vector_types
[i
];
953 e
[0].etype
= ETYPE_UNION
;
959 choose_type (enum FEATURE features
, struct entry
*e
, int r
, int in_array
)
963 i
= NTYPES2
- NTYPES1
;
964 if (features
& FEATURE_COMPLEX
)
966 if (features
& FEATURE_VECTOR
)
970 if (r
< NTYPES2
- NTYPES1
)
971 e
->type
= &base_types
[r
+ NTYPES1
];
972 r
-= NTYPES2
- NTYPES1
;
973 if (e
->type
== NULL
&& (features
& FEATURE_COMPLEX
))
976 e
->type
= &complex_types
[r
];
979 if (e
->type
== NULL
&& (features
& FEATURE_VECTOR
))
982 e
->type
= &vector_types
[r
];
989 /* This is from gcc.c-torture/execute/builtin-bitops-1.c. */
991 my_ffsll (unsigned long long x
)
996 /* We've tested LLONG_MAX for 64 bits so this should be safe. */
997 for (i
= 0; i
< 64; i
++)
1004 generate_fields (enum FEATURE features
, struct entry
*e
, struct entry
*parent
,
1007 int r
, i
, j
, ret
= 1, n
, incr
, sametype
;
1009 for (n
= 0; n
< len
; n
+= incr
)
1011 r
= generate_random ();
1012 /* 50% ETYPE_TYPE base_types NTYPES1
1013 12.5% ETYPE_TYPE other
1016 12.5% ETYPE_STRUCT|ETYPE_UNION|ETYPE_STRUCT_ARRAY|ETYPE_UNION_ARRAY */
1022 case 6: /* BITfields disabled for now as _Bool bitfields are broken. */
1027 e
[n
].etype
= ETYPE_TYPE
;
1028 e
[n
].type
= &base_types
[r
% NTYPES1
];
1031 e
[n
].etype
= ETYPE_TYPE
;
1032 choose_type (features
, &e
[n
], r
, 0);
1035 e
[n
].etype
= ETYPE_ARRAY
;
1039 e
[n
].type
= &base_types
[r
% NTYPES1
];
1041 choose_type (features
, &e
[n
], r
, 1);
1042 r
= generate_random ();
1043 if ((features
& FEATURE_ZEROARRAY
) && (r
& 3) == 0)
1046 if (n
== len
- 1 && (r
& 4)
1047 && (parent
->etype
== ETYPE_STRUCT
1048 || parent
->etype
== ETYPE_STRUCT_ARRAY
))
1051 for (k
= 0; k
< n
; ++k
)
1052 if (e
[k
].etype
!= ETYPE_BITFLD
|| e
[k
].len
)
1059 else if ((r
& 3) != 3)
1060 e
[n
].arr_len
= (r
>> 2) & 7;
1062 e
[n
].arr_len
= (r
>> 2) & 31;
1076 incr
= 1 + (r
>> 3) % (len
- n
);
1081 incr
= 1 + (r
>> 3) % (len
- n
);
1084 for (j
= n
; j
< n
+ incr
; ++j
)
1088 e
[j
].etype
= ETYPE_BITFLD
;
1089 if (j
== n
|| !sametype
)
1091 r
= generate_random ();
1094 = &bitfld_types
[r
% n_bitfld_types
];
1097 e
[j
].type
= e
[n
].type
;
1098 r
= generate_random ();
1101 switch (e
[j
].type
->bitfld
)
1103 case 'C': ma
= 8; break;
1104 case 'S': ma
= 16; break;
1105 case 'I': ma
= 32; break;
1107 case 'Q': ma
= 64; break;
1108 case 'B': ma
= 1; break;
1110 if (e
[j
].type
->type
== TYPE_UENUM
)
1111 mi
= my_ffsll (e
[j
].type
->maxval
+ 1) - 1;
1112 else if (e
[j
].type
->type
== TYPE_SENUM
)
1113 mi
= my_ffsll (e
[j
].type
->maxval
+ 1);
1127 if (sametype
&& (r
& 3) == 0 && ma
> 1)
1130 for (k
= n
; k
< j
; ++k
)
1133 e
[j
].len
= sum
? ma
- sum
: ma
;
1136 if (! (features
& FEATURE_ZEROBITFLD
) && mi
== 0)
1138 if (e
[j
].len
< mi
|| e
[j
].len
> ma
)
1139 e
[j
].len
= mi
+ (r
% (ma
+ 1 - mi
));
1141 if ((features
& FEATURE_ZEROBITFLD
) && (r
& 3) == 0
1153 e
[n
].etype
= ETYPE_STRUCT
;
1157 e
[n
].etype
= ETYPE_UNION
;
1161 e
[n
].etype
= ETYPE_STRUCT_ARRAY
;
1164 e
[n
].etype
= ETYPE_UNION_ARRAY
;
1168 e
[n
].len
= r
% (len
- n
);
1169 incr
= 1 + e
[n
].len
;
1170 generate_fields (features
, &e
[n
+ 1], &e
[n
], e
[n
].len
);
1171 if (e
[n
].etype
== ETYPE_STRUCT_ARRAY
1172 || e
[n
].etype
== ETYPE_UNION_ARRAY
)
1174 r
= generate_random ();
1175 if ((features
& FEATURE_ZEROARRAY
) && (r
& 3) == 0)
1178 if (n
+ incr
== len
&& (r
& 4)
1179 && (parent
->etype
== ETYPE_STRUCT
1180 || parent
->etype
== ETYPE_STRUCT_ARRAY
))
1183 for (k
= 0; k
< n
; ++k
)
1184 if (e
[k
].etype
!= ETYPE_BITFLD
|| e
[k
].len
)
1191 else if ((r
& 3) != 3)
1192 e
[n
].arr_len
= (r
>> 2) & 7;
1194 e
[n
].arr_len
= (r
>> 2) & 31;
1202 generate_random_tests (enum FEATURE features
, int len
)
1204 struct entry e
[len
+ 1];
1206 if (len
> 'z' - 'a' + 1)
1208 memset (e
, 0, sizeof (e
));
1209 r
= generate_random ();
1211 e
[0].etype
= ETYPE_UNION
;
1213 e
[0].etype
= ETYPE_STRUCT
;
1216 generate_fields (features
, &e
[1], &e
[0], len
);
1220 struct { const char *name
; enum FEATURE f
; }
1223 { "complex", FEATURE_COMPLEX
},
1224 { "vector", FEATURE_VECTOR
},
1225 { "[0] :0", FEATURE_ZEROARRAY
| FEATURE_ZEROBITFLD
},
1226 { "complex vector [0]",
1227 FEATURE_COMPLEX
| FEATURE_VECTOR
| FEATURE_ZEROARRAY
}
1231 main (int argc
, char **argv
)
1233 int i
, j
, count
, c
, n
= 3000;
1236 if (sizeof (int) != 4 || sizeof (long long) != 8)
1243 if (argv
[i
][0] == '-' && argv
[i
][2] == '\0')
1245 optarg
= argv
[i
+ 1];
1261 limidx
= atoi (optarg
);
1264 fprintf (stderr
, "unrecognized option %s\n", argv
[i
]);
1272 outfile
= fopen ("/dev/null", "w");
1273 if (outfile
== NULL
)
1275 fputs ("could not open /dev/null", stderr
);
1281 if (destdir
== NULL
&& !output_one
)
1284 fprintf (stderr
, "Usage:\n\
1285 %s [-s srcdir -d destdir] [-n count] [-i idx]\n\
1286 Either -s srcdir -d destdir or -i idx must be used\n", argv
[0]);
1290 if (srcdir
== NULL
&& !output_one
)
1293 for (i
= 0; i
< NTYPES2
; ++i
)
1294 if (base_types
[i
].bitfld
)
1295 bitfld_types
[n_bitfld_types
++] = base_types
[i
];
1296 for (i
= 0; i
< sizeof (features
) / sizeof (features
[0]); ++i
)
1305 for (j
= 1; j
<= 9; ++j
)
1306 while (idx
< startidx
+ j
* count
)
1307 generate_random_tests (features
[i
].f
, j
);
1308 while (idx
< startidx
+ count
* 10)
1309 generate_random_tests (features
[i
].f
, 10 + (generate_random () % 16));
1311 for (i
= 0; n
> 3000 && i
< sizeof (features
) / sizeof (features
[0]); ++i
)
1317 singles (features
[i
].f
);
1321 while (idx
< startidx
+ 1000)
1322 generate_random_tests (features
[i
].f
, 1);
1328 while (idx
< startidx
+ 100)
1329 generate_random_tests (features
[i
].f
, 1);
1332 for (j
= 2; j
<= 9; ++j
)
1333 while (idx
< startidx
+ (j
- 1) * count
)
1334 generate_random_tests (features
[i
].f
, j
);
1335 while (idx
< startidx
+ count
* 9)
1336 generate_random_tests (features
[i
].f
, 10 + (generate_random () % 16));
1341 generate_random_tests (ALL_FEATURES
, 1 + (generate_random () % 25));