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-01 05:54:11 +00:00
|
|
|
class TRUE(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
"""() -> (bool)
|
|
|
|
Push the constant TRUE onto the stack.
|
|
|
|
"""
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class FALSE(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
"""() -> (bool)
|
|
|
|
Push the constant FALSE onto the stack.
|
|
|
|
"""
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class IF(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
"""(bool) -> ()
|
|
|
|
Branch to another point if the top item of the stack is TRUE.
|
|
|
|
Otherwise fall through.
|
|
|
|
"""
|
|
|
|
|
|
|
|
target: int
|
|
|
|
|
|
|
|
# not, and, or, xor etc. can all be functions given if.
|
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
####################################################################################################
|
|
|
|
# Stack manipulation
|
|
|
|
####################################################################################################
|
2022-06-01 05:54:11 +00:00
|
|
|
class DUP(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
"""(A, B, ...) -> (A, B, ...)
|
|
|
|
Duplicate the top N items of the stack.
|
|
|
|
"""
|
|
|
|
|
|
|
|
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, ...)
|
|
|
|
Rotate the top N elements of the stack.
|
|
|
|
"""
|
|
|
|
|
|
|
|
nargs: int = 2
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class DROP(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
"""(*) -> ()
|
|
|
|
Drop the top N items of the stack.
|
|
|
|
"""
|
|
|
|
|
|
|
|
nargs: int = 1
|
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
####################################################################################################
|
|
|
|
# Functional abstraction
|
|
|
|
####################################################################################################
|
2022-06-01 05:54:11 +00:00
|
|
|
class CALLS(t.NamedTuple):
|
2022-04-21 06:17:59 +00:00
|
|
|
"""(... A) -> (... B)
|
|
|
|
Call [static]
|
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
Branch to `target` pushing the current point onto the call stack.
|
|
|
|
The callee will see a stack containg only the provided `nargs`.
|
|
|
|
A subsequent RETURN will return execution to the next point.
|
2022-04-15 06:31:58 +00:00
|
|
|
|
|
|
|
Executing a `CALL` pushes the name and module path of the current function.
|
2022-04-21 06:17:59 +00:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
CALLS is equvalent to `FUNREF; CALLF`
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
funref: str
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class RETURN(t.NamedTuple):
|
2022-04-21 06:17:59 +00:00
|
|
|
"""(... A) -> ()
|
2022-04-12 07:49:12 +00:00
|
|
|
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.
|
2022-04-15 06:31:58 +00:00
|
|
|
|
|
|
|
Executing a `RETURN` pops (restores) the name and module path of the current function back to that of the caller.
|
|
|
|
|
2022-04-12 07:49:12 +00:00
|
|
|
If the call stack is empty, `RETURN` will exit the interpreter.
|
|
|
|
"""
|
|
|
|
|
|
|
|
nargs: int
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class GOTO(t.NamedTuple):
|
2022-04-12 07:49:12 +00:00
|
|
|
"""() -> ()
|
|
|
|
Branch to another point within the same bytecode segment.
|
2022-04-15 06:31:58 +00:00
|
|
|
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.
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
target: int
|
|
|
|
anywhere: bool = False
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class FUNREF(t.NamedTuple):
|
2022-04-21 06:17:59 +00:00
|
|
|
"""() -> (`FUNREF<... A to ... B>`)
|
|
|
|
Construct a reference to a static codepoint.
|
|
|
|
"""
|
|
|
|
|
|
|
|
funref: str
|
2022-04-15 06:31:58 +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)
|
|
|
|
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-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
2022-04-21 06:17:59 +00:00
|
|
|
nargs: int = 0
|
|
|
|
|
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>`)
|
|
|
|
Construct a closure over the function reference at the top of the stack.
|
|
|
|
This may produce nullary closures.
|
|
|
|
"""
|
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>`)
|
|
|
|
Further close over the closure at the top of the stack.
|
|
|
|
This may produce nullary closures.
|
|
|
|
"""
|
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-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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
nargs: int = 0
|
|
|
|
|
2022-05-31 15:36:47 +00:00
|
|
|
####################################################################################################
|
|
|
|
# Structures
|
|
|
|
####################################################################################################
|
2022-06-01 05:54:11 +00:00
|
|
|
class STRUCT(t.NamedTuple):
|
2022-05-31 15:36:47 +00:00
|
|
|
"""(*) -> (T)
|
|
|
|
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`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
structref: str
|
|
|
|
nargs: int
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class FLOAD(t.NamedTuple):
|
2022-05-31 15:36:47 +00:00
|
|
|
"""(A) -> (B)
|
|
|
|
Consume the struct reference at the top of the stack, producing the value of the referenced field.
|
|
|
|
"""
|
|
|
|
|
|
|
|
fieldref: str
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class FSTORE(t.NamedTuple):
|
2022-05-31 15:36:47 +00:00
|
|
|
"""(A) -> (B)
|
|
|
|
Consume the struct reference at the top of the stack, producing the value of the referenced field.
|
|
|
|
"""
|
|
|
|
|
|
|
|
fieldref: str
|
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>)
|
|
|
|
Consume the top N items of the stack, producing an array of the type `typeref`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
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-04-21 06:17:59 +00:00
|
|
|
"""(NAT, ARRAY<T>) -> (T)
|
|
|
|
Consume a reference to an array and an index, producing the value at that index.
|
|
|
|
FIXME: Or a fault/signal.
|
2022-04-12 07:49:12 +00:00
|
|
|
"""
|
|
|
|
|
2022-06-01 05:54:11 +00:00
|
|
|
class ASTORE(t.NamedTuple):
|
2022-04-21 06:17:59 +00:00
|
|
|
"""(T, NAT, ARRAY<T>) -> (ARRAY<T>)
|
|
|
|
Consume a value T, storing it at an index in the given array.
|
|
|
|
Produces the updated array as the top of stack.
|
|
|
|
"""
|
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-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-04-12 07:49:12 +00:00
|
|
|
case Opcode.GOTO(t, anywhere=False):
|
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)
|