35 log(std::format(
"VM::{}(): normal instance created {:#x}\n", __func__, (uintptr_t)
this));
42 log(std::format(
"VM::{}(): fast instance created {:#x}\n", __func__, (uintptr_t)
this));
47 explicit VM(
const OpCode &op,
const int &operand1 = 0,
const int &operand2 = 0,
const int &operand3 = 0,
48 const int &operand4 = 0,
const int &operand5 = 0)
51 log(std::format(
"VM::{}(): operation instance created {:#x}\n", __func__, (uintptr_t)
this));
54 operation(op, operand1, operand2, operand3, operand4, operand5);
61 log(std::format(
"VM::{}(): deconstructed {:#x}\n", __func__, (uintptr_t)
this));
66 inline void initFFI(
const std::filesystem::path &path) {
68 ffi = std::make_unique<FFI>(path,
this);
74 class Halt :
public std::exception
77 const char *
what() const noexcept
override
93 using ImportHandler = std::function<void(
const std::filesystem::path &path)>;
171 #define REGISTER1 VM::Register::r0
172 #define REGISTER2 VM::Register::r1
173 #define REGISTER3 VM::Register::r2
177 inline Value __fastcall
operation(
const OpCode &op,
const int &operand1 = 0,
const int &operand2 = 0,
178 const int &operand3 = 0,
const int &operand4 = 0,
const int &operand5 = 0);
181 inline Value
operation(
const OpCode &op,
const int &operand1 = 0,
const int &operand2 = 0,
const int &operand3 = 0,
182 const int &operand4 = 0,
const int &operand5 = 0);
185 void push(
const Value &value);
197 void reset(
const bool &resetStack =
true,
const bool &resetFunctions =
true,
const bool &resetVariables =
true);
206 void log(
const Value &msg);
209 void logerr(
const Value &msg);
230 (
setRegister(regIndex++, std::forward<Args>(args)), ...);
242 Value arr[] = {
Value(std::forward<Args>(args))...};
243 for (
Value& v : arr | std::views::reverse)
283#define OPS_ARE_INCLUDED
Expanded opcode set for Phasor VM.
Throws when the HALT opcode is reached.
const char * what() const noexcept override
ImportHandler importHandler
Import handler for loading modules.
std::vector< Value > variables
Variable storage indexed by variable index.
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::vector< Value > stack
Stack for function calls.
const Bytecode * m_bytecode
Bytecode to execute.
VM(const Bytecode &bytecode)
std::vector< int > callStack
Call stack for function calls.
Value getVariable(size_t index)
Get a variable from the VM.
VM(const OpCode &op, const int &operand1=0, const int &operand2=0, const int &operand3=0, const int &operand4=0, const int &operand5=0)
std::map< std::string, NativeFunction > nativeFunctions
Native function registry.
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.
size_t getVariableCount()
Get the number of variables in the VM.
std::string getBytecodeInformation()
Get bytecode information for debugging.
void log(const Value &msg)
Log a Value to stdout.
std::array< Value, MAX_REGISTERS > registers
Virtual registers for register-based operations (v2.0).
size_t addVariable(const Value &value)
Add a variable to the VM.
void flush()
Flush stdout.
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.
void setVariable(size_t index, const Value &value)
Set a variable in the VM.
void initFFI(const std::filesystem::path &path)
void setRegister(uint8_t index, const Value &value)
Set a register value.
Value stackRun(OpCode opcode, Args &&... args)
Run an opcode with values pushed to the stack.
void freeRegister(uint8_t index)
Free a register (reset to null).
std::unique_ptr< FFI > ffi
FFI.
Value operation(const OpCode &op, const int &operand1=0, const int &operand2=0, const int &operand3=0, const int &operand4=0, const int &operand5=0)
Execute a single operation.
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.
int run(const Bytecode &bytecode)
Run the virtual machine Exits -1 on uncaught exception.
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.