-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisasm.go
More file actions
125 lines (106 loc) · 3.13 KB
/
disasm.go
File metadata and controls
125 lines (106 loc) · 3.13 KB
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
package bcl
import (
"fmt"
"io"
)
func (p *Prog) disasm() {
if p.name != "" {
fmt.Fprintln(p.output, "==", p.name, "==")
}
for offset := 0; offset < len(p.code); {
offset = p.disasmInstr(offset)
}
}
func (p *Prog) disasmInstr(offset int) int {
fmt.Fprintf(p.output, "%04d ", offset)
if offset > 0 && p.positions[offset] == p.positions[offset-1] {
fmt.Fprintf(p.output, " | ")
} else {
fmt.Fprintf(p.output, "%6s ", p.linePos.format(p.positions[offset]))
}
instr := opcode(p.code[offset])
switch instr {
case
opNOP, opRET, opPRINT, opPOP,
opENDBLOCK,
opDEFUBIND, opENDUBIND,
opNIL, opZERO, opONE, opTRUE, opFALSE,
opEQ, opLT, opGT,
opADD, opSUB, opMUL, opDIV, opNEG, opNOT, opUNPLUS:
return simpleInstr(p.output, instr, offset)
case opCONST, opGETFIELD, opSETFIELD:
return constInstr(p.output, instr, p, offset)
case opGETLOCAL, opSETLOCAL, opPOPN:
return varbyteargInstr(p.output, instr, p, offset)
case opDEFBLOCK:
return blockInstr(p.output, instr, p, offset)
case opJUMP, opJFALSE:
return jumpInstr(p.output, instr, +1, p, offset)
case opLOOP:
return jumpInstr(p.output, instr, -1, p, offset)
case opBIND:
return bindInstr(p.output, instr, p, offset)
default:
fmt.Fprintln(p.output, "unknown opcode", instr)
return offset + 1
}
}
func simpleInstr(w io.Writer, o opcode, offset int) int {
fmt.Fprintln(w, o)
return offset + 1
}
func byteargInstr(w io.Writer, o opcode, p *Prog, offset int) int {
arg := p.code[offset+1]
fmt.Fprintf(w, "%-10s %4d\n", o, arg)
return offset + 2
}
func varbyteargInstr(w io.Writer, o opcode, p *Prog, offset int) int {
arg, n := uvarintFromBytes(p.code[offset+1:])
fmt.Fprintf(w, "%-10s %4d\n", o, arg)
return offset + 1 + n
}
func constInstr(w io.Writer, o opcode, p *Prog, offset int) int {
idx, n := uvarintFromBytes(p.code[offset+1:])
fmt.Fprintf(w, "%-10s %4d '%v'\n", o, idx, p.constants[idx])
return offset + 1 + n
}
func blockInstr(w io.Writer, o opcode, p *Prog, offset int) int {
typeIdx, n1 := uvarintFromBytes(p.code[offset+1:])
nameIdx, n2 := uvarintFromBytes(p.code[offset+1+n1:])
fmt.Fprintf(w,
"%-10s %4d '%v'\t%4d '%v'\n",
o, typeIdx, p.constants[typeIdx], nameIdx, p.constants[nameIdx],
)
return offset + 1 + n1 + n2
}
func jumpInstr(w io.Writer, o opcode, sign int, p *Prog, offset int) int {
jump := u16FromBytes(p.code[offset+1:])
fmt.Fprintf(w, "%-10s %4d -> %04d\n",
o, jump, offset+1+jumpByteLength+sign*int(jump),
)
return offset + 1 + jumpByteLength
}
func bindInstr(w io.Writer, o opcode, p *Prog, offset int) int {
bindCode := p.code[offset+1]
tidx, n := uvarintFromBytes(p.code[offset+2:])
ncnt, m := uvarintFromBytes(p.code[offset+2+n:])
vv := make([]struct {
idx uint64
v value
}, ncnt)
var k int // cumulative size of incoming consts
for i := uint64(0); i < ncnt; i++ {
idx, j := uvarintFromBytes(p.code[offset+2+n+m+k:])
vv[i].idx, vv[i].v = idx, p.constants[idx]
k += j
}
fmt.Fprintf(w,
"%-10s %4d '%v'\t%4d#",
fmt.Sprintf("%s 0x%2X", o, bindCode), tidx, p.constants[tidx], ncnt,
)
for _, v := range vv {
fmt.Fprintf(w, "\t%4d '%v'", v.idx, v.v)
}
fmt.Fprintln(w)
return offset + 2 + n + m + k
}