32 log(std::format(
"\nVM::{}(): RUN (pc={})\n", __func__,
pc - 1));
50 catch (
const std::exception &e)
58 logerr(std::format(
"{}\n", e.what()));
70 log(std::format(
"VM::{}()\n", __func__));
78 log(std::format(
"VM::{}()\n", __func__));
85 reset(
true,
true,
true);
88void VM::reset(
const bool &resetStack,
const bool &resetFunctions,
const bool &resetVariables)
91 log(std::format(
"VM::{}()\n", __func__));
117 info = std::format(
"Stack Top: {:T}\n",
peek());
119 std::string registersStr;
125 registersStr += std::format(
"R{}: {:T}\n", regCount, reg);
131 "VM INFORMATION:\n{}PC: {}\nCS: {}",
143 std::string constants;
145 std::string functions;
146 std::string instructions;
148 for (
const auto &constant :
m_bytecode->constants)
150 constants += std::format(
"{:T}\n", constant);
152 for (
const auto &variable :
m_bytecode->variables)
154 variables += std::format(
"{}\n", variable.first);
156 for (
const auto &function :
m_bytecode->functionEntries)
158 functions += std::format(
"{}() PC = {}\n", function.first, function.second);
161 for (
const auto &instruction :
m_bytecode->instructions)
163 instructions += std::format(
"{}({}, {}, {})\n",
opCodeToString(instruction.op), instruction.operand1,
164 instruction.operand2, instruction.operand3);
168 info = std::format(
"BYTECODE INFORMATION:\n\nConstants: {}\n{}\nVariables: {}\n{}\nFunctions: {}\n{}\nInstructions: {}\n{}",
171 m_bytecode->functionEntries.size(), functions,
172 m_bytecode->instructions.size(), instructions);
void asm_print_stdout(const char *s, int64_t len)
Native print function.
void asm_print_stderr(const char *s, int64_t len)
Native print error function.
Throws when the HALT opcode is reached.
ImportHandler importHandler
Import handler for loading modules.
std::vector< Value > variables
Variable storage indexed by variable index.
void setImportHandler(const ImportHandler &handler)
Set the import handler for importing modules.
std::function< void(const std::filesystem::path &path)> ImportHandler
void logerr(const Value &msg)
Log a Value to stderr.
std::vector< Value > stack
Stack for function calls.
const Bytecode * m_bytecode
Bytecode to execute.
std::vector< int > callStack
Call stack for function calls.
std::map< std::string, NativeFunction > nativeFunctions
Native function registry.
std::string getBytecodeInformation()
Get bytecode information for debugging.
void log(const Value &msg)
Log a Value to stdout.
std::array< Value, MAX_REGISTERS > registers
Virtual registers for register-based operations (v2.0).
void flush()
Flush stdout.
void cleanup()
Clean up the virtual machine.
size_t pc
Program counter.
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.
Value peek()
Peek at the top value on the stack.
void flusherr()
Flush stderr.
int run(const Bytecode &bytecode)
Run the virtual machine Exits -1 on uncaught exception.
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.
A value in the Phasor VM.
std::string toString() const
Convert to string for printing.
The Phasor Programming Language and Runtime.
std::string opCodeToString(OpCode op)
Complete bytecode structure.
Instruction with up to 5 operands Format: instruction operand1, operand2, operand3 Each instruction u...
int32_t operand1
First operand.
int32_t operand2
Second operand.
int32_t operand3
Third operand.