refactor: improve thing types

Change thing_t enumeration to thing_type structure.

Closes: #30
This commit is contained in:
2026-06-29 20:34:42 +02:00
parent 8466902281
commit 6ab9254746
5 changed files with 121 additions and 105 deletions
+2 -8
View File
@@ -67,14 +67,8 @@ int main(void) {
furvmMod->serialize(file); furvmMod->serialize(file);
file.close(); file.close();
furvmMod->set_native_function("print", [](furvm::executor& executor) { furvmMod->set_native_function("print",
furvm::thing_h thing = executor.load_thing(0); [](furvm::executor& executor) { std::cout << executor.load_thing(0)->get<furvm::int_t>() << '\n'; });
switch (thing->type()) {
case furvm::thing_t::Int32: {
std::cout << thing->int32() << '\n';
} break;
}
});
furvm::executor_h executor = context->emplace_executor(context); furvm::executor_h executor = context->emplace_executor(context);
executor->push_frame(furvmMod, *furvmMod->function_at("main")); executor->push_frame(furvmMod, *furvmMod->function_at("main"));
+19 -2
View File
@@ -141,10 +141,27 @@ using mod_h = handle<mod, refcount_header<mod_id>>;
// thing.hpp // thing.hpp
/** /**
* @enum thing_t * @enum thing_type_t
* @brief Type of the thing's type.
*/
enum class thing_type_t : std::uint32_t;
/**
* @struct thing_type
* @brief Thing type. * @brief Thing type.
*/ */
enum class thing_t : std::uint8_t; struct thing_type;
/**
* @brief Thing type's identifier.
*/
using thing_type_id = std::uint32_t;
// TODO: Use generic header
/**
* @brief A handle to a thing type.
*/
using thing_type_h = handle<thing_type, refcount_header<thing_type_id>>;
/** /**
* @class bad_thing_access * @class bad_thing_access
+95 -84
View File
@@ -5,31 +5,34 @@
#include "furvm/exceptions.hpp" #include "furvm/exceptions.hpp"
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include <algorithm>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <functional>
#include <memory> #include <memory>
#include <new>
#include <stdexcept>
#include <utility> #include <utility>
#include <vector> #include <vector>
namespace furvm { namespace furvm {
enum class thing_t : std::uint8_t { enum class thing_type_t : std::uint32_t {
Int32, Primitive = 0,
}; };
struct thing_type {
thing_type_t type;
std::uint64_t value;
};
using int_t = std::int32_t; /**< A 4-byte integer. */
/** /**
* @brief Returns how many bytes a thing would take up. * @brief A 4-byte integer thing type.
*
* @param type Type of the thing.
* @return The byte count.
*/ */
static inline std::size_t thing_type_size(thing_t type) { static constexpr thing_type IntType = { thing_type_t::Primitive, sizeof(int_t) };
switch (type) {
case thing_t::Int32: return sizeof(std::int32_t);
default: return 0;
}
}
template <template <typename> typename Allocator> template <template <typename> typename Allocator>
class thing final { class thing final {
@@ -40,28 +43,37 @@ public:
/** /**
* @brief Constructs a thing. * @brief Constructs a thing.
* *
* @param type Type of the thing. * @param type Thing type.
* @param allocator Allocator for the thing's data. * @param allocator Allocator for the thing's data.
*/ */
thing(thing_t type, const allocator_type& allocator = {}) thing(const thing_type& type, const allocator_type& allocator = {})
: m_type(type), m_allocator(allocator) { : m_type(type), m_allocator(allocator) {
m_data = m_allocator.allocate(thing_type_size(type)); switch (type.type) {
case thing_type_t::Primitive: {
m_size = type.value;
} break;
}
m_size = (m_size + 3) & ~3; // NOTE: Align to 4 bytes
// TODO: Account for alignment
m_data = m_allocator.allocate(m_size);
} }
/** /**
* @brief Destructs a thing. * @brief Destructs a thing.
*/ */
~thing() { ~thing() {
if (m_data != nullptr) m_allocator.deallocate(m_data, thing_type_size(thing_t::Int32)); if (m_data != nullptr) m_allocator.deallocate(m_data, m_size);
} }
/** /**
* @brief Move constructor. * @brief Move constructor.
*/ */
thing(thing&& other) noexcept thing(thing&& other) noexcept
: m_type(other.m_type), m_data(other.m_data), m_allocator(std::move(other.m_allocator)) { : m_type(other.m_type), m_data(other.m_data), m_size(other.m_size), m_allocator(std::move(other.m_allocator)) {
other.m_type = {}; other.m_type = {};
other.m_data = nullptr; other.m_data = nullptr;
other.m_size = 0;
} }
/** /**
@@ -74,6 +86,7 @@ public:
m_allocator = std::move(other.m_allocator); m_allocator = std::move(other.m_allocator);
other.m_type = {}; other.m_type = {};
other.m_data = nullptr; other.m_data = nullptr;
other.m_size = 0;
return *this; return *this;
} }
@@ -86,10 +99,10 @@ public:
* @return A clone of this thing. * @return A clone of this thing.
*/ */
thing clone() const { thing clone() const {
class thing res(m_type, m_allocator); thing res(m_type, m_allocator);
switch (m_type) { switch (m_type.type) {
default: { case thing_type_t::Primitive: {
std::memcpy(res.m_data, m_data, thing_type_size(m_type)); std::memcpy(res.m_data, m_data, m_size);
} break; } break;
} }
return std::move(res); return std::move(res);
@@ -103,23 +116,39 @@ public:
constexpr auto type() const { return m_type; } constexpr auto type() const { return m_type; }
public: public:
/** /**
* @brief Returns the thing's int32 value. * @brief Returns a raw data pointer.
*
* @return The data pointer.
*/
void* raw() { return m_data; }
/**
* @brief Returns a raw data pointer.
*
* @return The data pointer.
*/
const void* raw() const { return m_data; }
public:
/**
* @brief Returns the thing's value.
* *
* @return The value. * @return The value.
*/ */
std::int32_t& int32() { template <typename T>
if (m_type != thing_t::Int32) throw bad_thing_access(); T& get() {
return *reinterpret_cast<std::int32_t*>(m_data); if (m_type.type == thing_type_t::Primitive && m_type.value != sizeof(T)) throw bad_thing_access();
return *std::launder(reinterpret_cast<T*>(m_data));
} }
/** /**
* @brief Returns the thing's int32 value. * @brief Returns the thing's value.
* *
* @return The value. * @return The value.
*/ */
const std::int32_t& int32() const { template <typename T>
if (m_type != thing_t::Int32) throw bad_thing_access(); const T& get() const {
return *reinterpret_cast<std::int32_t*>(m_data); if (m_type.type == thing_type_t::Primitive && m_type.value != sizeof(T)) throw bad_thing_access();
return *std::launder(reinterpret_cast<const T*>(m_data));
} }
public: public:
/** /**
@@ -128,11 +157,7 @@ public:
* @param rhs Thing to sum with this thing (right-hand-side). * @param rhs Thing to sum with this thing (right-hand-side).
* @return The sum. * @return The sum.
*/ */
thing add(const thing& rhs) const { thing add(const thing& rhs) const { return binary_op(rhs, std::plus<>{}); }
thing res(m_type, m_allocator);
res.int32() = int32() + rhs.int32();
return res;
}
/** /**
* @brief Returns a difference of two things. * @brief Returns a difference of two things.
@@ -140,11 +165,7 @@ public:
* @param rhs Thing to subtract from this thing (right-hand-side). * @param rhs Thing to subtract from this thing (right-hand-side).
* @return The sum. * @return The sum.
*/ */
thing sub(const thing& rhs) const { thing sub(const thing& rhs) const { return binary_op(rhs, std::minus<>{}); }
thing res(m_type, m_allocator);
res.int32() = int32() - rhs.int32();
return res;
}
/** /**
* @brief Returns a product of two things. * @brief Returns a product of two things.
@@ -152,11 +173,7 @@ public:
* @param rhs Thing to multiply with this thing (right-hand-side). * @param rhs Thing to multiply with this thing (right-hand-side).
* @return The product. * @return The product.
*/ */
thing mul(const thing& rhs) const { thing mul(const thing& rhs) const { return binary_op(rhs, std::multiplies<>{}); }
thing res(m_type, m_allocator);
res.int32() = int32() * rhs.int32();
return res;
}
/** /**
* @brief Returns a quotient of two things. * @brief Returns a quotient of two things.
@@ -164,11 +181,7 @@ public:
* @param rhs Thing to divide this thing by (right-hand-side). * @param rhs Thing to divide this thing by (right-hand-side).
* @return The quotient. * @return The quotient.
*/ */
thing div(const thing& rhs) const { thing div(const thing& rhs) const { return binary_op(rhs, std::divides<>{}); }
thing res(m_type, m_allocator);
res.int32() = int32() / rhs.int32();
return res;
}
/** /**
* @brief Returns a remainder of two things. * @brief Returns a remainder of two things.
@@ -176,11 +189,7 @@ public:
* @param rhs Thing to divide this thing by (right-hand-side). * @param rhs Thing to divide this thing by (right-hand-side).
* @return The remainder. * @return The remainder.
*/ */
thing mod(const thing& rhs) const { thing mod(const thing& rhs) const { return binary_op(rhs, std::modulus<>{}); }
thing res(m_type, m_allocator);
res.int32() = int32() % rhs.int32();
return res;
}
/** /**
* @brief Compares two things for equality. * @brief Compares two things for equality.
@@ -188,11 +197,7 @@ public:
* @param rhs Thing to compare this thing with (right-hand-side). * @param rhs Thing to compare this thing with (right-hand-side).
* @return A boolean result of the comparison in a thing form. * @return A boolean result of the comparison in a thing form.
*/ */
thing equals(const thing& rhs) const { thing equals(const thing& rhs) const { return binary_op(rhs, std::equal_to<>{}); }
thing res(m_type, m_allocator);
res.int32() = int32() == rhs.int32();
return res;
}
/** /**
* @brief Compares two things for inequality. * @brief Compares two things for inequality.
@@ -200,11 +205,7 @@ public:
* @param rhs Thing to compare this thing with (right-hand-side). * @param rhs Thing to compare this thing with (right-hand-side).
* @return A boolean result of the comparison in a thing form. * @return A boolean result of the comparison in a thing form.
*/ */
thing not_equals(const thing& rhs) const { thing not_equals(const thing& rhs) const { return binary_op(rhs, std::not_equal_to<>{}); }
thing res(m_type, m_allocator);
res.int32() = int32() != rhs.int32();
return res;
}
/** /**
* @brief Compares if this thing is less than an another thing. * @brief Compares if this thing is less than an another thing.
@@ -212,11 +213,7 @@ public:
* @param rhs The another thing. * @param rhs The another thing.
* @return A boolean result of the comparison in a thing form. * @return A boolean result of the comparison in a thing form.
*/ */
thing less_than(const thing& rhs) const { thing less_than(const thing& rhs) const { return binary_op(rhs, std::less<>{}); }
thing res(m_type, m_allocator);
res.int32() = int32() < rhs.int32();
return res;
}
/** /**
* @brief Compares if this thing is greater than an another thing. * @brief Compares if this thing is greater than an another thing.
@@ -224,11 +221,7 @@ public:
* @param rhs The another thing. * @param rhs The another thing.
* @return A boolean result of the comparison in a thing form. * @return A boolean result of the comparison in a thing form.
*/ */
thing greater_than(const thing& rhs) const { thing greater_than(const thing& rhs) const { return binary_op(rhs, std::greater<>{}); }
thing res(m_type, m_allocator);
res.int32() = int32() > rhs.int32();
return res;
}
/** /**
* @brief Compares if this thing is less than or equal to an another thing. * @brief Compares if this thing is less than or equal to an another thing.
@@ -236,11 +229,7 @@ public:
* @param rhs The another thing. * @param rhs The another thing.
* @return A boolean result of the comparison in a thing form. * @return A boolean result of the comparison in a thing form.
*/ */
thing less_equals(const thing& rhs) const { thing less_equals(const thing& rhs) const { return binary_op(rhs, std::less_equal<>{}); }
thing res(m_type, m_allocator);
res.int32() = int32() <= rhs.int32();
return res;
}
/** /**
* @brief Compares if this thing is greater than or equal to an another thing. * @brief Compares if this thing is greater than or equal to an another thing.
@@ -248,13 +237,35 @@ public:
* @param rhs The another thing. * @param rhs The another thing.
* @return A boolean result of the comparison in a thing form. * @return A boolean result of the comparison in a thing form.
*/ */
thing greater_equals(const thing& rhs) const { thing greater_equals(const thing& rhs) const { return binary_op(rhs, std::greater_equal<>{}); }
thing res(m_type, m_allocator); private:
res.int32() = int32() >= rhs.int32(); // TODO: Change to `to_long` after introducing long type.
return res; int_t to_int() const {
if (m_type.type != thing_type_t::Primitive) throw bad_thing_access();
switch (m_type.value) {
case sizeof(int_t): return get<int_t>();
default: throw std::runtime_error("unreachable");
}
}
template <typename Op>
thing binary_op(const thing& rhs, const Op& op) const {
if (type().type == thing_type_t::Primitive && rhs.type().type == thing_type_t::Primitive) {
std::size_t size = std::max(type().value, rhs.type().value);
thing res(thing_type{ thing_type_t::Primitive, size }, m_allocator);
switch (size) {
case sizeof(int_t): res.get<int_t>() = op(to_int(), rhs.to_int()); break;
default: throw std::runtime_error("unreachable");
}
return std::move(res);
}
throw std::runtime_error("unexpected operation");
} }
private: private:
thing_t m_type{}; thing_type m_type;
std::size_t m_size;
std::byte* m_data; std::byte* m_data;
allocator_type m_allocator; allocator_type m_allocator;
+2 -2
View File
@@ -91,7 +91,7 @@ void executor::step() {
switch (instr) { switch (instr) {
case instruction_t::NoOperation: break; case instruction_t::NoOperation: break;
case instruction_t::PushB2I: { case instruction_t::PushB2I: {
push_thing({ thing_t::Int32, m_context->thing_alloc() })->int32() = frame.mod->byte(frame.position++); push_thing({ IntType, m_context->thing_alloc() })->get<int_t>() = frame.mod->byte(frame.position++);
} break; } break;
case instruction_t::Drop: { case instruction_t::Drop: {
pop_thing(); pop_thing();
@@ -181,7 +181,7 @@ void executor::step() {
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 += (std::int8_t)offset; if (cond->get<int_t>() != 0) frame.position += (std::int8_t)offset;
} break; } break;
case instruction_t::Return: { case instruction_t::Return: {
pop_frame(); pop_frame();
+2 -8
View File
@@ -24,14 +24,8 @@ int main(void) {
furvm::mod_h furlangModule = context->emplace_module("furlang"); furvm::mod_h furlangModule = context->emplace_module("furlang");
furvm::function_h printFunc = furlangModule->emplace_function("print", 1, "print"); furvm::function_h printFunc = furlangModule->emplace_function("print", 1, "print");
furlangModule->set_native_function("print", [](furvm::executor& executor) { furlangModule->set_native_function("print",
furvm::thing_h thing = executor.load_thing(0); [](furvm::executor& executor) { std::cout << executor.load_thing(0)->get<furvm::int_t>() << '\n'; });
switch (thing->type()) {
case furvm::thing_t::Int32: {
std::cout << thing->int32() << '\n';
} break;
}
});
furvm::mod_h 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());
furvm::function_h mainFunc = mainModule->emplace_function("main", 0, 0); furvm::function_h mainFunc = mainModule->emplace_function("main", 0, 0);