feat(furvm): add step function to executor

This commit is contained in:
2026-06-06 23:30:25 +02:00
parent bfdbbb0d5f
commit e7e5f36380
7 changed files with 115 additions and 2 deletions
+32
View File
@@ -1,6 +1,10 @@
#include "furvm/executor.hpp"
#include "furvm/context.hpp" // IWYU pragma: keep
#include "furvm/instruction.hpp"
#include "furvm/thing.hpp"
#include <stdexcept>
namespace furvm {
@@ -47,4 +51,32 @@ thing_p executor::thing() const {
return m_stack.top();
}
void executor::step() {
if ((m_flags & executor_flags::Suspended) == executor_flags::Suspended) return;
struct frame& frame = m_frames.top();
instruction_t instr = static_cast<instruction_t>(frame.mod->byte(frame.position++));
switch (instr) {
case instruction_t::NoOperation: break;
case instruction_t::Return: {
pop_frame();
if (m_frames.empty()) m_flags = m_flags | executor_flags::Done;
} break;
case instruction_t::PushB2I: {
auto thing = thing::create(m_context, thing_t::Int32);
thing->int32() = frame.mod->byte(frame.position++);
m_stack.push(std::move(thing));
} break;
case instruction_t::Drop: {
m_stack.pop();
} break;
case instruction_t::PushConstant:
case instruction_t::Duplicate:
case instruction_t::ReturnValue: {
throw std::runtime_error("unimplemented");
}
}
}
} // namespace furvm
+4
View File
@@ -22,6 +22,10 @@ int main(void) {
auto executor = furvm::executor::create(context);
executor->push_frame(mainModule, 0);
while ((executor->flags() & furvm::executor_flags::Done) != furvm::executor_flags::Done) {
executor->step();
}
return 0;
}
+11 -1
View File
@@ -10,7 +10,7 @@ namespace furvm {
std::size_t thing_type_size(thing_t type) {
switch (type) {
case thing_t::Int: return 4;
case thing_t::Int32: return 4;
}
return 0;
}
@@ -28,6 +28,16 @@ thing_p thing::create(const context_p& context, thing_t type) {
return std::move(th);
}
std::int32_t& thing::int32() {
if (m_type != thing_t::Int32) throw bad_thing_access();
return *static_cast<std::int32_t*>(m_data);
}
const std::int32_t& thing::int32() const {
if (m_type != thing_t::Int32) throw bad_thing_access();
return *static_cast<std::int32_t*>(m_data);
}
} // namespace furvm
// NOLINTEND(cppcoreguidelines-no-malloc)