github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/cmd/gc/align.c (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  #include <u.h>
     6  #include <libc.h>
     7  #include "go.h"
     8  
     9  /*
    10   * machine size and rounding
    11   * alignment is dictated around
    12   * the size of a pointer, set in betypeinit
    13   * (see ../6g/galign.c).
    14   */
    15  
    16  static int defercalc;
    17  
    18  vlong
    19  rnd(vlong o, vlong r)
    20  {
    21  	if(r < 1 || r > 8 || (r&(r-1)) != 0)
    22  		fatal("rnd");
    23  	return (o+r-1)&~(r-1);
    24  }
    25  
    26  static void
    27  offmod(Type *t)
    28  {
    29  	Type *f;
    30  	int32 o;
    31  
    32  	o = 0;
    33  	for(f=t->type; f!=T; f=f->down) {
    34  		if(f->etype != TFIELD)
    35  			fatal("offmod: not TFIELD: %lT", f);
    36  		f->width = o;
    37  		o += widthptr;
    38  		if(o >= MAXWIDTH) {
    39  			yyerror("interface too large");
    40  			o = widthptr;
    41  		}
    42  	}
    43  }
    44  
    45  static vlong
    46  widstruct(Type *errtype, Type *t, vlong o, int flag)
    47  {
    48  	Type *f;
    49  	int64 w;
    50  	int32 maxalign;
    51  	
    52  	maxalign = flag;
    53  	if(maxalign < 1)
    54  		maxalign = 1;
    55  	for(f=t->type; f!=T; f=f->down) {
    56  		if(f->etype != TFIELD)
    57  			fatal("widstruct: not TFIELD: %lT", f);
    58  		if(f->type == T) {
    59  			// broken field, just skip it so that other valid fields
    60  			// get a width.
    61  			continue;
    62  		}
    63  		dowidth(f->type);
    64  		if(f->type->align > maxalign)
    65  			maxalign = f->type->align;
    66  		if(f->type->width < 0)
    67  			fatal("invalid width %lld", f->type->width);
    68  		w = f->type->width;
    69  		if(f->type->align > 0)
    70  			o = rnd(o, f->type->align);
    71  		f->width = o;	// really offset for TFIELD
    72  		if(f->nname != N) {
    73  			// this same stackparam logic is in addrescapes
    74  			// in typecheck.c.  usually addrescapes runs after
    75  			// widstruct, in which case we could drop this,
    76  			// but function closure functions are the exception.
    77  			if(f->nname->stackparam) {
    78  				f->nname->stackparam->xoffset = o;
    79  				f->nname->xoffset = 0;
    80  			} else
    81  				f->nname->xoffset = o;
    82  		}
    83  		o += w;
    84  		if(o >= MAXWIDTH) {
    85  			yyerror("type %lT too large", errtype);
    86  			o = 8;  // small but nonzero
    87  		}
    88  	}
    89  	// final width is rounded
    90  	if(flag)
    91  		o = rnd(o, maxalign);
    92  	t->align = maxalign;
    93  
    94  	// type width only includes back to first field's offset
    95  	if(t->type == T)
    96  		t->width = 0;
    97  	else
    98  		t->width = o - t->type->width;
    99  	return o;
   100  }
   101  
   102  void
   103  dowidth(Type *t)
   104  {
   105  	int32 et;
   106  	int64 w;
   107  	int lno;
   108  	Type *t1;
   109  
   110  	if(widthptr == 0)
   111  		fatal("dowidth without betypeinit");
   112  
   113  	if(t == T)
   114  		return;
   115  
   116  	if(t->width > 0)
   117  		return;
   118  
   119  	if(t->width == -2) {
   120  		lno = lineno;
   121  		lineno = t->lineno;
   122  		if(!t->broke)
   123  			yyerror("invalid recursive type %T", t);
   124  		t->width = 0;
   125  		lineno = lno;
   126  		return;
   127  	}
   128  
   129  	// defer checkwidth calls until after we're done
   130  	defercalc++;
   131  
   132  	lno = lineno;
   133  	lineno = t->lineno;
   134  	t->width = -2;
   135  	t->align = 0;
   136  
   137  	et = t->etype;
   138  	switch(et) {
   139  	case TFUNC:
   140  	case TCHAN:
   141  	case TMAP:
   142  	case TSTRING:
   143  		break;
   144  
   145  	default:
   146  		/* simtype == 0 during bootstrap */
   147  		if(simtype[t->etype] != 0)
   148  			et = simtype[t->etype];
   149  		break;
   150  	}
   151  
   152  	w = 0;
   153  	switch(et) {
   154  	default:
   155  		fatal("dowidth: unknown type: %T", t);
   156  		break;
   157  
   158  	/* compiler-specific stuff */
   159  	case TINT8:
   160  	case TUINT8:
   161  	case TBOOL:		// bool is int8
   162  		w = 1;
   163  		break;
   164  	case TINT16:
   165  	case TUINT16:
   166  		w = 2;
   167  		break;
   168  	case TINT32:
   169  	case TUINT32:
   170  	case TFLOAT32:
   171  		w = 4;
   172  		break;
   173  	case TINT64:
   174  	case TUINT64:
   175  	case TFLOAT64:
   176  	case TCOMPLEX64:
   177  		w = 8;
   178  		t->align = widthreg;
   179  		break;
   180  	case TCOMPLEX128:
   181  		w = 16;
   182  		t->align = widthreg;
   183  		break;
   184  	case TPTR32:
   185  		w = 4;
   186  		checkwidth(t->type);
   187  		break;
   188  	case TPTR64:
   189  		w = 8;
   190  		checkwidth(t->type);
   191  		break;
   192  	case TUNSAFEPTR:
   193  		w = widthptr;
   194  		break;
   195  	case TINTER:		// implemented as 2 pointers
   196  		w = 2*widthptr;
   197  		t->align = widthptr;
   198  		offmod(t);
   199  		break;
   200  	case TCHAN:		// implemented as pointer
   201  		w = widthptr;
   202  		checkwidth(t->type);
   203  
   204  		// make fake type to check later to
   205  		// trigger channel argument check.
   206  		t1 = typ(TCHANARGS);
   207  		t1->type = t;
   208  		checkwidth(t1);
   209  		break;
   210  	case TCHANARGS:
   211  		t1 = t->type;
   212  		dowidth(t->type);	// just in case
   213  		if(t1->type->width >= (1<<16))
   214  			yyerror("channel element type too large (>64kB)");
   215  		t->width = 1;
   216  		break;
   217  	case TMAP:		// implemented as pointer
   218  		w = widthptr;
   219  		checkwidth(t->type);
   220  		checkwidth(t->down);
   221  		break;
   222  	case TFORW:		// should have been filled in
   223  		if(!t->broke)
   224  			yyerror("invalid recursive type %T", t);
   225  		w = 1;	// anything will do
   226  		break;
   227  	case TANY:
   228  		// dummy type; should be replaced before use.
   229  		if(!debug['A'])
   230  			fatal("dowidth any");
   231  		w = 1;	// anything will do
   232  		break;
   233  	case TSTRING:
   234  		if(sizeof_String == 0)
   235  			fatal("early dowidth string");
   236  		w = sizeof_String;
   237  		t->align = widthptr;
   238  		break;
   239  	case TARRAY:
   240  		if(t->type == T)
   241  			break;
   242  		if(t->bound >= 0) {
   243  			uint64 cap;
   244  
   245  			dowidth(t->type);
   246  			if(t->type->width != 0) {
   247  				cap = (MAXWIDTH-1) / t->type->width;
   248  				if(t->bound > cap)
   249  					yyerror("type %lT larger than address space", t);
   250  			}
   251  			w = t->bound * t->type->width;
   252  			t->align = t->type->align;
   253  		}
   254  		else if(t->bound == -1) {
   255  			w = sizeof_Array;
   256  			checkwidth(t->type);
   257  			t->align = widthptr;
   258  		}
   259  		else if(t->bound == -100) {
   260  			if(!t->broke) {
   261  				yyerror("use of [...] array outside of array literal");
   262  				t->broke = 1;
   263  			}
   264  		}
   265  		else
   266  			fatal("dowidth %T", t);	// probably [...]T
   267  		break;
   268  
   269  	case TSTRUCT:
   270  		if(t->funarg)
   271  			fatal("dowidth fn struct %T", t);
   272  		w = widstruct(t, t, 0, 1);
   273  		break;
   274  
   275  	case TFUNC:
   276  		// make fake type to check later to
   277  		// trigger function argument computation.
   278  		t1 = typ(TFUNCARGS);
   279  		t1->type = t;
   280  		checkwidth(t1);
   281  
   282  		// width of func type is pointer
   283  		w = widthptr;
   284  		break;
   285  
   286  	case TFUNCARGS:
   287  		// function is 3 cated structures;
   288  		// compute their widths as side-effect.
   289  		t1 = t->type;
   290  		w = widstruct(t->type, *getthis(t1), 0, 0);
   291  		w = widstruct(t->type, *getinarg(t1), w, widthreg);
   292  		w = widstruct(t->type, *getoutarg(t1), w, widthreg);
   293  		t1->argwid = w;
   294  		if(w%widthreg)
   295  			warn("bad type %T %d\n", t1, w);
   296  		t->align = 1;
   297  		break;
   298  	}
   299  
   300  	if(widthptr == 4 && w != (int32)w)
   301  		yyerror("type %T too large", t);
   302  
   303  	t->width = w;
   304  	if(t->align == 0) {
   305  		if(w > 8 || (w&(w-1)) != 0)
   306  			fatal("invalid alignment for %T", t);
   307  		t->align = w;
   308  	}
   309  	lineno = lno;
   310  
   311  	if(defercalc == 1)
   312  		resumecheckwidth();
   313  	else
   314  		--defercalc;
   315  }
   316  
   317  /*
   318   * when a type's width should be known, we call checkwidth
   319   * to compute it.  during a declaration like
   320   *
   321   *	type T *struct { next T }
   322   *
   323   * it is necessary to defer the calculation of the struct width
   324   * until after T has been initialized to be a pointer to that struct.
   325   * similarly, during import processing structs may be used
   326   * before their definition.  in those situations, calling
   327   * defercheckwidth() stops width calculations until
   328   * resumecheckwidth() is called, at which point all the
   329   * checkwidths that were deferred are executed.
   330   * dowidth should only be called when the type's size
   331   * is needed immediately.  checkwidth makes sure the
   332   * size is evaluated eventually.
   333   */
   334  typedef struct TypeList TypeList;
   335  struct TypeList {
   336  	Type *t;
   337  	TypeList *next;
   338  };
   339  
   340  static TypeList *tlfree;
   341  static TypeList *tlq;
   342  
   343  void
   344  checkwidth(Type *t)
   345  {
   346  	TypeList *l;
   347  
   348  	if(t == T)
   349  		return;
   350  
   351  	// function arg structs should not be checked
   352  	// outside of the enclosing function.
   353  	if(t->funarg)
   354  		fatal("checkwidth %T", t);
   355  
   356  	if(!defercalc) {
   357  		dowidth(t);
   358  		return;
   359  	}
   360  	if(t->deferwidth)
   361  		return;
   362  	t->deferwidth = 1;
   363  
   364  	l = tlfree;
   365  	if(l != nil)
   366  		tlfree = l->next;
   367  	else
   368  		l = mal(sizeof *l);
   369  
   370  	l->t = t;
   371  	l->next = tlq;
   372  	tlq = l;
   373  }
   374  
   375  void
   376  defercheckwidth(void)
   377  {
   378  	// we get out of sync on syntax errors, so don't be pedantic.
   379  	if(defercalc && nerrors == 0)
   380  		fatal("defercheckwidth");
   381  	defercalc = 1;
   382  }
   383  
   384  void
   385  resumecheckwidth(void)
   386  {
   387  	TypeList *l;
   388  
   389  	if(!defercalc)
   390  		fatal("resumecheckwidth");
   391  	for(l = tlq; l != nil; l = tlq) {
   392  		l->t->deferwidth = 0;
   393  		tlq = l->next;
   394  		dowidth(l->t);
   395  		l->next = tlfree;
   396  		tlfree = l;
   397  	}
   398  	defercalc = 0;
   399  }
   400  
   401  void
   402  typeinit(void)
   403  {
   404  	int i, etype, sameas;
   405  	Type *t;
   406  	Sym *s, *s1;
   407  
   408  	if(widthptr == 0)
   409  		fatal("typeinit before betypeinit");
   410  
   411  	for(i=0; i<NTYPE; i++)
   412  		simtype[i] = i;
   413  
   414  	types[TPTR32] = typ(TPTR32);
   415  	dowidth(types[TPTR32]);
   416  
   417  	types[TPTR64] = typ(TPTR64);
   418  	dowidth(types[TPTR64]);
   419  	
   420  	t = typ(TUNSAFEPTR);
   421  	types[TUNSAFEPTR] = t;
   422  	t->sym = pkglookup("Pointer", unsafepkg);
   423  	t->sym->def = typenod(t);
   424  	
   425  	dowidth(types[TUNSAFEPTR]);
   426  
   427  	tptr = TPTR32;
   428  	if(widthptr == 8)
   429  		tptr = TPTR64;
   430  
   431  	for(i=TINT8; i<=TUINT64; i++)
   432  		isint[i] = 1;
   433  	isint[TINT] = 1;
   434  	isint[TUINT] = 1;
   435  	isint[TUINTPTR] = 1;
   436  
   437  	isfloat[TFLOAT32] = 1;
   438  	isfloat[TFLOAT64] = 1;
   439  
   440  	iscomplex[TCOMPLEX64] = 1;
   441  	iscomplex[TCOMPLEX128] = 1;
   442  
   443  	isptr[TPTR32] = 1;
   444  	isptr[TPTR64] = 1;
   445  
   446  	isforw[TFORW] = 1;
   447  
   448  	issigned[TINT] = 1;
   449  	issigned[TINT8] = 1;
   450  	issigned[TINT16] = 1;
   451  	issigned[TINT32] = 1;
   452  	issigned[TINT64] = 1;
   453  
   454  	/*
   455  	 * initialize okfor
   456  	 */
   457  	for(i=0; i<NTYPE; i++) {
   458  		if(isint[i] || i == TIDEAL) {
   459  			okforeq[i] = 1;
   460  			okforcmp[i] = 1;
   461  			okforarith[i] = 1;
   462  			okforadd[i] = 1;
   463  			okforand[i] = 1;
   464  			okforconst[i] = 1;
   465  			issimple[i] = 1;
   466  			minintval[i] = mal(sizeof(*minintval[i]));
   467  			maxintval[i] = mal(sizeof(*maxintval[i]));
   468  		}
   469  		if(isfloat[i]) {
   470  			okforeq[i] = 1;
   471  			okforcmp[i] = 1;
   472  			okforadd[i] = 1;
   473  			okforarith[i] = 1;
   474  			okforconst[i] = 1;
   475  			issimple[i] = 1;
   476  			minfltval[i] = mal(sizeof(*minfltval[i]));
   477  			maxfltval[i] = mal(sizeof(*maxfltval[i]));
   478  		}
   479  		if(iscomplex[i]) {
   480  			okforeq[i] = 1;
   481  			okforadd[i] = 1;
   482  			okforarith[i] = 1;
   483  			okforconst[i] = 1;
   484  			issimple[i] = 1;
   485  		}
   486  	}
   487  
   488  	issimple[TBOOL] = 1;
   489  
   490  	okforadd[TSTRING] = 1;
   491  
   492  	okforbool[TBOOL] = 1;
   493  
   494  	okforcap[TARRAY] = 1;
   495  	okforcap[TCHAN] = 1;
   496  
   497  	okforconst[TBOOL] = 1;
   498  	okforconst[TSTRING] = 1;
   499  
   500  	okforlen[TARRAY] = 1;
   501  	okforlen[TCHAN] = 1;
   502  	okforlen[TMAP] = 1;
   503  	okforlen[TSTRING] = 1;
   504  
   505  	okforeq[TPTR32] = 1;
   506  	okforeq[TPTR64] = 1;
   507  	okforeq[TUNSAFEPTR] = 1;
   508  	okforeq[TINTER] = 1;
   509  	okforeq[TCHAN] = 1;
   510  	okforeq[TSTRING] = 1;
   511  	okforeq[TBOOL] = 1;
   512  	okforeq[TMAP] = 1;	// nil only; refined in typecheck
   513  	okforeq[TFUNC] = 1;	// nil only; refined in typecheck
   514  	okforeq[TARRAY] = 1;	// nil slice only; refined in typecheck
   515  	okforeq[TSTRUCT] = 1;	// it's complicated; refined in typecheck
   516  
   517  	okforcmp[TSTRING] = 1;
   518  
   519  	for(i=0; i<nelem(okfor); i++)
   520  		okfor[i] = okfornone;
   521  
   522  	// binary
   523  	okfor[OADD] = okforadd;
   524  	okfor[OAND] = okforand;
   525  	okfor[OANDAND] = okforbool;
   526  	okfor[OANDNOT] = okforand;
   527  	okfor[ODIV] = okforarith;
   528  	okfor[OEQ] = okforeq;
   529  	okfor[OGE] = okforcmp;
   530  	okfor[OGT] = okforcmp;
   531  	okfor[OLE] = okforcmp;
   532  	okfor[OLT] = okforcmp;
   533  	okfor[OMOD] = okforand;
   534  	okfor[OMUL] = okforarith;
   535  	okfor[ONE] = okforeq;
   536  	okfor[OOR] = okforand;
   537  	okfor[OOROR] = okforbool;
   538  	okfor[OSUB] = okforarith;
   539  	okfor[OXOR] = okforand;
   540  	okfor[OLSH] = okforand;
   541  	okfor[ORSH] = okforand;
   542  
   543  	// unary
   544  	okfor[OCOM] = okforand;
   545  	okfor[OMINUS] = okforarith;
   546  	okfor[ONOT] = okforbool;
   547  	okfor[OPLUS] = okforarith;
   548  
   549  	// special
   550  	okfor[OCAP] = okforcap;
   551  	okfor[OLEN] = okforlen;
   552  
   553  	// comparison
   554  	iscmp[OLT] = 1;
   555  	iscmp[OGT] = 1;
   556  	iscmp[OGE] = 1;
   557  	iscmp[OLE] = 1;
   558  	iscmp[OEQ] = 1;
   559  	iscmp[ONE] = 1;
   560  
   561  	mpatofix(maxintval[TINT8], "0x7f");
   562  	mpatofix(minintval[TINT8], "-0x80");
   563  	mpatofix(maxintval[TINT16], "0x7fff");
   564  	mpatofix(minintval[TINT16], "-0x8000");
   565  	mpatofix(maxintval[TINT32], "0x7fffffff");
   566  	mpatofix(minintval[TINT32], "-0x80000000");
   567  	mpatofix(maxintval[TINT64], "0x7fffffffffffffff");
   568  	mpatofix(minintval[TINT64], "-0x8000000000000000");
   569  
   570  	mpatofix(maxintval[TUINT8], "0xff");
   571  	mpatofix(maxintval[TUINT16], "0xffff");
   572  	mpatofix(maxintval[TUINT32], "0xffffffff");
   573  	mpatofix(maxintval[TUINT64], "0xffffffffffffffff");
   574  
   575  	/* f is valid float if min < f < max.  (min and max are not themselves valid.) */
   576  	mpatoflt(maxfltval[TFLOAT32], "33554431p103");	/* 2^24-1 p (127-23) + 1/2 ulp*/
   577  	mpatoflt(minfltval[TFLOAT32], "-33554431p103");
   578  	mpatoflt(maxfltval[TFLOAT64], "18014398509481983p970");	/* 2^53-1 p (1023-52) + 1/2 ulp */
   579  	mpatoflt(minfltval[TFLOAT64], "-18014398509481983p970");
   580  
   581  	maxfltval[TCOMPLEX64] = maxfltval[TFLOAT32];
   582  	minfltval[TCOMPLEX64] = minfltval[TFLOAT32];
   583  	maxfltval[TCOMPLEX128] = maxfltval[TFLOAT64];
   584  	minfltval[TCOMPLEX128] = minfltval[TFLOAT64];
   585  
   586  	/* for walk to use in error messages */
   587  	types[TFUNC] = functype(N, nil, nil);
   588  
   589  	/* types used in front end */
   590  	// types[TNIL] got set early in lexinit
   591  	types[TIDEAL] = typ(TIDEAL);
   592  	types[TINTER] = typ(TINTER);
   593  
   594  	/* simple aliases */
   595  	simtype[TMAP] = tptr;
   596  	simtype[TCHAN] = tptr;
   597  	simtype[TFUNC] = tptr;
   598  	simtype[TUNSAFEPTR] = tptr;
   599  
   600  	/* pick up the backend typedefs */
   601  	for(i=0; typedefs[i].name; i++) {
   602  		s = lookup(typedefs[i].name);
   603  		s1 = pkglookup(typedefs[i].name, builtinpkg);
   604  
   605  		etype = typedefs[i].etype;
   606  		if(etype < 0 || etype >= nelem(types))
   607  			fatal("typeinit: %s bad etype", s->name);
   608  		sameas = typedefs[i].sameas;
   609  		if(sameas < 0 || sameas >= nelem(types))
   610  			fatal("typeinit: %s bad sameas", s->name);
   611  		simtype[etype] = sameas;
   612  		minfltval[etype] = minfltval[sameas];
   613  		maxfltval[etype] = maxfltval[sameas];
   614  		minintval[etype] = minintval[sameas];
   615  		maxintval[etype] = maxintval[sameas];
   616  
   617  		t = types[etype];
   618  		if(t != T)
   619  			fatal("typeinit: %s already defined", s->name);
   620  
   621  		t = typ(etype);
   622  		t->sym = s1;
   623  
   624  		dowidth(t);
   625  		types[etype] = t;
   626  		s1->def = typenod(t);
   627  	}
   628  
   629  	Array_array = rnd(0, widthptr);
   630  	Array_nel = rnd(Array_array+widthptr, widthint);
   631  	Array_cap = rnd(Array_nel+widthint, widthint);
   632  	sizeof_Array = rnd(Array_cap+widthint, widthptr);
   633  
   634  	// string is same as slice wo the cap
   635  	sizeof_String = rnd(Array_nel+widthint, widthptr);
   636  
   637  	dowidth(types[TSTRING]);
   638  	dowidth(idealstring);
   639  }
   640  
   641  /*
   642   * compute total size of f's in/out arguments.
   643   */
   644  int
   645  argsize(Type *t)
   646  {
   647  	Iter save;
   648  	Type *fp;
   649  	int64 w, x;
   650  
   651  	w = 0;
   652  
   653  	fp = structfirst(&save, getoutarg(t));
   654  	while(fp != T) {
   655  		x = fp->width + fp->type->width;
   656  		if(x > w)
   657  			w = x;
   658  		fp = structnext(&save);
   659  	}
   660  
   661  	fp = funcfirst(&save, t);
   662  	while(fp != T) {
   663  		x = fp->width + fp->type->width;
   664  		if(x > w)
   665  			w = x;
   666  		fp = funcnext(&save);
   667  	}
   668  
   669  	w = (w+widthptr-1) & ~(widthptr-1);
   670  	if((int)w != w)
   671  		fatal("argsize too big");
   672  	return w;
   673  }