-
Notifications
You must be signed in to change notification settings - Fork 2
/
prog.go
60 lines (51 loc) · 1.35 KB
/
prog.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
package hir
import (
`fmt`
`strings`
)
type Program struct {
Head *Ir
}
func (self Program) Free() {
for p, q := self.Head, self.Head; p != nil; p = q {
q = p.Ln
p.Free()
}
}
func (self Program) Disassemble() string {
ret := make([]string, 0, 64)
ref := make(map[*Ir]string)
/* scan all the branch target */
for p := self.Head; p != nil; p = p.Ln {
if p.IsBranch() {
if p.Op != OP_bsw {
if _, ok := ref[p.Br]; !ok {
ref[p.Br] = fmt.Sprintf("L_%d", len(ref))
}
} else {
for _, lb := range p.Switch() {
if lb != nil {
if _, ok := ref[lb]; !ok {
ref[lb] = fmt.Sprintf("L_%d", len(ref))
}
}
}
}
}
}
/* dump all the instructions */
for p := self.Head; p != nil; p = p.Ln {
var ok bool
var vv string
/* check for label reference */
if vv, ok = ref[p]; ok {
ret = append(ret, vv + ":")
}
/* indent each line */
for _, ln := range strings.Split(p.Disassemble(ref), "\n") {
ret = append(ret, " " + ln)
}
}
/* join them together */
return strings.Join(ret, "\n")
}