Changed stack architecture to move downwards.
TikzPainter have been adjusted for the change. Stack limits are now shown.
This commit is contained in:
parent
e4ff1d4420
commit
9688172806
15
Emulator.py
15
Emulator.py
|
@ -8,11 +8,13 @@ REGISTERS=["%rax", "%rbx", "%rcx", "%rdx", "%rsp", "%rbp", "%rsi", "%rdi",
|
||||||
|
|
||||||
class Emulator:
|
class Emulator:
|
||||||
|
|
||||||
def __init__ (self, source_text):
|
def __init__ (self, source_text, max_stack_size=1000):
|
||||||
self.registers = {}
|
self.registers = {}
|
||||||
for reg_name in REGISTERS:
|
for reg_name in REGISTERS:
|
||||||
self.registers[reg_name] = "Junk"
|
self.registers[reg_name] = "Junk"
|
||||||
self.stack = {}
|
self.stack = {}
|
||||||
|
for i in range(max_stack_size):
|
||||||
|
self.stack[i] = 0
|
||||||
self.code = []
|
self.code = []
|
||||||
self.labels = {}
|
self.labels = {}
|
||||||
self.changes = {}
|
self.changes = {}
|
||||||
|
@ -28,12 +30,13 @@ class Emulator:
|
||||||
Sets various stack elements, starting from 0 and going up.
|
Sets various stack elements, starting from 0 and going up.
|
||||||
Automatically sets rsp. This can be disabled by passing set_rsp=False.
|
Automatically sets rsp. This can be disabled by passing set_rsp=False.
|
||||||
"""
|
"""
|
||||||
i = -1
|
i = len(self.stack)
|
||||||
for element in stack_list:
|
for element in stack_list:
|
||||||
i += 1
|
i -= 1
|
||||||
self.stack[i] = element
|
self.stack[i] = element
|
||||||
if (not 'set_rsp' in kwargs) or kwargs['set_rsp']:
|
if (not 'set_rsp' in kwargs) or kwargs['set_rsp']:
|
||||||
self.setRegs(rsp=i)
|
self.setRegs(rsp=i)
|
||||||
|
self.last_sp = i-1
|
||||||
|
|
||||||
def setRegs (self, **reg_dict):
|
def setRegs (self, **reg_dict):
|
||||||
for reg_name, reg_val in reg_dict.iteritems():
|
for reg_name, reg_val in reg_dict.iteritems():
|
||||||
|
@ -82,15 +85,15 @@ class Emulator:
|
||||||
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):
|
def pushToStack (self, new_element):
|
||||||
self.registers['%rsp'] = self.registers['%rsp'] + 1
|
self.registers['%rsp'] -= 1
|
||||||
self.stack[self.registers['%rsp']] = new_element
|
self.stack[self.registers['%rsp']] = new_element
|
||||||
self.changedRegisters(('%rsp',"change"), ("m"+str(self.registers['%rsp']),"insert"))
|
self.changedRegisters(('%rsp',"change"), ("m"+str(self.registers['%rsp']),"insert"))
|
||||||
self.max_stack_size = max(self.registers['%rsp'], self.max_stack_size)
|
self.max_stack_size = max(self.registers['%rsp'], self.max_stack_size)
|
||||||
|
|
||||||
def popFromStack (self):
|
def popFromStack (self):
|
||||||
temp = self.stack[self.registers['%rsp']]
|
temp = self.stack[self.registers['%rsp']]
|
||||||
self.registers['%rsp'] -= 1
|
self.changedRegisters(('%rsp',"change"),("m"+str(self.registers['%rsp']),"remove"))
|
||||||
self.changedRegisters(('%rsp',"change"),("m"+str(self.registers['%rsp']+1),"remove"))
|
self.registers['%rsp'] += 1
|
||||||
return temp
|
return temp
|
||||||
|
|
||||||
def iterate (self):
|
def iterate (self):
|
||||||
|
|
|
@ -9,11 +9,12 @@ REG_STATUS_TO_COLOR = {
|
||||||
|
|
||||||
class TikzPainter:
|
class TikzPainter:
|
||||||
|
|
||||||
def __init__ (self, registers=None, max_stack_size=8):
|
def __init__ (self, registers=None, max_stack_size=8, stack_draw_mode="up"):
|
||||||
self.registers = registers if registers else REGISTERS
|
self.registers = registers if registers else REGISTERS
|
||||||
self.text = []
|
self.text = []
|
||||||
self.pos_x = 0
|
self.pos_x = 0
|
||||||
self.max_stack_size = max_stack_size
|
self.max_stack_size = max_stack_size
|
||||||
|
self.stack_draw_mode = stack_draw_mode
|
||||||
|
|
||||||
def addText (self, str, *args):
|
def addText (self, str, *args):
|
||||||
self.text.append(str.format(*args))
|
self.text.append(str.format(*args))
|
||||||
|
@ -21,6 +22,33 @@ class TikzPainter:
|
||||||
def getRegColor (self, reg_state):
|
def getRegColor (self, reg_state):
|
||||||
return REG_STATUS_TO_COLOR[reg_state]
|
return REG_STATUS_TO_COLOR[reg_state]
|
||||||
|
|
||||||
|
def drawStackUpward (self, pos, emu, x):
|
||||||
|
base_pointer = emu.getVal('%rbp')
|
||||||
|
|
||||||
|
pos -= 1.5
|
||||||
|
base_sp = min(emu.last_sp, emu.getVal('%rsp'))
|
||||||
|
pos += 0.5 * max(0, emu.getVal('%rsp')-emu.last_sp)
|
||||||
|
for index in range(base_sp, 3+min(emu.max_stack_size, emu.getVal('%rsp')+self.max_stack_size)):
|
||||||
|
reg_state = emu.regState("m"+str(index))
|
||||||
|
reg_dect = "register_node, fill="+self.getRegColor(reg_state)+"!10"
|
||||||
|
self.addText("\t\\node[{}]() at ({}, {}){{{}}};\n",
|
||||||
|
reg_dect, x, pos, emu.stack[index])
|
||||||
|
if base_pointer == index:
|
||||||
|
self.addText("\t\\draw ({0},{2}) -- ({1},{2});\n",x-1.1,x+1.1,pos-0.25)
|
||||||
|
base_pointer = emu.stack[base_pointer]
|
||||||
|
pos -= 0.5
|
||||||
|
return pos, x
|
||||||
|
|
||||||
|
def drawStackDownward (self, pos, emu, x):
|
||||||
|
pos -= 1
|
||||||
|
for index in range(emu.getVal('%rsp')-8, max(emu.last_sp, emu.getVal('%rsp'))):
|
||||||
|
reg_state = emu.regState("m"+str(index))
|
||||||
|
reg_dect = "register_node, fill="+self.getRegColor(reg_state)+"!10"
|
||||||
|
self.addText("\t\\node[{}]() at ({}, {}){{{}}};\n",
|
||||||
|
reg_dect, x, pos, emu.stack[index])
|
||||||
|
pos -= 0.5
|
||||||
|
return pos, x
|
||||||
|
|
||||||
def drawState (self, emu, line_nr=None):
|
def drawState (self, emu, line_nr=None):
|
||||||
x = (self.pos_x+1)*2.5-0.5
|
x = (self.pos_x+1)*2.5-0.5
|
||||||
self.pos_x += 1
|
self.pos_x += 1
|
||||||
|
@ -37,16 +65,10 @@ class TikzPainter:
|
||||||
reg_dect, x, pos, emu.getVal(reg_name))
|
reg_dect, x, pos, emu.getVal(reg_name))
|
||||||
|
|
||||||
# Draw stack
|
# Draw stack
|
||||||
pos -= 1.5
|
if self.stack_draw_mode == "up":
|
||||||
base_sp = max(emu.last_sp, emu.getVal('%rsp'))
|
pos, x = self.drawStackUpward(pos, emu, x)
|
||||||
if emu.last_sp - 1 == emu.getVal('%rsp'):
|
else:
|
||||||
pos += 0.5
|
pos, x = self.drawStackDownward(pos, emu, x)
|
||||||
for index in range(base_sp, max(-1, base_sp-self.max_stack_size),-1):
|
|
||||||
reg_state = emu.regState("m"+str(index))
|
|
||||||
reg_dect = "register_node, fill="+self.getRegColor(reg_state)+"!10"
|
|
||||||
self.addText("\t\\node[{}]() at ({}, {}){{{}}};\n",
|
|
||||||
reg_dect, x, pos, emu.stack[index])
|
|
||||||
pos -= 0.5
|
|
||||||
|
|
||||||
# Draw line signature
|
# Draw line signature
|
||||||
if line_nr == None:
|
if line_nr == None:
|
||||||
|
@ -54,22 +76,34 @@ class TikzPainter:
|
||||||
pos = 2
|
pos = 2
|
||||||
signature = emu.getLineSignature(line_nr)
|
signature = emu.getLineSignature(line_nr)
|
||||||
for token in signature:
|
for token in signature:
|
||||||
self.addText("\t\\node[text_node]() at ({}, {}){{{}}};\n", x-1.25, pos, token)
|
self.addText("\t\\node[text_node]() at ({}, {}){{{}}};\n",
|
||||||
|
x-1.25, pos, token)
|
||||||
pos -= 0.5
|
pos -= 0.5
|
||||||
|
|
||||||
def drawNames (self, emu):
|
def drawNames (self, emu):
|
||||||
x = 0
|
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:])
|
||||||
pos -= 0.5
|
pos -= 0.5
|
||||||
|
|
||||||
self.addText("\t\\node[text_node]() at ({}, {}){{TOS+1}};\n", x, pos-0.5)
|
# Draw stack
|
||||||
self.addText("\t\\node[text_node]() at ({}, {}){{TOS}};\n", x, pos-1.0)
|
if self.stack_draw_mode == "up":
|
||||||
|
self.drawStackNamesUpward(pos, emu, x)
|
||||||
|
else:
|
||||||
|
self.drawStackNamesDownward(pos, emu, x)
|
||||||
|
|
||||||
|
def drawStackNamesUpward (self, pos, emu, x):
|
||||||
|
self.addText("\t\\node[text_node]() at ({}, {}){{TOS+1}};\n",
|
||||||
|
x, pos-0.5)
|
||||||
|
self.addText("\t\\node[text_node]() at ({}, {}){{TOS}};\n",
|
||||||
|
x, pos-1.0)
|
||||||
pos -= 1
|
pos -= 1
|
||||||
for index in range(1,self.max_stack_size):
|
for index in range(1,self.max_stack_size):
|
||||||
pos -= 0.5
|
pos -= 0.5
|
||||||
self.addText("\t\\node[text_node]() at ({}, {}){{TOS-{}}};\n", x, pos, index)
|
self.addText("\t\\node[text_node]() at ({}, {}){{TOS-{}}};\n",
|
||||||
|
x, pos, index)
|
||||||
|
|
||||||
def __str__ (self):
|
def __str__ (self):
|
||||||
return "".join(self.text)
|
return "".join(self.text)
|
||||||
|
|
|
@ -49,8 +49,8 @@ return: leave # Clean up
|
||||||
|
|
||||||
|
|
||||||
OPTIONS = """register_node/.style={rectangle, draw=black!30,
|
OPTIONS = """register_node/.style={rectangle, draw=black!30,
|
||||||
fill=black!5, very thick, minimum size=0.5, minimum width=20mm},
|
fill=black!5, very thick, minimum size=0.5,
|
||||||
text_node/.style={}"""
|
minimum width=20mm}, text_node/.style={}"""
|
||||||
|
|
||||||
def printf (str, *args):
|
def printf (str, *args):
|
||||||
print(str.format(*args))
|
print(str.format(*args))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user