github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/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
    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 autopkg *Pkg // fake package for allocating auto variables
   160  
   161  var importpkg *Pkg // package being imported
   162  
   163  var itabpkg *Pkg // fake pkg for itab entries
   164  
   165  var itablinkpkg *Pkg // fake package for runtime itab entries
   166  
   167  var Runtimepkg *Pkg // package runtime
   168  
   169  var racepkg *Pkg // package runtime/race
   170  
   171  var msanpkg *Pkg // package runtime/msan
   172  
   173  var typepkg *Pkg // fake package for runtime type info (headers)
   174  
   175  var unsafepkg *Pkg // package unsafe
   176  
   177  var trackpkg *Pkg // fake package for field tracking
   178  
   179  var mappkg *Pkg // fake package for map zero value
   180  var zerosize int64
   181  
   182  var Tptr EType // either TPTR32 or TPTR64
   183  
   184  var myimportpath string
   185  
   186  var localimport string
   187  
   188  var asmhdr string
   189  
   190  var Simtype [NTYPE]EType
   191  
   192  var (
   193  	isforw    [NTYPE]bool
   194  	Isint     [NTYPE]bool
   195  	Isfloat   [NTYPE]bool
   196  	Iscomplex [NTYPE]bool
   197  	issimple  [NTYPE]bool
   198  )
   199  
   200  var (
   201  	okforeq    [NTYPE]bool
   202  	okforadd   [NTYPE]bool
   203  	okforand   [NTYPE]bool
   204  	okfornone  [NTYPE]bool
   205  	okforcmp   [NTYPE]bool
   206  	okforbool  [NTYPE]bool
   207  	okforcap   [NTYPE]bool
   208  	okforlen   [NTYPE]bool
   209  	okforarith [NTYPE]bool
   210  	okforconst [NTYPE]bool
   211  )
   212  
   213  var (
   214  	okfor [OEND][]bool
   215  	iscmp [OEND]bool
   216  )
   217  
   218  var Minintval [NTYPE]*Mpint
   219  
   220  var Maxintval [NTYPE]*Mpint
   221  
   222  var minfltval [NTYPE]*Mpflt
   223  
   224  var maxfltval [NTYPE]*Mpflt
   225  
   226  var xtop []*Node
   227  
   228  var exportlist []*Node
   229  
   230  var importlist []*Node // imported functions and methods with inlinable bodies
   231  
   232  var funcsyms []*Node
   233  
   234  var dclcontext Class // PEXTERN/PAUTO
   235  
   236  var incannedimport int
   237  
   238  var statuniqgen int // name generator for static temps
   239  
   240  var iota_ int32
   241  
   242  var lastconst []*Node
   243  
   244  var lasttype *Node
   245  
   246  var Maxarg int64
   247  
   248  var Stksize int64 // stack size for current frame
   249  
   250  var stkptrsize int64 // prefix of stack containing pointers
   251  
   252  var hasdefer bool // flag that curfn has defer statement
   253  
   254  var Curfn *Node
   255  
   256  var Widthptr int
   257  
   258  var Widthint int
   259  
   260  var Widthreg int
   261  
   262  var nblank *Node
   263  
   264  var typecheckok bool
   265  
   266  var compiling_runtime bool
   267  
   268  var compiling_wrappers int
   269  
   270  var use_writebarrier bool
   271  
   272  var pure_go bool
   273  
   274  var flag_installsuffix string
   275  
   276  var flag_race bool
   277  
   278  var flag_msan bool
   279  
   280  var flag_largemodel bool
   281  
   282  // Whether we are adding any sort of code instrumentation, such as
   283  // when the race detector is enabled.
   284  var instrumenting bool
   285  
   286  var debuglive int
   287  
   288  var Ctxt *obj.Link
   289  
   290  var writearchive bool
   291  
   292  var bstdout *bufio.Writer
   293  
   294  var Nacl bool
   295  
   296  var continpc *obj.Prog
   297  
   298  var breakpc *obj.Prog
   299  
   300  var Pc *obj.Prog
   301  
   302  var nodfp *Node
   303  
   304  var Disable_checknil int
   305  
   306  // interface to back end
   307  
   308  const (
   309  	// Pseudo-op, like TEXT, GLOBL, TYPE, PCDATA, FUNCDATA.
   310  	Pseudo = 1 << 1
   311  
   312  	// There's nothing to say about the instruction,
   313  	// but it's still okay to see.
   314  	OK = 1 << 2
   315  
   316  	// Size of right-side write, or right-side read if no write.
   317  	SizeB = 1 << 3
   318  	SizeW = 1 << 4
   319  	SizeL = 1 << 5
   320  	SizeQ = 1 << 6
   321  	SizeF = 1 << 7
   322  	SizeD = 1 << 8
   323  
   324  	// Left side (Prog.from): address taken, read, write.
   325  	LeftAddr  = 1 << 9
   326  	LeftRead  = 1 << 10
   327  	LeftWrite = 1 << 11
   328  
   329  	// Register in middle (Prog.reg); only ever read. (arm, ppc64)
   330  	RegRead    = 1 << 12
   331  	CanRegRead = 1 << 13
   332  
   333  	// Right side (Prog.to): address taken, read, write.
   334  	RightAddr  = 1 << 14
   335  	RightRead  = 1 << 15
   336  	RightWrite = 1 << 16
   337  
   338  	// Instruction kinds
   339  	Move  = 1 << 17 // straight move
   340  	Conv  = 1 << 18 // size conversion
   341  	Cjmp  = 1 << 19 // conditional jump
   342  	Break = 1 << 20 // breaks control flow (no fallthrough)
   343  	Call  = 1 << 21 // function call
   344  	Jump  = 1 << 22 // jump
   345  	Skip  = 1 << 23 // data instruction
   346  
   347  	// Set, use, or kill of carry bit.
   348  	// Kill means we never look at the carry bit after this kind of instruction.
   349  	// Originally for understanding ADC, RCR, and so on, but now also
   350  	// tracks set, use, and kill of the zero and overflow bits as well.
   351  	// TODO rename to {Set,Use,Kill}Flags
   352  	SetCarry  = 1 << 24
   353  	UseCarry  = 1 << 25
   354  	KillCarry = 1 << 26
   355  
   356  	// Special cases for register use. (amd64, 386)
   357  	ShiftCX  = 1 << 27 // possible shift by CX
   358  	ImulAXDX = 1 << 28 // possible multiply into DX:AX
   359  
   360  	// Instruction updates whichever of from/to is type D_OREG. (ppc64)
   361  	PostInc = 1 << 29
   362  )
   363  
   364  type Arch struct {
   365  	LinkArch *obj.LinkArch
   366  
   367  	REGSP        int
   368  	REGCTXT      int
   369  	REGCALLX     int // BX
   370  	REGCALLX2    int // AX
   371  	REGRETURN    int // AX
   372  	REGMIN       int
   373  	REGMAX       int
   374  	REGZERO      int // architectural zero register, if available
   375  	FREGMIN      int
   376  	FREGMAX      int
   377  	MAXWIDTH     int64
   378  	ReservedRegs []int
   379  
   380  	AddIndex            func(*Node, int64, *Node) bool // optional
   381  	Betypeinit          func()
   382  	Bgen_float          func(*Node, bool, int, *obj.Prog) // optional
   383  	Cgen64              func(*Node, *Node)                // only on 32-bit systems
   384  	Cgenindex           func(*Node, *Node, bool) *obj.Prog
   385  	Cgen_bmul           func(Op, *Node, *Node, *Node) bool
   386  	Cgen_float          func(*Node, *Node) // optional
   387  	Cgen_hmul           func(*Node, *Node, *Node)
   388  	RightShiftWithCarry func(*Node, uint, *Node)  // only on systems without RROTC instruction
   389  	AddSetCarry         func(*Node, *Node, *Node) // only on systems when ADD does not update carry flag
   390  	Cgen_shift          func(Op, bool, *Node, *Node, *Node)
   391  	Clearfat            func(*Node)
   392  	Cmp64               func(*Node, *Node, Op, int, *obj.Prog) // only on 32-bit systems
   393  	Defframe            func(*obj.Prog)
   394  	Dodiv               func(Op, *Node, *Node, *Node)
   395  	Excise              func(*Flow)
   396  	Expandchecks        func(*obj.Prog)
   397  	Getg                func(*Node)
   398  	Gins                func(obj.As, *Node, *Node) *obj.Prog
   399  
   400  	// Ginscmp generates code comparing n1 to n2 and jumping away if op is satisfied.
   401  	// The returned prog should be Patch'ed with the jump target.
   402  	// If op is not satisfied, code falls through to the next emitted instruction.
   403  	// Likely is the branch prediction hint: +1 for likely, -1 for unlikely, 0 for no opinion.
   404  	//
   405  	// Ginscmp must be able to handle all kinds of arguments for n1 and n2,
   406  	// not just simple registers, although it can assume that there are no
   407  	// function calls needed during the evaluation, and on 32-bit systems
   408  	// the values are guaranteed not to be 64-bit values, so no in-memory
   409  	// temporaries are necessary.
   410  	Ginscmp func(op Op, t *Type, n1, n2 *Node, likely int) *obj.Prog
   411  
   412  	// Ginsboolval inserts instructions to convert the result
   413  	// of a just-completed comparison to a boolean value.
   414  	// The first argument is the conditional jump instruction
   415  	// corresponding to the desired value.
   416  	// The second argument is the destination.
   417  	// If not present, Ginsboolval will be emulated with jumps.
   418  	Ginsboolval func(obj.As, *Node)
   419  
   420  	Ginscon      func(obj.As, int64, *Node)
   421  	Ginsnop      func()
   422  	Gmove        func(*Node, *Node)
   423  	Igenindex    func(*Node, *Node, bool) *obj.Prog
   424  	Peep         func(*obj.Prog)
   425  	Proginfo     func(*obj.Prog) // fills in Prog.Info
   426  	Regtyp       func(*obj.Addr) bool
   427  	Sameaddr     func(*obj.Addr, *obj.Addr) bool
   428  	Smallindir   func(*obj.Addr, *obj.Addr) bool
   429  	Stackaddr    func(*obj.Addr) bool
   430  	Blockcopy    func(*Node, *Node, int64, int64, int64)
   431  	Sudoaddable  func(obj.As, *Node, *obj.Addr) bool
   432  	Sudoclean    func()
   433  	Excludedregs func() uint64
   434  	RtoB         func(int) uint64
   435  	FtoB         func(int) uint64
   436  	BtoR         func(uint64) int
   437  	BtoF         func(uint64) int
   438  	Optoas       func(Op, *Type) obj.As
   439  	Doregbits    func(int) uint64
   440  	Regnames     func(*int) []string
   441  	Use387       bool // should 8g use 387 FP instructions instead of sse2.
   442  
   443  	// SSARegToReg maps ssa register numbers to obj register numbers.
   444  	SSARegToReg []int16
   445  
   446  	// SSAMarkMoves marks any MOVXconst ops that need to avoid clobbering flags.
   447  	SSAMarkMoves func(*SSAGenState, *ssa.Block)
   448  
   449  	// SSAGenValue emits Prog(s) for the Value.
   450  	SSAGenValue func(*SSAGenState, *ssa.Value)
   451  
   452  	// SSAGenBlock emits end-of-block Progs. SSAGenValue should be called
   453  	// for all values in the block before SSAGenBlock.
   454  	SSAGenBlock func(s *SSAGenState, b, next *ssa.Block)
   455  }
   456  
   457  var pcloc int32
   458  
   459  var Thearch Arch
   460  
   461  var Newproc *Node
   462  
   463  var Deferproc *Node
   464  
   465  var Deferreturn *Node
   466  
   467  var Panicindex *Node
   468  
   469  var panicslice *Node
   470  
   471  var panicdivide *Node
   472  
   473  var throwreturn *Node
   474  
   475  var growslice *Node
   476  
   477  var writebarrierptr *Node
   478  var typedmemmove *Node
   479  
   480  var panicdottype *Node