github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/cmd/internal/gc/go.go (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 package gc 6 7 import ( 8 "bytes" 9 "cmd/internal/gc/big" 10 "cmd/internal/obj" 11 ) 12 13 // avoid <ctype.h> 14 15 // The parser's maximum stack size. 16 // We have to use a #define macro here since yacc 17 // or bison will check for its definition and use 18 // a potentially smaller value if it is undefined. 19 const ( 20 NHUNK = 50000 21 BUFSIZ = 8192 22 NSYMB = 500 23 NHASH = 1024 24 MAXALIGN = 7 25 UINF = 100 26 PRIME1 = 3 27 BADWIDTH = -1000000000 28 MaxStackVarSize = 10 * 1024 * 1024 29 ) 30 31 const ( 32 // These values are known by runtime. 33 // The MEMx and NOEQx values must run in parallel. See algtype. 34 AMEM = iota 35 AMEM0 36 AMEM8 37 AMEM16 38 AMEM32 39 AMEM64 40 AMEM128 41 ANOEQ 42 ANOEQ0 43 ANOEQ8 44 ANOEQ16 45 ANOEQ32 46 ANOEQ64 47 ANOEQ128 48 ASTRING 49 AINTER 50 ANILINTER 51 ASLICE 52 AFLOAT32 53 AFLOAT64 54 ACPLX64 55 ACPLX128 56 AUNK = 100 57 ) 58 59 const ( 60 // Maximum size in bits for Mpints before signalling 61 // overflow and also mantissa precision for Mpflts. 62 Mpprec = 512 63 // Turn on for constant arithmetic debugging output. 64 Mpdebug = false 65 ) 66 67 // Mpint represents an integer constant. 68 type Mpint struct { 69 Val big.Int 70 Ovf bool // set if Val overflowed compiler limit (sticky) 71 } 72 73 // Mpflt represents a floating-point constant. 74 type Mpflt struct { 75 Val big.Float 76 } 77 78 // Mpcplx represents a complex constant. 79 type Mpcplx struct { 80 Real Mpflt 81 Imag Mpflt 82 } 83 84 type Val struct { 85 Ctype int16 86 U struct { 87 Bval bool // bool value CTBOOL 88 Xval *Mpint // int CTINT, rune CTRUNE 89 Fval *Mpflt // float CTFLT 90 Cval *Mpcplx // float CTCPLX 91 Sval string // string CTSTR 92 } 93 } 94 95 type Pkg struct { 96 Name string // package name 97 Path string // string literal used in import statement 98 Pathsym *Sym 99 Prefix string // escaped path for use in symbol table 100 Imported uint8 // export data of this package was parsed 101 Exported int8 // import line written in export data 102 Direct int8 // imported directly 103 Safe bool // whether the package is marked as safe 104 Syms map[string]*Sym 105 } 106 107 type Sym struct { 108 Lexical uint16 109 Flags uint8 110 Link *Sym 111 Uniqgen uint32 112 Importdef *Pkg // where imported definition was found 113 Linkname string // link name 114 115 // saved and restored by dcopy 116 Pkg *Pkg 117 Name string // variable name 118 Def *Node // definition: ONAME OTYPE OPACK or OLITERAL 119 Label *Label // corresponding label (ephemeral) 120 Block int32 // blocknumber to catch redeclaration 121 Lastlineno int32 // last declaration for diagnostic 122 Origpkg *Pkg // original package for . import 123 Lsym *obj.LSym 124 Fsym *Sym // funcsym 125 } 126 127 type Type struct { 128 Etype uint8 129 Nointerface bool 130 Noalg uint8 131 Chan uint8 132 Trecur uint8 // to detect loops 133 Printed uint8 134 Embedded uint8 // TFIELD embedded type 135 Siggen uint8 136 Funarg uint8 // on TSTRUCT and TFIELD 137 Copyany uint8 138 Local bool // created in this file 139 Deferwidth uint8 140 Broke uint8 // broken type definition. 141 Isddd bool // TFIELD is ... argument 142 Align uint8 143 Haspointers uint8 // 0 unknown, 1 no, 2 yes 144 145 Nod *Node // canonical OTYPE node 146 Orig *Type // original type (type literal or predefined type) 147 Lineno int 148 149 // TFUNC 150 Thistuple int 151 Outtuple int 152 Intuple int 153 Outnamed uint8 154 155 Method *Type 156 Xmethod *Type 157 158 Sym *Sym 159 Vargen int32 // unique name for OTYPE/ONAME 160 161 Nname *Node 162 Argwid int64 163 164 // most nodes 165 Type *Type // actual type for TFIELD, element type for TARRAY, TCHAN, TMAP, TPTRxx 166 Width int64 // offset in TFIELD, width in all others 167 168 // TFIELD 169 Down *Type // next struct field, also key type in TMAP 170 Outer *Type // outer struct 171 Note *string // literal string annotation 172 173 // TARRAY 174 Bound int64 // negative is dynamic array 175 176 // TMAP 177 Bucket *Type // internal type representing a hash bucket 178 Hmap *Type // internal type representing a Hmap (map header object) 179 Hiter *Type // internal type representing hash iterator state 180 Map *Type // link from the above 3 internal types back to the map type. 181 182 Maplineno int32 // first use of TFORW as map key 183 Embedlineno int32 // first use of TFORW as embedded type 184 185 // for TFORW, where to copy the eventual value to 186 Copyto *NodeList 187 188 Lastfn *Node // for usefield 189 } 190 191 type Label struct { 192 Used uint8 193 Sym *Sym 194 Def *Node 195 Use *NodeList 196 Link *Label 197 198 // for use during gen 199 Gotopc *obj.Prog // pointer to unresolved gotos 200 Labelpc *obj.Prog // pointer to code 201 Breakpc *obj.Prog // pointer to code 202 Continpc *obj.Prog // pointer to code 203 } 204 205 type InitEntry struct { 206 Xoffset int64 // struct, array only 207 Expr *Node // bytes of run-time computed expressions 208 } 209 210 type InitPlan struct { 211 Lit int64 212 Zero int64 213 Expr int64 214 E []InitEntry 215 } 216 217 const ( 218 EscUnknown = iota 219 EscHeap 220 EscScope 221 EscNone 222 EscReturn 223 EscNever 224 EscBits = 3 225 EscMask = (1 << EscBits) - 1 226 EscContentEscapes = 1 << EscBits // value obtained by indirect of parameter escapes to some returned result 227 EscReturnBits = EscBits + 1 228 ) 229 230 const ( 231 SymExport = 1 << 0 // to be exported 232 SymPackage = 1 << 1 233 SymExported = 1 << 2 // already written out by export 234 SymUniq = 1 << 3 235 SymSiggen = 1 << 4 236 SymAsm = 1 << 5 237 SymAlgGen = 1 << 6 238 ) 239 240 var dclstack *Sym 241 242 type Iter struct { 243 Done int 244 Tfunc *Type 245 T *Type 246 } 247 248 const ( 249 Txxx = iota 250 251 TINT8 252 TUINT8 253 TINT16 254 TUINT16 255 TINT32 256 TUINT32 257 TINT64 258 TUINT64 259 TINT 260 TUINT 261 TUINTPTR 262 263 TCOMPLEX64 264 TCOMPLEX128 265 266 TFLOAT32 267 TFLOAT64 268 269 TBOOL 270 271 TPTR32 272 TPTR64 273 274 TFUNC 275 TARRAY 276 T_old_DARRAY 277 TSTRUCT 278 TCHAN 279 TMAP 280 TINTER 281 TFORW 282 TFIELD 283 TANY 284 TSTRING 285 TUNSAFEPTR 286 287 // pseudo-types for literals 288 TIDEAL 289 TNIL 290 TBLANK 291 292 // pseudo-type for frame layout 293 TFUNCARGS 294 TCHANARGS 295 TINTERMETH 296 297 NTYPE 298 ) 299 300 const ( 301 CTxxx = iota 302 303 CTINT 304 CTRUNE 305 CTFLT 306 CTCPLX 307 CTSTR 308 CTBOOL 309 CTNIL 310 ) 311 312 const ( 313 /* types of channel */ 314 /* must match ../../pkg/nreflect/type.go:/Chandir */ 315 Cxxx = 0 316 Crecv = 1 << 0 317 Csend = 1 << 1 318 Cboth = Crecv | Csend 319 ) 320 321 // declaration context 322 const ( 323 Pxxx = uint8(iota) 324 PEXTERN // global variable 325 PAUTO // local variables 326 PPARAM // input arguments 327 PPARAMOUT // output results 328 PPARAMREF // closure variable reference 329 PFUNC // global function 330 331 PDISCARD // discard during parse of duplicate import 332 333 PHEAP = uint8(1 << 7) // an extra bit to identify an escaped variable 334 ) 335 336 const ( 337 Etop = 1 << 1 // evaluated at statement level 338 Erv = 1 << 2 // evaluated in value context 339 Etype = 1 << 3 340 Ecall = 1 << 4 // call-only expressions are ok 341 Efnstruct = 1 << 5 // multivalue function returns are ok 342 Eiota = 1 << 6 // iota is ok 343 Easgn = 1 << 7 // assigning to expression 344 Eindir = 1 << 8 // indirecting through expression 345 Eaddr = 1 << 9 // taking address of expression 346 Eproc = 1 << 10 // inside a go statement 347 Ecomplit = 1 << 11 // type in composite literal 348 ) 349 350 type Typedef struct { 351 Name string 352 Etype int 353 Sameas int 354 } 355 356 type Sig struct { 357 name string 358 pkg *Pkg 359 isym *Sym 360 tsym *Sym 361 type_ *Type 362 mtype *Type 363 offset int32 364 link *Sig 365 } 366 367 type Io struct { 368 infile string 369 bin *obj.Biobuf 370 nlsemi int 371 eofnl int 372 last int 373 peekc int 374 peekc1 int // second peekc for ... 375 cp string // used for content when bin==nil 376 importsafe bool 377 } 378 379 type Dlist struct { 380 field *Type 381 } 382 383 type Idir struct { 384 link *Idir 385 dir string 386 } 387 388 /* 389 * argument passing to/from 390 * smagic and umagic 391 */ 392 type Magic struct { 393 W int // input for both - width 394 S int // output for both - shift 395 Bad int // output for both - unexpected failure 396 397 // magic multiplier for signed literal divisors 398 Sd int64 // input - literal divisor 399 Sm int64 // output - multiplier 400 401 // magic multiplier for unsigned literal divisors 402 Ud uint64 // input - literal divisor 403 Um uint64 // output - multiplier 404 Ua int // output - adder 405 } 406 407 /* 408 * note this is the runtime representation 409 * of the compilers arrays. 410 * 411 * typedef struct 412 * { // must not move anything 413 * uchar array[8]; // pointer to data 414 * uchar nel[4]; // number of elements 415 * uchar cap[4]; // allocated number of elements 416 * } Array; 417 */ 418 var Array_array int // runtime offsetof(Array,array) - same for String 419 420 var Array_nel int // runtime offsetof(Array,nel) - same for String 421 422 var Array_cap int // runtime offsetof(Array,cap) 423 424 var sizeof_Array int // runtime sizeof(Array) 425 426 /* 427 * note this is the runtime representation 428 * of the compilers strings. 429 * 430 * typedef struct 431 * { // must not move anything 432 * uchar array[8]; // pointer to data 433 * uchar nel[4]; // number of elements 434 * } String; 435 */ 436 var sizeof_String int // runtime sizeof(String) 437 438 var dotlist [10]Dlist // size is max depth of embeddeds 439 440 var curio Io 441 442 var pushedio Io 443 444 var lexlineno int32 445 446 var lineno int32 447 448 var prevlineno int32 449 450 var pragcgobuf string 451 452 var infile string 453 454 var outfile string 455 456 var bout *obj.Biobuf 457 458 var nerrors int 459 460 var nsavederrors int 461 462 var nsyntaxerrors int 463 464 var decldepth int 465 466 var safemode int 467 468 var nolocalimports int 469 470 var lexbuf bytes.Buffer 471 var strbuf bytes.Buffer 472 473 var litbuf string 474 475 var Debug [256]int 476 477 var debugstr string 478 479 var Debug_checknil int 480 var Debug_typeassert int 481 482 var importmyname *Sym // my name for package 483 484 var localpkg *Pkg // package being compiled 485 486 var importpkg *Pkg // package being imported 487 488 var structpkg *Pkg // package that declared struct, during import 489 490 var builtinpkg *Pkg // fake package for builtins 491 492 var gostringpkg *Pkg // fake pkg for Go strings 493 494 var itabpkg *Pkg // fake pkg for itab cache 495 496 var Runtimepkg *Pkg // package runtime 497 498 var racepkg *Pkg // package runtime/race 499 500 var typepkg *Pkg // fake package for runtime type info (headers) 501 502 var typelinkpkg *Pkg // fake package for runtime type info (data) 503 504 var weaktypepkg *Pkg // weak references to runtime type info 505 506 var unsafepkg *Pkg // package unsafe 507 508 var trackpkg *Pkg // fake package for field tracking 509 510 var Tptr int // either TPTR32 or TPTR64 511 512 var myimportpath string 513 514 var idirs *Idir 515 516 var localimport string 517 518 var asmhdr string 519 520 var Types [NTYPE]*Type 521 522 var idealstring *Type 523 524 var idealbool *Type 525 526 var bytetype *Type 527 528 var runetype *Type 529 530 var errortype *Type 531 532 var Simtype [NTYPE]uint8 533 534 var ( 535 Isptr [NTYPE]bool 536 isforw [NTYPE]bool 537 Isint [NTYPE]bool 538 Isfloat [NTYPE]bool 539 Iscomplex [NTYPE]bool 540 Issigned [NTYPE]bool 541 issimple [NTYPE]bool 542 ) 543 544 var ( 545 okforeq [NTYPE]bool 546 okforadd [NTYPE]bool 547 okforand [NTYPE]bool 548 okfornone [NTYPE]bool 549 okforcmp [NTYPE]bool 550 okforbool [NTYPE]bool 551 okforcap [NTYPE]bool 552 okforlen [NTYPE]bool 553 okforarith [NTYPE]bool 554 okforconst [NTYPE]bool 555 ) 556 557 var ( 558 okfor [OEND][]bool 559 iscmp [OEND]bool 560 ) 561 562 var Minintval [NTYPE]*Mpint 563 564 var Maxintval [NTYPE]*Mpint 565 566 var minfltval [NTYPE]*Mpflt 567 568 var maxfltval [NTYPE]*Mpflt 569 570 var xtop *NodeList 571 572 var externdcl *NodeList 573 574 var exportlist *NodeList 575 576 var importlist *NodeList // imported functions and methods with inlinable bodies 577 578 var funcsyms *NodeList 579 580 var dclcontext uint8 // PEXTERN/PAUTO 581 582 var incannedimport int 583 584 var statuniqgen int // name generator for static temps 585 586 var loophack int 587 588 var iota_ int32 589 590 var lastconst *NodeList 591 592 var lasttype *Node 593 594 var Maxarg int64 595 596 var Stksize int64 // stack size for current frame 597 598 var stkptrsize int64 // prefix of stack containing pointers 599 600 var blockgen int32 // max block number 601 602 var block int32 // current block number 603 604 var Hasdefer int // flag that curfn has defer statetment 605 606 var Curfn *Node 607 608 var Widthptr int 609 610 var Widthint int 611 612 var Widthreg int 613 614 var typesw *Node 615 616 var nblank *Node 617 618 var hunk string 619 620 var nhunk int32 621 622 var thunk int32 623 624 var Funcdepth int32 625 626 var typecheckok int 627 628 var compiling_runtime int 629 630 var compiling_wrappers int 631 632 var inl_nonlocal int 633 634 var use_writebarrier int 635 636 var pure_go int 637 638 var flag_installsuffix string 639 640 var flag_race int 641 642 var flag_largemodel int 643 644 var noescape bool 645 646 var nosplit bool 647 648 var nowritebarrier bool 649 650 var debuglive int 651 652 var Ctxt *obj.Link 653 654 var nointerface bool 655 656 var writearchive int 657 658 var bstdout obj.Biobuf 659 660 var Nacl bool 661 662 var continpc *obj.Prog 663 664 var breakpc *obj.Prog 665 666 var Pc *obj.Prog 667 668 var nodfp *Node 669 670 var Disable_checknil int 671 672 var zerosize int64 673 674 type Flow struct { 675 Prog *obj.Prog // actual instruction 676 P1 *Flow // predecessors of this instruction: p1, 677 P2 *Flow // and then p2 linked though p2link. 678 P2link *Flow 679 S1 *Flow // successors of this instruction (at most two: s1 and s2). 680 S2 *Flow 681 Link *Flow // next instruction in function code 682 683 Active int32 // usable by client 684 685 Id int32 // sequence number in flow graph 686 Rpo int32 // reverse post ordering 687 Loop uint16 // x5 for every loop 688 Refset uint8 // diagnostic generated 689 690 Data interface{} // for use by client 691 } 692 693 type Graph struct { 694 Start *Flow 695 Num int 696 697 // After calling flowrpo, rpo lists the flow nodes in reverse postorder, 698 // and each non-dead Flow node f has g->rpo[f->rpo] == f. 699 Rpo []*Flow 700 } 701 702 /* 703 * interface to back end 704 */ 705 706 const ( 707 // Pseudo-op, like TEXT, GLOBL, TYPE, PCDATA, FUNCDATA. 708 Pseudo = 1 << 1 709 710 // There's nothing to say about the instruction, 711 // but it's still okay to see. 712 OK = 1 << 2 713 714 // Size of right-side write, or right-side read if no write. 715 SizeB = 1 << 3 716 SizeW = 1 << 4 717 SizeL = 1 << 5 718 SizeQ = 1 << 6 719 SizeF = 1 << 7 720 SizeD = 1 << 8 721 722 // Left side (Prog.from): address taken, read, write. 723 LeftAddr = 1 << 9 724 LeftRead = 1 << 10 725 LeftWrite = 1 << 11 726 727 // Register in middle (Prog.reg); only ever read. (arm, ppc64) 728 RegRead = 1 << 12 729 CanRegRead = 1 << 13 730 731 // Right side (Prog.to): address taken, read, write. 732 RightAddr = 1 << 14 733 RightRead = 1 << 15 734 RightWrite = 1 << 16 735 736 // Instruction kinds 737 Move = 1 << 17 // straight move 738 Conv = 1 << 18 // size conversion 739 Cjmp = 1 << 19 // conditional jump 740 Break = 1 << 20 // breaks control flow (no fallthrough) 741 Call = 1 << 21 // function call 742 Jump = 1 << 22 // jump 743 Skip = 1 << 23 // data instruction 744 745 // Set, use, or kill of carry bit. 746 // Kill means we never look at the carry bit after this kind of instruction. 747 SetCarry = 1 << 24 748 UseCarry = 1 << 25 749 KillCarry = 1 << 26 750 751 // Special cases for register use. (amd64, 386) 752 ShiftCX = 1 << 27 // possible shift by CX 753 ImulAXDX = 1 << 28 // possible multiply into DX:AX 754 755 // Instruction updates whichever of from/to is type D_OREG. (ppc64) 756 PostInc = 1 << 29 757 ) 758 759 type Arch struct { 760 Thechar int 761 Thestring string 762 Thelinkarch *obj.LinkArch 763 Typedefs []Typedef 764 REGSP int 765 REGCTXT int 766 REGCALLX int // BX 767 REGCALLX2 int // AX 768 REGRETURN int // AX 769 REGMIN int 770 REGMAX int 771 REGZERO int // architectural zero register, if available 772 FREGMIN int 773 FREGMAX int 774 MAXWIDTH int64 775 ReservedRegs []int 776 777 AddIndex func(*Node, int64, *Node) bool // optional 778 Betypeinit func() 779 Bgen_float func(*Node, int, int, *obj.Prog) // optional 780 Cgen64 func(*Node, *Node) // only on 32-bit systems 781 Cgenindex func(*Node, *Node, bool) *obj.Prog 782 Cgen_bmul func(int, *Node, *Node, *Node) bool 783 Cgen_float func(*Node, *Node) // optional 784 Cgen_hmul func(*Node, *Node, *Node) 785 Cgen_shift func(int, bool, *Node, *Node, *Node) 786 Clearfat func(*Node) 787 Cmp64 func(*Node, *Node, int, int, *obj.Prog) // only on 32-bit systems 788 Defframe func(*obj.Prog) 789 Dodiv func(int, *Node, *Node, *Node) 790 Excise func(*Flow) 791 Expandchecks func(*obj.Prog) 792 Getg func(*Node) 793 Gins func(int, *Node, *Node) *obj.Prog 794 Ginscon func(int, int64, *Node) 795 Ginsnop func() 796 Gmove func(*Node, *Node) 797 Igenindex func(*Node, *Node, bool) *obj.Prog 798 Linkarchinit func() 799 Peep func(*obj.Prog) 800 Proginfo func(*obj.Prog) // fills in Prog.Info 801 Regtyp func(*obj.Addr) bool 802 Sameaddr func(*obj.Addr, *obj.Addr) bool 803 Smallindir func(*obj.Addr, *obj.Addr) bool 804 Stackaddr func(*obj.Addr) bool 805 Stackcopy func(*Node, *Node, int64, int64, int64) 806 Sudoaddable func(int, *Node, *obj.Addr) bool 807 Sudoclean func() 808 Excludedregs func() uint64 809 RtoB func(int) uint64 810 FtoB func(int) uint64 811 BtoR func(uint64) int 812 BtoF func(uint64) int 813 Optoas func(int, *Type) int 814 Doregbits func(int) uint64 815 Regnames func(*int) []string 816 Use387 bool // should 8g use 387 FP instructions instead of sse2. 817 } 818 819 var pcloc int32 820 821 var Thearch Arch 822 823 var Newproc *Node 824 825 var Deferproc *Node 826 827 var Deferreturn *Node 828 829 var Panicindex *Node 830 831 var panicslice *Node 832 833 var throwreturn *Node