Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Compiler_Bytecode_VM Class Reference

Small portable VM for executing one Compiler_Bytecode_Module. More...

#include <Bytecode_Interpreter.H>

Collaboration diagram for Aleph::Compiler_Bytecode_VM:
[legend]

Classes

struct  Frame
 

Public Member Functions

 Compiler_Bytecode_VM (const Compiler_Bytecode_Module &module)
 Builds a VM over one immutable bytecode module.
 
void reset ()
 Resets globals to Invalid and clears any pending call stack.
 
const Compiler_Bytecode_Module & module () const noexcept
 Returns the immutable bytecode module executed by this VM.
 
const DynArray< Compiler_Bytecode_Value > & global_values () const noexcept
 Returns the current global slot values.
 
const Compiler_Bytecode_Value * global_slot (const Compiler_IR_Global_Slot_Id slot_id) const noexcept
 Returns one global slot value if the slot id is valid.
 
size_t call_stack_depth () const noexcept
 Returns the current dynamic call-stack depth.
 
bool set_global (const Compiler_IR_Global_Slot_Id slot_id, const Compiler_Bytecode_Value &value)
 Writes one global slot before or after execution.
 
Compiler_Bytecode_Execution_Result run_top_level (const size_t max_steps=0)
 Executes the module top-level body.
 
Compiler_Bytecode_Execution_Result call_function (const Compiler_Bytecode_Function_Id function_id, const DynArray< Compiler_Bytecode_Value > &arguments, const size_t max_steps=0)
 Executes one bytecode function directly.
 
std::string dump_globals () const
 Returns a deterministic dump of the current global state.
 

Private Member Functions

const Frame & current_frame () const
 
Frame & current_frame ()
 
Compiler_Bytecode_Execution_Result make_error (const std::string &code, const std::string &message, const size_t executed_instructions, const Frame *frame=nullptr, const Compiler_Bytecode_Instruction *inst=nullptr) const
 
Compiler_Bytecode_Execution_Result make_validation_error (const Compiler_Bytecode_Validation_Report &report, const size_t executed_instructions=0) const
 
bool module_is_valid (Compiler_Bytecode_Execution_Result &error) const
 
bool push_frame (const Compiler_Bytecode_Function &function, const DynArray< Compiler_Bytecode_Value > &arguments, const bool returns_to_caller, const Compiler_Bytecode_PC caller_resume_pc, const Compiler_Bytecode_Register_Id caller_destination, const size_t executed_instructions, Compiler_Bytecode_Execution_Result &error)
 
bool read_register (const Frame &frame, const Compiler_Bytecode_Register_Id id, const Compiler_Bytecode_Instruction &inst, const size_t executed_instructions, Compiler_Bytecode_Value &value, Compiler_Bytecode_Execution_Result &error) const
 
bool write_register (Frame &frame, const Compiler_Bytecode_Register_Id id, const Compiler_Bytecode_Value &value, const Compiler_Bytecode_Instruction &inst, const size_t executed_instructions, Compiler_Bytecode_Execution_Result &error) const
 
bool read_local (const Frame &frame, const Compiler_IR_Local_Slot_Id id, const Compiler_Bytecode_Instruction &inst, const size_t executed_instructions, Compiler_Bytecode_Value &value, Compiler_Bytecode_Execution_Result &error) const
 
bool write_local (Frame &frame, const Compiler_IR_Local_Slot_Id id, const Compiler_Bytecode_Value &value, const Compiler_Bytecode_Instruction &inst, const size_t executed_instructions, Compiler_Bytecode_Execution_Result &error) const
 
bool read_global (const Frame &frame, const Compiler_IR_Global_Slot_Id id, const Compiler_Bytecode_Instruction &inst, const size_t executed_instructions, Compiler_Bytecode_Value &value, Compiler_Bytecode_Execution_Result &error) const
 
bool write_global (Frame &frame, const Compiler_IR_Global_Slot_Id id, const Compiler_Bytecode_Value &value, const Compiler_Bytecode_Instruction &inst, const size_t executed_instructions, Compiler_Bytecode_Execution_Result &error)
 
bool require_integer (const Compiler_Bytecode_Value &value, const Frame &frame, const Compiler_Bytecode_Instruction &inst, const size_t executed_instructions, const std::string &context, long long &result, Compiler_Bytecode_Execution_Result &error) const
 
bool require_bool (const Compiler_Bytecode_Value &value, const Frame &frame, const Compiler_Bytecode_Instruction &inst, const size_t executed_instructions, const std::string &context, bool &result, Compiler_Bytecode_Execution_Result &error) const
 
bool read_callee_and_arguments (const Frame &frame, const Compiler_Bytecode_Instruction &inst, const size_t executed_instructions, Compiler_Bytecode_Value &callee, DynArray< Compiler_Bytecode_Value > &arguments, Compiler_Bytecode_Execution_Result &error) const
 
bool complete_current_frame (const Compiler_Bytecode_Value &value, const Compiler_Bytecode_Completion_Kind kind, const size_t executed_instructions, Compiler_Bytecode_Execution_Result &completed, Compiler_Bytecode_Execution_Result &error)
 
bool execute_host_call (Frame &frame, const Compiler_Bytecode_Instruction &inst, const Compiler_Bytecode_Value &callee, const DynArray< Compiler_Bytecode_Value > &arguments, const size_t executed_instructions, Compiler_Bytecode_Execution_Result &error)
 
Compiler_Bytecode_Execution_Result execute_loop (const size_t max_steps)
 
Compiler_Bytecode_Execution_Result execute_entry (const Compiler_Bytecode_Function &function, const DynArray< Compiler_Bytecode_Value > &arguments, const size_t max_steps)
 

Private Attributes

const Compiler_Bytecode_Module * bytecode_module = nullptr
 
DynArray< Compiler_Bytecode_Value > globals
 
DynArray< Frame > call_stack
 

Detailed Description

Small portable VM for executing one Compiler_Bytecode_Module.

The VM owns mutable runtime state only:

  • global slot values
  • the active call stack
  • transient register/local arrays per frame

The bytecode module itself remains immutable and can be shared.

Definition at line 483 of file Bytecode_Interpreter.H.

Constructor & Destructor Documentation

◆ Compiler_Bytecode_VM()

Aleph::Compiler_Bytecode_VM::Compiler_Bytecode_VM ( const Compiler_Bytecode_Module &  module)
inlineexplicit

Builds a VM over one immutable bytecode module.

Global slots start as Invalid. Call set_global() before execution if you want to preload constants or host functions.

Definition at line 1610 of file Bytecode_Interpreter.H.

References reset().

Member Function Documentation

◆ call_function()

Compiler_Bytecode_Execution_Result Aleph::Compiler_Bytecode_VM::call_function ( const Compiler_Bytecode_Function_Id  function_id,
const DynArray< Compiler_Bytecode_Value > &  arguments,
const size_t  max_steps = 0 
)
inline

Executes one bytecode function directly.

Parameters
function_idCallee id inside module().functions.
argumentsPositional bytecode values already prepared by the caller.
max_stepsOptional execution limit. Use 0 for no limit.
Returns
Structured completion result.

Definition at line 1699 of file Bytecode_Interpreter.H.

References Aleph::blossom_maximum_cardinality_matching(), bytecode_module, execute_entry(), Aleph::Compiler_Bytecode_Module::functions, and make_error().

◆ call_stack_depth()

size_t Aleph::Compiler_Bytecode_VM::call_stack_depth ( ) const
inlinenoexcept

Returns the current dynamic call-stack depth.

Definition at line 1652 of file Bytecode_Interpreter.H.

References call_stack.

◆ complete_current_frame()

◆ current_frame() [1/2]

Frame & Aleph::Compiler_Bytecode_VM::current_frame ( )
inlineprivate

Definition at line 507 of file Bytecode_Interpreter.H.

References call_stack.

◆ current_frame() [2/2]

const Frame & Aleph::Compiler_Bytecode_VM::current_frame ( ) const
inlineprivate

Definition at line 501 of file Bytecode_Interpreter.H.

References call_stack.

Referenced by complete_current_frame(), and execute_loop().

◆ dump_globals()

std::string Aleph::Compiler_Bytecode_VM::dump_globals ( ) const
inline

Returns a deterministic dump of the current global state.

Definition at line 1718 of file Bytecode_Interpreter.H.

References bytecode_module, Aleph::compiler_dump_bytecode_globals(), and globals.

◆ execute_entry()

◆ execute_host_call()

◆ execute_loop()

Compiler_Bytecode_Execution_Result Aleph::Compiler_Bytecode_VM::execute_loop ( const size_t  max_steps)
inlineprivate

Definition at line 949 of file Bytecode_Interpreter.H.

References Aleph::Amp, Aleph::and, Aleph::AndAnd, Aleph::Bang, Aleph::Binary, Aleph::blossom_maximum_cardinality_matching(), Aleph::Branch, bytecode_module, Aleph::Call, call_stack, Aleph::Caret, Aleph::compiler_bytecode_value_to_string(), Aleph::compiler_bytecode_values_equal(), complete_current_frame(), Aleph::completed(), Aleph::Compiler_Bytecode_Interpreter_Detail::constant_to_value(), current_frame(), Aleph::EqEq, Aleph::error(), execute_host_call(), Aleph::Function, Aleph::Compiler_Bytecode_Value::function_id, Aleph::Compiler_Bytecode_Module::functions, Aleph::Greater, Aleph::GreaterEq, Aleph::Halt, Aleph::Halted, Aleph::Compiler_Bytecode_Execution_Result::halted(), Aleph::Host_Function, Aleph::Compiler_Bytecode_Value::is_function_ref(), Aleph::Compiler_Bytecode_Value::is_runtime_value(), Aleph::Jump, Aleph::Interpreter_Value::kind, Aleph::Less, Aleph::LessEq, Aleph::Load_Constant, Aleph::Load_Function, Aleph::Load_Global, Aleph::Load_Local, Aleph::Compiler_Bytecode_Value::make_bool(), make_error(), Aleph::Compiler_Bytecode_Value::make_function_ref(), Aleph::Compiler_Bytecode_Value::make_integer(), Aleph::Compiler_Bytecode_Value::make_unit(), Aleph::Minus, Aleph::NotEq, Aleph::Compiler_Bytecode_Execution_Result::ok(), Aleph::OrOr, Aleph::Percent, Aleph::Pipe, Aleph::Plus, push_frame(), read_callee_and_arguments(), read_global(), read_local(), read_register(), require_bool(), require_integer(), Aleph::Return, Aleph::Returned, Aleph::Compiler_Bytecode_Value::runtime_value, Aleph::Slash, Aleph::Star, Aleph::Store_Global, Aleph::Store_Local, Aleph::Tilde, Aleph::Compiler_Bytecode_Detail::token_name(), Aleph::Trap, Aleph::Unary, value, write_global(), write_local(), and write_register().

Referenced by execute_entry().

◆ global_slot()

const Compiler_Bytecode_Value * Aleph::Compiler_Bytecode_VM::global_slot ( const Compiler_IR_Global_Slot_Id  slot_id) const
inlinenoexcept

Returns one global slot value if the slot id is valid.

Definition at line 1643 of file Bytecode_Interpreter.H.

References globals.

◆ global_values()

const DynArray< Compiler_Bytecode_Value > & Aleph::Compiler_Bytecode_VM::global_values ( ) const
inlinenoexcept

Returns the current global slot values.

Definition at line 1636 of file Bytecode_Interpreter.H.

References globals.

◆ make_error()

◆ make_validation_error()

Compiler_Bytecode_Execution_Result Aleph::Compiler_Bytecode_VM::make_validation_error ( const Compiler_Bytecode_Validation_Report &  report,
const size_t  executed_instructions = 0 
) const
inlineprivate

◆ module()

const Compiler_Bytecode_Module & Aleph::Compiler_Bytecode_VM::module ( ) const
inlinenoexcept

Returns the immutable bytecode module executed by this VM.

Definition at line 1629 of file Bytecode_Interpreter.H.

References bytecode_module.

◆ module_is_valid()

bool Aleph::Compiler_Bytecode_VM::module_is_valid ( Compiler_Bytecode_Execution_Result &  error) const
inlineprivate

◆ push_frame()

◆ read_callee_and_arguments()

bool Aleph::Compiler_Bytecode_VM::read_callee_and_arguments ( const Frame &  frame,
const Compiler_Bytecode_Instruction &  inst,
const size_t  executed_instructions,
Compiler_Bytecode_Value &  callee,
DynArray< Compiler_Bytecode_Value > &  arguments,
Compiler_Bytecode_Execution_Result &  error 
) const
inlineprivate

◆ read_global()

bool Aleph::Compiler_Bytecode_VM::read_global ( const Frame &  frame,
const Compiler_IR_Global_Slot_Id  id,
const Compiler_Bytecode_Instruction &  inst,
const size_t  executed_instructions,
Compiler_Bytecode_Value &  value,
Compiler_Bytecode_Execution_Result &  error 
) const
inlineprivate

◆ read_local()

bool Aleph::Compiler_Bytecode_VM::read_local ( const Frame &  frame,
const Compiler_IR_Local_Slot_Id  id,
const Compiler_Bytecode_Instruction &  inst,
const size_t  executed_instructions,
Compiler_Bytecode_Value &  value,
Compiler_Bytecode_Execution_Result &  error 
) const
inlineprivate

◆ read_register()

bool Aleph::Compiler_Bytecode_VM::read_register ( const Frame &  frame,
const Compiler_Bytecode_Register_Id  id,
const Compiler_Bytecode_Instruction &  inst,
const size_t  executed_instructions,
Compiler_Bytecode_Value &  value,
Compiler_Bytecode_Execution_Result &  error 
) const
inlineprivate

◆ require_bool()

bool Aleph::Compiler_Bytecode_VM::require_bool ( const Compiler_Bytecode_Value &  value,
const Frame &  frame,
const Compiler_Bytecode_Instruction &  inst,
const size_t  executed_instructions,
const std::string &  context,
bool &  result,
Compiler_Bytecode_Execution_Result &  error 
) const
inlineprivate

◆ require_integer()

bool Aleph::Compiler_Bytecode_VM::require_integer ( const Compiler_Bytecode_Value &  value,
const Frame &  frame,
const Compiler_Bytecode_Instruction &  inst,
const size_t  executed_instructions,
const std::string &  context,
long long &  result,
Compiler_Bytecode_Execution_Result &  error 
) const
inlineprivate

◆ reset()

void Aleph::Compiler_Bytecode_VM::reset ( )
inline

Resets globals to Invalid and clears any pending call stack.

Definition at line 1618 of file Bytecode_Interpreter.H.

References bytecode_module, call_stack, Aleph::Compiler_Bytecode_Module::global_slots, globals, and Aleph::Compiler_Bytecode_Interpreter_Detail::reset_values().

Referenced by Compiler_Bytecode_VM().

◆ run_top_level()

Compiler_Bytecode_Execution_Result Aleph::Compiler_Bytecode_VM::run_top_level ( const size_t  max_steps = 0)
inline

Executes the module top-level body.

Parameters
max_stepsOptional execution limit. Use 0 for no limit.
Returns
Structured completion result.

Definition at line 1679 of file Bytecode_Interpreter.H.

References Aleph::blossom_maximum_cardinality_matching(), bytecode_module, execute_entry(), make_error(), and Aleph::Compiler_Bytecode_Module::top_level.

◆ set_global()

bool Aleph::Compiler_Bytecode_VM::set_global ( const Compiler_IR_Global_Slot_Id  slot_id,
const Compiler_Bytecode_Value &  value 
)
inline

Writes one global slot before or after execution.

Parameters
slot_idGlobal slot id to update.
valueNew runtime value.
Returns
true if the slot exists, false otherwise.

Definition at line 1664 of file Bytecode_Interpreter.H.

References globals, and value.

◆ write_global()

bool Aleph::Compiler_Bytecode_VM::write_global ( Frame &  frame,
const Compiler_IR_Global_Slot_Id  id,
const Compiler_Bytecode_Value &  value,
const Compiler_Bytecode_Instruction &  inst,
const size_t  executed_instructions,
Compiler_Bytecode_Execution_Result &  error 
)
inlineprivate

◆ write_local()

bool Aleph::Compiler_Bytecode_VM::write_local ( Frame &  frame,
const Compiler_IR_Local_Slot_Id  id,
const Compiler_Bytecode_Value &  value,
const Compiler_Bytecode_Instruction &  inst,
const size_t  executed_instructions,
Compiler_Bytecode_Execution_Result &  error 
) const
inlineprivate

◆ write_register()

bool Aleph::Compiler_Bytecode_VM::write_register ( Frame &  frame,
const Compiler_Bytecode_Register_Id  id,
const Compiler_Bytecode_Value &  value,
const Compiler_Bytecode_Instruction &  inst,
const size_t  executed_instructions,
Compiler_Bytecode_Execution_Result &  error 
) const
inlineprivate

Member Data Documentation

◆ bytecode_module

const Compiler_Bytecode_Module* Aleph::Compiler_Bytecode_VM::bytecode_module = nullptr
private

◆ call_stack

DynArray<Frame> Aleph::Compiler_Bytecode_VM::call_stack
private

◆ globals

DynArray<Compiler_Bytecode_Value> Aleph::Compiler_Bytecode_VM::globals
private

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