Files

1021 lines
34 KiB
C++

#ifndef FURVM_THING_HPP
#define FURVM_THING_HPP
#include "furlang/arena.hpp"
#include "furlang/utility/hash.hpp"
#include "furvm/exceptions.hpp"
#include "furvm/fwd.hpp"
#include "furvm/types.hpp"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstring>
#include <functional>
#include <iterator>
#include <new>
#include <stdexcept>
#include <type_traits>
#include <unordered_map>
#include <utility>
namespace furvm {
namespace detail {
struct thing_type_hash {
std::size_t operator()(const thing_type& type) const {
std::size_t seed = std::hash<decltype(type.type)>{}(type.type);
switch (type.type) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64:
case thing_type::String: return seed;
case thing_type::Ptr:
case thing_type::Ref: return furlang::utility::hash_combine(seed, thing_type_hash{}(*type.value.typeRef));
case thing_type::Array:
seed = furlang::utility::hash_combine(seed, thing_type_hash{}(*type.value.array.type));
seed = furlang::utility::hash_combine(seed,
std::hash<decltype(type.value.array.size)>{}(type.value.array.size));
return seed;
case thing_type::Slice: return furlang::utility::hash_combine(seed, thing_type_hash{}(*type.value.slice.type));
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
};
} // namespace detail
class thing_type_store {
public:
thing_type* insert(thing_type& type) {
if (auto it = m_typeMap.find(type); it != m_typeMap.end()) return m_map[type.id = it->second];
if (type.id == thing_type::INVALID_ID) type.id = m_counter++;
thing_type* ptr = m_arena.allocate<thing_type>(type);
m_map[type.id] = ptr;
m_typeMap[type] = type.id;
return ptr;
}
thing_type* insert(const thing_type& type) {
if (auto it = m_typeMap.find(type); it != m_typeMap.end()) return m_map[it->second];
thing_type_id id = m_counter++;
thing_type* ptr = m_arena.allocate<thing_type>(type);
m_map[id] = ptr;
m_typeMap[type] = id;
return ptr;
}
thing_type* at(thing_type_id id) const {
if (auto it = m_map.find(id); it != m_map.end()) return it->second;
return nullptr;
}
private:
furlang::arena m_arena;
std::unordered_map<thing_type_id, thing_type*> m_map;
std::unordered_map<thing_type, thing_type_id, detail::thing_type_hash> m_typeMap;
thing_type_id m_counter = 0;
};
template <template <typename> typename Allocator>
class thing final {
friend class executor;
template <template <typename> class Other>
friend class thing;
public:
using allocator_type = Allocator<std::byte>; /**< Allocator type. */
public:
struct string {
std::size_t size;
u8* data;
static bool matches(const thing_type& type) { return type.type == thing_type::String; }
};
struct dynamic_array {
std::size_t size;
std::byte* data;
static bool matches(const thing_type& type) {
return type.type == thing_type::Array && type.value.array.size == 0;
}
};
struct slice {
std::size_t length;
std::byte* data;
static bool matches(const thing_type& type) { return type.type == thing_type::Slice; }
};
struct header {
thing_type type;
};
private:
template <bool Const>
class generic_iterator {
using owner_type = std::conditional_t<Const, const thing, thing>;
using data_type = std::byte;
public:
using difference_type = std::ptrdiff_t;
using value_type = thing;
using pointer = std::conditional_t<Const, const value_type*, value_type*>;
using reference = std::conditional_t<Const, const value_type&, value_type&>;
using iterator_category = std::random_access_iterator_tag;
public:
generic_iterator() = default;
generic_iterator(owner_type* owner, data_type* ptr)
: m_owner(owner), m_ptr(ptr) {
fill();
}
public:
reference operator*() { return m_thing; }
value_type operator*() const {
thing thing = { m_thing.type() };
thing.assign(m_thing);
return thing;
}
reference operator[](difference_type n) { return *(*this + n); }
pointer operator->() { return &m_thing; }
generic_iterator& operator+=(difference_type n) {
m_ptr += n * step_size();
fill();
return *this;
}
generic_iterator& operator-=(difference_type n) {
m_ptr -= n * step_size();
fill();
return *this;
}
generic_iterator operator+(difference_type n) const { return { m_owner, m_ptr + (n * step_size()) }; }
friend generic_iterator operator+(difference_type n, const generic_iterator& it) {
return { it.m_owner, it.m_ptr + (n * it.step_size()) };
}
generic_iterator operator-(difference_type n) const { return { m_owner, m_ptr - (n * step_size()) }; }
difference_type operator-(const generic_iterator& other) const { return (m_ptr - other.m_ptr) / step_size(); }
generic_iterator& operator++() {
m_ptr += step_size();
fill();
return *this;
}
generic_iterator operator++(int) { return { m_owner, m_ptr + step_size() }; }
generic_iterator& operator--() {
m_ptr -= step_size();
fill();
return *this;
}
generic_iterator operator--(int) { return { m_owner, m_ptr - step_size() }; }
bool operator==(const generic_iterator& other) const {
return m_owner == other.m_owner && m_ptr == other.m_ptr;
}
bool operator!=(const generic_iterator& other) const {
return m_owner != other.m_owner || m_ptr != other.m_ptr;
}
bool operator<(const generic_iterator& other) const { return other.m_ptr < m_ptr; }
bool operator>(const generic_iterator& other) const { return other.m_ptr > m_ptr; }
bool operator<=(const generic_iterator& other) const { return other.m_ptr <= m_ptr; }
bool operator>=(const generic_iterator& other) const { return other.m_ptr >= m_ptr; }
private:
difference_type step_size() const { return thing::compute_size_na(m_owner->inner_type()); }
void fill() {
m_thing.m_reference = true;
m_thing.m_data = m_ptr;
m_thing.m_type = &m_owner->inner_type();
m_thing.m_size = compute_size_na(m_owner->inner_type());
}
private:
owner_type* m_owner = nullptr;
data_type* m_ptr = nullptr;
thing m_thing;
};
using iterator = generic_iterator<false>;
using const_iterator = generic_iterator<true>;
public:
thing(const allocator_type& allocator = {})
: m_allocator(allocator) {}
/**
* @brief Constructs a thing.
*
* @param type Thing type.
* @param allocator Allocator for the thing's data.
*/
thing(const thing_type& type, const allocator_type& allocator = {})
: m_size(compute_size_na(type)), m_allocator(allocator) {
assert(type.type != thing_type::Ref);
allocate(type);
}
/* NOTE: Furvm forbids allocating references on the heap.
* This limitation is required for the current implementation of references.
* Essentialy, references are special things that point directly to other thing's data.
* The distinction between a reference and the owner is stored inside the reference's
* thing instance, which makes it impossible to represent them on the heap; however,
* the same does not apply to the executor's stack, nor it should apply to compound
* types in the future.
*
* TODO: Reword the note above.
*/
template <template <typename> class Other = Allocator>
static thing make_reference(const thing<Other>& owner, const allocator_type& allocator = {}) {
thing ref = { allocator };
ref.m_reference = true;
ref.m_data = owner.m_data;
ref.m_type = owner.m_type;
ref.m_size = owner.m_size;
return ref;
}
/**
* @brief Destructs a thing.
*/
~thing() { free(); }
/**
* @brief Move constructor.
*/
thing(thing&& other) noexcept
: m_reference(other.m_reference),
m_type(other.m_type),
m_size(other.m_size),
m_data(other.m_data),
m_allocator(other.m_allocator) {
other.m_type = nullptr;
other.m_data = nullptr;
other.m_size = 0;
}
/**
* @brief Move constructor.
*/
thing& operator=(thing&& other) noexcept {
if (this == &other) return *this;
free();
m_reference = other.m_reference;
m_type = other.m_type;
m_size = other.m_size;
m_data = other.m_data;
m_allocator = std::move(other.m_allocator);
other.m_type = nullptr;
other.m_data = nullptr;
other.m_size = 0;
return *this;
}
thing(const thing& other)
: m_reference(other.m_reference), m_size(other.m_size), m_allocator(other.m_allocator) {
if (m_reference) {
m_type = other.m_type;
m_data = other.m_data;
return;
}
allocate(other.type());
other.copy(*this);
}
thing& operator=(const thing& other) {
if (this == &other) return *this;
free();
m_reference = other.m_reference;
m_size = other.m_size;
if (m_reference) {
m_type = other.m_type;
m_data = other.m_data;
return *this;
}
allocate(other.type());
other.copy(*this);
return *this;
}
template <template <typename> class Other>
thing(const thing<Other>& other)
: m_reference(other.m_reference), m_size(other.m_size) {
if (m_reference) {
m_type = other.m_type;
m_data = other.m_data;
return;
}
allocate(other.type());
other.copy(*this);
}
template <template <typename> class Other>
thing& operator=(const thing<Other>& other) {
if (this == &other) return *this;
free();
m_reference = other.m_reference;
m_size = other.m_size;
if (m_reference) {
m_type = other.m_type;
m_data = other.m_data;
return *this;
}
allocate(other.type());
other.copy(*this);
return *this;
}
public:
/**
* @brief Returns a clone of the thing.
*
* @return A clone of this thing.
*/
thing clone() const {
thing res(m_type, m_allocator);
copy(res);
return res;
}
private:
template <template <typename> class Other>
void copy(thing<Other>& dst) const {
switch (m_type->type) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64:
case thing_type::Ptr:
case thing_type::Slice: std::memcpy(dst.m_data, m_data, m_size); return;
case thing_type::Array: copy_list(*m_type, dst.m_data, m_data); return;
case thing_type::Ref: // TODO: Implement arrays of references (I think they're possible).
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
public:
/**
* @brief Returns the thing's type.
*
* @return The type.
*/
constexpr thing_type type() const { return *m_type; }
/**
* @brief Checks if the thing is of a specified type.
*
* Compares the true type.
*
* @param type Type to compare.
* @return true if the types match.
*/
constexpr bool is(enum thing_type::type type) const {
if (type == thing_type::Ref) return m_reference;
return m_type->type == type;
}
constexpr bool is_reference() const { return m_reference; }
public:
/**
* @brief Returns a raw data pointer.
*
* @return The data pointer.
*/
std::byte* raw() { return m_data; }
/**
* @brief Returns a raw data pointer.
*
* @return The data pointer.
*/
const std::byte* raw() const { return m_data; }
public:
/**
* @brief Returns the thing's value.
*
* @return The value.
*/
template <typename T>
T& get() {
if (!detail::thing_traits<T>{}(*m_type)) throw bad_thing_access();
return *std::launder(reinterpret_cast<T*>(m_data));
}
/**
* @brief Returns the thing's value.
*
* @return The value.
*/
template <typename T>
const T& get() const {
if (!detail::thing_traits<T>{}(*m_type)) throw bad_thing_access();
return *std::launder(reinterpret_cast<const T*>(m_data));
}
template <typename T>
void set(T&& newValue) {
if (!detail::thing_traits<T>{}(*m_type)) throw bad_thing_access();
*std::launder(reinterpret_cast<T*>(m_data)) = std::forward<T>(newValue);
}
public:
/**
* @brief Returns a sum of two things.
*
* @param rhs Thing to sum with this thing (right-hand-side).
* @return The sum.
*/
thing add(const thing& rhs) const { return binary_op(rhs, std::plus<>{}); }
/**
* @brief Returns a difference of two things.
*
* @param rhs Thing to subtract from this thing (right-hand-side).
* @return The sum.
*/
thing sub(const thing& rhs) const { return binary_op(rhs, std::minus<>{}); }
/**
* @brief Returns a product of two things.
*
* @param rhs Thing to multiply with this thing (right-hand-side).
* @return The product.
*/
thing mul(const thing& rhs) const { return binary_op(rhs, std::multiplies<>{}); }
/**
* @brief Returns a quotient of two things.
*
* @param rhs Thing to divide this thing by (right-hand-side).
* @return The quotient.
*/
thing div(const thing& rhs) const { return binary_op(rhs, std::divides<>{}); }
/**
* @brief Returns a remainder of two things.
*
* @param rhs Thing to divide this thing by (right-hand-side).
* @return The remainder.
*/
thing mod(const thing& rhs) const { return binary_op(rhs, std::modulus<>{}); }
/**
* @brief Compares two things for equality.
*
* @param rhs Thing to compare this thing with (right-hand-side).
* @return A boolean result of the comparison in a thing form.
*/
thing equals(const thing& rhs) const { return binary_op(rhs, std::equal_to<>{}); }
/**
* @brief Compares two things for inequality.
*
* @param rhs Thing to compare this thing with (right-hand-side).
* @return A boolean result of the comparison in a thing form.
*/
thing not_equals(const thing& rhs) const { return binary_op(rhs, std::not_equal_to<>{}); }
/**
* @brief Compares if this thing is less than an another thing.
*
* @param rhs The another thing.
* @return A boolean result of the comparison in a thing form.
*/
thing less_than(const thing& rhs) const { return binary_op(rhs, std::less<>{}); }
/**
* @brief Compares if this thing is greater than an another thing.
*
* @param rhs The another thing.
* @return A boolean result of the comparison in a thing form.
*/
thing greater_than(const thing& rhs) const { return binary_op(rhs, std::greater<>{}); }
/**
* @brief Compares if this thing is less than or equal to an another thing.
*
* @param rhs The another thing.
* @return A boolean result of the comparison in a thing form.
*/
thing less_equals(const thing& rhs) const { return binary_op(rhs, std::less_equal<>{}); }
/**
* @brief Compares if this thing is greater than or equal to an another thing.
*
* @param rhs The another thing.
* @return A boolean result of the comparison in a thing form.
*/
thing greater_equals(const thing& rhs) const { return binary_op(rhs, std::greater_equal<>{}); }
public:
/**
* @brief Returns a largest integer value of the thing.
*
* @return The integer value.
*/
s64 integer() const {
switch (type().type) {
case thing_type::S8: return get<s8>();
case thing_type::S16: return get<s16>();
case thing_type::S32: return get<s32>();
case thing_type::S64: return get<s64>();
case thing_type::U8: return get<u8>();
case thing_type::U16: return get<u16>();
case thing_type::U32: return get<u32>();
case thing_type::U64: return get<u64>();
default: throw std::runtime_error("unreachable");
}
}
void resize(u64 newSize) {
switch (type().type) {
case thing_type::Array: {
if (type().value.array.size > 0) throw std::runtime_error("cannot resize a static array");
auto& array = get<dynamic_array>();
if (newSize < 0 || newSize == array.size) return;
std::size_t innerSize = compute_size_na(*type().value.array.type);
std::byte* newData = new std::byte[innerSize * newSize];
std::memcpy(newData, array.data, innerSize * std::min(static_cast<u64>(array.size), newSize));
array.size = newSize;
delete[] array.data;
array.data = newData;
} break;
case thing_type::String: {
auto& string = get<struct string>();
if (newSize < 0 || newSize == string.size) return;
u8* newData = new u8[newSize];
std::memcpy(newData, string.data, std::min(static_cast<u64>(string.size), newSize));
string.size = newSize;
delete[] string.data;
string.data = newData;
} break;
default: throw bad_thing_access();
}
}
thing at(u64 index) const {
thing ref = { m_allocator };
ref.m_reference = true;
ref.m_size = compute_size_na(*(ref.m_type = &inner_type()));
switch (type().type) {
case thing_type::String: {
auto& string = get<struct string>();
if (index < 0 || index >= string.size) throw std::out_of_range("index out of range");
ref.m_data = reinterpret_cast<std::byte*>(string.data + index);
return ref;
}
case thing_type::Array: {
if (type().value.array.size == 0) {
auto& array = get<dynamic_array>();
if (index < 0 || index >= array.size) throw std::out_of_range("index out of range");
ref.m_data = array.data + (index * ref.m_size);
return ref;
}
if (index < 0 || index >= type().value.array.size) throw std::out_of_range("index out of range");
ref.m_data = m_data + (index * ref.m_size);
return ref;
}
case thing_type::Slice: {
const auto& slice = get<struct slice>();
if (index < 0 || index >= slice.length) throw std::out_of_range("index out of range");
ref.m_data = slice.data + (index * ref.m_size);
return ref;
}
default: throw bad_thing_access();
}
}
thing slice(u64 begin, u64 len) const {
auto& inner = inner_type();
thing_type sliceType;
sliceType.type = thing_type::Slice;
sliceType.value.slice.type = &inner;
thing slice = { sliceType, m_allocator };
auto& data = slice.get<struct slice>();
if (begin >= length()) throw std::out_of_range("begin index out of range");
len = std::min(len, length() - begin);
switch (type().type) {
case thing_type::String: {
data.data = reinterpret_cast<std::byte*>(get<struct string>().data);
} break;
case thing_type::Array: {
data.data = ((type().value.array.size != 0) ? m_data : get<dynamic_array>().data);
} break;
case thing_type::Slice: {
data.data = get<struct slice>().data;
} break;
default: throw bad_thing_access();
}
data.data += (compute_size_na(inner) * begin);
data.length = len;
return slice;
}
u64 length() const {
switch (type().type) {
case thing_type::String: return get<struct string>().size;
case thing_type::Array:
return type().value.array.size == 0 ? get<dynamic_array>().size : type().value.array.size;
case thing_type::Slice: return get<struct slice>().length;
default: throw bad_thing_access();
}
}
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
T cast_to() const {
return visit_primitive([](auto value) { return static_cast<T>(value); });
}
/**
* @brief Self-explainatory.
*
* TODO: Document
*/
void assign(thing&& thing) {
class thing rhs = std::move(thing);
if (type() != rhs.type()) throw std::runtime_error("thing type mismatch");
// TODO: Move this to another function
switch (type().type) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64: std::memcpy(m_data, rhs.m_data, m_size); return;
case thing_type::String:
case thing_type::Ptr:
case thing_type::Ref:
case thing_type::Array:
case thing_type::Slice: throw std::runtime_error("unimplemented");
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
void assign(const thing& rhs) {
if (type() != rhs.type()) throw std::runtime_error("thing type mismatch");
// TODO: Move this to another function
switch (type().type) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64: std::memcpy(m_data, rhs.m_data, m_size); return;
case thing_type::Ptr:
case thing_type::Ref:
case thing_type::Array:
case thing_type::Slice: throw std::runtime_error("unimplemented");
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
template <typename T>
void assign(const T& value) {
if constexpr (detail::cassignable_to_thing<T, thing>::value) {
detail::thing_traits<T>::assign_to(*this, value);
} else {
get<T>() = value;
}
}
template <typename T>
void assign(T&& value) { // NOLINT
if constexpr (detail::massignable_to_thing<T, thing>::value) {
detail::thing_traits<T>::assign_to(*this, std::move(value)); // NOLINT
} else {
get<T>() = std::move(value); // NOLINT
}
}
public:
iterator begin() {
switch (type().type) {
case thing_type::String: return { this, reinterpret_cast<std::byte*>(get<struct string>().data) };
case thing_type::Array: return { this, (type().value.array.size == 0) ? get<dynamic_array>().data : m_data };
case thing_type::Slice: return { this, get<struct slice>().data };
default: throw bad_thing_access();
}
}
iterator end() { return begin() + length(); }
const_iterator cbegin() const {
switch (type().type) {
case thing_type::String: return { this, reinterpret_cast<std::byte*>(get<struct string>().data) };
case thing_type::Array: return { this, (type().value.array.size == 0) ? get<dynamic_array>().data : m_data };
case thing_type::Slice: return { this, get<struct slice>().data };
default: throw bad_thing_access();
}
}
const_iterator cend() const { return cbegin() + length(); }
const_iterator begin() const { return cbegin(); }
const_iterator end() const { return cend(); }
private:
static void copy_list(const thing_type& arrayType, void* dst, const void* src) {
if (arrayType.type != thing_type::Array || arrayType.value.array.type == nullptr)
throw std::runtime_error("invalid type");
const auto& innerType = *arrayType.value.array.type;
std::size_t elementSize = compute_size_na(innerType);
std::size_t size = 0;
if (arrayType.value.array.size == 0) {
const dynamic_array& srcDynArr = *std::launder(reinterpret_cast<const dynamic_array*>(src));
dynamic_array& dstDynArr = *std::launder(reinterpret_cast<dynamic_array*>(dst));
size = dstDynArr.size = srcDynArr.size;
if (dstDynArr.size < 0) {
dstDynArr.data = nullptr;
return;
}
src = srcDynArr.data;
dst = dstDynArr.data = new std::byte[dstDynArr.size * elementSize];
} else {
size = arrayType.value.array.size;
}
switch (innerType.type) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64:
case thing_type::String:
case thing_type::Ptr:
case thing_type::Ref:
case thing_type::Slice: std::memcpy(dst, src, size * elementSize); return;
case thing_type::Array:
for (std::size_t i = 0; i < size; ++i) {
copy_list(*innerType.value.array.type,
reinterpret_cast<std::byte*>(dst) + (i * elementSize),
reinterpret_cast<const std::byte*>(src) + (i * elementSize));
}
return;
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
private:
static std::size_t compute_size_na(const thing_type& type) {
switch (type.type) {
case thing_type::S8: return sizeof(s8);
case thing_type::S16: return sizeof(s16);
case thing_type::S32: return sizeof(s32);
case thing_type::S64: return sizeof(s64);
case thing_type::U8: return sizeof(u8);
case thing_type::U16: return sizeof(u16);
case thing_type::U32: return sizeof(u32);
case thing_type::U64: return sizeof(u64);
case thing_type::String: return sizeof(string);
case thing_type::Ptr: return sizeof(void*);
case thing_type::Array:
return type.value.array.size == 0 ? sizeof(dynamic_array)
: compute_size_na(*type.value.array.type) * type.value.array.size;
case thing_type::Slice: return sizeof(struct slice);
case thing_type::Ref:
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
// NOTE: Align to 4 bytes
static std::size_t compute_size(const thing_type& type) { return (compute_size_na(type) + 3) & ~3; }
private:
template <typename Func>
decltype(auto) visit_primitive(Func&& func) const {
switch (type().type) {
case thing_type::S8: return std::forward<Func>(func)(get<s8>());
case thing_type::S16: return std::forward<Func>(func)(get<s16>());
case thing_type::S32: return std::forward<Func>(func)(get<s32>());
case thing_type::S64: return std::forward<Func>(func)(get<s64>());
case thing_type::U8: return std::forward<Func>(func)(get<u8>());
case thing_type::U16: return std::forward<Func>(func)(get<u16>());
case thing_type::U32: return std::forward<Func>(func)(get<u32>());
case thing_type::U64: return std::forward<Func>(func)(get<u64>());
default: throw bad_thing_access();
}
}
template <typename Op>
thing binary_op(const thing& rhs, const Op& op) const {
if (thing_type::is_primitive(type().type) && thing_type::is_primitive(type().type)) {
static constexpr enum thing_type::type promotions[8 * 8] = {
// S8
thing_type::S8,
thing_type::S16,
thing_type::S32,
thing_type::S64,
thing_type::S16,
thing_type::S16,
thing_type::S32,
thing_type::U64,
// S16
thing_type::S16,
thing_type::S16,
thing_type::S32,
thing_type::S64,
thing_type::S16,
thing_type::S16,
thing_type::S32,
thing_type::U64,
// S32
thing_type::S32,
thing_type::S32,
thing_type::S32,
thing_type::S64,
thing_type::S32,
thing_type::S32,
thing_type::U32,
thing_type::U64,
// S64
thing_type::S64,
thing_type::S64,
thing_type::S64,
thing_type::S64,
thing_type::S64,
thing_type::S64,
thing_type::S64,
thing_type::U64,
// U8
thing_type::S16,
thing_type::S16,
thing_type::S32,
thing_type::S64,
thing_type::U8,
thing_type::U16,
thing_type::U32,
thing_type::U64,
// U16
thing_type::S16,
thing_type::S16,
thing_type::S32,
thing_type::S64,
thing_type::U16,
thing_type::U16,
thing_type::U32,
thing_type::U64,
// U32
thing_type::S32,
thing_type::S32,
thing_type::U32,
thing_type::S64,
thing_type::U32,
thing_type::U32,
thing_type::U32,
thing_type::U64,
// U64
thing_type::U64,
thing_type::U64,
thing_type::U64,
thing_type::U64,
thing_type::U64,
thing_type::U64,
thing_type::U64,
thing_type::U64,
};
enum thing_type::type resultType = promotions[type().type + (rhs.type().type * 8)];
thing res = { thing_type{ resultType }, m_allocator };
switch (resultType) {
case thing_type::S8: res.get<s8>() = Op{}(cast_to<s8>(), rhs.cast_to<s8>()); return res;
case thing_type::S16: res.get<s16>() = Op{}(cast_to<s16>(), rhs.cast_to<s16>()); return res;
case thing_type::S32: res.get<s32>() = Op{}(cast_to<s32>(), rhs.cast_to<s32>()); return res;
case thing_type::S64: res.get<s64>() = Op{}(cast_to<s64>(), rhs.cast_to<s64>()); return res;
case thing_type::U8: res.get<u8>() = Op{}(cast_to<u8>(), rhs.cast_to<u8>()); return res;
case thing_type::U16: res.get<u16>() = Op{}(cast_to<u16>(), rhs.cast_to<u16>()); return res;
case thing_type::U32: res.get<u32>() = Op{}(cast_to<u32>(), rhs.cast_to<u32>()); return res;
case thing_type::U64: res.get<u64>() = Op{}(cast_to<u64>(), rhs.cast_to<u64>()); return res;
case thing_type::String:
case thing_type::Ptr: // TODO: Pointer arithmetics
case thing_type::Ref:
case thing_type::Array:
case thing_type::Slice:
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
throw std::runtime_error("unexpected operation");
}
private:
void allocate(const thing_type& type) {
m_data = m_allocator.allocate(sizeof(header) + compute_size(type));
header* hdr = reinterpret_cast<header*>(m_data);
hdr->type = type;
m_type = &hdr->type;
m_data += sizeof(header);
std::memset(m_data, 0, m_size);
}
void free() {
if (!m_reference && m_data != nullptr) m_allocator.deallocate(m_data - sizeof(header), m_size + sizeof(header));
m_data = nullptr;
m_type = nullptr;
}
thing_type& inner_type() const {
switch (type().type) {
case thing_type::String: {
static thing_type s_inner = { thing_type::U8 };
return s_inner;
}
case thing_type::Array: return *type().value.array.type;
case thing_type::Slice: return *type().value.slice.type;
default: throw bad_thing_access();
}
}
private:
// A flag indicating whether the thing instance owns the data, or not.
bool m_reference = false;
const thing_type* m_type = nullptr;
std::size_t m_size = 0;
std::byte* m_data = nullptr;
allocator_type m_allocator;
};
namespace detail {
template <>
struct thing_traits<std::string_view> {
template <template <typename...> class Allocator>
static void assign_to(thing<Allocator>& thing, const std::string_view& value) {
using Thing = furvm::thing<Allocator>;
auto& string = thing.template get<typename Thing::string>();
string.data = new furvm::u8[string.size = value.length()];
std::memcpy(string.data, value.data(), value.length());
}
};
template <>
struct thing_traits<std::string> {
template <template <typename...> class Allocator>
static void assign_to(thing<Allocator>& thing, const std::string& value) {
using Thing = furvm::thing<Allocator>;
auto& string = thing.template get<typename Thing::string>();
string.data = new furvm::u8[string.size = value.length()];
std::memcpy(string.data, value.data(), value.length());
}
};
} // namespace detail
} // namespace furvm
#endif // FURVM_THING_HPP