-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.py
327 lines (275 loc) · 11.1 KB
/
translator.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
import argparse
import logging
import re
from typing import Tuple
from isa import (
Opcode,
write_code,
get_opcode_names,
write_commented_code,
MEMORY_SIZE,
MIN_SIGN,
MAX_SIGN,
)
DATA_SECTION = "section data:"
CODE_SECTION = "section code:"
def get_meaningful_token(line: str) -> str:
return line.split(";", 1)[0].strip()
def is_label(value: str) -> bool:
return bool(re.fullmatch(r"^[a-zA-Z_][a-zA-Z_0-9]*:$", value))
def is_label_name(value: str) -> bool:
return bool(re.fullmatch(r"^[a-zA-Z_][a-zA-Z_0-9]*$", value))
def is_variable(value: str) -> bool:
return bool(
re.fullmatch(
r"^[a-zA-Z_][a-zA-Z_0-9]*:\s*((-?\d+)|(\"[^\"]*\")|(\'[^\']*\')|([bB][fF]\s+\d+))$",
value,
)
)
def is_int(value: str) -> bool:
return bool(re.fullmatch(r"^-?\d+$", value))
def is_positive_int(value: str) -> bool:
return bool(re.fullmatch(r"^0*[1-9]\d*$", value))
def is_str(value: str) -> bool:
return bool(re.fullmatch(r"^(\".*\")|(\'.*\')$", value))
def is_bf(value: str) -> bool:
return bool(re.fullmatch(r"^[bB][fF]\s+\d+$", value))
def is_opcode0(value: str) -> bool:
return bool(re.fullmatch(r"^\w+$", value))
def is_opcode1(value: str) -> bool:
return bool(re.fullmatch(r"^\w+ +((\w+)|(-?\d+))$", value))
def translate_stage_1(
text: str,
) -> Tuple[dict[str, int | list[int]], list[Opcode | str | int], dict[str, int], int]:
data: dict[str, int | list[int]] = {}
code: list[Opcode | str | int] = []
labels: dict[str, int] = {}
position = 1
code_position = position
data_mode = False
code_mode = False
for line_num, line in enumerate(text.splitlines(), 1):
token: str = get_meaningful_token(line)
if token == "":
continue
if token.lower() == DATA_SECTION:
if data_mode or code_mode:
raise Exception(f"Wrong section on line {line_num}")
data_mode = True
continue
if token.lower() == CODE_SECTION:
if code_mode:
raise Exception(f"Wrong section on line {line_num}")
code_mode = True
data_mode = False
code_position = position
continue
if is_variable(token):
if data_mode:
name: str
value: str
name, value = map(lambda s: s.strip(), token.split(":", maxsplit=1))
if name.lower() in labels:
raise Exception(
f"Redefinition of label `{name}` on line {line_num}"
)
if is_int(value):
if MIN_SIGN <= int(value) <= MAX_SIGN:
data[name.lower()] = int(value)
labels[name.lower()] = position
position += 1
else:
raise Exception(
f"Value {value} out of boundaries on line {line_num}"
)
elif is_str(value):
pstr = [len(value) - 2] + [ord(c) for c in value[1:-1]]
data[name.lower()] = pstr
labels[name.lower()] = position
position += len(pstr)
elif is_bf(value):
_, size = value.split(maxsplit=1)
if is_positive_int(size):
data[name.lower()] = [0] * int(size)
labels[name.lower()] = position
position += int(size)
else:
raise Exception(
f"Buffer size is not positive integer on line {line_num}"
)
else:
raise Exception(f"Invalid variable: `{token}` on line {line_num}")
elif code_mode:
raise Exception(f"Variable inside code section on line {line_num}")
else:
raise Exception(f"Variable outside of any section on line {line_num}")
elif is_label(token):
if data_mode:
raise Exception(f"Label inside data section on line {line_num}")
elif code_mode:
label: str = token[:-1]
if label.lower() in labels:
raise Exception(
f"Redefinition of label `{label}` on line {line_num}"
)
labels[label.lower()] = position
else:
raise Exception(f"Label outside of any section on line {line_num}")
elif is_opcode1(token):
if data_mode:
raise Exception(f"Instruction inside data section on line {line_num}")
elif code_mode:
mnemonic: str
arg: str
mnemonic, arg = list(map(lambda s: s.strip(), token.split()))
if mnemonic.upper() not in get_opcode_names():
raise Exception(
f"Invalid mnemonic: `{mnemonic}` on line {line_num}"
)
opcode: Opcode = Opcode[mnemonic.upper()]
match opcode:
case Opcode.PUSH:
if is_label_name(arg):
code.append(opcode)
code.append(arg.lower())
position += 2
elif is_int(arg):
if MIN_SIGN <= int(arg) <= MAX_SIGN:
code.append(opcode)
code.append(int(arg))
position += 2
else:
raise Exception(
f"Argument {arg} out of boundaries on line {line_num}"
)
else:
raise Exception(
f"Invalid argument for `PUSH` on line {line_num}"
)
case _:
raise Exception(
f"`{opcode.name.upper()}` does not take an argument"
)
else:
raise Exception(
f"Instruction outside of any section on line {line_num}"
)
elif is_opcode0(token):
if data_mode:
raise Exception(f"Instruction inside data section on line {line_num}")
elif code_mode:
if token.upper() not in get_opcode_names():
raise Exception(f"Invalid mnemonic: `{token}` on line {line_num}")
opcode = Opcode[token.upper()]
if opcode in [Opcode.PUSH]:
raise Exception(f"`{token.upper()}` must take an argument")
code.append(opcode)
position += 1
else:
raise Exception(
f"Instruction outside of any section on line {line_num}"
)
else:
raise Exception(f"Invalid instruction format on line {line_num}")
if position >= MEMORY_SIZE:
raise Exception(
f"Program is too long, must be <= {MEMORY_SIZE} instructions"
)
return data, code, labels, code_position
def translate_stage_2(
data: dict[str, int | list[int]],
code: list[Opcode | str | int],
labels: dict[str, int],
) -> list[int]:
plain_data: list[int] = [
item
for element in data.values()
for item in (element if isinstance(element, list) else [element])
]
plain_data.insert(0, len(plain_data) + 1)
plain_code: list[int] = []
for element in code:
if isinstance(element, Opcode):
plain_code.append(element.value)
elif isinstance(element, str):
if element not in labels.keys():
raise Exception(f"Label not defined: {element}")
plain_code.append(labels[element])
elif isinstance(element, int):
plain_code.append(element)
return plain_data + plain_code
def value_to_binary32(value: int) -> str:
formatted_value = format(value & 0xFFFFFFFF, "032b")
indices = [i for i in range(0, 32 + 1, 4)]
return " ".join(
[formatted_value[i:j] for i, j in zip(indices, indices[1:] + [None])]
)
def comment_code(
data: dict[str, int | list[int]],
code: list[Opcode | str | int],
labels: dict[str, int],
code_position: int,
) -> str:
address = 0
commented_code = [
f"{address}\t{value_to_binary32(code_position)}\tstart_address = {code_position}"
]
for name, data_value in data.items():
if isinstance(data_value, int):
address += 1
commented_code.append(
f"{address}\t{value_to_binary32(data_value)}\t{name} = {data_value}"
)
elif isinstance(data_value, list):
address += 1
commented_code.append(
f"{address}\t{value_to_binary32(data_value[0])}\t{name} : {data_value[0]}"
)
for i in data_value[1:]:
address += 1
if data_value[0] == 0:
commented_code.append(
f"{address}\t{value_to_binary32(i)}\t{" " * len(name)} {i}"
)
else:
commented_code.append(
f"{address}\t{value_to_binary32(i)}\t{" " * len(name)} `{chr(i)}`"
)
for code_value in code:
address += 1
if isinstance(code_value, str):
commented_code.append(
f"{address}\t{value_to_binary32(labels[code_value])}\t{code_value} ({labels[code_value]})"
)
elif isinstance(code_value, Opcode):
commented_code.append(
f"{address}\t{value_to_binary32(code_value)}\t{code_value.name}"
)
elif isinstance(code_value, int):
commented_code.append(
f"{address}\t{value_to_binary32(code_value)}\t{code_value}"
)
return "\n".join(commented_code)
def translate(text: str) -> Tuple[list[int], str]:
data, code, labels, code_position = translate_stage_1(text)
translated_code = translate_stage_2(data, code, labels)
commented_code = comment_code(data, code, labels, code_position)
return translated_code, commented_code
def main(source_name: str, target_name: str) -> None:
with open(source_name, encoding="utf-8") as file:
text: str = file.read()
code, commented_code = translate(text)
write_code(target_name, code)
write_commented_code(target_name, commented_code)
print("Translation successful")
print(f"Source LoC: {len(text.splitlines())}, Number of Instructions: {len(code)}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="ASM code translator")
parser.add_argument("source_file", help="Source file name")
parser.add_argument("target_file", help="Target file name")
args = parser.parse_args()
try:
main(args.source_file, args.target_file)
except Exception as e:
logging.error(e)
raise e