2022-04-12 07:49:12 +00:00
|
|
|
"""The instruction set for Shogoth."""
|
|
|
|
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
import typing as t
|
2022-04-12 07:49:12 +00:00
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
from .typing import *
|
2022-04-15 06:31:58 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
|
|
|
|
class Opcode:
|
2022-06-01 05:08:29 +00:00
|
|
|
####################################################################################################
|
|
|
|
# Logic
|
|
|
|
####################################################################################################
|
2022-06-11 06:28:24 +00:00
|
|
|
|
|
|
|
# FIXME: This should become an instantiation of the BOOL enum
|
2022-06-01 05:54:11 +00:00
|
|
|
class TRUE(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
"""() -> (bool)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
Push the constant TRUE onto the stack.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
# FIXME: This should become an instantiation of the BOOL enum
|
2022-06-01 05:54:11 +00:00
|
|
|
class FALSE(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
"""() -> (bool)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
Push the constant FALSE onto the stack.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
# FIXME: This should become a `VTEST` macro ... or may be replaceable
|
2022-06-01 05:54:11 +00:00
|
|
|
class IF(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
"""(bool) -> ()
|
2022-06-01 06:22:53 +00:00
|
|
|
|
|
|
|
Branch to another point if the top item of the stack is TRUE. Otherwise fall through.
|
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
target: int
|
|
|
|
|
|
|
|
# not, and, or, xor etc. can all be functions given if.
|
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
class GOTO(t.NamedTuple):
|
|
|
|
"""() -> ()
|
|
|
|
|
|
|
|
Branch to another point within the same bytecode segment. The target MUST be within the same module range as the
|
|
|
|
current function. Branching does NOT update the name or module of the current function.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
target: int
|
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
####################################################################################################
|
|
|
|
# Stack manipulation
|
|
|
|
####################################################################################################
|
2022-06-01 06:22:53 +00:00
|
|
|
# https://wiki.laptop.org/go/Forth_stack_operators
|
|
|
|
# https://www.forth.com/starting-forth/2-stack-manipulation-operators-arithmetic/
|
|
|
|
# https://docs.oracle.com/javase/specs/jvms/se18/html/jvms-6.html#jvms-6.5.swap
|
2022-06-11 06:28:24 +00:00
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class DUP(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
"""(A, B, ...) -> (A, B, ...)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
Duplicate the top N items of the stack.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
nargs: int = 1
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class ROT(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
"""(A, B, ... Z) -> (Z, A, B, ...)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
Rotate the top N elements of the stack.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
nargs: int = 2
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class DROP(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
"""(*) -> ()
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
Drop the top N items of the stack.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
nargs: int = 1
|
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
class SLOT(t.NamedTuple):
|
|
|
|
"""(..., A) -> (A, ..., A)
|
2022-04-21 06:17:59 +00:00
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
Copy the Nth (counting up from 0 at the bottom of the stack) item to the top of the stack.
|
|
|
|
Intended to allow users to emulate (immutable) frame local slots for reused values.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
target: int
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
####################################################################################################
|
|
|
|
# Functional abstraction
|
|
|
|
####################################################################################################
|
2022-04-12 07:49:12 +00:00
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
class IDENTIFIERC(t.NamedTuple):
|
|
|
|
"""() -> (IDENTIFIER)
|
2022-04-12 07:49:12 +00:00
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
An inline constant which produces an identifier to the stack.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
Identifiers name functions, fields and types but are not strings.
|
|
|
|
They are a VM-internal naming structure with reference to the module.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
val: str
|
2022-04-12 07:49:12 +00:00
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class FUNREF(t.NamedTuple):
|
2022-06-11 06:28:24 +00:00
|
|
|
"""(IDENTIFIER) -> (`FUNREF<... A to ... B>`)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
Construct a reference to a static codepoint.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
"""
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class CALLF(t.NamedTuple):
|
2022-04-21 06:17:59 +00:00
|
|
|
"""(`FUNREF<... A to ... B>`, ... A) -> (... B)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
Call [funref]
|
|
|
|
|
|
|
|
Make a dynamic call to the function reference at the top of stack.
|
|
|
|
The callee will see a stack containg only the provided `nargs`.
|
|
|
|
A subsequent RETURN will return execution to the next point.
|
|
|
|
|
|
|
|
Executing a `CALL` pushes the name and module path of the current function.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
nargs: int = 0
|
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
class RETURN(t.NamedTuple):
|
|
|
|
"""(... A) -> ()
|
|
|
|
|
|
|
|
Return to the source of the last `CALL`. The returnee will see the top `nargs` values of the present stack
|
|
|
|
appended to theirs. All other values on the stack will be discarded.
|
|
|
|
|
|
|
|
Executing a `RETURN` pops (restores) the name and module path of the current function back to that of the caller.
|
|
|
|
|
|
|
|
If the call stack is empty, `RETURN` will exit the interpreter.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
nargs: int
|
|
|
|
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class CLOSUREF(t.NamedTuple):
|
2022-04-21 06:17:59 +00:00
|
|
|
"""(`FUNREF<A, ... B to ... C>`, A) -> (`CLOSURE<... B to ... C>`)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
|
|
|
Construct a closure over the function reference at the top of the stack. This may produce nullary closures.
|
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
"""
|
2022-06-01 06:11:14 +00:00
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
nargs: int = 0
|
2022-04-21 06:17:59 +00:00
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class CLOSUREC(t.NamedTuple):
|
2022-04-21 06:17:59 +00:00
|
|
|
"""(`CLOSURE<A, ... B to ... C>`, A) -> (`CLOSURE<... B to ... C>`)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
|
|
|
Further close over the closure at the top of the stack. This may produce nullary closures.
|
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
"""
|
2022-06-01 06:11:14 +00:00
|
|
|
|
2022-06-01 06:09:59 +00:00
|
|
|
nargs: int = 0
|
2022-04-21 06:17:59 +00:00
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class CALLC(t.NamedTuple):
|
2022-05-31 15:36:47 +00:00
|
|
|
"""(`CLOSURE<... A to ... B>`, ... A) -> (... B)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
Call [closure]
|
|
|
|
|
|
|
|
Make a dynamic call to the closure at the top of stack.
|
|
|
|
The callee will see a stack containg only the provided `nargs` and closed-overs.
|
|
|
|
A subsequent RETURN will return execution to the next point.
|
|
|
|
|
|
|
|
Executing a `CALL` pushes the name and module path of the current function.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
nargs: int = 0
|
|
|
|
|
2022-05-31 15:36:47 +00:00
|
|
|
####################################################################################################
|
|
|
|
# Structures
|
|
|
|
####################################################################################################
|
2022-06-07 16:32:15 +00:00
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
# FIXME: This lacks any sort of way to do dynamic type/field references
|
2022-06-07 16:32:15 +00:00
|
|
|
|
|
|
|
class TYPEREF(t.NamedTuple):
|
|
|
|
"""(IDENTIFIER) -> (TYPEREF)
|
|
|
|
|
|
|
|
Produces a TYPEREF to the type named by the provided IDENTIFIER.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
class FIELDREF(t.NamedTuple):
|
|
|
|
"""(IDENTIFIER, TYPEREF) -> (FIELDREF)
|
|
|
|
|
|
|
|
|
|
|
|
Produces a FIELDREF to the field named by the provided IDENTIFIER.
|
|
|
|
The FIELDREF must be within and with reference to a sum type.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
class VARIANTREF(t.NamedTuple):
|
|
|
|
"""(IDENTIFIER, TYPEREF) -> (VARIANTREF)
|
|
|
|
|
|
|
|
Produce a VARIANTREF to an 'arm' of the given variant type.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class STRUCT(t.NamedTuple):
|
2022-06-07 16:32:15 +00:00
|
|
|
"""(STRUCTREF<S>, ...) -> (S)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-05-31 15:36:47 +00:00
|
|
|
Consume the top N items of the stack, producing a struct of the type `structref`.
|
|
|
|
|
|
|
|
The name and module path of the current function MUST match the name and module path of `structref`.
|
2022-06-07 16:32:15 +00:00
|
|
|
The arity of this opcode MUST match the arity of the struct.
|
|
|
|
The signature of the struct MUST match the signature fo the top N of the stack.
|
2022-05-31 15:36:47 +00:00
|
|
|
"""
|
|
|
|
|
2022-06-07 16:32:15 +00:00
|
|
|
nargs: int = 0
|
2022-05-31 15:36:47 +00:00
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class FLOAD(t.NamedTuple):
|
2022-06-07 16:32:15 +00:00
|
|
|
"""(FIELDREF<f ⊢ T ∈ S>, S) -> (T)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-05-31 15:36:47 +00:00
|
|
|
Consume the struct reference at the top of the stack, producing the value of the referenced field.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-05-31 15:36:47 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
fieldref: str
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class FSTORE(t.NamedTuple):
|
2022-06-07 16:32:15 +00:00
|
|
|
"""(FIELDREF<f ⊢ T ∈ S>, S, T) -> (S)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
|
|
|
Consume the struct reference at the top of the stack and a value, producing a new copy of the struct in which
|
|
|
|
that field has been updated to the new value.
|
|
|
|
|
2022-05-31 15:36:47 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
fieldref: str
|
2022-04-21 06:17:59 +00:00
|
|
|
|
2022-06-07 16:32:15 +00:00
|
|
|
class VARIANT(t.NamedTuple):
|
|
|
|
"""(VARIANTREF<a ⊢ A ⊂ B>, ...) -> (B)
|
|
|
|
|
|
|
|
Construct an instance of an 'arm' of a variant.
|
|
|
|
The type of the 'arm' is considered to be the type of the whole variant.
|
|
|
|
|
|
|
|
The name and module path of the current function MUST match the name and module path of `VARIANTREF`.
|
|
|
|
The arity of this opcode MUST match the arity of the arm.
|
|
|
|
The signature of the arm MUST match the signature fo the top N of the stack.
|
|
|
|
"""
|
|
|
|
|
|
|
|
nargs: int = 0
|
|
|
|
|
|
|
|
class VTEST(t.NamedTuple):
|
|
|
|
"""(VARIANTREF<a ⊢ A ⊂ B>, B) -> (bool)
|
|
|
|
|
|
|
|
Test whether B is a given arm of a variant A .
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
class VLOAD(t.NamedTuple):
|
|
|
|
"""(VARIANTREF<a ⊢ A ⊂ B>, B) -> (A)
|
|
|
|
|
|
|
|
Load the value of the variant arm.
|
|
|
|
VLOAD errors (undefined) if B is not within the variant.
|
|
|
|
VLOAD errors (undefined) if the value in B is not an A - use VTEST as needed.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
####################################################################################################
|
|
|
|
# Arrays
|
|
|
|
####################################################################################################
|
2022-06-01 05:54:11 +00:00
|
|
|
class ARRAY(t.NamedTuple):
|
2022-04-21 06:17:59 +00:00
|
|
|
"""(*) -> (ARRAY<Y>)
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
Consume the top N items of the stack, producing an array of the type `typeref`.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
typeref: str
|
2022-04-15 06:31:58 +00:00
|
|
|
nargs: int
|
2022-04-12 07:49:12 +00:00
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class ALOAD(t.NamedTuple):
|
2022-06-01 06:22:53 +00:00
|
|
|
"""(ARRAY<T>, NAT) -> (T)
|
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
Consume a reference to an array and an index, producing the value at that index.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
|
|
|
FIXME: Or a signal/fault.
|
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class ASTORE(t.NamedTuple):
|
2022-06-01 06:22:53 +00:00
|
|
|
"""(ARRAY<T>, NAT, T) -> (ARRAY<T>)
|
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
Consume a value T, storing it at an index in the given array.
|
|
|
|
Produces the updated array as the top of stack.
|
2022-06-01 06:22:53 +00:00
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
"""
|
2022-04-12 07:49:12 +00:00
|
|
|
|
2022-06-01 05:08:29 +00:00
|
|
|
####################################################################################################
|
|
|
|
# Naturals
|
|
|
|
####################################################################################################
|
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
# Integers
|
|
|
|
####################################################################################################
|
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
# Ratios
|
|
|
|
####################################################################################################
|
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
class BREAK(t.NamedTuple):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class Module(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
opcodes: list = []
|
|
|
|
functions: dict = {}
|
|
|
|
types: dict = {}
|
|
|
|
constants: dict = {}
|
|
|
|
|
|
|
|
def copy(self):
|
|
|
|
return Module(
|
|
|
|
self.opcodes.copy(),
|
|
|
|
self.functions.copy(),
|
|
|
|
self.types.copy(),
|
|
|
|
self.constants.copy(),
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
2022-04-21 06:17:59 +00:00
|
|
|
def translate(start: int, end: int, i: "Opcode"):
|
|
|
|
# FIXME: Consolidate bounds checks somehow
|
2022-04-12 07:49:12 +00:00
|
|
|
match i:
|
|
|
|
case Opcode.IF(t):
|
2022-04-21 06:17:59 +00:00
|
|
|
d = t + start
|
|
|
|
assert start <= d < end
|
|
|
|
return Opcode.IF(d)
|
|
|
|
|
2022-06-11 06:28:24 +00:00
|
|
|
case Opcode.GOTO(t):
|
2022-04-21 06:17:59 +00:00
|
|
|
d = t + start
|
|
|
|
assert start <= d < end
|
|
|
|
return Opcode.GOTO(d)
|
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
case _:
|
|
|
|
return i
|
|
|
|
|
|
|
|
def define_function(self, name, opcodes):
|
|
|
|
# FIXME: This is way way WAAAAAAY too minimal. Lots of other stuff goes on a "function."
|
|
|
|
# For instance how to install handlers?
|
|
|
|
# How to consume capabilities?
|
|
|
|
|
|
|
|
try:
|
2022-04-21 06:17:59 +00:00
|
|
|
sig = FunctionRef.parse(name)
|
2022-04-12 07:49:12 +00:00
|
|
|
assert sig.name
|
|
|
|
except:
|
|
|
|
raise ValueError("Illegal name provided")
|
|
|
|
|
|
|
|
start = len(self.opcodes)
|
|
|
|
self.functions[name] = start
|
|
|
|
for op in opcodes:
|
2022-04-21 06:17:59 +00:00
|
|
|
self.opcodes.append(self.translate(start, start + len(opcodes), op))
|
2022-04-12 07:49:12 +00:00
|
|
|
return name
|
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
def define_type(self, name, signature):
|
2022-04-12 07:49:12 +00:00
|
|
|
# FIXME: What in TARNATION is this going to do
|
|
|
|
pass
|
2022-06-01 05:08:29 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
b = []
|
|
|
|
marks = {v: k for k, v in self.functions.items()}
|
|
|
|
for i, o in zip(range(1<<64), self.opcodes):
|
|
|
|
if(i in marks):
|
|
|
|
b.append(f"{marks[i]}:")
|
|
|
|
b.append(f"{i: >10}: {o}")
|
|
|
|
return "\n".join(b)
|