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
5package gc
6
7import (
8	"cmd/compile/internal/ssa"
9	"cmd/compile/internal/types"
10	"cmd/internal/obj"
11	"cmd/internal/src"
12	"sync"
13)
14
15const (
16	BADWIDTH = types.BADWIDTH
17)
18
19var (
20	// maximum size variable which we will allocate on the stack.
21	// This limit is for explicit variable declarations like "var x T" or "x := ...".
22	// Note: the flag smallframes can update this value.
23	maxStackVarSize = int64(10 * 1024 * 1024)
24
25	// maximum size of implicit variables that we will allocate on the stack.
26	//   p := new(T)          allocating T on the stack
27	//   p := &T{}            allocating T on the stack
28	//   s := make([]T, n)    allocating [n]T on the stack
29	//   s := []byte("...")   allocating [n]byte on the stack
30	// Note: the flag smallframes can update this value.
31	maxImplicitStackVarSize = int64(64 * 1024)
32
33	// smallArrayBytes is the maximum size of an array which is considered small.
34	// Small arrays will be initialized directly with a sequence of constant stores.
35	// Large arrays will be initialized by copying from a static temp.
36	// 256 bytes was chosen to minimize generated code + statictmp size.
37	smallArrayBytes = int64(256)
38)
39
40// isRuntimePkg reports whether p is package runtime.
41func isRuntimePkg(p *types.Pkg) bool {
42	if compiling_runtime && p == localpkg {
43		return true
44	}
45	return p.Path == "runtime"
46}
47
48// The Class of a variable/function describes the "storage class"
49// of a variable or function. During parsing, storage classes are
50// called declaration contexts.
51type Class uint8
52
53//go:generate stringer -type=Class
54const (
55	Pxxx      Class = iota // no class; used during ssa conversion to indicate pseudo-variables
56	PEXTERN                // global variable
57	PAUTO                  // local variables
58	PAUTOHEAP              // local variable or parameter moved to heap
59	PPARAM                 // input arguments
60	PPARAMOUT              // output results
61	PFUNC                  // global function
62
63	// Careful: Class is stored in three bits in Node.flags.
64	_ = uint((1 << 3) - iota) // static assert for iota <= (1 << 3)
65)
66
67// note this is the runtime representation
68// of the compilers slices.
69//
70// typedef	struct
71// {				// must not move anything
72// 	uchar	array[8];	// pointer to data
73// 	uchar	nel[4];		// number of elements
74// 	uchar	cap[4];		// allocated number of elements
75// } Slice;
76var slice_array int // runtime offsetof(Slice,array) - same for String
77
78var slice_nel int // runtime offsetof(Slice,nel) - same for String
79
80var slice_cap int // runtime offsetof(Slice,cap)
81
82var sizeof_Slice int // runtime sizeof(Slice)
83
84// note this is the runtime representation
85// of the compilers strings.
86//
87// typedef	struct
88// {				// must not move anything
89// 	uchar	array[8];	// pointer to data
90// 	uchar	nel[4];		// number of elements
91// } String;
92var sizeof_String int // runtime sizeof(String)
93
94var pragcgobuf [][]string
95
96var outfile string
97var linkobj string
98
99// nerrors is the number of compiler errors reported
100// since the last call to saveerrors.
101var nerrors int
102
103// nsavederrors is the total number of compiler errors
104// reported before the last call to saveerrors.
105var nsavederrors int
106
107var nsyntaxerrors int
108
109var decldepth int32
110
111var nolocalimports bool
112
113var Debug [256]int
114
115var debugstr string
116
117var Debug_checknil int
118var Debug_typeassert int
119
120var localpkg *types.Pkg // package being compiled
121
122var inimport bool // set during import
123
124var itabpkg *types.Pkg // fake pkg for itab entries
125
126var itablinkpkg *types.Pkg // fake package for runtime itab entries
127
128var Runtimepkg *types.Pkg // fake package runtime
129
130var racepkg *types.Pkg // package runtime/race
131
132var msanpkg *types.Pkg // package runtime/msan
133
134var unsafepkg *types.Pkg // package unsafe
135
136var trackpkg *types.Pkg // fake package for field tracking
137
138var mappkg *types.Pkg // fake package for map zero value
139
140var gopkg *types.Pkg // pseudo-package for method symbols on anonymous receiver types
141
142var zerosize int64
143
144var myimportpath string
145
146var localimport string
147
148var asmhdr string
149
150var simtype [NTYPE]types.EType
151
152var (
153	isInt     [NTYPE]bool
154	isFloat   [NTYPE]bool
155	isComplex [NTYPE]bool
156	issimple  [NTYPE]bool
157)
158
159var (
160	okforeq    [NTYPE]bool
161	okforadd   [NTYPE]bool
162	okforand   [NTYPE]bool
163	okfornone  [NTYPE]bool
164	okforcmp   [NTYPE]bool
165	okforbool  [NTYPE]bool
166	okforcap   [NTYPE]bool
167	okforlen   [NTYPE]bool
168	okforarith [NTYPE]bool
169	okforconst [NTYPE]bool
170)
171
172var (
173	okfor [OEND][]bool
174	iscmp [OEND]bool
175)
176
177var minintval [NTYPE]*Mpint
178
179var maxintval [NTYPE]*Mpint
180
181var minfltval [NTYPE]*Mpflt
182
183var maxfltval [NTYPE]*Mpflt
184
185var xtop []*Node
186
187var exportlist []*Node
188
189var importlist []*Node // imported functions and methods with inlinable bodies
190
191var (
192	funcsymsmu sync.Mutex // protects funcsyms and associated package lookups (see func funcsym)
193	funcsyms   []*types.Sym
194)
195
196var dclcontext Class // PEXTERN/PAUTO
197
198var Curfn *Node
199
200var Widthptr int
201
202var Widthreg int
203
204var nblank *Node
205
206var typecheckok bool
207
208var compiling_runtime bool
209
210// Compiling the standard library
211var compiling_std bool
212
213var use_writebarrier bool
214
215var pure_go bool
216
217var flag_installsuffix string
218
219var flag_race bool
220
221var flag_msan bool
222
223var flagDWARF bool
224
225// Whether we are adding any sort of code instrumentation, such as
226// when the race detector is enabled.
227var instrumenting bool
228
229// Whether we are tracking lexical scopes for DWARF.
230var trackScopes bool
231
232// Controls generation of DWARF inlined instance records. Zero
233// disables, 1 emits inlined routines but suppresses var info,
234// and 2 emits inlined routines with tracking of formals/locals.
235var genDwarfInline int
236
237var debuglive int
238
239var Ctxt *obj.Link
240
241var writearchive bool
242
243var nodfp *Node
244
245var disable_checknil int
246
247var autogeneratedPos src.XPos
248
249// interface to back end
250
251type Arch struct {
252	LinkArch *obj.LinkArch
253
254	REGSP     int
255	MAXWIDTH  int64
256	Use387    bool // should 386 backend use 387 FP instructions instead of sse2.
257	SoftFloat bool
258
259	PadFrame func(int64) int64
260
261	// ZeroRange zeroes a range of memory on stack. It is only inserted
262	// at function entry, and it is ok to clobber registers.
263	ZeroRange func(*Progs, *obj.Prog, int64, int64, *uint32) *obj.Prog
264
265	Ginsnop      func(*Progs) *obj.Prog
266	Ginsnopdefer func(*Progs) *obj.Prog // special ginsnop for deferreturn
267
268	// SSAMarkMoves marks any MOVXconst ops that need to avoid clobbering flags.
269	SSAMarkMoves func(*SSAGenState, *ssa.Block)
270
271	// SSAGenValue emits Prog(s) for the Value.
272	SSAGenValue func(*SSAGenState, *ssa.Value)
273
274	// SSAGenBlock emits end-of-block Progs. SSAGenValue should be called
275	// for all values in the block before SSAGenBlock.
276	SSAGenBlock func(s *SSAGenState, b, next *ssa.Block)
277}
278
279var thearch Arch
280
281var (
282	staticbytes,
283	zerobase *Node
284
285	assertE2I,
286	assertE2I2,
287	assertI2I,
288	assertI2I2,
289	deferproc,
290	deferprocStack,
291	Deferreturn,
292	Duffcopy,
293	Duffzero,
294	gcWriteBarrier,
295	goschedguarded,
296	growslice,
297	msanread,
298	msanwrite,
299	newobject,
300	newproc,
301	panicdivide,
302	panicshift,
303	panicdottypeE,
304	panicdottypeI,
305	panicnildottype,
306	panicoverflow,
307	raceread,
308	racereadrange,
309	racewrite,
310	racewriterange,
311	x86HasPOPCNT,
312	x86HasSSE41,
313	x86HasFMA,
314	armHasVFPv4,
315	arm64HasATOMICS,
316	typedmemclr,
317	typedmemmove,
318	Udiv,
319	writeBarrier,
320	zerobaseSym *obj.LSym
321
322	BoundsCheckFunc [ssa.BoundsKindCount]*obj.LSym
323	ExtendCheckFunc [ssa.BoundsKindCount]*obj.LSym
324
325	// GO386=387
326	ControlWord64trunc,
327	ControlWord32 *obj.LSym
328
329	// Wasm
330	WasmMove,
331	WasmZero,
332	WasmDiv,
333	WasmTruncS,
334	WasmTruncU,
335	SigPanic *obj.LSym
336)
337