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