1
0
infernal-interpreter/TikzPainter.py

76 lines
2.0 KiB
Python
Raw Normal View History

REG_STATUS_TO_COLOR = {
"insert": "green",
"change": "yellow",
"remove": "red",
"none": "black",
"jump": "orange"
}
class TikzPainter:
def __init__ (self, registers=None, max_stack_size=8):
self.registers = registers if registers else REGISTERS
self.text = []
self.pos_x = 0
self.max_stack_size = max_stack_size
def addText (self, str, *args):
self.text.append(str.format(*args))
def getRegColor (self, reg_state):
return REG_STATUS_TO_COLOR[reg_state]
def drawState (self, emu, line_nr=None):
x = (self.pos_x+1)*2.5-0.5
self.pos_x += 1
# Draw register cells
pos = 0.5
for reg_name in self.registers:
pos -= 0.5
if reg_name =="":
continue
reg_state = emu.regState(reg_name)
reg_dect = "register_node, fill="+self.getRegColor(reg_state)+"!10"
self.addText("\t\\node[{}]() at ({}, {}){{{}}};\n",
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
# Draw line signature
if line_nr == None:
return
pos = 2
signature = emu.getLineSignature(line_nr)
for token in signature:
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:])
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)
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)
def __str__ (self):
return "".join(self.text)