Phasor 2.2.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
Phasor::VM Class Reference

Virtual Machine. More...

#include <VM.hpp>

Collaboration diagram for Phasor::VM:
[legend]

Public Types

enum  Register {
  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
}
 Enum for lower 26 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

void run (const Bytecode &bytecode)
 Run the virtual machine.
void registerNativeFunction (const std::string &name, NativeFunction fn)
 Register a native function.
void setImportHandler (ImportHandler handler)
void freeVariable (const size_t index)
 Free a variable in the VM.
size_t addVariable (const Value &value)
 Add a variable to the VM.
void setVariable (const size_t index, const Value &value)
 Set a variable in the VM.
Value getVariable (const 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 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.
void log (const Value &msg)
 Use the VM's logging via print opcode.
void logerr (const Value &msg)

Private Attributes

ImportHandler importHandler
 Import handler for loading modules.
std::array< Value, 256 > registers
 Virtual registers for register-based operations (v2.0).
std::vector< Valuestack
 Stack for function calls.
std::vector< int > callStack
 Call stack for function calls.
std::vector< Valuevariables
 Variable storage indexed by variable index.
const Bytecodebytecode
 Bytecode to execute.
size_t pc = 0
 Program counter.
std::map< std::string, NativeFunctionnativeFunctions
 Native function registry.

Detailed Description

Virtual Machine.

Definition at line 17 of file VM.hpp.

Member Typedef Documentation

◆ ImportHandler

using Phasor::VM::ImportHandler = std::function<void(const std::filesystem::path &path)>

Definition at line 29 of file VM.hpp.

◆ NativeFunction

using Phasor::VM::NativeFunction = std::function<Value(const std::vector<Value> &args, VM *vm)>

Native function signature.

Definition at line 24 of file VM.hpp.

Member Enumeration Documentation

◆ Register

Enum for lower 26 registers.

Enumerator
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 

Definition at line 70 of file VM.hpp.

Member Function Documentation

◆ addVariable()

size_t Phasor::VM::addVariable ( const Value & value)

Add a variable to the VM.

Parameters
valueThe value to add
Returns
The index of the variable

Definition at line 923 of file VM.cpp.

Here is the caller graph for this function:

◆ freeRegister()

void Phasor::VM::freeRegister ( uint8_t index)

Free a register (reset to null).

Parameters
indexRegister index to free

Definition at line 965 of file VM.cpp.

◆ freeVariable()

void Phasor::VM::freeVariable ( const size_t index)

Free a variable in the VM.

Definition at line 929 of file VM.cpp.

◆ getInformation()

std::string Phasor::VM::getInformation ( )

Get VM information for debugging.

Definition at line 980 of file VM.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getRegister()

Value Phasor::VM::getRegister ( uint8_t index)

Get a register value.

Parameters
indexRegister index
Returns
Value in the register

Definition at line 970 of file VM.cpp.

Here is the caller graph for this function:

◆ getRegisterCount()

size_t Phasor::VM::getRegisterCount ( )

Get the total number of registers.

Returns
Number of registers

Definition at line 975 of file VM.cpp.

◆ getVariable()

Value Phasor::VM::getVariable ( const size_t index)

Get a variable from the VM.

Definition at line 946 of file VM.cpp.

Here is the caller graph for this function:

◆ getVariableCount()

size_t Phasor::VM::getVariableCount ( )

Get the number of variables in the VM.

Definition at line 955 of file VM.cpp.

◆ log()

void Phasor::VM::log ( const Value & msg)

Use the VM's logging via print opcode.

Definition at line 1000 of file VM.cpp.

Here is the call graph for this function:

◆ logerr()

void Phasor::VM::logerr ( const Value & msg)

Definition at line 1006 of file VM.cpp.

Here is the call graph for this function:

◆ operation()

Value Phasor::VM::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.

Definition at line 26 of file VM.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ peek()

Value Phasor::VM::peek ( )

Peek at the top value on the stack.

Definition at line 895 of file VM.cpp.

Here is the caller graph for this function:

◆ pop()

Value Phasor::VM::pop ( )

Pop a value from the stack.

Definition at line 884 of file VM.cpp.

Here is the caller graph for this function:

◆ push()

void Phasor::VM::push ( const Value & value)

Push a value onto the stack.

Definition at line 879 of file VM.cpp.

Here is the caller graph for this function:

◆ registerNativeFunction()

void Phasor::VM::registerNativeFunction ( const std::string & name,
NativeFunction fn )

Register a native function.

Definition at line 869 of file VM.cpp.

Here is the caller graph for this function:

◆ reset()

void Phasor::VM::reset ( const bool & resetStack = true,
const bool & resetFunctions = true,
const bool & resetVariables = true )

Reset the virtual machine.

Definition at line 904 of file VM.cpp.

Here is the caller graph for this function:

◆ run()

void Phasor::VM::run ( const Bytecode & bytecode)

Run the virtual machine.

Definition at line 9 of file VM.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setImportHandler()

void Phasor::VM::setImportHandler ( ImportHandler handler)

Definition at line 874 of file VM.cpp.

Here is the caller graph for this function:

◆ setRegister()

void Phasor::VM::setRegister ( uint8_t index,
const Value & value )

Set a register value.

Parameters
indexRegister index
valueValue to set

Definition at line 960 of file VM.cpp.

Here is the caller graph for this function:

◆ setVariable()

void Phasor::VM::setVariable ( const size_t index,
const Value & value )

Set a variable in the VM.

Parameters
indexThe index of the variable
valueThe value to set

Definition at line 937 of file VM.cpp.

Member Data Documentation

◆ bytecode

const Bytecode* Phasor::VM::bytecode
private

Bytecode to execute.

Definition at line 140 of file VM.hpp.

◆ callStack

std::vector<int> Phasor::VM::callStack
private

Call stack for function calls.

Definition at line 134 of file VM.hpp.

◆ importHandler

ImportHandler Phasor::VM::importHandler
private

Import handler for loading modules.

Definition at line 125 of file VM.hpp.

◆ nativeFunctions

std::map<std::string, NativeFunction> Phasor::VM::nativeFunctions
private

Native function registry.

Definition at line 146 of file VM.hpp.

◆ pc

size_t Phasor::VM::pc = 0
private

Program counter.

Definition at line 143 of file VM.hpp.

◆ registers

std::array<Value, 256> Phasor::VM::registers
private

Virtual registers for register-based operations (v2.0).

Definition at line 128 of file VM.hpp.

◆ stack

std::vector<Value> Phasor::VM::stack
private

Stack for function calls.

Definition at line 131 of file VM.hpp.

◆ variables

std::vector<Value> Phasor::VM::variables
private

Variable storage indexed by variable index.

Definition at line 137 of file VM.hpp.


The documentation for this class was generated from the following files: