Switched from tabs to spaces.
This commit is contained in:
parent
34e8d6642f
commit
eddb64c9b8
184
AsciiPainter.py
184
AsciiPainter.py
|
@ -5,109 +5,109 @@ import subprocess
|
||||||
NORMAL_COLOR = '\033[0m'
|
NORMAL_COLOR = '\033[0m'
|
||||||
|
|
||||||
REG_STATUS_TO_COLOR = {
|
REG_STATUS_TO_COLOR = {
|
||||||
"insert": '\033[32m',
|
"insert": '\033[32m',
|
||||||
"change": '\033[33m',
|
"change": '\033[33m',
|
||||||
"remove": '\033[31m',
|
"remove": '\033[31m',
|
||||||
"none": '\033[0m',
|
"none": '\033[0m',
|
||||||
"jump": '\033[36m'
|
"jump": '\033[36m'
|
||||||
}
|
}
|
||||||
|
|
||||||
class AsciiPainter:
|
class AsciiPainter:
|
||||||
|
|
||||||
def __init__ (self, registers=None, max_stack_size=8, stack_draw_mode="up"):
|
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.states = []
|
self.states = []
|
||||||
self.max_stack_size = max_stack_size
|
self.max_stack_size = max_stack_size
|
||||||
self.stack_draw_mode = stack_draw_mode
|
self.stack_draw_mode = stack_draw_mode
|
||||||
|
|
||||||
def saveState (self, emu, line_nr=None):
|
def saveState (self, emu, line_nr=None):
|
||||||
# Init state
|
# Init state
|
||||||
state = []
|
state = []
|
||||||
self.states.append(state)
|
self.states.append(state)
|
||||||
# Write opcode to state
|
# Write opcode to state
|
||||||
if line_nr != None:
|
if line_nr != None:
|
||||||
signature = emu.getLineSignature(line_nr)
|
signature = emu.getLineSignature(line_nr)
|
||||||
for token in signature:
|
for token in signature:
|
||||||
state.append({ 'state': 'none', 'val': token })
|
state.append({ 'state': 'none', 'val': token })
|
||||||
#
|
#
|
||||||
for i in range(3-len(state)): state.append({'state':'none', 'val': ''})
|
for i in range(3-len(state)): state.append({'state':'none', 'val': ''})
|
||||||
for i in range(len(state)-3): state.pop()
|
for i in range(len(state)-3): state.pop()
|
||||||
|
state.append({'state':'none', 'val':''})
|
||||||
|
# Write registers to state
|
||||||
|
for reg_name in self.registers:
|
||||||
|
if reg_name =="":
|
||||||
state.append({'state':'none', 'val':''})
|
state.append({'state':'none', 'val':''})
|
||||||
# Write registers to state
|
continue
|
||||||
for reg_name in self.registers:
|
reg_state = emu.regState(reg_name)
|
||||||
if reg_name =="":
|
state.append({ 'state': reg_state, 'val': emu.getVal(reg_name) })
|
||||||
state.append({'state':'none', 'val':''})
|
# Write stack to state
|
||||||
continue
|
stack_base_pointer = emu.getVal('%rbp')
|
||||||
reg_state = emu.regState(reg_name)
|
base_sp = emu.getVal('%rsp')
|
||||||
state.append({ 'state': reg_state, 'val': emu.getVal(reg_name) })
|
assert(isinstance(emu.getVal('%rsp'), (int, long)))
|
||||||
# Write stack to state
|
state.append({'state':'none', 'val': ''})
|
||||||
stack_base_pointer = emu.getVal('%rbp')
|
if emu.regState('m'+str(base_sp-1)) == 'remove':
|
||||||
base_sp = emu.getVal('%rsp')
|
state.append({'state': emu.regState('m'+str(base_sp-1)), 'val': emu.stack[base_sp-1]})
|
||||||
assert(isinstance(emu.getVal('%rsp'), (int, long)))
|
else:
|
||||||
|
state.append({'state': 'none', 'val': ''})
|
||||||
|
for index in range(0, self.max_stack_size):
|
||||||
|
stack_i = base_sp + index
|
||||||
|
if stack_i < emu.max_stack_size:
|
||||||
|
state.append({ 'state': emu.regState("m"+str(stack_i)), 'val': emu.stack[stack_i] })
|
||||||
|
if stack_base_pointer == index:
|
||||||
|
stack_base_pointer = emu.stack[base_pointer]
|
||||||
|
# TODO: Draw stack frame seperators
|
||||||
|
else:
|
||||||
state.append({'state':'none', 'val': ''})
|
state.append({'state':'none', 'val': ''})
|
||||||
if emu.regState('m'+str(base_sp-1)) == 'remove':
|
|
||||||
state.append({'state': emu.regState('m'+str(base_sp-1)), 'val': emu.stack[base_sp-1]})
|
|
||||||
else:
|
|
||||||
state.append({'state': 'none', 'val': ''})
|
|
||||||
for index in range(0, self.max_stack_size):
|
|
||||||
stack_i = base_sp + index
|
|
||||||
if stack_i < emu.max_stack_size:
|
|
||||||
state.append({ 'state': emu.regState("m"+str(stack_i)), 'val': emu.stack[stack_i] })
|
|
||||||
if stack_base_pointer == index:
|
|
||||||
stack_base_pointer = emu.stack[base_pointer]
|
|
||||||
# TODO: Draw stack frame seperators
|
|
||||||
else:
|
|
||||||
state.append({'state':'none', 'val': ''})
|
|
||||||
|
|
||||||
def getWidthOfColumns(self, number_states, number_states_pr_block, number_state_vars):
|
def getWidthOfColumns(self, number_states, number_states_pr_block, number_state_vars):
|
||||||
widths = [0 for i in range(0, number_states_pr_block)]
|
widths = [0 for i in range(0, number_states_pr_block)]
|
||||||
for base_state in range(0, number_states, number_states_pr_block):
|
for base_state in range(0, number_states, number_states_pr_block):
|
||||||
for var_i in range(0, number_state_vars):
|
for var_i in range(0, number_state_vars):
|
||||||
for state_i in range(0, min(number_states_pr_block, number_states - base_state)):
|
for state_i in range(0, min(number_states_pr_block, number_states - base_state)):
|
||||||
width = len(str(self.states[base_state+state_i][var_i]['val']))
|
width = len(str(self.states[base_state+state_i][var_i]['val']))
|
||||||
widths[state_i] = max(widths[state_i], width)
|
widths[state_i] = max(widths[state_i], width)
|
||||||
return widths
|
return widths
|
||||||
|
|
||||||
def getWidthOfNameColumn (self, number_state_vars):
|
def getWidthOfNameColumn (self, number_state_vars):
|
||||||
widest = 0
|
widest = 0
|
||||||
|
for var_i in range(0, number_state_vars):
|
||||||
|
widest = max(widest, len(self.nameOfVar(var_i-4)))
|
||||||
|
return widest
|
||||||
|
|
||||||
|
def to_string (self, emu):
|
||||||
|
number_states = len(self.states)
|
||||||
|
number_state_vars = len(self.states[0])
|
||||||
|
|
||||||
|
term_width = int(subprocess.check_output(['tput', 'cols']))
|
||||||
|
number_states_pr_block = term_width / (10+2) - 1
|
||||||
|
separator = '-' * term_width
|
||||||
|
l = []
|
||||||
|
|
||||||
|
name_fmt = '{}{:'+str(self.getWidthOfNameColumn(number_state_vars))+'} '
|
||||||
|
column_fmt = map(lambda width: '{}{:>'+str(width)+'} ', self.getWidthOfColumns(number_states, number_states_pr_block, number_state_vars))
|
||||||
|
|
||||||
|
for base_state in range(0, number_states, number_states_pr_block):
|
||||||
|
l.append(separator)
|
||||||
for var_i in range(0, number_state_vars):
|
for var_i in range(0, number_state_vars):
|
||||||
widest = max(widest, len(self.nameOfVar(var_i-4)))
|
l.append(name_fmt.format(NORMAL_COLOR, self.nameOfVar(var_i-4)))
|
||||||
return widest
|
for state_i in range(0, min(number_states_pr_block, number_states - base_state)):
|
||||||
|
var = self.states[base_state+state_i][var_i]
|
||||||
|
l.append(column_fmt[state_i].format(REG_STATUS_TO_COLOR[var['state']], var['val'], NORMAL_COLOR))
|
||||||
|
l.append('\n')
|
||||||
|
l.append(separator)
|
||||||
|
|
||||||
def to_string (self, emu):
|
return ''.join(l)
|
||||||
number_states = len(self.states)
|
|
||||||
number_state_vars = len(self.states[0])
|
|
||||||
|
|
||||||
term_width = int(subprocess.check_output(['tput', 'cols']))
|
def nameOfVar (self, i):
|
||||||
number_states_pr_block = term_width / (10+2) - 1
|
if i < 0: return ''
|
||||||
separator = '-' * term_width
|
if i < len(self.registers):
|
||||||
l = []
|
return self.registers[i]
|
||||||
|
elif i != len(self.registers):
|
||||||
|
state_i = i-len(self.registers) - 2
|
||||||
|
return 'TOS{}{}'.format('+' if state_i < 0 else '-', abs(state_i))
|
||||||
|
else:
|
||||||
|
return ''
|
||||||
|
|
||||||
name_fmt = '{}{:'+str(self.getWidthOfNameColumn(number_state_vars))+'} '
|
def __str__ (self):
|
||||||
column_fmt = map(lambda width: '{}{:>'+str(width)+'} ', self.getWidthOfColumns(number_states, number_states_pr_block, number_state_vars))
|
return 'AsciiPainter[]'
|
||||||
|
|
||||||
for base_state in range(0, number_states, number_states_pr_block):
|
|
||||||
l.append(separator)
|
|
||||||
for var_i in range(0, number_state_vars):
|
|
||||||
l.append(name_fmt.format(NORMAL_COLOR, self.nameOfVar(var_i-4)))
|
|
||||||
for state_i in range(0, min(number_states_pr_block, number_states - base_state)):
|
|
||||||
var = self.states[base_state+state_i][var_i]
|
|
||||||
l.append(column_fmt[state_i].format(REG_STATUS_TO_COLOR[var['state']], var['val'], NORMAL_COLOR))
|
|
||||||
l.append('\n')
|
|
||||||
l.append(separator)
|
|
||||||
|
|
||||||
return ''.join(l)
|
|
||||||
|
|
||||||
def nameOfVar (self, i):
|
|
||||||
if i < 0: return ''
|
|
||||||
if i < len(self.registers):
|
|
||||||
return self.registers[i]
|
|
||||||
elif i != len(self.registers):
|
|
||||||
state_i = i-len(self.registers) - 2
|
|
||||||
return 'TOS{}{}'.format('+' if state_i < 0 else '-', abs(state_i))
|
|
||||||
else:
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def __str__ (self):
|
|
||||||
return 'TikzPainter[]'
|
|
||||||
|
|
||||||
|
|
252
Emulator.py
252
Emulator.py
|
@ -9,153 +9,153 @@ REGISTERS=["%rax", "%rbx", "%rcx", "%rdx", "%rsp", "%rbp", "%rsi", "%rdi",
|
||||||
|
|
||||||
class CodeParseException (BaseException):
|
class CodeParseException (BaseException):
|
||||||
|
|
||||||
def __init__ (self, line_nr, string):
|
def __init__ (self, line_nr, string):
|
||||||
self.line_nr = line_nr
|
self.line_nr = line_nr
|
||||||
self.string = string
|
self.string = string
|
||||||
|
|
||||||
class InternalExecutionException (BaseException):
|
class InternalExecutionException (BaseException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Emulator:
|
class Emulator:
|
||||||
|
|
||||||
def __init__ (self, source_text, max_stack_size=1000):
|
def __init__ (self, source_text, max_stack_size=1000):
|
||||||
self.source = source_text
|
self.source = source_text
|
||||||
self.registers = {}
|
self.registers = {}
|
||||||
for reg_name in REGISTERS:
|
for reg_name in REGISTERS:
|
||||||
self.registers[reg_name] = Junk.Junk()
|
self.registers[reg_name] = Junk.Junk()
|
||||||
self.stack = {}
|
self.stack = {}
|
||||||
for i in range(max_stack_size):
|
for i in range(max_stack_size):
|
||||||
self.stack[i] = Junk.Junk()
|
self.stack[i] = Junk.Junk()
|
||||||
self.code = []
|
self.code = []
|
||||||
self.labels = {}
|
self.labels = {}
|
||||||
self.changes = {}
|
self.changes = {}
|
||||||
self.status = {'i':True}
|
self.status = {'i':True}
|
||||||
self.last_sp = 0
|
self.last_sp = 0
|
||||||
self.max_stack_size = 0
|
self.max_stack_size = 0
|
||||||
index = 0
|
index = 0
|
||||||
for line in iter(source_text.splitlines()):
|
for line in iter(source_text.splitlines()):
|
||||||
index = self.processSourceLine(line, index)
|
index = self.processSourceLine(line, index)
|
||||||
|
|
||||||
def setStack (self, *stack_list, **kwargs):
|
def setStack (self, *stack_list, **kwargs):
|
||||||
"""
|
"""
|
||||||
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 = len(self.stack)
|
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
|
||||||
self.setRegs(rsp=i)
|
self.setRegs(rsp=i)
|
||||||
self.last_sp = i-1
|
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():
|
||||||
assert(reg_name[0] != '%')
|
assert(reg_name[0] != '%')
|
||||||
self.registers["%"+reg_name] = reg_val
|
self.registers["%"+reg_name] = reg_val
|
||||||
|
|
||||||
def getVal (self, val_text):
|
def getVal (self, val_text):
|
||||||
if val_text[0] == "$":
|
if val_text[0] == "$":
|
||||||
return int(val_text[1:])
|
return int(val_text[1:])
|
||||||
elif val_text[0] == "%":
|
elif val_text[0] == "%":
|
||||||
return self.registers[val_text]
|
return self.registers[val_text]
|
||||||
else:
|
else:
|
||||||
raise ValueError('{} is not an usable value name'.format(val_text))
|
raise ValueError('{} is not an usable value name'.format(val_text))
|
||||||
|
|
||||||
def compareVal (self, valT1, valT2):
|
def compareVal (self, valT1, valT2):
|
||||||
val1 = self.getVal(valT2)
|
val1 = self.getVal(valT2)
|
||||||
val2 = self.getVal(valT1)
|
val2 = self.getVal(valT1)
|
||||||
if isinstance(val1,Junk.Junk) or isinstance(val2,Junk.Junk):
|
if isinstance(val1,Junk.Junk) or isinstance(val2,Junk.Junk):
|
||||||
self.status["i"] = (True,)
|
self.status["i"] = (True,)
|
||||||
return
|
return
|
||||||
self.status["g"] = val1>val2
|
self.status["g"] = val1>val2
|
||||||
self.status["l"] = val1<val2
|
self.status["l"] = val1<val2
|
||||||
self.status["e"] = val1==val2
|
self.status["e"] = val1==val2
|
||||||
self.status["i"] = False
|
self.status["i"] = False
|
||||||
|
|
||||||
def changedRegisters (self, *args):
|
def changedRegisters (self, *args):
|
||||||
for reg, val in args:
|
for reg, val in args:
|
||||||
self.changes[reg] = val
|
self.changes[reg] = val
|
||||||
|
|
||||||
def processSourceLine(self, line_text, index):
|
def processSourceLine(self, line_text, index):
|
||||||
tokens = re.findall(r'[^\s,]+', line_text)
|
tokens = re.findall(r'[^\s,]+', line_text)
|
||||||
if len(tokens) <= 0 or tokens[0][0] == '#':
|
if len(tokens) <= 0 or tokens[0][0] == '#':
|
||||||
return index
|
return index
|
||||||
if tokens[0][-1] == ':':
|
if tokens[0][-1] == ':':
|
||||||
self.labels[tokens[0][:-1]] = index
|
self.labels[tokens[0][:-1]] = index
|
||||||
tokens = tokens[1:]
|
tokens = tokens[1:]
|
||||||
for i in range(len(tokens)):
|
for i in range(len(tokens)):
|
||||||
if tokens[i][0] == "#":
|
if tokens[i][0] == "#":
|
||||||
tokens = tokens[:i]
|
tokens = tokens[:i]
|
||||||
break
|
break
|
||||||
self.code.append(tokens)
|
self.code.append(tokens)
|
||||||
return index + 1
|
return index + 1
|
||||||
|
|
||||||
def regState (self, reg_name):
|
def regState (self, reg_name):
|
||||||
if reg_name in self.changes:
|
if reg_name in self.changes:
|
||||||
return self.changes[reg_name]
|
return self.changes[reg_name]
|
||||||
else:
|
else:
|
||||||
return "none"
|
return "none"
|
||||||
|
|
||||||
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):
|
def pushToStack (self, new_element):
|
||||||
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.changedRegisters(('%rsp',"change"),("m"+str(self.registers['%rsp']),"remove"))
|
self.changedRegisters(('%rsp',"change"),("m"+str(self.registers['%rsp']),"remove"))
|
||||||
self.registers['%rsp'] += 1
|
self.registers['%rsp'] += 1
|
||||||
return temp
|
return temp
|
||||||
|
|
||||||
def jump (self, label, cond_op="or", **conditions):
|
def jump (self, label, cond_op="or", **conditions):
|
||||||
if len(conditions)>0 and self.status["i"]:
|
if len(conditions)>0 and self.status["i"]:
|
||||||
raise Junk.JunkComparisonException(*self.status['i'])
|
raise Junk.JunkComparisonException(*self.status['i'])
|
||||||
and_, or_ = True, False
|
and_, or_ = True, False
|
||||||
for cnd_name, cnd_val in conditions.iteritems():
|
for cnd_name, cnd_val in conditions.iteritems():
|
||||||
if self.status[cnd_name] == cnd_val:
|
if self.status[cnd_name] == cnd_val:
|
||||||
or_ = True
|
or_ = True
|
||||||
else:
|
else:
|
||||||
and_ = False
|
and_ = False
|
||||||
if or_ if cond_op=="or" else and_:
|
if or_ if cond_op=="or" else and_:
|
||||||
self.registers['%rip'] = self.labels[label]
|
self.registers['%rip'] = self.labels[label]
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
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']
|
||||||
if self.registers['%rip'] >= len(self.code) or isinstance(self.registers['%rip'], Junk.Junk):
|
if self.registers['%rip'] >= len(self.code) or isinstance(self.registers['%rip'], Junk.Junk):
|
||||||
return None
|
return None
|
||||||
if not isinstance(self.registers['%rip'], (int, long)):
|
if not isinstance(self.registers['%rip'], (int, long)):
|
||||||
raise InternalExecutionException("Register %rip should be integer, but was "+str(self.registers['%rip']))
|
raise InternalExecutionException("Register %rip should be integer, but was "+str(self.registers['%rip']))
|
||||||
instruct = self.code[self.registers['%rip']]
|
instruct = self.code[self.registers['%rip']]
|
||||||
opcode = instruct[0]
|
opcode = instruct[0]
|
||||||
self.changes = {}
|
self.changes = {}
|
||||||
|
|
||||||
OPCODES[opcode](self, *instruct[1:])
|
OPCODES[opcode](self, *instruct[1:])
|
||||||
|
|
||||||
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',"jump"))
|
self.changedRegisters(('%rip',"jump"))
|
||||||
|
|
||||||
return old_rip
|
return old_rip
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
output = self.iterate()
|
output = self.iterate()
|
||||||
if output is None:
|
if output is None:
|
||||||
raise StopIteration()
|
raise StopIteration()
|
||||||
else:
|
else:
|
||||||
return output
|
return output
|
||||||
|
|
||||||
def getUsedRegisters (self):
|
def getUsedRegisters (self):
|
||||||
return [reg_name for reg_name, reg_val in self.registers.iteritems() if not isinstance(reg_val, Junk.Junk)]
|
return [reg_name for reg_name, reg_val in self.registers.iteritems() if not isinstance(reg_val, Junk.Junk)]
|
||||||
|
|
||||||
|
|
34
Junk.py
34
Junk.py
|
@ -6,27 +6,27 @@ class JunkComparisonException (BaseException):
|
||||||
|
|
||||||
class Junk:
|
class Junk:
|
||||||
|
|
||||||
def __init__ (self, represents = None):
|
def __init__ (self, represents = None):
|
||||||
assert(represents == None or isinstance(represents, basestring))
|
assert(represents == None or isinstance(represents, basestring))
|
||||||
self.repr = represents
|
self.repr = represents
|
||||||
|
|
||||||
def __str__ (self):
|
def __str__ (self):
|
||||||
if self.repr:
|
if self.repr:
|
||||||
return '['+self.repr+']'
|
return '['+self.repr+']'
|
||||||
return "[junk]"
|
return "[junk]"
|
||||||
|
|
||||||
def __repr__ (self):
|
def __repr__ (self):
|
||||||
return self.__str__()
|
return self.__str__()
|
||||||
|
|
||||||
def __add__ (self, other):
|
def __add__ (self, other):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __radd__ (self, other):
|
def __radd__ (self, other):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __sub__ (self, other):
|
def __sub__ (self, other):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __rsub__ (self, other):
|
def __rsub__ (self, other):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
208
TikzPainter.py
208
TikzPainter.py
|
@ -2,128 +2,128 @@
|
||||||
import re
|
import re
|
||||||
|
|
||||||
REG_STATUS_TO_COLOR = {
|
REG_STATUS_TO_COLOR = {
|
||||||
"insert": "green",
|
"insert": "green",
|
||||||
"change": "yellow",
|
"change": "yellow",
|
||||||
"remove": "red",
|
"remove": "red",
|
||||||
"none": "black",
|
"none": "black",
|
||||||
"jump": "orange"
|
"jump": "orange"
|
||||||
}
|
}
|
||||||
|
|
||||||
DEFAULT_OPTIONS = """register_node/.style={rectangle, draw=black!30,
|
DEFAULT_OPTIONS = """register_node/.style={rectangle, draw=black!30,
|
||||||
fill=black!5, very thick, minimum size=0.5,
|
fill=black!5, very thick, minimum size=0.5,
|
||||||
minimum width=20mm}, text_node/.style={}"""
|
minimum width=20mm}, text_node/.style={}"""
|
||||||
|
|
||||||
class TikzPainter:
|
class TikzPainter:
|
||||||
|
|
||||||
def __init__ (self, registers=None, max_stack_size=8, stack_draw_mode="up", options=None):
|
def __init__ (self, registers=None, max_stack_size=8, stack_draw_mode="up", options=None):
|
||||||
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
|
self.stack_draw_mode = stack_draw_mode
|
||||||
self.options = options if options else DEFAULT_OPTIONS
|
self.options = options if options else DEFAULT_OPTIONS
|
||||||
|
|
||||||
def addText (self, str, *args):
|
def addText (self, str, *args):
|
||||||
text = re.sub(r"[^\\]%", "\\%", str.format(*args))
|
text = re.sub(r"[^\\]%", "\\%", str.format(*args))
|
||||||
self.text.append(text)
|
self.text.append(text)
|
||||||
|
|
||||||
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):
|
def drawStackUpward (self, pos, emu, x):
|
||||||
base_pointer = emu.getVal('%rbp')
|
base_pointer = emu.getVal('%rbp')
|
||||||
|
|
||||||
pos -= 1.5
|
pos -= 1.5
|
||||||
base_sp = min(emu.last_sp, emu.getVal('%rsp'))
|
base_sp = min(emu.last_sp, emu.getVal('%rsp'))
|
||||||
pos += 0.5 * max(0, emu.getVal('%rsp')-emu.last_sp)
|
pos += 0.5 * max(0, emu.getVal('%rsp')-emu.last_sp)
|
||||||
assert(isinstance(emu.getVal('%rsp'), (int, long)))
|
assert(isinstance(emu.getVal('%rsp'), (int, long)))
|
||||||
for index in range(base_sp, 3+min(emu.max_stack_size, emu.getVal('%rsp')+self.max_stack_size)):
|
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_state = emu.regState("m"+str(index))
|
||||||
reg_dect = "register_node, fill="+self.getRegColor(reg_state)+"!10"
|
reg_dect = "register_node, fill="+self.getRegColor(reg_state)+"!10"
|
||||||
self.addText("\t\\node[{}]() at ({}, {}){{{}}};\n",
|
self.addText("\t\\node[{}]() at ({}, {}){{{}}};\n",
|
||||||
reg_dect, x, pos, emu.stack[index])
|
reg_dect, x, pos, emu.stack[index])
|
||||||
if base_pointer == index:
|
if base_pointer == index:
|
||||||
self.addText("\t\\draw ({0},{2}) -- ({1},{2});\n",x-1.1,x+1.1,pos-0.25)
|
self.addText("\t\\draw ({0},{2}) -- ({1},{2});\n",x-1.1,x+1.1,pos-0.25)
|
||||||
base_pointer = emu.stack[base_pointer]
|
base_pointer = emu.stack[base_pointer]
|
||||||
pos -= 0.5
|
pos -= 0.5
|
||||||
return pos, x
|
return pos, x
|
||||||
|
|
||||||
def drawStackDownward (self, pos, emu, x):
|
def drawStackDownward (self, pos, emu, x):
|
||||||
pos -= 1
|
pos -= 1
|
||||||
for index in range(emu.getVal('%rsp')-8, max(emu.last_sp, emu.getVal('%rsp'))):
|
for index in range(emu.getVal('%rsp')-8, max(emu.last_sp, emu.getVal('%rsp'))):
|
||||||
reg_state = emu.regState("m"+str(index))
|
reg_state = emu.regState("m"+str(index))
|
||||||
reg_dect = "register_node, fill="+self.getRegColor(reg_state)+"!10"
|
reg_dect = "register_node, fill="+self.getRegColor(reg_state)+"!10"
|
||||||
self.addText("\t\\node[{}]() at ({}, {}){{{}}};\n",
|
self.addText("\t\\node[{}]() at ({}, {}){{{}}};\n",
|
||||||
reg_dect, x, pos, emu.stack[index])
|
reg_dect, x, pos, emu.stack[index])
|
||||||
pos -= 0.5
|
pos -= 0.5
|
||||||
return pos, x
|
return pos, x
|
||||||
|
|
||||||
def saveState (self, emu, line_nr=None):
|
def saveState (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
|
||||||
|
|
||||||
# Draw register cells
|
# Draw register cells
|
||||||
pos = 0.5
|
pos = 0.5
|
||||||
for reg_name in self.registers:
|
for reg_name in self.registers:
|
||||||
pos -= 0.5
|
pos -= 0.5
|
||||||
if reg_name =="":
|
if reg_name =="":
|
||||||
continue
|
continue
|
||||||
reg_state = emu.regState(reg_name)
|
reg_state = emu.regState(reg_name)
|
||||||
reg_dect = "register_node, fill="+self.getRegColor(reg_state)+"!10"
|
reg_dect = "register_node, fill="+self.getRegColor(reg_state)+"!10"
|
||||||
self.addText("\t\\node[{}]() at ({}, {}){{{}}};\n",
|
self.addText("\t\\node[{}]() at ({}, {}){{{}}};\n",
|
||||||
reg_dect, x, pos, emu.getVal(reg_name))
|
reg_dect, x, pos, emu.getVal(reg_name))
|
||||||
|
|
||||||
# Draw stack
|
# Draw stack
|
||||||
if self.stack_draw_mode == "up":
|
if self.stack_draw_mode == "up":
|
||||||
pos, x = self.drawStackUpward(pos, emu, x)
|
pos, x = self.drawStackUpward(pos, emu, x)
|
||||||
else:
|
else:
|
||||||
pos, x = self.drawStackDownward(pos, emu, x)
|
pos, x = self.drawStackDownward(pos, emu, x)
|
||||||
|
|
||||||
# Draw line signature
|
# Draw line signature
|
||||||
if line_nr == None:
|
if line_nr == None:
|
||||||
return
|
return
|
||||||
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",
|
self.addText("\t\\node[text_node]() at ({}, {}){{{}}};\n",
|
||||||
x-1.25, pos, token)
|
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",
|
self.addText("\t\\node[text_node]() at ({}, {}){{{}}};\n",
|
||||||
x, pos, reg_name[1:])
|
x, pos, reg_name[1:])
|
||||||
pos -= 0.5
|
pos -= 0.5
|
||||||
|
|
||||||
# Draw stack
|
# Draw stack
|
||||||
if self.stack_draw_mode == "up":
|
if self.stack_draw_mode == "up":
|
||||||
self.drawStackNamesUpward(pos, emu, x)
|
self.drawStackNamesUpward(pos, emu, x)
|
||||||
else:
|
else:
|
||||||
self.drawStackNamesDownward(pos, emu, x)
|
self.drawStackNamesDownward(pos, emu, x)
|
||||||
|
|
||||||
def drawStackNamesUpward (self, pos, emu, x):
|
def drawStackNamesUpward (self, pos, emu, x):
|
||||||
self.addText("\t\\node[text_node]() at ({}, {}){{TOS+1}};\n",
|
self.addText("\t\\node[text_node]() at ({}, {}){{TOS+1}};\n",
|
||||||
x, pos-0.5)
|
x, pos-0.5)
|
||||||
self.addText("\t\\node[text_node]() at ({}, {}){{TOS}};\n",
|
self.addText("\t\\node[text_node]() at ({}, {}){{TOS}};\n",
|
||||||
x, pos-1.0)
|
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",
|
self.addText("\t\\node[text_node]() at ({}, {}){{TOS-{}}};\n",
|
||||||
x, pos, index)
|
x, pos, index)
|
||||||
|
|
||||||
def to_string (self, emu):
|
def to_string (self, emu):
|
||||||
PRE = """ \\documentclass{standalone}
|
PRE = """ \\documentclass{standalone}
|
||||||
\\usepackage{tikz}
|
\\usepackage{tikz}
|
||||||
\\begin{document}
|
\\begin{document}
|
||||||
\\begin{tikzpicture}["""+self.options+"]"
|
\\begin{tikzpicture}["""+self.options+"]"
|
||||||
POST = """\\end{tikzpicture}
|
POST = """\\end{tikzpicture}
|
||||||
\\end{document}"""
|
\\end{document}"""
|
||||||
self.drawNames(emu)
|
self.drawNames(emu)
|
||||||
return PRE + "".join(self.text) + POST
|
return PRE + "".join(self.text) + POST
|
||||||
|
|
||||||
def __str__ (self):
|
def __str__ (self):
|
||||||
return 'TikzPainter[]'
|
return 'TikzPainter[]'
|
||||||
|
|
||||||
|
|
109
main.py
109
main.py
|
@ -29,75 +29,74 @@ PAINTER_ID_TO_CLASS = {
|
||||||
}
|
}
|
||||||
|
|
||||||
def parse_args ():
|
def parse_args ():
|
||||||
parser = argparse.ArgumentParser(description = ARG_DESC, epilog = LICENSE)
|
parser = argparse.ArgumentParser(description = ARG_DESC, epilog = LICENSE)
|
||||||
parser.add_argument('-i', '--input-file', default='-', help = 'File to use as input', dest = 'input-file')
|
parser.add_argument('-i', '--input-file', default='-', help = 'File to use as input', dest = 'input-file')
|
||||||
parser.add_argument('-o', '--output-file', default='-', help = 'File to use as output', dest = 'output-file')
|
parser.add_argument('-o', '--output-file', default='-', help = 'File to use as output', dest = 'output-file')
|
||||||
parser.add_argument('-p', '--painter', default='ascii', help = 'Drawer to use', dest = 'painter-name', choices=PAINTER_ID_TO_CLASS.keys())
|
parser.add_argument('-p', '--painter', default='ascii', help = 'Drawer to use', dest = 'painter-name', choices=PAINTER_ID_TO_CLASS.keys())
|
||||||
for register in REGISTERS:
|
for register in REGISTERS:
|
||||||
parser.add_argument('--'+register[1:], nargs = 1, type = int, default = None, dest = register, help = 'the initial value of the register')
|
parser.add_argument('--'+register[1:], nargs = 1, type = int, default = None, dest = register, help = 'the initial value of the register')
|
||||||
args = vars(parser.parse_args())
|
args = vars(parser.parse_args())
|
||||||
|
|
||||||
# Determine args #
|
# Determine args #
|
||||||
registers_init = {}
|
registers_init = {}
|
||||||
for register in REGISTERS:
|
for register in REGISTERS:
|
||||||
registers_init[register[1:]] = Junk.Junk('old '+register[1:])
|
registers_init[register[1:]] = Junk.Junk('old '+register[1:])
|
||||||
#
|
#
|
||||||
registers_init['rip'] = 0
|
registers_init['rip'] = 0
|
||||||
registers_init['rsp'] = 0
|
registers_init['rsp'] = 0
|
||||||
registers_init['rbp'] = Junk.Junk('old bp')
|
registers_init['rbp'] = Junk.Junk('old bp')
|
||||||
#
|
#
|
||||||
for register in REGISTERS:
|
for register in REGISTERS:
|
||||||
if args[register]:
|
if args[register]:
|
||||||
registers_init[register[1:]] = args[register][0]
|
registers_init[register[1:]] = args[register][0]
|
||||||
|
|
||||||
#
|
#
|
||||||
program = ""
|
program = ""
|
||||||
if args['input-file'] == '-':
|
if args['input-file'] == '-':
|
||||||
program = sys.stdin.read()
|
program = sys.stdin.read()
|
||||||
else:
|
else:
|
||||||
with open(args['input-file']) as file:
|
with open(args['input-file']) as file:
|
||||||
program = file.read()
|
program = file.read()
|
||||||
|
|
||||||
# Output file:
|
# Output file:
|
||||||
output_file = None
|
output_file = None
|
||||||
if args['output-file'] == '-':
|
if args['output-file'] == '-':
|
||||||
output_file = sys.stdout
|
output_file = sys.stdout
|
||||||
else:
|
else:
|
||||||
output_file = open(args['output-file'], 'w')
|
output_file = open(args['output-file'], 'w')
|
||||||
|
|
||||||
|
# Determine painter class
|
||||||
|
painter_class = PAINTER_ID_TO_CLASS[args['painter-name']]
|
||||||
|
|
||||||
# Determine painter class
|
## Return
|
||||||
painter_class = PAINTER_ID_TO_CLASS[args['painter-name']]
|
return (program, registers_init, output_file, painter_class)
|
||||||
|
|
||||||
## Return
|
|
||||||
return (program, registers_init, output_file, painter_class)
|
|
||||||
|
|
||||||
def main ():
|
def main ():
|
||||||
|
|
||||||
(program, registers_init, output_file, painter_class) = parse_args()
|
(program, registers_init, output_file, painter_class) = parse_args()
|
||||||
|
|
||||||
# Determine registers to display
|
# Determine registers to display
|
||||||
registers_to_draw = ["%rip","%rbp","%rsp", ""]
|
registers_to_draw = ["%rip","%rbp","%rsp", ""]
|
||||||
for match in re.findall(r"%r[a-z0-9]{1,2}", program):
|
for match in re.findall(r"%r[a-z0-9]{1,2}", program):
|
||||||
if match not in registers_to_draw:
|
if match not in registers_to_draw:
|
||||||
registers_to_draw.append(match)
|
registers_to_draw.append(match)
|
||||||
|
|
||||||
# Setup emulator and drawer
|
# Setup emulator and drawer
|
||||||
emu = Emulator(program)
|
emu = Emulator(program)
|
||||||
emu.setRegs(**registers_init)
|
emu.setRegs(**registers_init)
|
||||||
emu.setStack(Junk.Junk("junk..."), Junk.Junk("call eip"))
|
emu.setStack(Junk.Junk("junk..."), Junk.Junk("call eip"))
|
||||||
|
|
||||||
painter = painter_class(registers_to_draw)
|
painter = painter_class(registers_to_draw)
|
||||||
|
|
||||||
# Iteratively draw states.
|
# Iteratively draw states.
|
||||||
painter.saveState(emu)
|
painter.saveState(emu)
|
||||||
for line_nr in emu:
|
for line_nr in emu:
|
||||||
painter.saveState(emu, line_nr)
|
painter.saveState(emu, line_nr)
|
||||||
|
|
||||||
# Display result
|
# Display result
|
||||||
output_file.write(painter.to_string(emu))
|
output_file.write(painter.to_string(emu))
|
||||||
output_file.close()
|
output_file.close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
52
opcodes.py
52
opcodes.py
|
@ -2,10 +2,10 @@
|
||||||
OPCODES = {}
|
OPCODES = {}
|
||||||
|
|
||||||
def add_opcode (*opcode_names):
|
def add_opcode (*opcode_names):
|
||||||
def wrapped (func):
|
def wrapped (func):
|
||||||
for name in opcode_names:
|
for name in opcode_names:
|
||||||
OPCODES[name] = func
|
OPCODES[name] = func
|
||||||
return wrapped
|
return wrapped
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
|
@ -13,76 +13,76 @@ def add_opcode (*opcode_names):
|
||||||
|
|
||||||
@add_opcode("push","pushq")
|
@add_opcode("push","pushq")
|
||||||
def push(emu, value):
|
def push(emu, value):
|
||||||
emu.pushToStack(emu.getVal(value))
|
emu.pushToStack(emu.getVal(value))
|
||||||
|
|
||||||
@add_opcode("pop","popq")
|
@add_opcode("pop","popq")
|
||||||
def pop(emu, target_reg):
|
def pop(emu, target_reg):
|
||||||
emu.registers[target_reg] = emu.popFromStack()
|
emu.registers[target_reg] = emu.popFromStack()
|
||||||
emu.changedRegisters((target_reg,"change"))
|
emu.changedRegisters((target_reg,"change"))
|
||||||
|
|
||||||
# Arithmetic
|
# Arithmetic
|
||||||
|
|
||||||
@add_opcode("mov","movq")
|
@add_opcode("mov","movq")
|
||||||
def mov(emu, from_reg, target_reg):
|
def mov(emu, from_reg, target_reg):
|
||||||
emu.registers[target_reg] = emu.getVal(from_reg)
|
emu.registers[target_reg] = emu.getVal(from_reg)
|
||||||
emu.changedRegisters((target_reg,"change"))
|
emu.changedRegisters((target_reg,"change"))
|
||||||
|
|
||||||
@add_opcode("add","addq")
|
@add_opcode("add","addq")
|
||||||
def add(emu, add_val, target_reg):
|
def add(emu, add_val, target_reg):
|
||||||
emu.registers[target_reg] += emu.getVal(add_val)
|
emu.registers[target_reg] += emu.getVal(add_val)
|
||||||
emu.changedRegisters((target_reg,"change"))
|
emu.changedRegisters((target_reg,"change"))
|
||||||
|
|
||||||
@add_opcode("sub","subq")
|
@add_opcode("sub","subq")
|
||||||
def sub(emu, sub_val, target_reg):
|
def sub(emu, sub_val, target_reg):
|
||||||
emu.registers[target_reg] -= emu.getVal(sub_val)
|
emu.registers[target_reg] -= emu.getVal(sub_val)
|
||||||
emu.changedRegisters((target_reg,"change"))
|
emu.changedRegisters((target_reg,"change"))
|
||||||
|
|
||||||
# Comparison and conditional jumping
|
# Comparison and conditional jumping
|
||||||
|
|
||||||
@add_opcode("cmp","cmpq")
|
@add_opcode("cmp","cmpq")
|
||||||
def comp(emu, val1, val2):
|
def comp(emu, val1, val2):
|
||||||
emu.compareVal(val1, val2)
|
emu.compareVal(val1, val2)
|
||||||
|
|
||||||
@add_opcode("jg")
|
@add_opcode("jg")
|
||||||
def jg(emu, label):
|
def jg(emu, label):
|
||||||
emu.jump(label, g=True)
|
emu.jump(label, g=True)
|
||||||
|
|
||||||
@add_opcode("jl")
|
@add_opcode("jl")
|
||||||
def jl(emu, label):
|
def jl(emu, label):
|
||||||
emu.jump(label, l=True)
|
emu.jump(label, l=True)
|
||||||
|
|
||||||
@add_opcode("je")
|
@add_opcode("je")
|
||||||
def je(emu, label):
|
def je(emu, label):
|
||||||
emu.jump(label, e=True)
|
emu.jump(label, e=True)
|
||||||
|
|
||||||
@add_opcode("jge")
|
@add_opcode("jge")
|
||||||
def jge(emu, label):
|
def jge(emu, label):
|
||||||
emu.jump(label, "or", g=True, e=True)
|
emu.jump(label, "or", g=True, e=True)
|
||||||
|
|
||||||
@add_opcode("jle")
|
@add_opcode("jle")
|
||||||
def jle(emu, label):
|
def jle(emu, label):
|
||||||
emu.jump(label, "or", l=True, e=True)
|
emu.jump(label, "or", l=True, e=True)
|
||||||
|
|
||||||
@add_opcode("jne")
|
@add_opcode("jne")
|
||||||
def jne(emu, label):
|
def jne(emu, label):
|
||||||
emu.jump(label, e=False)
|
emu.jump(label, e=False)
|
||||||
|
|
||||||
# Unconditional Jumping
|
# Unconditional Jumping
|
||||||
|
|
||||||
@add_opcode("jmp")
|
@add_opcode("jmp")
|
||||||
def jmp(emu, label):
|
def jmp(emu, label):
|
||||||
emu.registers['%rip'] = emu.labels[label]
|
emu.registers['%rip'] = emu.labels[label]
|
||||||
|
|
||||||
@add_opcode("call")
|
@add_opcode("call")
|
||||||
def call(emu, label):
|
def call(emu, label):
|
||||||
emu.pushToStack(emu.registers['%rip']+1)
|
emu.pushToStack(emu.registers['%rip']+1)
|
||||||
emu.registers['%rip'] = emu.labels[label]
|
emu.registers['%rip'] = emu.labels[label]
|
||||||
|
|
||||||
@add_opcode("leave")
|
@add_opcode("leave")
|
||||||
def leave(emu):
|
def leave(emu):
|
||||||
emu.registers["%rsp"] = emu.registers["%rbp"]
|
emu.registers["%rsp"] = emu.registers["%rbp"]
|
||||||
emu.registers["%rbp"] = emu.popFromStack()
|
emu.registers["%rbp"] = emu.popFromStack()
|
||||||
|
|
||||||
@add_opcode("ret")
|
@add_opcode("ret")
|
||||||
def ret(emu):
|
def ret(emu):
|
||||||
emu.registers['%rip'] = emu.popFromStack()
|
emu.registers['%rip'] = emu.popFromStack()
|
||||||
|
|
166
tests/tests.py
166
tests/tests.py
|
@ -9,49 +9,49 @@ import infernal
|
||||||
tests = []
|
tests = []
|
||||||
|
|
||||||
def add_test(name, result, register, code):
|
def add_test(name, result, register, code):
|
||||||
tests.append((name, result, register, code))
|
tests.append((name, result, register, code))
|
||||||
|
|
||||||
def printf (str, *args):
|
def printf (str, *args):
|
||||||
print(str.format(*args))
|
print(str.format(*args))
|
||||||
|
|
||||||
def execute_tests():
|
def execute_tests():
|
||||||
print("Executing tests!")
|
print("Executing tests!")
|
||||||
total_tests = 0
|
total_tests = 0
|
||||||
failed_tests = 0
|
failed_tests = 0
|
||||||
error_tests = 0
|
error_tests = 0
|
||||||
for name, result, register, code in tests:
|
for name, result, register, code in tests:
|
||||||
total_tests += 1
|
total_tests += 1
|
||||||
line_nr = None
|
line_nr = None
|
||||||
try:
|
try:
|
||||||
emu = infernal.Emulator(code)
|
emu = infernal.Emulator(code)
|
||||||
emu.setStack("junk...", "calling eip")
|
emu.setStack("junk...", "calling eip")
|
||||||
emu.setRegs( rip = 0, rbp = 'old bp')
|
emu.setRegs( rip = 0, rbp = 'old bp')
|
||||||
except infernal.CodeParseException as e:
|
except infernal.CodeParseException as e:
|
||||||
error_tests += 1
|
error_tests += 1
|
||||||
printf("Encountered error when parsing {}, at line {}: {}",
|
printf("Encountered error when parsing {}, at line {}: {}",
|
||||||
name, e.line_nr, e.str)
|
name, e.line_nr, e.str)
|
||||||
try:
|
try:
|
||||||
for line_nr in emu:
|
for line_nr in emu:
|
||||||
pass
|
pass
|
||||||
if isinstance(result, BaseException):
|
if isinstance(result, BaseException):
|
||||||
printf("Error should have happened in {}, but did not", name)
|
printf("Error should have happened in {}, but did not", name)
|
||||||
failed_tests += 1
|
failed_tests += 1
|
||||||
output = emu.getVal(register)
|
output = emu.getVal(register)
|
||||||
if output != result:
|
if output != result:
|
||||||
failed_tests += 1
|
failed_tests += 1
|
||||||
printf("Failed in {}. {} was {}, should be {}",
|
printf("Failed in {}. {} was {}, should be {}",
|
||||||
name, register, output, result)
|
name, register, output, result)
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
print(e)
|
print(e)
|
||||||
if not isinstance(result, BaseException) or not isinstance(e, result):
|
if not isinstance(result, BaseException) or not isinstance(e, result):
|
||||||
error_tests += 1
|
error_tests += 1
|
||||||
printf("Encountered error in {}, at operation {}: {}",
|
printf("Encountered error in {}, at operation {}: {}",
|
||||||
name, emu.getVal('%rip'), e)
|
name, emu.getVal('%rip'), e)
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
printf("Tests done! {}/{}.",
|
printf("Tests done! {}/{}.",
|
||||||
total_tests - failed_tests - error_tests, total_tests)
|
total_tests - failed_tests - error_tests, total_tests)
|
||||||
printf("{} failed, and {} encountered a python error",
|
printf("{} failed, and {} encountered a python error",
|
||||||
failed_tests, error_tests)
|
failed_tests, error_tests)
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# Arithmetic Operations
|
# Arithmetic Operations
|
||||||
|
@ -81,77 +81,77 @@ subq %rax, %rsi
|
||||||
# Branching
|
# Branching
|
||||||
|
|
||||||
branch_tests = [
|
branch_tests = [
|
||||||
("jg",10,">",5,0), ("jg",5,">",5,1), ("jg",5,">",10,1),
|
("jg",10,">",5,0), ("jg",5,">",5,1), ("jg",5,">",10,1),
|
||||||
("jl",10,"<",5,1), ("jl",5,"<",5,1), ("jl",5,"<",10,0),
|
("jl",10,"<",5,1), ("jl",5,"<",5,1), ("jl",5,"<",10,0),
|
||||||
("je",10,"==",5,1),("je",5,"==",5,0),("je",5,"==",10,1),
|
("je",10,"==",5,1),("je",5,"==",5,0),("je",5,"==",10,1),
|
||||||
("jge",10,">=",5,0),("jge",5,">=",5,0),("jge",5,">=",10,1),
|
("jge",10,">=",5,0),("jge",5,">=",5,0),("jge",5,">=",10,1),
|
||||||
("jle",10,"<=",5,1),("jle",5,"<=",5,0),("jle",5,"<=",10,0),
|
("jle",10,"<=",5,1),("jle",5,"<=",5,0),("jle",5,"<=",10,0),
|
||||||
("jne",10,"!=",5,0),("jne",5,"!=",5,1),("jne",5,"!=",10,0),
|
("jne",10,"!=",5,0),("jne",5,"!=",5,1),("jne",5,"!=",10,0),
|
||||||
]
|
]
|
||||||
|
|
||||||
for jump_instruct, a, comp, b, result in branch_tests:
|
for jump_instruct, a, comp, b, result in branch_tests:
|
||||||
add_test("branch {a} {comp} {b}={result}".format(a=a,comp=comp,b=b,result=not result), result, "%rsi", """
|
add_test("branch {a} {comp} {b}={result}".format(a=a,comp=comp,b=b,result=not result), result, "%rsi", """
|
||||||
start: cmpq ${b}, ${a}
|
start: cmpq ${b}, ${a}
|
||||||
{jump_instruct} true
|
{jump_instruct} true
|
||||||
movq $1, %rsi
|
movq $1, %rsi
|
||||||
jmp return
|
jmp return
|
||||||
true: movq $0, %rsi
|
true: movq $0, %rsi
|
||||||
return: ret
|
return: ret
|
||||||
""".format(a=a,b=b,jump_instruct=jump_instruct))
|
""".format(a=a,b=b,jump_instruct=jump_instruct))
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# Junk Comparisons
|
# Junk Comparisons
|
||||||
|
|
||||||
add_test("invalid comparison 1", infernal.JunkComparisonException, "None", """
|
add_test("invalid comparison 1", infernal.JunkComparisonException, "None", """
|
||||||
start: movq $100, %rsp # Set stack pointer to a random position.
|
start: movq $100, %rsp # Set stack pointer to a random position.
|
||||||
popq %rsi # Move a Junk value into %rsi
|
popq %rsi # Move a Junk value into %rsi
|
||||||
cmpq $10, %rsi # Do a Junk comparison, which triggers the `i` comp
|
cmpq $10, %rsi # Do a Junk comparison, which triggers the `i` comp
|
||||||
# virtual register.
|
# virtual register.
|
||||||
jg start # Attempt to do a jump to the start, if (Junk-10)>0,
|
jg start # Attempt to do a jump to the start, if (Junk-10)>0,
|
||||||
# which makes no sense, and thus throws an error.
|
# which makes no sense, and thus throws an error.
|
||||||
""")
|
""")
|
||||||
|
|
||||||
add_test("invalid addition 1", infernal.JunkComparisonException, "None", """
|
add_test("invalid addition 1", infernal.JunkComparisonException, "None", """
|
||||||
start: movq $100, %rsp # Set stack pointer to a random position.
|
start: movq $100, %rsp # Set stack pointer to a random position.
|
||||||
popq %rsi # Move a Junk value into %rsi
|
popq %rsi # Move a Junk value into %rsi
|
||||||
addq %rsi, %rsp # Adds Junk to 101, which produces Junk.
|
addq %rsi, %rsp # Adds Junk to 101, which produces Junk.
|
||||||
cmpq $10, %rsp # Do a Junk comparison, which triggers the `i` comp
|
cmpq $10, %rsp # Do a Junk comparison, which triggers the `i` comp
|
||||||
# virtual register.
|
# virtual register.
|
||||||
jg start # Attempt to do a jump to the start, if (Junk-10)>0,
|
jg start # Attempt to do a jump to the start, if (Junk-10)>0,
|
||||||
# which makes no sense, and thus throws an error.
|
# which makes no sense, and thus throws an error.
|
||||||
""")
|
""")
|
||||||
|
|
||||||
add_test("invalid addition 2", infernal.JunkComparisonException, "None", """
|
add_test("invalid addition 2", infernal.JunkComparisonException, "None", """
|
||||||
start: movq $100, %rsp # Set stack pointer to a random position.
|
start: movq $100, %rsp # Set stack pointer to a random position.
|
||||||
popq %rsi # Move a Junk value into %rsi
|
popq %rsi # Move a Junk value into %rsi
|
||||||
addq %rsp, %rsi # Adds 101 to Junk, which produces Junk.
|
addq %rsp, %rsi # Adds 101 to Junk, which produces Junk.
|
||||||
cmpq $10, %rsi # Do a Junk comparison, which triggers the `i` comp
|
cmpq $10, %rsi # Do a Junk comparison, which triggers the `i` comp
|
||||||
# virtual register.
|
# virtual register.
|
||||||
jg start # Attempt to do a jump to the start, if (Junk-10)>0,
|
jg start # Attempt to do a jump to the start, if (Junk-10)>0,
|
||||||
# which makes no sense, and thus throws an error.
|
# which makes no sense, and thus throws an error.
|
||||||
""")
|
""")
|
||||||
|
|
||||||
add_test("invalid subtraction 1", infernal.JunkComparisonException, "None", """
|
add_test("invalid subtraction 1", infernal.JunkComparisonException, "None", """
|
||||||
start: movq $100, %rsp # Set stack pointer to a random position.
|
start: movq $100, %rsp # Set stack pointer to a random position.
|
||||||
popq %rsi # Move a Junk value into %rsi
|
popq %rsi # Move a Junk value into %rsi
|
||||||
subq %rsi, %rsp # Adds Junk to 101, which produces Junk.
|
subq %rsi, %rsp # Adds Junk to 101, which produces Junk.
|
||||||
cmpq $10, %rsp # Do a Junk comparison, which triggers the `i` comp
|
cmpq $10, %rsp # Do a Junk comparison, which triggers the `i` comp
|
||||||
# virtual register.
|
# virtual register.
|
||||||
jg start # Attempt to do a jump to the start, if (Junk-10)>0,
|
jg start # Attempt to do a jump to the start, if (Junk-10)>0,
|
||||||
# which makes no sense, and thus throws an error.
|
# which makes no sense, and thus throws an error.
|
||||||
""")
|
""")
|
||||||
|
|
||||||
add_test("invalid subtraction 2", infernal.JunkComparisonException, "None", """
|
add_test("invalid subtraction 2", infernal.JunkComparisonException, "None", """
|
||||||
start: movq $100, %rsp # Set stack pointer to a random position.
|
start: movq $100, %rsp # Set stack pointer to a random position.
|
||||||
popq %rsi # Move a Junk value into %rsi
|
popq %rsi # Move a Junk value into %rsi
|
||||||
subq %rsp, %rsi # Adds 101 to Junk, which produces Junk.
|
subq %rsp, %rsi # Adds 101 to Junk, which produces Junk.
|
||||||
cmpq $10, %rsi # Do a Junk comparison, which triggers the `i` comp
|
cmpq $10, %rsi # Do a Junk comparison, which triggers the `i` comp
|
||||||
# virtual register.
|
# virtual register.
|
||||||
jg start # Attempt to do a jump to the start, if (Junk-10)>0,
|
jg start # Attempt to do a jump to the start, if (Junk-10)>0,
|
||||||
# which makes no sense, and thus throws an error.
|
# which makes no sense, and thus throws an error.
|
||||||
""")
|
""")
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
execute_tests()
|
execute_tests()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user