-
Notifications
You must be signed in to change notification settings - Fork 1
/
assembler.go
232 lines (200 loc) · 6.72 KB
/
assembler.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"bufio"
"log"
"os"
"path"
"strings"
"github.com/thatoddmailbox/gbasm/parser"
"github.com/thatoddmailbox/gbasm/rom"
"github.com/thatoddmailbox/gbasm/utils"
)
type Instruction struct {
Mnemonic string
Operands []string
}
func Assembler_ParseFile(filePath string, origin int, maxLength int) int {
fileBase := path.Base(filePath)
log.Printf("Parsing file %s...\n", fileBase)
Assembler_FindLabelsInFile(filePath, fileBase)
Assembler_ParseFilePass(filePath, fileBase, origin, maxLength, 0) // first pass just finds what labels are pointing to
return Assembler_ParseFilePass(filePath, fileBase, origin, maxLength, 1)
}
func Assembler_FindLabelsInFile(filePath string, fileBase string) {
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
lineNumber := 0
for scanner.Scan() {
lineNumber++
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
continue
}
if line[0] == '.' {
// is it an include?
if strings.HasPrefix(line, ".incasm") {
// if so, get those labels
instructionParts := strings.Split(line, " ")
includedFilePath := path.Join(path.Dir(filePath), strings.Replace(instructionParts[1], "\"", "", -1))
Assembler_FindLabelsInFile(includedFilePath, path.Base(includedFilePath))
}
}
if line[len(line)-1] == ':' {
// it's a label
labelName := line[:len(line)-1]
_, existsInDefs := rom.Current.Definitions[labelName]
existsInUnpointedDefs := utils.StringInSlice(labelName, rom.Current.UnpointedDefinitions)
if existsInDefs || existsInUnpointedDefs {
log.Fatalf("Tried to declare already existing label or constant '%s' at %s:%d", labelName, fileBase, lineNumber)
}
rom.Current.UnpointedDefinitions = append(rom.Current.UnpointedDefinitions, labelName)
}
}
}
func Assembler_ParseFilePass(filePath string, fileBase string, origin int, maxLength int, pass int) int {
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer file.Close()
outputIndex := origin
scanner := bufio.NewScanner(file)
lineNumber := 0
inMultilineComment := false
for scanner.Scan() {
lineNumber++
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
continue
}
if inMultilineComment {
if len(line) > 1 && line[len(line)-2] == '*' && line[len(line)-1] == '/' {
inMultilineComment = false
}
continue
}
if line[0] == '.' && len(line) > 1 {
// special instruction
instructionParts := strings.Split(line, " ")
switch instructionParts[0][1:] {
case "def":
if pass != 0 {
// defines only apply on the first pass
continue
}
key := instructionParts[1]
val, valid := parser.ParseNumber(parser.SimplifyPotentialExpression(instructionParts[2], pass, fileBase, lineNumber))
if !valid {
log.Fatalf("Expected number, got '%s' at %s:%d", instructionParts[2], fileBase, lineNumber)
}
_, exists := rom.Current.Definitions[key]
if exists {
log.Fatalf("Tried to declare already existing label or constant '%s' at %s:%d", key, fileBase, lineNumber)
}
rom.Current.Definitions[key] = val
case "org":
newOrigin, valid := parser.ParseNumber(instructionParts[1])
if !valid {
log.Fatalf("Expected number, got '%s' at %s:%d", instructionParts[1], fileBase, lineNumber)
}
outputIndex = newOrigin
case "incasm":
includedFilePath := path.Join(path.Dir(filePath), strings.Replace(instructionParts[1], "\"", "", -1))
outputIndex = Assembler_ParseFilePass(includedFilePath, path.Base(includedFilePath), outputIndex, maxLength, pass)
default:
log.Fatalf("Unknown special instruction '%s' at %s:%d", instructionParts[0][1:], fileBase, lineNumber)
}
} else {
// it's either a comment, label, or instruction
// is it a label?
if line[len(line)-1] == ':' {
// it is
if pass != 0 {
// labels only apply on the first pass
continue
}
labelName := line[:len(line)-1]
_, exists := rom.Current.Definitions[labelName]
if exists {
log.Fatalf("Tried to declare already existing label or constant '%s' at %s:%d", labelName, fileBase, lineNumber)
}
rom.Current.Definitions[labelName] = outputIndex
} else {
// parse it character-by-character
buf := ""
instruction := Instruction{}
foundAnInstruction := false
inAString := false
for i := 0; i < len(line); i++ {
char := line[i]
if !inAString && ((char == '/' && (len(line)-i) > 1) || char == ';') {
if char == ';' || line[i+1] == '/' {
// single-line comment
break
} else if line[i+1] == '*' {
// start of multi-line comment
inMultilineComment = true
break
} else {
log.Fatalf("Unexpected '%s' after '/' at %s:%d", string(line[i+1]), fileBase, lineNumber)
}
} else if char == ' ' && instruction.Mnemonic == "" {
// yay we have a mnemonic
instruction.Mnemonic = strings.ToUpper(buf)
foundAnInstruction = true
buf = ""
} else if char == '"' {
inAString = !inAString
buf += string(char)
} else if char == ',' && foundAnInstruction && !inAString {
// yay we have an operand
instruction.Operands = append(instruction.Operands, buf)
foundAnInstruction = true
buf = ""
} else {
buf += string(char)
}
}
if !foundAnInstruction && buf != "" {
// it's an instruction with no operands
foundAnInstruction = true
instruction.Mnemonic = strings.ToUpper(buf)
buf = ""
}
if foundAnInstruction {
if buf != "" {
// add any extra as the last operand
instruction.Operands = append(instruction.Operands, buf)
}
// process the operands and any expressions in them
for i := 0; i < len(instruction.Operands); i++ {
instruction.Operands[i] = parser.SimplifyPotentialExpression(instruction.Operands[i], pass, fileBase, lineNumber)
if utils.StringInSlice(strings.ToUpper(instruction.Operands[i]), append(append(parser.RegisterNames8, parser.RegisterNames16...), parser.ConditionCodes...)) {
// capitalize register and condition code names
instruction.Operands[i] = strings.ToUpper(instruction.Operands[i])
}
}
// now, actually assemble the instruction
outputIndex = Assembler_AssembleInstruction(instruction, outputIndex, fileBase, lineNumber)
}
}
}
}
if err = scanner.Err(); err != nil {
panic(err)
}
return outputIndex
}
func Assembler_AssembleInstruction(instruction Instruction, outputIndex int, fileBase string, lineNumber int) int {
output := OpCodes_GetOutput(instruction, fileBase, lineNumber)
for i := 0; i < len(output); i++ {
rom.Current.Output[outputIndex] = output[i]
outputIndex++
}
rom.Current.UsedByteCount += len(output)
return outputIndex
}