Phasor 3.1.1
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]

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< FFIffi
 FFI.
int status = 0
 Exit code.
ImportHandler importHandler
 Import handler for loading modules.
std::array< Value, MAX_REGISTERSregisters
 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 Bytecodem_bytecode {}
 Bytecode to execute.
size_t pc = 0
 Program counter.
std::map< std::string, NativeFunctionnativeFunctions
 Native function registry.

Detailed Description

Virtual Machine.

Definition at line 29 of file VM.hpp.

Member Typedef Documentation

◆ ImportHandler

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

Definition at line 93 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 88 of file VM.hpp.

Member Enumeration Documentation

◆ Register

enum Phasor::VM::Register : uint8_t

Enum for 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 
r26 
r27 
r28 
r29 
r30 
r31 

Definition at line 135 of file VM.hpp.

Constructor & Destructor Documentation

◆ VM() [1/3]

Phasor::VM::VM ( )
inlineexplicit

Definition at line 32 of file VM.hpp.

Here is the call graph for this function:

◆ VM() [2/3]

Phasor::VM::VM ( const Bytecode & bytecode)
inlineexplicit

Definition at line 39 of file VM.hpp.

Here is the call graph for this function:

◆ VM() [3/3]

Phasor::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 )
inlineexplicit

Definition at line 47 of file VM.hpp.

Here is the call graph for this function:

◆ ~VM()

Phasor::VM::~VM ( )
inline

Definition at line 57 of file VM.hpp.

Here is the call graph for this function:

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 8 of file Variables.cpp.

Here is the call graph for this function:

◆ cleanup()

void Phasor::VM::cleanup ( )

Clean up the virtual machine.

Definition at line 76 of file Utility.cpp.

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

◆ flush()

void Phasor::VM::flush ( )

Flush stdout.

Definition at line 188 of file Utility.cpp.

Here is the caller graph for this function:

◆ flusherr()

void Phasor::VM::flusherr ( )

Flush stderr.

Definition at line 193 of file Utility.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 17 of file Register.cpp.

Here is the call graph for this function:

◆ freeVariable()

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

Free a variable in the VM.

Definition at line 18 of file Variables.cpp.

Here is the call graph for this function:

◆ getBytecodeInformation()

std::string Phasor::VM::getBytecodeInformation ( )

Get bytecode information for debugging.

Definition at line 140 of file Utility.cpp.

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

◆ getInformation()

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

Get VM information for debugging.

Definition at line 111 of file Utility.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 26 of file Register.cpp.

Here is the call graph for this function:

◆ getRegisterCount()

size_t Phasor::VM::getRegisterCount ( )

Get the total number of registers.

Returns
Number of registers

Definition at line 35 of file Register.cpp.

Here is the call graph for this function:

◆ getVariable()

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

Get a variable from the VM.

Definition at line 43 of file Variables.cpp.

Here is the call graph for this function:

◆ getVariableCount()

size_t Phasor::VM::getVariableCount ( )

Get the number of variables in the VM.

Definition at line 60 of file Variables.cpp.

Here is the call graph for this function:

◆ initFFI()

void Phasor::VM::initFFI ( const std::filesystem::path & path)
inline

Definition at line 66 of file VM.hpp.

Here is the caller graph for this function:

◆ log()

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

Log a Value to stdout.

Definition at line 176 of file Utility.cpp.

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

◆ logerr()

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

Log a Value to stderr.

Definition at line 182 of file Utility.cpp.

Here is the call graph for this function:
Here is the caller 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 )
inline

Execute a single operation.

Definition at line 7 of file Operations.cc.inl.

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 38 of file Stack.cpp.

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

◆ pop()

Value Phasor::VM::pop ( )

Pop a value from the stack.

Definition at line 17 of file Stack.cpp.

Here is the call graph for this function:
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 8 of file Stack.cpp.

Here is the call graph for this function:
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 5 of file Native.cpp.

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

◆ regRun()

template<typename... Args>
Value Phasor::VM::regRun ( OpCode opcode,
Args &&... args )
inline

Run an opcode with arguments pre-loaded into registers.

Template Parameters
ArgsArgument types
Parameters
opcodeOpcode to run
argsArguments to load into registers
Returns
Return value of the operation

Definition at line 227 of file VM.hpp.

Here is the call graph for this function:
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 88 of file Utility.cpp.

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

◆ run()

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

Run the virtual machine Exits -1 on uncaught exception.

Definition at line 13 of file Utility.cpp.

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

◆ setImportHandler()

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

Set the import handler for importing modules.

Definition at line 67 of file Utility.cpp.

Here is the call graph for this function:
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 8 of file Register.cpp.

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

◆ setStatus()

void Phasor::VM::setStatus ( int newStatus)
inline

Set VM exit code.

Definition at line 218 of file VM.hpp.

Here is the caller graph for this function:

◆ setVariable()

void Phasor::VM::setVariable ( 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 30 of file Variables.cpp.

Here is the call graph for this function:

◆ stackRun()

template<typename... Args>
Value Phasor::VM::stackRun ( OpCode opcode,
Args &&... args )
inline

Run an opcode with values pushed to the stack.

Template Parameters
ArgsArgument types
Parameters
opcodeOpcode to run
argsArguments to push to the stack
Returns
Value returned to stack

Definition at line 241 of file VM.hpp.

Here is the call graph for this function:

Member Data Documentation

◆ callStack

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

Call stack for function calls.

Definition at line 267 of file VM.hpp.

◆ ffi

std::unique_ptr<FFI> Phasor::VM::ffi
private

FFI.

Definition at line 252 of file VM.hpp.

◆ importHandler

ImportHandler Phasor::VM::importHandler
private

Import handler for loading modules.

Definition at line 258 of file VM.hpp.

◆ m_bytecode

const Bytecode* Phasor::VM::m_bytecode {}
private

Bytecode to execute.

Definition at line 273 of file VM.hpp.

◆ nativeFunctions

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

Native function registry.

Definition at line 279 of file VM.hpp.

◆ pc

size_t Phasor::VM::pc = 0
private

Program counter.

Definition at line 276 of file VM.hpp.

◆ registers

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

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

Definition at line 261 of file VM.hpp.

◆ stack

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

Stack for function calls.

Definition at line 264 of file VM.hpp.

◆ status

int Phasor::VM::status = 0
private

Exit code.

Definition at line 255 of file VM.hpp.

◆ variables

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

Variable storage indexed by variable index.

Definition at line 270 of file VM.hpp.


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