-
Notifications
You must be signed in to change notification settings - Fork 3
/
assembler_pass2.py
290 lines (244 loc) · 8.65 KB
/
assembler_pass2.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
"""
Assembler for DM2018W assembly language.
This assembler is for fully resolved instructions,
which may be the output of assm_xform.py, which
transforms instructions with symbolic addresses into
instructions with fully resolved (PC-relative) addresses.
Assembly instruction format with all options is
label: instruction
Labels are resolved (translated into addresses) in
assm_xform.py; in this pass of the interpreter they
are only for documentation.
Both parts are optional: A label may appear without
an instruction, and an instruction may appear without
a label.
A label is just an alphabetic string, eg.,
myDogBoo but not Betcha_5_Dollars
An instruction has the following form:
opcode/predicate target,src1,src2[disp]
Opcode is required, and should be one of the DM2018W
instruction codes (ADD, MOVE, etc); case-insensitive
/predicate is optional. If present, it should be some
combination of N,Z,P, e.g., /NP would be "execute if
not zero". If /predicate is not given, it is interpreted
as /ALWAYS, which is an alias for /NZP.
target is a register number (r0,r1, ... r15) or one of the
register aliases ZERO, PC, SP, etc.
src1 and src2 are likewise register specifiers.
[disp] is optional. If present, it is a 12 bit
signed integer displacement. If absent, it is
treated as [0].
DATA is a pseudo-operation:
myvar: DATA 18
indicates that the integer value 18
should be stored at this location, rather than
a DM2018S instruction.
"""
from instr_format import Instruction, instruction_from_dict
import memory
import argparse
from typing import Union, List
from enum import Enum, auto
import sys
import io
import re
import logging
logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
# Configuration constants
ERROR_LIMIT = 5 # Abandon assembly if we exceed this
# Exceptions raised by this module
class SyntaxError(Exception):
pass
###
# The whole instruction line is encoded as a single
# regex with capture names for the parts we might
# refer to. Error messages will be crappy (we'll only
# know that the pattern didn't match, and not why), but
# we get a very simple match/process cycle. By creating
# a dict containing the captured fields, we can determine
# which optional parts are present (e.g., there could be
# label without an instruction or an instruction without
# a label).
###
# To simplify client code, we'd like to return a dict with
# the right fields even if the line is syntactically incorrect.
DICT_NO_MATCH = { 'label': None, 'opcode': None, 'predicate': None,
'target': None, 'src1': None, 'src2': None,
'offset': None, 'comment': None }
###
# Although the DM2018W instruction set is very simple, a source
# line can still come in several forms. Each form (even comments)
# can start with a label.
###
class AsmSrcKind(Enum):
"""Distinguish which kind of assembly language instruction
we have matched. Each element of the enum corresponds to
one of the regular expressions below.
"""
# Blank or just a comment, optionally
# with a label
COMMENT = auto()
# Fully specified (all addresses resolved)
FULL = auto()
# A data location, not an instruction
DATA = auto()
# Lines that contain only a comment (and possibly a label).
# This includes blank lines and labels on a line by themselves.
#
ASM_COMMENT_PAT = re.compile(r"""
# Optional label
(
(?P<label> [a-zA-Z]\w*):
)?
\s*
# Optional comment follows # or ;
(
(?P<comment>[\#;].*)
)?
\s*$
""", re.VERBOSE)
# Instructions with fully specified fields. We can generate
# code directly from these. In the transformation phase we
# pass these through unchanged, just keeping track of how much
# room they require in the final object code.
ASM_FULL_PAT = re.compile(r"""
# Optional label
(
(?P<label> [a-zA-Z]\w*):
)?
# The instruction proper
\s*
(?P<opcode> [a-zA-Z]+) # Opcode
(/ (?P<predicate> [a-zA-Z]+) )? # Predicate (optional)
\s+
(?P<target> r[0-9]+), # Target register
(?P<src1> r[0-9]+), # Source register 1
(?P<src2> r[0-9]+) # Source register 2
(\[ (?P<offset>[-]?[0-9]+) \])? # Offset (optional)
# Optional comment follows # or ;
(
\s*
(?P<comment>[\#;].*)
)?
\s*$
""", re.VERBOSE)
# Defaults for values that ASM_FULL_PAT makes optional
INSTR_DEFAULTS = [ ('predicate', 'ALWAYS'), ('offset', '0') ]
# A data word in memory; not a DM2018W instruction
#
ASM_DATA_PAT = re.compile(r"""
# Optional label
(
(?P<label> [a-zA-Z]\w*):
)?
# The instruction proper
\s*
(?P<opcode> DATA) # Opcode
# Optional data value
\s*
(?P<value> (0x[a-fA-F0-9]+)
| ([0-9]+))?
# Optional comment follows # or ;
(
\s*
(?P<comment>[\#;].*)
)?
\s*$
""", re.VERBOSE)
PATTERNS = [(ASM_FULL_PAT, AsmSrcKind.FULL),
(ASM_DATA_PAT, AsmSrcKind.DATA),
(ASM_COMMENT_PAT, AsmSrcKind.COMMENT)
]
def parse_line(line: str) -> dict:
"""Parse one line of assembly code.
Returns a dict containing the matched fields,
some of which may be empty. Raises SyntaxError
if the line does not match assembly language
syntax. Sets the 'kind' field to indicate
which of the patterns was matched.
"""
log.debug("\nParsing assembler line: '{}'".format(line))
# Try each kind of pattern
for pattern, kind in PATTERNS:
match = pattern.fullmatch(line)
if match:
fields = match.groupdict()
fields["kind"] = kind
log.debug("Extracted fields {}".format(fields))
return fields
raise SyntaxError("Assembler syntax error in {}".format(line))
def fill_defaults(fields: dict) -> None:
"""Fill in default values for optional fields of instruction"""
for key, value in INSTR_DEFAULTS:
if fields[key] == None:
fields[key] = value
def value_parse(int_literal: str) -> int:
"""Parse an integer literal that could look like
42 or like 0x2a
"""
if int_literal.startswith("0x"):
return int(int_literal, 16)
else:
return int(int_literal, 10)
def assemble(lines: List[str]) -> List[int]:
"""
Simple one-pass assembly for now; must be extended to two
passes to write convenient assembly code with labels; probably
to three passes with expansion of control flow into standard
assembly code.
"""
error_count = 0
instructions = [ ]
for lnum in range(len(lines)):
line = lines[lnum]
log.debug("Processing line {}: {}".format(lnum, line))
try:
fields = parse_line(line)
if fields["kind"] == AsmSrcKind.FULL:
log.debug("Constructing instruction")
fill_defaults(fields)
instr = instruction_from_dict(fields)
word = instr.encode()
instructions.append(word)
elif fields["kind"] == AsmSrcKind.DATA:
word = value_parse(fields["value"])
instructions.append(word)
else:
log.debug("No instruction on line")
except SyntaxError as e:
error_count += 1
print("Syntax error in line {}: {}".format(lnum, line))
except KeyError as e:
error_count += 1
print("Unknown word in line {}: {}".format(lnum, e))
except Exception as e:
error_count += 1
print("Exception encountered in line {}: {}".format(lnum, e))
if error_count > ERROR_LIMIT:
print("Too many errors; abandoning")
sys.exit(1)
return instructions
def cli() -> object:
"""Get arguments from command line"""
parser = argparse.ArgumentParser(description="Duck Machine Assembler (pass 2)")
parser.add_argument("sourcefile", type=argparse.FileType('r'),
nargs="?", default=sys.stdin,
help="Duck Machine assembly code file")
parser.add_argument("objfile", type=argparse.FileType('w'),
nargs="?", default=sys.stdout,
help="Object file output")
args = parser.parse_args()
return args
def main():
""""Assemble a Duck Machine program"""
args = cli()
lines = args.sourcefile.readlines()
object_code = assemble(lines)
log.debug("Object code: \n{}".format(object_code))
for word in object_code:
log.debug("Instruction word {}".format(word))
print(word,file=args.objfile)
if __name__ == "__main__":
main()