github.com/rakyll/go@v0.0.0-20170216000551-64c02460d703/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  	"cmd/compile/internal/ssa"
     9  	"cmd/internal/bio"
    10  	"cmd/internal/obj"
    11  	"cmd/internal/src"
    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  	Direct   bool   // imported directly
    27  	Syms     map[string]*Sym
    28  }
    29  
    30  // Sym represents an object name. Most commonly, this is a Go identifier naming
    31  // an object declared within a package, but Syms are also used to name internal
    32  // synthesized objects.
    33  //
    34  // As an exception, field and method names that are exported use the Sym
    35  // associated with localpkg instead of the package that declared them. This
    36  // allows using Sym pointer equality to test for Go identifier uniqueness when
    37  // handling selector expressions.
    38  type Sym struct {
    39  	Flags     SymFlags
    40  	Link      *Sym
    41  	Importdef *Pkg   // where imported definition was found
    42  	Linkname  string // link name
    43  
    44  	// saved and restored by dcopy
    45  	Pkg        *Pkg
    46  	Name       string   // object name
    47  	Def        *Node    // definition: ONAME OTYPE OPACK or OLITERAL
    48  	Block      int32    // blocknumber to catch redeclaration
    49  	Lastlineno src.XPos // last declaration for diagnostic
    50  
    51  	Label   *Node // corresponding label (ephemeral)
    52  	Origpkg *Pkg  // original package for . import
    53  	Lsym    *obj.LSym
    54  	Fsym    *Sym // funcsym
    55  }
    56  
    57  type SymFlags uint8
    58  
    59  const (
    60  	SymExport SymFlags = 1 << iota // to be exported
    61  	SymPackage
    62  	SymExported // already written out by export
    63  	SymUniq
    64  	SymSiggen
    65  	SymAsm
    66  	SymAlgGen
    67  )
    68  
    69  func (sym *Sym) isAlias() bool {
    70  	return sym.Def != nil && sym.Def.Sym != sym
    71  }
    72  
    73  // The Class of a variable/function describes the "storage class"
    74  // of a variable or function. During parsing, storage classes are
    75  // called declaration contexts.
    76  type Class uint8
    77  
    78  const (
    79  	Pxxx      Class = iota
    80  	PEXTERN         // global variable
    81  	PAUTO           // local variables
    82  	PAUTOHEAP       // local variable or parameter moved to heap
    83  	PPARAM          // input arguments
    84  	PPARAMOUT       // output results
    85  	PFUNC           // global function
    86  
    87  	PDISCARD // discard during parse of duplicate import
    88  )
    89  
    90  // note this is the runtime representation
    91  // of the compilers arrays.
    92  //
    93  // typedef	struct
    94  // {				// must not move anything
    95  // 	uchar	array[8];	// pointer to data
    96  // 	uchar	nel[4];		// number of elements
    97  // 	uchar	cap[4];		// allocated number of elements
    98  // } Array;
    99  var array_array int // runtime offsetof(Array,array) - same for String
   100  
   101  var array_nel int // runtime offsetof(Array,nel) - same for String
   102  
   103  var array_cap int // runtime offsetof(Array,cap)
   104  
   105  var sizeof_Array int // runtime sizeof(Array)
   106  
   107  // note this is the runtime representation
   108  // of the compilers strings.
   109  //
   110  // typedef	struct
   111  // {				// must not move anything
   112  // 	uchar	array[8];	// pointer to data
   113  // 	uchar	nel[4];		// number of elements
   114  // } String;
   115  var sizeof_String int // runtime sizeof(String)
   116  
   117  var pragcgobuf string
   118  
   119  var outfile string
   120  var linkobj string
   121  
   122  var bout *bio.Writer
   123  
   124  // nerrors is the number of compiler errors reported
   125  // since the last call to saveerrors.
   126  var nerrors int
   127  
   128  // nsavederrors is the total number of compiler errors
   129  // reported before the last call to saveerrors.
   130  var nsavederrors int
   131  
   132  var nsyntaxerrors int
   133  
   134  var decldepth int32
   135  
   136  var safemode bool
   137  
   138  var nolocalimports bool
   139  
   140  var Debug [256]int
   141  
   142  var debugstr string
   143  
   144  var Debug_checknil int
   145  var Debug_typeassert int
   146  
   147  var localpkg *Pkg // package being compiled
   148  
   149  var importpkg *Pkg // package being imported
   150  
   151  var itabpkg *Pkg // fake pkg for itab entries
   152  
   153  var itablinkpkg *Pkg // fake package for runtime itab entries
   154  
   155  var Runtimepkg *Pkg // package runtime
   156  
   157  var racepkg *Pkg // package runtime/race
   158  
   159  var msanpkg *Pkg // package runtime/msan
   160  
   161  var typepkg *Pkg // fake package for runtime type info (headers)
   162  
   163  var unsafepkg *Pkg // package unsafe
   164  
   165  var trackpkg *Pkg // fake package for field tracking
   166  
   167  var mappkg *Pkg // fake package for map zero value
   168  var zerosize int64
   169  
   170  var Tptr EType // either TPTR32 or TPTR64
   171  
   172  var myimportpath string
   173  
   174  var localimport string
   175  
   176  var asmhdr string
   177  
   178  var simtype [NTYPE]EType
   179  
   180  var (
   181  	isforw    [NTYPE]bool
   182  	isInt     [NTYPE]bool
   183  	isFloat   [NTYPE]bool
   184  	isComplex [NTYPE]bool
   185  	issimple  [NTYPE]bool
   186  )
   187  
   188  var (
   189  	okforeq    [NTYPE]bool
   190  	okforadd   [NTYPE]bool
   191  	okforand   [NTYPE]bool
   192  	okfornone  [NTYPE]bool
   193  	okforcmp   [NTYPE]bool
   194  	okforbool  [NTYPE]bool
   195  	okforcap   [NTYPE]bool
   196  	okforlen   [NTYPE]bool
   197  	okforarith [NTYPE]bool
   198  	okforconst [NTYPE]bool
   199  )
   200  
   201  var (
   202  	okfor [OEND][]bool
   203  	iscmp [OEND]bool
   204  )
   205  
   206  var minintval [NTYPE]*Mpint
   207  
   208  var maxintval [NTYPE]*Mpint
   209  
   210  var minfltval [NTYPE]*Mpflt
   211  
   212  var maxfltval [NTYPE]*Mpflt
   213  
   214  var xtop []*Node
   215  
   216  var exportlist []*Node
   217  
   218  var importlist []*Node // imported functions and methods with inlinable bodies
   219  
   220  var funcsyms []*Node
   221  
   222  var dclcontext Class // PEXTERN/PAUTO
   223  
   224  var statuniqgen int // name generator for static temps
   225  
   226  var Maxarg int64
   227  
   228  var Stksize int64 // stack size for current frame
   229  
   230  var stkptrsize int64 // prefix of stack containing pointers
   231  
   232  var hasdefer bool // flag that curfn has defer statement
   233  
   234  var Curfn *Node
   235  
   236  var Widthptr int
   237  
   238  var Widthint int
   239  
   240  var Widthreg int
   241  
   242  var nblank *Node
   243  
   244  var typecheckok bool
   245  
   246  var compiling_runtime bool
   247  
   248  var compiling_wrappers int
   249  
   250  var use_writebarrier bool
   251  
   252  var pure_go bool
   253  
   254  var flag_installsuffix string
   255  
   256  var flag_race bool
   257  
   258  var flag_msan bool
   259  
   260  var flag_largemodel bool
   261  
   262  // Whether we are adding any sort of code instrumentation, such as
   263  // when the race detector is enabled.
   264  var instrumenting bool
   265  
   266  var debuglive int
   267  
   268  var Ctxt *obj.Link
   269  
   270  var writearchive bool
   271  
   272  var Nacl bool
   273  
   274  var pc *obj.Prog
   275  
   276  var nodfp *Node
   277  
   278  var disable_checknil int
   279  
   280  // interface to back end
   281  
   282  const (
   283  	// Pseudo-op, like TEXT, GLOBL, TYPE, PCDATA, FUNCDATA.
   284  	Pseudo = 1 << 1
   285  
   286  	// There's nothing to say about the instruction,
   287  	// but it's still okay to see.
   288  	OK = 1 << 2
   289  
   290  	// Size of right-side write, or right-side read if no write.
   291  	SizeB = 1 << 3
   292  	SizeW = 1 << 4
   293  	SizeL = 1 << 5
   294  	SizeQ = 1 << 6
   295  	SizeF = 1 << 7
   296  	SizeD = 1 << 8
   297  
   298  	// Left side (Prog.from): address taken, read, write.
   299  	LeftAddr  = 1 << 9
   300  	LeftRead  = 1 << 10
   301  	LeftWrite = 1 << 11
   302  
   303  	// Register in middle (Prog.reg); only ever read. (arm, ppc64)
   304  	RegRead    = 1 << 12
   305  	CanRegRead = 1 << 13
   306  
   307  	// Right side (Prog.to): address taken, read, write.
   308  	RightAddr  = 1 << 14
   309  	RightRead  = 1 << 15
   310  	RightWrite = 1 << 16
   311  
   312  	// Instruction kinds
   313  	Move  = 1 << 17 // straight move
   314  	Conv  = 1 << 18 // size conversion
   315  	Cjmp  = 1 << 19 // conditional jump
   316  	Break = 1 << 20 // breaks control flow (no fallthrough)
   317  	Call  = 1 << 21 // function call
   318  	Jump  = 1 << 22 // jump
   319  	Skip  = 1 << 23 // data instruction
   320  
   321  	// Set, use, or kill of carry bit.
   322  	// Kill means we never look at the carry bit after this kind of instruction.
   323  	// Originally for understanding ADC, RCR, and so on, but now also
   324  	// tracks set, use, and kill of the zero and overflow bits as well.
   325  	// TODO rename to {Set,Use,Kill}Flags
   326  	SetCarry  = 1 << 24
   327  	UseCarry  = 1 << 25
   328  	KillCarry = 1 << 26
   329  
   330  	// Special cases for register use. (amd64, 386)
   331  	ShiftCX  = 1 << 27 // possible shift by CX
   332  	ImulAXDX = 1 << 28 // possible multiply into DX:AX
   333  
   334  	// Instruction updates whichever of from/to is type D_OREG. (ppc64)
   335  	PostInc = 1 << 29
   336  
   337  	// Optional 3rd input operand, only ever read.
   338  	From3Read = 1 << 30
   339  )
   340  
   341  type Arch struct {
   342  	LinkArch *obj.LinkArch
   343  
   344  	REGSP    int
   345  	MAXWIDTH int64
   346  
   347  	Defframe func(*obj.Prog)
   348  	Proginfo func(*obj.Prog) ProgInfo
   349  	Use387   bool // should 8g use 387 FP instructions instead of sse2.
   350  
   351  	// SSAMarkMoves marks any MOVXconst ops that need to avoid clobbering flags.
   352  	SSAMarkMoves func(*SSAGenState, *ssa.Block)
   353  
   354  	// SSAGenValue emits Prog(s) for the Value.
   355  	SSAGenValue func(*SSAGenState, *ssa.Value)
   356  
   357  	// SSAGenBlock emits end-of-block Progs. SSAGenValue should be called
   358  	// for all values in the block before SSAGenBlock.
   359  	SSAGenBlock func(s *SSAGenState, b, next *ssa.Block)
   360  }
   361  
   362  var pcloc int32
   363  
   364  var Thearch Arch
   365  
   366  var (
   367  	staticbytes,
   368  	zerobase *Node
   369  
   370  	Newproc,
   371  	Deferproc,
   372  	Deferreturn,
   373  	Duffcopy,
   374  	Duffzero,
   375  	panicindex,
   376  	panicslice,
   377  	panicdivide,
   378  	growslice,
   379  	panicdottypeE,
   380  	panicdottypeI,
   381  	panicnildottype,
   382  	assertE2I,
   383  	assertE2I2,
   384  	assertI2I,
   385  	assertI2I2 *obj.LSym
   386  )