1
0
infernal-interpreter/infernal_interpreter/Emulator.py

190 lines
5.4 KiB
Python
Raw Normal View History

import re
2024-07-09 23:03:12 +00:00
from . import Junk
from .opcodes import OPCODES
2024-07-09 23:15:47 +00:00
REGISTERS = [
'%rax',
'%rbx',
'%rcx',
'%rdx',
'%rsp',
'%rbp',
'%rsi',
'%rdi',
'%r8',
'%r9',
'%r10',
'%r11',
'%r12',
'%r13',
'%r14',
'%r15',
]
class CodeParseException(BaseException):
def __init__(self, line_nr, string):
2018-02-06 19:56:33 +00:00
self.line_nr = line_nr
self.string = string
2024-07-09 23:15:47 +00:00
class InternalExecutionException(BaseException):
2018-02-06 19:56:33 +00:00
pass
2024-07-09 23:15:47 +00:00
class Emulator:
def __init__(self, source_text, max_stack_size=1000):
2018-02-06 19:56:33 +00:00
self.source = source_text
self.registers = {}
for reg_name in REGISTERS:
self.registers[reg_name] = Junk.Junk()
self.stack = {}
for i in range(max_stack_size):
self.stack[i] = Junk.Junk()
self.code = []
self.labels = {}
self.changes = {}
2024-07-09 23:15:47 +00:00
self.status = {'i': True}
2018-02-06 19:56:33 +00:00
self.last_sp = 0
self.max_stack_size = 0
index = 0
for line in iter(source_text.splitlines()):
index = self.processSourceLine(line, index)
2024-07-09 23:15:47 +00:00
def setStack(self, *stack_list, **kwargs):
"""Sets various stack elements, starting from 0 and going up.
2018-02-06 19:56:33 +00:00
Automatically sets rsp. This can be disabled by passing set_rsp=False.
"""
i = len(self.stack)
for element in stack_list:
i -= 1
self.stack[i] = element
self.setRegs(rsp=i)
2024-07-09 23:15:47 +00:00
self.last_sp = i - 1
2018-02-06 19:56:33 +00:00
2024-07-09 23:15:47 +00:00
def setRegs(self, **reg_dict):
for reg_name, reg_val in reg_dict.items():
2024-07-09 23:15:47 +00:00
assert reg_name[0] != '%'
self.registers['%' + reg_name] = reg_val
2018-02-06 19:56:33 +00:00
2024-07-09 23:15:47 +00:00
def getVal(self, val_text):
if val_text[0] == '$':
2018-02-06 19:56:33 +00:00
return int(val_text[1:])
2024-07-09 23:15:47 +00:00
elif val_text[0] == '%':
2018-02-06 19:56:33 +00:00
return self.registers[val_text]
else:
2024-07-09 23:15:47 +00:00
raise ValueError(f'{val_text} is not an usable value name')
2018-02-06 19:56:33 +00:00
2024-07-09 23:15:47 +00:00
def compareVal(self, valT1, valT2):
2018-02-06 19:56:33 +00:00
val1 = self.getVal(valT2)
val2 = self.getVal(valT1)
2024-07-09 23:15:47 +00:00
if isinstance(val1, Junk.Junk) or isinstance(val2, Junk.Junk):
self.status['i'] = (True,)
2018-02-06 19:56:33 +00:00
return
2024-07-09 23:15:47 +00:00
self.status['g'] = val1 > val2
self.status['l'] = val1 < val2
self.status['e'] = val1 == val2
self.status['i'] = False
2018-02-06 19:56:33 +00:00
2024-07-09 23:15:47 +00:00
def changedRegisters(self, *args):
2018-02-06 19:56:33 +00:00
for reg, val in args:
self.changes[reg] = val
def processSourceLine(self, line_text, index):
tokens = re.findall(r'[^\s,]+', line_text)
if len(tokens) <= 0 or tokens[0][0] == '#':
return index
if tokens[0][-1] == ':':
self.labels[tokens[0][:-1]] = index
tokens = tokens[1:]
for i in range(len(tokens)):
2024-07-09 23:15:47 +00:00
if tokens[i][0] == '#':
2018-02-06 19:56:33 +00:00
tokens = tokens[:i]
break
self.code.append(tokens)
return index + 1
2024-07-09 23:15:47 +00:00
def regState(self, reg_name):
2018-02-06 19:56:33 +00:00
if reg_name in self.changes:
return self.changes[reg_name]
else:
2024-07-09 23:15:47 +00:00
return 'none'
2018-02-06 19:56:33 +00:00
2024-07-09 23:15:47 +00:00
def getLineSignature(self, line_nr):
return [
'\\' + token if token[0] == '$' or token[0] == '%' else token
for token in self.code[line_nr]
]
2018-02-06 19:56:33 +00:00
2024-07-09 23:15:47 +00:00
def pushToStack(self, new_element):
2018-02-06 19:56:33 +00:00
self.registers['%rsp'] -= 1
self.stack[self.registers['%rsp']] = new_element
2024-07-09 23:15:47 +00:00
self.changedRegisters(
('%rsp', 'change'), ('m' + str(self.registers['%rsp']), 'insert'),
)
2018-02-06 19:56:33 +00:00
self.max_stack_size = max(self.registers['%rsp'], self.max_stack_size)
2024-07-09 23:15:47 +00:00
def popFromStack(self):
2018-02-06 19:56:33 +00:00
temp = self.stack[self.registers['%rsp']]
2024-07-09 23:15:47 +00:00
self.changedRegisters(
('%rsp', 'change'), ('m' + str(self.registers['%rsp']), 'remove'),
)
2018-02-06 19:56:33 +00:00
self.registers['%rsp'] += 1
return temp
2024-07-09 23:15:47 +00:00
def jump(self, label, cond_op='or', **conditions):
if len(conditions) > 0 and self.status['i']:
2018-02-06 19:56:33 +00:00
raise Junk.JunkComparisonException(*self.status['i'])
and_, or_ = True, False
for cnd_name, cnd_val in conditions.items():
2018-02-06 19:56:33 +00:00
if self.status[cnd_name] == cnd_val:
or_ = True
else:
and_ = False
2024-07-09 23:15:47 +00:00
if or_ if cond_op == 'or' else and_:
2018-02-06 19:56:33 +00:00
self.registers['%rip'] = self.labels[label]
return True
else:
return False
2024-07-09 23:15:47 +00:00
def iterate(self):
2018-02-06 19:56:33 +00:00
old_rip = self.registers['%rip']
self.last_sp = self.registers['%rsp']
2024-07-09 23:15:47 +00:00
if isinstance(old_rip, Junk.Junk) or old_rip >= len(self.code):
2018-02-06 19:56:33 +00:00
return None
if not isinstance(self.registers['%rip'], int):
2024-07-09 23:15:47 +00:00
raise InternalExecutionException(
'Register %rip should be integer, but was '
+ str(self.registers['%rip']),
)
2018-02-06 19:56:33 +00:00
instruct = self.code[self.registers['%rip']]
opcode = instruct[0]
self.changes = {}
OPCODES[opcode](self, *instruct[1:])
if self.registers['%rip'] == old_rip:
self.registers['%rip'] += 1
else:
2024-07-09 23:15:47 +00:00
self.changedRegisters(('%rip', 'jump'))
2018-02-06 19:56:33 +00:00
return old_rip
def __iter__(self):
return self
def __next__(self):
2018-02-06 19:56:33 +00:00
output = self.iterate()
if output is None:
raise StopIteration()
else:
return output
2024-07-09 23:15:47 +00:00
def getUsedRegisters(self):
return [
reg_name
for reg_name, reg_val in self.registers.items()
if not isinstance(reg_val, Junk.Junk)
]