aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/gc/universe.go
blob: cd68719a9978aa513848382017bff32a06b0972a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// TODO(gri) This file should probably become part of package types.

package gc

import (
	"cmd/compile/internal/base"
	"cmd/compile/internal/ir"
	"cmd/compile/internal/types"
	"cmd/internal/src"
)

var basicTypes = [...]struct {
	name  string
	etype types.Kind
}{
	{"int8", types.TINT8},
	{"int16", types.TINT16},
	{"int32", types.TINT32},
	{"int64", types.TINT64},
	{"uint8", types.TUINT8},
	{"uint16", types.TUINT16},
	{"uint32", types.TUINT32},
	{"uint64", types.TUINT64},
	{"float32", types.TFLOAT32},
	{"float64", types.TFLOAT64},
	{"complex64", types.TCOMPLEX64},
	{"complex128", types.TCOMPLEX128},
	{"bool", types.TBOOL},
	{"string", types.TSTRING},
}

var typedefs = [...]struct {
	name     string
	etype    types.Kind
	sameas32 types.Kind
	sameas64 types.Kind
}{
	{"int", types.TINT, types.TINT32, types.TINT64},
	{"uint", types.TUINT, types.TUINT32, types.TUINT64},
	{"uintptr", types.TUINTPTR, types.TUINT32, types.TUINT64},
}

var builtinFuncs = [...]struct {
	name string
	op   ir.Op
}{
	{"append", ir.OAPPEND},
	{"cap", ir.OCAP},
	{"close", ir.OCLOSE},
	{"complex", ir.OCOMPLEX},
	{"copy", ir.OCOPY},
	{"delete", ir.ODELETE},
	{"imag", ir.OIMAG},
	{"len", ir.OLEN},
	{"make", ir.OMAKE},
	{"new", ir.ONEW},
	{"panic", ir.OPANIC},
	{"print", ir.OPRINT},
	{"println", ir.OPRINTN},
	{"real", ir.OREAL},
	{"recover", ir.ORECOVER},
}

// isBuiltinFuncName reports whether name matches a builtin function
// name.
func isBuiltinFuncName(name string) bool {
	for _, fn := range &builtinFuncs {
		if fn.name == name {
			return true
		}
	}
	return false
}

var unsafeFuncs = [...]struct {
	name string
	op   ir.Op
}{
	{"Alignof", ir.OALIGNOF},
	{"Offsetof", ir.OOFFSETOF},
	{"Sizeof", ir.OSIZEOF},
}

// initUniverse initializes the universe block.
func initUniverse() {
	if Widthptr == 0 {
		base.Fatalf("typeinit before betypeinit")
	}

	slicePtrOffset = 0
	sliceLenOffset = Rnd(slicePtrOffset+int64(Widthptr), int64(Widthptr))
	sliceCapOffset = Rnd(sliceLenOffset+int64(Widthptr), int64(Widthptr))
	sizeofSlice = Rnd(sliceCapOffset+int64(Widthptr), int64(Widthptr))

	// string is same as slice wo the cap
	sizeofString = Rnd(sliceLenOffset+int64(Widthptr), int64(Widthptr))

	for et := types.Kind(0); et < types.NTYPE; et++ {
		simtype[et] = et
	}

	types.Types[types.TANY] = types.New(types.TANY)
	types.Types[types.TINTER] = types.NewInterface(ir.LocalPkg, nil)

	defBasic := func(kind types.Kind, pkg *types.Pkg, name string) *types.Type {
		sym := pkg.Lookup(name)
		n := ir.NewDeclNameAt(src.NoXPos, sym)
		n.SetOp(ir.OTYPE)
		t := types.NewBasic(kind, n)
		n.SetType(t)
		sym.Def = n
		if kind != types.TANY {
			dowidth(t)
		}
		return t
	}

	for _, s := range &basicTypes {
		types.Types[s.etype] = defBasic(s.etype, ir.BuiltinPkg, s.name)
	}

	for _, s := range &typedefs {
		sameas := s.sameas32
		if Widthptr == 8 {
			sameas = s.sameas64
		}
		simtype[s.etype] = sameas

		types.Types[s.etype] = defBasic(s.etype, ir.BuiltinPkg, s.name)
	}

	// We create separate byte and rune types for better error messages
	// rather than just creating type alias *types.Sym's for the uint8 and
	// int32 types. Hence, (bytetype|runtype).Sym.isAlias() is false.
	// TODO(gri) Should we get rid of this special case (at the cost
	// of less informative error messages involving bytes and runes)?
	// (Alternatively, we could introduce an OTALIAS node representing
	// type aliases, albeit at the cost of having to deal with it everywhere).
	types.ByteType = defBasic(types.TUINT8, ir.BuiltinPkg, "byte")
	types.RuneType = defBasic(types.TINT32, ir.BuiltinPkg, "rune")

	// error type
	s := ir.BuiltinPkg.Lookup("error")
	n := ir.NewDeclNameAt(src.NoXPos, s)
	n.SetOp(ir.OTYPE)
	types.ErrorType = types.NewNamed(n)
	types.ErrorType.SetUnderlying(makeErrorInterface())
	n.SetType(types.ErrorType)
	s.Def = n
	dowidth(types.ErrorType)

	types.Types[types.TUNSAFEPTR] = defBasic(types.TUNSAFEPTR, unsafepkg, "Pointer")

	// simple aliases
	simtype[types.TMAP] = types.TPTR
	simtype[types.TCHAN] = types.TPTR
	simtype[types.TFUNC] = types.TPTR
	simtype[types.TUNSAFEPTR] = types.TPTR

	for _, s := range &builtinFuncs {
		s2 := ir.BuiltinPkg.Lookup(s.name)
		s2.Def = NewName(s2)
		ir.AsNode(s2.Def).SetSubOp(s.op)
	}

	for _, s := range &unsafeFuncs {
		s2 := unsafepkg.Lookup(s.name)
		s2.Def = NewName(s2)
		ir.AsNode(s2.Def).SetSubOp(s.op)
	}

	s = ir.BuiltinPkg.Lookup("true")
	s.Def = nodbool(true)
	ir.AsNode(s.Def).SetSym(lookup("true"))

	s = ir.BuiltinPkg.Lookup("false")
	s.Def = nodbool(false)
	ir.AsNode(s.Def).SetSym(lookup("false"))

	s = lookup("_")
	ir.BlankSym = s
	s.Block = -100
	s.Def = NewName(s)
	types.Types[types.TBLANK] = types.New(types.TBLANK)
	ir.AsNode(s.Def).SetType(types.Types[types.TBLANK])
	ir.BlankNode = ir.AsNode(s.Def)
	ir.BlankNode.SetTypecheck(1)

	s = ir.BuiltinPkg.Lookup("_")
	s.Block = -100
	s.Def = NewName(s)
	types.Types[types.TBLANK] = types.New(types.TBLANK)
	ir.AsNode(s.Def).SetType(types.Types[types.TBLANK])

	types.Types[types.TNIL] = types.New(types.TNIL)
	s = ir.BuiltinPkg.Lookup("nil")
	s.Def = nodnil()
	ir.AsNode(s.Def).SetSym(s)

	s = ir.BuiltinPkg.Lookup("iota")
	s.Def = ir.Nod(ir.OIOTA, nil, nil)
	ir.AsNode(s.Def).SetSym(s)

	for et := types.TINT8; et <= types.TUINT64; et++ {
		isInt[et] = true
	}
	isInt[types.TINT] = true
	isInt[types.TUINT] = true
	isInt[types.TUINTPTR] = true

	isFloat[types.TFLOAT32] = true
	isFloat[types.TFLOAT64] = true

	isComplex[types.TCOMPLEX64] = true
	isComplex[types.TCOMPLEX128] = true

	// initialize okfor
	for et := types.Kind(0); et < types.NTYPE; et++ {
		if isInt[et] || et == types.TIDEAL {
			okforeq[et] = true
			okforcmp[et] = true
			okforarith[et] = true
			okforadd[et] = true
			okforand[et] = true
			ir.OKForConst[et] = true
			issimple[et] = true
		}

		if isFloat[et] {
			okforeq[et] = true
			okforcmp[et] = true
			okforadd[et] = true
			okforarith[et] = true
			ir.OKForConst[et] = true
			issimple[et] = true
		}

		if isComplex[et] {
			okforeq[et] = true
			okforadd[et] = true
			okforarith[et] = true
			ir.OKForConst[et] = true
			issimple[et] = true
		}
	}

	issimple[types.TBOOL] = true

	okforadd[types.TSTRING] = true

	okforbool[types.TBOOL] = true

	okforcap[types.TARRAY] = true
	okforcap[types.TCHAN] = true
	okforcap[types.TSLICE] = true

	ir.OKForConst[types.TBOOL] = true
	ir.OKForConst[types.TSTRING] = true

	okforlen[types.TARRAY] = true
	okforlen[types.TCHAN] = true
	okforlen[types.TMAP] = true
	okforlen[types.TSLICE] = true
	okforlen[types.TSTRING] = true

	okforeq[types.TPTR] = true
	okforeq[types.TUNSAFEPTR] = true
	okforeq[types.TINTER] = true
	okforeq[types.TCHAN] = true
	okforeq[types.TSTRING] = true
	okforeq[types.TBOOL] = true
	okforeq[types.TMAP] = true    // nil only; refined in typecheck
	okforeq[types.TFUNC] = true   // nil only; refined in typecheck
	okforeq[types.TSLICE] = true  // nil only; refined in typecheck
	okforeq[types.TARRAY] = true  // only if element type is comparable; refined in typecheck
	okforeq[types.TSTRUCT] = true // only if all struct fields are comparable; refined in typecheck

	okforcmp[types.TSTRING] = true

	for i := range okfor {
		okfor[i] = okfornone[:]
	}

	// binary
	okfor[ir.OADD] = okforadd[:]
	okfor[ir.OAND] = okforand[:]
	okfor[ir.OANDAND] = okforbool[:]
	okfor[ir.OANDNOT] = okforand[:]
	okfor[ir.ODIV] = okforarith[:]
	okfor[ir.OEQ] = okforeq[:]
	okfor[ir.OGE] = okforcmp[:]
	okfor[ir.OGT] = okforcmp[:]
	okfor[ir.OLE] = okforcmp[:]
	okfor[ir.OLT] = okforcmp[:]
	okfor[ir.OMOD] = okforand[:]
	okfor[ir.OMUL] = okforarith[:]
	okfor[ir.ONE] = okforeq[:]
	okfor[ir.OOR] = okforand[:]
	okfor[ir.OOROR] = okforbool[:]
	okfor[ir.OSUB] = okforarith[:]
	okfor[ir.OXOR] = okforand[:]
	okfor[ir.OLSH] = okforand[:]
	okfor[ir.ORSH] = okforand[:]

	// unary
	okfor[ir.OBITNOT] = okforand[:]
	okfor[ir.ONEG] = okforarith[:]
	okfor[ir.ONOT] = okforbool[:]
	okfor[ir.OPLUS] = okforarith[:]

	// special
	okfor[ir.OCAP] = okforcap[:]
	okfor[ir.OLEN] = okforlen[:]

	// comparison
	iscmp[ir.OLT] = true
	iscmp[ir.OGT] = true
	iscmp[ir.OGE] = true
	iscmp[ir.OLE] = true
	iscmp[ir.OEQ] = true
	iscmp[ir.ONE] = true
}

func makeErrorInterface() *types.Type {
	sig := types.NewSignature(types.NoPkg, fakeRecvField(), nil, []*types.Field{
		types.NewField(src.NoXPos, nil, types.Types[types.TSTRING]),
	})
	method := types.NewField(src.NoXPos, lookup("Error"), sig)
	return types.NewInterface(types.NoPkg, []*types.Field{method})
}

// finishUniverse makes the universe block visible within the current package.
func finishUniverse() {
	// Operationally, this is similar to a dot import of builtinpkg, except
	// that we silently skip symbols that are already declared in the
	// package block rather than emitting a redeclared symbol error.

	for _, s := range ir.BuiltinPkg.Syms {
		if s.Def == nil {
			continue
		}
		s1 := lookup(s.Name)
		if s1.Def != nil {
			continue
		}

		s1.Def = s.Def
		s1.Block = s.Block
	}

	nodfp = NewName(lookup(".fp"))
	nodfp.SetType(types.Types[types.TINT32])
	nodfp.SetClass(ir.PPARAM)
	nodfp.SetUsed(true)
}