refactor: serialize types in module

Refs: #23
This commit is contained in:
2026-07-04 16:15:40 +02:00
parent cf284592fd
commit 100c542fe1
3 changed files with 32 additions and 14 deletions
+5 -5
View File
@@ -101,7 +101,7 @@ public:
std::memcpy(res.m_data, m_data, m_size); std::memcpy(res.m_data, m_data, m_size);
} break; } break;
case type_t::List: { case type_t::List: {
copy_list(resolve_type(m_type->list, m_modules), res.get<list_t>(), get<list_t>()); copy_list(resolve_type(*m_type->list, m_modules), res.get<list_t>(), get<list_t>());
} break; } break;
case type_t::Import: throw std::runtime_error("unreachable"); case type_t::Import: throw std::runtime_error("unreachable");
} }
@@ -274,10 +274,10 @@ public:
if (m_type->t != type_t::List) throw bad_thing_access(); if (m_type->t != type_t::List) throw bad_thing_access();
auto& list = get<list_t>(); auto& list = get<list_t>();
if (newSize < 0 || newSize == list.size) return; if (newSize < 0 || newSize == list.size) return;
std::byte* newData = new std::byte[compute_size_na(resolve_type(m_type->list, m_modules)) * newSize]; std::byte* newData = new std::byte[compute_size_na(resolve_type(*m_type->list, m_modules)) * newSize];
std::memcpy(newData, std::memcpy(newData,
list.data, list.data,
compute_size_na(resolve_type(m_type->list, m_modules)) * std::min(list.size, newSize)); compute_size_na(resolve_type(*m_type->list, m_modules)) * std::min(list.size, newSize));
list.size = newSize; list.size = newSize;
delete[] list.data; delete[] list.data;
list.data = newData; list.data = newData;
@@ -287,7 +287,7 @@ public:
if (m_type->t != type_t::List) throw bad_thing_access(); if (m_type->t != type_t::List) throw bad_thing_access();
auto& list = get<list_t>(); auto& list = get<list_t>();
if (index < 0 || index >= list.size) throw std::out_of_range("index out of range"); if (index < 0 || index >= list.size) throw std::out_of_range("index out of range");
thing res = { m_type->list, m_modules, m_allocator }; thing res = { *m_type->list, m_modules, m_allocator };
res.get<reference_t>() = list.data; // TODO: Account for padding, alignment and stuff res.get<reference_t>() = list.data; // TODO: Account for padding, alignment and stuff
return std::move(res); return std::move(res);
} }
@@ -314,7 +314,7 @@ private:
case type_t::Reference: std::memcpy(dst.data, src.data, size); break; case type_t::Reference: std::memcpy(dst.data, src.data, size); break;
case type_t::List: case type_t::List:
for (std::size_t i = 0; i < size; ++i) { for (std::size_t i = 0; i < size; ++i) {
copy_list(innerType->list, copy_list(*innerType->list,
*std::launder(reinterpret_cast<list_t*>(dst.data)), *std::launder(reinterpret_cast<list_t*>(dst.data)),
*std::launder(reinterpret_cast<list_t*>(src.data))); *std::launder(reinterpret_cast<list_t*>(src.data)));
} }
+5 -9
View File
@@ -2,10 +2,9 @@
#define FURVM_TYPE_HPP #define FURVM_TYPE_HPP
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include "furvm/handle.hpp" // IWYU pragma: keep
#include <cstdint> #include <cstdint>
#include <memory>
#include <utility>
namespace furvm { namespace furvm {
@@ -18,8 +17,8 @@ enum class type_t : std::uint32_t {
}; };
using primitive_type = std::uint64_t; using primitive_type = std::uint64_t;
using reference_type = std::shared_ptr<type>; using reference_type = type_p;
using list_type = std::shared_ptr<type>; using list_type = type_h;
struct import_type { struct import_type {
mod_id mod; mod_id mod;
@@ -44,11 +43,8 @@ struct type {
type(const reference_type& reference) type(const reference_type& reference)
: t(type_t::Reference), reference(reference) {} : t(type_t::Reference), reference(reference) {}
static type make_list(const list_type& list) { type(const list_type& list)
type type(type_t::List); : t(type_t::List), list(list) {}
new (&type.list) list_type(list);
return type;
}
type(const import_type& imp) type(const import_type& imp)
: t(type_t::Import), imp(imp) {} : t(type_t::Import), imp(imp) {}
+22
View File
@@ -2,8 +2,10 @@
#include "furvm/detail/serialization.hpp" #include "furvm/detail/serialization.hpp"
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include "furvm/type.hpp"
#include <ios> #include <ios>
#include <stdexcept>
namespace furvm { namespace furvm {
@@ -35,6 +37,26 @@ std::ostream& mod::serialize(std::ostream& os) const {
} }
} }
type_id typeCount = type_id(m_types.cend() - m_types.cbegin());
detail::serialize(os, typeCount);
for (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::Reference: throw std::runtime_error("reference type serialization is unimplemented");
case type_t::List: detail::serialize(os, type->list.id()); break;
case type_t::Import: {
detail::serialize(os, type->imp.mod);
detail::serialize(os, type->imp.type);
} break;
}
}
detail::serialize(os, std::uint64_t(m_bytecode.size())); detail::serialize(os, std::uint64_t(m_bytecode.size()));
return os.write(reinterpret_cast<const char*>(m_bytecode.data()), static_cast<std::streamsize>(m_bytecode.size())); return os.write(reinterpret_cast<const char*>(m_bytecode.data()), static_cast<std::streamsize>(m_bytecode.size()));
} }