Compare commits
6 Commits
ec2c81fc56
...
99666495e7
| Author | SHA1 | Date | |
|---|---|---|---|
|
99666495e7
|
|||
|
6f023765b5
|
|||
|
65e5fdf324
|
|||
|
5a1b7c5aa6
|
|||
|
f14f3854e2
|
|||
|
6fbdb2299b
|
@@ -11,6 +11,7 @@ Checks: >
|
||||
-bugprone-assignment-in-if-condition,
|
||||
-bugprone-easily-swappable-parameters,
|
||||
-bugprone-multi-level-implicit-pointer-conversion,
|
||||
-bugprone-unintended-char-ostream-output,
|
||||
-readability-function-cognitive-complexity,
|
||||
-readability-magic-numbers,
|
||||
-readability-redundant-access-specifiers,
|
||||
|
||||
@@ -1,49 +1,13 @@
|
||||
#ifndef FURVM_CONSTANT_HPP
|
||||
#define FURVM_CONSTANT_HPP
|
||||
|
||||
#include "furvm/exceptions.hpp"
|
||||
#include "furvm/fwd.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <string_view>
|
||||
|
||||
namespace furvm {
|
||||
|
||||
/**
|
||||
* @brief Bad constant access exception.
|
||||
*/
|
||||
class bad_constant_access : public std::exception {
|
||||
public:
|
||||
bad_constant_access() = default;
|
||||
~bad_constant_access() override = default;
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*/
|
||||
bad_constant_access(bad_constant_access&&) noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*/
|
||||
bad_constant_access& operator=(bad_constant_access&&) noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Copy constructor.
|
||||
*/
|
||||
bad_constant_access(const bad_constant_access&) = default;
|
||||
|
||||
/**
|
||||
* @brief Copy constructor.
|
||||
*/
|
||||
bad_constant_access& operator=(const bad_constant_access&) = default;
|
||||
public:
|
||||
/**
|
||||
* @brief Returns a C-style string describing the cause of the error.
|
||||
*
|
||||
* @return The cause of the error.
|
||||
*/
|
||||
const char* what() const noexcept override { return "bad constant access"; }
|
||||
};
|
||||
|
||||
enum class constant_t : std::uint8_t {
|
||||
String, /**< String constant. */
|
||||
};
|
||||
|
||||
@@ -92,6 +92,8 @@ public:
|
||||
auto erase_thing(Args&&... args) {
|
||||
return m_things.erase(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
thing_allocator<std::byte> thing_alloc() const { return m_thingAllocator; }
|
||||
public:
|
||||
/**
|
||||
* @brief Removes unreferenced things from the thing list.
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef FURVM_DETAIL_SERIALIZATION_HPP
|
||||
#define FURVM_DETAIL_SERIALIZATION_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
namespace furvm {
|
||||
namespace detail {
|
||||
|
||||
std::ostream& serialize(std::ostream& os, std::int8_t value);
|
||||
std::ostream& serialize(std::ostream& os, std::int16_t value);
|
||||
std::ostream& serialize(std::ostream& os, std::int32_t value);
|
||||
std::ostream& serialize(std::ostream& os, std::int64_t value);
|
||||
std::ostream& serialize(std::ostream& os, std::uint8_t value);
|
||||
std::ostream& serialize(std::ostream& os, std::uint16_t value);
|
||||
std::ostream& serialize(std::ostream& os, std::uint32_t value);
|
||||
std::ostream& serialize(std::ostream& os, std::uint64_t value);
|
||||
|
||||
std::ostream& serialize(std::ostream& os, const std::string& value);
|
||||
|
||||
} // namespace detail
|
||||
} // namespace furvm
|
||||
|
||||
#endif // FURVM_DETAIL_SERIALIZATION_HPP
|
||||
@@ -5,6 +5,75 @@
|
||||
|
||||
namespace furvm {
|
||||
|
||||
class bad_thing_access : public std::exception {
|
||||
public:
|
||||
bad_thing_access() = default;
|
||||
~bad_thing_access() override = default;
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*/
|
||||
bad_thing_access(bad_thing_access&&) noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*/
|
||||
bad_thing_access& operator=(bad_thing_access&&) noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Copy constructor.
|
||||
*/
|
||||
bad_thing_access(const bad_thing_access&) = default;
|
||||
|
||||
/**
|
||||
* @brief Copy constructor.
|
||||
*/
|
||||
bad_thing_access& operator=(const bad_thing_access&) = default;
|
||||
public:
|
||||
/**
|
||||
* @brief Returns a C-style string describing the cause of the error.
|
||||
*
|
||||
* @return The cause of the error.
|
||||
*/
|
||||
const char* what() const noexcept override { return "bad thing access"; }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Bad constant access exception.
|
||||
*/
|
||||
class bad_constant_access : public std::exception {
|
||||
public:
|
||||
bad_constant_access() = default;
|
||||
~bad_constant_access() override = default;
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*/
|
||||
bad_constant_access(bad_constant_access&&) noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*/
|
||||
bad_constant_access& operator=(bad_constant_access&&) noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Copy constructor.
|
||||
*/
|
||||
bad_constant_access(const bad_constant_access&) = default;
|
||||
|
||||
/**
|
||||
* @brief Copy constructor.
|
||||
*/
|
||||
bad_constant_access& operator=(const bad_constant_access&) = default;
|
||||
public:
|
||||
/**
|
||||
* @brief Returns a C-style string describing the cause of the error.
|
||||
*
|
||||
* @return The cause of the error.
|
||||
*/
|
||||
const char* what() const noexcept override { return "bad constant access"; }
|
||||
};
|
||||
|
||||
class stack_underflow : public std::exception {
|
||||
public:
|
||||
stack_underflow() = default;
|
||||
|
||||
@@ -86,7 +86,7 @@ public:
|
||||
* @param mod Module.
|
||||
* @param function Function handle.
|
||||
*/
|
||||
void push_frame(const mod_h& mod, const function_h& function);
|
||||
void push_frame(const mod_h& mod, function function);
|
||||
|
||||
/**
|
||||
* @brief Pops the top frame.
|
||||
|
||||
@@ -18,31 +18,24 @@ enum class function_t : std::uint8_t {
|
||||
|
||||
using native_function = std::function<void(executor&)>;
|
||||
|
||||
struct import_function {
|
||||
mod_id mod;
|
||||
function_id function;
|
||||
};
|
||||
|
||||
class function {
|
||||
public:
|
||||
template <typename Name>
|
||||
function(Name&& name, bytecode_pos position)
|
||||
: m_name(std::forward<Name>(name)), m_type(function_t::Normal), m_value(position) {}
|
||||
|
||||
function(const char* name, bytecode_pos position)
|
||||
: m_name(name), m_type(function_t::Normal), m_value(position) {}
|
||||
|
||||
template <typename Name>
|
||||
function(Name&& name, const native_function& native)
|
||||
: m_name(std::forward<Name>(name)), m_type(function_t::Native), m_value(native) {}
|
||||
|
||||
function(const char* name, const native_function& native)
|
||||
: m_name(name), m_type(function_t::Native), m_value(native) {}
|
||||
|
||||
template <typename Name, typename ModuleName>
|
||||
function(Name&& name, ModuleName&& moduleName, function_id function)
|
||||
: m_name(std::forward<Name>(name)),
|
||||
m_type(function_t::Import),
|
||||
m_value(std::forward<ModuleName>(moduleName), function) {}
|
||||
|
||||
template <typename ModuleName>
|
||||
function(const char* name, ModuleName&& moduleName, function_id function)
|
||||
: m_name(name), m_type(function_t::Import), m_value(std::forward<ModuleName>(moduleName), function) {}
|
||||
template <typename Name>
|
||||
function(Name&& name, const import_function& imp)
|
||||
: m_name(std::forward<Name>(name)), m_type(function_t::Import), m_value(imp) {}
|
||||
|
||||
~function();
|
||||
|
||||
@@ -105,19 +98,9 @@ public:
|
||||
*
|
||||
* @return The name.
|
||||
*/
|
||||
const std::string& imported_module() const {
|
||||
const import_function& imp() const {
|
||||
if (m_type != function_t::Import) throw std::runtime_error("function type mismatch");
|
||||
return m_value.imp.moduleName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a module of imported function.
|
||||
*
|
||||
* @return A handle to the module.
|
||||
*/
|
||||
function_id imported_function() const {
|
||||
if (m_type != function_t::Import) throw std::runtime_error("function type mismatch");
|
||||
return m_value.imp.function;
|
||||
return m_value.imp;
|
||||
}
|
||||
private:
|
||||
std::string m_name;
|
||||
@@ -126,10 +109,7 @@ private:
|
||||
union value {
|
||||
std::size_t position = 0;
|
||||
native_function native;
|
||||
struct {
|
||||
std::string moduleName;
|
||||
function_id function;
|
||||
} imp;
|
||||
import_function imp;
|
||||
|
||||
value() = default;
|
||||
|
||||
@@ -139,9 +119,8 @@ private:
|
||||
value(const native_function& native)
|
||||
: native(native) {}
|
||||
|
||||
template <typename Name>
|
||||
value(Name&& moduleName, function_id function)
|
||||
: imp({ std::forward<Name>(moduleName), function }) {}
|
||||
value(const import_function& imp)
|
||||
: imp(imp) {}
|
||||
|
||||
~value() {}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "furvm/fwd.hpp"
|
||||
#include "furvm/handle.hpp"
|
||||
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
@@ -17,6 +18,8 @@ class mod {
|
||||
friend class serializer;
|
||||
public:
|
||||
using bytecode_t = std::vector<byte>; /**< An alias to a vector of bytes. */
|
||||
|
||||
static constexpr char MAGIC[4] = { 'F', 'u', 'r', 'M' }; /** Furvm module file magic. */
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new module.
|
||||
@@ -91,6 +94,8 @@ public:
|
||||
}
|
||||
|
||||
void erase_function(function_id id) { m_functions.erase(id); }
|
||||
public:
|
||||
std::ostream& serialize(std::ostream& os) const;
|
||||
private:
|
||||
bytecode_t m_bytecode;
|
||||
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
#ifndef FURVM_SERIALIZER_HPP
|
||||
#define FURVM_SERIALIZER_HPP
|
||||
|
||||
#include "furvm/fwd.hpp"
|
||||
|
||||
#include <ostream>
|
||||
|
||||
namespace furvm {
|
||||
|
||||
class serializer {
|
||||
public:
|
||||
static constexpr byte MODULE_MAGIC[4] = { 'F', 'u', 'r', 'M' };
|
||||
static constexpr std::uint32_t VERSION = 0;
|
||||
public:
|
||||
static bool serialize_module(std::ostream& os, const mod_p& mod);
|
||||
};
|
||||
|
||||
} // namespace furvm
|
||||
|
||||
#endif // FURVM_SERIALIZER_HPP
|
||||
@@ -2,11 +2,13 @@
|
||||
#define FURVM_THING_HPP
|
||||
|
||||
#include "furlang/arena.hpp"
|
||||
#include "furvm/exceptions.hpp"
|
||||
#include "furvm/fwd.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -29,39 +31,6 @@ static inline std::size_t thing_type_size(thing_t type) {
|
||||
}
|
||||
}
|
||||
|
||||
class bad_thing_access : public std::exception {
|
||||
public:
|
||||
bad_thing_access() = default;
|
||||
~bad_thing_access() override = default;
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*/
|
||||
bad_thing_access(bad_thing_access&&) noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*/
|
||||
bad_thing_access& operator=(bad_thing_access&&) noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Copy constructor.
|
||||
*/
|
||||
bad_thing_access(const bad_thing_access&) = default;
|
||||
|
||||
/**
|
||||
* @brief Copy constructor.
|
||||
*/
|
||||
bad_thing_access& operator=(const bad_thing_access&) = default;
|
||||
public:
|
||||
/**
|
||||
* @brief Returns a C-style string describing the cause of the error.
|
||||
*
|
||||
* @return The cause of the error.
|
||||
*/
|
||||
const char* what() const noexcept override { return "bad thing access"; }
|
||||
};
|
||||
|
||||
template <template <typename> typename Allocator>
|
||||
class thing final {
|
||||
friend class executor;
|
||||
@@ -73,7 +42,9 @@ public:
|
||||
m_data = m_allocator.allocate(thing_type_size(type));
|
||||
}
|
||||
|
||||
~thing() { m_allocator.deallocate(m_data, thing_type_size(thing_t::Int32)); }
|
||||
~thing() {
|
||||
if (m_data != nullptr) m_allocator.deallocate(m_data, thing_type_size(thing_t::Int32));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
@@ -203,7 +174,7 @@ public:
|
||||
}
|
||||
private:
|
||||
thing_t m_type{};
|
||||
std::byte* m_data = nullptr;
|
||||
std::byte* m_data;
|
||||
|
||||
allocator_type m_allocator;
|
||||
};
|
||||
@@ -212,11 +183,13 @@ 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;
|
||||
public:
|
||||
explicit thing_allocator(furlang::arena& arena) noexcept
|
||||
: m_arena(&arena) {}
|
||||
: m_arena(&arena), m_deadThings(std::make_shared<dead_things>()) {}
|
||||
|
||||
template <typename U>
|
||||
thing_allocator(const thing_allocator<U>& other) noexcept
|
||||
@@ -232,18 +205,26 @@ public:
|
||||
|
||||
template <typename U>
|
||||
thing_allocator(thing_allocator<U>&& other) noexcept
|
||||
: m_arena(std::move(other.m_arena)) {}
|
||||
: m_arena(std::move(other.m_arena)), m_deadThings(std::move(other.m_deadThings)) {}
|
||||
|
||||
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;
|
||||
}
|
||||
public:
|
||||
[[nodiscard]] T* allocate(std::size_t count = 1) { return m_arena->allocate<T>(count); }
|
||||
[[nodiscard]] T* allocate(std::size_t count = 1) {
|
||||
for (auto it = m_deadThings->begin(); it != m_deadThings->end(); ++it) {
|
||||
if (it->second != count) continue;
|
||||
m_deadThings->erase(it);
|
||||
return it->first;
|
||||
}
|
||||
return m_arena->allocate<T>(count);
|
||||
}
|
||||
|
||||
void deallocate(T* ptr, std::size_t count) noexcept { m_deadThings.emplace_back(ptr, count); }
|
||||
void deallocate(T* ptr, std::size_t count) noexcept { m_deadThings->emplace_back(ptr, count); }
|
||||
public:
|
||||
template <typename U>
|
||||
bool operator==(const thing_allocator<U>& other) const noexcept {
|
||||
@@ -257,7 +238,7 @@ public:
|
||||
private:
|
||||
furlang::arena* m_arena;
|
||||
|
||||
std::vector<std::pair<T*, std::size_t>> m_deadThings;
|
||||
std::shared_ptr<dead_things> m_deadThings;
|
||||
};
|
||||
|
||||
} // namespace furvm
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "furvm/detail/serialization.hpp"
|
||||
|
||||
#include <ostream>
|
||||
|
||||
namespace furvm::detail {
|
||||
|
||||
std::ostream& serialize(std::ostream& os, std::int8_t value) {
|
||||
return os << (char)((value >> 0ULL) & 0xFF);
|
||||
}
|
||||
|
||||
std::ostream& serialize(std::ostream& os, std::int16_t value) {
|
||||
return os << (char)((value >> 0ULL) & 0xFF) << (char)((value >> 8ULL) & 0xFF);
|
||||
}
|
||||
|
||||
std::ostream& serialize(std::ostream& os, std::int32_t value) {
|
||||
return os << (char)((value >> 0ULL) & 0xFF) << (char)((value >> 8ULL) & 0xFF) << (char)((value >> 16ULL) & 0xFF)
|
||||
<< (char)((value >> 24ULL) & 0xFF);
|
||||
}
|
||||
|
||||
std::ostream& serialize(std::ostream& os, std::int64_t value) {
|
||||
return os << (char)((value >> 0ULL) & 0xFF) << (char)((value >> 8ULL) & 0xFF) << (char)((value >> 16ULL) & 0xFF)
|
||||
<< (char)((value >> 24ULL) & 0xFF) << (char)((value >> 32ULL) & 0xFF) << (char)((value >> 40ULL) & 0xFF)
|
||||
<< (char)((value >> 48ULL) & 0xFF) << (char)((value >> 56ULL) & 0xFF);
|
||||
}
|
||||
|
||||
std::ostream& serialize(std::ostream& os, std::uint8_t value) {
|
||||
return os << (char)((value >> 0ULL) & 0xFF);
|
||||
}
|
||||
|
||||
std::ostream& serialize(std::ostream& os, std::uint16_t value) {
|
||||
return os << (char)((value >> 0ULL) & 0xFF) << (char)((value >> 8ULL) & 0xFF);
|
||||
}
|
||||
|
||||
std::ostream& serialize(std::ostream& os, std::uint32_t value) {
|
||||
return os << (char)((value >> 0ULL) & 0xFF) << (char)((value >> 8ULL) & 0xFF) << (char)((value >> 16ULL) & 0xFF)
|
||||
<< (char)((value >> 24ULL) & 0xFF);
|
||||
}
|
||||
|
||||
std::ostream& serialize(std::ostream& os, std::uint64_t value) {
|
||||
return os << (char)((value >> 0ULL) & 0xFF) << (char)((value >> 8ULL) & 0xFF) << (char)((value >> 16ULL) & 0xFF)
|
||||
<< (char)((value >> 24ULL) & 0xFF) << (char)((value >> 32ULL) & 0xFF) << (char)((value >> 40ULL) & 0xFF)
|
||||
<< (char)((value >> 48ULL) & 0xFF) << (char)((value >> 56ULL) & 0xFF);
|
||||
}
|
||||
|
||||
std::ostream& serialize(std::ostream& os, const std::string& value) {
|
||||
return serialize(os, std::uint16_t(value.size())).write(value.data(), static_cast<std::streamsize>(value.size()));
|
||||
}
|
||||
|
||||
} // namespace furvm::detail
|
||||
+14
-10
@@ -12,9 +12,17 @@
|
||||
|
||||
namespace furvm {
|
||||
|
||||
void executor::push_frame(const mod_h& mod, const function_h& function) {
|
||||
if (function->type() != function_t::Normal) throw std::runtime_error("unexpected function type");
|
||||
m_frames.emplace((struct executor::frame){ mod, function->position(), m_stack.size() });
|
||||
void executor::push_frame(const mod_h& mod, function function) {
|
||||
while (function.type() == function_t::Import) {
|
||||
function = *m_context->module_at(function.imp().mod)->function_at(function.imp().function);
|
||||
}
|
||||
switch (function.type()) {
|
||||
case function_t::Normal: {
|
||||
m_frames.emplace((struct executor::frame){ mod, function.position(), m_stack.size() });
|
||||
} break;
|
||||
case function_t::Native:
|
||||
default: throw std::runtime_error("unexpected function type");
|
||||
}
|
||||
}
|
||||
|
||||
struct executor::frame executor::pop_frame() {
|
||||
@@ -70,7 +78,7 @@ void executor::step() {
|
||||
switch (instr) {
|
||||
case instruction_t::NoOperation: break;
|
||||
case instruction_t::PushB2I: {
|
||||
push_thing({ thing_t::Int32, m_context->m_thingAllocator })->int32() = frame.mod->byte(frame.position++);
|
||||
push_thing({ thing_t::Int32, m_context->thing_alloc() })->int32() = frame.mod->byte(frame.position++);
|
||||
} break;
|
||||
case instruction_t::Drop: {
|
||||
pop_thing();
|
||||
@@ -155,13 +163,9 @@ void executor::step() {
|
||||
|
||||
const function_h& function = frame.mod->function_at(funcId);
|
||||
switch (function->type()) {
|
||||
case function_t::Normal: push_frame(frame.mod, function); break;
|
||||
case function_t::Normal:
|
||||
case function_t::Import: push_frame(frame.mod, *function); break;
|
||||
case function_t::Native: function->native()(*this); break;
|
||||
case function_t::Import: {
|
||||
// const mod_p& impMod = m_context->m_modules.at(function->imported_module());
|
||||
// push_frame(frame.mod, function->imported_function());
|
||||
throw std::runtime_error("unimplemented");
|
||||
} break;
|
||||
}
|
||||
} break;
|
||||
case instruction_t::Jump: {
|
||||
|
||||
@@ -10,7 +10,7 @@ function::~function() {
|
||||
case function_t::Native:
|
||||
default: break;
|
||||
case function_t::Import: {
|
||||
m_value.imp.moduleName.~basic_string();
|
||||
m_value.imp.~import_function();
|
||||
} break;
|
||||
}
|
||||
}
|
||||
@@ -25,8 +25,7 @@ function::function(function&& other) noexcept
|
||||
new (&m_value.native) native_function(std::move(other.m_value.native));
|
||||
} break;
|
||||
case function_t::Import: {
|
||||
m_value.imp.moduleName = std::move(other.m_value.imp.moduleName);
|
||||
m_value.imp.function = other.m_value.imp.function;
|
||||
m_value.imp = std::move(other.m_value.imp);
|
||||
} break;
|
||||
}
|
||||
other.m_value.position = 0;
|
||||
@@ -45,8 +44,7 @@ function& function::operator=(function&& other) noexcept {
|
||||
new (&m_value.native) native_function(std::move(other.m_value.native));
|
||||
} break;
|
||||
case function_t::Import: {
|
||||
m_value.imp.moduleName = std::move(other.m_value.imp.moduleName);
|
||||
m_value.imp.function = other.m_value.imp.function;
|
||||
m_value.imp = std::move(other.m_value.imp);
|
||||
} break;
|
||||
}
|
||||
other.m_value.position = 0;
|
||||
@@ -64,8 +62,7 @@ function::function(const function& other)
|
||||
new (&m_value.native) native_function(other.m_value.native);
|
||||
} break;
|
||||
case function_t::Import: {
|
||||
m_value.imp.moduleName = other.m_value.imp.moduleName;
|
||||
m_value.imp.function = other.m_value.imp.function;
|
||||
m_value.imp = other.m_value.imp;
|
||||
} break;
|
||||
}
|
||||
}
|
||||
@@ -83,8 +80,7 @@ function& function::operator=(const function& other) {
|
||||
new (&m_value.native) native_function(other.m_value.native);
|
||||
} break;
|
||||
case function_t::Import: {
|
||||
m_value.imp.moduleName = other.m_value.imp.moduleName;
|
||||
m_value.imp.function = other.m_value.imp.function;
|
||||
m_value.imp = other.m_value.imp;
|
||||
} break;
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -6,7 +6,6 @@
|
||||
#include "furvm/fwd.hpp"
|
||||
#include "furvm/instruction.hpp"
|
||||
#include "furvm/module.hpp"
|
||||
#include "furvm/serializer.hpp"
|
||||
#include "furvm/thing.hpp"
|
||||
|
||||
#include <array>
|
||||
@@ -29,8 +28,10 @@ int main(void) {
|
||||
furvm::mod_h mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end());
|
||||
furvm::function_h mainFunc = mainModule->emplace_function("main", 0);
|
||||
|
||||
mainModule->serialize(std::cout);
|
||||
|
||||
furvm::executor_h executor = context->emplace_executor(context);
|
||||
executor->push_frame(mainModule, mainFunc);
|
||||
executor->push_frame(mainModule, *mainFunc);
|
||||
|
||||
static constexpr std::size_t FPC = 3; // Frames per collection
|
||||
|
||||
|
||||
+32
-1
@@ -1,3 +1,34 @@
|
||||
#include "furvm/module.hpp"
|
||||
|
||||
namespace furvm {}
|
||||
#include "furvm/detail/serialization.hpp"
|
||||
#include "furvm/fwd.hpp"
|
||||
|
||||
#include <ios>
|
||||
|
||||
namespace furvm {
|
||||
|
||||
std::ostream& mod::serialize(std::ostream& os) const {
|
||||
os << MAGIC;
|
||||
detail::serialize(os, std::uint32_t(0)); // version
|
||||
|
||||
detail::serialize(os, function_id(m_functionMap.size()));
|
||||
for (const auto& [name, id] : m_functionMap) {
|
||||
detail::serialize(os, name);
|
||||
function_h func = m_functions.at(id);
|
||||
detail::serialize(os, std::uint8_t(func->type()));
|
||||
switch (func->type()) {
|
||||
case function_t::Normal: {
|
||||
detail::serialize(os, func->position());
|
||||
} break;
|
||||
case function_t::Native: break;
|
||||
case function_t::Import: {
|
||||
detail::serialize(os, func->imp().mod);
|
||||
detail::serialize(os, func->imp().function);
|
||||
} 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()));
|
||||
}
|
||||
} // namespace furvm
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
#include "furvm/serializer.hpp"
|
||||
|
||||
#include "furvm/function.hpp" // IWYU pragma: keep
|
||||
#include "furvm/fwd.hpp"
|
||||
#include "furvm/module.hpp" // IWYU pragma: keep
|
||||
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
|
||||
namespace furvm {
|
||||
|
||||
template <typename T>
|
||||
static inline void write_raw(std::ostream& os, const T& value) {
|
||||
os.write(reinterpret_cast<const char*>(&value), sizeof(T));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline void read_raw(std::istream& is, T& value) {
|
||||
is.read(reinterpret_cast<char*>(&value), sizeof(T));
|
||||
}
|
||||
|
||||
static inline void write_u64(std::ostream& os, std::uint64_t value) {
|
||||
os.put(static_cast<char>((value >> 0ULL) & 0xFF));
|
||||
os.put(static_cast<char>((value >> 8ULL) & 0xFF));
|
||||
os.put(static_cast<char>((value >> 16ULL) & 0xFF));
|
||||
os.put(static_cast<char>((value >> 24ULL) & 0xFF));
|
||||
os.put(static_cast<char>((value >> 32ULL) & 0xFF));
|
||||
os.put(static_cast<char>((value >> 40ULL) & 0xFF));
|
||||
os.put(static_cast<char>((value >> 48ULL) & 0xFF));
|
||||
os.put(static_cast<char>((value >> 56ULL) & 0xFF));
|
||||
}
|
||||
|
||||
static inline void read_u64(std::istream& is, std::uint64_t& value) {
|
||||
value = (static_cast<std::uint64_t>(is.get()) << 0) | (static_cast<std::uint64_t>(is.get()) << 8) |
|
||||
(static_cast<std::uint64_t>(is.get()) << 16) | (static_cast<std::uint64_t>(is.get()) << 24) |
|
||||
(static_cast<std::uint64_t>(is.get()) << 32) | (static_cast<std::uint64_t>(is.get()) << 40) |
|
||||
(static_cast<std::uint64_t>(is.get()) << 48) | (static_cast<std::uint64_t>(is.get()) << 56);
|
||||
}
|
||||
|
||||
static inline void write_u32(std::ostream& os, std::uint32_t value) {
|
||||
os.put(static_cast<char>((value >> 0ULL) & 0xFF));
|
||||
os.put(static_cast<char>((value >> 8ULL) & 0xFF));
|
||||
os.put(static_cast<char>((value >> 16ULL) & 0xFF));
|
||||
os.put(static_cast<char>((value >> 24ULL) & 0xFF));
|
||||
}
|
||||
|
||||
static inline void read_u32(std::istream& is, std::uint32_t& value) {
|
||||
value = static_cast<std::uint32_t>(is.get() << 0) | static_cast<std::uint32_t>(is.get() << 8) |
|
||||
static_cast<std::uint32_t>(is.get() << 16) | static_cast<std::uint32_t>(is.get() << 24);
|
||||
}
|
||||
|
||||
static inline void write_u16(std::ostream& os, std::uint16_t value) {
|
||||
os.put(static_cast<char>((value >> 0ULL) & 0xFF));
|
||||
os.put(static_cast<char>((value >> 8ULL) & 0xFF));
|
||||
}
|
||||
|
||||
static inline void read_u16(std::istream& is, std::uint16_t& value) {
|
||||
value = static_cast<std::uint16_t>(is.get() << 0) | static_cast<std::uint16_t>(is.get() << 8);
|
||||
}
|
||||
|
||||
static inline void write_u8(std::ostream& os, std::uint8_t value) {
|
||||
os.put(static_cast<char>((value >> 0ULL) & 0xFF));
|
||||
}
|
||||
|
||||
static inline void read_u8(std::istream& is, std::uint8_t& value) {
|
||||
value = static_cast<std::uint8_t>(is.get() << 0);
|
||||
}
|
||||
|
||||
bool serializer::serialize_module(std::ostream& os, const mod_p& mod) {
|
||||
// os.write(reinterpret_cast<const char*>(MODULE_MAGIC), sizeof(MODULE_MAGIC));
|
||||
// write_u32(os, VERSION);
|
||||
|
||||
// write_u32(os, mod->m_functions.size());
|
||||
// for (function_id id = 0; id < static_cast<function_id>(mod->m_functions.size()); ++id) {
|
||||
// const auto& func = mod->function_at(id);
|
||||
// write_u16(os, id);
|
||||
// write_u8(os, static_cast<std::uint8_t>(func->type()));
|
||||
// switch (func->type()) {
|
||||
// case function_t::Normal: {
|
||||
// write_u64(os, func->position());
|
||||
// } break;
|
||||
// case function_t::Native: {
|
||||
// // TODO: Replace with a reference to the function's name from constant pool
|
||||
// throw std::runtime_error("cannot serialize native functions yet");
|
||||
// } break;
|
||||
// case function_t::Import: {
|
||||
// // TODO: Replace with a reference to the module's and function's name from constant pool
|
||||
// throw std::runtime_error("cannot serialize import functions yet");
|
||||
// } break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// write_u64(os, mod->m_bytecode.size());
|
||||
// os.write(reinterpret_cast<const char*>(mod->m_bytecode.data()),
|
||||
// static_cast<std::streamsize>(mod->m_bytecode.size()));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace furvm
|
||||
Reference in New Issue
Block a user