-
Notifications
You must be signed in to change notification settings - Fork 10
/
emitters.go
42 lines (34 loc) · 933 Bytes
/
emitters.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
package astcomp
import (
"fmt"
"github.com/arnodel/golua/ast"
"github.com/arnodel/golua/ir"
)
// These methods offer the convenience of not having to calculate the line
// number when emitting instructions.
func (c *compiler) emitInstr(l ast.Locator, instr ir.Instruction) {
c.CodeBuilder.Emit(instr, getLine(l))
}
func (c *compiler) emitJump(l ast.Locator, lbl ir.Name) {
if !c.CodeBuilder.EmitJump(lbl, getLine(l)) {
panic(Error{
Where: l,
Message: fmt.Sprintf("no visible label '%s'", lbl),
})
}
}
func (c *compiler) emitLoadConst(l ast.Locator, k ir.Constant, reg ir.Register) {
ir.EmitConstant(c.CodeBuilder, k, reg, getLine(l))
}
func (c *compiler) emitMove(l ast.Locator, dst, src ir.Register) {
ir.EmitMove(c.CodeBuilder, dst, src, getLine(l))
}
func getLine(l ast.Locator) int {
if l != nil {
locStart := l.Locate().StartPos()
if locStart != nil {
return locStart.Line
}
}
return 0
}