prog8/tinyvm/vm.py

671 lines
26 KiB
Python
Raw Normal View History

2018-02-25 15:43:00 +00:00
# 8/16 bit virtual machine
# machine specs:
# MEMORY: 64K bytes, treated as one single array, indexed per byte, ONLY DATA - NO CODE
# elements addressable as one of three elementary data types:
# 8-bit byte (singed and unsigned),
# 16-bit words (two 8-bit bytes, signed and unsigned) (stored in LSB order),
# 5-byte MFLPT floating point
# addressing is possible via byte index (for the $0000-$00ff range) or via an unsigned word.
2018-02-27 19:39:46 +00:00
# there is NO memory management at all; all of the mem is globally shared and always available in full.
# certain blocks of memory can be marked as read-only (write attempts will then crash the vm)
2018-02-25 15:43:00 +00:00
#
# MEMORY ACCESS: via explicit load and store instructions,
# to put a value onto the stack or store the value on the top of the stack,
# or in one of the dynamic variables.
#
# I/O: either via programmed I/O routines:
2018-03-02 23:07:44 +00:00
# write [byte/bytearray to text output/screen],
# read [byte/bytearray from keyboard],
# wait [till any input comes available], @todo
# check [if input is available) @todo
# or via memory-mapped I/O (text screen matrix, keyboard scan register) @todo
2018-02-25 15:43:00 +00:00
#
# CPU: stack based execution, no registers.
# unlimited dynamic variables (v0, v1, ...) that have a value and a type.
# types:
2018-02-27 19:39:46 +00:00
# 1-bit boolean,
# 8-bit byte (singed and unsigned),
# 16-bit words (two 8-bit bytes, signed and unsigned),
# floating point,
2018-02-25 15:43:00 +00:00
# array of bytes (signed and unsigned),
# array of words (signed and unsigned),
# matrix (2-dimensional array) of bytes (signed and unsigned).
2018-02-27 19:39:46 +00:00
# all of these can have the flag CONST as well which means they cannot be modified.
2018-02-25 15:43:00 +00:00
#
# push (constant,
# mark, unwind to previous mark.
#
# CPU INSTRUCTIONS:
# stack manipulation mainly:
# nop
# push var / push2 var1, var2
# pop var / pop2 var1, var2
2018-02-27 19:39:46 +00:00
# various arithmetic operations, logical operations, boolean test and comparison operations
2018-02-25 15:43:00 +00:00
# jump label
# jump_if_true label, jump_if_false label
2018-02-27 19:39:46 +00:00
# @todo jump_if_status_XX label special system dependent status register conditional check such as carry bit or overflow bit)
2018-02-25 15:43:00 +00:00
# return (return values on stack)
# syscall function (special system dependent implementation)
# call function (arguments are on stack)
2018-02-27 19:39:46 +00:00
# enter / exit (function call frame)
2018-02-25 15:43:00 +00:00
#
2018-02-27 21:16:45 +00:00
# TIMER INTERRUPT: triggered around each 1/30th of a second.
2018-02-25 15:43:00 +00:00
# executes on a DIFFERENT stack and with a different PROGRAM LIST,
2018-02-27 19:39:46 +00:00
# but with access to ALL THE SAME DYNAMIC VARIABLES.
2018-02-27 21:16:45 +00:00
# This suspends the main program until the timer program RETURNs!
2018-02-25 15:43:00 +00:00
#
import time
import itertools
2018-02-27 19:39:46 +00:00
import collections
import array
2018-02-27 21:16:45 +00:00
import threading
2018-02-27 19:39:46 +00:00
import pprint
2018-02-25 15:43:00 +00:00
from typing import Dict, List, Tuple, Union
from il65.emit import mflpt5_to_float, to_mflpt5
2018-03-02 23:07:44 +00:00
from .program import Instruction, Variable, Block, Program, Opcode
2018-02-25 15:43:00 +00:00
class ExecutionError(Exception):
pass
2018-02-27 19:39:46 +00:00
class TerminateExecution(SystemExit):
pass
class MemoryAccessError(Exception):
pass
2018-02-25 15:43:00 +00:00
class Memory:
def __init__(self):
self.mem = bytearray(65536)
2018-02-27 19:39:46 +00:00
self.readonly = bytearray(65536)
def mark_readonly(self, start: int, end: int) -> None:
self.readonly[start:end+1] = [1] * (end-start+1)
2018-02-25 15:43:00 +00:00
def get_byte(self, index: int) -> int:
return self.mem[index]
def get_sbyte(self, index: int) -> int:
return 256 - self.mem[index]
def get_word(self, index: int) -> int:
return self.mem[index] + 256 * self.mem[index+1]
def get_sword(self, index: int) -> int:
return 65536 - (self.mem[index] + 256 * self.mem[index+1])
def get_float(self, index: int) -> float:
return mflpt5_to_float(self.mem[index: index+5])
def set_byte(self, index: int, value: int) -> None:
2018-02-27 19:39:46 +00:00
if self.readonly[index]:
raise MemoryAccessError("read-only", index)
2018-02-25 15:43:00 +00:00
self.mem[index] = value
def set_sbyte(self, index: int, value: int) -> None:
2018-02-27 19:39:46 +00:00
if self.readonly[index]:
raise MemoryAccessError("read-only", index)
2018-02-25 15:43:00 +00:00
self.mem[index] = value + 256
def set_word(self, index: int, value: int) -> None:
2018-02-27 19:39:46 +00:00
if self.readonly[index] or self.readonly[index+1]:
raise MemoryAccessError("read-only", index)
2018-02-25 15:43:00 +00:00
hi, lo = divmod(value, 256)
self.mem[index] = lo
self.mem[index+1] = hi
def set_sword(self, index: int, value: int) -> None:
2018-02-27 19:39:46 +00:00
if self.readonly[index] or self.readonly[index+1]:
raise MemoryAccessError("read-only", index)
2018-02-25 15:43:00 +00:00
hi, lo = divmod(value + 65536, 256)
self.mem[index] = lo
self.mem[index+1] = hi
def set_float(self, index: int, value: float) -> None:
2018-02-27 19:39:46 +00:00
if any(self.readonly[index:index+5]):
raise MemoryAccessError("read-only", index)
2018-02-25 15:43:00 +00:00
self.mem[index: index+5] = to_mflpt5(value)
2018-02-27 21:16:45 +00:00
class CallFrameMarker:
2018-03-02 01:49:43 +00:00
__slots__ = ["returninstruction"]
2018-02-27 21:16:45 +00:00
2018-03-02 01:49:43 +00:00
def __init__(self, instruction: Instruction) -> None:
self.returninstruction = instruction
2018-02-27 21:16:45 +00:00
2018-03-02 01:49:43 +00:00
def __str__(self) -> str:
return repr(self)
2018-02-27 21:16:45 +00:00
2018-03-02 01:49:43 +00:00
def __repr__(self) -> str:
return "<CallFrameMarker returninstruction={:s}>".format(str(self.returninstruction))
2018-02-27 21:16:45 +00:00
2018-03-02 01:49:43 +00:00
StackValueType = Union[bool, int, float, bytearray, array.array, CallFrameMarker]
2018-02-25 15:43:00 +00:00
class Stack:
def __init__(self):
self.stack = []
2018-02-27 19:39:46 +00:00
self.pop_history = collections.deque(maxlen=10)
2018-02-25 15:43:00 +00:00
def debug_peek(self, size: int) -> List[StackValueType]:
return self.stack[-size:]
def size(self) -> int:
return len(self.stack)
def pop(self) -> StackValueType:
2018-02-27 19:39:46 +00:00
x = self.stack.pop()
self.pop_history.append(x)
return x
2018-02-25 15:43:00 +00:00
def pop2(self) -> Tuple[StackValueType, StackValueType]:
2018-02-27 19:39:46 +00:00
x, y = self.stack.pop(), self.stack.pop()
self.pop_history.append(x)
self.pop_history.append(y)
return x, y
2018-02-25 15:43:00 +00:00
def pop3(self) -> Tuple[StackValueType, StackValueType, StackValueType]:
2018-02-27 19:39:46 +00:00
x, y, z = self.stack.pop(), self.stack.pop(), self.stack.pop()
self.pop_history.append(x)
self.pop_history.append(y)
self.pop_history.append(z)
return x, y, z
2018-02-25 15:43:00 +00:00
2018-03-02 01:49:43 +00:00
def pop_under(self, number: int) -> StackValueType:
return self.stack.pop(-1-number)
2018-02-25 15:43:00 +00:00
def push(self, item: StackValueType) -> None:
self._typecheck(item)
self.stack.append(item)
def push2(self, first: StackValueType, second: StackValueType) -> None:
self._typecheck(first)
self._typecheck(second)
self.stack.append(first)
self.stack.append(second)
def push3(self, first: StackValueType, second: StackValueType, third: StackValueType) -> None:
self._typecheck(first)
self._typecheck(second)
self._typecheck(third)
self.stack.extend([first, second, third])
2018-03-02 01:49:43 +00:00
def push_under(self, number: int, value: StackValueType) -> None:
self.stack.insert(-number, value)
2018-02-25 15:43:00 +00:00
def peek(self) -> StackValueType:
return self.stack[-1] if self.stack else None
def swap(self) -> None:
x = self.stack[-1]
self.stack[-1] = self.stack[-2]
self.stack[-2] = x
def _typecheck(self, value: StackValueType):
2018-03-02 01:49:43 +00:00
if type(value) not in (bool, int, float, bytearray, array.array, CallFrameMarker):
2018-02-27 21:16:45 +00:00
raise TypeError("invalid item type pushed")
2018-02-25 15:43:00 +00:00
# noinspection PyPep8Naming,PyUnusedLocal,PyMethodMayBeStatic
class VM:
2018-02-27 19:39:46 +00:00
str_encoding = "iso-8859-15"
str_alt_encoding = "iso-8859-15"
readonly_mem_ranges = [] # type: List[Tuple[int, int]]
2018-02-27 21:16:45 +00:00
timer_irq_resolution = 1/30
timer_irq_interlock = threading.Lock()
timer_irq_event = threading.Event()
2018-02-25 15:43:00 +00:00
2018-02-27 21:16:45 +00:00
def __init__(self, program: Program, timerprogram: Program=Program([])) -> None:
2018-02-25 15:43:00 +00:00
opcode_names = [oc.name for oc in Opcode]
for ocname in opcode_names:
if not hasattr(self, "opcode_" + ocname):
raise NotImplementedError("missing opcode method for " + ocname)
for method in dir(self):
if method.startswith("opcode_"):
if not method[7:] in opcode_names:
raise RuntimeError("opcode method for undefined opcode " + method)
2018-03-02 01:49:43 +00:00
for oc in Opcode:
if oc not in self.dispatch_table:
raise NotImplementedError("no dispatch entry in table for " + oc.name)
2018-02-25 15:43:00 +00:00
self.memory = Memory()
2018-02-27 19:39:46 +00:00
for start, end in self.readonly_mem_ranges:
self.memory.mark_readonly(start, end)
2018-02-25 15:43:00 +00:00
self.main_stack = Stack()
self.timer_stack = Stack()
2018-02-27 21:16:45 +00:00
self.main_program, self.timer_program, self.variables, self.labels = self.flatten_programs(program, timerprogram)
2018-02-25 15:43:00 +00:00
self.connect_instruction_pointers(self.main_program)
self.connect_instruction_pointers(self.timer_program)
self.program = self.main_program
self.stack = self.main_stack
self.pc = None # type: Instruction
2018-02-27 19:39:46 +00:00
self.system = System(self)
assert all(i.next for i in self.main_program
if i.opcode != Opcode.TERMINATE), "main: all instrs next must be set"
assert all(i.next for i in self.timer_program
2018-02-27 21:16:45 +00:00
if i.opcode not in (Opcode.TERMINATE, Opcode.RETURN)), "timer: all instrs next must be set"
assert all(i.alt_next for i in self.main_program
if i.opcode in (Opcode.CALL, Opcode.JUMP_IF_FALSE, Opcode.JUMP_IF_TRUE)), "main: alt_nexts must be set"
assert all(i.alt_next for i in self.timer_program
if i.opcode in (Opcode.CALL, Opcode.JUMP_IF_FALSE, Opcode.JUMP_IF_TRUE)), "timer: alt_nexts must be set"
threading.Thread(target=self.timer_irq, name="timer_irq", daemon=True).start()
2018-02-27 19:39:46 +00:00
print("[TinyVM starting up.]")
2018-02-25 15:43:00 +00:00
2018-02-27 21:16:45 +00:00
def flatten_programs(self, main: Program, timer: Program) \
-> Tuple[List[Instruction], List[Instruction], Dict[str, Variable], Dict[str, Instruction]]:
variables = {} # type: Dict[str, Variable]
labels = {} # type: Dict[str, Instruction]
instructions_main = [] # type: List[Instruction]
instructions_timer = [] # type: List[Instruction]
for block in main.blocks:
flat = self.flatten(block, variables, labels)
instructions_main.extend(flat)
instructions_main.append(Instruction(Opcode.TERMINATE, [], None, None))
for block in timer.blocks:
flat = self.flatten(block, variables, labels)
instructions_timer.extend(flat)
return instructions_main, instructions_timer, variables, labels
2018-02-25 15:43:00 +00:00
def flatten(self, block: Block, variables: Dict[str, Variable], labels: Dict[str, Instruction]) -> List[Instruction]:
def block_prefix(b: Block) -> str:
if b.parent:
return block_prefix(b.parent) + "." + b.name
else:
return b.name
prefix = block_prefix(block)
instructions = block.instructions
for ins in instructions:
if ins.opcode == Opcode.SYSCALL:
continue
if ins.args:
newargs = []
for a in ins.args:
if type(a) is str:
newargs.append(prefix + "." + a)
else:
newargs.append(a)
ins.args = newargs
for vardef in block.variables:
vname = prefix + "." + vardef.name
assert vname not in variables
variables[vname] = vardef
for name, instr in block.labels.items():
name = prefix + "." + name
assert name not in labels
labels[name] = instr
for subblock in block.blocks:
instructions.extend(self.flatten(subblock, variables, labels))
del block.instructions
del block.variables
del block.labels
return instructions
def connect_instruction_pointers(self, instructions: List[Instruction]) -> None:
i1, i2 = itertools.tee(instructions)
next(i2, None)
for i, nexti in itertools.zip_longest(i1, i2):
2018-02-27 21:16:45 +00:00
if i.opcode in (Opcode.JUMP_IF_TRUE, Opcode.JUMP_IF_FALSE):
i.next = nexti # normal flow target
i.alt_next = self.labels[i.args[0]] # conditional jump target
elif i.opcode == Opcode.JUMP:
i.next = self.labels[i.args[0]] # jump target
elif i.opcode == Opcode.CALL:
2018-03-02 01:49:43 +00:00
i.next = self.labels[i.args[1]] # call target
2018-02-27 21:16:45 +00:00
i.alt_next = nexti # return instruction
else:
i.next = nexti
2018-02-25 15:43:00 +00:00
def run(self) -> None:
self.pc = self.program[0] # first instruction of the main program
2018-03-02 01:49:43 +00:00
self.stack.push(CallFrameMarker(None)) # enter the call frame so the timer program can end with a RETURN
2018-02-25 15:43:00 +00:00
try:
2018-02-27 21:16:45 +00:00
while self.pc is not None:
with self.timer_irq_interlock:
2018-02-28 01:40:16 +00:00
next_pc = self.dispatch_table[self.pc.opcode](self, self.pc)
2018-02-27 21:16:45 +00:00
if next_pc:
self.pc = self.pc.next
2018-02-27 19:39:46 +00:00
except TerminateExecution as x:
why = str(x)
2018-02-27 21:16:45 +00:00
print("[TinyVM execution terminated{:s}]\n".format(": "+why if why else "."))
2018-02-27 19:39:46 +00:00
return
2018-02-25 15:43:00 +00:00
except Exception as x:
print("EXECUTION ERROR")
self.debug_stack(5)
raise
2018-02-27 21:16:45 +00:00
else:
print("[TinyVM execution ended.]")
def timer_irq(self) -> None:
resolution = 1/30
wait_time = resolution
while True:
self.timer_irq_event.wait(wait_time)
self.timer_irq_event.clear()
start = time.perf_counter()
if self.timer_program:
with self.timer_irq_interlock:
previous_pc = self.pc
previous_program = self.program
previous_stack = self.stack
self.stack = self.timer_stack
self.program = self.timer_program
self.pc = self.program[0]
2018-03-02 01:49:43 +00:00
self.stack.push(CallFrameMarker(None)) # enter the call frame so the timer program can end with a RETURN
2018-02-27 21:16:45 +00:00
while self.pc is not None:
2018-02-28 01:40:16 +00:00
next_pc = self.dispatch_table[self.pc.opcode](self, self.pc)
2018-02-27 21:16:45 +00:00
if next_pc:
self.pc = self.pc.next
self.pc = previous_pc
self.program = previous_program
self.stack = previous_stack
current = time.perf_counter()
duration, previously = current - start, current
wait_time = max(0, resolution - duration)
2018-02-25 15:43:00 +00:00
def debug_stack(self, size: int=5) -> None:
stack = self.stack.debug_peek(size)
if len(stack) > 0:
2018-03-02 01:49:43 +00:00
print("** stack (top {:d}):".format(size))
2018-02-25 15:43:00 +00:00
for i, value in enumerate(reversed(stack), start=1):
2018-03-02 01:49:43 +00:00
print(" {:d}. {:s} {:s}".format(i, type(value).__name__, str(value)))
2018-02-25 15:43:00 +00:00
else:
2018-03-02 01:49:43 +00:00
print("** stack is empty.")
2018-02-27 19:39:46 +00:00
if self.stack.pop_history:
2018-03-02 01:49:43 +00:00
print("** last {:d} values popped from stack (most recent on top):".format(self.stack.pop_history.maxlen))
pprint.pprint(list(reversed(self.stack.pop_history)), indent=2, compact=True, width=20) # type: ignore
2018-02-25 15:43:00 +00:00
if self.pc is not None:
2018-02-27 19:39:46 +00:00
print("* instruction:", self.pc)
2018-02-25 15:43:00 +00:00
2018-02-27 19:39:46 +00:00
def assign_variable(self, variable: Variable, value: StackValueType) -> None:
assert not variable.const, "cannot modify a const"
variable.value = value
2018-02-25 15:43:00 +00:00
def opcode_NOP(self, instruction: Instruction) -> bool:
# do nothing
return True
def opcode_TERMINATE(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
raise TerminateExecution()
2018-02-25 15:43:00 +00:00
def opcode_PUSH(self, instruction: Instruction) -> bool:
value = self.variables[instruction.args[0]].value
self.stack.push(value)
return True
2018-03-02 01:49:43 +00:00
def opcode_DUP(self, instruction: Instruction) -> bool:
self.stack.push(self.stack.peek())
return True
def opcode_DUP2(self, instruction: Instruction) -> bool:
x = self.stack.peek()
self.stack.push(x)
self.stack.push(x)
return True
2018-02-25 15:43:00 +00:00
def opcode_PUSH2(self, instruction: Instruction) -> bool:
value1 = self.variables[instruction.args[0]].value
value2 = self.variables[instruction.args[1]].value
self.stack.push2(value1, value2)
return True
def opcode_PUSH3(self, instruction: Instruction) -> bool:
value1 = self.variables[instruction.args[0]].value
value2 = self.variables[instruction.args[1]].value
value3 = self.variables[instruction.args[2]].value
self.stack.push3(value1, value2, value3)
return True
def opcode_POP(self, instruction: Instruction) -> bool:
value = self.stack.pop()
variable = self.variables[instruction.args[0]]
2018-02-27 19:39:46 +00:00
self.assign_variable(variable, value)
2018-02-25 15:43:00 +00:00
return True
def opcode_POP2(self, instruction: Instruction) -> bool:
value1, value2 = self.stack.pop2()
variable = self.variables[instruction.args[0]]
2018-02-27 19:39:46 +00:00
self.assign_variable(variable, value1)
2018-02-25 15:43:00 +00:00
variable = self.variables[instruction.args[1]]
2018-02-27 19:39:46 +00:00
self.assign_variable(variable, value2)
2018-02-25 15:43:00 +00:00
return True
def opcode_POP3(self, instruction: Instruction) -> bool:
value1, value2, value3 = self.stack.pop3()
variable = self.variables[instruction.args[0]]
2018-02-27 19:39:46 +00:00
self.assign_variable(variable, value1)
2018-02-25 15:43:00 +00:00
variable = self.variables[instruction.args[1]]
2018-02-27 19:39:46 +00:00
self.assign_variable(variable, value2)
2018-02-25 15:43:00 +00:00
variable = self.variables[instruction.args[2]]
2018-02-27 19:39:46 +00:00
self.assign_variable(variable, value3)
2018-02-25 15:43:00 +00:00
return True
def opcode_ADD(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first + second) # type: ignore
2018-02-25 15:43:00 +00:00
return True
def opcode_SUB(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first - second) # type: ignore
2018-02-25 15:43:00 +00:00
return True
def opcode_MUL(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first * second) # type: ignore
2018-02-25 15:43:00 +00:00
return True
def opcode_DIV(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first / second) # type: ignore
2018-02-25 15:43:00 +00:00
return True
def opcode_AND(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first and second)
2018-02-25 15:43:00 +00:00
return True
def opcode_OR(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first or second)
2018-02-25 15:43:00 +00:00
return True
def opcode_XOR(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
ifirst = 1 if first else 0
isecond = 1 if second else 0
self.stack.push(bool(ifirst ^ isecond))
2018-02-25 15:43:00 +00:00
return True
def opcode_NOT(self, instruction: Instruction) -> bool:
self.stack.push(not self.stack.pop())
return True
2018-02-27 21:16:45 +00:00
def opcode_TEST(self, instruction: Instruction) -> bool:
self.stack.push(bool(self.stack.pop()))
return True
2018-02-25 15:43:00 +00:00
def opcode_CMP_EQ(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first == second)
return True
2018-02-25 15:43:00 +00:00
def opcode_CMP_LT(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first < second) # type: ignore
2018-02-25 15:43:00 +00:00
return True
def opcode_CMP_GT(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first > second) # type: ignore
2018-02-25 15:43:00 +00:00
return True
def opcode_CMP_LTE(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first <= second) # type: ignore
2018-02-25 15:43:00 +00:00
return True
def opcode_CMP_GTE(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first >= second) # type: ignore
2018-02-25 15:43:00 +00:00
return True
2018-02-27 21:16:45 +00:00
def opcode_CALL(self, instruction: Instruction) -> bool:
2018-03-02 01:49:43 +00:00
# arguments are already on the stack
self.stack.push_under(instruction.args[0], CallFrameMarker(instruction.alt_next))
2018-02-27 21:16:45 +00:00
return True
2018-02-25 15:43:00 +00:00
def opcode_RETURN(self, instruction: Instruction) -> bool:
2018-03-02 01:49:43 +00:00
callframe = self.stack.pop_under(instruction.args[0])
2018-03-02 23:07:44 +00:00
assert isinstance(callframe, CallFrameMarker), callframe
2018-03-02 01:49:43 +00:00
self.pc = callframe.returninstruction
2018-02-27 21:16:45 +00:00
return False
2018-02-25 15:43:00 +00:00
def opcode_JUMP(self, instruction: Instruction) -> bool:
2018-02-27 21:16:45 +00:00
return True # jump simply points to the next instruction elsewhere
2018-02-25 15:43:00 +00:00
def opcode_JUMP_IF_TRUE(self, instruction: Instruction) -> bool:
result = self.stack.pop()
if result:
2018-02-27 21:16:45 +00:00
self.pc = self.pc.alt_next # alternative next instruction
2018-02-25 15:43:00 +00:00
return False
return True
def opcode_JUMP_IF_FALSE(self, instruction: Instruction) -> bool:
result = self.stack.pop()
if result:
return True
2018-02-27 21:16:45 +00:00
self.pc = self.pc.alt_next # alternative next instruction
2018-02-25 15:43:00 +00:00
return False
def opcode_SYSCALL(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
call = getattr(self.system, "syscall_" + instruction.args[0], None)
2018-02-25 15:43:00 +00:00
if call:
return call()
else:
raise RuntimeError("no syscall method for " + instruction.args[0])
2018-02-28 01:40:16 +00:00
dispatch_table = {
Opcode.TERMINATE: opcode_TERMINATE,
Opcode.NOP: opcode_NOP,
Opcode.PUSH: opcode_PUSH,
Opcode.PUSH2: opcode_PUSH2,
Opcode.PUSH3: opcode_PUSH3,
Opcode.POP: opcode_POP,
Opcode.POP2: opcode_POP2,
Opcode.POP3: opcode_POP3,
2018-03-02 01:49:43 +00:00
Opcode.DUP: opcode_DUP,
Opcode.DUP2: opcode_DUP2,
2018-02-28 01:40:16 +00:00
Opcode.ADD: opcode_ADD,
Opcode.SUB: opcode_SUB,
Opcode.MUL: opcode_MUL,
Opcode.DIV: opcode_DIV,
Opcode.AND: opcode_AND,
Opcode.OR: opcode_OR,
Opcode.XOR: opcode_XOR,
Opcode.NOT: opcode_NOT,
Opcode.TEST: opcode_TEST,
Opcode.CMP_EQ: opcode_CMP_EQ,
Opcode.CMP_LT: opcode_CMP_LT,
Opcode.CMP_GT: opcode_CMP_GT,
Opcode.CMP_LTE: opcode_CMP_LTE,
Opcode.CMP_GTE: opcode_CMP_GTE,
Opcode.CALL: opcode_CALL,
Opcode.RETURN: opcode_RETURN,
Opcode.JUMP: opcode_JUMP,
Opcode.JUMP_IF_TRUE: opcode_JUMP_IF_TRUE,
Opcode.JUMP_IF_FALSE: opcode_JUMP_IF_FALSE,
Opcode.SYSCALL: opcode_SYSCALL,
}
2018-02-27 19:39:46 +00:00
class System:
def __init__(self, vm: VM) -> None:
self.vm = vm
def _encodestr(self, string: str, alt: bool=False) -> bytearray:
return bytearray(string, self.vm.str_alt_encoding if alt else self.vm.str_encoding)
2018-03-02 23:07:44 +00:00
def _decodestr(self, bb: Union[bytearray, array.array], alt: bool=False) -> str:
return str(bb, self.vm.str_alt_encoding if alt else self.vm.str_encoding) # type: ignore
2018-02-27 19:39:46 +00:00
2018-02-25 15:43:00 +00:00
def syscall_printstr(self) -> bool:
2018-02-27 19:39:46 +00:00
value = self.vm.stack.pop()
2018-02-27 21:16:45 +00:00
if isinstance(value, (bytearray, array.array)):
2018-02-25 15:43:00 +00:00
print(self._decodestr(value), end="")
return True
else:
2018-02-27 19:39:46 +00:00
raise TypeError("printstr expects bytearray", value)
2018-02-25 15:43:00 +00:00
2018-03-02 23:07:44 +00:00
def syscall_printchr(self) -> bool:
character = self.vm.stack.pop()
if isinstance(character, int):
print(self._decodestr(bytearray([character])), end="")
return True
else:
raise TypeError("printchr expects integer (1 char)", character)
def syscall_input(self) -> bool:
self.vm.stack.push(self._encodestr(input()))
return True
def syscall_getchr(self) -> bool:
self.vm.stack.push(self._encodestr(input() + '\n')[0])
return True
2018-02-25 15:43:00 +00:00
def syscall_decimalstr_signed(self) -> bool:
2018-02-27 19:39:46 +00:00
value = self.vm.stack.pop()
2018-02-25 15:43:00 +00:00
if type(value) is int:
2018-02-27 19:39:46 +00:00
self.vm.stack.push(self._encodestr(str(value)))
2018-02-25 15:43:00 +00:00
return True
else:
2018-02-27 19:39:46 +00:00
raise TypeError("decimalstr expects int", value)
2018-02-25 15:43:00 +00:00
def syscall_hexstr_signed(self) -> bool:
2018-02-27 19:39:46 +00:00
value = self.vm.stack.pop()
2018-02-25 15:43:00 +00:00
if type(value) is int:
if value >= 0: # type: ignore
strvalue = "${:x}".format(value)
else:
strvalue = "-${:x}".format(-value) # type: ignore
2018-02-27 19:39:46 +00:00
self.vm.stack.push(self._encodestr(strvalue))
2018-02-25 15:43:00 +00:00
return True
else:
2018-02-27 19:39:46 +00:00
raise TypeError("hexstr expects int", value)
def syscall_memwrite_byte(self) -> bool:
value, address = self.vm.stack.pop2()
self.vm.memory.set_byte(address, value) # type: ignore
return True
def syscall_memwrite_sbyte(self) -> bool:
value, address = self.vm.stack.pop2()
self.vm.memory.set_sbyte(address, value) # type: ignore
return True
2018-03-02 23:07:44 +00:00
def syscall_memwrite_word(self) -> bool:
value, address = self.vm.stack.pop2()
self.vm.memory.set_word(address, value) # type: ignore
return True
def syscall_memwrite_sword(self) -> bool:
value, address = self.vm.stack.pop2()
self.vm.memory.set_sword(address, value) # type: ignore
return True
def syscall_memwrite_float(self) -> bool:
value, address = self.vm.stack.pop2()
self.vm.memory.set_float(address, value) # type: ignore
return True
def syscall_memwrite_str(self) -> bool:
strbytes, address = self.vm.stack.pop2()
for i, b in enumerate(strbytes): # type: ignore
self.vm.memory.set_byte(address+i, b) # type: ignore
return True