1
0
infernal-interpreter/infernal_interpreter/AsciiPainter.py

148 lines
5.2 KiB
Python
Raw Permalink Normal View History

import subprocess
NORMAL_COLOR = '\033[0m'
REG_STATUS_TO_COLOR = {
2024-07-09 23:15:47 +00:00
'insert': '\033[32m',
'change': '\033[33m',
'remove': '\033[31m',
'none': '\033[0m',
'jump': '\033[36m',
}
2024-07-09 23:15:47 +00:00
class AsciiPainter:
def __init__(self, registers=None, max_stack_size=8, stack_draw_mode='up'):
2018-02-06 19:56:33 +00:00
self.registers = registers if registers else REGISTERS
self.states = []
self.max_stack_size = max_stack_size
self.stack_draw_mode = stack_draw_mode
2024-07-09 23:15:47 +00:00
def saveState(self, emu, line_nr: int | None = None) -> None:
2018-02-06 19:56:33 +00:00
# Init state
state = []
self.states.append(state)
# Write opcode to state
if line_nr is not None:
2018-02-06 19:56:33 +00:00
signature = emu.getLineSignature(line_nr)
for token in signature:
2024-07-09 23:15:47 +00:00
state.append({'state': 'none', 'val': token})
2018-02-06 19:56:33 +00:00
#
2024-07-09 23:15:47 +00:00
for i in range(3 - len(state)):
state.append({'state': 'none', 'val': ''})
for i in range(len(state) - 3):
state.pop()
state.append({'state': 'none', 'val': ''})
2018-02-06 19:56:33 +00:00
# Write registers to state
for reg_name in self.registers:
2024-07-09 23:15:47 +00:00
if reg_name == '':
state.append({'state': 'none', 'val': ''})
2018-02-06 19:56:33 +00:00
continue
reg_state = emu.regState(reg_name)
2024-07-09 23:15:47 +00:00
state.append({'state': reg_state, 'val': emu.getVal(reg_name)})
2018-02-06 19:56:33 +00:00
# Write stack to state
stack_base_pointer = emu.getVal('%rbp')
base_sp = emu.getVal('%rsp')
2024-07-09 23:15:47 +00:00
assert isinstance(emu.getVal('%rsp'), int)
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],
},
)
2018-02-06 19:56:33 +00:00
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:
2024-07-09 23:15:47 +00:00
state.append(
{
'state': emu.regState('m' + str(stack_i)),
'val': emu.stack[stack_i],
},
)
2018-02-06 19:56:33 +00:00
if stack_base_pointer == index:
stack_base_pointer = emu.stack[base_pointer]
# TODO: Draw stack frame seperators
else:
2024-07-09 23:15:47 +00:00
state.append({'state': 'none', 'val': ''})
2018-02-06 19:56:33 +00:00
2024-07-09 23:15:47 +00:00
def getWidthOfColumns(
2024-07-09 23:19:26 +00:00
self,
number_states: int,
number_states_per_block: int,
number_state_vars: int,
2024-07-09 23:15:47 +00:00
) -> list[int]:
widths = [0 for i in range(0, number_states_per_block)]
for base_state in range(0, number_states, number_states_per_block):
2024-07-09 23:15:47 +00:00
for var_i in range(0, number_state_vars):
for state_i in range(
2024-07-09 23:19:26 +00:00
0,
min(number_states_per_block, number_states - base_state),
2024-07-09 23:15:47 +00:00
):
width = len(str(self.states[base_state + state_i][var_i]['val']))
widths[state_i] = max(widths[state_i], width)
2018-02-06 19:56:33 +00:00
return widths
2024-07-09 23:15:47 +00:00
def getWidthOfNameColumn(self, number_state_vars: int) -> int:
2018-02-06 19:56:33 +00:00
widest = 0
for var_i in range(0, number_state_vars):
2024-07-09 23:15:47 +00:00
widest = max(widest, len(self.nameOfVar(var_i - 4)))
2018-02-06 19:56:33 +00:00
return widest
2024-07-09 23:15:47 +00:00
def to_string(self, emu) -> str:
2018-02-06 19:56:33 +00:00
number_states = len(self.states)
number_state_vars = len(self.states[0])
term_width = int(subprocess.check_output(['tput', 'cols']))
2024-07-09 23:15:47 +00:00
number_states_per_block: int = term_width // (10 + 2) - 1
2018-02-06 19:56:33 +00:00
separator = '-' * term_width
output: list[str] = []
2018-02-06 19:56:33 +00:00
2024-07-09 23:15:47 +00:00
name_fmt = '{}{:' + str(self.getWidthOfNameColumn(number_state_vars)) + '} '
column_fmt = [
'{}{:>' + str(width) + '} '
for width in self.getWidthOfColumns(
2024-07-09 23:19:26 +00:00
number_states,
number_states_per_block,
number_state_vars,
2024-07-09 23:15:47 +00:00
)
]
2018-02-06 19:56:33 +00:00
for base_state in range(0, number_states, number_states_per_block):
output.append(separator)
2018-02-06 19:20:09 +00:00
for var_i in range(0, number_state_vars):
2024-07-09 23:15:47 +00:00
output.append(name_fmt.format(NORMAL_COLOR, self.nameOfVar(var_i - 4)))
for state_i in range(
2024-07-09 23:19:26 +00:00
0,
min(number_states_per_block, number_states - base_state),
2024-07-09 23:15:47 +00:00
):
var = self.states[base_state + state_i][var_i]
output.append(
column_fmt[state_i].format(
2024-07-09 23:19:26 +00:00
REG_STATUS_TO_COLOR[var['state']],
var['val'],
NORMAL_COLOR,
2024-07-09 23:15:47 +00:00
),
)
output.append('\n')
output.append(separator)
2018-02-06 19:56:33 +00:00
return ''.join(output)
2018-02-06 19:56:33 +00:00
2024-07-09 23:15:47 +00:00
def nameOfVar(self, i):
if i < 0:
return ''
2018-02-06 19:56:33 +00:00
if i < len(self.registers):
return self.registers[i]
elif i != len(self.registers):
2024-07-09 23:15:47 +00:00
state_i = i - len(self.registers) - 2
2018-02-06 19:56:33 +00:00
return 'TOS{}{}'.format('+' if state_i < 0 else '-', abs(state_i))
else:
return ''
2024-07-09 23:15:47 +00:00
def __str__(self):
2018-02-06 19:56:33 +00:00
return 'AsciiPainter[]'