1
0

Did a little overhauling for the OPCODES.

This commit is contained in:
Jon Michael Aanes 2015-12-16 02:02:19 +01:00
parent 5fb171d25c
commit f564b27fa6

View File

@ -7,7 +7,8 @@ REG_STATUS_TO_COLOR = {
"insert": "green", "insert": "green",
"change": "yellow", "change": "yellow",
"remove": "red", "remove": "red",
"none": "black" "none": "black",
"jump": "orange"
} }
class TikzPainter: class TikzPainter:
@ -61,7 +62,7 @@ class TikzPainter:
pos -= 0.5 pos -= 0.5
def drawNames (self, emu): def drawNames (self, emu):
x = -1 x = 0
pos = 0 pos = 0
for reg_name in self.registers: for reg_name in self.registers:
self.addText("\t\\node[text_node]() at ({}, {}){{{}}};\n", x, pos, reg_name[1:]) self.addText("\t\\node[text_node]() at ({}, {}){{{}}};\n", x, pos, reg_name[1:])
@ -145,6 +146,18 @@ class Emulator:
def getLineSignature (self, line_nr): def getLineSignature (self, line_nr):
return [ "\\"+token if token[0]=="$" or token[0]=="%" else token for token in self.code[line_nr] ] return [ "\\"+token if token[0]=="$" or token[0]=="%" else token for token in self.code[line_nr] ]
def pushToStack (self, new_element):
self.registers['%rsp'] = self.registers['%rsp'] + 1
self.stack[self.registers['%rsp']] = new_element
self.changedRegisters(('%rsp',"change"), ("m"+str(self.registers['%rsp']),"insert"))
self.max_stack_size = max(self.registers['%rsp'], self.max_stack_size)
def popFromStack (self):
temp = self.stack[self.registers['%rsp']]
self.registers['%rsp'] -= 1
self.changedRegisters(('%rsp',"change"),("m"+str(self.registers['%rsp']+1),"remove"))
return temp
def iterate (self): def iterate (self):
old_rip = self.registers['%rip'] old_rip = self.registers['%rip']
self.last_sp = self.registers['%rsp'] self.last_sp = self.registers['%rsp']
@ -153,26 +166,24 @@ class Emulator:
instruct = self.code[self.registers['%rip']] instruct = self.code[self.registers['%rip']]
opcode = instruct[0] opcode = instruct[0]
self.changes = {} self.changes = {}
if opcode == "push" or opcode == "pushq": if opcode[:4] == "push":
self.registers['%rsp'] = self.registers['%rsp'] + 1 self.pushToStack(self.getVal(instruct[1]))
self.stack[self.registers['%rsp']] = self.registers[instruct[1]] elif opcode[:3] == "pop":
self.changedRegisters(('%rsp',"change"),("m"+str(self.registers['%rsp']),"insert")) self.registers[instruct[1]] = self.popFromStack()
self.max_stack_size = max(self.registers['%rsp'], self.max_stack_size) self.changedRegisters((instruct[1],"change"))
elif opcode == "popq":
self.registers[instruct[1]] = self.stack[self.registers['%rsp']] elif opcode[:3] == "mov":
self.registers['%rsp'] = self.registers['%rsp'] - 1
self.changedRegisters(('%rsp',"change"),("m"+str(self.registers['%rsp']+1),"remove"))
elif opcode == "movq" or opcode == "mov":
self.changedRegisters((instruct[2],"change")) self.changedRegisters((instruct[2],"change"))
self.registers[instruct[2]] = self.getVal(instruct[1]) self.registers[instruct[2]] = self.getVal(instruct[1])
elif opcode == "cmpq": elif opcode[:3] == "add":
self.compareVal(instruct[1],instruct[2])
elif opcode == "addq":
self.changedRegisters((instruct[2],"change")) self.changedRegisters((instruct[2],"change"))
self.registers[instruct[2]] = self.getVal(instruct[2]) + self.getVal(instruct[1]) self.registers[instruct[2]] = self.getVal(instruct[2]) + self.getVal(instruct[1])
elif opcode == "subq": elif opcode[:3] == "sub":
self.changedRegisters((instruct[2],"change")) self.changedRegisters((instruct[2],"change"))
self.registers[instruct[2]] = self.getVal(instruct[2]) - self.getVal(instruct[1]) self.registers[instruct[2]] = self.getVal(instruct[2]) - self.getVal(instruct[1])
elif opcode[:3] == "cmp":
self.compareVal(instruct[1],instruct[2])
elif opcode == "jg": elif opcode == "jg":
if self.status["g"]: if self.status["g"]:
self.registers['%rip'] = self.labels[instruct[1]] self.registers['%rip'] = self.labels[instruct[1]]
@ -180,26 +191,18 @@ class Emulator:
self.registers['%rip'] = self.labels[instruct[1]] self.registers['%rip'] = self.labels[instruct[1]]
elif opcode == "call": elif opcode == "call":
self.registers['%rsp'] = self.registers['%rsp'] + 1 self.pushToStack(self.registers['%rip']+1)
self.stack[self.registers['%rsp']] = self.registers['%rip']+1
self.registers['%rip'] = self.labels[instruct[1]] self.registers['%rip'] = self.labels[instruct[1]]
self.changedRegisters(('%rsp',"change"),("m"+str(self.registers['%rsp']),"insert"))
self.max_stack_size = max(self.registers['%rsp'], self.max_stack_size)
elif opcode == "leave": elif opcode == "leave":
self.registers["%rsp"] = self.registers["%rbp"] self.registers["%rsp"] = self.registers["%rbp"]
self.registers["%rbp"] = self.stack[self.registers['%rsp']] self.registers["%rbp"] = self.popFromStack()
self.registers['%rsp'] = self.registers['%rsp'] - 1
self.changedRegisters(('%rbp',"change"))
self.changedRegisters(('%rsp',"change"),("m"+str(self.registers['%rsp']+1),"remove"))
elif opcode == "ret": elif opcode == "ret":
self.registers['%rip'] = self.stack[self.registers['%rsp']] self.registers['%rip'] = self.popFromStack()
self.registers['%rsp'] = self.registers['%rsp'] - 1
self.changedRegisters(('%rsp',"change"),("m"+str(self.registers['%rsp']+1),"remove"))
if self.registers['%rip'] == old_rip: if self.registers['%rip'] == old_rip:
self.registers['%rip'] += 1 self.registers['%rip'] += 1
else: else:
self.changedRegisters(('%rip',"change")) self.changedRegisters(('%rip',"jump"))
return old_rip return old_rip