gipu/python/assembler.py

238 lines
7.1 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 14:31:12 +01:00
return None
2017-05-15 11:49:11 +01:00
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 14:31:12 +01:00
return None
2017-05-15 11:49:11 +01:00
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
2017-05-15 14:31:12 +01:00
def movi(op_str, dst_str, src_str):
global assembled
if is_reg(dst_str):
if dst_str != "ip":
op_val = to_uint8(ops[op_str])
dst_val = to_uint8(regs[dst_str])
src_val = to_uint16(src_str)
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("CAN'T MOVI TO IP!\n")
return False
else:
sys.stderr.write(
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst_str))
return False
return True
def movr(op_str, dst_str, src_str):
global assembled
2017-05-15 14:31:12 +01:00
if is_reg(dst_str) and is_reg(src_str):
if dst_str != "ip" and src_str != "ip":
op_val = to_uint8(ops[op_str])
dstsrc_val = (to_uint8(regs[dst_str])[0]
<< 4) ^ to_uint8(regs[src_str])[0]
if op_val and dstsrc_val:
assembled += op_val + to_uint8(dstsrc_val)
else:
sys.stderr.write(
"ERROR WHILE ASSEMBLING UNKNOWN VALUES\n")
return False
else:
sys.stderr.write("CAN'T MOVR IP!\n")
return False
else:
if not is_reg(dst_str):
sys.stderr.write(
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst_str))
else:
sys.stderr.write(
2017-05-15 14:31:12 +01:00
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(src_str))
return False
return True
def getm(op_str, dst_str, src_str):
global assembled
if is_reg(dst_str):
if dst_str != "ip":
op_val = to_uint8(ops[op_str])
dst_val = to_uint8(regs[dst_str])
src_val = to_uint16(src_str)
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("CAN'T MOVI TO IP!\n")
return False
else:
sys.stderr.write(
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst_str))
return False
return True
def putm(op_str, dst_str, src_str):
global assembled
if is_reg(src_str):
if src_str != "ip":
op_val = to_uint8(ops[op_str])
src_val = to_uint8(regs[src_str])
dst_val = to_uint16(dst_str)
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("CAN'T MOVI TO IP!\n")
return False
else:
sys.stderr.write(
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst_str))
return False
return True
def addi(op_str, dst_str, src_str):
global assembled
if is_reg(dst_str):
if src_str != "ip":
op_val = to_uint8(ops[op_str])
src_val = to_uint16(src_str)
dst_val = to_uint8(regs[dst_str])
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("CAN'T MOVI TO IP!\n")
return False
else:
sys.stderr.write(
2017-05-15 14:31:12 +01:00
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst_str))
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 14:31:12 +01:00
elif op_name == "movr":
movr(op_name, instruction[1], instruction[2])
elif op_name == "getm":
getm(op_name, instruction[1], instruction[2])
elif op_name == "putm":
putm(op_name, instruction[1], instruction[2])
elif op_name == "addi":
addi(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
2017-05-15 14:31:12 +01:00
if len(sys.argv) < 3:
print("Usage: {} file_to_assemble output".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:
2017-05-15 14:31:12 +01:00
sys.stderr.write(
"Nothing was assembled! Did you use the section delimiters?\n")
with open(sys.argv[2], 'wb') as f:
2017-05-15 11:49:11 +01:00
f.write(assembled)
if __name__ == '__main__':
main()