2017-05-15 11:49:11 +01:00
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import struct
|
2017-05-16 16:39:49 +01:00
|
|
|
import IPython
|
2017-05-17 10:01:47 +01:00
|
|
|
import copy
|
2017-05-16 16:39:49 +01:00
|
|
|
|
|
|
|
|
2017-05-17 10:01:47 +01:00
|
|
|
class AssemblerException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidRegister(AssemblerException):
|
2017-05-16 17:47:51 +01:00
|
|
|
|
|
|
|
def __init__(self, register):
|
|
|
|
super().__init__("Invalid register: {}".format(register))
|
|
|
|
|
|
|
|
|
2017-05-17 10:01:47 +01:00
|
|
|
class InvalidOperation(AssemblerException):
|
2017-05-16 17:47:51 +01:00
|
|
|
|
|
|
|
def __init__(self, operation):
|
|
|
|
super().__init__("Invalid operation: {}".format(operation))
|
|
|
|
|
|
|
|
|
2017-05-17 10:01:47 +01:00
|
|
|
class ExpectedImmediate(AssemblerException):
|
2017-05-16 17:47:51 +01:00
|
|
|
|
|
|
|
def __init__(self, value):
|
|
|
|
super().__init__("Expected immediate, got {}".format(value))
|
|
|
|
|
|
|
|
|
2017-05-17 10:01:47 +01:00
|
|
|
class ExpectedRegister(AssemblerException):
|
2017-05-16 17:47:51 +01:00
|
|
|
|
|
|
|
def __init__(self, value):
|
|
|
|
super().__init__("Expected register, got {}".format(value))
|
|
|
|
|
|
|
|
|
2017-05-17 10:01:47 +01:00
|
|
|
class IPOverwrite(AssemblerException):
|
2017-05-16 17:47:51 +01:00
|
|
|
|
|
|
|
def __init__(self, instruction=None):
|
|
|
|
if instruction:
|
|
|
|
super().__init__("IP can't be overwritten. Instruction: {}".format(instruction))
|
|
|
|
else:
|
|
|
|
super().__init__("IP can't be overwritten.")
|
|
|
|
|
|
|
|
|
2017-05-17 10:01:47 +01:00
|
|
|
class InvalidValue(AssemblerException):
|
2017-05-16 17:47:51 +01:00
|
|
|
|
|
|
|
def __init__(self, instruction):
|
|
|
|
super().__init__("Invalid value while assembling: {}".format(instruction))
|
|
|
|
|
2017-05-17 10:01:47 +01:00
|
|
|
rol = lambda val, r_bits, max_bits: \
|
2017-05-17 17:58:00 +01:00
|
|
|
(val << r_bits % max_bits) & (2**max_bits - 1) | \
|
|
|
|
((val & (2**max_bits - 1)) >> (max_bits - (r_bits % max_bits)))
|
|
|
|
|
2017-05-16 17:47:51 +01:00
|
|
|
|
2017-05-16 16:39:49 +01:00
|
|
|
class VMAssembler:
|
2017-05-17 10:01:47 +01:00
|
|
|
|
|
|
|
def __init__(self, key):
|
|
|
|
self.assembled_code = bytearray()
|
|
|
|
self.define_ops(key)
|
2017-05-16 16:39:49 +01:00
|
|
|
|
|
|
|
def parse(self, instruction):
|
2017-05-18 14:41:05 +01:00
|
|
|
action = getattr(self, "{}".format(instruction.opcode.method))
|
2017-05-16 16:39:49 +01:00
|
|
|
action(instruction)
|
|
|
|
|
|
|
|
def process_code_line(self, line):
|
|
|
|
sys.stdout.write("CODE: ")
|
|
|
|
components = [x for x in re.split('\W', line) if x]
|
|
|
|
instruction = VMInstruction(components[0], components[1:])
|
2017-05-18 12:20:17 +01:00
|
|
|
sys.stdout.write(str(instruction) + "\n")
|
2017-05-16 16:39:49 +01:00
|
|
|
self.parse(instruction)
|
|
|
|
|
|
|
|
def imm2reg(self, instruction):
|
|
|
|
"""
|
|
|
|
Intel syntax -> REG, IMM
|
|
|
|
"""
|
|
|
|
opcode = instruction.opcode
|
|
|
|
reg = instruction.args[0]
|
|
|
|
imm = instruction.args[1]
|
2017-05-17 10:01:47 +01:00
|
|
|
if reg.name == "ip":
|
|
|
|
raise IPOverwrite(instruction)
|
|
|
|
if not imm.isimm():
|
|
|
|
raise ExpectedImmediate(imm)
|
|
|
|
if not reg.isreg():
|
|
|
|
raise ExpectedRegister(reg)
|
|
|
|
if not opcode.uint8() or not reg.uint8() or not imm.uint16():
|
|
|
|
raise InvalidValue(instruction)
|
|
|
|
self.assembled_code += opcode.uint8() + reg.uint8() + imm.uint16()
|
2017-05-16 17:47:51 +01:00
|
|
|
return
|
2017-05-16 16:39:49 +01:00
|
|
|
|
|
|
|
def reg2reg(self, instruction):
|
2017-05-17 17:58:00 +01:00
|
|
|
"""
|
|
|
|
Intel syntax -> DST_REG, SRC_REG
|
|
|
|
"""
|
|
|
|
opcode = instruction.opcode
|
|
|
|
dst_reg = instruction.args[0]
|
|
|
|
src_reg = instruction.args[1]
|
|
|
|
if dst_reg.name == "ip" or src_reg.name == "ip":
|
|
|
|
raise IPOverwrite(instruction)
|
|
|
|
if not dst_reg.isreg():
|
|
|
|
raise ExpectedRegister(dst_reg)
|
|
|
|
if not src_reg.isreg():
|
|
|
|
raise ExpectedRegister(src_reg)
|
|
|
|
if not opcode.uint8() or not dst_reg.uint8() or not src_reg.uint8():
|
|
|
|
raise InvalidValue(instruction)
|
|
|
|
byte_with_nibbles = struct.pack("<B", dst_reg.uint8()[0] << 4 ^ (
|
|
|
|
src_reg.uint8()[0] & 0b00001111))
|
|
|
|
self.assembled_code += opcode.uint8() + byte_with_nibbles
|
2017-05-16 16:39:49 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
def reg2imm(self, instruction):
|
|
|
|
"""
|
|
|
|
Intel syntax -> IMM, REG
|
|
|
|
"""
|
|
|
|
opcode = instruction.opcode
|
|
|
|
imm = instruction.args[0]
|
|
|
|
reg = instruction.args[1]
|
2017-05-17 10:01:47 +01:00
|
|
|
if reg.name == "ip":
|
|
|
|
raise IPOverwrite(instruction)
|
|
|
|
if not imm.isimm():
|
|
|
|
raise ExpectedImmediate(imm)
|
|
|
|
if not reg.isreg():
|
|
|
|
raise ExpectedRegister(reg)
|
|
|
|
if not opcode.uint8() or not reg.uint8() or not imm.uint16():
|
|
|
|
raise InvalidValue(instruction)
|
|
|
|
self.assembled_code += opcode.uint8() + imm.uint16() + reg.uint8()
|
2017-05-16 17:47:51 +01:00
|
|
|
return
|
2017-05-16 16:39:49 +01:00
|
|
|
|
2017-05-18 14:41:05 +01:00
|
|
|
def byt2reg(self, instruction):
|
|
|
|
"""
|
|
|
|
Intel syntax -> REG, [BYTE]IMM
|
|
|
|
"""
|
|
|
|
opcode = instruction.opcode
|
|
|
|
reg = instruction.args[0]
|
|
|
|
imm = instruction.args[1]
|
|
|
|
if reg.name == "ip":
|
|
|
|
raise IPOverwrite(instruction)
|
|
|
|
if not imm.isimm():
|
|
|
|
raise ExpectedImmediate(imm)
|
|
|
|
if not reg.isreg():
|
|
|
|
raise ExpectedRegister(reg)
|
|
|
|
if not opcode.uint8() or not reg.uint8() or not imm.uint8():
|
|
|
|
raise InvalidValue(instruction)
|
|
|
|
self.assembled_code += opcode.uint8() + reg.uint8() + imm.uint8()
|
2017-05-16 16:39:49 +01:00
|
|
|
return
|
|
|
|
|
2017-05-18 14:41:05 +01:00
|
|
|
def regonly(self, instruction):
|
|
|
|
"""
|
|
|
|
Instruction with only an argument: a register
|
|
|
|
"""
|
|
|
|
opcode = instruction.opcode
|
|
|
|
reg = instruction.args[0]
|
|
|
|
if reg.name == "ip":
|
|
|
|
raise IPOverwrite(instruction)
|
|
|
|
if not reg.isreg():
|
|
|
|
raise ExpectedRegister(reg)
|
|
|
|
if not opcode.uint8() or not reg.uint8():
|
|
|
|
raise InvalidValue(instruction)
|
|
|
|
self.assembled_code += opcode.uint8() + reg.uint8()
|
|
|
|
return
|
2017-05-16 16:39:49 +01:00
|
|
|
|
2017-05-18 14:41:05 +01:00
|
|
|
def immonly(self, instruction):
|
|
|
|
"""
|
|
|
|
Instruction with only an argument: an immediate
|
|
|
|
"""
|
|
|
|
opcode = instruction.opcode
|
|
|
|
imm = instruction.args[0]
|
|
|
|
if not imm.isimm():
|
|
|
|
raise ExpectedImmediate(imm)
|
|
|
|
if not opcode.uint8() or not imm.uint16():
|
|
|
|
raise InvalidValue(instruction)
|
|
|
|
self.assembled_code += opcode.uint8() + imm.uint16()
|
|
|
|
return
|
2017-05-16 16:39:49 +01:00
|
|
|
|
2017-05-18 14:41:05 +01:00
|
|
|
def single(self, instruction):
|
|
|
|
"""
|
|
|
|
Instruction with no arguments
|
|
|
|
"""
|
|
|
|
opcode = instruction.opcode
|
|
|
|
self.assembled_code += opcode.uint8()
|
|
|
|
return
|
2017-05-17 17:58:00 +01:00
|
|
|
|
2017-05-17 10:01:47 +01:00
|
|
|
def define_ops(self, key):
|
|
|
|
key_ba = bytearray(key, 'utf-8')
|
|
|
|
olds = copy.deepcopy(ops)
|
|
|
|
for b in key_ba:
|
|
|
|
for op_com in ops:
|
2017-05-18 14:41:05 +01:00
|
|
|
op_com.set_value(rol(b ^ op_com.value, b % 8, 8))
|
2017-05-17 10:01:47 +01:00
|
|
|
for i in ops:
|
|
|
|
for j in ops:
|
|
|
|
j.set_value(rol(j.value, i.value % 8, 8))
|
|
|
|
for o, n in zip(olds, ops):
|
|
|
|
print("{} : {}->{}".format(o.name, hex(o.value), hex(n.value)))
|
|
|
|
|
2017-05-16 16:39:49 +01:00
|
|
|
|
|
|
|
class VMComponent:
|
|
|
|
"""
|
2017-05-16 17:47:51 +01:00
|
|
|
Represents a register, operation or an immediate the VM recognizes
|
2017-05-16 16:39:49 +01:00
|
|
|
"""
|
|
|
|
|
2017-05-18 14:41:05 +01:00
|
|
|
def __init__(self, name, value, method=None):
|
2017-05-16 17:47:51 +01:00
|
|
|
self.name = name.casefold()
|
2017-05-16 16:39:49 +01:00
|
|
|
self.value = value
|
2017-05-18 14:41:05 +01:00
|
|
|
self.method = method
|
2017-05-16 16:39:49 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "{}".format(self.name)
|
|
|
|
|
2017-05-17 10:01:47 +01:00
|
|
|
def set_name(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def set_value(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
2017-05-16 16:39:49 +01:00
|
|
|
def uint8(self):
|
|
|
|
numre = re.compile("^[0-9]+$")
|
|
|
|
if isinstance(self.value, int):
|
|
|
|
return struct.pack("<B", self.value)
|
|
|
|
elif self.value.startswith("0x"):
|
|
|
|
return struct.pack("<B", int(self.value, 16))
|
|
|
|
elif numre.match(self.value): # only numbers
|
|
|
|
return struct.pack("<B", int(self.value))
|
|
|
|
return None
|
|
|
|
|
|
|
|
def uint16(self):
|
|
|
|
numre = re.compile("^[0-9]+$")
|
|
|
|
if isinstance(self.value, int):
|
|
|
|
return struct.pack("<H", self.value)
|
|
|
|
elif self.value.startswith("0x"):
|
|
|
|
return struct.pack("<H", int(self.value, 16))
|
|
|
|
elif numre.match(self.value): # only numbers
|
|
|
|
return struct.pack("<H", int(self.value))
|
|
|
|
return None
|
|
|
|
|
2017-05-16 17:47:51 +01:00
|
|
|
def isreg(self):
|
|
|
|
if self.name not in [x.casefold() for x in reg_names]:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def isop(self):
|
2017-05-18 14:41:05 +01:00
|
|
|
if self.name not in [x[0].casefold() for x in op_names]:
|
2017-05-16 17:47:51 +01:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def isimm(self):
|
|
|
|
if self.name != self.value:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2017-05-16 16:39:49 +01:00
|
|
|
|
|
|
|
class VMInstruction:
|
|
|
|
"""
|
|
|
|
Represents an instruction the VM recognizes.
|
|
|
|
e.g: MOVI [R0, 2]
|
|
|
|
^ ^
|
|
|
|
opcode args
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, opcode, instr_list):
|
|
|
|
immediate_regexp = re.compile("^(0x*|[0-9]*$)")
|
2017-05-18 12:20:17 +01:00
|
|
|
self.opcode = next((x for x in ops if x.name == opcode), None)
|
2017-05-16 16:39:49 +01:00
|
|
|
self.args = []
|
|
|
|
for el in instr_list:
|
|
|
|
if not immediate_regexp.match(el):
|
|
|
|
# create a VM component for a register
|
2017-05-18 12:20:17 +01:00
|
|
|
reg_comp = next((x for x in regs if x.name == el), None)
|
|
|
|
self.args.append(reg_comp)
|
2017-05-16 16:39:49 +01:00
|
|
|
else:
|
|
|
|
# directly append the immediate
|
|
|
|
self.args.append(VMComponent(el, el))
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "{} {}".format(self.opcode.name, ", ".join([x.name for x in self.args]))
|
2017-05-15 11:49:11 +01:00
|
|
|
|
2017-05-18 14:41:05 +01:00
|
|
|
op_names = [["MOVI", "imm2reg"],
|
|
|
|
["MOVR", "reg2reg"],
|
|
|
|
["LOAD", "imm2reg"],
|
|
|
|
["STOR", "reg2imm"],
|
|
|
|
["ADDI", "imm2reg"],
|
|
|
|
["ADDR", "reg2reg"],
|
|
|
|
["SUBI", "imm2reg"],
|
|
|
|
["SUBR", "reg2reg"],
|
|
|
|
["XORB", "byt2reg"],
|
|
|
|
["XORW", "imm2reg"],
|
|
|
|
["XORR", "reg2reg"],
|
|
|
|
["NOTR", "regonly"],
|
|
|
|
["MULI", "imm2reg"],
|
|
|
|
["MULR", "reg2reg"],
|
|
|
|
["DIVI", "imm2reg"],
|
|
|
|
["DIVR", "reg2reg"],
|
|
|
|
["PUSH", "regonly"],
|
|
|
|
["POOP", "regonly"],
|
|
|
|
["CMPI", "imm2reg"],
|
|
|
|
["CMPR", "reg2reg"],
|
|
|
|
["JMPI", "immonly"],
|
|
|
|
["JMPR", "regonly"],
|
|
|
|
["JPAI", "immonly"],
|
|
|
|
["JPAR", "regonly"],
|
|
|
|
["JPBI", "immonly"],
|
|
|
|
["JPBR", "regonly"],
|
|
|
|
["JPEI", "immonly"],
|
|
|
|
["JPER", "regonly"],
|
|
|
|
["SHIT", "single"],
|
|
|
|
["NOPE", "single"],
|
|
|
|
["GRMN", "single"]]
|
2017-05-17 10:01:47 +01:00
|
|
|
|
2017-05-15 11:49:11 +01:00
|
|
|
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)}
|
2017-05-18 14:41:05 +01:00
|
|
|
ops = [VMComponent(le[0], i, le[1]) for i, le in enumerate(op_names)]
|
2017-05-16 16:39:49 +01:00
|
|
|
regs = [VMComponent(s.casefold(), i) for i, s in enumerate(reg_names)]
|
|
|
|
|
2017-05-16 17:47:51 +01:00
|
|
|
|
2017-05-15 11:49:11 +01:00
|
|
|
def assemble_data(line):
|
|
|
|
sys.stdout.write("DATA:\t")
|
|
|
|
sys.stdout.write(line.strip(",") + "\n")
|
|
|
|
|
2017-05-16 17:47:51 +01:00
|
|
|
|
2017-05-15 11:49:11 +01:00
|
|
|
def main():
|
2017-05-17 10:01:47 +01:00
|
|
|
if len(sys.argv) < 4:
|
2017-05-17 17:58:00 +01:00
|
|
|
print("Usage: {} opcodes_key file_to_assemble output".format(
|
|
|
|
sys.argv[0]))
|
2017-05-15 12:24:30 +01:00
|
|
|
return
|
2017-05-17 10:01:47 +01:00
|
|
|
vma = VMAssembler(sys.argv[1])
|
|
|
|
with open(sys.argv[2], 'r') as f:
|
|
|
|
gen = (line.casefold().strip() for line in f if line != "\n")
|
2017-05-15 12:24:30 +01:00
|
|
|
flag = None
|
2017-05-15 11:49:11 +01:00
|
|
|
|
|
|
|
for line in gen:
|
2017-05-16 16:39:49 +01:00
|
|
|
if line in section_flags:
|
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-16 16:39:49 +01:00
|
|
|
vma.process_code_line(line)
|
2017-05-15 13:39:40 +01:00
|
|
|
elif flag == section_flags["code:"]:
|
2017-05-16 16:39:49 +01:00
|
|
|
vma.process_code_line(line)
|
2017-05-15 12:24:30 +01:00
|
|
|
if not flag:
|
2017-05-15 14:31:12 +01:00
|
|
|
sys.stderr.write(
|
|
|
|
"Nothing was assembled! Did you use the section delimiters?\n")
|
2017-05-17 10:01:47 +01:00
|
|
|
with open(sys.argv[3], 'wb') as f:
|
2017-05-16 16:39:49 +01:00
|
|
|
f.write(vma.assembled_code)
|
2017-05-15 11:49:11 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|