-
Notifications
You must be signed in to change notification settings - Fork 20
/
gen_control_flow.go
405 lines (354 loc) · 8.6 KB
/
gen_control_flow.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
package main
func (stmt *StmtIf) emit() {
emit("# if")
if stmt.simplestmt != nil {
stmt.simplestmt.emit()
}
stmt.cond.emit()
emit("cmpq $0, %%rax")
labelEndif := makeLabel()
if stmt.els != nil {
labelElse := makeLabel()
emit("je %s # jump if 0", labelElse)
emit("# then block")
stmt.then.emit()
emit("jmp %s # jump to endif", labelEndif)
emit("# else block")
emit("%s:", labelElse)
stmt.els.emit()
emit("# endif")
emit("%s:", labelEndif)
} else {
// no else block
emit("je %s # jump if 0", labelEndif)
emit("# then block")
stmt.then.emit()
emit("# endif")
emit("%s:", labelEndif)
}
}
func (stmt *StmtSwitch) isTypeSwitch() bool {
_, isTypeSwitch := stmt.cond.(*ExprTypeSwitchGuard)
return isTypeSwitch
}
func emitConvertNilToEmptyString() {
emit("# emitConvertNilToEmptyString")
emit("POP_8")
emit("PUSH_8")
emit("# convert nil to an empty string")
emit("cmpq $0, %%rax")
emit("popq %%rax")
labelEnd := makeLabel()
emit("jne %s # jump if not nil", labelEnd)
emit("# if nil then")
emitEmptyString()
emit("%s:", labelEnd)
}
func emitCompareEqFromStack(gtype *Gtype) {
if gtype.isString() {
emitGoStringsEqualFromStack()
} else {
emit("CMP_FROM_STACK sete")
}
}
func emitCompareDynamicTypeFromStack(gtype *Gtype) {
emitConvertNilToEmptyString()
emit("PUSH_8")
if gtype.isNil() {
emitEmptyString()
} else {
typeLabel := symbolTable.getTypeLabel(gtype)
emit("LOAD_STRING_LITERAL .%s # type: %s", typeLabel, gtype.String())
}
emit("PUSH_8")
emitCompareEqFromStack(gUintptr)
}
func (stmt *StmtSwitch) emit() {
emit("# switch statement")
labelEnd := makeLabel()
var labels []string
// switch (expr) {
var cond Expr
if stmt.cond != nil {
cond = stmt.cond
emit("# the cond expression")
cond.emit()
emitPush(cond.getGtype())
} else {
// switch {
emit("# no condition")
}
// case exp1,exp2,..:
// stmt1;
// stmt2;
// ...
for i, caseClause := range stmt.cases {
var j int = i
emit("# case %d", j)
myCaseLabel := makeLabel()
labels = append(labels, myCaseLabel)
if stmt.cond == nil {
for _, e := range caseClause.exprs {
e.emit()
emit("cmpq $0, %%rax")
emit("jne %s # jump if matches", myCaseLabel)
}
} else if stmt.isTypeSwitch() {
// compare type
for _, gtype := range caseClause.gtypes {
emit("# Duplicate the cond value in stack")
emit("POP_24")
emit("PUSH_24")
emit("pushq %%rcx # push dynamic type addr")
emitCompareDynamicTypeFromStack(gtype)
emit("cmpq $0, %%rax")
emit("jne %s # jump if matches", myCaseLabel)
}
} else {
for _, e := range caseClause.exprs {
emit("# Duplicate the cond value in stack")
if e.getGtype().isString() {
emit("POP_SLICE # the cond value")
emit("PUSH_SLICE # the cond value")
emit("PUSH_SLICE # the cond valiue")
e.emit()
emit("PUSH_SLICE")
} else {
emit("POP_8 # the cond value")
emit("PUSH_8 # the cond value")
emit("PUSH_8 # arg1: the cond value")
e.emit()
emit("PUSH_8 # arg2: case value")
}
emitCompareEqFromStack(e.getGtype())
emit("cmpq $0, %%rax")
emit("jne %s # jump if matches", myCaseLabel)
}
}
}
var defaultLabel string
if stmt.dflt == nil {
emit("jmp %s", labelEnd)
} else {
emit("# jump to default")
defaultLabel = makeLabel()
emit("jmp %s", defaultLabel)
}
if cond != nil && cond.getGtype().is24WidthType() {
emit("POP_24 # destroy the cond value")
} else {
emit("POP_8 # destroy the cond value")
}
emit("#")
for i, caseClause := range stmt.cases {
emit("# case clause")
emit("%s:", labels[i])
caseClause.compound.emit()
emit("jmp %s", labelEnd)
}
if stmt.dflt != nil {
emit("# default case clause")
emit("%s:", defaultLabel)
stmt.dflt.emit()
}
emit("%s: # end of switch", labelEnd)
}
func (f *IrStmtForRangeList) emit() {
// i = 0
emit("# init index")
f.init.emit()
emit("%s: # begin loop ", f.labels.labelBegin)
f.cond.emit()
emit("cmpq $0, %%rax")
emit("je %s # if false, go to loop end", f.labels.labelEndLoop)
if f.assignVar != nil {
f.assignVar.emit()
}
f.block.emit()
emit("%s: # end block", f.labels.labelEndBlock)
f.cond2.emit()
emit("cmpq $0, %%rax")
emit("jne %s # if this iteration is final, go to loop end", f.labels.labelEndLoop)
f.incr.emit()
emit("jmp %s", f.labels.labelBegin)
emit("%s: # end loop", f.labels.labelEndLoop)
}
func (f *IrStmtClikeFor) emit() {
emit("# emit IrStmtClikeFor")
if f.cls.init != nil {
f.cls.init.emit()
}
emit("%s: # begin loop ", f.labels.labelBegin)
if f.cls.cond != nil {
f.cls.cond.emit()
emit("cmpq $0, %%rax")
emit("je %s # jump if false", f.labels.labelEndLoop)
}
f.block.emit()
emit("%s: # end block", f.labels.labelEndBlock)
if f.cls.post != nil {
f.cls.post.emit()
}
emit("jmp %s", f.labels.labelBegin)
emit("%s: # end loop", f.labels.labelEndLoop)
}
func (f *StmtFor) emit() {
assertNotReached(f.token())
}
func (f *StmtFor) convert() Stmt {
// Determine kind
if f.rng != nil {
if f.rng.rangeexpr.getGtype().getKind() == G_MAP {
f.kind = FOR_KIND_RANGE_MAP
} else {
f.kind = FOR_KIND_RANGE_LIST
}
} else {
f.kind = FOR_KIND_CLIKE
}
l1 := makeLabel()
l2 := makeLabel()
l3 := makeLabel()
lbls := f.labels
// @FIXME: f.labels.labelBegin = l1 does not work!!!
lbls.labelBegin = l1
lbls.labelEndBlock = l2
lbls.labelEndLoop = l3
var em Stmt
switch f.kind {
case FOR_KIND_RANGE_MAP:
assertNotNil(f.rng.indexvar != nil, f.rng.tok)
em = &IrStmtRangeMap{
tok: f.token(),
block: f.block,
labels: f.labels,
rangeexpr: f.rng.rangeexpr,
indexvar: f.rng.indexvar,
valuevar: f.rng.valuevar,
mapCounter: f.rng.invisibleMapCounter,
}
case FOR_KIND_RANGE_LIST:
assertNotNil(f.rng.indexvar != nil, f.rng.tok)
assert(f.rng.rangeexpr.getGtype().isArrayLike(), f.rng.tok, "rangeexpr should be G_ARRAY or G_SLICE, but got ", f.rng.rangeexpr.getGtype().String())
var init = &StmtAssignment{
lefts: []Expr{
f.rng.indexvar,
},
rights: []Expr{
&ExprNumberLiteral{
val: 0,
},
},
}
// i < len(list)
var cond = &ExprBinop{
op: "<",
left: f.rng.indexvar, // i
// @TODO
// The range expression x is evaluated once before beginning the loop
right: &ExprLen{
arg: f.rng.rangeexpr, // len(expr)
},
}
// v = s[i]
var assignVar *StmtAssignment
if f.rng.valuevar != nil {
assignVar = &StmtAssignment{
lefts: []Expr{
f.rng.valuevar,
},
rights: []Expr{
&ExprIndex{
collection: f.rng.rangeexpr,
index: f.rng.indexvar,
},
},
}
}
// break if i == len(list) - 1
var cond2 = &ExprBinop{
op: "==",
left: f.rng.indexvar, // i
// @TODO2
// The range expression x is evaluated once before beginning the loop
right: &ExprBinop{
op: "-",
left: &ExprLen{
arg: f.rng.rangeexpr, // len(expr)
},
right: &ExprNumberLiteral{
val: 1,
},
},
}
// i++
var incr = &StmtInc{
operand: f.rng.indexvar,
}
em = &IrStmtForRangeList{
init: init,
cond: cond,
assignVar: assignVar,
cond2: cond2,
incr: incr,
block: f.block,
labels: f.labels,
}
case FOR_KIND_CLIKE:
em = &IrStmtClikeFor{
tok: f.token(),
labels: f.labels,
cls: f.cls,
block: f.block,
}
default:
assertNotReached(f.token())
}
return em
}
func (stmt *StmtReturn) emitDeferAndReturn() {
if len(stmt.labelDeferHandler) != 0 {
emit("# defer and return")
emit("jmp %s", stmt.labelDeferHandler)
}
}
func (ast *StmtDefer) emit() {
emit("# defer")
/*
// arguments should be evaluated immediately
var args []Expr
switch ast.expr.(type) {
case *ExprMethodcall:
call := ast.expr.(*ExprMethodcall)
args = call.args
case *ExprFuncallOrConversion:
call := ast.expr.(*ExprFuncallOrConversion)
args = call.args
default:
errorft(ast.token(), "defer should be a funcall")
}
*/
labelStart := string(makeLabel()) + "_defer"
labelEnd := string(makeLabel()) + "_defer"
ast.label = labelStart
emit("jmp %s", labelEnd)
emit("%s: # defer start", labelStart)
for i := 0; i < len(retRegi); i++ {
emit("pushq %%%s", retRegi[i])
}
ast.expr.emit()
for i := len(retRegi) - 1; i >= 0; i-- {
emit("popq %%%s", retRegi[i])
}
emit("leave")
emit("ret")
emit("%s: # defer end", labelEnd)
}
func (ast *StmtContinue) emit() {
assert(len(ast.labels.labelEndBlock) > 0, ast.token(), "labelEndBlock should not be empty")
emit("jmp %s # continue", ast.labels.labelEndBlock)
}
func (ast *StmtBreak) emit() {
assert(len(ast.labels.labelEndLoop) > 0, ast.token(), "labelEndLoop should not be empty")
emit("jmp %s # break", ast.labels.labelEndLoop)
}