Rewrite using handlers, singledispatch, hooks

This commit is contained in:
Reid 'arrdem' McKenzie 2022-08-12 23:26:42 -06:00
parent a79644aa23
commit b1a309527a
4 changed files with 355 additions and 239 deletions

View file

@ -45,7 +45,7 @@ class FuncBuilder(object):
pass pass
case _: case _:
self._stack -= getattr(op, 'nargs', 0) self._stack -= getattr(op, "nargs", 0)
self._stack += 1 self._stack += 1
def write(self, op: Union[isa.Opcode, isa.Label, Sequence[isa.Opcode]]): def write(self, op: Union[isa.Opcode, isa.Label, Sequence[isa.Opcode]]):

View file

@ -9,8 +9,12 @@ context (a virtual machine) which DOES have an easily introspected and serialize
""" """
from copy import deepcopy from dataclasses import dataclass
from textwrap import indent from functools import (
singledispatch,
update_wrapper,
)
from typing import Optional
from ichor import isa from ichor import isa
from ichor.state import ( from ichor.state import (
@ -25,13 +29,42 @@ from ichor.state import (
) )
@dataclass
class InterpreterState(object):
module: Module
stackframe: Stackframe
clock: int = 0
class InterpreterError(Exception): class InterpreterError(Exception):
"""An error raised by the interpreter when something goes awry.""" """An error raised by the interpreter when something goes awry."""
def __init__(self, module, stack, message=None): def __init__(self, state: InterpreterState, message: str, cause: Optional[Exception] = None):
self.module = module super().__init__(message, cause)
self.stack = stack self.state = state
super().__init__(message)
class InterpreterReturn(Exception):
def __init__(self, val):
super().__init__()
self.val = val
class InterpreterRestart(Exception):
def __init__(self, state: InterpreterState):
super().__init__()
self.state = state
def handledispatch(func):
dispatcher = singledispatch(func)
def wrapper(self, state, opcode):
assert isinstance(state, InterpreterState)
assert isinstance(opcode, isa.Opcode)
return dispatcher.dispatch(opcode.__class__)(self, state, opcode)
wrapper.register = dispatcher.register
update_wrapper(wrapper, func)
return wrapper
class Interpreter(object): class Interpreter(object):
@ -39,238 +72,305 @@ class Interpreter(object):
def __init__(self, bootstrap_module: Module): def __init__(self, bootstrap_module: Module):
self.bootstrap = bootstrap_module self.bootstrap = bootstrap_module
def run(self, opcodes, stack=[]): def pre_instr(self, state: InterpreterState, opcode: isa.Opcode) -> InterpreterState:
"""Directly interpret some opcodes in the configured environment.""" return state
mod = self.bootstrap.copy() def post_instr(self, state: InterpreterState, opcode: isa.Opcode) -> InterpreterState:
main = mod.define_function(";<main>;;", opcodes) return state
main_fun = mod.functions[main]
main_ip = mod.labels[main]
stackframe = Stackframe(main_fun, main_ip, stack)
clock: int = 0
print(mod) def step(self, state: InterpreterState, opcode: isa.Opcode) -> InterpreterState:
return self.handle_opcode(state, opcode)
def _error(msg=None): def handle_unknown(self, state: InterpreterState, opcode: isa.Opcode) -> InterpreterState:
# Note this is pretty expensive because we have to snapshot the stack BEFORE we do anything raise InterpreterError(state, "Unsupported operation: {opcode}")
# And the stack object isn't immutable or otherwise designed for cheap snapshotting
raise InterpreterError(mod, deepcopy(stackframe), msg)
def _debug(): def handle_fault(self, state, opcode, message, cause=None) -> InterpreterState:
b = [] raise InterpreterError(state, message, cause)
b.append(f"clock {clock}:")
b.append(" stack:")
for offset, it in zip(range(0, len(stackframe), 1), stackframe):
b.append(f" {offset: <3} {it}")
b.append(f" op: {op}")
print(indent("\n".join(b), " " * stackframe.depth))
@handledispatch
def handle_opcode(self, state: InterpreterState, opcode: isa.Opcode) -> InterpreterState:
return self.handle_unknown(state, opcode)
while True: @handle_opcode.register(isa.IDENTIFIERC)
op = mod.codepage[stackframe._ip] def _handle_identifierc(self, state: InterpreterState, opcode: isa.IDENTIFIERC) -> InterpreterState:
_debug() name = opcode.val
clock += 1 if not (name in state.module.functions
or name in state.module.types
or any(name in t.constructors for t in state.module.types.values())):
return self.handle_fault(state, opcode, "IDENTIFIERC references unknown entity")
match op: state.stackframe.push(Identifier(name))
case isa.IDENTIFIERC(name): state.stackframe._ip += 1
if not (name in mod.functions return state
or name in mod.types
or any(name in t.constructors for t in mod.types.values())):
_error("IDENTIFIERC references unknown entity")
stackframe.push(Identifier(name)) @handle_opcode.register(isa.TYPEREF)
def _handle_typeref(self, state, opcode) -> InterpreterState:
case isa.TYPEREF(): id = state.stackframe.pop()
id = stackframe.pop()
if not isinstance(id, Identifier): if not isinstance(id, Identifier):
_error("TYPEREF consumes an identifier") return self.handle_fault(state, opcode, "TYPEREF consumes an identifier")
if not id.name in mod.types:
_error("TYPEREF must be given a valid type identifier")
stackframe.push(TypeRef(id.name)) if not id.name in state.module.types:
return self.handle_fault(state, opcode, "TYPEREF must be given a valid type identifier")
case isa.ARMREF(): state.stackframe.push(TypeRef(id.name))
id: Identifier = stackframe.pop() state.stackframe._ip += 1
return state
@handle_opcode.register(isa.ARMREF)
def _handle_armref(self, state, opcode) -> InterpreterState:
id: Identifier = state.stackframe.pop()
if not isinstance(id, Identifier): if not isinstance(id, Identifier):
_error("VARIANTREF consumes an identifier and a typeref") return self.handle_fault(state, opcode, "VARIANTREF consumes an identifier and a typeref")
t: TypeRef = stackframe.pop() t: TypeRef = state.stackframe.pop()
if not isinstance(t, TypeRef): if not isinstance(t, TypeRef):
_error("VARIANTREF consumes an identifier and a typeref") return self.handle_fault(state, opcode, "VARIANTREF consumes an identifier and a typeref")
type = mod.types[t.name] type = state.module.types[t.name]
if id.name not in type.constructors: if id.name not in type.constructors:
_error(f"VARIANTREF given {id.name!r} which does not name a constructor within {type!r}") return self.handle_fault(state, opcode, f"VARIANTREF given {id.name!r} which does not name a constructor within {type!r}")
stackframe.push(VariantRef(t, id.name)) state.stackframe.push(VariantRef(t, id.name))
state.stackframe._ip += 1
return state
case isa.ARM(n): @handle_opcode.register(isa.ARM)
armref: VariantRef = stackframe.pop() def _handle_arm(self, state: InterpreterState, opcode: isa.ARM) -> InterpreterState:
armref: VariantRef = state.stackframe.pop()
if not isinstance(armref, VariantRef): if not isinstance(armref, VariantRef):
_error("VARIANT must be given a valid constructor reference") return self.handle_fault(state, opcode, "VARIANT must be given a valid constructor reference")
ctor = mod.types[armref.type.name].constructors[armref.arm] ctor = state.module.types[armref.type.name].constructors[armref.arm]
if n != len(ctor): if opcode.nargs != len(ctor):
_error("VARIANT given n-args inconsistent with the type constructor") return self.handle_fault(state, opcode, "VARIANT given n-args inconsistent with the type constructor")
if n > len(stackframe): if opcode.nargs > len(state.stackframe):
_error("Stack size violation") return self.handle_fault(state, opcode, "Stack size violation")
# FIXME: Where does type variable to type binding occur? # FIXME: Where does type variable to type binding occur?
# Certainly needs to be AT LEAST here, where we also need to be doing some typechecking # Certainly needs to be AT LEAST here, where we also need to be doing some typechecking
v = Variant(armref.type.name, armref.arm, tuple(stackframe[:n])) v = Variant(armref.type.name, armref.arm, tuple(state.stackframe[:opcode.nargs]))
stackframe.drop(n) state.stackframe.drop(opcode.nargs)
stackframe.push(v) state.stackframe.push(v)
state.stackframe._ip += 1
return state
case isa.ATEST(n): @handle_opcode.register(isa.ATEST)
armref: VariantRef = stackframe.pop() def _handle_atest(self, state: InterpreterState, opcode: isa.ATEST) -> InterpreterState:
armref: VariantRef = state.stackframe.pop()
if not isinstance(armref, VariantRef): if not isinstance(armref, VariantRef):
_error("VTEST must be given a variant reference") return self.handle_fault(state, opcode, "VTEST must be given a variant reference")
inst: Variant = stackframe.pop() inst: Variant = state.stackframe.pop()
if not isinstance(inst, Variant): if not isinstance(inst, Variant):
_error("VTEST must be given an instance of a variant") return self.handle_fault(state, opcode, "VTEST must be given an instance of a variant")
if inst.type == armref.type.name and inst.variant == armref.arm: if inst.type == armref.type.name and inst.variant == armref.arm:
stackframe.goto(n) state.stackframe.goto(opcode.target)
continue else:
state.stackframe._ip += 1
case isa.GOTO(n): return state
if (n < 0):
_error("Illegal branch target")
stackframe.goto(n)
continue
case isa.DUP(n): @handle_opcode.register(isa.GOTO)
if (n > len(stackframe)): def _handle_goto(self, state, opcode: isa.GOTO) -> InterpreterState:
_error("Stack size violation") if (opcode.target < 0):
return self.handle_fault(state, opcode, "Illegal branch target")
state.stackframe.goto(opcode.target)
return state
stackframe.dup(n) @handle_opcode.register(isa.DUP)
def _handle_dupe(self, state, opcode: isa.DUP) -> InterpreterState:
if (opcode.nargs > len(state.stackframe)):
return self.handle_fault(state, opcode, "Stack size violation")
case isa.ROT(n): state.stackframe.dup(opcode.nargs)
if (n > len(stackframe)): state.stackframe._ip += 1
_error("Stack size violation") return state
stackframe.rot(n) @handle_opcode.register(isa.ROT)
def _handle_rot(self, state, opcode: isa.DUP) -> InterpreterState:
if (opcode.nargs > len(state.stackframe)):
return self.handle_fault(state, opcode, "Stack size violation")
case isa.DROP(n): state.stackframe.rot(opcode.nargs)
if (n > len(stackframe)): state.stackframe._ip += 1
_error("Stack size violation") return state
stackframe.drop(n) @handle_opcode.register(isa.DROP)
def _handle_drop(self, state, opcode: isa.DROP) -> InterpreterState:
if (opcode.nargs > len(state.stackframe)):
return self.handle_fault(state, opcode, "Stack size violation")
case isa.SLOT(n): state.stackframe.drop(opcode.nargs)
if (n < 0): state.stackframe._ip += 1
_error("SLOT must have a positive reference") return state
if (n > len(stackframe) - 1):
_error("SLOT reference out of range")
stackframe.slot(n)
case isa.FUNREF(): @handle_opcode.register(isa.SLOT)
id = stackframe.pop() def _handle_slot(self, state, opcode: isa.SLOT) -> InterpreterState:
if (opcode.target < 0):
return self.handle_fault(state, opcode, "SLOT must have a positive reference")
if (opcode.target > len(state.stackframe) - 1):
return self.handle_fault(state, opcode, "SLOT reference out of range")
state.stackframe.slot(opcode.target)
state.stackframe._ip += 1
return state
@handle_opcode.register(isa.FUNREF)
def _handle_funref(self, state, opcode) -> InterpreterState:
id = state.stackframe.pop()
if not isinstance(id, Identifier): if not isinstance(id, Identifier):
_error("FUNREF consumes an IDENTIFIER") return self.handle_fault(state, opcode, "FUNREF consumes an IDENTIFIER")
try: try:
# FIXME: Verify this statically # FIXME: Verify this statically
stackframe.push(FunctionRef.parse(id.name)) state.stackframe.push(FunctionRef.parse(id.name))
except: except:
_error("Invalid function ref") return self.handle_fault(state, opcode, "Invalid function ref")
case isa.CALLF(n): state.stackframe._ip += 1
sig = stackframe.pop() return state
@handle_opcode.register(isa.CALLF)
def _handle_callf(self, state, opcode: isa.CALLF) -> InterpreterState:
sig = state.stackframe.pop()
if not isinstance(sig, FunctionRef): if not isinstance(sig, FunctionRef):
_error("CALLF requires a funref at top of stack") return self.handle_fault(state, opcode, "CALLF requires a funref at top of stack")
fun = mod.functions[sig.name]
if n != len(fun.arguments): fun = state.module.functions[sig.name]
_error("CALLF target violation; argument count missmatch") if opcode.nargs != len(fun.arguments):
if n > len(stackframe): return self.handle_fault(state, opcode, "CALLF target violation; argument count missmatch")
_error("Stack size violation")
if opcode.nargs > len(state.stackframe):
return self.handle_fault(state, opcode, "Stack size violation")
try: try:
ip = mod.labels[fun.signature] ip = state.module.labels[fun.signature]
state.stackframe = state.stackframe.call(fun, ip)
except KeyError: except KeyError:
_error("Unknown target") return self.handle_fault(state, opcode, "Unknown FUNREF target")
stackframe = stackframe.call(fun, ip) return state
continue
case isa.RETURN(): @handle_opcode.register(isa.RETURN)
def _handle_return(self, state, opcode: isa.RETURN) -> InterpreterState:
n = 1 # FIXME: clean this up n = 1 # FIXME: clean this up
if (n > len(stackframe)): if (n > len(state.stackframe)):
_error("Stack size violation") return self.handle_fault(state, opcode, "Stack size violation")
if stackframe.depth == 0: if state.stackframe.depth == 0:
return stackframe[:n] raise InterpreterReturn(state.stackframe[:n])
if (len(stackframe._fun.returns) != n): if (len(state.stackframe._fun.returns) != n):
_error("Signature violation") return self.handle_fault(state, opcode, "Signature violation")
stackframe = stackframe.ret(n) state.stackframe = state.stackframe.ret(n)
continue return state
case isa.CLOSUREF(n): @handle_opcode.register(isa.CLOSUREF)
sig = stackframe.pop() def _handle_closuref(self, state: InterpreterState, opcode: isa.CLOSUREF) -> InterpreterState:
n = opcode.nargs
sig = state.stackframe.pop()
if not isinstance(sig, FunctionRef): if not isinstance(sig, FunctionRef):
_error("CLOSUREF requires a funref at top of stack") return self.handle_fault(state, opcode, "CLOSUREF requires a funref at top of stack")
fun = mod.functions[sig.name]
fun = state.module.functions[sig.name]
if not n <= len(fun.arguments): if not n <= len(fun.arguments):
_error("CLOSUREF target violation; too many parameters provided") return self.handle_fault(state, opcode, "CLOSUREF target violation; too many parameters provided")
if n > len(stackframe):
_error("Stack size violation") if n > len(state.stackframe):
return self.handle_fault(state, opcode, "Stack size violation")
c = Closure( c = Closure(
sig, sig,
stackframe[:n] state.stackframe[:n]
) )
stackframe.drop(n) state.stackframe.drop(n)
stackframe.push(c) state.stackframe.push(c)
state.stackframe._ip += 1
return state
case isa.CLOSUREC(n): @handle_opcode.register(isa.CLOSUREC)
c = stackframe.pop() def _handle_closurec(self, state, opcode: isa.CLOSUREC) -> InterpreterState:
n = opcode.nargs
c = state.stackframe.pop()
if not isinstance(c, Closure): if not isinstance(c, Closure):
_error("CLOSUREC requires a closure at top of stack") return self.handle_fault(state, opcode, "CLOSUREC requires a closure at top of stack")
fun = mod.functions[c.funref.name]
fun = state.module.functions[c.funref.name]
if n + len(c.frag) > len(fun.arguments): if n + len(c.frag) > len(fun.arguments):
_error("CLOSUREC target violation; too many parameters provided") return self.handle_fault(state, opcode, "CLOSUREC target violation; too many parameters provided")
if n > len(stackframe):
_error("Stack size violation") if n > len(state.stackframe):
return self.handle_fault(state, opcode, "Stack size violation")
c = Closure( c = Closure(
c.funref, c.funref,
stackframe[:n] + c.frag state.stackframe[:n] + c.frag
) )
stackframe.drop(n) state.stackframe.drop(n)
stackframe.push(c) state.stackframe.push(c)
state.stackframe._ip += 1
return state
case isa.CALLC(n): @handle_opcode.register(isa.CALLC)
c = stackframe.pop() def _handle_callc(self, state, opcode: isa.CALLC) -> InterpreterState:
n = opcode.nargs
c = state.stackframe.pop()
if not isinstance(c, Closure): if not isinstance(c, Closure):
_error("CALLC requires a closure at top of stack") return self.handle_fault(state, opcode, "CALLC requires a closure at top of stack")
fun = mod.functions[c.funref.name] fun = state.module.functions[c.funref.name]
if n + len(c.frag) != len(fun.arguments): if n + len(c.frag) != len(fun.arguments):
_error("CALLC target vionation; argument count missmatch") return self.handle_fault(state, opcode, "CALLC target vionation; argument count missmatch")
if n > len(stackframe): if n > len(state.stackframe):
_error("Stack size violation") return self.handle_fault(state, opcode, "Stack size violation")
# Extract the function signature
# Push the closure's stack fragment # Push the closure's stack fragment
stackframe._stack = c.frag + stackframe._stack state.stackframe._stack = c.frag + state.stackframe._stack
# Perform a "normal" funref call # Perform a "normal" funref call
try: try:
ip = mod.labels[fun.signature] ip = state.module.labels[fun.signature]
except KeyError: except KeyError:
_error("Unknown target") return self.handle_fault(state, opcode, "Unknown target")
stackframe = stackframe.call(fun, ip) state.stackframe = state.stackframe.call(fun, ip)
return state
@handle_opcode.register(isa.BREAK)
def _handle_break(self, state, _) -> InterpreterState:
raise InterpreterReturn(state.stackframe._stack)
def run(self, opcodes, stack=[]):
"""Directly interpret some opcodes in the configured environment."""
_mod = self.bootstrap.copy()
_main = _mod.define_function(";<main>;;", opcodes)
_main_fun = _mod.functions[_main]
_main_ip = _mod.labels[_main]
state = InterpreterState(
_mod, Stackframe(_main_fun, _main_ip, stack)
)
while True:
try:
opcode = state.module.codepage[state.stackframe._ip]
self.pre_instr(state, opcode)
state = self.handle_opcode(state, opcode)
self.post_instr(state, opcode)
state.clock += 1
# FIXME: This case analysis isn't super obvious.
except InterpreterReturn as r:
return r.val
except InterpreterRestart as r:
state = r.state
continue continue
case isa.BREAK(): except Exception as e:
# FIXME: let users override this / set custom handlers raise e
return stackframe._stack
case _:
raise Exception(f"Unhandled interpreter state {op}")
stackframe._ip += 1

View file

@ -1,10 +1,29 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from textwrap import indent
from ichor import isa
from ichor.bootstrap import BOOTSTRAP from ichor.bootstrap import BOOTSTRAP
from ichor.interpreter import Interpreter from ichor.interpreter import (
Interpreter,
InterpreterState,
)
import pytest import pytest
class LoggingInterpreter(Interpreter):
def pre_instr(self, state: InterpreterState, opcode: isa.Opcode) -> InterpreterState:
b = []
b.append(f"clock {state.clock}:")
b.append(" stack:")
for offset, it in zip(range(0, len(state.stackframe), 1), state.stackframe):
b.append(f" {offset: <3} {it}")
b.append(f" op: {opcode}")
print(indent("\n".join(b), " " * state.stackframe.depth))
return state
@pytest.fixture @pytest.fixture
def vm(): def vm():
return Interpreter(BOOTSTRAP) return LoggingInterpreter(BOOTSTRAP)

View file

@ -4,11 +4,8 @@ Tests coverign the VM interpreter
from .fixtures import * # noqa from .fixtures import * # noqa
from ichor.bootstrap import (
FALSE,
TRUE,
)
from ichor import isa from ichor import isa
from ichor.bootstrap import FALSE, TRUE
from ichor.interpreter import InterpreterError from ichor.interpreter import InterpreterError
import pytest import pytest