github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/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 488 var litbuf string 489 490 var Debug [256]int 491 492 var debugstr string 493 494 var Debug_checknil int 495 var Debug_typeassert int 496 497 var importmyname *Sym // my name for package 498 499 var localpkg *Pkg // package being compiled 500 501 var importpkg *Pkg // package being imported 502 503 var structpkg *Pkg // package that declared struct, during import 504 505 var builtinpkg *Pkg // fake package for builtins 506 507 var gostringpkg *Pkg // fake pkg for Go strings 508 509 var itabpkg *Pkg // fake pkg for itab cache 510 511 var Runtimepkg *Pkg // package runtime 512 513 var racepkg *Pkg // package runtime/race 514 515 var msanpkg *Pkg // package runtime/msan 516 517 var typepkg *Pkg // fake package for runtime type info (headers) 518 519 var typelinkpkg *Pkg // fake package for runtime type info (data) 520 521 var weaktypepkg *Pkg // weak references to runtime type info 522 523 var unsafepkg *Pkg // package unsafe 524 525 var trackpkg *Pkg // fake package for field tracking 526 527 var Tptr EType // either TPTR32 or TPTR64 528 529 var myimportpath string 530 531 var idirs *Idir 532 533 var localimport string 534 535 var asmhdr string 536 537 var Types [NTYPE]*Type 538 539 var idealstring *Type 540 541 var idealbool *Type 542 543 var bytetype *Type 544 545 var runetype *Type 546 547 var errortype *Type 548 549 var Simtype [NTYPE]EType 550 551 var ( 552 Isptr [NTYPE]bool 553 isforw [NTYPE]bool 554 Isint [NTYPE]bool 555 Isfloat [NTYPE]bool 556 Iscomplex [NTYPE]bool 557 Issigned [NTYPE]bool 558 issimple [NTYPE]bool 559 ) 560 561 var ( 562 okforeq [NTYPE]bool 563 okforadd [NTYPE]bool 564 okforand [NTYPE]bool 565 okfornone [NTYPE]bool 566 okforcmp [NTYPE]bool 567 okforbool [NTYPE]bool 568 okforcap [NTYPE]bool 569 okforlen [NTYPE]bool 570 okforarith [NTYPE]bool 571 okforconst [NTYPE]bool 572 ) 573 574 var ( 575 okfor [OEND][]bool 576 iscmp [OEND]bool 577 ) 578 579 var Minintval [NTYPE]*Mpint 580 581 var Maxintval [NTYPE]*Mpint 582 583 var minfltval [NTYPE]*Mpflt 584 585 var maxfltval [NTYPE]*Mpflt 586 587 var xtop *NodeList 588 589 var externdcl []*Node 590 591 var exportlist []*Node 592 593 var importlist []*Node // imported functions and methods with inlinable bodies 594 595 var funcsyms []*Node 596 597 var dclcontext Class // PEXTERN/PAUTO 598 599 var incannedimport int 600 601 var statuniqgen int // name generator for static temps 602 603 var loophack bool 604 605 var iota_ int32 606 607 var lastconst *NodeList 608 609 var lasttype *Node 610 611 var Maxarg int64 612 613 var Stksize int64 // stack size for current frame 614 615 var stkptrsize int64 // prefix of stack containing pointers 616 617 var blockgen int32 // max block number 618 619 var block int32 // current block number 620 621 var hasdefer bool // flag that curfn has defer statement 622 623 var Curfn *Node 624 625 var Widthptr int 626 627 var Widthint int 628 629 var Widthreg int 630 631 var typesw *Node 632 633 var nblank *Node 634 635 var Funcdepth int32 636 637 var typecheckok bool 638 639 var compiling_runtime int 640 641 var compiling_wrappers int 642 643 var use_writebarrier int 644 645 var pure_go int 646 647 var flag_installsuffix string 648 649 var flag_race int 650 651 var flag_msan int 652 653 var flag_largemodel int 654 655 // Whether we are adding any sort of code instrumentation, such as 656 // when the race detector is enabled. 657 var instrumenting bool 658 659 // Pending annotations for next func declaration. 660 var ( 661 noescape bool 662 noinline bool 663 norace bool 664 nosplit bool 665 nowritebarrier bool 666 systemstack bool 667 ) 668 669 var debuglive int 670 671 var Ctxt *obj.Link 672 673 var nointerface bool 674 675 var writearchive int 676 677 var bstdout obj.Biobuf 678 679 var Nacl bool 680 681 var continpc *obj.Prog 682 683 var breakpc *obj.Prog 684 685 var Pc *obj.Prog 686 687 var nodfp *Node 688 689 var Disable_checknil int 690 691 type Flow struct { 692 Prog *obj.Prog // actual instruction 693 P1 *Flow // predecessors of this instruction: p1, 694 P2 *Flow // and then p2 linked though p2link. 695 P2link *Flow 696 S1 *Flow // successors of this instruction (at most two: s1 and s2). 697 S2 *Flow 698 Link *Flow // next instruction in function code 699 700 Active int32 // usable by client 701 702 Id int32 // sequence number in flow graph 703 Rpo int32 // reverse post ordering 704 Loop uint16 // x5 for every loop 705 Refset bool // diagnostic generated 706 707 Data interface{} // for use by client 708 } 709 710 type Graph struct { 711 Start *Flow 712 Num int 713 714 // After calling flowrpo, rpo lists the flow nodes in reverse postorder, 715 // and each non-dead Flow node f has g->rpo[f->rpo] == f. 716 Rpo []*Flow 717 } 718 719 // interface to back end 720 721 const ( 722 // Pseudo-op, like TEXT, GLOBL, TYPE, PCDATA, FUNCDATA. 723 Pseudo = 1 << 1 724 725 // There's nothing to say about the instruction, 726 // but it's still okay to see. 727 OK = 1 << 2 728 729 // Size of right-side write, or right-side read if no write. 730 SizeB = 1 << 3 731 SizeW = 1 << 4 732 SizeL = 1 << 5 733 SizeQ = 1 << 6 734 SizeF = 1 << 7 735 SizeD = 1 << 8 736 737 // Left side (Prog.from): address taken, read, write. 738 LeftAddr = 1 << 9 739 LeftRead = 1 << 10 740 LeftWrite = 1 << 11 741 742 // Register in middle (Prog.reg); only ever read. (arm, ppc64) 743 RegRead = 1 << 12 744 CanRegRead = 1 << 13 745 746 // Right side (Prog.to): address taken, read, write. 747 RightAddr = 1 << 14 748 RightRead = 1 << 15 749 RightWrite = 1 << 16 750 751 // Instruction kinds 752 Move = 1 << 17 // straight move 753 Conv = 1 << 18 // size conversion 754 Cjmp = 1 << 19 // conditional jump 755 Break = 1 << 20 // breaks control flow (no fallthrough) 756 Call = 1 << 21 // function call 757 Jump = 1 << 22 // jump 758 Skip = 1 << 23 // data instruction 759 760 // Set, use, or kill of carry bit. 761 // Kill means we never look at the carry bit after this kind of instruction. 762 SetCarry = 1 << 24 763 UseCarry = 1 << 25 764 KillCarry = 1 << 26 765 766 // Special cases for register use. (amd64, 386) 767 ShiftCX = 1 << 27 // possible shift by CX 768 ImulAXDX = 1 << 28 // possible multiply into DX:AX 769 770 // Instruction updates whichever of from/to is type D_OREG. (ppc64) 771 PostInc = 1 << 29 772 ) 773 774 type Arch struct { 775 Thechar int 776 Thestring string 777 Thelinkarch *obj.LinkArch 778 Typedefs []Typedef 779 REGSP int 780 REGCTXT int 781 REGCALLX int // BX 782 REGCALLX2 int // AX 783 REGRETURN int // AX 784 REGMIN int 785 REGMAX int 786 REGZERO int // architectural zero register, if available 787 FREGMIN int 788 FREGMAX int 789 MAXWIDTH int64 790 ReservedRegs []int 791 792 AddIndex func(*Node, int64, *Node) bool // optional 793 Betypeinit func() 794 Bgen_float func(*Node, bool, int, *obj.Prog) // optional 795 Cgen64 func(*Node, *Node) // only on 32-bit systems 796 Cgenindex func(*Node, *Node, bool) *obj.Prog 797 Cgen_bmul func(Op, *Node, *Node, *Node) bool 798 Cgen_float func(*Node, *Node) // optional 799 Cgen_hmul func(*Node, *Node, *Node) 800 Cgen_shift func(Op, bool, *Node, *Node, *Node) 801 Clearfat func(*Node) 802 Cmp64 func(*Node, *Node, Op, int, *obj.Prog) // only on 32-bit systems 803 Defframe func(*obj.Prog) 804 Dodiv func(Op, *Node, *Node, *Node) 805 Excise func(*Flow) 806 Expandchecks func(*obj.Prog) 807 Getg func(*Node) 808 Gins func(int, *Node, *Node) *obj.Prog 809 810 // Ginscmp generates code comparing n1 to n2 and jumping away if op is satisfied. 811 // The returned prog should be Patch'ed with the jump target. 812 // If op is not satisfied, code falls through to the next emitted instruction. 813 // Likely is the branch prediction hint: +1 for likely, -1 for unlikely, 0 for no opinion. 814 // 815 // Ginscmp must be able to handle all kinds of arguments for n1 and n2, 816 // not just simple registers, although it can assume that there are no 817 // function calls needed during the evaluation, and on 32-bit systems 818 // the values are guaranteed not to be 64-bit values, so no in-memory 819 // temporaries are necessary. 820 Ginscmp func(op Op, t *Type, n1, n2 *Node, likely int) *obj.Prog 821 822 // Ginsboolval inserts instructions to convert the result 823 // of a just-completed comparison to a boolean value. 824 // The first argument is the conditional jump instruction 825 // corresponding to the desired value. 826 // The second argument is the destination. 827 // If not present, Ginsboolval will be emulated with jumps. 828 Ginsboolval func(int, *Node) 829 830 Ginscon func(int, int64, *Node) 831 Ginsnop func() 832 Gmove func(*Node, *Node) 833 Igenindex func(*Node, *Node, bool) *obj.Prog 834 Linkarchinit func() 835 Peep func(*obj.Prog) 836 Proginfo func(*obj.Prog) // fills in Prog.Info 837 Regtyp func(*obj.Addr) bool 838 Sameaddr func(*obj.Addr, *obj.Addr) bool 839 Smallindir func(*obj.Addr, *obj.Addr) bool 840 Stackaddr func(*obj.Addr) bool 841 Blockcopy func(*Node, *Node, int64, int64, int64) 842 Sudoaddable func(int, *Node, *obj.Addr) bool 843 Sudoclean func() 844 Excludedregs func() uint64 845 RtoB func(int) uint64 846 FtoB func(int) uint64 847 BtoR func(uint64) int 848 BtoF func(uint64) int 849 Optoas func(Op, *Type) int 850 Doregbits func(int) uint64 851 Regnames func(*int) []string 852 Use387 bool // should 8g use 387 FP instructions instead of sse2. 853 } 854 855 var pcloc int32 856 857 var Thearch Arch 858 859 var Newproc *Node 860 861 var Deferproc *Node 862 863 var Deferreturn *Node 864 865 var Panicindex *Node 866 867 var panicslice *Node 868 869 var throwreturn *Node