blob: ccf2a86bb3f1419f5c9023e337bfb41c5066904a [file] [log] [blame]
Russ Cox8c195bd2015-02-13 14:40:36 -05001// 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
5package gc
6
7import (
Michael Pratta4e31d42016-03-12 14:07:40 -08008 "cmd/compile/internal/ssa"
Matthew Dempsky4b7e36c2016-04-06 21:45:29 -07009 "cmd/internal/bio"
Russ Cox8c195bd2015-02-13 14:40:36 -050010 "cmd/internal/obj"
Russ Cox8c195bd2015-02-13 14:40:36 -050011)
12
Russ Cox8c195bd2015-02-13 14:40:36 -050013const (
Rob Pike24a43e62015-03-05 10:39:23 -080014 UINF = 100
Rob Pike24a43e62015-03-05 10:39:23 -080015 BADWIDTH = -1000000000
16 MaxStackVarSize = 10 * 1024 * 1024
17)
18
Russ Cox8c195bd2015-02-13 14:40:36 -050019type Pkg struct {
David Chase8eec2bb2016-03-11 00:10:52 -050020 Name string // package name, e.g. "sys"
21 Path string // string literal used in import statement, e.g. "runtime/internal/sys"
David Crawshawf1209362016-03-31 10:02:10 -040022 Pathsym *obj.LSym
Russ Coxcdb7d7d2015-03-05 13:57:36 -050023 Prefix string // escaped path for use in symbol table
Marvin Stengere03c7892015-09-08 05:46:31 +020024 Imported bool // export data of this package was parsed
Marvin Stengere03c7892015-09-08 05:46:31 +020025 Direct bool // imported directly
Russ Coxd0b59de2015-03-02 16:21:15 -050026 Syms map[string]*Sym
Russ Cox8c195bd2015-02-13 14:40:36 -050027}
28
Matthew Dempskycca4ddb2016-04-05 12:48:49 -070029// Sym represents an object name. Most commonly, this is a Go identifier naming
30// an object declared within a package, but Syms are also used to name internal
31// synthesized objects.
32//
33// As a special exception, field and method names that are exported use the Sym
34// associated with localpkg instead of the package that declared them. This
35// allows using Sym pointer equality to test for Go identifier uniqueness when
36// handling selector expressions.
Russ Cox8c195bd2015-02-13 14:40:36 -050037type Sym struct {
Robert Griesemer37c29722016-03-01 16:37:20 -080038 Flags SymFlags
Matthew Dempsky071e43a2016-02-26 01:37:28 -080039 Link *Sym
Russ Coxcdb7d7d2015-03-05 13:57:36 -050040 Importdef *Pkg // where imported definition was found
41 Linkname string // link name
42
43 // saved and restored by dcopy
Russ Cox8c195bd2015-02-13 14:40:36 -050044 Pkg *Pkg
Russ Coxcdb7d7d2015-03-05 13:57:36 -050045 Name string // variable name
46 Def *Node // definition: ONAME OTYPE OPACK or OLITERAL
Russ Coxcdb7d7d2015-03-05 13:57:36 -050047 Block int32 // blocknumber to catch redeclaration
48 Lastlineno int32 // last declaration for diagnostic
Robert Griesemer157f0692016-03-10 15:07:08 -080049
50 Label *Label // corresponding label (ephemeral)
51 Origpkg *Pkg // original package for . import
52 Lsym *obj.LSym
53 Fsym *Sym // funcsym
Russ Cox8c195bd2015-02-13 14:40:36 -050054}
55
Russ Cox8c195bd2015-02-13 14:40:36 -050056type Label struct {
Robert Griesemerdef9c0b2016-03-10 20:35:27 -080057 Def *Node
Russ Cox8c195bd2015-02-13 14:40:36 -050058}
59
Robert Griesemer37c29722016-03-01 16:37:20 -080060type SymFlags uint8
61
Russ Cox8c195bd2015-02-13 14:40:36 -050062const (
Robert Griesemer37c29722016-03-01 16:37:20 -080063 SymExport SymFlags = 1 << iota // to be exported
64 SymPackage
65 SymExported // already written out by export
66 SymUniq
67 SymSiggen
68 SymAsm
69 SymAlgGen
Russ Cox8c195bd2015-02-13 14:40:36 -050070)
71
Robert Griesemercd7d7382015-10-26 14:57:36 -070072// The Class of a variable/function describes the "storage class"
73// of a variable or function. During parsing, storage classes are
74// called declaration contexts.
75type Class uint8
76
Russ Cox8c195bd2015-02-13 14:40:36 -050077const (
Robert Griesemercd7d7382015-10-26 14:57:36 -070078 Pxxx Class = iota
79 PEXTERN // global variable
80 PAUTO // local variables
Russ Coxb6dc3e62016-05-25 01:33:24 -040081 PAUTOHEAP // local variable or parameter moved to heap
Robert Griesemercd7d7382015-10-26 14:57:36 -070082 PPARAM // input arguments
83 PPARAMOUT // output results
Robert Griesemercd7d7382015-10-26 14:57:36 -070084 PFUNC // global function
Russ Coxcdb7d7d2015-03-05 13:57:36 -050085
86 PDISCARD // discard during parse of duplicate import
Russ Cox8c195bd2015-02-13 14:40:36 -050087)
88
Jeremy Jackins6327e8d2015-10-22 09:51:12 +090089// note this is the runtime representation
90// of the compilers arrays.
91//
92// typedef struct
93// { // must not move anything
94// uchar array[8]; // pointer to data
95// uchar nel[4]; // number of elements
96// uchar cap[4]; // allocated number of elements
97// } Array;
Dave Cheney584978f2016-09-16 00:33:29 +100098var array_array int // runtime offsetof(Array,array) - same for String
Russ Cox8c195bd2015-02-13 14:40:36 -050099
Dave Cheney584978f2016-09-16 00:33:29 +1000100var array_nel int // runtime offsetof(Array,nel) - same for String
Russ Cox8c195bd2015-02-13 14:40:36 -0500101
Dave Cheney584978f2016-09-16 00:33:29 +1000102var array_cap int // runtime offsetof(Array,cap)
Russ Cox8c195bd2015-02-13 14:40:36 -0500103
104var sizeof_Array int // runtime sizeof(Array)
105
Jeremy Jackins6327e8d2015-10-22 09:51:12 +0900106// note this is the runtime representation
107// of the compilers strings.
108//
109// typedef struct
110// { // must not move anything
111// uchar array[8]; // pointer to data
112// uchar nel[4]; // number of elements
113// } String;
Russ Cox8c195bd2015-02-13 14:40:36 -0500114var sizeof_String int // runtime sizeof(String)
115
Russ Cox8c195bd2015-02-13 14:40:36 -0500116var pragcgobuf string
117
118var infile string
119
120var outfile string
Russ Coxfeb61312016-04-26 21:50:59 -0400121var linkobj string
Russ Cox8c195bd2015-02-13 14:40:36 -0500122
Dave Cheney8f2edf12016-04-08 19:14:03 +1000123var bout *bio.Writer
Russ Cox8c195bd2015-02-13 14:40:36 -0500124
125var nerrors int
126
127var nsavederrors int
128
129var nsyntaxerrors int
130
Josh Bleecher Snyder2b063bd2015-05-14 19:33:31 -0700131var decldepth int32
Russ Cox8c195bd2015-02-13 14:40:36 -0500132
Matthew Dempsky980ab122016-04-13 18:37:18 -0700133var safemode bool
Russ Cox8c195bd2015-02-13 14:40:36 -0500134
Matthew Dempsky980ab122016-04-13 18:37:18 -0700135var nolocalimports bool
Russ Cox8c195bd2015-02-13 14:40:36 -0500136
Russ Cox8c195bd2015-02-13 14:40:36 -0500137var Debug [256]int
138
139var debugstr string
140
141var Debug_checknil int
Russ Cox4224d812015-03-20 00:06:10 -0400142var Debug_typeassert int
Russ Cox8c195bd2015-02-13 14:40:36 -0500143
Russ Cox8c195bd2015-02-13 14:40:36 -0500144var localpkg *Pkg // package being compiled
145
146var importpkg *Pkg // package being imported
147
Michel Lespinasse859b63c2016-03-18 17:21:33 -0700148var itabpkg *Pkg // fake pkg for itab entries
Michel Lespinassef00bbd52016-03-17 06:18:13 -0700149
150var itablinkpkg *Pkg // fake package for runtime itab entries
151
Russ Cox8c195bd2015-02-13 14:40:36 -0500152var Runtimepkg *Pkg // package runtime
153
154var racepkg *Pkg // package runtime/race
155
Ian Lance Taylor0c69f132015-10-21 07:04:10 -0700156var msanpkg *Pkg // package runtime/msan
157
Russ Cox8c195bd2015-02-13 14:40:36 -0500158var typepkg *Pkg // fake package for runtime type info (headers)
159
Russ Cox8c195bd2015-02-13 14:40:36 -0500160var unsafepkg *Pkg // package unsafe
161
162var trackpkg *Pkg // fake package for field tracking
163
Keith Randall60fd32a2016-04-19 08:31:04 -0700164var mappkg *Pkg // fake package for map zero value
165var zerosize int64
166
Marvin Stenger8e7a3ea2015-09-24 23:21:18 +0200167var Tptr EType // either TPTR32 or TPTR64
Russ Cox8c195bd2015-02-13 14:40:36 -0500168
169var myimportpath string
170
Russ Cox8c195bd2015-02-13 14:40:36 -0500171var localimport string
172
173var asmhdr string
174
Dave Cheney584978f2016-09-16 00:33:29 +1000175var simtype [NTYPE]EType
Russ Cox8c195bd2015-02-13 14:40:36 -0500176
Josh Bleecher Snyder25da5942015-03-01 07:54:01 +0000177var (
Josh Bleecher Snyder25da5942015-03-01 07:54:01 +0000178 isforw [NTYPE]bool
Dave Cheney584978f2016-09-16 00:33:29 +1000179 isInt [NTYPE]bool
180 isFloat [NTYPE]bool
181 isComplex [NTYPE]bool
Josh Bleecher Snyder25da5942015-03-01 07:54:01 +0000182 issimple [NTYPE]bool
183)
Russ Cox8c195bd2015-02-13 14:40:36 -0500184
Josh Bleecher Snyder25da5942015-03-01 07:54:01 +0000185var (
186 okforeq [NTYPE]bool
187 okforadd [NTYPE]bool
188 okforand [NTYPE]bool
189 okfornone [NTYPE]bool
190 okforcmp [NTYPE]bool
191 okforbool [NTYPE]bool
192 okforcap [NTYPE]bool
193 okforlen [NTYPE]bool
194 okforarith [NTYPE]bool
195 okforconst [NTYPE]bool
196)
Russ Cox8c195bd2015-02-13 14:40:36 -0500197
Josh Bleecher Snyder25da5942015-03-01 07:54:01 +0000198var (
199 okfor [OEND][]bool
200 iscmp [OEND]bool
201)
Russ Cox8c195bd2015-02-13 14:40:36 -0500202
Dave Cheney584978f2016-09-16 00:33:29 +1000203var minintval [NTYPE]*Mpint
Russ Cox8c195bd2015-02-13 14:40:36 -0500204
Dave Cheney584978f2016-09-16 00:33:29 +1000205var maxintval [NTYPE]*Mpint
Russ Cox8c195bd2015-02-13 14:40:36 -0500206
207var minfltval [NTYPE]*Mpflt
208
209var maxfltval [NTYPE]*Mpflt
210
Ian Lance Taylorf444b8a2016-03-09 20:29:21 -0800211var xtop []*Node
Russ Cox8c195bd2015-02-13 14:40:36 -0500212
Robert Griesemer3d3bc882015-08-12 14:29:50 -0700213var exportlist []*Node
Russ Cox8c195bd2015-02-13 14:40:36 -0500214
Håvard Haugen391cc542015-09-06 22:38:49 +0200215var importlist []*Node // imported functions and methods with inlinable bodies
Russ Cox8c195bd2015-02-13 14:40:36 -0500216
Robert Griesemera2119ac2015-10-05 16:33:53 -0700217var funcsyms []*Node
Russ Cox8c195bd2015-02-13 14:40:36 -0500218
Robert Griesemercd7d7382015-10-26 14:57:36 -0700219var dclcontext Class // PEXTERN/PAUTO
Russ Cox8c195bd2015-02-13 14:40:36 -0500220
Russ Cox8c195bd2015-02-13 14:40:36 -0500221var statuniqgen int // name generator for static temps
222
Russ Cox8c195bd2015-02-13 14:40:36 -0500223var iota_ int32
224
Ian Lance Taylorf444b8a2016-03-09 20:29:21 -0800225var lastconst []*Node
Russ Cox8c195bd2015-02-13 14:40:36 -0500226
227var lasttype *Node
228
229var Maxarg int64
230
231var Stksize int64 // stack size for current frame
232
233var stkptrsize int64 // prefix of stack containing pointers
234
Håvard Haugen25946642015-09-07 22:19:30 +0200235var hasdefer bool // flag that curfn has defer statement
Russ Cox8c195bd2015-02-13 14:40:36 -0500236
237var Curfn *Node
238
239var Widthptr int
240
241var Widthint int
242
243var Widthreg int
244
Russ Cox8c195bd2015-02-13 14:40:36 -0500245var nblank *Node
246
Håvard Haugendc3540d2015-08-30 23:56:40 +0200247var typecheckok bool
Russ Cox8c195bd2015-02-13 14:40:36 -0500248
Matthew Dempsky980ab122016-04-13 18:37:18 -0700249var compiling_runtime bool
Russ Cox8c195bd2015-02-13 14:40:36 -0500250
251var compiling_wrappers int
252
Matthew Dempsky980ab122016-04-13 18:37:18 -0700253var use_writebarrier bool
Russ Cox8c195bd2015-02-13 14:40:36 -0500254
Matthew Dempsky980ab122016-04-13 18:37:18 -0700255var pure_go bool
Russ Cox8c195bd2015-02-13 14:40:36 -0500256
257var flag_installsuffix string
258
Matthew Dempsky980ab122016-04-13 18:37:18 -0700259var flag_race bool
Russ Cox8c195bd2015-02-13 14:40:36 -0500260
Matthew Dempsky980ab122016-04-13 18:37:18 -0700261var flag_msan bool
Ian Lance Taylor0c69f132015-10-21 07:04:10 -0700262
Matthew Dempsky980ab122016-04-13 18:37:18 -0700263var flag_largemodel bool
Russ Cox8c195bd2015-02-13 14:40:36 -0500264
Ian Lance Taylor9e902f02015-10-20 10:00:07 -0700265// Whether we are adding any sort of code instrumentation, such as
266// when the race detector is enabled.
267var instrumenting bool
268
Russ Cox8c195bd2015-02-13 14:40:36 -0500269var debuglive int
270
271var Ctxt *obj.Link
272
Matthew Dempsky980ab122016-04-13 18:37:18 -0700273var writearchive bool
Russ Cox8c195bd2015-02-13 14:40:36 -0500274
Russ Cox8c195bd2015-02-13 14:40:36 -0500275var Nacl bool
276
Russ Cox8c195bd2015-02-13 14:40:36 -0500277var Pc *obj.Prog
278
Russ Cox8c195bd2015-02-13 14:40:36 -0500279var nodfp *Node
280
Dave Cheney584978f2016-09-16 00:33:29 +1000281var disable_checknil int
Russ Cox8c195bd2015-02-13 14:40:36 -0500282
Jeremy Jackins6327e8d2015-10-22 09:51:12 +0900283// interface to back end
Russ Cox8c195bd2015-02-13 14:40:36 -0500284
285const (
Russ Coxcdb7d7d2015-03-05 13:57:36 -0500286 // Pseudo-op, like TEXT, GLOBL, TYPE, PCDATA, FUNCDATA.
287 Pseudo = 1 << 1
288
289 // There's nothing to say about the instruction,
290 // but it's still okay to see.
291 OK = 1 << 2
292
293 // Size of right-side write, or right-side read if no write.
294 SizeB = 1 << 3
295 SizeW = 1 << 4
296 SizeL = 1 << 5
297 SizeQ = 1 << 6
298 SizeF = 1 << 7
299 SizeD = 1 << 8
300
301 // Left side (Prog.from): address taken, read, write.
302 LeftAddr = 1 << 9
303 LeftRead = 1 << 10
304 LeftWrite = 1 << 11
305
306 // Register in middle (Prog.reg); only ever read. (arm, ppc64)
Russ Cox8c195bd2015-02-13 14:40:36 -0500307 RegRead = 1 << 12
308 CanRegRead = 1 << 13
Russ Coxcdb7d7d2015-03-05 13:57:36 -0500309
310 // Right side (Prog.to): address taken, read, write.
Russ Cox8c195bd2015-02-13 14:40:36 -0500311 RightAddr = 1 << 14
312 RightRead = 1 << 15
313 RightWrite = 1 << 16
Russ Coxcdb7d7d2015-03-05 13:57:36 -0500314
315 // Instruction kinds
316 Move = 1 << 17 // straight move
317 Conv = 1 << 18 // size conversion
318 Cjmp = 1 << 19 // conditional jump
319 Break = 1 << 20 // breaks control flow (no fallthrough)
320 Call = 1 << 21 // function call
321 Jump = 1 << 22 // jump
322 Skip = 1 << 23 // data instruction
323
324 // Set, use, or kill of carry bit.
325 // Kill means we never look at the carry bit after this kind of instruction.
David Chase8eec2bb2016-03-11 00:10:52 -0500326 // Originally for understanding ADC, RCR, and so on, but now also
327 // tracks set, use, and kill of the zero and overflow bits as well.
328 // TODO rename to {Set,Use,Kill}Flags
Russ Coxcdb7d7d2015-03-05 13:57:36 -0500329 SetCarry = 1 << 24
330 UseCarry = 1 << 25
331 KillCarry = 1 << 26
332
333 // Special cases for register use. (amd64, 386)
334 ShiftCX = 1 << 27 // possible shift by CX
335 ImulAXDX = 1 << 28 // possible multiply into DX:AX
336
337 // Instruction updates whichever of from/to is type D_OREG. (ppc64)
338 PostInc = 1 << 29
Russ Cox8c195bd2015-02-13 14:40:36 -0500339)
340
341type Arch struct {
Matthew Dempskyc6e11fe2016-04-06 12:01:40 -0700342 LinkArch *obj.LinkArch
343
Matthew Dempsky1f2930c2016-09-15 16:33:11 -0700344 REGSP int
Matthew Dempsky1f2930c2016-09-15 16:33:11 -0700345 MAXWIDTH int64
Russ Coxb115c352015-03-18 17:26:36 -0400346
Matthew Dempsky544010a2016-09-15 16:37:17 -0700347 Defframe func(*obj.Prog)
348 Proginfo func(*obj.Prog) // fills in Prog.Info
349 Use387 bool // should 8g use 387 FP instructions instead of sse2.
Michael Pratta4e31d42016-03-12 14:07:40 -0800350
Michael Pratta4e31d42016-03-12 14:07:40 -0800351 // 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)
Russ Cox8c195bd2015-02-13 14:40:36 -0500360}
361
362var pcloc int32
363
364var Thearch Arch
365
366var Newproc *Node
367
368var Deferproc *Node
369
370var Deferreturn *Node
371
Dave Cheney584978f2016-09-16 00:33:29 +1000372var panicindex *Node
Russ Cox8c195bd2015-02-13 14:40:36 -0500373
374var panicslice *Node
375
David Chase18559e22015-10-28 13:55:46 -0400376var panicdivide *Node
377
Keith Randall8c5bfcc2015-09-18 15:11:30 -0700378var growslice *Node
379
Keith Randall5ba31942016-01-25 17:06:54 -0800380var writebarrierptr *Node
381var typedmemmove *Node
Keith Randall8c5bfcc2015-09-18 15:11:30 -0700382
383var panicdottype *Node