-
Notifications
You must be signed in to change notification settings - Fork 43
/
disasm.go
74 lines (66 loc) · 1.68 KB
/
disasm.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
package quasigo
import (
"fmt"
"strings"
)
// TODO(quasilyte): generate extra opcode info so we can simplify disasm function?
func disasm(env *Env, fn *Func) string {
var out strings.Builder
dbg, ok := env.debug.funcs[fn]
if !ok {
return "<unknown>\n"
}
code := fn.code
labels := map[int]string{}
walkBytecode(code, func(pc int, op opcode) {
switch op {
case opJumpTrue, opJumpFalse, opJump:
offset := decode16(code, pc+1)
targetPC := pc + offset
if _, ok := labels[targetPC]; !ok {
labels[targetPC] = fmt.Sprintf("L%d", len(labels))
}
}
})
walkBytecode(code, func(pc int, op opcode) {
if l := labels[pc]; l != "" {
fmt.Fprintf(&out, "%s:\n", l)
}
var arg interface{}
var comment string
switch op {
case opCallNative:
id := decode16(code, pc+1)
arg = id
comment = env.nativeFuncs[id].name
case opPushParam, opPushIntParam:
index := int(code[pc+1])
arg = index
comment = dbg.paramNames[index]
case opSetLocal, opSetIntLocal, opPushLocal, opPushIntLocal, opIncLocal, opDecLocal:
index := int(code[pc+1])
arg = index
comment = dbg.localNames[index]
case opPushConst:
arg = int(code[pc+1])
comment = fmt.Sprintf("value=%#v", fn.constants[code[pc+1]])
case opPushIntConst:
arg = int(code[pc+1])
comment = fmt.Sprintf("value=%#v", fn.intConstants[code[pc+1]])
case opJumpTrue, opJumpFalse, opJump:
offset := decode16(code, pc+1)
targetPC := pc + offset
arg = offset
comment = labels[targetPC]
}
if comment != "" {
comment = " # " + comment
}
if arg == nil {
fmt.Fprintf(&out, " %s%s\n", op, comment)
} else {
fmt.Fprintf(&out, " %s %#v%s\n", op, arg, comment)
}
})
return out.String()
}