![]() |
Phasor 3.1.1
Stack VM based Programming Language
|
Virtual Machine. More...
#include <VM.hpp>
Classes | |
| class | Halt |
| Throws when the HALT opcode is reached. More... | |
Public Types | |
| enum | Register : uint8_t { r0 , r1 , r2 , r3 , r4 , r5 , r6 , r7 , r8 , r9 , r10 , r11 , r12 , r13 , r14 , r15 , r16 , r17 , r18 , r19 , r20 , r21 , r22 , r23 , r24 , r25 , r26 , r27 , r28 , r29 , r30 , r31 } |
| Enum for registers. More... | |
| using | NativeFunction = std::function<Value(const std::vector<Value> &args, VM *vm)> |
| Native function signature. | |
| using | ImportHandler = std::function<void(const std::filesystem::path &path)> |
Public Member Functions | |
| VM () | |
| VM (const Bytecode &bytecode) | |
| VM (const OpCode &op, const int &operand1=0, const int &operand2=0, const int &operand3=0, const int &operand4=0, const int &operand5=0) | |
| ~VM () | |
| void | initFFI (const std::filesystem::path &path) |
| int | run (const Bytecode &bytecode) |
| Run the virtual machine Exits -1 on uncaught exception. | |
| void | registerNativeFunction (const std::string &name, NativeFunction fn) |
| Register a native function. | |
| void | setImportHandler (const ImportHandler &handler) |
| Set the import handler for importing modules. | |
| void | freeVariable (size_t index) |
| Free a variable in the VM. | |
| size_t | addVariable (const Value &value) |
| Add a variable to the VM. | |
| void | setVariable (size_t index, const Value &value) |
| Set a variable in the VM. | |
| Value | getVariable (size_t index) |
| Get a variable from the VM. | |
| size_t | getVariableCount () |
| Get the number of variables in the VM. | |
| void | setRegister (uint8_t index, const Value &value) |
| Set a register value. | |
| void | freeRegister (uint8_t index) |
| Free a register (reset to null). | |
| Value | getRegister (uint8_t index) |
| Get a register value. | |
| size_t | getRegisterCount () |
| Get the total number of registers. | |
| 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. | |
| void | push (const Value &value) |
| Push a value onto the stack. | |
| Value | pop () |
| Pop a value from the stack. | |
| Value | peek () |
| Peek at the top value on the stack. | |
| void | cleanup () |
| Clean up the virtual machine. | |
| void | reset (const bool &resetStack=true, const bool &resetFunctions=true, const bool &resetVariables=true) |
| Reset the virtual machine. | |
| std::string | getInformation () |
| Get VM information for debugging. | |
| std::string | getBytecodeInformation () |
| Get bytecode information for debugging. | |
| void | log (const Value &msg) |
| Log a Value to stdout. | |
| void | logerr (const Value &msg) |
| Log a Value to stderr. | |
| void | flush () |
| Flush stdout. | |
| void | flusherr () |
| Flush stderr. | |
| void | setStatus (int newStatus) |
| Set VM exit code. | |
| template<typename... Args> | |
| Value | regRun (OpCode opcode, Args &&...args) |
| Run an opcode with arguments pre-loaded into registers. | |
| template<typename... Args> | |
| Value | stackRun (OpCode opcode, Args &&... args) |
| Run an opcode with values pushed to the stack. | |
Private Attributes | |
| std::unique_ptr< FFI > | ffi |
| FFI. | |
| int | status = 0 |
| Exit code. | |
| ImportHandler | importHandler |
| Import handler for loading modules. | |
| std::array< Value, MAX_REGISTERS > | registers |
| Virtual registers for register-based operations (v2.0). | |
| std::vector< Value > | stack |
| Stack for function calls. | |
| std::vector< int > | callStack |
| Call stack for function calls. | |
| std::vector< Value > | variables |
| Variable storage indexed by variable index. | |
| const Bytecode * | m_bytecode {} |
| Bytecode to execute. | |
| size_t | pc = 0 |
| Program counter. | |
| std::map< std::string, NativeFunction > | nativeFunctions |
| Native function registry. | |
| using Phasor::VM::ImportHandler = std::function<void(const std::filesystem::path &path)> |
| using Phasor::VM::NativeFunction = std::function<Value(const std::vector<Value> &args, VM *vm)> |
| enum Phasor::VM::Register : uint8_t |
|
inlineexplicit |
|
inlineexplicit |
|
inlineexplicit |
|
inline |
| size_t Phasor::VM::addVariable | ( | const Value & | value | ) |
Add a variable to the VM.
| value | The value to add |
Definition at line 8 of file Variables.cpp.
| void Phasor::VM::cleanup | ( | ) |
Clean up the virtual machine.
Definition at line 76 of file Utility.cpp.
| void Phasor::VM::flush | ( | ) |
Flush stdout.
Definition at line 188 of file Utility.cpp.
| void Phasor::VM::flusherr | ( | ) |
Flush stderr.
Definition at line 193 of file Utility.cpp.
| void Phasor::VM::freeRegister | ( | uint8_t | index | ) |
Free a register (reset to null).
| index | Register index to free |
Definition at line 17 of file Register.cpp.
| void Phasor::VM::freeVariable | ( | size_t | index | ) |
Free a variable in the VM.
Definition at line 18 of file Variables.cpp.
| std::string Phasor::VM::getBytecodeInformation | ( | ) |
Get bytecode information for debugging.
Definition at line 140 of file Utility.cpp.
| std::string Phasor::VM::getInformation | ( | ) |
Get VM information for debugging.
Definition at line 111 of file Utility.cpp.
| Value Phasor::VM::getRegister | ( | uint8_t | index | ) |
Get a register value.
| index | Register index |
Definition at line 26 of file Register.cpp.
| size_t Phasor::VM::getRegisterCount | ( | ) |
Get the total number of registers.
Definition at line 35 of file Register.cpp.
| Value Phasor::VM::getVariable | ( | size_t | index | ) |
Get a variable from the VM.
Definition at line 43 of file Variables.cpp.
| size_t Phasor::VM::getVariableCount | ( | ) |
Get the number of variables in the VM.
Definition at line 60 of file Variables.cpp.
|
inline |
| void Phasor::VM::log | ( | const Value & | msg | ) |
Log a Value to stdout.
Definition at line 176 of file Utility.cpp.
| void Phasor::VM::logerr | ( | const Value & | msg | ) |
Log a Value to stderr.
Definition at line 182 of file Utility.cpp.
|
inline |
Execute a single operation.
Definition at line 7 of file Operations.cc.inl.
| Value Phasor::VM::peek | ( | ) |
| Value Phasor::VM::pop | ( | ) |
| void Phasor::VM::push | ( | const Value & | value | ) |
| void Phasor::VM::registerNativeFunction | ( | const std::string & | name, |
| NativeFunction | fn ) |
Register a native function.
Definition at line 5 of file Native.cpp.
Run an opcode with arguments pre-loaded into registers.
| Args | Argument types |
| opcode | Opcode to run |
| args | Arguments to load into registers |
Definition at line 227 of file VM.hpp.
| void Phasor::VM::reset | ( | const bool & | resetStack = true, |
| const bool & | resetFunctions = true, | ||
| const bool & | resetVariables = true ) |
Reset the virtual machine.
Definition at line 88 of file Utility.cpp.
| int Phasor::VM::run | ( | const Bytecode & | bytecode | ) |
Run the virtual machine Exits -1 on uncaught exception.
Definition at line 13 of file Utility.cpp.
| void Phasor::VM::setImportHandler | ( | const ImportHandler & | handler | ) |
Set the import handler for importing modules.
Definition at line 67 of file Utility.cpp.
| void Phasor::VM::setRegister | ( | uint8_t | index, |
| const Value & | value ) |
Set a register value.
Definition at line 8 of file Register.cpp.
|
inline |
| void Phasor::VM::setVariable | ( | size_t | index, |
| const Value & | value ) |
Set a variable in the VM.
| index | The index of the variable |
| value | The value to set |
Definition at line 30 of file Variables.cpp.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |