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
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 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. */
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
38 typedef unsigned int hashval_t
;
59 unsigned long long int maxval
;
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 */
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, ' ' },
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
];
171 enum ETYPE etype
: 8;
176 unsigned char arr_len
;
179 /* Used to chain together entries in the hash table. */
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
;
193 switchfiles (int fields
)
196 static char *destbuf
, *destptr
;
207 size_t len
= strlen (destdir
);
208 destbuf
= malloc (len
+ 20);
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");
221 fputs ("failed to create test files\n", stderr
);
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. */
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)
249 /* { dg-do run { xfail *-*-* } } */\n\
250 /* { dg-options \"-w -I%s -fgnu-runtime\" } */\n", srcdir
);
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\
262 #define TX(n, type, attrs, fields, ops) \\\n\
263 type S##n { fields } attrs; \\\n\
264 void test##n (void) \\\n\
266 char *encoding = @encode (type S##n); \\\n\
267 if (objc_sizeof_type (encoding) != sizeof(type S##n)) \\\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\
273 if (objc_alignof_type (encoding) != __alignof__ (type S##n)) \\\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\
280 #include \"t%03d_test.h\"\n\
285 #define TX(n, type, attrs, fields, ops) test##n ();\n\
286 #include \"t%03d_test.h\"\n\
294 }\n", filecnt
, filecnt
);
296 sprintf (destptr
, "t%03d_test.h", filecnt
);
297 outfile
= fopen (destbuf
, "w");
302 else if (fields
<= 4)
304 else if (fields
<= 6)
310 unsigned long long int
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;
321 subfield (struct entry
*e
, char *letter
)
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
)
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
);
342 snprintf (buf
, 20, "%c[%d]", *letter
, e
[0].arr_len
);
355 fprintf (outfile
, "%s{", p
);
358 fprintf (outfile
, "%s %s{", e
[0].attrib
, p
);
361 fprintf (outfile
, "%s %s{", p
, e
[0].attrib
);
365 for (i
= 1; i
<= e
[0].len
; )
366 i
+= subfield (e
+ i
, letter
);
373 fprintf (outfile
, "}%s;", buf
);
376 fprintf (outfile
, "}%s %s;", e
[0].attrib
, buf
);
379 fprintf (outfile
, "}%s %s;", buf
, e
[0].attrib
);
385 if (e
[0].etype
== ETYPE_ARRAY
)
387 if (e
[0].arr_len
== 255)
388 snprintf (buf
, 20, "%c[]", *letter
);
390 snprintf (buf
, 20, "%c[%d]", *letter
, e
[0].arr_len
);
399 switch (generate_random () % 3)
402 fprintf (outfile
, "%s %s %s;", e
[0].attrib
, e
[0].type
->name
, buf
);
405 fprintf (outfile
, "%s %s %s;", e
[0].type
->name
, e
[0].attrib
, buf
);
408 fprintf (outfile
, "%s %s %s;", e
[0].type
->name
, buf
, e
[0].attrib
);
412 fprintf (outfile
, "%s %s;", e
[0].type
->name
, buf
);
418 switch (generate_random () % 3)
421 fprintf (outfile
, "%s %s:0;", e
[0].attrib
, e
[0].type
->name
);
424 fprintf (outfile
, "%s %s:0;", e
[0].type
->name
, e
[0].attrib
);
427 fprintf (outfile
, "%s:0 %s;", e
[0].type
->name
, e
[0].attrib
);
431 fprintf (outfile
, "%s:0;", e
[0].type
->name
);
435 switch (e
[0].type
->bitfld
)
442 snprintf (buf
, 20, "B%cN(%d)", e
[0].type
->bitfld
, e
[0].len
);
446 snprintf (buf
, 20, "%d", e
[0].len
);
452 switch (generate_random () % 3)
455 fprintf (outfile
, "%s %s %c:%s;", e
[0].attrib
, e
[0].type
->name
,
459 fprintf (outfile
, "%s %s %c:%s;", e
[0].type
->name
, e
[0].attrib
,
463 fprintf (outfile
, "%s %c:%s %s;", e
[0].type
->name
, *letter
,
468 fprintf (outfile
, "%s %c:%s;", e
[0].type
->name
, *letter
, buf
);
479 output_FNB (char mode
, struct entry
*e
)
481 unsigned long long int l1
, l2
, m
;
485 if (e
->type
->type
== TYPE_OTHER
)
489 fprintf (outfile
, "N(%d,%s)", idx
, namebuf
);
492 fprintf (outfile
, "%c(%d,%s,", mode
, idx
, namebuf
);
495 switch (e
->type
->type
)
498 signs
= generate_random () & 3;
501 m
&= e
->len
> 1 ? (1ULL << (e
->len
- 1)) - 1 : 1;
504 fprintf (outfile
, "%s%llu%s,%s%llu%s",
505 (signs
& 1) ? "-" : "", l1
, l1
> 2147483647 ? "LL" : "",
506 (signs
& 2) ? "-" : "", l2
, l2
> 2147483647 ? "LL" : "");
511 m
&= (1ULL << e
->len
) - 1;
514 fprintf (outfile
, "%lluU%s,%lluU%s", l1
, l1
> 4294967295U ? "LL" : "",
515 l2
, l2
> 4294967295U ? "LL" : "");
520 signs
= generate_random () & 3;
521 fprintf (outfile
, "%s%f,%s%f", (signs
& 1) ? "-" : "",
522 ((double) l1
) / 64, (signs
& 2) ? "-" : "", ((double) l2
) / 64);
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;
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" : "");
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" : "");
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" : "");
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);
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);
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);
577 p
= strchr (e
->type
->name
, '\0');
578 while (--p
>= e
->type
->name
&& *p
>= '0' && *p
<= '9');
583 l1
+= e
->type
->maxval
- 6;
585 l2
+= e
->type
->maxval
- 6;
586 fprintf (outfile
, "e%s_%lld,e%s_%lld", p
, l1
, p
, l2
);
590 p
= strchr (e
->type
->name
, '\0');
591 while (--p
>= e
->type
->name
&& *p
>= '0' && *p
<= '9');
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));
604 fprintf (outfile
, "(%s)&intarray[%lld],(%s)&intarray[%lld]",
605 e
->type
->name
, l1
, e
->type
->name
, l2
);
610 fprintf (outfile
, "fn%lld,fn%lld", l1
, l2
);
615 fputs (")", outfile
);
619 subvalues (struct entry
*e
, char *p
, char *letter
)
623 if (p
>= namebuf
+ sizeof (namebuf
) - 32)
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
;
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');
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;
661 if (e
[0].arr_len
== 0 || e
[0].arr_len
== 255)
666 i
= generate_random () % e
[0].arr_len
;
667 snprintf (p
, sizeof (namebuf
) - (p
- namebuf
),
668 "%c[%d]", *letter
, i
);
670 if ((generate_random () & 7) == 0)
672 j
= generate_random () % e
[0].arr_len
;
675 snprintf (p
, sizeof (namebuf
) - (p
- namebuf
),
676 "%c[%d]", *letter
, j
);
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:
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 */
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 --------------------------------------------------------------------
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 */
778 a
= b
= 0x9e3779b9; /* the golden ratio; an arbitrary value */
779 c
= initval
; /* the previous hash value */
781 /*---------------------------------------- handle most of the key */
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));
791 /*------------------------------------- handle the last 11 bytes */
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);
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);
807 /* case 0: nothing left to add */
810 /*-------------------------------------------- report the result */
815 e_hash (const void *a
)
817 const struct entry
*e
= a
;
821 if (e
[0].etype
!= ETYPE_STRUCT
&& e
[0].etype
!= ETYPE_UNION
)
823 for (i
= 0; i
<= e
[0].len
; ++i
)
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
);
830 ret
= iterative_hash (e
[i
].attrib
, attriblen
, ret
);
836 e_eq (const void *a
, const void *b
)
838 const struct entry
*ea
= a
, *eb
= b
;
840 if (ea
[0].etype
!= ETYPE_STRUCT
&& ea
[0].etype
!= ETYPE_UNION
)
842 if (ea
[0].len
!= eb
[0].len
)
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
)
851 if ((ea
[i
].attrib
== NULL
) ^ (eb
[i
].attrib
== NULL
))
853 if (ea
[i
].attrib
&& strcmp (ea
[i
].attrib
, eb
[i
].attrib
) != 0)
860 e_exists (const struct entry
*e
)
866 for (h
= hash_table
[hval
% HASH_SIZE
]; h
; h
= h
->next
)
873 e_insert (struct entry
*e
)
878 e
->next
= hash_table
[hval
% HASH_SIZE
];
879 hash_table
[hval
% HASH_SIZE
] = e
;
883 output (struct entry
*e
)
888 const char *skip_cint
= "";
890 if (e
[0].etype
!= ETYPE_STRUCT
&& e
[0].etype
!= ETYPE_UNION
)
896 n
= (struct entry
*) malloc ((e
[0].len
+ 1) * sizeof (struct entry
));
897 memcpy (n
, e
, (e
[0].len
+ 1) * sizeof (struct entry
));
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
))
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",
914 else if (e
[0].etype
== ETYPE_STRUCT
)
915 fprintf (outfile
, "T%s(%d,", skip_cint
, idx
);
917 fprintf (outfile
, "U%s(%d,", skip_cint
, idx
);
919 for (i
= 1; i
<= e
[0].len
; )
920 i
+= subfield (e
+ i
, &c
);
921 fputs (",", outfile
);
923 for (i
= 1; i
<= e
[0].len
; )
925 i
+= subvalues (e
+ i
, namebuf
, &c
);
926 if (e
[0].etype
== ETYPE_UNION
)
929 fputs (")\n", outfile
);
930 if (output_one
&& idx
== limidx
)
939 FEATURE_ZEROARRAY
= 8,
940 FEATURE_ZEROBITFLD
= 16,
941 ALL_FEATURES
= FEATURE_COMPLEX
| FEATURE_VECTOR
| FEATURE_ZEROARRAY
946 singles (enum FEATURE features
)
950 memset (e
, 0, sizeof (e
));
951 e
[0].etype
= ETYPE_STRUCT
;
953 e
[0].etype
= ETYPE_UNION
;
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
];
963 e
[0].etype
= ETYPE_UNION
;
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
];
973 e
[0].etype
= ETYPE_UNION
;
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
];
983 e
[0].etype
= ETYPE_UNION
;
989 choose_type (enum FEATURE features
, struct entry
*e
, int r
, int in_array
)
993 i
= NTYPES2
- NTYPES1
;
994 if (features
& FEATURE_COMPLEX
)
996 if (features
& FEATURE_VECTOR
)
1000 if (r
< NTYPES2
- NTYPES1
)
1001 e
->type
= &base_types
[r
+ NTYPES1
];
1002 r
-= NTYPES2
- NTYPES1
;
1003 if (e
->type
== NULL
&& (features
& FEATURE_COMPLEX
))
1006 e
->type
= &complex_types
[r
];
1009 if (e
->type
== NULL
&& (features
& FEATURE_VECTOR
))
1012 e
->type
= &vector_types
[r
];
1015 if (e
->type
== NULL
)
1019 /* This is from gcc.c-torture/execute/builtin-bitops-1.c. */
1021 my_ffsll (unsigned long long x
)
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
))
1034 generate_fields (enum FEATURE features
, struct entry
*e
, struct entry
*parent
,
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
1046 12.5% ETYPE_STRUCT|ETYPE_UNION|ETYPE_STRUCT_ARRAY|ETYPE_UNION_ARRAY */
1052 case 6: /* BITfields disabled for now as _Bool bitfields are broken. */
1057 e
[n
].etype
= ETYPE_TYPE
;
1058 e
[n
].type
= &base_types
[r
% NTYPES1
];
1061 e
[n
].etype
= ETYPE_TYPE
;
1062 choose_type (features
, &e
[n
], r
, 0);
1065 e
[n
].etype
= ETYPE_ARRAY
;
1069 e
[n
].type
= &base_types
[r
% NTYPES1
];
1071 choose_type (features
, &e
[n
], r
, 1);
1072 r
= generate_random ();
1073 if ((features
& FEATURE_ZEROARRAY
) && (r
& 3) == 0)
1076 if (n
== len
- 1 && (r
& 4)
1077 && (parent
->etype
== ETYPE_STRUCT
1078 || parent
->etype
== ETYPE_STRUCT_ARRAY
))
1081 for (k
= 0; k
< n
; ++k
)
1082 if (e
[k
].etype
!= ETYPE_BITFLD
|| e
[k
].len
)
1089 else if ((r
& 3) != 3)
1090 e
[n
].arr_len
= (r
>> 2) & 7;
1092 e
[n
].arr_len
= (r
>> 2) & 31;
1106 incr
= 1 + (r
>> 3) % (len
- n
);
1111 incr
= 1 + (r
>> 3) % (len
- n
);
1114 for (j
= n
; j
< n
+ incr
; ++j
)
1118 e
[j
].etype
= ETYPE_BITFLD
;
1119 if (j
== n
|| !sametype
)
1121 r
= generate_random ();
1124 = &bitfld_types
[r
% n_bitfld_types
];
1127 e
[j
].type
= e
[n
].type
;
1128 r
= generate_random ();
1131 switch (e
[j
].type
->bitfld
)
1133 case 'C': ma
= 8; break;
1134 case 'S': ma
= 16; break;
1135 case 'I': ma
= 32; break;
1137 case 'Q': ma
= 64; break;
1138 case 'B': ma
= 1; break;
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);
1157 if (sametype
&& (r
& 3) == 0 && ma
> 1)
1160 for (k
= n
; k
< j
; ++k
)
1163 e
[j
].len
= sum
? ma
- sum
: ma
;
1166 if (! (features
& FEATURE_ZEROBITFLD
) && mi
== 0)
1168 if (e
[j
].len
< mi
|| e
[j
].len
> ma
)
1169 e
[j
].len
= mi
+ (r
% (ma
+ 1 - mi
));
1171 if ((features
& FEATURE_ZEROBITFLD
) && (r
& 3) == 0
1183 e
[n
].etype
= ETYPE_STRUCT
;
1187 e
[n
].etype
= ETYPE_UNION
;
1191 e
[n
].etype
= ETYPE_STRUCT_ARRAY
;
1194 e
[n
].etype
= ETYPE_UNION_ARRAY
;
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)
1208 if (n
+ incr
== len
&& (r
& 4)
1209 && (parent
->etype
== ETYPE_STRUCT
1210 || parent
->etype
== ETYPE_STRUCT_ARRAY
))
1213 for (k
= 0; k
< n
; ++k
)
1214 if (e
[k
].etype
!= ETYPE_BITFLD
|| e
[k
].len
)
1221 else if ((r
& 3) != 3)
1222 e
[n
].arr_len
= (r
>> 2) & 7;
1224 e
[n
].arr_len
= (r
>> 2) & 31;
1232 generate_random_tests (enum FEATURE features
, int len
)
1234 struct entry e
[len
+ 1];
1236 if (len
> 'z' - 'a' + 1)
1238 memset (e
, 0, sizeof (e
));
1239 r
= generate_random ();
1241 e
[0].etype
= ETYPE_UNION
;
1243 e
[0].etype
= ETYPE_STRUCT
;
1246 generate_fields (features
, &e
[1], &e
[0], len
);
1250 struct { const char *name
; enum FEATURE f
; }
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;
1266 if (sizeof (int) != 4 || sizeof (long long) != 8)
1273 if (argv
[i
][0] == '-' && argv
[i
][2] == '\0')
1275 optarg
= argv
[i
+ 1];
1291 limidx
= atoi (optarg
);
1294 fprintf (stderr
, "unrecognized option %s\n", argv
[i
]);
1302 outfile
= fopen ("/dev/null", "w");
1303 if (outfile
== NULL
)
1305 fputs ("could not open /dev/null", stderr
);
1311 if (destdir
== NULL
&& !output_one
)
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]);
1320 if (srcdir
== NULL
&& !output_one
)
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
)
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
)
1347 singles (features
[i
].f
);
1351 while (idx
< startidx
+ 1000)
1352 generate_random_tests (features
[i
].f
, 1);
1358 while (idx
< startidx
+ 100)
1359 generate_random_tests (features
[i
].f
, 1);
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));
1371 generate_random_tests (ALL_FEATURES
, 1 + (generate_random () % 25));