refactor(furvm): improve type system

Closes: #52
This commit is contained in:
2026-07-11 00:45:09 +02:00
parent abbc1714c1
commit 3c0588e8db
13 changed files with 438 additions and 426 deletions
-24
View File
@@ -1,24 +0,0 @@
#include "furvm/context.hpp"
#include "furvm/thing.hpp" // IWYU pragma: keep
#include "furvm/type.hpp"
#include <cstdint>
#include <memory>
namespace furvm {
context::context()
: m_thingAllocator(m_thingArena) {
mod core;
core.emplace_type(std::make_shared<type>(byteType));
core.emplace_type(std::make_shared<type>(shortType));
core.emplace_type(std::make_shared<type>(intType));
core.emplace_type(std::make_shared<type>(longType));
static_assert(sizeof(std::uintptr_t) == 8, "Unsupported platform");
core.emplace_type(*core.type_at(3));
emplace("core", std::move(core)).dispatch();
}
} // namespace furvm
+55 -34
View File
@@ -6,14 +6,35 @@
#include "furvm/fwd.hpp"
#include "furvm/instruction.hpp"
#include "furvm/thing.hpp"
#include "furvm/type.hpp"
#include <cassert>
#include <cstdint>
#include <stdexcept>
#include <vector>
namespace furvm {
thing_type executor::thing_type_impl(mod_h mod, mod_type type) const {
while (type.type == mod_type::Import) {
auto imprt = std::move(type.value.imprt);
mod = m_context->at(imprt.modId);
type = *mod->type_at(imprt.typeId);
}
switch (static_cast<enum thing_type::type>(type.type)) {
case thing_type::Primitive: return { static_cast<enum thing_type::type>(type.type), type.value.primitive };
case thing_type::Array: {
return { static_cast<enum thing_type::type>(type.type),
{ thing_type(mod, *mod->type_at(type.value.array.typeId)), type.value.array.size } };
}
default: throw std::runtime_error("invalid thing type");
}
}
thing_type* executor::thing_type(const mod_h& mod, const mod_type& type) const {
struct thing_type thingType = thing_type_impl(mod, type);
return m_context->thing_type_store().insert(thingType);
}
void executor::push_frame(const mod_h& mod, function function) {
mod_h modInst = mod;
while (function.type() == function_t::Import) {
@@ -26,7 +47,7 @@ void executor::push_frame(const mod_h& mod, function function) {
args.reserve(signature.params.size());
for (const auto& param : signature.params) {
auto arg = pop_thing();
if (arg->type().type != param) throw std::runtime_error("function argument type mismatch");
if (arg->type() != *thing_type(mod, *param)) throw std::runtime_error("function argument type mismatch");
args.push_back(std::move(arg));
}
@@ -87,10 +108,6 @@ thing_h executor::load_thing(variable_t variable) const {
return frame.variables[variable];
}
static type_ref get_type_ref(const mod_h& mod, type_id id) {
return { mod, mod->type_at(id) };
}
void executor::step() {
if ((m_flags & executor_flags::Suspended) == executor_flags::Suspended) return;
@@ -100,25 +117,25 @@ void executor::step() {
switch (instr) {
case instruction_t::NoOperation: break;
case instruction_t::PushB2I: {
push_thing({ get_type_ref(m_context->at("core"), 2), m_context, m_context->thing_alloc() })->get<int_t>() =
push_thing({ (struct thing_type){ thing_type::Primitive, { 4 } }, m_context->thing_alloc() })->get<int>() =
frame.mod->byte(frame.position++);
} break;
case instruction_t::Array: {
type_id typeId = static_cast<type_id>(frame.mod->byte(frame.position)) |
(static_cast<type_id>(frame.mod->byte(frame.position + 1)) << 8) |
(static_cast<type_id>(frame.mod->byte(frame.position + 2)) << 16) |
(static_cast<type_id>(frame.mod->byte(frame.position + 3)) << 24);
frame.position += 4;
mod_type_id typeId = static_cast<mod_type_id>(frame.mod->byte(frame.position)) |
(static_cast<mod_type_id>(frame.mod->byte(frame.position + 1)) << 8) |
(static_cast<mod_type_id>(frame.mod->byte(frame.position + 2)) << 16) |
(static_cast<mod_type_id>(frame.mod->byte(frame.position + 3)) << 24);
frame.position += 4;
auto type = furvm::thing<>::resolve_type(get_type_ref(frame.mod, typeId), m_context);
if (*type.type == nullptr || type->t != type_t::Array || *type->array.type == nullptr)
const auto& type = *thing_type(frame.mod, *frame.mod->type_at(typeId));
if (type.type != thing_type::Array || type.value.array.type == nullptr || type.value.array.type == &type)
throw std::runtime_error("invalid array type");
auto array = push_thing({ type, m_context, m_context->thing_alloc() });
auto array = push_thing({ type, m_context->thing_alloc() });
if (type->array.size == 0) {
auto sizeThing = pop_thing();
long_t size = sizeThing->integer();
if (type.value.array.size == 0) {
auto sizeThing = pop_thing();
std::int64_t size = sizeThing->integer();
array->resize(size);
}
@@ -199,27 +216,31 @@ void executor::step() {
} break;
case instruction_t::Pointerof: {
auto thing = pop_thing();
auto ptr = push_thing({ get_type_ref(m_context->at("core"), 4), m_context, m_context->thing_alloc() });
switch (furvm::thing<>::resolve_type(thing->type(), m_context)->t) {
case type_t::Primitive: ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(thing->raw()); break;
case type_t::Array:
ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(thing->get<array_t>().data);
auto ptr = push_thing(
{ (struct thing_type){ thing_type::Primitive, { sizeof(std::uintptr_t) } }, m_context->thing_alloc() });
switch (thing->type().type) {
case thing_type::Primitive: ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(thing->raw()); break;
case thing_type::Array:
ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(
thing->type().value.array.size == 0 ? thing->get<furvm::thing<>::array>().dynamic.data
: thing->get<furvm::thing<>::array>().flat);
break;
case type_t::Import:
default: throw std::runtime_error("unreachable");
case thing_type::Count: throw std::runtime_error("unreachable");
}
} break;
case instruction_t::Sizeof: {
auto thing = pop_thing();
auto size = push_thing({ get_type_ref(m_context->at("core"), 3), m_context, m_context->thing_alloc() });
auto type = furvm::thing<>::resolve_type(thing->type(), m_context);
switch (type->t) {
case type_t::Primitive: size->get<long_t>() = static_cast<long_t>(type->primitive); break;
case type_t::Array:
size->get<long_t>() =
(type->array.size == 0) ? thing->get<array_t>().dynamic.size : static_cast<long_t>(type->array.size);
auto size = push_thing(
{ (struct thing_type){ thing_type::Primitive, { sizeof(std::size_t) } }, m_context->thing_alloc() });
switch (thing->type().type) {
case thing_type::Primitive:
size->get<std::int64_t>() = static_cast<std::int64_t>(thing->type().value.primitive);
break;
case thing_type::Array:
size->get<std::int64_t>() = static_cast<std::int64_t>((thing->type().value.array.size == 0)
? thing->get<furvm::thing<>::array>().dynamic.size
: thing->type().value.array.size);
break;
case type_t::Import:
default: throw std::runtime_error("unreachable");
}
} break;
@@ -247,7 +268,7 @@ void executor::step() {
case instruction_t::JumpNotZero: {
byte offset = frame.mod->byte(frame.position++);
auto cond = pop_thing();
if (cond->get<int_t>() != 0) frame.position += (std::int8_t)offset;
if (cond->integer() != 0) frame.position += (std::int8_t)offset;
} break;
case instruction_t::Return: {
pop_frame();
+8
View File
@@ -98,4 +98,12 @@ function& function::operator=(const function& other) {
return *this;
}
namespace detail {
std::size_t function_sig_hash::operator()(const function_sig& signature) const {
return furlang::utility::vector_hash<mod_type_h, detail::handle_hash<mod_type_h>>{}(signature.params);
}
} // namespace detail
} // namespace furvm
+11 -10
View File
@@ -11,19 +11,19 @@
#include <sstream>
static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
switch (thing.type()->t) {
case furvm::type_t::Primitive: {
switch (thing.type().type) {
case furvm::thing_type::Primitive: {
std::cout << thing.integer();
} break;
case furvm::type_t::Array: {
case furvm::thing_type::Array: {
std::cout << "{ ";
for (furvm::long_t i = 0; i < thing.size(); ++i) {
for (furvm::thing<>::s64 i = 0; i < thing.size(); ++i) {
if (i > 0) std::cout << ", ";
print_thing(thing.at(i));
}
std::cout << " }\n";
} break;
default: std::cerr << "{Something went wrong}";
default: std::cerr << "{Type not recognized}";
}
}
@@ -47,7 +47,7 @@ int main(int argc, char** argv) {
#else
static const furvm::byte s_bytecode[] = {
static_cast<furvm::byte>(furvm::instruction_t::Array),
0,
1,
0,
0,
0,
@@ -58,10 +58,11 @@ int main(int argc, char** argv) {
static_cast<furvm::byte>(furvm::instruction_t::Return),
};
furvm::mod_h mod = context->emplace("main", s_bytecode, s_bytecode + sizeof(s_bytecode));
furvm::type_h intType = mod->emplace_type(std::make_shared<furvm::type>(context->at("core")->type_at(2), 10));
auto mainFunc = mod->emplace_function("main", furvm::function_sig{}, 0);
mod->emplace_function(furvm::function_sig{ { intType } }, "print").dispatch();
auto mod = context->emplace("main", s_bytecode, s_bytecode + sizeof(s_bytecode));
auto intType = mod->emplace_type(sizeof(int));
auto arrayType = mod->emplace_type(intType.id(), 10);
auto mainFunc = mod->emplace_function("main", furvm::function_sig{}, 0);
mod->emplace_function(furvm::function_sig{ { arrayType } }, "print").dispatch();
#endif
mod->set_native_function("print", [](furvm::executor& executor) { print_thing(*executor.load_thing(0)); });
+24 -24
View File
@@ -3,7 +3,6 @@
#include "furvm/detail/serialization.hpp"
#include "furvm/function.hpp"
#include "furvm/fwd.hpp"
#include "furvm/type.hpp"
#include <cstdint>
#include <cstring>
@@ -18,25 +17,26 @@ std::ostream& mod::serialize(std::ostream& os) const {
os.write(MAGIC, sizeof(MAGIC));
detail::serialize(os, std::uint32_t(0)); // version
type_id typeCount = type_id(m_types.cend() - m_types.cbegin());
mod_type_id typeCount = mod_type_id(m_types.cend() - m_types.cbegin());
detail::serialize(os, typeCount);
for (type_id id = 0; id < typeCount; ++id) {
for (mod_type_id id = 0; id < typeCount; ++id) {
if (!m_types.contains(id)) {
detail::serialize(os, std::uint8_t(0xFF)); // null type
continue;
}
auto type = *m_types.at(id);
switch (type->t) {
case type_t::Primitive: detail::serialize(os, type->primitive); break;
case type_t::Array: {
detail::serialize(os, type->array.size);
detail::serialize(os, type->array.type.id());
switch (type.type) {
case mod_type::Primitive: detail::serialize(os, type.value.primitive); break;
case mod_type::Array: {
detail::serialize(os, type.value.array.typeId);
detail::serialize(os, type.value.array.size);
} break;
case type_t::Import: {
detail::serialize(os, type->imp.mod);
detail::serialize(os, type->imp.type);
case mod_type::Import: {
detail::serialize(os, type.value.imprt.modId);
detail::serialize(os, type.value.imprt.typeId);
} break;
case mod_type::Count: throw std::runtime_error("unreachable");
}
}
@@ -93,32 +93,32 @@ mod mod::load(std::istream& is) {
mod mod;
type_id typeCount = 0;
mod_type_id typeCount = 0;
detail::load(is, typeCount);
for (type_id id = 0; id < typeCount; ++id) {
for (mod_type_id id = 0; id < typeCount; ++id) {
std::uint8_t type = 0;
detail::load(is, type);
if (type == 0xFF) continue;
switch ((type_t)type) {
case type_t::Primitive: {
primitive_type primitive = 0;
switch ((enum mod_type::type)type) {
case mod_type::Primitive: {
mod_type::primitive primitive = 0;
detail::load(is, primitive);
mod.emplace_type(id, std::make_shared<class type>(primitive)).dispatch();
mod.emplace_type(id, primitive).dispatch();
} break;
case type_t::Array: {
case mod_type::Array: {
mod_type_id typeId = 0;
detail::load(is, typeId);
std::size_t size = 0;
detail::load(is, size);
type_id typeId = 0;
detail::load(is, typeId);
mod.emplace_type(id, std::make_shared<class type>(mod.type_at(typeId), size)).dispatch();
mod.emplace_type(id, typeId, size).dispatch();
} break;
case type_t::Import: {
case mod_type::Import: {
std::string modName;
detail::load(is, modName);
type_id typeId = 0;
mod_type_id typeId = 0;
detail::load(is, typeId);
mod.emplace_type(id, std::make_shared<class type>(import_type{ std::move(modName), typeId })).dispatch();
mod.emplace_type(id, std::move(modName), typeId).dispatch();
} break;
default: throw std::runtime_error("unknown type type");
}