15#include <memory_resource>
37 VM(
const OpCode &op,
const int &operand1 = 0,
const int &operand2 = 0,
const int &operand3 = 0);
41 void initFFI(
const std::filesystem::path &path);
48 class Halt :
public std::exception
51 const char *
what() const noexcept
override
59 int run(
const Bytecode &bytecode,
const size_t startPC = 0);
70 using ImportHandler = std::function<void(
const std::filesystem::path &path)>;
151#define REGISTER1 VM::Register::r0
152#define REGISTER2 VM::Register::r1
153#define REGISTER3 VM::Register::r2
157 Value __fastcall
operation(
const OpCode &op,
const int &operand1 = 0,
const int &operand2 = 0,
158 const int &operand3 = 0);
161 Value
operation(
const OpCode &op,
const int &operand1 = 0,
const int &operand2 = 0,
const int &operand3 = 0);
164 void push(
const Value &value);
176 void reset(
const bool &resetStack =
true,
const bool &resetFunctions =
true,
const bool &resetVariables =
true);
185 void log(
const Value &
msg);
211 (
setRegister(regIndex++, std::forward<Args>(args)), ...);
225 Value arr[] = {
Value(std::forward<Args>(args))...};
226 for (
Value &v : arr | std::views::reverse)
Expanded opcode set for Phasor VM.
Throws when the HALT opcode is reached.
const char * what() const noexcept override
std::array< Value, MAX_REGISTERS > registers
Virtual registers for register-based operations (v2.0).
ImportHandler importHandler
Import handler for loading modules.
Value operation(const OpCode &op, const int &operand1=0, const int &operand2=0, const int &operand3=0)
Execute a single operation.
std::vector< Value > variables
Variable storage indexed by variable index, or simply: the managed heap.
std::function< Value(const std::vector< Value > &args, VM *vm)> NativeFunction
Native function signature.
Value pop()
Pop a value from the stack.
void setImportHandler(const ImportHandler &handler)
Set the import handler for importing modules.
std::function< void(const std::filesystem::path &path)> ImportHandler
void logerr(const Value &msg)
Log a Value to stderr.
std::string getVersion()
Get Phasor VM version.
std::unique_ptr< FFI > ffi
FFI.
bool isDirectCall
is a direct call to a function
Value getVariable(size_t index)
Get a variable from the VM.
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Value getRegister(uint8_t index)
Get a register value.
void setStatus(int newStatus)
Set VM exit code.
void freeVariableByName(const std::string &name)
Free a variable by name in the VM.
size_t getVariableCount()
Get the number of variables in the VM.
std::vector< int > callStack
Call stack for function calls.
std::string getBytecodeInformation()
Get bytecode information for debugging.
void log(const Value &msg)
Log a Value to stdout.
size_t addVariable(const Value &value)
Add a variable to the VM.
void flush()
Flush stdout.
std::pmr::vector< Value > stack
void cleanup()
Clean up the virtual machine.
Register
Enum for registers.
size_t pc
Program counter.
void push(const Value &value)
Push a value onto the stack.
Value stackRun(OpCode opcode, Args &&...args)
Run an opcode with values pushed to the stack.
void setVariable(size_t index, const Value &value)
Set a variable in the VM.
int run(const Bytecode &bytecode, const size_t startPC=0)
Run the virtual machine Exits -1 on uncaught exception.
void initFFI(const std::filesystem::path &path)
Initialize the FFI plugins.
void setRegister(uint8_t index, const Value &value)
Set a register value.
std::map< std::string, NativeFunction > nativeFunctions
Native function registry.
Value runFunction(const std::string &name, const Bytecode &bytecode)
Run a function from bytecode on the virtual machine.
void freeRegister(uint8_t index)
Free a register (reset to null).
const Bytecode * m_bytecode
Bytecode to execute.
void setup(const Bytecode &bc, const size_t initialPC)
std::pmr::monotonic_buffer_resource stack_pool
Stack.
Value regRun(OpCode opcode, Args &&...args)
Run an opcode with arguments pre-loaded into registers.
Value peek()
Peek at the top value on the stack.
void flusherr()
Flush stderr.
size_t getRegisterCount()
Get the total number of registers.
void reset(const bool &resetStack=true, const bool &resetFunctions=true, const bool &resetVariables=true)
Reset the virtual machine.
void freeVariable(size_t index)
Free a variable in the VM.
std::string getInformation()
Get VM information for debugging.
A value in the Phasor VM.
The Phasor Programming Language and Runtime.
Complete bytecode structure.