github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/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/ssa"
    10  	"cmd/internal/obj"
    11  )
    12  
    13  const (
    14  	UINF            = 100
    15  	BADWIDTH        = -1000000000
    16  	MaxStackVarSize = 10 * 1024 * 1024
    17  )
    18  
    19  type Val struct {
    20  	// U contains one of:
    21  	// bool     bool when n.ValCtype() == CTBOOL
    22  	// *Mpint   int when n.ValCtype() == CTINT, rune when n.ValCtype() == CTRUNE
    23  	// *Mpflt   float when n.ValCtype() == CTFLT
    24  	// *Mpcplx  pair of floats when n.ValCtype() == CTCPLX
    25  	// string   string when n.ValCtype() == CTSTR
    26  	// *Nilval  when n.ValCtype() == CTNIL
    27  	U interface{}
    28  }
    29  
    30  type NilVal struct{}
    31  
    32  func (v Val) Ctype() Ctype {
    33  	switch x := v.U.(type) {
    34  	default:
    35  		Fatalf("unexpected Ctype for %T", v.U)
    36  		panic("not reached")
    37  	case nil:
    38  		return 0
    39  	case *NilVal:
    40  		return CTNIL
    41  	case bool:
    42  		return CTBOOL
    43  	case *Mpint:
    44  		if x.Rune {
    45  			return CTRUNE
    46  		}
    47  		return CTINT
    48  	case *Mpflt:
    49  		return CTFLT
    50  	case *Mpcplx:
    51  		return CTCPLX
    52  	case string:
    53  		return CTSTR
    54  	}
    55  }
    56  
    57  type Pkg struct {
    58  	Name     string // package name, e.g. "sys"
    59  	Path     string // string literal used in import statement, e.g. "runtime/internal/sys"
    60  	Pathsym  *Sym
    61  	Prefix   string // escaped path for use in symbol table
    62  	Imported bool   // export data of this package was parsed
    63  	Exported bool   // import line written in export data
    64  	Direct   bool   // imported directly
    65  	Safe     bool   // whether the package is marked as safe
    66  	Syms     map[string]*Sym
    67  }
    68  
    69  type Sym struct {
    70  	Flags     SymFlags
    71  	Link      *Sym
    72  	Importdef *Pkg   // where imported definition was found
    73  	Linkname  string // link name
    74  
    75  	// saved and restored by dcopy
    76  	Pkg        *Pkg
    77  	Name       string // variable name
    78  	Def        *Node  // definition: ONAME OTYPE OPACK or OLITERAL
    79  	Block      int32  // blocknumber to catch redeclaration
    80  	Lastlineno int32  // last declaration for diagnostic
    81  
    82  	Label   *Label // corresponding label (ephemeral)
    83  	Origpkg *Pkg   // original package for . import
    84  	Lsym    *obj.LSym
    85  	Fsym    *Sym // funcsym
    86  }
    87  
    88  type Label struct {
    89  	Sym *Sym
    90  	Def *Node
    91  	Use []*Node
    92  
    93  	// for use during gen
    94  	Gotopc   *obj.Prog // pointer to unresolved gotos
    95  	Labelpc  *obj.Prog // pointer to code
    96  	Breakpc  *obj.Prog // pointer to code
    97  	Continpc *obj.Prog // pointer to code
    98  
    99  	Used bool
   100  }
   101  
   102  type SymFlags uint8
   103  
   104  const (
   105  	SymExport SymFlags = 1 << iota // to be exported
   106  	SymPackage
   107  	SymExported // already written out by export
   108  	SymUniq
   109  	SymSiggen
   110  	SymAsm
   111  	SymAlgGen
   112  )
   113  
   114  var dclstack *Sym
   115  
   116  // Ctype describes the constant kind of an "ideal" (untyped) constant.
   117  type Ctype int8
   118  
   119  const (
   120  	CTxxx Ctype = iota
   121  
   122  	CTINT
   123  	CTRUNE
   124  	CTFLT
   125  	CTCPLX
   126  	CTSTR
   127  	CTBOOL
   128  	CTNIL
   129  )
   130  
   131  // ChanDir is whether a channel can send, receive, or both.
   132  type ChanDir uint8
   133  
   134  func (c ChanDir) CanRecv() bool { return c&Crecv != 0 }
   135  func (c ChanDir) CanSend() bool { return c&Csend != 0 }
   136  
   137  const (
   138  	// types of channel
   139  	// must match ../../../../reflect/type.go:/ChanDir
   140  	Crecv ChanDir = 1 << 0
   141  	Csend ChanDir = 1 << 1
   142  	Cboth ChanDir = Crecv | Csend
   143  )
   144  
   145  // The Class of a variable/function describes the "storage class"
   146  // of a variable or function. During parsing, storage classes are
   147  // called declaration contexts.
   148  type Class uint8
   149  
   150  const (
   151  	Pxxx      Class = iota
   152  	PEXTERN         // global variable
   153  	PAUTO           // local variables
   154  	PPARAM          // input arguments
   155  	PPARAMOUT       // output results
   156  	PPARAMREF       // closure variable reference
   157  	PFUNC           // global function
   158  
   159  	PDISCARD // discard during parse of duplicate import
   160  
   161  	PHEAP = 1 << 7 // an extra bit to identify an escaped variable
   162  )
   163  
   164  const (
   165  	Etop      = 1 << 1 // evaluated at statement level
   166  	Erv       = 1 << 2 // evaluated in value context
   167  	Etype     = 1 << 3
   168  	Ecall     = 1 << 4  // call-only expressions are ok
   169  	Efnstruct = 1 << 5  // multivalue function returns are ok
   170  	Eiota     = 1 << 6  // iota is ok
   171  	Easgn     = 1 << 7  // assigning to expression
   172  	Eindir    = 1 << 8  // indirecting through expression
   173  	Eaddr     = 1 << 9  // taking address of expression
   174  	Eproc     = 1 << 10 // inside a go statement
   175  	Ecomplit  = 1 << 11 // type in composite literal
   176  )
   177  
   178  type Sig struct {
   179  	name   string
   180  	pkg    *Pkg
   181  	isym   *Sym
   182  	tsym   *Sym
   183  	type_  *Type
   184  	mtype  *Type
   185  	offset int32
   186  }
   187  
   188  // note this is the runtime representation
   189  // of the compilers arrays.
   190  //
   191  // typedef	struct
   192  // {					// must not move anything
   193  // 	uchar	array[8];	// pointer to data
   194  // 	uchar	nel[4];		// number of elements
   195  // 	uchar	cap[4];		// allocated number of elements
   196  // } Array;
   197  var Array_array int // runtime offsetof(Array,array) - same for String
   198  
   199  var Array_nel int // runtime offsetof(Array,nel) - same for String
   200  
   201  var Array_cap int // runtime offsetof(Array,cap)
   202  
   203  var sizeof_Array int // runtime sizeof(Array)
   204  
   205  // note this is the runtime representation
   206  // of the compilers strings.
   207  //
   208  // typedef	struct
   209  // {					// must not move anything
   210  // 	uchar	array[8];	// pointer to data
   211  // 	uchar	nel[4];		// number of elements
   212  // } String;
   213  var sizeof_String int // runtime sizeof(String)
   214  
   215  // lexlineno is the line number _after_ the most recently read rune.
   216  // In particular, it's advanced (or rewound) as newlines are read (or unread).
   217  var lexlineno int32
   218  
   219  // lineno is the line number at the start of the most recently lexed token.
   220  var lineno int32
   221  
   222  var pragcgobuf string
   223  
   224  var infile string
   225  
   226  var outfile string
   227  
   228  var bout *obj.Biobuf
   229  
   230  var nerrors int
   231  
   232  var nsavederrors int
   233  
   234  var nsyntaxerrors int
   235  
   236  var decldepth int32
   237  
   238  var safemode int
   239  
   240  var nolocalimports int
   241  
   242  var lexbuf bytes.Buffer
   243  var strbuf bytes.Buffer
   244  var litbuf string // LLITERAL value for use in syntax error messages
   245  
   246  var Debug [256]int
   247  
   248  var debugstr string
   249  
   250  var Debug_checknil int
   251  var Debug_typeassert int
   252  
   253  var localpkg *Pkg // package being compiled
   254  
   255  var importpkg *Pkg // package being imported
   256  
   257  var itabpkg *Pkg // fake pkg for itab entries
   258  
   259  var itablinkpkg *Pkg // fake package for runtime itab entries
   260  
   261  var Runtimepkg *Pkg // package runtime
   262  
   263  var racepkg *Pkg // package runtime/race
   264  
   265  var msanpkg *Pkg // package runtime/msan
   266  
   267  var typepkg *Pkg // fake package for runtime type info (headers)
   268  
   269  var typelinkpkg *Pkg // fake package for runtime type info (data)
   270  
   271  var unsafepkg *Pkg // package unsafe
   272  
   273  var trackpkg *Pkg // fake package for field tracking
   274  
   275  var Tptr EType // either TPTR32 or TPTR64
   276  
   277  var myimportpath string
   278  
   279  var localimport string
   280  
   281  var asmhdr string
   282  
   283  var Simtype [NTYPE]EType
   284  
   285  var (
   286  	isforw    [NTYPE]bool
   287  	Isint     [NTYPE]bool
   288  	Isfloat   [NTYPE]bool
   289  	Iscomplex [NTYPE]bool
   290  	issimple  [NTYPE]bool
   291  )
   292  
   293  var (
   294  	okforeq    [NTYPE]bool
   295  	okforadd   [NTYPE]bool
   296  	okforand   [NTYPE]bool
   297  	okfornone  [NTYPE]bool
   298  	okforcmp   [NTYPE]bool
   299  	okforbool  [NTYPE]bool
   300  	okforcap   [NTYPE]bool
   301  	okforlen   [NTYPE]bool
   302  	okforarith [NTYPE]bool
   303  	okforconst [NTYPE]bool
   304  )
   305  
   306  var (
   307  	okfor [OEND][]bool
   308  	iscmp [OEND]bool
   309  )
   310  
   311  var Minintval [NTYPE]*Mpint
   312  
   313  var Maxintval [NTYPE]*Mpint
   314  
   315  var minfltval [NTYPE]*Mpflt
   316  
   317  var maxfltval [NTYPE]*Mpflt
   318  
   319  var xtop []*Node
   320  
   321  var externdcl []*Node
   322  
   323  var exportlist []*Node
   324  
   325  var importlist []*Node // imported functions and methods with inlinable bodies
   326  
   327  var funcsyms []*Node
   328  
   329  var dclcontext Class // PEXTERN/PAUTO
   330  
   331  var incannedimport int
   332  
   333  var statuniqgen int // name generator for static temps
   334  
   335  var iota_ int32
   336  
   337  var lastconst []*Node
   338  
   339  var lasttype *Node
   340  
   341  var Maxarg int64
   342  
   343  var Stksize int64 // stack size for current frame
   344  
   345  var stkptrsize int64 // prefix of stack containing pointers
   346  
   347  var blockgen int32 // max block number
   348  
   349  var block int32 // current block number
   350  
   351  var hasdefer bool // flag that curfn has defer statement
   352  
   353  var Curfn *Node
   354  
   355  var Widthptr int
   356  
   357  var Widthint int
   358  
   359  var Widthreg int
   360  
   361  var nblank *Node
   362  
   363  var Funcdepth int32
   364  
   365  var typecheckok bool
   366  
   367  var compiling_runtime int
   368  
   369  var compiling_wrappers int
   370  
   371  var use_writebarrier int
   372  
   373  var pure_go int
   374  
   375  var flag_installsuffix string
   376  
   377  var flag_race int
   378  
   379  var flag_msan int
   380  
   381  var flag_largemodel int
   382  
   383  // Whether we are adding any sort of code instrumentation, such as
   384  // when the race detector is enabled.
   385  var instrumenting bool
   386  
   387  var debuglive int
   388  
   389  var Ctxt *obj.Link
   390  
   391  var writearchive int
   392  
   393  var bstdout obj.Biobuf
   394  
   395  var Nacl bool
   396  
   397  var continpc *obj.Prog
   398  
   399  var breakpc *obj.Prog
   400  
   401  var Pc *obj.Prog
   402  
   403  var nodfp *Node
   404  
   405  var Disable_checknil int
   406  
   407  type Flow struct {
   408  	Prog   *obj.Prog // actual instruction
   409  	P1     *Flow     // predecessors of this instruction: p1,
   410  	P2     *Flow     // and then p2 linked though p2link.
   411  	P2link *Flow
   412  	S1     *Flow // successors of this instruction (at most two: s1 and s2).
   413  	S2     *Flow
   414  	Link   *Flow // next instruction in function code
   415  
   416  	Active int32 // usable by client
   417  
   418  	Id     int32  // sequence number in flow graph
   419  	Rpo    int32  // reverse post ordering
   420  	Loop   uint16 // x5 for every loop
   421  	Refset bool   // diagnostic generated
   422  
   423  	Data interface{} // for use by client
   424  }
   425  
   426  type Graph struct {
   427  	Start *Flow
   428  	Num   int
   429  
   430  	// After calling flowrpo, rpo lists the flow nodes in reverse postorder,
   431  	// and each non-dead Flow node f has g->rpo[f->rpo] == f.
   432  	Rpo []*Flow
   433  }
   434  
   435  // interface to back end
   436  
   437  const (
   438  	// Pseudo-op, like TEXT, GLOBL, TYPE, PCDATA, FUNCDATA.
   439  	Pseudo = 1 << 1
   440  
   441  	// There's nothing to say about the instruction,
   442  	// but it's still okay to see.
   443  	OK = 1 << 2
   444  
   445  	// Size of right-side write, or right-side read if no write.
   446  	SizeB = 1 << 3
   447  	SizeW = 1 << 4
   448  	SizeL = 1 << 5
   449  	SizeQ = 1 << 6
   450  	SizeF = 1 << 7
   451  	SizeD = 1 << 8
   452  
   453  	// Left side (Prog.from): address taken, read, write.
   454  	LeftAddr  = 1 << 9
   455  	LeftRead  = 1 << 10
   456  	LeftWrite = 1 << 11
   457  
   458  	// Register in middle (Prog.reg); only ever read. (arm, ppc64)
   459  	RegRead    = 1 << 12
   460  	CanRegRead = 1 << 13
   461  
   462  	// Right side (Prog.to): address taken, read, write.
   463  	RightAddr  = 1 << 14
   464  	RightRead  = 1 << 15
   465  	RightWrite = 1 << 16
   466  
   467  	// Instruction kinds
   468  	Move  = 1 << 17 // straight move
   469  	Conv  = 1 << 18 // size conversion
   470  	Cjmp  = 1 << 19 // conditional jump
   471  	Break = 1 << 20 // breaks control flow (no fallthrough)
   472  	Call  = 1 << 21 // function call
   473  	Jump  = 1 << 22 // jump
   474  	Skip  = 1 << 23 // data instruction
   475  
   476  	// Set, use, or kill of carry bit.
   477  	// Kill means we never look at the carry bit after this kind of instruction.
   478  	// Originally for understanding ADC, RCR, and so on, but now also
   479  	// tracks set, use, and kill of the zero and overflow bits as well.
   480  	// TODO rename to {Set,Use,Kill}Flags
   481  	SetCarry  = 1 << 24
   482  	UseCarry  = 1 << 25
   483  	KillCarry = 1 << 26
   484  
   485  	// Special cases for register use. (amd64, 386)
   486  	ShiftCX  = 1 << 27 // possible shift by CX
   487  	ImulAXDX = 1 << 28 // possible multiply into DX:AX
   488  
   489  	// Instruction updates whichever of from/to is type D_OREG. (ppc64)
   490  	PostInc = 1 << 29
   491  )
   492  
   493  type Arch struct {
   494  	Thechar      int
   495  	Thestring    string
   496  	Thelinkarch  *obj.LinkArch
   497  	REGSP        int
   498  	REGCTXT      int
   499  	REGCALLX     int // BX
   500  	REGCALLX2    int // AX
   501  	REGRETURN    int // AX
   502  	REGMIN       int
   503  	REGMAX       int
   504  	REGZERO      int // architectural zero register, if available
   505  	FREGMIN      int
   506  	FREGMAX      int
   507  	MAXWIDTH     int64
   508  	ReservedRegs []int
   509  
   510  	AddIndex     func(*Node, int64, *Node) bool // optional
   511  	Betypeinit   func()
   512  	Bgen_float   func(*Node, bool, int, *obj.Prog) // optional
   513  	Cgen64       func(*Node, *Node)                // only on 32-bit systems
   514  	Cgenindex    func(*Node, *Node, bool) *obj.Prog
   515  	Cgen_bmul    func(Op, *Node, *Node, *Node) bool
   516  	Cgen_float   func(*Node, *Node) // optional
   517  	Cgen_hmul    func(*Node, *Node, *Node)
   518  	Cgen_shift   func(Op, bool, *Node, *Node, *Node)
   519  	Clearfat     func(*Node)
   520  	Cmp64        func(*Node, *Node, Op, int, *obj.Prog) // only on 32-bit systems
   521  	Defframe     func(*obj.Prog)
   522  	Dodiv        func(Op, *Node, *Node, *Node)
   523  	Excise       func(*Flow)
   524  	Expandchecks func(*obj.Prog)
   525  	Getg         func(*Node)
   526  	Gins         func(obj.As, *Node, *Node) *obj.Prog
   527  
   528  	// Ginscmp generates code comparing n1 to n2 and jumping away if op is satisfied.
   529  	// The returned prog should be Patch'ed with the jump target.
   530  	// If op is not satisfied, code falls through to the next emitted instruction.
   531  	// Likely is the branch prediction hint: +1 for likely, -1 for unlikely, 0 for no opinion.
   532  	//
   533  	// Ginscmp must be able to handle all kinds of arguments for n1 and n2,
   534  	// not just simple registers, although it can assume that there are no
   535  	// function calls needed during the evaluation, and on 32-bit systems
   536  	// the values are guaranteed not to be 64-bit values, so no in-memory
   537  	// temporaries are necessary.
   538  	Ginscmp func(op Op, t *Type, n1, n2 *Node, likely int) *obj.Prog
   539  
   540  	// Ginsboolval inserts instructions to convert the result
   541  	// of a just-completed comparison to a boolean value.
   542  	// The first argument is the conditional jump instruction
   543  	// corresponding to the desired value.
   544  	// The second argument is the destination.
   545  	// If not present, Ginsboolval will be emulated with jumps.
   546  	Ginsboolval func(obj.As, *Node)
   547  
   548  	Ginscon      func(obj.As, int64, *Node)
   549  	Ginsnop      func()
   550  	Gmove        func(*Node, *Node)
   551  	Igenindex    func(*Node, *Node, bool) *obj.Prog
   552  	Peep         func(*obj.Prog)
   553  	Proginfo     func(*obj.Prog) // fills in Prog.Info
   554  	Regtyp       func(*obj.Addr) bool
   555  	Sameaddr     func(*obj.Addr, *obj.Addr) bool
   556  	Smallindir   func(*obj.Addr, *obj.Addr) bool
   557  	Stackaddr    func(*obj.Addr) bool
   558  	Blockcopy    func(*Node, *Node, int64, int64, int64)
   559  	Sudoaddable  func(obj.As, *Node, *obj.Addr) bool
   560  	Sudoclean    func()
   561  	Excludedregs func() uint64
   562  	RtoB         func(int) uint64
   563  	FtoB         func(int) uint64
   564  	BtoR         func(uint64) int
   565  	BtoF         func(uint64) int
   566  	Optoas       func(Op, *Type) obj.As
   567  	Doregbits    func(int) uint64
   568  	Regnames     func(*int) []string
   569  	Use387       bool // should 8g use 387 FP instructions instead of sse2.
   570  
   571  	// SSARegToReg maps ssa register numbers to obj register numbers.
   572  	SSARegToReg []int16
   573  
   574  	// SSAMarkMoves marks any MOVXconst ops that need to avoid clobbering flags.
   575  	SSAMarkMoves func(*SSAGenState, *ssa.Block)
   576  
   577  	// SSAGenValue emits Prog(s) for the Value.
   578  	SSAGenValue func(*SSAGenState, *ssa.Value)
   579  
   580  	// SSAGenBlock emits end-of-block Progs. SSAGenValue should be called
   581  	// for all values in the block before SSAGenBlock.
   582  	SSAGenBlock func(s *SSAGenState, b, next *ssa.Block)
   583  }
   584  
   585  var pcloc int32
   586  
   587  var Thearch Arch
   588  
   589  var Newproc *Node
   590  
   591  var Deferproc *Node
   592  
   593  var Deferreturn *Node
   594  
   595  var Panicindex *Node
   596  
   597  var panicslice *Node
   598  
   599  var panicdivide *Node
   600  
   601  var throwreturn *Node
   602  
   603  var growslice *Node
   604  
   605  var writebarrierptr *Node
   606  var typedmemmove *Node
   607  
   608  var panicdottype *Node