forked from KPGPMC/furlang
refactor(furvm): refactor thing
A pretty sloppy thing class refactor. Refs: #12
This commit is contained in:
@@ -53,28 +53,12 @@ public:
|
|||||||
|
|
||||||
class thing final {
|
class thing final {
|
||||||
friend class executor;
|
friend class executor;
|
||||||
private:
|
|
||||||
/**
|
|
||||||
* @brief A private token for the private constructor.
|
|
||||||
*
|
|
||||||
* Also `rzecz` in Polish translates to `thing` I think.
|
|
||||||
*/
|
|
||||||
struct rzecz {
|
|
||||||
explicit rzecz() = default;
|
|
||||||
};
|
|
||||||
public:
|
public:
|
||||||
using nref_t = std::size_t; /**< Type of reference count. */
|
using nref_t = std::size_t; /**< Type of reference count. */
|
||||||
|
|
||||||
static constexpr thing_handle GENERATION_SIZE = 12; /**< Bit size of generation part in thing_handle. */
|
static constexpr thing_handle GENERATION_SIZE = 12; /**< Bit size of generation part in thing_handle. */
|
||||||
public:
|
public:
|
||||||
/**
|
thing(const context_p& context, thing_t type);
|
||||||
* @brief Private constructor.
|
|
||||||
*
|
|
||||||
* @param id
|
|
||||||
* @param type
|
|
||||||
* @param context
|
|
||||||
*/
|
|
||||||
thing(rzecz, thing_handle id, thing_t type, const context_p& context);
|
|
||||||
|
|
||||||
~thing();
|
~thing();
|
||||||
|
|
||||||
@@ -189,29 +173,6 @@ public:
|
|||||||
*/
|
*/
|
||||||
friend thing_p operator>=(const thing_p& lhs, const thing_p& rhs);
|
friend thing_p operator>=(const thing_p& lhs, const thing_p& rhs);
|
||||||
public:
|
public:
|
||||||
/**
|
|
||||||
* @brief Returns a new thing.
|
|
||||||
*
|
|
||||||
* @param context Furvm context.
|
|
||||||
* @param args Arguments to forward to the thing constructor.
|
|
||||||
* @return Shared pointer to the new thing.
|
|
||||||
*/
|
|
||||||
template <typename... Args,
|
|
||||||
typename = std::enable_if_t<std::is_constructible_v<thing, rzecz, thing_handle, Args..., const context_p&>>>
|
|
||||||
static thing_p create(const context_p& context, Args&&... args) {
|
|
||||||
thing_handle id = context->m_things.size();
|
|
||||||
if (!context->m_deadThings.empty()) {
|
|
||||||
id = context->m_deadThings.front();
|
|
||||||
context->m_deadThings.pop();
|
|
||||||
id += 1 << GENERATION_SIZE;
|
|
||||||
}
|
|
||||||
thing_handle idx = id & ((1ULL << ((sizeof(id) * 8) - GENERATION_SIZE)) - 1);
|
|
||||||
|
|
||||||
auto th = std::make_shared<thing>(rzecz{}, id, std::forward<Args>(args)..., context);
|
|
||||||
context->m_things.emplace(context->m_things.begin() + idx, th);
|
|
||||||
return std::move(th);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns a clone of the thing.
|
* @brief Returns a clone of the thing.
|
||||||
*
|
*
|
||||||
@@ -251,9 +212,8 @@ public:
|
|||||||
*/
|
*/
|
||||||
constexpr nref_t reference_count() const { return m_refCount; }
|
constexpr nref_t reference_count() const { return m_refCount; }
|
||||||
private:
|
private:
|
||||||
thing_handle m_id;
|
|
||||||
thing_t m_type;
|
|
||||||
context_p m_context;
|
context_p m_context;
|
||||||
|
thing_t m_type;
|
||||||
|
|
||||||
nref_t m_refCount = 0;
|
nref_t m_refCount = 0;
|
||||||
void* m_data = nullptr;
|
void* m_data = nullptr;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#include "furvm/thing.hpp"
|
#include "furvm/thing.hpp"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace furvm {
|
namespace furvm {
|
||||||
@@ -86,7 +87,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: {
|
||||||
auto thing = thing::create(m_context, thing_t::Int32);
|
auto thing = std::make_shared<class thing>(m_context, thing_t::Int32);
|
||||||
thing->int32() = frame.mod->byte(frame.position++);
|
thing->int32() = frame.mod->byte(frame.position++);
|
||||||
push_thing(std::move(thing));
|
push_thing(std::move(thing));
|
||||||
} break;
|
} break;
|
||||||
|
|||||||
@@ -27,9 +27,6 @@ int main(void) {
|
|||||||
auto mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end());
|
auto mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end());
|
||||||
mainModule->emplace_function("main", 0);
|
mainModule->emplace_function("main", 0);
|
||||||
|
|
||||||
std::ofstream file("./test.furm", std::ios::binary);
|
|
||||||
furvm::serializer::serialize_module(file, mainModule);
|
|
||||||
|
|
||||||
auto executor = context->emplace_executor(context);
|
auto executor = context->emplace_executor(context);
|
||||||
executor->push_frame(mainModule, 0);
|
executor->push_frame(mainModule, 0);
|
||||||
|
|
||||||
|
|||||||
+16
-23
@@ -17,8 +17,8 @@ std::size_t thing_type_size(thing_t type) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
thing::thing(rzecz, thing_handle id, thing_t type, const context_p& context)
|
thing::thing(const context_p& context, thing_t type)
|
||||||
: m_id(id), m_type(type), m_context(context) {
|
: m_context(context), m_type(type) {
|
||||||
std::size_t size = thing_type_size(type);
|
std::size_t size = thing_type_size(type);
|
||||||
std::byte* data = nullptr;
|
std::byte* data = nullptr;
|
||||||
if (!m_context->m_deadThingData.empty()) {
|
if (!m_context->m_deadThingData.empty()) {
|
||||||
@@ -56,12 +56,7 @@ thing::~thing() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
thing::thing(thing&& other) noexcept
|
thing::thing(thing&& other) noexcept
|
||||||
: m_id(other.m_id),
|
: m_context(std::move(other.m_context)), m_type(other.m_type), m_refCount(other.m_refCount), m_data(other.m_data) {
|
||||||
m_type(other.m_type),
|
|
||||||
m_context(std::move(other.m_context)),
|
|
||||||
m_refCount(other.m_refCount),
|
|
||||||
m_data(other.m_data) {
|
|
||||||
other.m_id = {};
|
|
||||||
other.m_type = {};
|
other.m_type = {};
|
||||||
other.m_refCount = {};
|
other.m_refCount = {};
|
||||||
other.m_data = nullptr;
|
other.m_data = nullptr;
|
||||||
@@ -69,13 +64,11 @@ thing::thing(thing&& other) noexcept
|
|||||||
|
|
||||||
thing& thing::operator=(thing&& other) noexcept {
|
thing& thing::operator=(thing&& other) noexcept {
|
||||||
if (this == &other) return *this;
|
if (this == &other) return *this;
|
||||||
m_id = other.m_id;
|
|
||||||
m_type = other.m_type;
|
|
||||||
m_context = std::move(other.m_context);
|
m_context = std::move(other.m_context);
|
||||||
|
m_type = other.m_type;
|
||||||
m_refCount = other.m_refCount;
|
m_refCount = other.m_refCount;
|
||||||
m_data = other.m_data;
|
m_data = other.m_data;
|
||||||
|
|
||||||
other.m_id = {};
|
|
||||||
other.m_type = {};
|
other.m_type = {};
|
||||||
other.m_refCount = {};
|
other.m_refCount = {};
|
||||||
other.m_data = nullptr;
|
other.m_data = nullptr;
|
||||||
@@ -89,7 +82,7 @@ static constexpr std::uint16_t thing_type_pair(thing_t lhs, thing_t rhs) {
|
|||||||
thing_p operator+(const thing_p& lhs, const thing_p& rhs) {
|
thing_p operator+(const thing_p& lhs, const thing_p& rhs) {
|
||||||
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
||||||
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
||||||
auto res = thing::create(lhs->m_context, thing_t::Int32);
|
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
|
||||||
res->int32() = lhs->int32() + rhs->int32();
|
res->int32() = lhs->int32() + rhs->int32();
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -100,7 +93,7 @@ thing_p operator+(const thing_p& lhs, const thing_p& rhs) {
|
|||||||
thing_p operator-(const thing_p& lhs, const thing_p& rhs) {
|
thing_p operator-(const thing_p& lhs, const thing_p& rhs) {
|
||||||
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
||||||
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
||||||
auto res = thing::create(lhs->m_context, thing_t::Int32);
|
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
|
||||||
res->int32() = lhs->int32() - rhs->int32();
|
res->int32() = lhs->int32() - rhs->int32();
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -111,7 +104,7 @@ thing_p operator-(const thing_p& lhs, const thing_p& rhs) {
|
|||||||
thing_p operator*(const thing_p& lhs, const thing_p& rhs) {
|
thing_p operator*(const thing_p& lhs, const thing_p& rhs) {
|
||||||
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
||||||
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
||||||
auto res = thing::create(lhs->m_context, thing_t::Int32);
|
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
|
||||||
res->int32() = lhs->int32() * rhs->int32();
|
res->int32() = lhs->int32() * rhs->int32();
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -122,7 +115,7 @@ thing_p operator*(const thing_p& lhs, const thing_p& rhs) {
|
|||||||
thing_p operator/(const thing_p& lhs, const thing_p& rhs) {
|
thing_p operator/(const thing_p& lhs, const thing_p& rhs) {
|
||||||
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
||||||
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
||||||
auto res = thing::create(lhs->m_context, thing_t::Int32);
|
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
|
||||||
res->int32() = lhs->int32() / rhs->int32();
|
res->int32() = lhs->int32() / rhs->int32();
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -133,7 +126,7 @@ thing_p operator/(const thing_p& lhs, const thing_p& rhs) {
|
|||||||
thing_p operator%(const thing_p& lhs, const thing_p& rhs) {
|
thing_p operator%(const thing_p& lhs, const thing_p& rhs) {
|
||||||
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
||||||
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
||||||
auto res = thing::create(lhs->m_context, thing_t::Int32);
|
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
|
||||||
res->int32() = lhs->int32() % rhs->int32();
|
res->int32() = lhs->int32() % rhs->int32();
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -144,7 +137,7 @@ thing_p operator%(const thing_p& lhs, const thing_p& rhs) {
|
|||||||
thing_p operator==(const thing_p& lhs, const thing_p& rhs) {
|
thing_p operator==(const thing_p& lhs, const thing_p& rhs) {
|
||||||
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
||||||
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
||||||
auto res = thing::create(lhs->m_context, thing_t::Int32);
|
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
|
||||||
res->int32() = static_cast<std::int32_t>(lhs->int32() == rhs->int32());
|
res->int32() = static_cast<std::int32_t>(lhs->int32() == rhs->int32());
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -155,7 +148,7 @@ thing_p operator==(const thing_p& lhs, const thing_p& rhs) {
|
|||||||
thing_p operator!=(const thing_p& lhs, const thing_p& rhs) {
|
thing_p operator!=(const thing_p& lhs, const thing_p& rhs) {
|
||||||
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
||||||
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
||||||
auto res = thing::create(lhs->m_context, thing_t::Int32);
|
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
|
||||||
res->int32() = static_cast<std::int32_t>(lhs->int32() != rhs->int32());
|
res->int32() = static_cast<std::int32_t>(lhs->int32() != rhs->int32());
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -166,7 +159,7 @@ thing_p operator!=(const thing_p& lhs, const thing_p& rhs) {
|
|||||||
thing_p operator<(const thing_p& lhs, const thing_p& rhs) {
|
thing_p operator<(const thing_p& lhs, const thing_p& rhs) {
|
||||||
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
||||||
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
||||||
auto res = thing::create(lhs->m_context, thing_t::Int32);
|
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
|
||||||
res->int32() = static_cast<std::int32_t>(lhs->int32() < rhs->int32());
|
res->int32() = static_cast<std::int32_t>(lhs->int32() < rhs->int32());
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -177,7 +170,7 @@ thing_p operator<(const thing_p& lhs, const thing_p& rhs) {
|
|||||||
thing_p operator>(const thing_p& lhs, const thing_p& rhs) {
|
thing_p operator>(const thing_p& lhs, const thing_p& rhs) {
|
||||||
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
||||||
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
||||||
auto res = thing::create(lhs->m_context, thing_t::Int32);
|
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
|
||||||
res->int32() = static_cast<std::int32_t>(lhs->int32() > rhs->int32());
|
res->int32() = static_cast<std::int32_t>(lhs->int32() > rhs->int32());
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -188,7 +181,7 @@ thing_p operator>(const thing_p& lhs, const thing_p& rhs) {
|
|||||||
thing_p operator<=(const thing_p& lhs, const thing_p& rhs) {
|
thing_p operator<=(const thing_p& lhs, const thing_p& rhs) {
|
||||||
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
||||||
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
||||||
auto res = thing::create(lhs->m_context, thing_t::Int32);
|
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
|
||||||
res->int32() = static_cast<std::int32_t>(lhs->int32() <= rhs->int32());
|
res->int32() = static_cast<std::int32_t>(lhs->int32() <= rhs->int32());
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -199,7 +192,7 @@ thing_p operator<=(const thing_p& lhs, const thing_p& rhs) {
|
|||||||
thing_p operator>=(const thing_p& lhs, const thing_p& rhs) {
|
thing_p operator>=(const thing_p& lhs, const thing_p& rhs) {
|
||||||
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
|
||||||
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
|
||||||
auto res = thing::create(lhs->m_context, thing_t::Int32);
|
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
|
||||||
res->int32() = static_cast<std::int32_t>(lhs->int32() >= rhs->int32());
|
res->int32() = static_cast<std::int32_t>(lhs->int32() >= rhs->int32());
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -216,7 +209,7 @@ thing_p thing::clone(const thing_p& thing) {
|
|||||||
}
|
}
|
||||||
thing_handle idx = id & ((1ULL << ((sizeof(id) * 8) - GENERATION_SIZE)) - 1);
|
thing_handle idx = id & ((1ULL << ((sizeof(id) * 8) - GENERATION_SIZE)) - 1);
|
||||||
|
|
||||||
auto th = std::make_shared<class thing>(rzecz{}, id, thing->m_type, thing->m_context);
|
auto th = std::make_shared<class thing>(thing->m_context, thing->m_type);
|
||||||
switch (thing->m_type) {
|
switch (thing->m_type) {
|
||||||
// Primitives
|
// Primitives
|
||||||
default: {
|
default: {
|
||||||
|
|||||||
Reference in New Issue
Block a user