+1
-1
@@ -10,7 +10,7 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
// taken from furvm uwu :3 ^^ nya~ ngh~
|
// taken from furvm uwu :3 ^^ nya~ ngh~
|
||||||
static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
|
static void print_thing(const furvm::thing<>& thing) {
|
||||||
using namespace furvm;
|
using namespace furvm;
|
||||||
|
|
||||||
switch (thing.true_type().type) {
|
switch (thing.true_type().type) {
|
||||||
|
|||||||
@@ -7,9 +7,7 @@
|
|||||||
#include "furvm/handle.hpp"
|
#include "furvm/handle.hpp"
|
||||||
#include "furvm/module.hpp" // IWYU pragma: keep
|
#include "furvm/module.hpp" // IWYU pragma: keep
|
||||||
#include "furvm/thing.hpp" // IWYU pragma: keep
|
#include "furvm/thing.hpp" // IWYU pragma: keep
|
||||||
#include "furvm/thing_allocator.hpp"
|
|
||||||
|
|
||||||
#include <cstddef>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -22,8 +20,7 @@ public:
|
|||||||
/**
|
/**
|
||||||
* @brief Constructs a context.
|
* @brief Constructs a context.
|
||||||
*/
|
*/
|
||||||
context()
|
context() {}
|
||||||
: m_thingAllocator(m_thingArena) {}
|
|
||||||
|
|
||||||
~context() = default;
|
~context() = default;
|
||||||
|
|
||||||
@@ -70,21 +67,13 @@ public:
|
|||||||
|
|
||||||
const std::vector<executor>& executors() const { return m_executors; }
|
const std::vector<executor>& executors() const { return m_executors; }
|
||||||
public:
|
public:
|
||||||
/**
|
|
||||||
* @brief Returns context's thing allocator.
|
|
||||||
*
|
|
||||||
* @return The thing allocator.
|
|
||||||
*/
|
|
||||||
thing_allocator<std::byte> thing_alloc() const { return m_thingAllocator; }
|
|
||||||
|
|
||||||
thing_type_store& tt_store() { return m_thingTypeStore; }
|
thing_type_store& tt_store() { return m_thingTypeStore; }
|
||||||
private:
|
private:
|
||||||
handle_container<mod_h> m_modules;
|
handle_container<mod_h> m_modules;
|
||||||
std::vector<executor> m_executors;
|
std::vector<executor> m_executors;
|
||||||
|
|
||||||
furlang::arena m_thingArena;
|
furlang::arena m_thingArena;
|
||||||
thing_allocator<std::byte> m_thingAllocator;
|
class thing_type_store m_thingTypeStore;
|
||||||
class thing_type_store m_thingTypeStore;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace furvm
|
} // namespace furvm
|
||||||
|
|||||||
@@ -140,11 +140,6 @@ using mod_id = std::string;
|
|||||||
*/
|
*/
|
||||||
using mod_h = handle<mod, refcount_header<mod_id>>;
|
using mod_h = handle<mod, refcount_header<mod_id>>;
|
||||||
|
|
||||||
// thing_allocator.hpp
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
class thing_allocator;
|
|
||||||
|
|
||||||
// thing.hpp
|
// thing.hpp
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -161,7 +156,7 @@ using thing_type_id = std::uint32_t;
|
|||||||
*
|
*
|
||||||
* A stack element. Think of it like of a value in C++ or I guess a class in java.
|
* A stack element. Think of it like of a value in C++ or I guess a class in java.
|
||||||
*/
|
*/
|
||||||
template <template <typename> typename Allocator = thing_allocator>
|
template <template <typename> typename Allocator = std::allocator>
|
||||||
class thing;
|
class thing;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
#include "furlang/utility/hash.hpp"
|
#include "furlang/utility/hash.hpp"
|
||||||
#include "furvm/exceptions.hpp"
|
#include "furvm/exceptions.hpp"
|
||||||
#include "furvm/fwd.hpp"
|
#include "furvm/fwd.hpp"
|
||||||
#include "furvm/thing_allocator.hpp" // IWYU pragma: keep
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|||||||
@@ -1,113 +0,0 @@
|
|||||||
#ifndef FURVM_THING_ALLOCATOR_HPP
|
|
||||||
#define FURVM_THING_ALLOCATOR_HPP
|
|
||||||
|
|
||||||
#include "furlang/arena.hpp"
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace furvm {
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
class thing_allocator {
|
|
||||||
template <typename>
|
|
||||||
friend class thing_allocator;
|
|
||||||
|
|
||||||
using dead_things = std::vector<std::pair<T*, std::size_t>>;
|
|
||||||
public:
|
|
||||||
using value_type = T; /**< Value type. */
|
|
||||||
public:
|
|
||||||
thing_allocator() = default;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Constructs a thing allocator.
|
|
||||||
*
|
|
||||||
* @param arena Base arena allocator.
|
|
||||||
*/
|
|
||||||
explicit thing_allocator(furlang::arena& arena) noexcept
|
|
||||||
: m_arena(&arena), m_deadThings(std::make_shared<dead_things>()) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Move constructor.
|
|
||||||
*/
|
|
||||||
template <typename U>
|
|
||||||
thing_allocator(thing_allocator<U>&& other) noexcept
|
|
||||||
: m_arena(std::move(other.m_arena)), m_deadThings(std::move(other.m_deadThings)) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Move constructor.
|
|
||||||
*/
|
|
||||||
template <typename U>
|
|
||||||
thing_allocator& operator=(thing_allocator<U>&& other) noexcept {
|
|
||||||
if (this == &other) return *this;
|
|
||||||
m_arena = std::move(other.m_arena);
|
|
||||||
m_deadThings = std::move(other.m_deadThings);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Copy constructor.
|
|
||||||
*/
|
|
||||||
template <typename U>
|
|
||||||
thing_allocator(const thing_allocator<U>& other) noexcept
|
|
||||||
: m_arena(other.m_arena), m_deadThings(other.m_deadThings) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Copy constructor.
|
|
||||||
*/
|
|
||||||
template <typename U>
|
|
||||||
thing_allocator& operator=(const thing_allocator<U>& other) noexcept {
|
|
||||||
if (this == &other) return *this;
|
|
||||||
m_arena = other.m_arena;
|
|
||||||
m_deadThings = other.m_deadThings;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* @brief Returns a free chunk of memory.
|
|
||||||
*
|
|
||||||
* @param count Count of the things that must fit inside the chunk.
|
|
||||||
* @return The chunk.
|
|
||||||
*/
|
|
||||||
[[nodiscard]] T* allocate(std::size_t count = 1) {
|
|
||||||
for (auto it = m_deadThings->begin(); it != m_deadThings->end(); ++it) {
|
|
||||||
if (it->second != count) continue;
|
|
||||||
T* data = it->first;
|
|
||||||
m_deadThings->erase(it);
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
return m_arena->allocate<T>(count);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Recycles the pointer.
|
|
||||||
*/
|
|
||||||
void deallocate(T* ptr, std::size_t count) noexcept { m_deadThings->emplace_back(ptr, count); }
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* @brief Compares two thing allocators for equality.
|
|
||||||
*
|
|
||||||
* @return true if the two things are equal.
|
|
||||||
*/
|
|
||||||
template <typename U>
|
|
||||||
bool operator==(const thing_allocator<U>& other) const noexcept {
|
|
||||||
return m_arena == other.m_arena && m_deadThings == other.m_deadThings;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Compares two thing allocators for inequality.
|
|
||||||
*
|
|
||||||
* @return true if the two things are not equal.
|
|
||||||
*/
|
|
||||||
template <typename U>
|
|
||||||
bool operator!=(const thing_allocator<U>& other) const noexcept {
|
|
||||||
return m_arena != other.m_arena || m_deadThings != other.m_deadThings;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
furlang::arena* m_arena = nullptr;
|
|
||||||
|
|
||||||
std::shared_ptr<dead_things> m_deadThings;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace furvm
|
|
||||||
|
|
||||||
#endif // FURVM_THING_ALLOCATOR_HPP
|
|
||||||
+13
-21
@@ -49,8 +49,7 @@ thing_type* executor::mod_to_thing_type(const mod_h& mod, const mod_type& type)
|
|||||||
}
|
}
|
||||||
|
|
||||||
thing<> executor::make_reference(const thing<>& thing) const {
|
thing<> executor::make_reference(const thing<>& thing) const {
|
||||||
furvm::thing<> ref = { (struct thing_type){ thing_type::Ref, m_context->tt_store().insert(thing.type()) },
|
furvm::thing<> ref = { (struct thing_type){ thing_type::Ref, m_context->tt_store().insert(thing.type()) } };
|
||||||
m_context->thing_alloc() };
|
|
||||||
ref.reference(thing);
|
ref.reference(thing);
|
||||||
return std::move(ref);
|
return std::move(ref);
|
||||||
}
|
}
|
||||||
@@ -188,27 +187,22 @@ void executor::step() {
|
|||||||
switch (instr.type) {
|
switch (instr.type) {
|
||||||
case instruction_t::NoOperation: break;
|
case instruction_t::NoOperation: break;
|
||||||
case instruction_t::PushS8: {
|
case instruction_t::PushS8: {
|
||||||
push_thing({ (struct thing_type){ thing_type::S8 }, m_context->thing_alloc() }).get<thing_type::s8>() =
|
push_thing({ (struct thing_type){ thing_type::S8 } }).get<thing_type::s8>() = instr.arg.s8;
|
||||||
instr.arg.s8;
|
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::PushU8: {
|
case instruction_t::PushU8: {
|
||||||
push_thing({ (struct thing_type){ thing_type::U8 }, m_context->thing_alloc() }).get<thing_type::u8>() =
|
push_thing({ (struct thing_type){ thing_type::U8 } }).get<thing_type::u8>() = instr.arg.u8;
|
||||||
instr.arg.u8;
|
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::PushS16: {
|
case instruction_t::PushS16: {
|
||||||
push_thing({ (struct thing_type){ thing_type::S16 }, m_context->thing_alloc() }).get<thing_type::s16>() =
|
push_thing({ (struct thing_type){ thing_type::S16 } }).get<thing_type::s16>() = instr.arg.s16;
|
||||||
instr.arg.s16;
|
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::PushU16: {
|
case instruction_t::PushU16: {
|
||||||
push_thing({ (struct thing_type){ thing_type::U16 }, m_context->thing_alloc() }).get<thing_type::u16>() =
|
push_thing({ (struct thing_type){ thing_type::U16 } }).get<thing_type::u16>() = instr.arg.u16;
|
||||||
instr.arg.u16;
|
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::PushS32: {
|
case instruction_t::PushS32: {
|
||||||
push_thing({ (struct thing_type){ thing_type::S32 }, m_context->thing_alloc() }).get<thing_type::s32>() =
|
push_thing({ (struct thing_type){ thing_type::S32 } }).get<thing_type::s32>() = instr.arg.s8; // NOLINT
|
||||||
instr.arg.s8; // NOLINT
|
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::PushU32: {
|
case instruction_t::PushU32: {
|
||||||
push_thing({ (struct thing_type){ thing_type::U32 }, m_context->thing_alloc() }).get<thing_type::u32>() =
|
push_thing({ (struct thing_type){ thing_type::U32 } }).get<thing_type::u32>() =
|
||||||
static_cast<thing_type::u32>(instr.arg.u8);
|
static_cast<thing_type::u32>(instr.arg.u8);
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::Array: {
|
case instruction_t::Array: {
|
||||||
@@ -216,7 +210,7 @@ void executor::step() {
|
|||||||
if (type.type != thing_type::Array || type.value.array.type == nullptr || type.value.array.type == &type)
|
if (type.type != thing_type::Array || type.value.array.type == nullptr || type.value.array.type == &type)
|
||||||
throw std::runtime_error("invalid array type");
|
throw std::runtime_error("invalid array type");
|
||||||
|
|
||||||
auto& array = push_thing({ type, m_context->thing_alloc() });
|
auto& array = push_thing({ type });
|
||||||
|
|
||||||
if (type.value.array.size == 0) {
|
if (type.value.array.size == 0) {
|
||||||
auto sizeThing = pop_thing();
|
auto sizeThing = pop_thing();
|
||||||
@@ -310,13 +304,12 @@ void executor::step() {
|
|||||||
} break;
|
} break;
|
||||||
case instruction_t::Pointerof: {
|
case instruction_t::Pointerof: {
|
||||||
auto thing = pop_thing();
|
auto thing = pop_thing();
|
||||||
push_thing({ (struct thing_type){ thing_type::Ptr, m_context->tt_store().at(thing.type().id) },
|
push_thing({ (struct thing_type){ thing_type::Ptr, m_context->tt_store().at(thing.type().id) } }).get<void*>() =
|
||||||
m_context->thing_alloc() })
|
thing.raw();
|
||||||
.get<void*>() = thing.raw();
|
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::Sizeof: {
|
case instruction_t::Sizeof: {
|
||||||
auto thing = pop_thing();
|
auto thing = pop_thing();
|
||||||
auto& size = push_thing({ (struct thing_type){ thing_type::U64 }, m_context->thing_alloc() });
|
auto& size = push_thing({ (struct thing_type){ thing_type::U64 } });
|
||||||
switch (thing.type().type) {
|
switch (thing.type().type) {
|
||||||
case thing_type::S8:
|
case thing_type::S8:
|
||||||
case thing_type::S16:
|
case thing_type::S16:
|
||||||
@@ -339,9 +332,8 @@ void executor::step() {
|
|||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::Lengthof: {
|
case instruction_t::Lengthof: {
|
||||||
auto thing = pop_thing();
|
auto thing = pop_thing();
|
||||||
push_thing({ (struct thing_type){ thing_type::U64 }, m_context->thing_alloc() }).get<thing_type::u64>() =
|
push_thing({ (struct thing_type){ thing_type::U64 } }).get<thing_type::u64>() = thing.length();
|
||||||
thing.length();
|
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::Load: {
|
case instruction_t::Load: {
|
||||||
push_thing(make_reference(load_thing(instr.arg.u16)));
|
push_thing(make_reference(load_thing(instr.arg.u16)));
|
||||||
|
|||||||
+3
-5
@@ -1,7 +1,6 @@
|
|||||||
#include "furlang/arena.hpp"
|
#include "furlang/arena.hpp"
|
||||||
#include "furvm/furvm.hpp"
|
#include "furvm/furvm.hpp"
|
||||||
#include "furvm/thing.hpp"
|
#include "furvm/thing.hpp"
|
||||||
#include "furvm/thing_allocator.hpp"
|
|
||||||
|
|
||||||
#include "gtest/gtest.h" // IWYU pragma: keep
|
#include "gtest/gtest.h" // IWYU pragma: keep
|
||||||
|
|
||||||
@@ -10,12 +9,11 @@ namespace {
|
|||||||
// TODO: Basic program tests (e.g. for loops)
|
// TODO: Basic program tests (e.g. for loops)
|
||||||
|
|
||||||
TEST(Things, Ops) {
|
TEST(Things, Ops) {
|
||||||
furlang::arena arena;
|
furlang::arena arena;
|
||||||
furvm::thing_allocator<std::byte> alloc{ arena };
|
|
||||||
|
|
||||||
furvm::thing lhs{ furvm::thing_type{ furvm::thing_type::U32 }, alloc };
|
furvm::thing lhs{ furvm::thing_type{ furvm::thing_type::U32 } };
|
||||||
lhs.get<furvm::thing_type::u32>() = 6;
|
lhs.get<furvm::thing_type::u32>() = 6;
|
||||||
furvm::thing rhs{ furvm::thing_type{ furvm::thing_type::U32 }, alloc };
|
furvm::thing rhs{ furvm::thing_type{ furvm::thing_type::U32 } };
|
||||||
rhs.get<furvm::thing_type::u32>() = 7;
|
rhs.get<furvm::thing_type::u32>() = 7;
|
||||||
|
|
||||||
auto res = lhs.add(rhs);
|
auto res = lhs.add(rhs);
|
||||||
|
|||||||
Reference in New Issue
Block a user