-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler.py
More file actions
323 lines (248 loc) · 10.3 KB
/
Copy pathassembler.py
File metadata and controls
323 lines (248 loc) · 10.3 KB
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
import importlib
import os
import argparse
from lib.lib import *
arg_parser = argparse.ArgumentParser(
exit_on_error = True,
prog = "BCOMP Assembler",
description = "This program assembles your '.asm' files in to '.o' files, using the commands specified in commands.txt",
usage="To assemble your code, you have to specify the path to your '.asm' file, and run it.\nFor additional information use the -h flag.",
)
arg_parser.add_argument('input_file', help="The path to your '.asm' file")
arg_parser.add_argument('-o', '--output-path', help="The path to the directory, that will contain your compiled code")
arg_parser.add_argument('-f', '--file-name', help="The name of your compiled file. File type will be ignored!")
arg_parser.add_argument('-cpp', '--cpp', default=False, action='store_true', help="Export as C++ array for importing to Arduino")
arg_parser.add_argument('-ino', '--arduino', default=False, action='store_true', help="Export as C++ array in to the specified arduino file")
arg_parser.add_argument('-v', '--version', default="0", type=str, help="The assembly version. Set to 0 to read from file. (Requires the 1st line to be a comment)")
arg_parser.add_argument('-lm', '--list-macros', default=False, action='store_true', help="List all available macros for the specified assembly version")
#Parse command line arguments
try:
arguments = arg_parser.parse_args()
except Exception as e:
print(f"{RED}ERROR: {str(e).capitalize()}{WHITE}")
exit(1)
"""if len(sys.argv[1::]) == 0:
print(f"{RED}No input file given!{WHITE}")
exit()
if len(sys.argv) > 1 and not ".asm" in sys.argv[1]:
print(f"{RED}Missing or invalid file type! (Must be .asm){WHITE}")
exit()"""
print(f"--- Loading {AQUA}commands{WHITE}")
commands, command_lookup = load_commands(True)
out = []
labels = {}
constants = {}
built_in = {
"RSYS": 0,
"RA": 1,
"RB": 2,
"RC": 3,
"RX": 4,
"RY": 5,
"RRADR": 6,
"RADR": 6,
"RWADR": 7,
}
read_lines = []
ASM_VERSION = arguments.version
MACRO = None # The dynamically loaded python macro library
print(f"--- Preprocessing: {AQUA}{arguments.input_file}{WHITE}")
try:
with open(arguments.input_file, "r", encoding="utf8") as f:
for i, line in enumerate(f):
# Determine assembly version
if arguments.version == "0" and i == 0 and line[0:5] == ";asm ":
ASM_VERSION = line[5::]
continue
line = line.strip().replace("\t", " ")
if line == "" or line[0] == ";":
print(f"{RED}SKIPPING{WHITE}")
continue
tokens = []
for t in line.split(" "):
if len(t) > 0 and t[0] == ";": break
tokens.append(t)
# Fix literal spaces (" ")
for i, t in enumerate(tokens):
if t == '"' and tokens[i-1] == '"':
tokens.insert(i-1, t + " " + tokens.pop(i))
tokens.pop(i)
# Join separated arguments, if not in string or is macro
if tokens[0][0] != "#" and len(tokens) > 2:
if len(tokens[1]) > 0 and tokens[1][0] != '"' and tokens[1][-1] == ',':
print("Comma!", tokens[1], tokens[2])
tokens.insert(1, [tokens.pop(1)[:-1:], tokens.pop(1)])
# make it min. 2 tokens, and wrap 2nd token
if len(tokens) == 1: tokens.append("0")
if tokens[1] == "" or tokens[1] == ";": tokens[1] = "0"
print(f"{GRAY}{i:>03d} {WHITE}{line}")
read_lines.append({
"index": i + 1,
"tokens": tokens
})
except FileNotFoundError as e:
print(f"{RED}File not found!{WHITE}")
exit(1)
ASM_VERSION = ASM_VERSION.strip()
print(f"--- Importing macros for assembly version: {AQUA}{ASM_VERSION}{WHITE}")
if ASM_VERSION == "0":
print(f"--- {GRAY}No assembly version specified!{WHITE}")
else:
MACRO = importlib.import_module(f"assembler_macros.v{ASM_VERSION.replace('.', '_')}")
#Executing macros
i = 0
while i < len(read_lines):
data = read_lines[i]
ins = data["tokens"][0]
arg = data["tokens"][1]
macro_name = ins[1::]
#Is macro?
if MACRO == None or ins[0] != "#":
i += 1
continue
print(f"{GRAY}Executing macro {AQUA}{macro_name}{WHITE}")
result = MACRO.Execute(macro_name, data["tokens"][1::])
if not result.success: exit(1)
inserted_code_length = len(result.instructions)
insertion_line_index = read_lines[i]["index"]
# Insert the code from the macro, but convert all int -> str
for macro_index, line in enumerate(result.instructions):
return_tokens = [(str(a) if type(a) == int else a) for a in line]
if type(return_tokens[1]) == list:
for j, t in enumerate(return_tokens[1]):
if type(t) == int: return_tokens[1][j] = str(t)
read_lines.insert(i + macro_index, {
"index": i + macro_index,
"tokens": return_tokens
})
# Remove the original macro line
read_lines.pop(i + inserted_code_length)
i += inserted_code_length
# Fix line indexes
for i, line in enumerate(read_lines): line["index"] = i
#for data in read_lines:
# print(data)
#exit()
print(f"--- Compiling: {AQUA}{arguments.input_file}{WHITE}")
#Scanning for labels, constants and macros
special_offset = 0 # Offset caused by labels, constants or macros (whole empty line)
for i, data in enumerate(read_lines):
index = data["index"]
ins = data["tokens"][0]
arg = data["tokens"][1]
special_name = ins[1::]
#Is label?
if ins[0] == ":":
print(f"{WHITE}Label found: {AQUA}{ins}{WHITE} value: {GRAY}{i - special_offset}")
labels[special_name] = i - special_offset
special_offset += 1
continue
#Is constant?
if ins[0] == "$":
print(f"{WHITE}Constant found: {AQUA}{ins}{WHITE} value: {GRAY}{arg}")
if arg[0] == '"': constants[special_name] = ord(arg[1:-1:][0]) # Get character index
else: constants[special_name] = parseNumber(arg)
special_offset += 1
continue
#Is built-in constant?
if ins[0] == "R":
print(f"{WHITE}Built-in constant found: {AQUA}{ins}{WHITE} value: {GRAY}{arg}")
if arg[0] == '"': constants[special_name] = ord(arg[1:-1:][0]) # Get character index
else: constants[special_name] = parseNumber(arg)
special_offset += 1
continue
# Reaplace label, constant and word values
for _, data in enumerate(read_lines):
index = data["index"]
ins = data["tokens"][0]
arg = data["tokens"][1]
print(f"{GRAY}{index:03d}: {AQUA}{ins}\t{WHITE}{arg}")
#Is label declaration?
if ins[0] == ":": continue
#Is constant declaration?
if ins[0] == "$": continue
ins = ins.lower()
#Is keyword?
if not ins in commands:
print(f"{RED}ERROR: Unrecognised keyword: '{ins.upper()}'!{WHITE}")
exit(1)
# Convert string and int arguments to list
if type(arg) == str: arg = [arg]
# Replace values in all arguments
for i, a in enumerate(arg):
label_arg = a.replace(":", "")
constant_arg = a.replace("$", "")
built_in_arg = a
#Replace label value in argument
if a[0] == ":":
if not label_arg in labels:
print(f"{RED}ERROR: Unrecognised label: {WHITE}{label_arg}")
exit(1)
arg[i] = str(labels[label_arg])
print(f"{GRAY}Replacing label {AQUA}{label_arg}{GRAY} with {WHITE}{labels[label_arg]}")
#Replace constant value in argument
if a[0] == "$":
if not constant_arg in constants:
print(f"{RED}ERROR: Unrecognised constant: {WHITE}{constant_arg}")
exit(1)
arg[i] = str(constants[constant_arg])
print(f"{GRAY}Replacing constant {AQUA}{constant_arg}{GRAY} with {WHITE}{constants[constant_arg]}")
#Replace built-in constant value in argument
if a[0] == "R":
if not built_in_arg in built_in:
print(f"{RED}ERROR: Unrecognised built-in value: {WHITE}{built_in_arg}")
exit(1)
arg[i] = str(built_in[built_in_arg])
print(f"{GRAY}Replacing built-in value {AQUA}{built_in_arg}{GRAY} with {WHITE}{built_in[built_in_arg]}")
#Append command index
out.append(commands[ins][0])
# Join values to a single number and append argument
bin_arg = ""
for i, a in enumerate(arg):
#print(i, "a:", a)
try:
bin_arg = decToBin(parseNumber(a, throwError = True), 16 // len(arg)) + bin_arg
except Exception as e:
print(f"{RED}ERROR: Invalid argument! (Numbers only){WHITE}")
exit(1)
#print("bin_arg", bin_arg)
out.append(binToDec(bin_arg))
#Writing to file
def path_leaf(path):
head, tail = os.path.split(path)
return tail or os.basename(head)
def give_new_type(full_file_path, new_type):
return ".".join(path_leaf(full_file_path).split(".")[0:-1]) + new_type
save_path = "./binaries/" if os.path.exists("./binaries/") else "./"
save_name = give_new_type(arguments.input_file, ".o")
if arguments.output_path: save_path = arguments.output_path
if arguments.file_name: save_name = give_new_type(arguments.file_name + ".D", ".o")
# Reset save name if saving to .h file
if arguments.arduino: save_name = ""
f = open(save_path + save_name, "w", encoding="utf8")
print(f"--- Writing to: {AQUA}{os.path.join(save_path, save_name)}{WHITE}")
if arguments.cpp:
# As valid C++ code
for i,ins in enumerate(out[::2]):
arg = out[i*2+1]
f.write("{" + str(ins) + "," + str(arg) + "},\n")
if arguments.arduino:
# As valid C++ code to rom.h
f.write("typedef struct {\n")
f.write(" unsigned int ins;\n")
f.write(" unsigned int arg;\n")
f.write("} Command;\n")
f.write("\n")
f.write("const PROGMEM Command rom[] = {")
for i,ins in enumerate(out[::2]):
arg = out[i*2+1]
f.write("\n{" + str(ins) + "," + str(arg) + "},")
f.write("\n};\n\n")
f.write(f"int rom_index = 0;\n")
f.write(f"int rom_length = sizeof(rom) / sizeof(rom[0]);")
else:
# As comma separated values
for i,ins in enumerate(out[::2]):
#print(f"{AQUA}{ins}\t{WHITE}{out[i*2+1]}\t({AQUA}{command_lookup[int(ins)]}{WHITE})")
f.write(f"{ins},{out[i*2+1]}{';' if i*2+2 < len(out) else ''}")
f.close()