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