📄 struct-layout-1_generate.c
字号:
{ "Talx1E4", TYPE_UENUM, 255, ' ' },{ "Talx1E5", TYPE_SENUM, 32767, ' ' },{ "Talx1E6", TYPE_UENUM, 65535, ' ' },{ "Talx1E7", TYPE_SENUM, 2147483647, ' ' },{ "Talx1E8", TYPE_UENUM, 4294967295U, ' ' },{ "Talx1E9", TYPE_SENUM, 1099511627775LL, ' ' },{ "Talx2short", TYPE_INT, 32767, 'S' },{ "Talx2ushort", TYPE_UINT, 65535, 'S' },{ "Talx2int", TYPE_INT, 2147483647, 'I' },{ "Talx2uint", TYPE_UINT, 4294967295U, 'I' },{ "Talx2long", TYPE_INT, 9223372036854775807LL, 'L' },{ "Talx2ulong", TYPE_UINT, 18446744073709551615ULL, 'L' },{ "Talx2llong", TYPE_INT, 9223372036854775807LL, 'Q' },{ "Talx2ullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },{ "Talx2ptr", TYPE_PTR, 0, 0 },{ "Talx2cptr", TYPE_PTR, 0, 0 },{ "Talx2iptr", TYPE_PTR, 0, 0 },{ "Talx2float", TYPE_FLOAT, 0, 0 },{ "Talx2double", TYPE_FLOAT, 0, 0 },{ "Talx2ldouble", TYPE_FLOAT, 0, 0 },{ "Talx2E0", TYPE_UENUM, 0, ' ' },{ "Talx2E1", TYPE_UENUM, 1, ' ' },{ "Talx2E2", TYPE_SENUM, 3, ' ' },{ "Talx2E3", TYPE_SENUM, 127, ' ' },{ "Talx2E4", TYPE_UENUM, 255, ' ' },{ "Talx2E5", TYPE_SENUM, 32767, ' ' },{ "Talx2E6", TYPE_UENUM, 65535, ' ' },{ "Talx2E7", TYPE_SENUM, 2147483647, ' ' },{ "Talx2E8", TYPE_UENUM, 4294967295U, ' ' },{ "Talx2E9", TYPE_SENUM, 1099511627775LL, ' ' },{ "Talx4int", TYPE_INT, 2147483647, 'I' },{ "Talx4uint", TYPE_UINT, 4294967295U, 'I' },{ "Talx4long", TYPE_INT, 9223372036854775807LL, 'L' },{ "Talx4ulong", TYPE_UINT, 18446744073709551615ULL, 'L' },{ "Talx4llong", TYPE_INT, 9223372036854775807LL, 'Q' },{ "Talx4ullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },{ "Talx4ptr", TYPE_PTR, 0, 0 },{ "Talx4cptr", TYPE_PTR, 0, 0 },{ "Talx4iptr", TYPE_PTR, 0, 0 },{ "Talx4float", TYPE_FLOAT, 0, 0 },{ "Talx4double", TYPE_FLOAT, 0, 0 },{ "Talx4ldouble", TYPE_FLOAT, 0, 0 },{ "Talx4E0", TYPE_UENUM, 0, ' ' },{ "Talx4E1", TYPE_UENUM, 1, ' ' },{ "Talx4E2", TYPE_SENUM, 3, ' ' },{ "Talx4E3", TYPE_SENUM, 127, ' ' },{ "Talx4E4", TYPE_UENUM, 255, ' ' },{ "Talx4E5", TYPE_SENUM, 32767, ' ' },{ "Talx4E6", TYPE_UENUM, 65535, ' ' },{ "Talx4E7", TYPE_SENUM, 2147483647, ' ' },{ "Talx4E8", TYPE_UENUM, 4294967295U, ' ' },{ "Talx4E9", TYPE_SENUM, 1099511627775LL, ' ' },{ "Taly8long", TYPE_INT, 9223372036854775807LL, 'L' },{ "Taly8ulong", TYPE_UINT, 18446744073709551615ULL, 'L' },{ "Talx8llong", TYPE_INT, 9223372036854775807LL, 'Q' },{ "Talx8ullong", TYPE_UINT, 18446744073709551615ULL, 'Q' },{ "Taly8ptr", TYPE_PTR, 0, 0 },{ "Taly8cptr", TYPE_PTR, 0, 0 },{ "Taly8iptr", TYPE_PTR, 0, 0 },{ "Talx8double", TYPE_FLOAT, 0, 0 },{ "Talx8ldouble", TYPE_FLOAT, 0, 0 }#define NAATYPES2 (sizeof (attrib_array_types) / sizeof (attrib_array_types[0]))};struct types complex_attrib_array_types[] = {{ "Talx1cchar", TYPE_CUINT, 127, 0 },{ "Talx1cschar", TYPE_CINT, 127, 0 },{ "Talx1cuchar", TYPE_CUINT, 255, 0 },{ "Talx1cshort", TYPE_CINT, 32767, 0 },{ "Talx1cushort", TYPE_CUINT, 65535, 0 },{ "Talx1cint", TYPE_CINT, 2147483647, 0 },{ "Talx1cuint", TYPE_CUINT, 4294967295U, 0 },{ "Talx1clong", TYPE_CINT, 9223372036854775807LL, 0 },{ "Talx1culong", TYPE_CUINT, 18446744073709551615ULL, 0 },{ "Talx1cllong", TYPE_CINT, 9223372036854775807LL, 0 },{ "Talx1cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },{ "Talx1cfloat", TYPE_CFLOAT, 0, 0 },{ "Talx1cdouble", TYPE_CFLOAT, 0, 0 },{ "Talx1cldouble", TYPE_CFLOAT, 0, 0 },{ "Talx2cchar", TYPE_CUINT, 127, 0 },{ "Talx2cschar", TYPE_CINT, 127, 0 },{ "Talx2cuchar", TYPE_CUINT, 255, 0 },{ "Talx2cshort", TYPE_CINT, 32767, 0 },{ "Talx2cushort", TYPE_CUINT, 65535, 0 },{ "Talx2cint", TYPE_CINT, 2147483647, 0 },{ "Talx2cuint", TYPE_CUINT, 4294967295U, 0 },{ "Talx2clong", TYPE_CINT, 9223372036854775807LL, 0 },{ "Talx2culong", TYPE_CUINT, 18446744073709551615ULL, 0 },{ "Talx2cllong", TYPE_CINT, 9223372036854775807LL, 0 },{ "Talx2cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },{ "Talx2cfloat", TYPE_CFLOAT, 0, 0 },{ "Talx2cdouble", TYPE_CFLOAT, 0, 0 },{ "Talx2cldouble", TYPE_CFLOAT, 0, 0 },{ "Talx4cshort", TYPE_CINT, 32767, 0 },{ "Talx4cushort", TYPE_CUINT, 65535, 0 },{ "Talx4cint", TYPE_CINT, 2147483647, 0 },{ "Talx4cuint", TYPE_CUINT, 4294967295U, 0 },{ "Talx4clong", TYPE_CINT, 9223372036854775807LL, 0 },{ "Talx4culong", TYPE_CUINT, 18446744073709551615ULL, 0 },{ "Talx4cllong", TYPE_CINT, 9223372036854775807LL, 0 },{ "Talx4cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },{ "Talx4cfloat", TYPE_CFLOAT, 0, 0 },{ "Talx4cdouble", TYPE_CFLOAT, 0, 0 },{ "Talx4cldouble", TYPE_CFLOAT, 0, 0 },{ "Talx8cint", TYPE_CINT, 2147483647, 0 },{ "Talx8cuint", TYPE_CUINT, 4294967295U, 0 },{ "Talx8clong", TYPE_CINT, 9223372036854775807LL, 0 },{ "Talx8culong", TYPE_CUINT, 18446744073709551615ULL, 0 },{ "Talx8cllong", TYPE_CINT, 9223372036854775807LL, 0 },{ "Talx8cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },{ "Talx8cfloat", TYPE_CFLOAT, 0, 0 },{ "Talx8cdouble", TYPE_CFLOAT, 0, 0 },{ "Talx8cldouble", TYPE_CFLOAT, 0, 0 },{ "Taly16clong", TYPE_CINT, 9223372036854775807LL, 0 },{ "Taly16culong", TYPE_CUINT, 18446744073709551615ULL, 0 },{ "Talx16cllong", TYPE_CINT, 9223372036854775807LL, 0 },{ "Talx16cullong", TYPE_CUINT, 18446744073709551615ULL, 0 },{ "Talx16cdouble", TYPE_CFLOAT, 0, 0 },{ "Talx16cldouble", TYPE_CFLOAT, 0, 0 }#define NCAATYPES2 (sizeof (complex_attrib_array_types) / sizeof (complex_attrib_array_types[0]))};struct types bitfld_types[NTYPES2];int n_bitfld_types;struct types aligned_bitfld_types[NATYPES2];int n_aligned_bitfld_types;const char *attributes[] = {"atal", "atpa", "atal1", "atal2", "atal4", "atal8", "atal16", #define NATTRIBS1 7"atalpa", "atpaal", "atal1pa", "atal2pa", "atal4pa", "atal8pa", "atal16pa", "atpaal1", "atpaal2", "atpaal4", "atpaal8", "atpaal16"#define NATTRIBS2 (sizeof (attributes) / sizeof (attributes[0]))};enum ETYPE{ ETYPE_TYPE, ETYPE_ARRAY, ETYPE_BITFLD, ETYPE_STRUCT, ETYPE_UNION, ETYPE_STRUCT_ARRAY, ETYPE_UNION_ARRAY};struct entry{#ifdef __GNUC__ enum ETYPE etype : 8;#else unsigned char etype;#endif unsigned short len; unsigned char arr_len; struct types *type; const char *attrib; /* Used to chain together entries in the hash table. */ struct entry *next;};/* A prime number giving the number of slots in the hash table. */ #define HASH_SIZE 32749 static struct entry *hash_table[HASH_SIZE];static int idx, limidx, output_one;static const char *destdir;static const char *srcdir;FILE *outfile;voidswitchfiles (int fields){ static int filecnt; static char *destbuf, *destptr; ++filecnt; if (outfile) fclose (outfile); if (output_one) { outfile = stdout; return; } if (destbuf == NULL) { size_t len = strlen (destdir); destbuf = malloc (len + 20); if (!destbuf) abort (); memcpy (destbuf, destdir, len); if (!len || destbuf[len - 1] != '/') destbuf[len++] = '/'; destptr = destbuf + len; } sprintf (destptr, "t%03d_main.c", filecnt); outfile = fopen (destbuf, "w"); if (outfile == NULL) { fail: fputs ("failed to create test files\n", stderr); exit (1); } fprintf (outfile, "\/* { dg-options \"-I%s\" } */\n\/* { dg-options \"-I%s -fno-common\" { target hppa*-*-hpux* } } */\n\#include \"struct-layout-1.h\"\n\\n\#define TX(n, type, attrs, fields, ops) extern void test##n (void);\n\#include \"t%03d_test.h\"\n\#undef TX\n\\n\int main (void)\n\{\n\#define TX(n, type, attrs, fields, ops) test##n ();\n\#include \"t%03d_test.h\"\n\#undef TX\n\ if (fails)\n\ {\n\ fflush (stdout);\n\ abort ();\n\ }\n\ exit (0);\n\}\n", srcdir, srcdir, filecnt, filecnt); fclose (outfile); sprintf (destptr, "t%03d_x.c", filecnt); outfile = fopen (destbuf, "w"); if (outfile == NULL) goto fail; fprintf (outfile, "\/* { dg-options \"-w -I%s\" } */\n\/* { dg-options \"-w -I%s -fno-common\" { target hppa*-*-hpux* } } */\n\#include \"struct-layout-1_x1.h\"\n\#include \"t%03d_test.h\"\n\#include \"struct-layout-1_x2.h\"\n\#include \"t%03d_test.h\"\n", srcdir, srcdir, filecnt, filecnt); fclose (outfile); sprintf (destptr, "t%03d_y.c", filecnt); outfile = fopen (destbuf, "w"); if (outfile == NULL) goto fail; fprintf (outfile, "\/* { dg-options \"-w -I%s\" } */\n\/* { dg-options \"-w -I%s -fno-common\" { target hppa*-*-hpux* } } */\n\#include \"struct-layout-1_y1.h\"\n\#include \"t%03d_test.h\"\n\#include \"struct-layout-1_y2.h\"\n\#include \"t%03d_test.h\"\n", srcdir, srcdir, filecnt, filecnt); fclose (outfile); sprintf (destptr, "t%03d_test.h", filecnt); outfile = fopen (destbuf, "w"); if (outfile == NULL) goto fail; if (fields <= 2) limidx = idx + 300; else if (fields <= 4) limidx = idx + 200; else if (fields <= 6) limidx = idx + 100; else limidx = idx + 50;}unsigned long long intgetrandll (void){ unsigned long long int ret; ret = generate_random () & 0xffffff; ret |= (generate_random () & 0xffffffLL) << 24; ret |= ((unsigned long long int) generate_random ()) << 48; return ret;}intsubfield (struct entry *e, char *letter){ int i, type; char buf[20]; const char *p; switch (e[0].etype) { case ETYPE_STRUCT: case ETYPE_UNION: case ETYPE_STRUCT_ARRAY: case ETYPE_UNION_ARRAY: type = e[0].attrib ? 1 + (generate_random () & 3) : 0; if (e[0].etype == ETYPE_STRUCT || e[0].etype == ETYPE_STRUCT_ARRAY) p = "struct"; else p = "union"; if (e[0].etype == ETYPE_STRUCT_ARRAY || e[0].etype == ETYPE_UNION_ARRAY) { if (e[0].arr_len == 255) snprintf (buf, 20, "%c[]", *letter); else snprintf (buf, 20, "%c[%d]", *letter, e[0].arr_len); } else { buf[0] = *letter; buf[1] = '\0'; } ++*letter; switch (type) { case 0: case 3: case 4: fprintf (outfile, "%s{", p); break; case 1: fprintf (outfile, "%s %s{", e[0].attrib, p); break; case 2: fprintf (outfile, "%s %s{", p, e[0].attrib); break; } for (i = 1; i <= e[0].len; ) i += subfield (e + i, letter); switch (type) { case 0: case 1: case 2: fprintf (outfile, "}%s;", buf); break; case 3: fprintf (outfile, "}%s %s;", e[0].attrib, buf); break; case 4: fprintf (outfile, "}%s %s;", buf, e[0].attrib); break; } return 1 + e[0].len; case ETYPE_TYPE: case ETYPE_ARRAY: if (e[0].etype == ETYPE_ARRAY) { if (e[0].arr_len == 255) snprintf (buf, 20, "%c[]", *letter); else snprintf (buf, 20, "%c[%d]", *letter, e[0].arr_len); } else { buf[0] = *letter; buf[1] = '\0'; } ++*letter; if (e[0].attrib) switch (generate_random () % 3) { case 0: fprintf (outfile, "%s %s %s;", e[0].attrib, e[0].type->name, buf); break; case 1: fprintf (outfile, "%s %s %s;", e[0].type->name, e[0].attrib, buf); break; case 2: fprintf (outfile, "%s %s %s;", e[0].type->name, buf, e[0].attrib); break; } else fprintf (outfile, "%s %s;", e[0].type->name, buf); return 1; case ETYPE_BITFLD: if (e[0].len == 0) { if (e[0].attrib) switch (generate_random () % 3) { case 0: fprintf (outfile, "%s %s:0;", e[0].attrib, e[0].type->name); break; case 1: fprintf (outfile, "%s %s:0;", e[0].type->name, e[0].attrib); break; case 2: fprintf (outfile, "%s:0 %s;", e[0].type->name, e[0].attrib); break; } else fprintf (outfile, "%s:0;", e[0].type->name); ++*letter; return 1; } switch (e[0].type->bitfld) { case 'C': case 'S': case 'I': case 'L': case 'Q': snprintf (buf, 20, "B%cN(%d)", e[0].type->bitfld, e[0].len); break; case 'B': case ' ': snprintf (buf, 20, "%d", e[0].len); break; default: abort (); } if (e[0].attrib) switch (generate_random () % 3) { case 0: fprintf (outfile, "%s %s %c:%s;", e[0].attrib, e[0].type->name, *letter, buf); break; case 1: fprintf (outfile, "%s %s %c:%s;", e[0].type->name, e[0].attrib, *letter, buf); break; case 2: fprintf (outfile, "%s %c:%s %s;", e[0].type->name, *letter, buf, e[0].attrib); break; } else fprintf (outfile, "%s %c:%s;", e[0].type->name, *letter, buf); ++*letter; return 1; default: abort (); }}char namebuf[1024];voidoutput_FNB (char mode, struct entry *e){ unsigned long long int l1, l2, m; int signs = 0; const char *p, *q; if (e->type->type == TYPE_OTHER) { if (mode == 'B') abort (); fprintf (outfile, "N(%d,%s)", idx, namebuf); return; } fprintf (outfile, "%c(%d,%s,", mode, idx, namebuf); l1 = getrandll (); l2 = getrandll (); switch (e->type->type) { case TYPE_INT: signs = generate_random () & 3; m = e->type->maxval; if (mode == 'B') m &= e->len > 1 ? (1ULL << (e->len - 1)) - 1 : 1; l1 &= m; l2 &= m; fprintf (outfile, "%s%llu%s,%s%llu%s", (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "", (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : ""); break; case TYPE_UINT: m = e->type->maxval; if (mode == 'B')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -