gipu/python/assembler.py

131 lines
3.5 KiB
Python
Raw Normal View History

2017-05-15 11:49:11 +01:00
import sys
import re
import struct
op_names = ["MOVI",
"MOVR",
"GETM",
"PUTM",
"ADDI",
"ADDR",
"SUBI",
"SUBR",
"XORI",
"XORR",
"NOTI",
"NOTR",
"MULI",
"MULR",
"DIVI",
"DIVR",
"PUSH",
"POOP",
"CALL",
"HALT",
"NOPE"]
reg_names = ["R0", "R1", "R2", "R3", "S0", "S1", "S2", "S3", "IP", "BP", "SP"]
section_names = ["DATA:", "CODE:", "STACK:"]
2017-05-15 13:39:40 +01:00
section_flags = {s.casefold(): i + 1 for i, s in enumerate(section_names)}
ops = {s.casefold(): i for i, s in enumerate(op_names)}
regs = {s.casefold(): i for i, s in enumerate(reg_names)}
2017-05-15 11:49:11 +01:00
assembled = bytearray()
def to_uint8(data):
alphanum = re.compile("^[0-9]+$")
if isinstance(data, int):
return struct.pack("<B", data)
2017-05-15 11:49:11 +01:00
elif data.startswith("0x"):
return struct.pack("<B", int(data, 16))
2017-05-15 11:49:11 +01:00
elif alphanum.match(data): # only numbers
return struct.pack("<B", int(data))
2017-05-15 11:49:11 +01:00
else:
return None
2017-05-15 11:49:11 +01:00
def to_uint16(data):
alphanum = re.compile("^[0-9]+$")
if isinstance(data, int):
return struct.pack("<H", data)
2017-05-15 11:49:11 +01:00
elif data.startswith("0x"):
return struct.pack("<H", int(data, 16))
2017-05-15 11:49:11 +01:00
elif alphanum.match(data): # only numbers
return struct.pack("<H", int(data))
2017-05-15 11:49:11 +01:00
else:
return None
def is_reg(data):
2017-05-15 13:39:40 +01:00
if data not in [rn.casefold() for rn in reg_names]:
2017-05-15 11:49:11 +01:00
return False
return True
def movi(op, dst, src):
global assembled
2017-05-15 13:39:40 +01:00
if (is_reg(dst) and dst is not "ip"):
op_val = to_uint8(ops[op])
dst_val = to_uint8(regs[dst])
src_val = to_uint16(src)
if op_val and dst_val and src_val:
assembled += op_val + dst_val + src_val
else:
sys.stderr.write(
"ERROR WHILE ASSEMBLING UNKNOWN VALUES\n")
return False
else:
sys.stderr.write(
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst))
return False
return True
2017-05-15 11:49:11 +01:00
def assemble_code(line):
global assembled
2017-05-15 11:56:05 +01:00
sys.stdout.write("CODE: ")
2017-05-15 11:49:11 +01:00
instruction = [x for x in re.split('\W', line) if x]
2017-05-15 13:39:40 +01:00
op_name = instruction[0].casefold()
2017-05-15 11:49:11 +01:00
2017-05-15 13:39:40 +01:00
if op_name not in [on.casefold() for on in op_names]:
2017-05-15 11:49:11 +01:00
sys.stderr.write(
"ERROR WHILE ASSEMBLING UNKNOWN OPERATION: {}\n".format(op_name))
return False
2017-05-15 11:56:05 +01:00
sys.stdout.write("{} {}\n".format(op_name, ", ".join(instruction[1::])))
2017-05-15 13:39:40 +01:00
if op_name == "movi":
movi(op_name, instruction[1], instruction[2])
2017-05-15 11:49:11 +01:00
return True
def assemble_data(line):
sys.stdout.write("DATA:\t")
sys.stdout.write(line.strip(",") + "\n")
def main():
global assembled
if (len(sys.argv) < 2):
print("Usage: {} <file_to_assemble>".format(sys.argv[0]))
return
with open(sys.argv[1], 'r') as f:
2017-05-15 13:39:40 +01:00
gen = (line.casefold().strip("\n") for line in f if line != "\n")
flag = None
2017-05-15 11:49:11 +01:00
for line in gen:
2017-05-15 13:39:40 +01:00
if line.startswith(tuple([sn.casefold() for sn in section_names])):
2017-05-15 11:49:11 +01:00
flag = section_flags[line]
continue
2017-05-15 13:39:40 +01:00
if flag == section_flags["data:"]:
2017-05-15 11:49:11 +01:00
assemble_data(line)
2017-05-15 13:39:40 +01:00
elif flag == section_flags["code:"]:
2017-05-15 11:49:11 +01:00
assemble_code(line)
if not flag:
sys.stderr.write("Nothing was assembled! Did you use the section delimiters?\n")
2017-05-15 11:49:11 +01:00
with open("./out.gipu", 'wb') as f:
f.write(assembled)
if __name__ == '__main__':
main()