-
Notifications
You must be signed in to change notification settings - Fork 3
/
assemble.go
177 lines (152 loc) · 3.4 KB
/
assemble.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
package zog
import "fmt"
func Assemble(s string) (*Assembly, error) {
s += "\n"
assembler := &PegAssembler{Buffer: s}
assembler.Init()
assembler.Current.Init()
err := assembler.Parse()
if err != nil {
//assembler.Print()
assembler.PrintSyntaxTree()
return nil, fmt.Errorf("Can't parse: %s", err)
}
//assembler.Print()
//assembler.PrintSyntaxTree()
assembler.Execute()
assembly := assembler.GetAssembly()
err = assembly.ResolveAddresses()
if err != nil {
return nil, fmt.Errorf("Can't resolve addresses: %s", err)
}
return assembly, nil
}
type LabelledInstruction struct {
Label string
Inst Instruction
Addr uint16
}
type Assembly struct {
BaseAddr uint16
Linsts []LabelledInstruction
Labels map[string]int
resolved bool
}
func (a *Assembly) Encode() ([]byte, error) {
err := a.ResolveAddresses()
if err != nil {
return nil, err
}
buf := make([]byte, 0)
for _, linst := range a.Linsts {
instBuf := linst.Inst.Encode()
buf = append(buf, instBuf...)
}
return buf, nil
}
func (a *Assembly) ResolveAddresses() error {
if a.resolved {
return nil
}
// One pass to work out addresses for instructions
addr := a.BaseAddr
for i := range a.Linsts {
a.Linsts[i].Addr = addr
buf := a.Linsts[i].Inst.Encode()
addr += uint16(len(buf))
}
// One pass to find and resolve labels
for i := range a.Linsts {
err := a.Linsts[i].Inst.Resolve(a)
if err != nil {
return fmt.Errorf("Failed to resolve label for [%s]: %s", a.Linsts[i].Inst, err)
}
label := a.Linsts[i].Label
if label != "" {
label += ":"
}
fmt.Printf("%-16s %04X: %s\n", label, a.Linsts[i].Addr, a.Linsts[i].Inst)
}
a.resolved = true
return nil
}
func (a *Assembly) ResolveLoc8(l Loc8) error {
contents, ok := l.(Contents)
if !ok {
return nil
}
return a.resolveContents(contents)
}
func (a *Assembly) ResolveLoc16(l Loc16) error {
label, ok := l.(*Label)
if ok {
addr, err := a.FindLabelAddr(label.name)
if err != nil {
return err
}
label.Imm16 = Imm16(addr)
return nil
}
contents, ok := l.(Contents)
if !ok {
return nil
}
return a.resolveContents(contents)
}
func (a *Assembly) resolveContents(c Contents) error {
label, ok := c.addr.(*Label)
if !ok {
return nil
}
addr, err := a.FindLabelAddr(label.name)
if err != nil {
return err
}
label.Imm16 = Imm16(addr)
return nil
}
func (a *Assembly) FindLabelAddr(name string) (uint16, error) {
index, ok := a.Labels[name]
if !ok {
return 0, fmt.Errorf("Can't find label: %s", name)
}
if index < 0 || index >= len(a.Linsts) {
return 0, fmt.Errorf("Label index out of range (%d > %d)", index, len(a.Linsts))
}
return a.Linsts[index].Addr, nil
}
func (a *Assembly) Init() {
a.Labels = make(map[string]int)
}
func (a *Assembly) Instructions() []Instruction {
insts := make([]Instruction, 0)
for _, linst := range a.Linsts {
insts = append(insts, linst.Inst)
}
return insts
}
func (a *Assembly) String() string {
colWidth := 20
str := ""
printPrefix := func(prefix string, iStr string) string {
if prefix != "" {
prefix += ":"
}
s := ""
s += fmt.Sprintf(prefix)
width := colWidth - len(prefix)
for i := 0; i < width; i++ {
s += fmt.Sprintf(" ")
}
s += iStr
s += "\n"
return s
}
str += printPrefix("", fmt.Sprintf("org %04x", a.BaseAddr))
str += printPrefix("", "")
for _, linst := range a.Linsts {
str += printPrefix(linst.Label, linst.Inst.String())
}
str += printPrefix("", "")
return str
}