|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Small portable VM for executing one Compiler_Bytecode_Module.
More...
#include <Bytecode_Interpreter.H>
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 Attributes | |
| const Compiler_Bytecode_Module * | bytecode_module = nullptr |
| DynArray< Compiler_Bytecode_Value > | globals |
| DynArray< Frame > | call_stack |
Small portable VM for executing one Compiler_Bytecode_Module.
The VM owns mutable runtime state only:
The bytecode module itself remains immutable and can be shared.
Definition at line 483 of file Bytecode_Interpreter.H.
|
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().
|
inline |
Executes one bytecode function directly.
| function_id | Callee id inside module().functions. |
| arguments | Positional bytecode values already prepared by the caller. |
| max_steps | Optional execution limit. Use 0 for no limit. |
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().
|
inlinenoexcept |
Returns the current dynamic call-stack depth.
Definition at line 1652 of file Bytecode_Interpreter.H.
References call_stack.
|
inlineprivate |
Definition at line 865 of file Bytecode_Interpreter.H.
References Aleph::blossom_maximum_cardinality_matching(), call_stack, Aleph::completed(), current_frame(), Aleph::error(), Aleph::Compiler_Bytecode_Execution_Result::halted(), Aleph::Returned, Aleph::Compiler_Bytecode_Execution_Result::returned(), value, and write_register().
Referenced by execute_loop().
|
inlineprivate |
Definition at line 507 of file Bytecode_Interpreter.H.
References call_stack.
Definition at line 501 of file Bytecode_Interpreter.H.
References call_stack.
Referenced by complete_current_frame(), and execute_loop().
|
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.
|
inlineprivate |
Definition at line 1582 of file Bytecode_Interpreter.H.
References Aleph::blossom_maximum_cardinality_matching(), call_stack, Aleph::compiler_bytecode_invalid_id(), Aleph::error(), execute_loop(), module_is_valid(), and push_frame().
Referenced by call_function(), and run_top_level().
|
inlineprivate |
Definition at line 894 of file Bytecode_Interpreter.H.
References Aleph::DynArray< T >::access(), Aleph::and, Aleph::blossom_maximum_cardinality_matching(), Aleph::error(), Aleph::Compiler_Bytecode_Execution_Result::error, Aleph::Interpreter_Value::host_function, Aleph::Interpreter_Value::host_user_data, make_error(), Aleph::Interpreter_Value::make_invalid(), Aleph::Compiler_Bytecode_Value::make_value(), Aleph::Compiler_Bytecode_VM::Frame::pc, Aleph::Compiler_Bytecode_Value::runtime_value, Aleph::DynArray< T >::size(), Aleph::Compiler_Bytecode_Runtime_Error::span, and write_register().
Referenced by execute_loop().
|
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().
|
inlinenoexcept |
Returns one global slot value if the slot id is valid.
Definition at line 1643 of file Bytecode_Interpreter.H.
References globals.
|
inlinenoexcept |
Returns the current global slot values.
Definition at line 1636 of file Bytecode_Interpreter.H.
References globals.
|
inlineprivate |
Definition at line 513 of file Bytecode_Interpreter.H.
References Aleph::and, Aleph::blossom_maximum_cardinality_matching(), Aleph::Compiler_Bytecode_Runtime_Error::code, Aleph::code(), Aleph::error(), Aleph::message(), and Aleph::Compiler_Bytecode_Execution_Result::runtime_error().
Referenced by call_function(), execute_host_call(), execute_loop(), make_validation_error(), push_frame(), read_callee_and_arguments(), read_global(), read_local(), read_register(), require_bool(), require_integer(), run_top_level(), write_global(), write_local(), and write_register().
|
inlineprivate |
Definition at line 534 of file Bytecode_Interpreter.H.
References Aleph::blossom_maximum_cardinality_matching(), make_error(), and Aleph::message().
Referenced by module_is_valid().
|
inlinenoexcept |
Returns the immutable bytecode module executed by this VM.
Definition at line 1629 of file Bytecode_Interpreter.H.
References bytecode_module.
|
inlineprivate |
Definition at line 544 of file Bytecode_Interpreter.H.
References Aleph::blossom_maximum_cardinality_matching(), bytecode_module, Aleph::error(), make_validation_error(), and Aleph::validate_bytecode_module().
Referenced by execute_entry().
|
inlineprivate |
Definition at line 555 of file Bytecode_Interpreter.H.
References Aleph::DynArray< T >::access(), Aleph::blossom_maximum_cardinality_matching(), call_stack, Aleph::Compiler_Bytecode_VM::Frame::caller_destination, Aleph::Compiler_Bytecode_VM::Frame::caller_resume_pc, Aleph::Compiler_Bytecode_Interpreter_Detail::count_parameters(), Aleph::Compiler_Bytecode_Function::entry_pc, Aleph::error(), Aleph::Compiler_Bytecode_VM::Frame::function, Aleph::Compiler_Bytecode_Function::local_slots, Aleph::Compiler_Bytecode_VM::Frame::locals, make_error(), Aleph::Compiler_Bytecode_Function::name, Aleph::Parameter, Aleph::Compiler_Bytecode_VM::Frame::pc, Aleph::Compiler_Bytecode_Function::register_count, Aleph::Compiler_Bytecode_VM::Frame::registers, Aleph::Compiler_Bytecode_Interpreter_Detail::reset_values(), Aleph::Compiler_Bytecode_VM::Frame::returns_to_caller, and Aleph::DynArray< T >::size().
Referenced by execute_entry(), and execute_loop().
|
inlineprivate |
Definition at line 822 of file Bytecode_Interpreter.H.
References Aleph::DynArray< T >::append(), Aleph::blossom_maximum_cardinality_matching(), Aleph::DynArray< T >::clear(), Aleph::error(), make_error(), and read_register().
Referenced by execute_loop().
|
inlineprivate |
Definition at line 718 of file Bytecode_Interpreter.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::error(), globals, make_error(), and value.
Referenced by execute_loop().
|
inlineprivate |
Definition at line 664 of file Bytecode_Interpreter.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::error(), Aleph::Compiler_Bytecode_VM::Frame::locals, make_error(), and value.
Referenced by execute_loop().
|
inlineprivate |
Definition at line 610 of file Bytecode_Interpreter.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::error(), make_error(), Aleph::Compiler_Bytecode_VM::Frame::registers, and value.
Referenced by execute_loop(), and read_callee_and_arguments().
|
inlineprivate |
Definition at line 797 of file Bytecode_Interpreter.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::Bool, bytecode_module, Aleph::compiler_bytecode_value_to_string(), Aleph::error(), make_error(), and value.
Referenced by execute_loop().
|
inlineprivate |
Definition at line 772 of file Bytecode_Interpreter.H.
References Aleph::blossom_maximum_cardinality_matching(), bytecode_module, Aleph::compiler_bytecode_value_to_string(), Aleph::error(), Aleph::Integer, make_error(), and value.
Referenced by execute_loop().
|
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().
|
inline |
Executes the module top-level body.
| max_steps | Optional execution limit. Use 0 for no limit. |
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.
|
inline |
Writes one global slot before or after execution.
| slot_id | Global slot id to update. |
| value | New runtime value. |
true if the slot exists, false otherwise. Definition at line 1664 of file Bytecode_Interpreter.H.
|
inlineprivate |
Definition at line 750 of file Bytecode_Interpreter.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::error(), globals, make_error(), and value.
Referenced by execute_loop().
|
inlineprivate |
Definition at line 696 of file Bytecode_Interpreter.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::error(), Aleph::Compiler_Bytecode_VM::Frame::locals, make_error(), and value.
Referenced by execute_loop().
|
inlineprivate |
Definition at line 642 of file Bytecode_Interpreter.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::error(), make_error(), Aleph::Compiler_Bytecode_VM::Frame::registers, and value.
Referenced by complete_current_frame(), execute_host_call(), and execute_loop().
|
private |
Definition at line 496 of file Bytecode_Interpreter.H.
Referenced by call_function(), dump_globals(), execute_loop(), module(), module_is_valid(), require_bool(), require_integer(), reset(), and run_top_level().
Definition at line 498 of file Bytecode_Interpreter.H.
Referenced by call_stack_depth(), complete_current_frame(), current_frame(), current_frame(), execute_entry(), execute_loop(), push_frame(), and reset().
|
private |
Definition at line 497 of file Bytecode_Interpreter.H.
Referenced by dump_globals(), global_slot(), global_values(), read_global(), reset(), set_global(), and write_global().