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