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