-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
365 lines (274 loc) · 13.1 KB
/
parser.py
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import typing
import re
import base
class Segment:
_content: list[tuple]
def __init__(self):
self._content = []
def add(self, arg):
self._content.append(arg)
class Parser:
def __init__(self, input_string):
"""
Initialises the parser
"""
self.input = input_string
self.input_pos = 0
def get_next_term(self, peek: bool = False, extra_delimiters: str = "", match_parentheses: bool = False) -> typing.Optional[str]:
"""
Returns the next non-comment space separated word (or any other delimiter).
If peek is set to True, the 'input_pos' will not be advanced. If
match_parentheses is set to True, square bracket parentheses will be
matched.
"""
pos = self.input_pos
# Loop to exit comment
while True:
# Skip until the first non-whitespace character, ie. not one of:
# ' ', '\n', '\t', '\r', '\f' and '\v'
while pos < len(self.input) and self.input[pos].isspace():
pos += 1
# Return None to signal end of file
if pos == len(self.input):
if not peek:
self.input_pos = pos
return None
if self.input[pos] != ";":
break
pos = 1 + self.input.find("\n", pos)
if pos == 0: # No newline found - return None
if not peek:
self.input_pos = pos
return None
# Find the end of the word by finding the next whitespace character.
end_pos = pos
nesting = 0
while end_pos < len(self.input):
if self.input[end_pos] == "[":
nesting += 1
elif self.input[end_pos] == "]":
nesting -= 1
if (not match_parentheses or nesting == 0) and (self.input[end_pos].isspace() or self.input[end_pos] == ";" or self.input[end_pos] in extra_delimiters):
break
end_pos += 1
result = self.input[pos:end_pos]
if self.input[end_pos] in extra_delimiters:
end_pos += 1
# Update the input_pos value
if not peek:
self.input_pos = end_pos
return result
def parseSections(self) -> list:
# process the file line by line
segment = None # 'code', 'data' or None
aliases = {}
tokens = []
# Tokenise based on spaces
while (term := self.get_next_term()) is not None:
if term == "@CODE":
if self.get_next_term(peek=True) == "=":
self.get_next_term() # To consume the '='
term = self.get_next_term()
address = self.get_value(self.get_next_term())
if address is None:
raise ValueError(f"Expected a number literal after '@CODE =' - got {term!r}")
else:
address = 0x3ffff
tokens.append((base.Token.CODE_SEGMENT_START, address))
segment = "code"
elif term == "@DATA":
if self.get_next_term(peek=True) == "=":
self.get_next_term() # To consume the '='
term = self.get_next_term()
address = self.get_value(term)
if address is None:
raise ValueError(f"Expected a number literal after '@DATA =' - got {term!r}")
else:
address = 0x3ffff
tokens.append((base.Token.DATA_SEGMENT_START, address))
segment = "data"
elif term == "@END":
break
elif term in {"@STACK", "@STACKSIZE", "@INCLUDE"}:
raise NotImplementedError(f"Statement {term} is not supported.")
elif self.get_next_term(peek=True) == "EQU":
# An EQU alias: [term] EQU [value]
self.get_next_term() # To consume the 'EQU'
value_term = self.get_next_term()
value = self.get_value(value_term)
if value is None:
raise ValueError(f"Expected a number literal after '{term} EQU' - got {value_term!r}")
aliases[term] = value
elif segment == "data":
# lines are [term]:? DW [value](,[value])*
# or [term]:? DS [length]
label = term.removesuffix(":")
op = self.get_next_term()
if op == "DW":
# Define some words
tokens.append((base.Token.LABEL, label))
count = 0
while (value := self.get_value(self.get_next_term(peek=True, extra_delimiters=","))) is not None:
self.get_next_term(extra_delimiters=",") # to consume the value
tokens.append((base.Token.DATA, value))
count += 1
# Improvement: add sizeof(<label>) as an implicit EQU
aliases[f"sizeof({label})"] = count
elif op == "DS":
# Define an array ("storage")
tokens.append((base.Token.LABEL, label))
size = self.get_value(self.get_next_term())
for _ in range(size):
tokens.append((base.Token.DATA, 0))
# Improvement: add sizeof(<label>) as an implicit EQU
aliases[f"sizeof({label})"] = size
else:
raise ValueError(f"Unknown data definition type: {op!r}")
elif segment == "code":
# check if this is a label
if term.endswith(":"):
label = term.removesuffix(":")
tokens.append((base.Token.LABEL, label))
else:
# Mnemonics are case-insensitive
mnemonic = term.upper()
for operands_count in base.Instructions:
if mnemonic in base.Instructions[operands_count]:
break
else:
raise ValueError(f"Unknown mnemonic {term!r} encountered.")
operands = []
for i in range(operands_count):
operands.append(self.get_next_term(match_parentheses=True))
parsed_ops = self.parse_operands(operands)
mnemonic, parsed_ops = self.handle_simplified_mnemonics(mnemonic, parsed_ops)
expected_types = base.InstructionOperands[mnemonic]
for (got, *_), expected in zip(parsed_ops, expected_types):
if got not in expected:
raise ValueError(f"Invalid operand types. Expected operand types {expected_types}, got {parsed_ops}.")
# Add the line to the segment
tokens.append((base.Token.MNEMONIC, mnemonic, parsed_ops))
else:
raise ValueError(f"Term {term!r} outside segment - segment is {segment}")
return tokens, aliases
def handle_simplified_mnemonics(self, mnemonic: str, parsed_ops: list) -> tuple[str, list]:
"""
This function translates the simplified mnemonic into their full form.
"""
if mnemonic == "RTS":
# JMP [SP++] -> JMP [r7++]
return "JMP", [(base.Token.AM_POST_INC, 7)]
if mnemonic == "PUSH":
if parsed_ops[0][0] != base.Token.AM_REGISTER:
raise ValueError(f"PUSH instruction takes a register operand - got {parsed_ops}")
# STOR rX, [--SP] -> STOR rX, [--r7]
return "STOR", [parsed_ops[0], (base.Token.AM_PRE_DEC, 7)]
if mnemonic == "PULL":
if parsed_ops[0][0] != base.Token.AM_REGISTER:
raise ValueError(f"PULL instruction takes a register operand - got {parsed_ops}")
# LOAD rX, [SP++] -> LOAD rX, [r7++]
return "LOAD", [parsed_ops[0], (base.Token.AM_POST_INC, 7)]
return mnemonic, parsed_ops
def parse_operands(self, operands: list[str]) -> list[typing.Union[tuple[base.Token, typing.Union[int, str]], tuple[base.Token, int, int]]]:
"""
Given a list of operands, returns a list of tokenised operands.
"""
value_or_label = r"((?:-?\s*[0-9]+)|(?:\$[0-9a-fA-F]+)|(?:%[01]+)|(?:'..?')|(?:\"..?\")|(?:[a-zA-Z0-9_]+))"
register = r"((?:[rR][0-7])|SP|GB)"
value_re = re.compile(value_or_label)
register_re = re.compile(register)
indexed_re = re.compile(r"\s*".join([r"\[", register, r"[\+-]", value_or_label, r"\]"]))
reg_indexed_re = re.compile(r"\s*".join([r"\[", register, r"\+", register, r"\]"]))
post_inc_re = re.compile(r"\s*".join([r"\[", register, r"\+\+", r"\]"]))
pre_dec_re = re.compile(r"\s*".join([r"\[", r"--", register, r"\]"]))
ind_indexed_re = re.compile(r"\s*".join([r"\[", r"\[", register, r"\]", r"[\+-]", value_or_label, r"\]"]))
ind_reg_indexed_re = re.compile(r"\s*".join([r"\[", r"\[", register, r"\]", r"\+", register, r"\]"]))
tokens = []
for operand in operands:
name = operand.strip()
if register_re.fullmatch(name):
n = self.get_reg(name)
tokens.append((base.Token.AM_REGISTER, n))
elif (m := reg_indexed_re.fullmatch(name)):
reg0, reg1 = m.groups()
reg0 = self.get_reg(reg0)
reg1 = self.get_reg(reg1)
tokens.append((base.Token.AM_REG_INDEXED, reg0, reg1))
elif (m := indexed_re.fullmatch(name)):
reg, disp = m.groups()
reg = self.get_reg(reg)
res = self.get_value(disp)
# If disp is a label, it cannot be resolved, so keep the string
if res is not None:
disp = res
tokens.append((base.Token.AM_INDEXED, reg, disp))
elif (m := post_inc_re.fullmatch(name)):
reg, = m.groups()
reg = self.get_reg(reg)
tokens.append((base.Token.AM_POST_INC, reg))
elif (m := pre_dec_re.fullmatch(name)):
reg, = m.groups()
reg = self.get_reg(reg)
tokens.append((base.Token.AM_PRE_DEC, reg))
elif (m := ind_indexed_re.fullmatch(name)):
reg, disp = m.groups()
reg = self.get_reg(reg)
res = self.get_value(disp)
# If disp is a label, it cannot be resolved, so keep the string
if res is not None:
disp = res
tokens.append((base.Token.AM_IND_INDEXED, reg, disp))
elif (m := ind_reg_indexed_re.fullmatch(name)):
reg0, reg1 = m.groups()
reg0 = self.get_reg(reg0)
reg1 = self.get_reg(reg1)
tokens.append((base.Token.AM_IND_REG_INDEXED, reg0, reg1))
elif value_re.fullmatch(name):
n = self.get_value(name)
if n is not None:
tokens.append((base.Token.AM_VALUE, n))
else:
tokens.append((base.Token.AM_LABEL, name))
else: # must be a label
print(f"Warning: Unknown operand thing: {name!r} - assuming it's a label")
tokens.append((base.Token.AM_LABEL, name))
return tokens
def get_reg(self, reg_str: str) -> int:
"""
Converts a register representation to an int
"""
# Register aliases
if reg_str == "SP":
reg_str = "R7"
elif reg_str == "GB":
reg_str = "R6"
r, n = reg_str
if not (r in "rR" and 0 <= int(n) <= 7):
raise ValueError(f"Could not parse register {reg_str}")
return int(n)
def get_value(self, str_value: str) -> typing.Optional[int]:
"""
Converts a literal value to an integer. See section 4.1.
Returns None on failure.
"""
try:
if str_value.startswith("%"): # binary
_, sign_bit, *value_part = str_value
full_value = "".join(value_part).rjust(18, sign_bit)
return int(full_value, 2) % 2 ** 18
elif str_value.startswith("$"): # hexadecimal
full_value = str_value[1:]
return int(full_value, 16) % 2 ** 18
elif str_value.startswith("'") or str_value.startswith('"'): # ascii
_, *chars, _ = str_value
# no need for modular reduction, since these values can never exceed
# 2 ** 18
if len(chars) == 1:
return ord(chars[0])
elif len(chars) != 2:
raise ValueError(f"Cannot parse value {str_value!r} as ASCII literal: Wrong number of characters ({len(chars)}) - must be 1 or 2.")
return (ord(chars[1]) << 8) | ord(chars[0])
else: # decimal
return int(str_value, 10) % 2 ** 18
except ValueError:
return None