-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.go
754 lines (707 loc) · 20.3 KB
/
codegen.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
package mjingo
import (
"github.com/hnakamur/mjingo/internal/datast/stack"
"github.com/hnakamur/mjingo/option"
)
type codeGenerator struct {
instructions instructions
blocks map[string]instructions
pendingBlock stack.Stack[pendingBlock]
currentLine uint32
spanStack stack.Stack[span]
filterLocalIds map[string]localID
testLocalIds map[string]localID
rawTemplateBytes uint
}
// Represents an open block of code that does not yet have updated
// jump targets.
type pendingBlock interface {
typ() pendingBlockType
}
var _ = pendingBlock(branchPendingBlock{})
var _ = pendingBlock(loopPendingBlock{})
var _ = pendingBlock(scBoolPendingBlock{})
type branchPendingBlock struct{ jumpInst uint }
type loopPendingBlock struct{ iterInst uint }
type scBoolPendingBlock struct{ instructions []uint }
func (branchPendingBlock) typ() pendingBlockType { return pendingBlockTypeBranch }
func (loopPendingBlock) typ() pendingBlockType { return pendingBlockTypeLoop }
func (scBoolPendingBlock) typ() pendingBlockType { return pendingBlockTypeScBool }
type pendingBlockType int
const (
pendingBlockTypeBranch pendingBlockType = iota + 1
pendingBlockTypeLoop
pendingBlockTypeScBool
)
func newCodeGenerator(file, source string) *codeGenerator {
return &codeGenerator{
instructions: newInstructions(file, source),
blocks: make(map[string]instructions),
pendingBlock: stack.NewStackWithCapacity[pendingBlock](32),
filterLocalIds: make(map[string]localID),
testLocalIds: make(map[string]localID),
}
}
func (g *codeGenerator) CompileStmt(stmt statement) {
switch st := stmt.(type) {
case templateStmt:
g.setLineFromSpan(st.span)
for _, node := range st.children {
g.CompileStmt(node)
}
case emitExprStmt:
g.compileEmitExpr(emitExprStmt{expr: st.expr, span: st.span})
case emitRawStmt:
g.setLineFromSpan(st.span)
g.add(emitRawInstruction{Val: st.raw})
g.rawTemplateBytes += uint(len(st.raw))
case forLoopStmt:
g.compileForLoop(st)
case ifCondStmt:
g.compileIfStmt(st)
case withBlockStmt:
g.setLineFromSpan(st.span)
g.add(pushWithInstruction{})
for _, assign := range st.assignments {
g.compileExpr(assign.rhs)
g.compileAssignment(assign.lhs)
}
for _, node := range st.body {
g.CompileStmt(node)
}
g.add(popFrameInstruction{})
case setStmt:
g.setLineFromSpan(st.span)
g.compileExpr(st.expr)
g.compileAssignment(st.target)
case setBlockStmt:
g.setLineFromSpan(st.span)
g.add(beginCaptureInstruction{Mode: captureModeCapture})
for _, node := range st.body {
g.CompileStmt(node)
}
g.add(endCaptureInstruction{})
if st.filter.IsSome() {
g.compileExpr(st.filter.Unwrap())
}
g.compileAssignment(st.target)
case autoEscapeStmt:
g.setLineFromSpan(st.span)
g.compileExpr(st.enabled)
g.add(pushAutoEscapeInstruction{})
for _, node := range st.body {
g.CompileStmt(node)
}
g.add(popAutoEscapeInstruction{})
case filterBlockStmt:
g.setLineFromSpan(st.span)
g.add(beginCaptureInstruction{Mode: captureModeCapture})
for _, node := range st.body {
g.CompileStmt(node)
}
g.add(endCaptureInstruction{})
g.compileExpr(st.filter)
g.add(emitInstruction{})
case blockStmt:
g.compileBlock(st)
case importStmt:
g.add(beginCaptureInstruction{Mode: captureModeDiscard})
g.add(pushWithInstruction{})
g.compileExpr(st.expr)
g.addWithSpan(includeInstruction{IgnoreMissing: false}, st.span)
g.add(exportLocalsInstruction{})
g.add(popFrameInstruction{})
g.compileAssignment(st.name)
g.add(endCaptureInstruction{})
case fromImportStmt:
g.add(beginCaptureInstruction{Mode: captureModeDiscard})
g.add(pushWithInstruction{})
g.compileExpr(st.expr)
g.addWithSpan(includeInstruction{IgnoreMissing: false}, st.span)
for _, importName := range st.names {
g.compileExpr(importName.name)
}
g.add(popFrameInstruction{})
for i := len(st.names) - 1; i >= 0; i-- {
importName := st.names[i]
g.compileAssignment(importName.as.UnwrapOr(importName.name))
}
g.add(endCaptureInstruction{})
case extendsStmt:
g.setLineFromSpan(st.span)
g.compileExpr(st.name)
g.addWithSpan(loadBlocksInstruction{}, st.span)
case includeStmt:
g.setLineFromSpan(st.span)
g.compileExpr(st.name)
g.addWithSpan(includeInstruction{IgnoreMissing: st.ignoreMissing}, st.span)
case macroStmt:
g.compileMacro(st)
case callBlockStmt:
g.compileCallBlock(st)
case doStmt:
g.compileDo(st)
default:
panic("unreachable")
}
}
func (g *codeGenerator) compileBlock(block blockStmt) {
g.setLineFromSpan(block.span)
sub := g.newSubgenerator()
for _, node := range block.body {
sub.CompileStmt(node)
}
insts := g.finishSubgenerator(sub)
g.blocks[block.name] = insts
g.add(callBlockInstruction{Name: block.name})
}
func (g *codeGenerator) compileEmitExpr(exp emitExprStmt) {
g.setLineFromSpan(exp.span)
if callExpr, ok := exp.expr.(callExpr); ok {
switch ct := callExpr.call.data.identityCall().(type) {
case callTypeFunction:
if ct.name == "super" && len(callExpr.call.data.args) == 0 {
g.addWithSpan(fastSuperInstruction{}, callExpr.call.span)
return
} else if ct.name == "loop" && len(callExpr.call.data.args) == 1 {
g.compileExpr(callExpr.call.data.args[0])
g.add(fastRecurseInstruction{})
return
}
case callTypeBlock:
g.add(callBlockInstruction{Name: ct.name})
return
}
}
g.compileExpr(exp.expr)
g.add(emitInstruction{})
}
func (g *codeGenerator) compileForLoop(forLoop forLoopStmt) {
g.setLineFromSpan(forLoop.span)
if forLoop.filterExpr.IsSome() {
// filter expressions work like a nested for loop without
// the special loop variable that append into a new list
// just outside of the loop.
g.add(buildListInstruction{Count: 0})
g.compileExpr(forLoop.iter)
g.startForLoop(false, false)
g.add(dupTopInstruction{})
g.compileAssignment(forLoop.target)
g.compileExpr(forLoop.filterExpr.Unwrap())
g.startIf()
g.add(listAppendInstruction{})
g.startElse()
g.add(discardTopInstruction{})
g.endIf()
g.endForLoop(false)
} else {
g.compileExpr(forLoop.iter)
}
g.startForLoop(true, forLoop.recursive)
g.compileAssignment(forLoop.target)
for _, node := range forLoop.body {
g.CompileStmt(node)
}
g.endForLoop(len(forLoop.elseBody) != 0)
if len(forLoop.elseBody) != 0 {
g.startIf()
for _, node := range forLoop.elseBody {
g.CompileStmt(node)
}
g.endIf()
}
}
func (g *codeGenerator) compileAssignment(expr astExpr) {
switch exp := expr.(type) {
case varExpr:
g.add(storeLocalInstruction{Name: exp.id})
case listExpr:
g.pushSpan(exp.span)
g.add(unpackListInstruction{Count: uint(len(exp.items))})
for _, expr := range exp.items {
g.compileAssignment(expr)
}
g.popSpan()
default:
panic("unreachable")
}
}
func (g *codeGenerator) compileMacroExpression(macroDecl macroStmt) {
g.setLineFromSpan(macroDecl.span)
inst := g.add(jumpInstruction{JumpTarget: ^uint(0)})
j := len(macroDecl.defaults) - 1
for i := len(macroDecl.args) - 1; i >= 0; i-- {
if j >= 0 {
g.add(dupTopInstruction{})
g.add(isUndefinedInstruction{})
g.startIf()
g.add(discardTopInstruction{})
g.compileExpr(macroDecl.defaults[j])
g.endIf()
j--
}
g.compileAssignment(macroDecl.args[i])
}
for _, node := range macroDecl.body {
g.CompileStmt(node)
}
g.add(returnInstruction{})
undeclared := findMacroClosure(macroDecl)
callerReference := undeclared.Contains("caller")
undeclared.Delete("caller")
macroInst := g.nextInstruction()
for _, name := range undeclared.Keys() {
g.add(encloseInstruction{Name: name})
}
g.add(getClosureInstruction{})
ids := make([]Value, 0, len(macroDecl.args))
for _, arg := range macroDecl.args {
if varExp, ok := arg.(varExpr); ok {
ids = append(ids, valueFromString(varExp.id))
} else {
panic("unreachable")
}
}
g.add(loadConstInstruction{Val: valueFromSlice(ids)})
flags := uint8(0)
if callerReference {
flags |= macroCaller
}
g.add(buildMacroInstruction{Name: macroDecl.name, Offset: inst + 1, Flags: flags})
if g.instructions.instructions[inst].Typ() == instTypeJump {
g.instructions.instructions[inst] = jumpInstruction{JumpTarget: macroInst}
} else {
panic("unreachable")
}
}
func (g *codeGenerator) compileMacro(macroDecl macroStmt) {
g.compileMacroExpression(macroDecl)
g.add(storeLocalInstruction{Name: macroDecl.name})
}
func (g *codeGenerator) compileCallBlock(callBlock callBlockStmt) {
g.compileCall(callBlock.call, option.Some(callBlock.macroDecl))
g.add(emitInstruction{})
}
func (g *codeGenerator) compileDo(doTag doStmt) {
g.compileCall(doTag.call, option.None[macroStmt]())
}
func (g *codeGenerator) compileIfStmt(ifCond ifCondStmt) {
g.setLineFromSpan(ifCond.span)
g.compileExpr(ifCond.expr)
g.startIf()
for _, node := range ifCond.trueBody {
g.CompileStmt(node)
}
if len(ifCond.falseBody) > 0 {
g.startElse()
for _, node := range ifCond.falseBody {
g.CompileStmt(node)
}
}
g.endIf()
}
func (g *codeGenerator) compileExpr(exp astExpr) {
switch exp := exp.(type) {
case varExpr:
g.setLineFromSpan(exp.span)
g.add(lookupInstruction{Name: exp.id})
case constExpr:
g.setLineFromSpan(exp.span)
g.add(loadConstInstruction{Val: exp.val})
case sliceExpr:
g.pushSpan(exp.span)
g.compileExpr(exp.expr)
if exp.start.IsSome() {
g.compileExpr(exp.start.Unwrap())
} else {
g.add(loadConstInstruction{Val: valueFromI64(int64(0))})
}
if exp.stop.IsSome() {
g.compileExpr(exp.stop.Unwrap())
} else {
g.add(loadConstInstruction{Val: none})
}
if exp.step.IsSome() {
g.compileExpr(exp.step.Unwrap())
} else {
g.add(loadConstInstruction{Val: valueFromI64(int64(1))})
}
g.add(sliceInstruction{})
g.popSpan()
case unaryOpExpr:
g.setLineFromSpan(exp.span)
switch exp.op {
case unaryOpTypeNot:
g.compileExpr(exp.expr)
g.add(notInstruction{})
case unaryOpTypeNeg:
// common case: negative numbers. In that case we
// directly negate them if this is possible without
// an error.
if c, ok := exp.expr.(constExpr); ok {
negated, err := opNeg(c.val)
if err == nil {
g.add(loadConstInstruction{Val: negated})
return
}
}
g.compileExpr(exp.expr)
g.addWithSpan(negInstruction{}, exp.span)
}
case binOpExpr:
g.compileBinOp(exp)
case ifExpr:
g.setLineFromSpan(exp.span)
g.compileExpr(exp.testExpr)
g.startIf()
g.compileExpr(exp.trueExpr)
g.startElse()
if exp.falseExpr.IsSome() {
g.compileExpr(exp.falseExpr.Unwrap())
} else {
g.add(loadConstInstruction{Val: Undefined})
}
g.endIf()
case filterExpr:
g.pushSpan(exp.span)
if exp.expr.IsSome() {
g.compileExpr(exp.expr.Unwrap())
}
for _, arg := range exp.args {
g.compileExpr(arg)
}
localID := getLocalID(g.filterLocalIds, exp.name)
g.add(applyFilterInstruction{Name: exp.name, ArgCount: uint(len(exp.args)) + 1, LocalID: localID})
g.popSpan()
case testExpr:
g.pushSpan(exp.span)
g.compileExpr(exp.expr)
for _, arg := range exp.args {
g.compileExpr(arg)
}
localID := getLocalID(g.testLocalIds, exp.name)
g.add(performTestInstruction{Name: exp.name, ArgCount: uint(len(exp.args)) + 1, LocalID: localID})
g.popSpan()
case getAttrExpr:
g.pushSpan(exp.span)
g.compileExpr(exp.expr)
g.add(getAttrInstruction{Name: exp.name})
g.popSpan()
case getItemExpr:
g.pushSpan(exp.span)
g.compileExpr(exp.expr)
g.compileExpr(exp.subscriptExpr)
g.add(getItemInstruction{})
g.popSpan()
case callExpr:
g.compileCall(exp.call, option.None[macroStmt]())
case listExpr:
if v := exp.asConst(); v.IsSome() {
g.add(loadConstInstruction{Val: v.Unwrap()})
} else {
g.setLineFromSpan(exp.span)
for _, item := range exp.items {
g.compileExpr(item)
}
g.add(buildListInstruction{Count: uint(len(exp.items))})
}
case mapExpr:
if v := exp.asConst(); v.IsSome() {
g.add(loadConstInstruction{Val: v.Unwrap()})
} else {
g.setLineFromSpan(exp.span)
if len(exp.keys) != len(exp.values) {
panic("mismatch length of keys and values for a map")
}
for i, key := range exp.keys {
v := exp.values[i]
g.compileExpr(key)
g.compileExpr(v)
}
g.add(buildMapInstruction{PairCount: uint(len(exp.keys))})
}
case kwargsExpr:
optVal := exp.asConst()
if optVal.IsSome() {
g.add(loadConstInstruction{Val: optVal.Unwrap()})
} else {
g.setLineFromSpan(exp.span)
for _, pair := range exp.pairs {
g.add(loadConstInstruction{Val: valueFromString(pair.key)})
g.compileExpr(pair.arg)
}
g.add(buildKwargsInstruction{PairCount: uint(len(exp.pairs))})
}
default:
panic("unreachable")
}
}
func (g *codeGenerator) compileCall(c spanned[call], caller option.Option[macroStmt]) {
g.pushSpan(c.span)
switch ct := c.data.identityCall().(type) {
case callTypeFunction:
argCount := g.compileCallArgs(c.data.args, caller)
g.add(callFunctionInstruction{Name: ct.name, ArgCount: argCount})
case callTypeBlock:
g.add(beginCaptureInstruction{Mode: captureModeCapture})
g.add(callBlockInstruction{Name: ct.name})
g.add(endCaptureInstruction{})
case callTypeMethod:
g.compileExpr(ct.expr)
argCount := g.compileCallArgs(c.data.args, caller)
g.add(callMethodInstruction{Name: ct.name, ArgCount: argCount + 1})
case callTypeObject:
g.compileExpr(ct.expr)
argCount := g.compileCallArgs(c.data.args, caller)
g.add(callObjectInstruction{ArgCount: argCount + 1})
}
g.popSpan()
}
func (g *codeGenerator) compileCallArgs(args []astExpr, caller option.Option[macroStmt]) uint {
if caller.IsSome() {
return g.compileCallArgsWithCaller(args, caller.Unwrap())
}
for _, arg := range args {
g.compileExpr(arg)
}
return uint(len(args))
}
func (g *codeGenerator) compileCallArgsWithCaller(args []astExpr, caller macroStmt) uint {
injectedCaller := false
// try to add the caller to already existing keyword arguments.
for _, arg := range args {
if m, ok := arg.(kwargsExpr); ok {
g.setLineFromSpan(m.span)
for _, pair := range m.pairs {
g.add(loadConstInstruction{Val: valueFromString(pair.key)})
g.compileExpr(pair.arg)
}
g.add(loadConstInstruction{Val: valueFromString("caller")})
g.compileMacroExpression(caller)
g.add(buildKwargsInstruction{PairCount: uint(len(m.pairs)) + 1})
injectedCaller = true
} else {
g.compileExpr(arg)
}
}
// if there are no keyword args so far, create a new kwargs object
// and add caller to that.
if !injectedCaller {
g.add(loadConstInstruction{Val: valueFromString("caller")})
g.compileMacroExpression(caller)
g.add(buildKwargsInstruction{PairCount: 1})
return uint(len(args)) + 1
}
return uint(len(args))
}
func (g *codeGenerator) startForLoop(withLoopVar, recursive bool) {
flags := uint8(0)
if withLoopVar {
flags |= loopFlagWithLoopVar
}
if recursive {
flags |= loopFlagRecursive
}
g.add(pushLoopInstruction{Flags: flags})
iterInst := g.add(iterateInstruction{JumpTarget: 0})
g.pendingBlock.Push(loopPendingBlock{iterInst: iterInst})
}
func (g *codeGenerator) endForLoop(pushDidNotIterate bool) {
b := g.pendingBlock.Pop()
if b == nil {
panic("pendingBlock should not be empty in endForLoop")
}
if b, ok := b.(loopPendingBlock); ok {
g.add(jumpInstruction{JumpTarget: b.iterInst})
loopEnd := g.nextInstruction()
if pushDidNotIterate {
g.add(pushDidNotIterateInstruction{})
}
g.add(popFrameInstruction{})
if _, ok := g.instructions.instructions[b.iterInst].(iterateInstruction); ok {
g.instructions.instructions[b.iterInst] = iterateInstruction{JumpTarget: loopEnd}
} else {
panic("must be iterateInstruction")
}
} else {
panic("must be loopPendingBlock")
}
}
func (g *codeGenerator) startIf() {
jumpInst := g.add(jumpIfFalseInstruction{JumpTarget: 0})
g.pendingBlock.Push(branchPendingBlock{jumpInst: jumpInst})
}
func (g *codeGenerator) startElse() {
jumpInst := g.add(jumpInstruction{JumpTarget: 0})
g.endCondition(jumpInst + 1)
g.pendingBlock.Push(branchPendingBlock{jumpInst: jumpInst})
}
func (g *codeGenerator) endIf() {
g.endCondition(g.nextInstruction())
}
// Starts a short cirquited bool block.
func (g *codeGenerator) startScBool() {
g.pendingBlock.Push(scBoolPendingBlock{})
}
// Emits a short circuited bool operator.
func (g *codeGenerator) scBool(and bool) {
if blk, ok := g.pendingBlock.Peek(); ok {
var inst instruction
if and {
inst = jumpIfFalseOrPopInstruction{JumpTarget: ^uint(0)}
} else {
inst = jumpIfTrueOrPopInstruction{JumpTarget: ^uint(0)}
}
instIdx := g.instructions.add(inst)
scBoolBlk := blk.(scBoolPendingBlock)
scBoolBlk.instructions = append(scBoolBlk.instructions, instIdx)
g.pendingBlock[len(g.pendingBlock)-1] = scBoolBlk
}
}
// Ends a short circuited bool block.
func (g *codeGenerator) endScBool() {
end := g.nextInstruction()
if !g.pendingBlock.Empty() {
blk := g.pendingBlock.Pop()
if scBoolBlk, ok := blk.(scBoolPendingBlock); ok {
for _, instIdx := range scBoolBlk.instructions {
switch g.instructions.instructions[instIdx].(type) {
case jumpIfFalseOrPopInstruction:
g.instructions.instructions[instIdx] = jumpIfFalseOrPopInstruction{JumpTarget: end}
case jumpIfTrueOrPopInstruction:
g.instructions.instructions[instIdx] = jumpIfTrueOrPopInstruction{JumpTarget: end}
default:
panic("unreachable")
}
}
}
}
}
func (g *codeGenerator) endCondition(jumpInst uint) {
if g.pendingBlock.Empty() {
panic("pendingBlock should not be empty in endCondition")
}
b := g.pendingBlock.Pop()
if b, ok := b.(branchPendingBlock); ok {
switch g.instructions.instructions[b.jumpInst].(type) {
case jumpIfFalseInstruction:
g.instructions.instructions[b.jumpInst] = jumpIfFalseInstruction{JumpTarget: jumpInst}
case jumpInstruction:
g.instructions.instructions[b.jumpInst] = jumpInstruction{JumpTarget: jumpInst}
}
} else {
panic("must be branchPendingBlock")
}
}
func (g *codeGenerator) Finish() (instructions, map[string]instructions) {
return g.instructions, g.blocks
}
func (g *codeGenerator) setLine(lineno uint32) {
g.currentLine = lineno
}
func (g *codeGenerator) setLineFromSpan(spn span) {
g.setLine(spn.StartLine)
}
func (g *codeGenerator) pushSpan(spn span) {
g.spanStack.Push(spn)
g.setLineFromSpan(spn)
}
func (g *codeGenerator) popSpan() {
g.spanStack.Pop()
}
func (g *codeGenerator) add(instr instruction) uint {
if spn, ok := g.spanStack.Peek(); ok {
if spn.StartLine == g.currentLine {
return g.instructions.addWithSpan(instr, spn)
}
}
return g.instructions.addWithLine(instr, g.currentLine)
}
func (g *codeGenerator) addWithSpan(instr instruction, spn span) uint {
return g.instructions.addWithSpan(instr, spn)
}
func (g *codeGenerator) nextInstruction() uint {
return uint(len(g.instructions.instructions))
}
func (g *codeGenerator) newSubgenerator() *codeGenerator {
sub := newCodeGenerator(g.instructions.name, g.instructions.source)
sub.currentLine = g.currentLine
if !g.spanStack.Empty() {
v, _ := g.spanStack.Peek()
sub.spanStack.Push(v)
}
return sub
}
func (g *codeGenerator) finishSubgenerator(sub *codeGenerator) instructions {
g.currentLine = sub.currentLine
insts, blocks := sub.finish()
for name, block := range blocks {
g.blocks[name] = block
}
return insts
}
func (g *codeGenerator) compileBinOp(exp binOpExpr) {
g.pushSpan(exp.span)
var instr instruction
switch exp.op {
case binOpTypeEq:
instr = eqInstruction{}
case binOpTypeNe:
instr = neInstruction{}
case binOpTypeLt:
instr = ltInstruction{}
case binOpTypeLte:
instr = lteInstruction{}
case binOpTypeGt:
instr = gtInstruction{}
case binOpTypeGte:
instr = gteInstruction{}
case binOpTypeScAnd, binOpTypeScOr:
g.startScBool()
g.compileExpr(exp.left)
g.scBool(exp.op == binOpTypeScAnd)
g.compileExpr(exp.right)
g.endScBool()
g.popSpan()
return
case binOpTypeAdd:
instr = addInstruction{}
case binOpTypeSub:
instr = subInstruction{}
case binOpTypeMul:
instr = mulInstruction{}
case binOpTypeDiv:
instr = divInstruction{}
case binOpTypeFloorDiv:
instr = intDivInstruction{}
case binOpTypeRem:
instr = remInstruction{}
case binOpTypePow:
instr = powInstruction{}
case binOpTypeConcat:
instr = stringConcatInstruction{}
case binOpTypeIn:
instr = inInstruction{}
}
g.compileExpr(exp.left)
g.compileExpr(exp.right)
g.add(instr)
g.popSpan()
}
func (g *codeGenerator) finish() (instructions, map[string]instructions) {
if !g.pendingBlock.Empty() {
panic("unreachable")
}
return g.instructions, g.blocks
}
func getLocalID(ids map[string]localID, name string) localID {
if id, ok := ids[name]; ok {
return id
} else if len(ids) >= maxLocals {
return ^localID(0)
} else {
nextID := localID(len(ids))
ids[name] = nextID
return nextID
}
}