refactor(furvm): use the handle system

Refs: #12
This commit is contained in:
2026-06-17 13:59:54 +02:00
parent e8ccaa6390
commit 00bcc0e6a5
10 changed files with 112 additions and 53 deletions
+2 -1
View File
@@ -28,7 +28,8 @@ Checks: >
-cppcoreguidelines-macro-usage, -cppcoreguidelines-macro-usage,
-cppcoreguidelines-owning-memory, -cppcoreguidelines-owning-memory,
-cppcoreguidelines-non-private-member-variables-in-classes, -cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-pro-bounds-avoid-unchecked-container-access -cppcoreguidelines-pro-bounds-avoid-unchecked-container-access,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay
WarningsAsErrors: "*" WarningsAsErrors: "*"
+11 -7
View File
@@ -2,6 +2,7 @@
#define FURVM_CONTEXT_HPP #define FURVM_CONTEXT_HPP
#include "furlang/arena.hpp" #include "furlang/arena.hpp"
#include "furvm/executor.hpp"
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include "furvm/module.hpp" #include "furvm/module.hpp"
@@ -40,12 +41,13 @@ public:
* @brief Emplaces a new module in this context. * @brief Emplaces a new module in this context.
* *
* @param args Arguments to forward to module's constructor. * @param args Arguments to forward to module's constructor.
* @return An index to the emplaced module. * @return A handle to the module.
*/ */
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<mod, Args...>>> template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<mod, Args...>>>
auto& emplace_module(Args&&... args) { mod_h emplace_module(Args&&... args) {
mod_p mod = std::make_shared<class mod>(std::forward<Args>(args)...); mod_p mod = std::make_shared<class mod>(std::forward<Args>(args)...);
return m_modules.emplace(mod->name(), std::move(mod)).first->second; std::string name = mod->name();
return { name, m_modules[name] = std::move(mod) };
} }
/** /**
@@ -89,9 +91,11 @@ public:
constexpr size_t module_count() const { return m_modules.size(); } constexpr size_t module_count() const { return m_modules.size(); }
public: public:
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<executor, Args...>>> template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<executor, Args...>>>
auto& emplace_executor(Args&&... args) { executor_h emplace_executor(Args&&... args) {
executor_p executor = std::make_shared<class executor>(std::forward<Args>(args)...); executor executor(std::forward<Args>(args)...);
return m_executors.emplace_back(std::move(executor)); executor_id id = m_executors.size();
m_executors.emplace_back(std::move(executor));
return { id, m_executors[id] };
} }
public: public:
/** /**
@@ -102,7 +106,7 @@ private:
std::unordered_map<std::string, mod_p> m_modules; std::unordered_map<std::string, mod_p> m_modules;
std::vector<thing_p> m_things; std::vector<thing_p> m_things;
std::vector<executor_p> m_executors; std::vector<executor> m_executors;
std::queue<thing_id> m_deadThings; std::queue<thing_id> m_deadThings;
std::vector<void*> m_deadThingData; std::vector<void*> m_deadThingData;
+11 -4
View File
@@ -33,7 +33,7 @@ public:
* Call frame. * Call frame.
*/ */
struct frame { struct frame {
mod_p mod; /**< Shared pointer to a module with the bytecode. */ mod_h mod; /**< Shared pointer to a module with the bytecode. */
std::size_t position; /**< Cursor to a current instruction in the bytecode. */ std::size_t position; /**< Cursor to a current instruction in the bytecode. */
std::size_t stackBase; /**< Snapshot of the stack size before this frame. */ std::size_t stackBase; /**< Snapshot of the stack size before this frame. */
@@ -60,8 +60,15 @@ public:
*/ */
executor& operator=(executor&&) noexcept = default; executor& operator=(executor&&) noexcept = default;
executor(const executor&) = delete; /**
executor& operator=(const executor&) = delete; * @brief Copy constructor.
*/
executor(const executor&) = default;
/**
* @brief Copy constructor.
*/
executor& operator=(const executor&) = default;
public: public:
/** /**
* @brief Returns flags of this executor. * @brief Returns flags of this executor.
@@ -76,7 +83,7 @@ public:
* @param mod Module. * @param mod Module.
* @param function Function handle. * @param function Function handle.
*/ */
void push_frame(const mod_p& mod, function_id function); void push_frame(const mod_h& mod, const function_h& function);
/** /**
* @brief Pops the top frame. * @brief Pops the top frame.
+9 -2
View File
@@ -56,8 +56,15 @@ public:
*/ */
function& operator=(function&&) noexcept; function& operator=(function&&) noexcept;
function(const function&) = delete; /**
function& operator=(const function&) = delete; * @brief Copy constructor.
*/
function(const function&);
/**
* @brief Copy constructor.
*/
function& operator=(const function&);
public: public:
/** /**
* @brief Returns a name of this function. * @brief Returns a name of this function.
+13 -11
View File
@@ -4,7 +4,7 @@
#include "furvm/function.hpp" #include "furvm/function.hpp"
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include <memory> #include <stdexcept>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <utility> #include <utility>
@@ -91,8 +91,9 @@ public:
* @return The function. * @return The function.
*/ */
template <typename Name> template <typename Name>
constexpr const function_p& function_at(Name&& name) const { function_h function_at(Name&& name) const {
return m_functions.at(std::forward<Name>(name)); if (auto it = m_functionMap.find(std::forward<Name>(name)); it != m_functionMap.end()) return it->second;
throw std::runtime_error("invalid function");
} }
/** /**
@@ -101,22 +102,23 @@ public:
* @param id Id of the function. * @param id Id of the function.
* @return The function. * @return The function.
*/ */
constexpr const function_p& function_at(function_id id) const { return m_functions.at(id); } function_h function_at(function_id id) const { return { id, m_functions.at(id) }; }
template <typename... Args> template <typename... Args>
function_id emplace_function(Args&&... args) { function_h emplace_function(Args&&... args) {
function_p function = std::make_shared<class function>(std::forward<Args>(args)...); function function(std::forward<Args>(args)...);
m_functionMap.emplace(function->name(), function); std::string name = function.name();
function_id id = m_functions.size(); function_id id = m_functions.size();
m_functions.emplace_back(std::move(function)); function_h handle = { id, m_functions.emplace_back(std::move(function)) };
return id; m_functionMap.emplace(std::move(name), handle);
return handle;
} }
private: private:
std::string m_name; std::string m_name;
bytecode_t m_bytecode; bytecode_t m_bytecode;
std::unordered_map<std::string, function_p> m_functionMap; std::unordered_map<std::string, function_h> m_functionMap;
std::vector<function_p> m_functions; std::vector<function> m_functions;
}; };
} // namespace furvm } // namespace furvm
+18 -18
View File
@@ -13,10 +13,9 @@
namespace furvm { namespace furvm {
void executor::push_frame(const mod_p& mod, function_id function) { void executor::push_frame(const mod_h& mod, const function_h& function) {
const auto& func = mod->function_at(function); if (function->type() != function_t::Normal) throw std::runtime_error("unexpected function type");
if (func->type() != function_t::Normal) throw std::runtime_error("unexpected function type"); m_frames.emplace((struct executor::frame){ mod, function->position(), m_stack.size() });
m_frames.emplace((struct executor::frame){ mod, func->position(), m_stack.size() });
} }
struct executor::frame executor::pop_frame() { struct executor::frame executor::pop_frame() {
@@ -83,12 +82,12 @@ void executor::step() {
struct frame& frame = m_frames.top(); struct frame& frame = m_frames.top();
instruction_t instr = static_cast<instruction_t>(frame.mod->byte(frame.position++)); instruction_t instr = static_cast<instruction_t>((*frame.mod)->byte(frame.position++));
switch (instr) { switch (instr) {
case instruction_t::NoOperation: break; case instruction_t::NoOperation: break;
case instruction_t::PushB2I: { case instruction_t::PushB2I: {
auto thing = std::make_shared<class thing>(m_context, thing_t::Int32); auto thing = std::make_shared<class thing>(m_context, thing_t::Int32);
thing->int32() = frame.mod->byte(frame.position++); thing->int32() = (*frame.mod)->byte(frame.position++);
push_thing(std::move(thing)); push_thing(std::move(thing));
} break; } break;
case instruction_t::Drop: { case instruction_t::Drop: {
@@ -156,37 +155,38 @@ void executor::step() {
push_thing(lhs >= rhs); push_thing(lhs >= rhs);
} break; } break;
case instruction_t::Load: { case instruction_t::Load: {
variable_t variable = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) | variable_t variable = static_cast<std::uint16_t>((*frame.mod)->byte(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8); (static_cast<std::uint16_t>((*frame.mod)->byte(frame.position + 1)) << 8);
frame.position += 2; frame.position += 2;
push_thing(load_thing(variable)); push_thing(load_thing(variable));
} break; } break;
case instruction_t::Store: { case instruction_t::Store: {
variable_t variable = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) | variable_t variable = static_cast<std::uint16_t>((*frame.mod)->byte(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8); (static_cast<std::uint16_t>((*frame.mod)->byte(frame.position + 1)) << 8);
frame.position += 2; frame.position += 2;
store_thing(variable, std::move(pop_thing())); store_thing(variable, std::move(pop_thing()));
} break; } break;
case instruction_t::Call: { case instruction_t::Call: {
function_id funcId = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) | function_id funcId = static_cast<std::uint16_t>((*frame.mod)->byte(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8); (static_cast<std::uint16_t>((*frame.mod)->byte(frame.position + 1)) << 8);
frame.position += 2; frame.position += 2;
const function_p& function = frame.mod->function_at(funcId); const function_h& function = (*frame.mod)->function_at(funcId);
switch (function->type()) { switch (function->type()) {
case function_t::Normal: push_frame(frame.mod, funcId); break; case function_t::Normal: push_frame(frame.mod, function); break;
case function_t::Native: function->native()(*this); break; case function_t::Native: function->native()(*this); break;
case function_t::Import: { case function_t::Import: {
const mod_p& impMod = m_context->m_modules.at(function->imported_module()); // const mod_p& impMod = m_context->m_modules.at(function->imported_module());
push_frame(frame.mod, function->imported_function()); // push_frame(frame.mod, function->imported_function());
throw std::runtime_error("unimplemented");
} break; } break;
} }
} break; } break;
case instruction_t::Jump: { case instruction_t::Jump: {
frame.position += frame.mod->byte(frame.position++); frame.position += (*frame.mod)->byte(frame.position++);
} break; } break;
case instruction_t::JumpNotZero: { case instruction_t::JumpNotZero: {
byte offset = frame.mod->byte(frame.position++); byte offset = (*frame.mod)->byte(frame.position++);
auto cond = pop_thing(); auto cond = pop_thing();
if (cond->int32() != 0) frame.position += offset; if (cond->int32() != 0) frame.position += offset;
} break; } break;
+37
View File
@@ -54,4 +54,41 @@ function& function::operator=(function&& other) noexcept {
return *this; return *this;
} }
function::function(const function& other)
: m_name(other.m_name), m_type(other.m_type) {
switch (m_type) {
case function_t::Normal: {
m_value.position = other.m_value.position;
} break;
case function_t::Native: {
new (&m_value.native) native_function(other.m_value.native);
} break;
case function_t::Import: {
m_value.imp.moduleName = other.m_value.imp.moduleName;
m_value.imp.function = other.m_value.imp.function;
} break;
}
}
function& function::operator=(const function& other) {
if (this == &other) return *this;
m_name = other.m_name;
m_type = other.m_type;
switch (m_type) {
case function_t::Normal: {
m_value.position = other.m_value.position;
} break;
case function_t::Native: {
new (&m_value.native) native_function(other.m_value.native);
} break;
case function_t::Import: {
m_value.imp.moduleName = other.m_value.imp.moduleName;
m_value.imp.function = other.m_value.imp.function;
} break;
}
return *this;
}
} // namespace furvm } // namespace furvm
+5 -4
View File
@@ -3,6 +3,7 @@
#include "furvm/context.hpp" #include "furvm/context.hpp"
#include "furvm/executor.hpp" #include "furvm/executor.hpp"
#include "furvm/function.hpp" #include "furvm/function.hpp"
#include "furvm/fwd.hpp"
#include "furvm/instruction.hpp" #include "furvm/instruction.hpp"
#include "furvm/serializer.hpp" #include "furvm/serializer.hpp"
#include "furvm/thing.hpp" #include "furvm/thing.hpp"
@@ -24,11 +25,11 @@ static constexpr std::array<furvm::byte, 8> s_bytecode = {
int main(void) { int main(void) {
auto context = std::make_shared<furvm::context>(); auto context = std::make_shared<furvm::context>();
auto mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end()); furvm::mod_h mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end());
mainModule->emplace_function("main", 0); furvm::function_h mainFunc = (*mainModule)->emplace_function("main", 0);
auto executor = context->emplace_executor(context); furvm::executor_h executor = context->emplace_executor(context);
executor->push_frame(mainModule, 0); executor->push_frame(mainModule, mainFunc);
static constexpr std::size_t FPC = 3; // Frames per collection static constexpr std::size_t FPC = 3; // Frames per collection
+1 -1
View File
@@ -73,7 +73,7 @@ bool serializer::serialize_module(std::ostream& os, const mod_p& mod) {
write_u32(os, mod->m_functions.size()); write_u32(os, mod->m_functions.size());
for (function_id id = 0; id < static_cast<function_id>(mod->m_functions.size()); ++id) { for (function_id id = 0; id < static_cast<function_id>(mod->m_functions.size()); ++id) {
const auto& func = mod->m_functions[id]; const auto& func = mod->function_at(id);
write_u16(os, id); write_u16(os, id);
write_u8(os, static_cast<std::uint8_t>(func->type())); write_u8(os, static_cast<std::uint8_t>(func->type()));
switch (func->type()) { switch (func->type()) {