From 96881728062c861333fc278e89c8242e83820d7d Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Thu, 17 Dec 2015 17:48:37 +0100 Subject: [PATCH] Changed stack architecture to move downwards. TikzPainter have been adjusted for the change. Stack limits are now shown. --- Emulator.py | 15 ++++++----- TikzPainter.py | 66 +++++++++++++++++++++++++++++++++++------------ tests/test_fib.py | 4 +-- 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/Emulator.py b/Emulator.py index 223a70e..9d04762 100644 --- a/Emulator.py +++ b/Emulator.py @@ -8,11 +8,13 @@ REGISTERS=["%rax", "%rbx", "%rcx", "%rdx", "%rsp", "%rbp", "%rsi", "%rdi", class Emulator: - def __init__ (self, source_text): + def __init__ (self, source_text, max_stack_size=1000): self.registers = {} for reg_name in REGISTERS: self.registers[reg_name] = "Junk" self.stack = {} + for i in range(max_stack_size): + self.stack[i] = 0 self.code = [] self.labels = {} self.changes = {} @@ -28,12 +30,13 @@ class Emulator: Sets various stack elements, starting from 0 and going up. Automatically sets rsp. This can be disabled by passing set_rsp=False. """ - i = -1 + i = len(self.stack) for element in stack_list: - i += 1 + i -= 1 self.stack[i] = element if (not 'set_rsp' in kwargs) or kwargs['set_rsp']: self.setRegs(rsp=i) + self.last_sp = i-1 def setRegs (self, **reg_dict): 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] ] def pushToStack (self, new_element): - self.registers['%rsp'] = self.registers['%rsp'] + 1 + 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")) + self.changedRegisters(('%rsp',"change"),("m"+str(self.registers['%rsp']),"remove")) + self.registers['%rsp'] += 1 return temp def iterate (self): diff --git a/TikzPainter.py b/TikzPainter.py index 5db1655..686c263 100644 --- a/TikzPainter.py +++ b/TikzPainter.py @@ -9,11 +9,12 @@ REG_STATUS_TO_COLOR = { 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.text = [] self.pos_x = 0 self.max_stack_size = max_stack_size + self.stack_draw_mode = stack_draw_mode def addText (self, str, *args): self.text.append(str.format(*args)) @@ -21,6 +22,33 @@ class TikzPainter: def getRegColor (self, 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): x = (self.pos_x+1)*2.5-0.5 self.pos_x += 1 @@ -37,16 +65,10 @@ class TikzPainter: reg_dect, x, pos, emu.getVal(reg_name)) # Draw stack - pos -= 1.5 - base_sp = max(emu.last_sp, emu.getVal('%rsp')) - if emu.last_sp - 1 == emu.getVal('%rsp'): - pos += 0.5 - 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 + if self.stack_draw_mode == "up": + pos, x = self.drawStackUpward(pos, emu, x) + else: + pos, x = self.drawStackDownward(pos, emu, x) # Draw line signature if line_nr == None: @@ -54,22 +76,34 @@ class TikzPainter: pos = 2 signature = emu.getLineSignature(line_nr) 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 def drawNames (self, emu): x = 0 pos = 0 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 - 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) + # Draw stack + 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 for index in range(1,self.max_stack_size): 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): return "".join(self.text) diff --git a/tests/test_fib.py b/tests/test_fib.py index 41be569..a141f23 100644 --- a/tests/test_fib.py +++ b/tests/test_fib.py @@ -49,8 +49,8 @@ return: leave # Clean up OPTIONS = """register_node/.style={rectangle, draw=black!30, - fill=black!5, very thick, minimum size=0.5, minimum width=20mm}, - text_node/.style={}""" + fill=black!5, very thick, minimum size=0.5, + minimum width=20mm}, text_node/.style={}""" def printf (str, *args): print(str.format(*args))