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 Fatal("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 uint8 // export data of this package was parsed 129 Exported int8 // import line written in export data 130 Direct int8 // 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 uint8 159 Chan uint8 160 Trecur uint8 // to detect loops 161 Printed uint8 162 Embedded uint8 // TFIELD embedded type 163 Siggen uint8 164 Funarg uint8 // on TSTRUCT and TFIELD 165 Copyany uint8 166 Local bool // created in this file 167 Deferwidth uint8 168 Broke uint8 // broken type definition. 169 Isddd bool // TFIELD is ... argument 170 Align uint8 171 Haspointers uint8 // 0 unknown, 1 no, 2 yes 172 173 Nod *Node // canonical OTYPE node 174 Orig *Type // original type (type literal or predefined type) 175 Lineno int 176 177 // TFUNC 178 Thistuple int 179 Outtuple int 180 Intuple int 181 Outnamed uint8 182 183 Method *Type 184 Xmethod *Type 185 186 Sym *Sym 187 Vargen int32 // unique name for OTYPE/ONAME 188 189 Nname *Node 190 Argwid int64 191 192 // most nodes 193 Type *Type // actual type for TFIELD, element type for TARRAY, TCHAN, TMAP, TPTRxx 194 Width int64 // offset in TFIELD, width in all others 195 196 // TFIELD 197 Down *Type // next struct field, also key type in TMAP 198 Outer *Type // outer struct 199 Note *string // literal string annotation 200 201 // TARRAY 202 Bound int64 // negative is dynamic array 203 204 // TMAP 205 Bucket *Type // internal type representing a hash bucket 206 Hmap *Type // internal type representing a Hmap (map header object) 207 Hiter *Type // internal type representing hash iterator state 208 Map *Type // link from the above 3 internal types back to the map type. 209 210 Maplineno int32 // first use of TFORW as map key 211 Embedlineno int32 // first use of TFORW as embedded type 212 213 // for TFORW, where to copy the eventual value to 214 Copyto *NodeList 215 216 Lastfn *Node // for usefield 217 } 218 219 type Label struct { 220 Used uint8 221 Sym *Sym 222 Def *Node 223 Use *NodeList 224 Link *Label 225 226 // for use during gen 227 Gotopc *obj.Prog // pointer to unresolved gotos 228 Labelpc *obj.Prog // pointer to code 229 Breakpc *obj.Prog // pointer to code 230 Continpc *obj.Prog // pointer to code 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 link *Sig 380 } 381 382 type Io struct { 383 infile string 384 bin *obj.Biobuf 385 nlsemi int 386 eofnl int 387 last int 388 peekc int 389 peekc1 int // second peekc for ... 390 cp string // used for content when bin==nil 391 importsafe bool 392 } 393 394 type Dlist struct { 395 field *Type 396 } 397 398 type Idir struct { 399 link *Idir 400 dir string 401 } 402 403 /* 404 * argument passing to/from 405 * smagic and umagic 406 */ 407 type Magic struct { 408 W int // input for both - width 409 S int // output for both - shift 410 Bad int // output for both - unexpected failure 411 412 // magic multiplier for signed literal divisors 413 Sd int64 // input - literal divisor 414 Sm int64 // output - multiplier 415 416 // magic multiplier for unsigned literal divisors 417 Ud uint64 // input - literal divisor 418 Um uint64 // output - multiplier 419 Ua int // output - adder 420 } 421 422 /* 423 * note this is the runtime representation 424 * of the compilers arrays. 425 * 426 * typedef struct 427 * { // must not move anything 428 * uchar array[8]; // pointer to data 429 * uchar nel[4]; // number of elements 430 * uchar cap[4]; // allocated number of elements 431 * } Array; 432 */ 433 var Array_array int // runtime offsetof(Array,array) - same for String 434 435 var Array_nel int // runtime offsetof(Array,nel) - same for String 436 437 var Array_cap int // runtime offsetof(Array,cap) 438 439 var sizeof_Array int // runtime sizeof(Array) 440 441 /* 442 * note this is the runtime representation 443 * of the compilers strings. 444 * 445 * typedef struct 446 * { // must not move anything 447 * uchar array[8]; // pointer to data 448 * uchar nel[4]; // number of elements 449 * } String; 450 */ 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 typepkg *Pkg // fake package for runtime type info (headers) 516 517 var typelinkpkg *Pkg // fake package for runtime type info (data) 518 519 var weaktypepkg *Pkg // weak references to runtime type info 520 521 var unsafepkg *Pkg // package unsafe 522 523 var trackpkg *Pkg // fake package for field tracking 524 525 var Tptr int // either TPTR32 or TPTR64 526 527 var myimportpath string 528 529 var idirs *Idir 530 531 var localimport string 532 533 var asmhdr string 534 535 var Types [NTYPE]*Type 536 537 var idealstring *Type 538 539 var idealbool *Type 540 541 var bytetype *Type 542 543 var runetype *Type 544 545 var errortype *Type 546 547 var Simtype [NTYPE]uint8 548 549 var ( 550 Isptr [NTYPE]bool 551 isforw [NTYPE]bool 552 Isint [NTYPE]bool 553 Isfloat [NTYPE]bool 554 Iscomplex [NTYPE]bool 555 Issigned [NTYPE]bool 556 issimple [NTYPE]bool 557 ) 558 559 var ( 560 okforeq [NTYPE]bool 561 okforadd [NTYPE]bool 562 okforand [NTYPE]bool 563 okfornone [NTYPE]bool 564 okforcmp [NTYPE]bool 565 okforbool [NTYPE]bool 566 okforcap [NTYPE]bool 567 okforlen [NTYPE]bool 568 okforarith [NTYPE]bool 569 okforconst [NTYPE]bool 570 ) 571 572 var ( 573 okfor [OEND][]bool 574 iscmp [OEND]bool 575 ) 576 577 var Minintval [NTYPE]*Mpint 578 579 var Maxintval [NTYPE]*Mpint 580 581 var minfltval [NTYPE]*Mpflt 582 583 var maxfltval [NTYPE]*Mpflt 584 585 var xtop *NodeList 586 587 var externdcl *NodeList 588 589 var exportlist *NodeList 590 591 var importlist *NodeList // imported functions and methods with inlinable bodies 592 593 var funcsyms *NodeList 594 595 var dclcontext uint8 // PEXTERN/PAUTO 596 597 var incannedimport int 598 599 var statuniqgen int // name generator for static temps 600 601 var loophack int 602 603 var iota_ int32 604 605 var lastconst *NodeList 606 607 var lasttype *Node 608 609 var Maxarg int64 610 611 var Stksize int64 // stack size for current frame 612 613 var stkptrsize int64 // prefix of stack containing pointers 614 615 var blockgen int32 // max block number 616 617 var block int32 // current block number 618 619 var Hasdefer int // flag that curfn has defer statetment 620 621 var Curfn *Node 622 623 var Widthptr int 624 625 var Widthint int 626 627 var Widthreg int 628 629 var typesw *Node 630 631 var nblank *Node 632 633 var hunk string 634 635 var nhunk int32 636 637 var thunk int32 638 639 var Funcdepth int32 640 641 var typecheckok int 642 643 var compiling_runtime int 644 645 var compiling_wrappers int 646 647 var use_writebarrier int 648 649 var pure_go int 650 651 var flag_installsuffix string 652 653 var flag_race int 654 655 var flag_largemodel int 656 657 // Pending annotations for next func declaration. 658 var ( 659 noescape bool 660 nosplit bool 661 nowritebarrier bool 662 systemstack bool 663 norace bool 664 ) 665 666 var debuglive int 667 668 var Ctxt *obj.Link 669 670 var nointerface bool 671 672 var writearchive int 673 674 var bstdout obj.Biobuf 675 676 var Nacl bool 677 678 var continpc *obj.Prog 679 680 var breakpc *obj.Prog 681 682 var Pc *obj.Prog 683 684 var nodfp *Node 685 686 var Disable_checknil int 687 688 var zerosize int64 689 690 type Flow struct { 691 Prog *obj.Prog // actual instruction 692 P1 *Flow // predecessors of this instruction: p1, 693 P2 *Flow // and then p2 linked though p2link. 694 P2link *Flow 695 S1 *Flow // successors of this instruction (at most two: s1 and s2). 696 S2 *Flow 697 Link *Flow // next instruction in function code 698 699 Active int32 // usable by client 700 701 Id int32 // sequence number in flow graph 702 Rpo int32 // reverse post ordering 703 Loop uint16 // x5 for every loop 704 Refset uint8 // diagnostic generated 705 706 Data interface{} // for use by client 707 } 708 709 type Graph struct { 710 Start *Flow 711 Num int 712 713 // After calling flowrpo, rpo lists the flow nodes in reverse postorder, 714 // and each non-dead Flow node f has g->rpo[f->rpo] == f. 715 Rpo []*Flow 716 } 717 718 /* 719 * interface to back end 720 */ 721 722 const ( 723 // Pseudo-op, like TEXT, GLOBL, TYPE, PCDATA, FUNCDATA. 724 Pseudo = 1 << 1 725 726 // There's nothing to say about the instruction, 727 // but it's still okay to see. 728 OK = 1 << 2 729 730 // Size of right-side write, or right-side read if no write. 731 SizeB = 1 << 3 732 SizeW = 1 << 4 733 SizeL = 1 << 5 734 SizeQ = 1 << 6 735 SizeF = 1 << 7 736 SizeD = 1 << 8 737 738 // Left side (Prog.from): address taken, read, write. 739 LeftAddr = 1 << 9 740 LeftRead = 1 << 10 741 LeftWrite = 1 << 11 742 743 // Register in middle (Prog.reg); only ever read. (arm, ppc64) 744 RegRead = 1 << 12 745 CanRegRead = 1 << 13 746 747 // Right side (Prog.to): address taken, read, write. 748 RightAddr = 1 << 14 749 RightRead = 1 << 15 750 RightWrite = 1 << 16 751 752 // Instruction kinds 753 Move = 1 << 17 // straight move 754 Conv = 1 << 18 // size conversion 755 Cjmp = 1 << 19 // conditional jump 756 Break = 1 << 20 // breaks control flow (no fallthrough) 757 Call = 1 << 21 // function call 758 Jump = 1 << 22 // jump 759 Skip = 1 << 23 // data instruction 760 761 // Set, use, or kill of carry bit. 762 // Kill means we never look at the carry bit after this kind of instruction. 763 SetCarry = 1 << 24 764 UseCarry = 1 << 25 765 KillCarry = 1 << 26 766 767 // Special cases for register use. (amd64, 386) 768 ShiftCX = 1 << 27 // possible shift by CX 769 ImulAXDX = 1 << 28 // possible multiply into DX:AX 770 771 // Instruction updates whichever of from/to is type D_OREG. (ppc64) 772 PostInc = 1 << 29 773 ) 774 775 type Arch struct { 776 Thechar int 777 Thestring string 778 Thelinkarch *obj.LinkArch 779 Typedefs []Typedef 780 REGSP int 781 REGCTXT int 782 REGCALLX int // BX 783 REGCALLX2 int // AX 784 REGRETURN int // AX 785 REGMIN int 786 REGMAX int 787 REGZERO int // architectural zero register, if available 788 FREGMIN int 789 FREGMAX int 790 MAXWIDTH int64 791 ReservedRegs []int 792 793 AddIndex func(*Node, int64, *Node) bool // optional 794 Betypeinit func() 795 Bgen_float func(*Node, bool, int, *obj.Prog) // optional 796 Cgen64 func(*Node, *Node) // only on 32-bit systems 797 Cgenindex func(*Node, *Node, bool) *obj.Prog 798 Cgen_bmul func(int, *Node, *Node, *Node) bool 799 Cgen_float func(*Node, *Node) // optional 800 Cgen_hmul func(*Node, *Node, *Node) 801 Cgen_shift func(int, bool, *Node, *Node, *Node) 802 Clearfat func(*Node) 803 Cmp64 func(*Node, *Node, int, int, *obj.Prog) // only on 32-bit systems 804 Defframe func(*obj.Prog) 805 Dodiv func(int, *Node, *Node, *Node) 806 Excise func(*Flow) 807 Expandchecks func(*obj.Prog) 808 Getg func(*Node) 809 Gins func(int, *Node, *Node) *obj.Prog 810 811 // Ginscmp generates code comparing n1 to n2 and jumping away if op is satisfied. 812 // The returned prog should be Patch'ed with the jump target. 813 // If op is not satisfied, code falls through to the next emitted instruction. 814 // Likely is the branch prediction hint: +1 for likely, -1 for unlikely, 0 for no opinion. 815 // 816 // Ginscmp must be able to handle all kinds of arguments for n1 and n2, 817 // not just simple registers, although it can assume that there are no 818 // function calls needed during the evaluation, and on 32-bit systems 819 // the values are guaranteed not to be 64-bit values, so no in-memory 820 // temporaries are necessary. 821 Ginscmp func(op int, t *Type, n1, n2 *Node, likely int) *obj.Prog 822 823 // Ginsboolval inserts instructions to convert the result 824 // of a just-completed comparison to a boolean value. 825 // The first argument is the conditional jump instruction 826 // corresponding to the desired value. 827 // The second argument is the destination. 828 // If not present, Ginsboolval will be emulated with jumps. 829 Ginsboolval func(int, *Node) 830 831 Ginscon func(int, int64, *Node) 832 Ginsnop func() 833 Gmove func(*Node, *Node) 834 Igenindex func(*Node, *Node, bool) *obj.Prog 835 Linkarchinit func() 836 Peep func(*obj.Prog) 837 Proginfo func(*obj.Prog) // fills in Prog.Info 838 Regtyp func(*obj.Addr) bool 839 Sameaddr func(*obj.Addr, *obj.Addr) bool 840 Smallindir func(*obj.Addr, *obj.Addr) bool 841 Stackaddr func(*obj.Addr) bool 842 Blockcopy func(*Node, *Node, int64, int64, int64) 843 Sudoaddable func(int, *Node, *obj.Addr) bool 844 Sudoclean func() 845 Excludedregs func() uint64 846 RtoB func(int) uint64 847 FtoB func(int) uint64 848 BtoR func(uint64) int 849 BtoF func(uint64) int 850 Optoas func(int, *Type) int 851 Doregbits func(int) uint64 852 Regnames func(*int) []string 853 Use387 bool // should 8g use 387 FP instructions instead of sse2. 854 } 855 856 var pcloc int32 857 858 var Thearch Arch 859 860 var Newproc *Node 861 862 var Deferproc *Node 863 864 var Deferreturn *Node 865 866 var Panicindex *Node 867 868 var panicslice *Node 869 870 var throwreturn *Node 871