@@ -101,7 +101,7 @@ public:
|
||||
std::memcpy(res.m_data, m_data, m_size);
|
||||
} break;
|
||||
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;
|
||||
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();
|
||||
auto& list = get<list_t>();
|
||||
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,
|
||||
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;
|
||||
delete[] list.data;
|
||||
list.data = newData;
|
||||
@@ -287,7 +287,7 @@ public:
|
||||
if (m_type->t != type_t::List) throw bad_thing_access();
|
||||
auto& list = get<list_t>();
|
||||
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
|
||||
return std::move(res);
|
||||
}
|
||||
@@ -314,7 +314,7 @@ private:
|
||||
case type_t::Reference: std::memcpy(dst.data, src.data, size); break;
|
||||
case type_t::List:
|
||||
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*>(src.data)));
|
||||
}
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
#define FURVM_TYPE_HPP
|
||||
|
||||
#include "furvm/fwd.hpp"
|
||||
#include "furvm/handle.hpp" // IWYU pragma: keep
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
namespace furvm {
|
||||
|
||||
@@ -18,8 +17,8 @@ enum class type_t : std::uint32_t {
|
||||
};
|
||||
|
||||
using primitive_type = std::uint64_t;
|
||||
using reference_type = std::shared_ptr<type>;
|
||||
using list_type = std::shared_ptr<type>;
|
||||
using reference_type = type_p;
|
||||
using list_type = type_h;
|
||||
|
||||
struct import_type {
|
||||
mod_id mod;
|
||||
@@ -44,11 +43,8 @@ struct type {
|
||||
type(const reference_type& reference)
|
||||
: t(type_t::Reference), reference(reference) {}
|
||||
|
||||
static type make_list(const list_type& list) {
|
||||
type type(type_t::List);
|
||||
new (&type.list) list_type(list);
|
||||
return type;
|
||||
}
|
||||
type(const list_type& list)
|
||||
: t(type_t::List), list(list) {}
|
||||
|
||||
type(const import_type& imp)
|
||||
: t(type_t::Import), imp(imp) {}
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
#include "furvm/detail/serialization.hpp"
|
||||
#include "furvm/fwd.hpp"
|
||||
#include "furvm/type.hpp"
|
||||
|
||||
#include <ios>
|
||||
#include <stdexcept>
|
||||
|
||||
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()));
|
||||
return os.write(reinterpret_cast<const char*>(m_bytecode.data()), static_cast<std::streamsize>(m_bytecode.size()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user