-
Notifications
You must be signed in to change notification settings - Fork 20
/
sema.go
484 lines (457 loc) · 11.6 KB
/
sema.go
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Semantic Analyzer to produce IR struct
package main
import "github.com/DQNEO/minigo/util"
var symbolTable *SymbolTable
type SymbolTable struct {
allScopes map[normalizedPackagePath]*Scope
uniquedDTypes []string
}
func makeDynamicTypeLabel(id int) string {
s := Sprintf("DynamicTypeId%d", id)
return s
}
func (symbolTable *SymbolTable) getTypeLabel(gtype *Gtype) string {
dynamicTypeId := util.Index(gtype.String(), symbolTable.uniquedDTypes)
if dynamicTypeId == -1 {
errorft(nil, "type %s not found in uniquedDTypes", gtype.String())
}
return makeDynamicTypeLabel(dynamicTypeId)
}
var typeId int = 1 // start with 1 because we want to zero as error
func setTypeIds(namedTypes []*DeclType) {
for _, namedType := range namedTypes {
namedType.gtype.receiverTypeId = typeId
typeId++
}
}
func resolve(sc *Scope, rel *Relation) *IdentBody {
relbody := sc.get(rel.name)
if relbody != nil {
if relbody.gtype != nil {
rel.gtype = relbody.gtype
} else if relbody.expr != nil {
rel.expr = relbody.expr
} else {
errorft(rel.token(), "Bad type relbody %v", relbody)
}
}
return relbody
}
func resolveIdents(pkg *AstPackage, universe *Scope) {
packageScope := pkg.scope
packageScope.outer = universe
for _, file := range pkg.files {
for _, rel := range file.unresolved {
relbody := resolve(packageScope, rel)
if relbody == nil {
errorft(rel.token(), "unresolved identifier %s", rel.name)
}
}
}
}
// copy methods from p.nameTypes to gtype.methods of each type
func attachMethodsToTypes(pmethods map[identifier]methods, packageScope *Scope) {
for typeName, methods := range pmethods {
var gTypeName identifier = identifier(typeName)
gtype := packageScope.getGtype(gTypeName)
if gtype == nil {
errorf("typaneme %s is not found in the package scope %s", gTypeName, packageScope.name)
}
gtype.methods = methods
}
}
func collectDecls(pkg *AstPackage) {
for _, f := range pkg.files {
for _, decl := range f.DeclList {
if decl.vardecl != nil {
pkg.vars = append(pkg.vars, decl.vardecl)
} else if decl.funcdecl != nil {
if decl.funcdecl.fname == "init" {
pkg.hasInit = true
}
pkg.funcs = append(pkg.funcs, decl.funcdecl)
}
}
}
}
func setStringLables(pkg *AstPackage, prefix identifier) {
for id, sl := range pkg.stringLiterals {
var no int = id + 1
sl.slabel = Sprintf("%s.S%d", prefix, no)
}
}
func calcStructSize(gtypes []*Gtype) {
for _, gtype := range gtypes {
if gtype.getKind() == G_STRUCT {
gtype.calcStructOffset()
} else if gtype.getKind() == G_POINTER && gtype.origType.getKind() == G_STRUCT {
gtype.origType.calcStructOffset()
}
}
}
func uniqueDynamicTypes(dynamicTypes []*Gtype) []string {
var r []string = builtinTypesAsString
for _, gtype := range dynamicTypes {
gs := gtype.String()
if !util.InArray(gs, r) {
r = append(r, gs)
}
}
return r
}
func composeMethodTable(funcs []*DeclFunc) map[int][]string {
var methodTable map[int][]string = make(map[int][]string) // receiverTypeId : []methodTable
for _, funcdecl := range funcs {
if funcdecl.receiver == nil {
continue
}
gtype := funcdecl.receiver.getGtype()
if gtype.kind == G_POINTER {
gtype = gtype.origType
}
if gtype.relation == nil {
errorf("no relation for %#v", funcdecl.receiver.getGtype())
}
typeId := gtype.relation.gtype.receiverTypeId
symbol := funcdecl.getSymbol()
methods := methodTable[typeId]
methods = append(methods, symbol)
methodTable[typeId] = methods
}
debugf("set methodTable")
return methodTable
}
func walkFunc(f *DeclFunc) *DeclFunc {
f.prologue = f.prepare()
f.body = walkStmtList(f.body)
return f
}
func walkStmtList(stmtList *StmtSatementList) *StmtSatementList {
if stmtList == nil {
return nil
}
for i, stmt := range stmtList.stmts {
stmt2 := walkStmt(stmt)
stmtList.stmts[i] = stmt2
}
return stmtList
}
func proxyToIRuntimeFunc(funcall *ExprFuncallOrConversion) *IrCall {
def := funcall.getFuncDef()
symbol := getFuncSymbol(IRuntimePath, string(def.builtinname))
var staticCall *IrCall = &IrCall{
tok: funcall.token(),
origExpr: funcall,
callee: def,
symbol: symbol,
args: funcall.args,
}
return staticCall
}
func walkExpr(expr Expr) Expr {
var r Expr
switch expr.(type) {
case nil:
return r
case *Relation:
e := expr.(*Relation)
return e.expr
case *ExprNilLiteral:
case *ExprNumberLiteral:
case *ExprStringLiteral:
case *ExprVariable:
case *ExprConstVariable:
case *ExprFuncallOrConversion:
funcall := expr.(*ExprFuncallOrConversion)
for i := 0; i < len(funcall.args); i++ {
arg := funcall.args[i]
arg = walkExpr(arg)
funcall.args[i] = arg
}
if funcall.typ != nil {
// Conversion (*T)(e)
r = &IrExprConversion{
tok: funcall.token(),
toGtype: funcall.typ,
arg: funcall.args[0],
}
return r
}
if funcall.rel.expr == nil && funcall.rel.gtype != nil {
// Conversion
r = &IrExprConversion{
tok: funcall.token(),
toGtype: funcall.rel.gtype,
arg: funcall.args[0],
}
return r
}
decl := funcall.getFuncDef()
switch decl {
case builtinPanic:
assert(len(funcall.args) == 1, funcall.token(), "invalid arguments for len()")
var staticCall *IrCall = &IrCall{
tok: funcall.token(),
origExpr: funcall,
callee: decl,
}
staticCall.symbol = getFuncSymbol(IRuntimePath, "panic")
staticCall.args = funcall.args
return staticCall
case builtinLen:
assert(len(funcall.args) == 1, funcall.token(), "invalid arguments for len()")
arg := funcall.args[0]
return &ExprLen{
tok: arg.token(),
arg: arg,
}
case builtinCap:
arg := funcall.args[0]
return &ExprCap{
tok: arg.token(),
arg: arg,
}
case builtinSyscall, builtinClone:
return proxyToIRuntimeFunc(funcall)
case builtinMake:
assert(funcall.typarg != nil, funcall.token(), "make() should take Type argment")
var staticCall *IrCall = &IrCall{
tok: funcall.token(),
origExpr: funcall,
callee: decl,
}
var lenArg Expr
var capArg Expr
switch funcall.typarg.getKind() {
case G_SLICE:
assert(len(funcall.args) >= 1, funcall.token(), "make() should take 1 argments other than type")
lenArg = funcall.args[0]
staticCall.symbol = getFuncSymbol(IRuntimePath, "makeSlice")
capArg = funcall.args[1]
size := funcall.typarg.elementType.getSize()
staticCall.args = []Expr{&ExprNumberLiteral{val: size}, lenArg, capArg}
return staticCall
case G_MAP:
// Replace by an empty map literal for now.
if len(funcall.args) >= 1 {
lenArg = funcall.args[0]
}
mapInitializer := &IrMapInitializer{
tok: funcall.token(),
gtype: funcall.typarg,
lenArg: lenArg,
}
return mapInitializer
default:
errorft(funcall.token(), "make for invalid type:%s", funcall.typarg.String())
}
case builtinAppend:
assert(len(funcall.args) == 2, funcall.token(), "append() should take 2 argments")
slice := funcall.args[0]
valueToAppend := funcall.args[1]
var staticCall *IrCall = &IrCall{
tok: funcall.token(),
origExpr: funcall,
callee: decl,
}
switch slice.getGtype().elementType.getSize() {
case 1:
staticCall.symbol = getFuncSymbol(IRuntimePath, "append1")
case 8:
staticCall.symbol = getFuncSymbol(IRuntimePath, "append8")
case 24:
if slice.getGtype().elementType.getKind() == G_INTERFACE && valueToAppend.getGtype().getKind() != G_INTERFACE {
eConvertion := &IrExprConversionToInterface{
tok: valueToAppend.token(),
arg: valueToAppend,
}
funcall.args[1] = eConvertion
}
staticCall.symbol = getFuncSymbol(IRuntimePath, "append24")
default:
TBI(slice.token(), "")
}
staticCall.args = funcall.args
return staticCall
}
return funcall
case *ExprMethodcall:
methodCall := expr.(*ExprMethodcall)
for i := 0; i < len(methodCall.args); i++ {
arg := methodCall.args[i]
arg = walkExpr(arg)
methodCall.args[i] = arg
}
methodCall.receiver = walkExpr(methodCall.receiver)
expr = methodCall
return expr
case *ExprBinop:
e := expr.(*ExprBinop)
e.left = walkExpr(e.left)
e.right = walkExpr(e.right)
return e
case *ExprUop:
e := expr.(*ExprUop)
e.operand = walkExpr(e.operand)
return e
case *ExprFuncRef:
case *ExprSlice:
e := expr.(*ExprSlice)
e.collection = walkExpr(e.collection)
e.low = walkExpr(e.low)
e.high = walkExpr(e.high)
e.max = walkExpr(e.max)
return e
case *ExprIndex:
e := expr.(*ExprIndex)
e.index = walkExpr(e.index)
e.collection = walkExpr(e.collection)
return e
case *ExprArrayLiteral:
case *ExprSliceLiteral:
e := expr.(*ExprSliceLiteral)
for i, v := range e.values {
v2 := walkExpr(v)
e.values[i] = v2
}
return e
case *ExprTypeAssertion:
case *ExprVaArg:
e := expr.(*ExprVaArg)
e.expr = walkExpr(e.expr)
return e
case *ExprStructLiteral:
e := expr.(*ExprStructLiteral)
for _, field := range e.fields {
field.value = walkExpr(field.value)
}
return e
case *ExprStructField:
e := expr.(*ExprStructField)
e.strct = walkExpr(e.strct)
return e
case *ExprTypeSwitchGuard:
case *ExprMapLiteral:
e := expr.(*ExprMapLiteral)
for _, elm := range e.elements {
elm.key = walkExpr(elm.key)
elm.value = walkExpr(elm.value)
}
case *ExprLen:
case *ExprCap:
//case *ExprConversionToInterface:
}
return expr
}
func walkStmt(stmt Stmt) Stmt {
var s2 Stmt
switch stmt.(type) {
case nil:
return s2
case *DeclVar:
s := stmt.(*DeclVar)
s.initval = walkExpr(s.initval)
return s
case *StmtFor:
s := stmt.(*StmtFor)
s2 = s.convert()
s2 = walkStmt(s2)
return s2
case *IrStmtRangeMap:
s := stmt.(*IrStmtRangeMap)
s.rangeexpr = walkExpr(s.rangeexpr)
s.block = walkStmtList(s.block)
return s
case *IrStmtForRangeList:
s := stmt.(*IrStmtForRangeList)
s.init = walkStmt(s.init)
s.cond = walkExpr(s.cond)
s.block = walkStmtList(s.block)
return s
case *IrStmtClikeFor:
s := stmt.(*IrStmtClikeFor)
cls := s.cls
cls.init = walkStmt(cls.init)
cls.cond = walkStmt(cls.cond)
cls.post = walkStmt(cls.post)
s.block = walkStmtList(s.block)
return s
case *StmtIf:
s := stmt.(*StmtIf)
s.simplestmt = walkStmt(s.simplestmt)
s.cond = walkExpr(s.cond)
s.then = walkStmt(s.then)
s.els = walkStmt(s.els)
return s
case *StmtReturn:
s := stmt.(*StmtReturn)
for i, expr := range s.exprs {
e := walkExpr(expr)
s.exprs[i] = e
}
return s
case *StmtInc:
s := stmt.(*StmtInc)
s.operand = walkExpr(s.operand)
return s
case *StmtDec:
s := stmt.(*StmtDec)
s.operand = walkExpr(s.operand)
return s
case *StmtSatementList:
s := stmt.(*StmtSatementList)
s = walkStmtList(s)
return s
case *StmtAssignment:
s := stmt.(*StmtAssignment)
for i, right := range s.rights {
right = walkExpr(right)
s.rights[i] = right
}
for i, left := range s.lefts {
left = walkExpr(left)
s.lefts[i] = left
}
return s
case *StmtShortVarDecl:
s := stmt.(*StmtShortVarDecl)
var s2 Stmt = &StmtAssignment{
tok: s.tok,
lefts: s.lefts,
rights: s.rights,
}
s2 = walkStmt(s2)
return s2
case *StmtContinue:
s := stmt.(*StmtContinue)
return s
case *StmtBreak:
s := stmt.(*StmtBreak)
return s
case *StmtExpr:
s := stmt.(*StmtExpr)
s.expr = walkExpr(s.expr)
return s
case *StmtDefer:
s := stmt.(*StmtDefer)
s.expr = walkExpr(s.expr)
return s
case *StmtGo:
s := stmt.(*StmtGo)
s.expr = walkExpr(s.expr)
_, ok := s.expr.(*ExprFuncallOrConversion)
if !ok {
panic("invalid expresson for go routine")
}
return s
case *StmtSwitch:
s := stmt.(*StmtSwitch)
s.cond = walkExpr(s.cond)
for _, cse := range s.cases {
cse.compound = walkStmtList(cse.compound)
}
s.dflt = walkStmtList(s.dflt)
return s
}
return stmt
}