⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 struct-layout-1_generate.c

📁 用于进行gcc测试
💻 C
📖 第 1 页 / 共 3 页
字号:
  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\/* { dg-options \"-w -I%s -mno-base-addresses\" { target mmix-*-* } } */\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, 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\/* { dg-options \"-w -I%s -mno-base-addresses\" { target mmix-*-* } } */\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, 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);	  /* If this is an array type, do not put aligned attributes on	     elements.  Aligning elements to a value greater than their	     size will result in a compiler error.  */	  if (type == 1	      && ((strncmp (e[0].attrib, "atal", 4) == 0)		   || strncmp (e[0].attrib, "atpaal", 6) == 0))	    type = 2;	}      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)	{	  /* If this is an array type, do not put aligned attributes on	     elements.  Aligning elements to a value greater than their	     size will result in a compiler error.  */	  if (e[0].etype == ETYPE_ARRAY	      && ((strncmp (e[0].attrib, "atal", 4) == 0)		   || strncmp (e[0].attrib, "atpaal", 6) == 0))	    type = 2;	  else	    type = generate_random () % 3;	  switch (type)	    {	    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;	}      snprintf (buf, 20, "%d", e[0].len);      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%" COMPAT_PRLL "u%s,%s%" COMPAT_PRLL "u%s",	       (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "",	       (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : "");      break;    case TYPE_UINT:      m = e->type->maxval;      if (mode == 'B')	m &= (1ULL << e->len) - 1;      l1 &= m;      l2 &= m;      fprintf (outfile,"%" COMPAT_PRLL "uU%s,%" COMPAT_PRLL "uU%s",	       l1, l1 > 4294967295U ? "LL" : "",	       l2, l2 > 4294967295U ? "LL" : "");      break;    case TYPE_FLOAT:      l1 &= 0xffffff;      l2 &= 0xffffff;      signs = generate_random () & 3;      fprintf (outfile, "%s%f,%s%f", (signs & 1) ? "-" : "",	       ((double) l1) / 64, (signs & 2) ? "-" : "", ((double) l2) / 64);      break;    case TYPE_UENUM:      if (e->type->maxval == 0)	fputs ("e0_0,e0_0", outfile);      else if (e->type->maxval == 1)	fprintf (outfile, "e1_%" COMPAT_PRLL "d,e1_%" COMPAT_PRLL "d",		 l1 & 1, l2 & 1);      else	{	  p = strchr (e->type->name, '\0');	  while (--p >= e->type->name && *p >= '0' && *p <= '9');	  p++;	  l1 %= 7;	  l2 %= 7;	  if (l1 > 3)	    l1 += e->type->maxval - 6;	  if (l2 > 3)	    l2 += e->type->maxval - 6;	  fprintf (outfile, "e%s_%" COMPAT_PRLL "d,e%s_%" COMPAT_PRLL "d",		   p, l1, p, l2);	}      break;    case TYPE_SENUM:      p = strchr (e->type->name, '\0');      while (--p >= e->type->name && *p >= '0' && *p <= '9');      p++;      l1 %= 7;      l2 %= 7;      fprintf (outfile, "e%s_%s%" COMPAT_PRLL "d,e%s_%s%" COMPAT_PRLL "d",	       p, l1 < 3 ? "m" : "",	       l1 == 3 ? 0LL : e->type->maxval - (l1 & 3),	       p, l2 < 3 ? "m" : "",	       l2 == 3 ? 0LL : e->type->maxval - (l2 & 3));      break;    case TYPE_PTR:      l1 %= 256;      l2 %= 256;      fprintf (outfile,	      "(%s)&intarray[%" COMPAT_PRLL "d], (%s)&intarray[%" COMPAT_PRLL "d]",	       e->type->name, l1, e->type->name, l2);      break;    case TYPE_FNPTR:      l1 %= 10;      l2 %= 10;      fprintf (outfile,	       "fn%" COMPAT_PRLL "d,fn%" COMPAT_PRLL "d", l1, l2);      break;    default:      abort ();    }  fputs (")", outfile);}intsubvalues (struct entry *e, char *p, char *letter){  int i, j;  char *q;  if (p >= namebuf + sizeof (namebuf) - 32)    abort ();  p[0] = *letter;  p[1] = '\0';  q = p + 1;  switch (e[0].etype)    {    case ETYPE_STRUCT_ARRAY:    case ETYPE_UNION_ARRAY:      if (e[0].arr_len == 0 || e[0].arr_len == 255)	{	  *letter += 1 + e[0].len;	  return 1 + e[0].len;	}      i = generate_random () % e[0].arr_len;      snprintf (p, sizeof (namebuf) - (p - namebuf) - 1,		"%c[%d]", *letter, i);      q = strchr (p, '\0');      /* FALLTHROUGH */    case ETYPE_STRUCT:    case ETYPE_UNION:      *q++ = '.';      ++*letter;      for (i = 1; i <= e[0].len; )	{	  i += subvalues (e + i, q, letter);	  if (e[0].etype == ETYPE_UNION || e[0].etype == ETYPE_UNION_ARRAY)	    {	      *letter += e[0].len - i + 1;	      break;	    }	}      return 1 + e[0].len;    case ETYPE_TYPE:      ++*letter;      output_FNB ('F', e);      return 1;    case ETYPE_ARRAY:      if (e[0].arr_len == 0 || e[0].arr_len == 255)	{	  ++*letter;	  return 1;	}      i = generate_random () % e[0].arr_len;      snprintf (p, sizeof (namebuf) - (p - namebuf),		"%c[%d]", *letter, i);      output_FNB ('F', e);      if ((generate_random () & 7) == 0)	{	  j = generate_random () % e[0].arr_len;	  if (i != j)	    {	      snprintf (p, sizeof (namebuf) - (p - namebuf),			"%c[%d]", *letter, j);	      output_FNB ('F', e);	    }	}      ++*letter;      return 1;    case ETYPE_BITFLD:      ++*letter;      if (e[0].len != 0)	output_FNB ('B', e);      return 1;    }}/* DERIVED FROM:--------------------------------------------------------------------lookup2.c, by Bob Jenkins, December 1996, Public Domain.hash(), hash2(), hash3, and mix() are externally useful functions.Routines to test the hash are included if SELF_TEST is defined.You can use this free for any purpose.  It has no warranty.--------------------------------------------------------------------*//*--------------------------------------------------------------------mix -- mix 3 32-bit values reversibly.For every delta with one or two bit set, and the deltas of all three  high bits or all three low bits, whether the original value of a,b,c  is almost all zero or is uniformly distributed,* If mix() is run forward or backward, at least 32 bits in a,b,c  have at least 1/4 probability of changing.* If mix() is run forward, every bit of c will change between 1/3 and  2/3 of the time.  (Well, 22/100 and 78/100 for some 2-bit deltas.)mix() was built out of 36 single-cycle latency instructions in a   structure that could supported 2x parallelism, like so:      a -= b;       a -= c; x = (c>>13);      b -= c; a ^= x;      b -= a; x = (a<<8);      c -= a; b ^= x;      c -= b; x = (b>>13);      ...  Unfortunately, superscalar Pentiums and Sparcs can't take advantage   of that parallelism.  They've also turned some of those single-cycle  latency instructions into multi-cycle latency instructions.  Still,  this is the fastest good hash I could find.  There were about 2^^68  to choose from.  I only looked at a billion or so.--------------------------------------------------------------------*//* same, but slower, works on systems that might have 8 byte hashval_t's */#define mix(a,b,c) \{ \  a -= b; a -= c; a ^= (c>>13); \  b -= c; b -= a; b ^= (a<< 8); \  c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \  a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \  b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \  c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \  a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \  b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \  c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \}/*--------------------------------------------------------------------hash() -- hash a variable-length key into a 32-bit value  k     : the key (the unaligned variable-length array of bytes)  len   : the length of the key, counting by bytes  level : can be any 4-byte valueReturns a 32-bit value.  Every bit of the key affects every bit ofthe return value.  Every 1-bit and 2-bit delta achieves avalanche.About 36+6len instructions.The best hash table sizes are powers of 2.  There is no need to domod a prime (mod is sooo slow!).  If you need less than 32 bits,use a bitmask.  For example, if you need only 10 bits, do  h = (h & hashmask(10));In which case, the hash table should have hashsize(10) elements.If you are hashing n strings (ub1 **)k, do it like this:  for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);By Bob Jenkins, 1996.  bob_jenkins@burtleburtle.net.  You may use thiscode any way you wish, private, educational, or commercial.  It's free.See http://burtleburtle.net/bob/hash/evahash.htmlUse for hash table lookup, or anything where one collision in 2^32 isacceptable.  Do NOT use for cryptographic purposes.--------------------------------------------------------------------*/static hashval_titerative_hash (const void *k_in /* the key */,		register size_t  length /* the length of the key */,		register hashval_t initval /* the previous hash, or					      an arbitrary value */){  register const unsigned char *k = (const unsigned char *)k_in;  register hashval_t a,b,c,len;  /* Set up the internal state */  len = length;  a = b = 0x9e3779b9;  /* the golden ratio; an arbitrary value */  c = initval;	   /* the previous hash value */  /*---------------------------------------- handle most of the key */    while (len >= 12)      {	a += (k[0] +((hashval_t)k[1]<<8) +((hashval_t)k[2]<<16) +((hashval_t)k[3]<<24));	b += (k[4] +((hashval_t)k[5]<<8) +((hashval_t)k[6]<<16) +((hashval_t)k[7]<<24));	c += (k[8] +((hashval_t)k[9]<<8) +((hashval_t)k[10]<<16)+((hashval_t)k[11]<<24));	mix(a,b,c);	k += 12; len -= 12;      }  /*------------------------------------- handle the last 11 bytes */  c += length;  switch(len)	      /* all the case statements fall through */    {    case 11: c+=((hashval_t)k[10]<<24);    case 10: c+=((hashval_t)k[9]<<16);    case 9 : c+=((hashval_t)k[8]<<8);      /* the first byte of c is reserved for the length */    case 8 : b+=((hashval_t)k[7]<<24);    case 7 : b+=((hashval_t)k[6]<<16);    case 6 : b+=((hashval_t)k[5]<<8);    case 5 : b+=k[4];    case 4 : a+=((hashval_t)k[3]<<24);    case 3 : a+=((hashval_t)k[2]<<16);    case 2 : a+=((hashval_t)k[1]<<8);    case 1 : a+=k[0];      /* case 0: nothing left to add */    }  mix(a,b,c);  /*-------------------------------------------- report the result */  return c;}hashval_te_hash (const void *a){  const struct entry *e = a;  hashval_t ret = 0;  int i;  if (e[0].etype != ETYPE_STRUCT && e[0].etype != ETYPE_UNION)    abort ();  for (i = 0; i <= e[0].len; ++i)    {      int attriblen;      ret = iterative_hash (&e[i], offsetof (struct entry, attrib), ret);      attriblen = e[i].attrib ? strlen (e[i].attrib) : -1;      ret = iterative_hash (&attriblen, sizeof (int), ret);      if (e[i].attrib)	ret = iterative_hash (e[i].attrib, attriblen, ret);    }  return ret;}inte_eq (const void *a, const void *b){  const struct entry *ea = a, *eb = b;  int i;  if (ea[0].etype != ETYPE_STRUCT && ea[0].etype != ETYPE_UNION)    abort ();  if (ea[0].len != eb[0].len)    return 0;  for (i = 0; i <= ea[0].len; ++i)    {      if (ea[i].etype != eb[i].etype	  || ea[i].len != eb[i].len	  || ea[i].arr_len != eb[i].arr_len	  || ea[i].type != eb[i].type)	return 0;      if ((ea[i].attrib == NULL) ^ (eb[i].attrib == NULL))	return 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -