github.com/euank/go@v0.0.0-20160829210321-495514729181/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  	"bufio"
     9  	"cmd/compile/internal/ssa"
    10  	"cmd/internal/bio"
    11  	"cmd/internal/obj"
    12  )
    13  
    14  const (
    15  	UINF            = 100
    16  	BADWIDTH        = -1000000000
    17  	MaxStackVarSize = 10 * 1024 * 1024
    18  )
    19  
    20  type Pkg struct {
    21  	Name     string // package name, e.g. "sys"
    22  	Path     string // string literal used in import statement, e.g. "runtime/internal/sys"
    23  	Pathsym  *obj.LSym
    24  	Prefix   string // escaped path for use in symbol table
    25  	Imported bool   // export data of this package was parsed
    26  	Exported bool   // import line written in export data
    27  	Direct   bool   // imported directly
    28  	Safe     bool   // whether the package is marked as safe
    29  	Syms     map[string]*Sym
    30  }
    31  
    32  // Sym represents an object name. Most commonly, this is a Go identifier naming
    33  // an object declared within a package, but Syms are also used to name internal
    34  // synthesized objects.
    35  //
    36  // As a special exception, field and method names that are exported use the Sym
    37  // associated with localpkg instead of the package that declared them. This
    38  // allows using Sym pointer equality to test for Go identifier uniqueness when
    39  // handling selector expressions.
    40  type Sym struct {
    41  	Flags     SymFlags
    42  	Link      *Sym
    43  	Importdef *Pkg   // where imported definition was found
    44  	Linkname  string // link name
    45  
    46  	// saved and restored by dcopy
    47  	Pkg        *Pkg
    48  	Name       string // variable name
    49  	Def        *Node  // definition: ONAME OTYPE OPACK or OLITERAL
    50  	Block      int32  // blocknumber to catch redeclaration
    51  	Lastlineno int32  // last declaration for diagnostic
    52  
    53  	Label   *Label // corresponding label (ephemeral)
    54  	Origpkg *Pkg   // original package for . import
    55  	Lsym    *obj.LSym
    56  	Fsym    *Sym // funcsym
    57  }
    58  
    59  type Label struct {
    60  	Sym *Sym
    61  	Def *Node
    62  	Use []*Node
    63  
    64  	// for use during gen
    65  	Gotopc   *obj.Prog // pointer to unresolved gotos
    66  	Labelpc  *obj.Prog // pointer to code
    67  	Breakpc  *obj.Prog // pointer to code
    68  	Continpc *obj.Prog // pointer to code
    69  
    70  	Used bool // for "label defined and not used" error
    71  }
    72  
    73  type SymFlags uint8
    74  
    75  const (
    76  	SymExport SymFlags = 1 << iota // to be exported
    77  	SymPackage
    78  	SymExported // already written out by export
    79  	SymUniq
    80  	SymSiggen
    81  	SymAsm
    82  	SymAlgGen
    83  )
    84  
    85  // The Class of a variable/function describes the "storage class"
    86  // of a variable or function. During parsing, storage classes are
    87  // called declaration contexts.
    88  type Class uint8
    89  
    90  const (
    91  	Pxxx      Class = iota
    92  	PEXTERN         // global variable
    93  	PAUTO           // local variables
    94  	PAUTOHEAP       // local variable or parameter moved to heap
    95  	PPARAM          // input arguments
    96  	PPARAMOUT       // output results
    97  	PFUNC           // global function
    98  
    99  	PDISCARD // discard during parse of duplicate import
   100  )
   101  
   102  // note this is the runtime representation
   103  // of the compilers arrays.
   104  //
   105  // typedef	struct
   106  // {					// must not move anything
   107  // 	uchar	array[8];	// pointer to data
   108  // 	uchar	nel[4];		// number of elements
   109  // 	uchar	cap[4];		// allocated number of elements
   110  // } Array;
   111  var Array_array int // runtime offsetof(Array,array) - same for String
   112  
   113  var Array_nel int // runtime offsetof(Array,nel) - same for String
   114  
   115  var Array_cap int // runtime offsetof(Array,cap)
   116  
   117  var sizeof_Array int // runtime sizeof(Array)
   118  
   119  // note this is the runtime representation
   120  // of the compilers strings.
   121  //
   122  // typedef	struct
   123  // {					// must not move anything
   124  // 	uchar	array[8];	// pointer to data
   125  // 	uchar	nel[4];		// number of elements
   126  // } String;
   127  var sizeof_String int // runtime sizeof(String)
   128  
   129  var pragcgobuf string
   130  
   131  var infile string
   132  
   133  var outfile string
   134  var linkobj string
   135  
   136  var bout *bio.Writer
   137  
   138  var nerrors int
   139  
   140  var nsavederrors int
   141  
   142  var nsyntaxerrors int
   143  
   144  var decldepth int32
   145  
   146  var safemode bool
   147  
   148  var nolocalimports bool
   149  
   150  var Debug [256]int
   151  
   152  var debugstr string
   153  
   154  var Debug_checknil int
   155  var Debug_typeassert int
   156  
   157  var localpkg *Pkg // package being compiled
   158  
   159  var importpkg *Pkg // package being imported
   160  
   161  var itabpkg *Pkg // fake pkg for itab entries
   162  
   163  var itablinkpkg *Pkg // fake package for runtime itab entries
   164  
   165  var Runtimepkg *Pkg // package runtime
   166  
   167  var racepkg *Pkg // package runtime/race
   168  
   169  var msanpkg *Pkg // package runtime/msan
   170  
   171  var typepkg *Pkg // fake package for runtime type info (headers)
   172  
   173  var unsafepkg *Pkg // package unsafe
   174  
   175  var trackpkg *Pkg // fake package for field tracking
   176  
   177  var mappkg *Pkg // fake package for map zero value
   178  var zerosize int64
   179  
   180  var Tptr EType // either TPTR32 or TPTR64
   181  
   182  var myimportpath string
   183  
   184  var localimport string
   185  
   186  var asmhdr string
   187  
   188  var Simtype [NTYPE]EType
   189  
   190  var (
   191  	isforw    [NTYPE]bool
   192  	Isint     [NTYPE]bool
   193  	Isfloat   [NTYPE]bool
   194  	Iscomplex [NTYPE]bool
   195  	issimple  [NTYPE]bool
   196  )
   197  
   198  var (
   199  	okforeq    [NTYPE]bool
   200  	okforadd   [NTYPE]bool
   201  	okforand   [NTYPE]bool
   202  	okfornone  [NTYPE]bool
   203  	okforcmp   [NTYPE]bool
   204  	okforbool  [NTYPE]bool
   205  	okforcap   [NTYPE]bool
   206  	okforlen   [NTYPE]bool
   207  	okforarith [NTYPE]bool
   208  	okforconst [NTYPE]bool
   209  )
   210  
   211  var (
   212  	okfor [OEND][]bool
   213  	iscmp [OEND]bool
   214  )
   215  
   216  var Minintval [NTYPE]*Mpint
   217  
   218  var Maxintval [NTYPE]*Mpint
   219  
   220  var minfltval [NTYPE]*Mpflt
   221  
   222  var maxfltval [NTYPE]*Mpflt
   223  
   224  var xtop []*Node
   225  
   226  var exportlist []*Node
   227  
   228  var importlist []*Node // imported functions and methods with inlinable bodies
   229  
   230  var funcsyms []*Node
   231  
   232  var dclcontext Class // PEXTERN/PAUTO
   233  
   234  var incannedimport int
   235  
   236  var statuniqgen int // name generator for static temps
   237  
   238  var iota_ int32
   239  
   240  var lastconst []*Node
   241  
   242  var lasttype *Node
   243  
   244  var Maxarg int64
   245  
   246  var Stksize int64 // stack size for current frame
   247  
   248  var stkptrsize int64 // prefix of stack containing pointers
   249  
   250  var hasdefer bool // flag that curfn has defer statement
   251  
   252  var Curfn *Node
   253  
   254  var Widthptr int
   255  
   256  var Widthint int
   257  
   258  var Widthreg int
   259  
   260  var nblank *Node
   261  
   262  var typecheckok bool
   263  
   264  var compiling_runtime bool
   265  
   266  var compiling_wrappers int
   267  
   268  var use_writebarrier bool
   269  
   270  var pure_go bool
   271  
   272  var flag_installsuffix string
   273  
   274  var flag_race bool
   275  
   276  var flag_msan bool
   277  
   278  var flag_largemodel bool
   279  
   280  // Whether we are adding any sort of code instrumentation, such as
   281  // when the race detector is enabled.
   282  var instrumenting bool
   283  
   284  var debuglive int
   285  
   286  var Ctxt *obj.Link
   287  
   288  var writearchive bool
   289  
   290  var bstdout *bufio.Writer
   291  
   292  var Nacl bool
   293  
   294  var continpc *obj.Prog
   295  
   296  var breakpc *obj.Prog
   297  
   298  var Pc *obj.Prog
   299  
   300  var nodfp *Node
   301  
   302  var Disable_checknil int
   303  
   304  // interface to back end
   305  
   306  const (
   307  	// Pseudo-op, like TEXT, GLOBL, TYPE, PCDATA, FUNCDATA.
   308  	Pseudo = 1 << 1
   309  
   310  	// There's nothing to say about the instruction,
   311  	// but it's still okay to see.
   312  	OK = 1 << 2
   313  
   314  	// Size of right-side write, or right-side read if no write.
   315  	SizeB = 1 << 3
   316  	SizeW = 1 << 4
   317  	SizeL = 1 << 5
   318  	SizeQ = 1 << 6
   319  	SizeF = 1 << 7
   320  	SizeD = 1 << 8
   321  
   322  	// Left side (Prog.from): address taken, read, write.
   323  	LeftAddr  = 1 << 9
   324  	LeftRead  = 1 << 10
   325  	LeftWrite = 1 << 11
   326  
   327  	// Register in middle (Prog.reg); only ever read. (arm, ppc64)
   328  	RegRead    = 1 << 12
   329  	CanRegRead = 1 << 13
   330  
   331  	// Right side (Prog.to): address taken, read, write.
   332  	RightAddr  = 1 << 14
   333  	RightRead  = 1 << 15
   334  	RightWrite = 1 << 16
   335  
   336  	// Instruction kinds
   337  	Move  = 1 << 17 // straight move
   338  	Conv  = 1 << 18 // size conversion
   339  	Cjmp  = 1 << 19 // conditional jump
   340  	Break = 1 << 20 // breaks control flow (no fallthrough)
   341  	Call  = 1 << 21 // function call
   342  	Jump  = 1 << 22 // jump
   343  	Skip  = 1 << 23 // data instruction
   344  
   345  	// Set, use, or kill of carry bit.
   346  	// Kill means we never look at the carry bit after this kind of instruction.
   347  	// Originally for understanding ADC, RCR, and so on, but now also
   348  	// tracks set, use, and kill of the zero and overflow bits as well.
   349  	// TODO rename to {Set,Use,Kill}Flags
   350  	SetCarry  = 1 << 24
   351  	UseCarry  = 1 << 25
   352  	KillCarry = 1 << 26
   353  
   354  	// Special cases for register use. (amd64, 386)
   355  	ShiftCX  = 1 << 27 // possible shift by CX
   356  	ImulAXDX = 1 << 28 // possible multiply into DX:AX
   357  
   358  	// Instruction updates whichever of from/to is type D_OREG. (ppc64)
   359  	PostInc = 1 << 29
   360  )
   361  
   362  type Arch struct {
   363  	LinkArch *obj.LinkArch
   364  
   365  	REGSP        int
   366  	REGCTXT      int
   367  	REGCALLX     int // BX
   368  	REGCALLX2    int // AX
   369  	REGRETURN    int // AX
   370  	REGMIN       int
   371  	REGMAX       int
   372  	REGZERO      int // architectural zero register, if available
   373  	FREGMIN      int
   374  	FREGMAX      int
   375  	MAXWIDTH     int64
   376  	ReservedRegs []int
   377  
   378  	AddIndex            func(*Node, int64, *Node) bool // optional
   379  	Betypeinit          func()
   380  	Bgen_float          func(*Node, bool, int, *obj.Prog) // optional
   381  	Cgen64              func(*Node, *Node)                // only on 32-bit systems
   382  	Cgenindex           func(*Node, *Node, bool) *obj.Prog
   383  	Cgen_bmul           func(Op, *Node, *Node, *Node) bool
   384  	Cgen_float          func(*Node, *Node) // optional
   385  	Cgen_hmul           func(*Node, *Node, *Node)
   386  	RightShiftWithCarry func(*Node, uint, *Node)  // only on systems without RROTC instruction
   387  	AddSetCarry         func(*Node, *Node, *Node) // only on systems when ADD does not update carry flag
   388  	Cgen_shift          func(Op, bool, *Node, *Node, *Node)
   389  	Clearfat            func(*Node)
   390  	Cmp64               func(*Node, *Node, Op, int, *obj.Prog) // only on 32-bit systems
   391  	Defframe            func(*obj.Prog)
   392  	Dodiv               func(Op, *Node, *Node, *Node)
   393  	Excise              func(*Flow)
   394  	Expandchecks        func(*obj.Prog)
   395  	Getg                func(*Node)
   396  	Gins                func(obj.As, *Node, *Node) *obj.Prog
   397  
   398  	// Ginscmp generates code comparing n1 to n2 and jumping away if op is satisfied.
   399  	// The returned prog should be Patch'ed with the jump target.
   400  	// If op is not satisfied, code falls through to the next emitted instruction.
   401  	// Likely is the branch prediction hint: +1 for likely, -1 for unlikely, 0 for no opinion.
   402  	//
   403  	// Ginscmp must be able to handle all kinds of arguments for n1 and n2,
   404  	// not just simple registers, although it can assume that there are no
   405  	// function calls needed during the evaluation, and on 32-bit systems
   406  	// the values are guaranteed not to be 64-bit values, so no in-memory
   407  	// temporaries are necessary.
   408  	Ginscmp func(op Op, t *Type, n1, n2 *Node, likely int) *obj.Prog
   409  
   410  	// Ginsboolval inserts instructions to convert the result
   411  	// of a just-completed comparison to a boolean value.
   412  	// The first argument is the conditional jump instruction
   413  	// corresponding to the desired value.
   414  	// The second argument is the destination.
   415  	// If not present, Ginsboolval will be emulated with jumps.
   416  	Ginsboolval func(obj.As, *Node)
   417  
   418  	Ginscon      func(obj.As, int64, *Node)
   419  	Ginsnop      func()
   420  	Gmove        func(*Node, *Node)
   421  	Igenindex    func(*Node, *Node, bool) *obj.Prog
   422  	Peep         func(*obj.Prog)
   423  	Proginfo     func(*obj.Prog) // fills in Prog.Info
   424  	Regtyp       func(*obj.Addr) bool
   425  	Sameaddr     func(*obj.Addr, *obj.Addr) bool
   426  	Smallindir   func(*obj.Addr, *obj.Addr) bool
   427  	Stackaddr    func(*obj.Addr) bool
   428  	Blockcopy    func(*Node, *Node, int64, int64, int64)
   429  	Sudoaddable  func(obj.As, *Node, *obj.Addr) bool
   430  	Sudoclean    func()
   431  	Excludedregs func() uint64
   432  	RtoB         func(int) uint64
   433  	FtoB         func(int) uint64
   434  	BtoR         func(uint64) int
   435  	BtoF         func(uint64) int
   436  	Optoas       func(Op, *Type) obj.As
   437  	Doregbits    func(int) uint64
   438  	Regnames     func(*int) []string
   439  	Use387       bool // should 8g use 387 FP instructions instead of sse2.
   440  
   441  	// SSARegToReg maps ssa register numbers to obj register numbers.
   442  	SSARegToReg []int16
   443  
   444  	// SSAMarkMoves marks any MOVXconst ops that need to avoid clobbering flags.
   445  	SSAMarkMoves func(*SSAGenState, *ssa.Block)
   446  
   447  	// SSAGenValue emits Prog(s) for the Value.
   448  	SSAGenValue func(*SSAGenState, *ssa.Value)
   449  
   450  	// SSAGenBlock emits end-of-block Progs. SSAGenValue should be called
   451  	// for all values in the block before SSAGenBlock.
   452  	SSAGenBlock func(s *SSAGenState, b, next *ssa.Block)
   453  }
   454  
   455  var pcloc int32
   456  
   457  var Thearch Arch
   458  
   459  var Newproc *Node
   460  
   461  var Deferproc *Node
   462  
   463  var Deferreturn *Node
   464  
   465  var Panicindex *Node
   466  
   467  var panicslice *Node
   468  
   469  var panicdivide *Node
   470  
   471  var throwreturn *Node
   472  
   473  var growslice *Node
   474  
   475  var writebarrierptr *Node
   476  var typedmemmove *Node
   477  
   478  var panicdottype *Node