refactor(furvm): improve primitive types
Improve primitive types and binary operations. Closes: #55
This commit is contained in:
@@ -18,8 +18,6 @@
|
|||||||
namespace furvm {
|
namespace furvm {
|
||||||
|
|
||||||
struct mod_type {
|
struct mod_type {
|
||||||
using primitive = std::uint64_t;
|
|
||||||
|
|
||||||
struct array {
|
struct array {
|
||||||
mod_type_id typeId;
|
mod_type_id typeId;
|
||||||
std::size_t size;
|
std::size_t size;
|
||||||
@@ -31,7 +29,14 @@ struct mod_type {
|
|||||||
};
|
};
|
||||||
|
|
||||||
enum type {
|
enum type {
|
||||||
Primitive = 0,
|
S8 = 0,
|
||||||
|
S16,
|
||||||
|
S32,
|
||||||
|
S64,
|
||||||
|
U8,
|
||||||
|
U16,
|
||||||
|
U32,
|
||||||
|
U64,
|
||||||
Array,
|
Array,
|
||||||
|
|
||||||
Import,
|
Import,
|
||||||
@@ -39,15 +44,11 @@ struct mod_type {
|
|||||||
} type;
|
} type;
|
||||||
union value {
|
union value {
|
||||||
std::nullptr_t null = nullptr;
|
std::nullptr_t null = nullptr;
|
||||||
primitive primitive;
|
|
||||||
array array;
|
array array;
|
||||||
imprt imprt;
|
imprt imprt;
|
||||||
|
|
||||||
value() = default;
|
value() = default;
|
||||||
|
|
||||||
value(std::uint64_t primitive)
|
|
||||||
: primitive(primitive) {}
|
|
||||||
|
|
||||||
value(mod_type_id id, std::size_t size)
|
value(mod_type_id id, std::size_t size)
|
||||||
: array({}) {
|
: array({}) {
|
||||||
array.typeId = id;
|
array.typeId = id;
|
||||||
@@ -69,8 +70,8 @@ struct mod_type {
|
|||||||
value& operator=(const value& other) = delete;
|
value& operator=(const value& other) = delete;
|
||||||
} value;
|
} value;
|
||||||
|
|
||||||
mod_type(primitive primitive)
|
mod_type(enum type type)
|
||||||
: type(Primitive), value(primitive) {}
|
: type(type) {}
|
||||||
|
|
||||||
mod_type(mod_type_id id, std::size_t size)
|
mod_type(mod_type_id id, std::size_t size)
|
||||||
: type(Array), value(id, size) {}
|
: type(Array), value(id, size) {}
|
||||||
@@ -83,8 +84,6 @@ struct mod_type {
|
|||||||
switch (type) {
|
switch (type) {
|
||||||
case Array: value.array.~array(); break;
|
case Array: value.array.~array(); break;
|
||||||
case Import: value.imprt.~imprt(); break;
|
case Import: value.imprt.~imprt(); break;
|
||||||
case Primitive:
|
|
||||||
case Count:
|
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,10 +91,9 @@ struct mod_type {
|
|||||||
mod_type(mod_type&& other) noexcept
|
mod_type(mod_type&& other) noexcept
|
||||||
: type(other.type) {
|
: type(other.type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Primitive: new (&value.primitive) primitive(other.value.primitive); break;
|
|
||||||
case Array: new (&value.array) array(other.value.array); break;
|
case Array: new (&value.array) array(other.value.array); break;
|
||||||
case Import: new (&value.imprt) imprt(std::move(other.value.imprt)); break;
|
case Import: new (&value.imprt) imprt(std::move(other.value.imprt)); break;
|
||||||
case Count: break;
|
default: break;
|
||||||
}
|
}
|
||||||
other.type = Count;
|
other.type = Count;
|
||||||
}
|
}
|
||||||
@@ -104,10 +102,9 @@ struct mod_type {
|
|||||||
if (this == &other) return *this;
|
if (this == &other) return *this;
|
||||||
type = other.type;
|
type = other.type;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Primitive: new (&value.primitive) primitive(other.value.primitive); break;
|
|
||||||
case Array: new (&value.array) array(other.value.array); break;
|
case Array: new (&value.array) array(other.value.array); break;
|
||||||
case Import: new (&value.imprt) imprt(std::move(other.value.imprt)); break;
|
case Import: new (&value.imprt) imprt(std::move(other.value.imprt)); break;
|
||||||
case Count: break;
|
default: break;
|
||||||
}
|
}
|
||||||
other.type = Count;
|
other.type = Count;
|
||||||
return *this;
|
return *this;
|
||||||
@@ -116,10 +113,9 @@ struct mod_type {
|
|||||||
mod_type(const mod_type& other)
|
mod_type(const mod_type& other)
|
||||||
: type(other.type) {
|
: type(other.type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Primitive: new (&value.primitive) primitive(other.value.primitive); break;
|
|
||||||
case Array: new (&value.array) array(other.value.array); break;
|
case Array: new (&value.array) array(other.value.array); break;
|
||||||
case Import: new (&value.imprt) imprt(other.value.imprt); break;
|
case Import: new (&value.imprt) imprt(other.value.imprt); break;
|
||||||
case Count: break;
|
default: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,10 +123,9 @@ struct mod_type {
|
|||||||
if (this == &other) return *this;
|
if (this == &other) return *this;
|
||||||
type = other.type;
|
type = other.type;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Primitive: new (&value.primitive) primitive(other.value.primitive); break;
|
|
||||||
case Array: new (&value.array) array(other.value.array); break;
|
case Array: new (&value.array) array(other.value.array); break;
|
||||||
case Import: new (&value.imprt) imprt(other.value.imprt); break;
|
case Import: new (&value.imprt) imprt(other.value.imprt); break;
|
||||||
case Count: break;
|
default: break;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|||||||
+231
-44
@@ -14,13 +14,21 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
#include <new>
|
#include <new>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <type_traits>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace furvm {
|
namespace furvm {
|
||||||
|
|
||||||
struct thing_type {
|
struct thing_type {
|
||||||
using primitive = std::uint64_t;
|
using s8 = std::int8_t;
|
||||||
|
using s16 = std::int16_t;
|
||||||
|
using s32 = std::int32_t;
|
||||||
|
using s64 = std::int64_t;
|
||||||
|
using u8 = std::uint8_t;
|
||||||
|
using u16 = std::uint16_t;
|
||||||
|
using u32 = std::uint32_t;
|
||||||
|
using u64 = std::uint64_t;
|
||||||
|
|
||||||
struct array {
|
struct array {
|
||||||
thing_type* type;
|
thing_type* type;
|
||||||
@@ -28,17 +36,23 @@ struct thing_type {
|
|||||||
};
|
};
|
||||||
|
|
||||||
enum type { // NOLINT
|
enum type { // NOLINT
|
||||||
Primitive = 0,
|
S8 = 0,
|
||||||
|
S16,
|
||||||
|
S32,
|
||||||
|
S64,
|
||||||
|
U8,
|
||||||
|
U16,
|
||||||
|
U32,
|
||||||
|
U64,
|
||||||
Array,
|
Array,
|
||||||
|
|
||||||
Count,
|
Count,
|
||||||
} type;
|
} type;
|
||||||
union value {
|
union value {
|
||||||
primitive primitive;
|
std::nullptr_t null = nullptr;
|
||||||
array array;
|
array array;
|
||||||
|
|
||||||
value(std::uint64_t primitive)
|
value() = default;
|
||||||
: primitive(primitive) {}
|
|
||||||
|
|
||||||
value(thing_type* type, std::size_t size)
|
value(thing_type* type, std::size_t size)
|
||||||
: array({}) {
|
: array({}) {
|
||||||
@@ -54,7 +68,14 @@ struct thing_type {
|
|||||||
bool operator==(const thing_type& other) const {
|
bool operator==(const thing_type& other) const {
|
||||||
if (type != other.type) return false;
|
if (type != other.type) return false;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Primitive: return value.primitive == other.value.primitive;
|
case S8:
|
||||||
|
case S16:
|
||||||
|
case S32:
|
||||||
|
case S64:
|
||||||
|
case U8:
|
||||||
|
case U16:
|
||||||
|
case U32:
|
||||||
|
case U64: return true;
|
||||||
case Array: return *value.array.type == *other.value.array.type && value.array.size == other.value.array.size;
|
case Array: return *value.array.type == *other.value.array.type && value.array.size == other.value.array.size;
|
||||||
case Count: break;
|
case Count: break;
|
||||||
}
|
}
|
||||||
@@ -62,6 +83,38 @@ struct thing_type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const thing_type& other) const { return !this->operator==(other); }
|
bool operator!=(const thing_type& other) const { return !this->operator==(other); }
|
||||||
|
|
||||||
|
static bool is_primitive(enum type type) {
|
||||||
|
switch (type) {
|
||||||
|
case S8:
|
||||||
|
case S16:
|
||||||
|
case S32:
|
||||||
|
case S64:
|
||||||
|
case U8:
|
||||||
|
case U16:
|
||||||
|
case U32:
|
||||||
|
case U64: return true;
|
||||||
|
case Array:
|
||||||
|
case Count: break;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::size_t primitive_size(enum type type) {
|
||||||
|
switch (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 Array:
|
||||||
|
case Count: break;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
@@ -70,10 +123,14 @@ struct thing_type_hash {
|
|||||||
std::size_t operator()(const thing_type& type) const {
|
std::size_t operator()(const thing_type& type) const {
|
||||||
std::size_t seed = std::hash<decltype(type.type)>{}(type.type);
|
std::size_t seed = std::hash<decltype(type.type)>{}(type.type);
|
||||||
switch (type.type) {
|
switch (type.type) {
|
||||||
case thing_type::Primitive:
|
case thing_type::S8:
|
||||||
seed =
|
case thing_type::S16:
|
||||||
furlang::utility::hash_combine(seed, std::hash<decltype(type.value.primitive)>{}(type.value.primitive));
|
case thing_type::S32:
|
||||||
return seed;
|
case thing_type::S64:
|
||||||
|
case thing_type::U8:
|
||||||
|
case thing_type::U16:
|
||||||
|
case thing_type::U32:
|
||||||
|
case thing_type::U64: return seed;
|
||||||
case thing_type::Array:
|
case thing_type::Array:
|
||||||
seed = furlang::utility::hash_combine(seed, thing_type_hash{}(*type.value.array.type));
|
seed = furlang::utility::hash_combine(seed, thing_type_hash{}(*type.value.array.type));
|
||||||
seed = furlang::utility::hash_combine(seed,
|
seed = furlang::utility::hash_combine(seed,
|
||||||
@@ -116,11 +173,6 @@ class thing final {
|
|||||||
public:
|
public:
|
||||||
using allocator_type = Allocator<std::byte>; /**< Allocator type. */
|
using allocator_type = Allocator<std::byte>; /**< Allocator type. */
|
||||||
public:
|
public:
|
||||||
using s8 = std::int8_t;
|
|
||||||
using s16 = std::int16_t;
|
|
||||||
using s32 = std::int32_t;
|
|
||||||
using s64 = std::int64_t;
|
|
||||||
|
|
||||||
union array {
|
union array {
|
||||||
std::byte flat[];
|
std::byte flat[];
|
||||||
struct {
|
struct {
|
||||||
@@ -186,7 +238,14 @@ public:
|
|||||||
|
|
||||||
thing res(m_type, m_allocator);
|
thing res(m_type, m_allocator);
|
||||||
switch (m_type.type) {
|
switch (m_type.type) {
|
||||||
case thing_type::Primitive: std::memcpy(res.m_data, m_data, m_size); return std::move(res);
|
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(res.m_data, m_data, m_size); return std::move(res);
|
||||||
case thing_type::Array: copy_list(m_type, res.get<array>(), get<array>()); return std::move(res);
|
case thing_type::Array: copy_list(m_type, res.get<array>(), get<array>()); return std::move(res);
|
||||||
case thing_type::Count: break;
|
case thing_type::Count: break;
|
||||||
}
|
}
|
||||||
@@ -221,8 +280,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T& get() {
|
T& get() {
|
||||||
std::size_t size = m_size > 0 ? m_size : compute_size_na(m_type);
|
if (compute_size_na(m_type) != sizeof(T)) throw bad_thing_access();
|
||||||
if (size != sizeof(T)) throw bad_thing_access();
|
|
||||||
return *std::launder(reinterpret_cast<T*>(m_data));
|
return *std::launder(reinterpret_cast<T*>(m_data));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,8 +291,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const T& get() const {
|
const T& get() const {
|
||||||
std::size_t size = m_size > 0 ? m_size : compute_size_na(m_type);
|
if (compute_size_na(m_type) != sizeof(T)) throw bad_thing_access();
|
||||||
if (size != sizeof(T)) throw bad_thing_access();
|
|
||||||
return *std::launder(reinterpret_cast<const T*>(m_data));
|
return *std::launder(reinterpret_cast<const T*>(m_data));
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
@@ -331,13 +388,16 @@ public:
|
|||||||
*
|
*
|
||||||
* @return The integer value.
|
* @return The integer value.
|
||||||
*/
|
*/
|
||||||
s64 integer() const {
|
thing_type::s64 integer() const {
|
||||||
if (m_type.type != thing_type::Primitive) throw bad_thing_access();
|
switch (m_type.type) {
|
||||||
switch (m_type.value.primitive) {
|
case thing_type::S8: return get<thing_type::s8>();
|
||||||
case sizeof(s8): return get<s8>();
|
case thing_type::S16: return get<thing_type::s16>();
|
||||||
case sizeof(s16): return get<s16>();
|
case thing_type::S32: return get<thing_type::s32>();
|
||||||
case sizeof(s32): return get<s32>();
|
case thing_type::S64: return get<thing_type::s64>();
|
||||||
case sizeof(s64): return get<s64>();
|
case thing_type::U8: return get<thing_type::u8>();
|
||||||
|
case thing_type::U16: return get<thing_type::u16>();
|
||||||
|
case thing_type::U32: return get<thing_type::u32>();
|
||||||
|
case thing_type::U64: return get<thing_type::u64>();
|
||||||
default: throw std::runtime_error("unreachable");
|
default: throw std::runtime_error("unreachable");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -346,7 +406,7 @@ public:
|
|||||||
|
|
||||||
constexpr bool is_reference() const { return m_size == 0; }
|
constexpr bool is_reference() const { return m_size == 0; }
|
||||||
|
|
||||||
void resize(s64 newSize) {
|
void resize(thing_type::u64 newSize) {
|
||||||
if (m_type.type != thing_type::Array) throw bad_thing_access();
|
if (m_type.type != thing_type::Array) throw bad_thing_access();
|
||||||
if (m_type.value.array.size > 0) throw std::runtime_error("cannot resize a static array");
|
if (m_type.value.array.size > 0) throw std::runtime_error("cannot resize a static array");
|
||||||
|
|
||||||
@@ -356,13 +416,13 @@ public:
|
|||||||
std::byte* newData = new std::byte[innerSize * newSize];
|
std::byte* newData = new std::byte[innerSize * newSize];
|
||||||
std::memcpy(newData,
|
std::memcpy(newData,
|
||||||
array.dynamic.data,
|
array.dynamic.data,
|
||||||
innerSize * std::min(static_cast<std::int64_t>(array.dynamic.size), newSize));
|
innerSize * std::min(static_cast<thing_type::u64>(array.dynamic.size), newSize));
|
||||||
array.dynamic.size = newSize;
|
array.dynamic.size = newSize;
|
||||||
delete[] array.dynamic.data;
|
delete[] array.dynamic.data;
|
||||||
array.dynamic.data = newData;
|
array.dynamic.data = newData;
|
||||||
}
|
}
|
||||||
|
|
||||||
thing at(s64 index) const {
|
thing at(thing_type::u64 index) const {
|
||||||
if (m_type.type != thing_type::Array) throw bad_thing_access();
|
if (m_type.type != thing_type::Array) throw bad_thing_access();
|
||||||
|
|
||||||
std::size_t elementSize = compute_size_na(*m_type.value.array.type);
|
std::size_t elementSize = compute_size_na(*m_type.value.array.type);
|
||||||
@@ -381,6 +441,11 @@ public:
|
|||||||
if (m_type.type != thing_type::Array) throw bad_thing_access();
|
if (m_type.type != thing_type::Array) throw bad_thing_access();
|
||||||
return m_type.value.array.size == 0 ? get<array>().dynamic.size : m_type.value.array.size;
|
return m_type.value.array.size == 0 ? get<array>().dynamic.size : m_type.value.array.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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); });
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
static void copy_list(const thing_type& arrayType, array& dst, const array& src) {
|
static void copy_list(const thing_type& arrayType, array& dst, const array& src) {
|
||||||
if (arrayType.type != thing_type::Array || arrayType.value.array.type == nullptr)
|
if (arrayType.type != thing_type::Array || arrayType.value.array.type == nullptr)
|
||||||
@@ -408,7 +473,14 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (innerType.type) {
|
switch (innerType.type) {
|
||||||
case thing_type::Primitive: std::memcpy(dst.flat, src.flat, size); return;
|
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(dst.flat, src.flat, size); return;
|
||||||
case thing_type::Array:
|
case thing_type::Array:
|
||||||
for (std::size_t i = 0; i < size; ++i) {
|
for (std::size_t i = 0; i < size; ++i) {
|
||||||
copy_list(*innerType.value.array.type,
|
copy_list(*innerType.value.array.type,
|
||||||
@@ -423,7 +495,14 @@ private:
|
|||||||
private:
|
private:
|
||||||
static std::size_t compute_size_na(const thing_type& type) {
|
static std::size_t compute_size_na(const thing_type& type) {
|
||||||
switch (type.type) {
|
switch (type.type) {
|
||||||
case thing_type::Primitive: return type.value.primitive;
|
case thing_type::S8: return sizeof(thing_type::s8);
|
||||||
|
case thing_type::S16: return sizeof(thing_type::s16);
|
||||||
|
case thing_type::S32: return sizeof(thing_type::s32);
|
||||||
|
case thing_type::S64: return sizeof(thing_type::s64);
|
||||||
|
case thing_type::U8: return sizeof(thing_type::u8);
|
||||||
|
case thing_type::U16: return sizeof(thing_type::u16);
|
||||||
|
case thing_type::U32: return sizeof(thing_type::u32);
|
||||||
|
case thing_type::U64: return sizeof(thing_type::u64);
|
||||||
case thing_type::Array:
|
case thing_type::Array:
|
||||||
return type.value.array.size == 0 ? sizeof(array)
|
return type.value.array.size == 0 ? sizeof(array)
|
||||||
: compute_size_na(*type.value.array.type) * type.value.array.size;
|
: compute_size_na(*type.value.array.type) * type.value.array.size;
|
||||||
@@ -439,23 +518,131 @@ private:
|
|||||||
thing(const thing_type& type, std::byte* data, const allocator_type& allocator = {})
|
thing(const thing_type& type, std::byte* data, const allocator_type& allocator = {})
|
||||||
: m_type(type), m_size(0), m_data(data), m_allocator(allocator) {}
|
: m_type(type), m_size(0), m_data(data), m_allocator(allocator) {}
|
||||||
private:
|
private:
|
||||||
|
template <typename Func>
|
||||||
|
decltype(auto) visit_primitive(Func&& func) const {
|
||||||
|
switch (m_type.type) {
|
||||||
|
case thing_type::S8: return std::forward<Func>(func)(get<thing_type::s8>());
|
||||||
|
case thing_type::S16: return std::forward<Func>(func)(get<thing_type::s16>());
|
||||||
|
case thing_type::S32: return std::forward<Func>(func)(get<thing_type::s32>());
|
||||||
|
case thing_type::S64: return std::forward<Func>(func)(get<thing_type::s64>());
|
||||||
|
case thing_type::U8: return std::forward<Func>(func)(get<thing_type::u8>());
|
||||||
|
case thing_type::U16: return std::forward<Func>(func)(get<thing_type::u16>());
|
||||||
|
case thing_type::U32: return std::forward<Func>(func)(get<thing_type::u32>());
|
||||||
|
case thing_type::U64: return std::forward<Func>(func)(get<thing_type::u64>());
|
||||||
|
default: throw bad_thing_access();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Op>
|
template <typename Op>
|
||||||
thing binary_op(const thing& rhs, const Op& op) const {
|
thing binary_op(const thing& rhs, const Op& op) const {
|
||||||
if (m_type.type == thing_type::Primitive && rhs.m_type.type == thing_type::Primitive) {
|
if (thing_type::is_primitive(m_type.type) && thing_type::is_primitive(rhs.m_type.type)) {
|
||||||
thing_type resultType = m_type.value.primitive >= rhs.m_type.value.primitive ? m_type : rhs.m_type;
|
static constexpr enum thing_type::type promotions[8 * 8] = {
|
||||||
std::size_t size = std::max(m_type.value.primitive, rhs.m_type.value.primitive);
|
// 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,
|
||||||
|
};
|
||||||
|
|
||||||
s64 result = op(integer(), rhs.integer());
|
enum thing_type::type resultType = promotions[m_type.type + (rhs.m_type.type * 8)];
|
||||||
|
|
||||||
thing res(resultType, m_allocator);
|
thing res = { thing_type{ resultType }, m_allocator };
|
||||||
switch (size) {
|
switch (resultType) {
|
||||||
case sizeof(s8): res.get<s8>() = result; break;
|
case thing_type::S8:
|
||||||
case sizeof(s16): res.get<s16>() = result; break;
|
res.get<thing_type::s8>() = Op{}(cast_to<thing_type::s8>(), rhs.cast_to<thing_type::s8>());
|
||||||
case sizeof(s32): res.get<s32>() = result; break;
|
return res;
|
||||||
case sizeof(s64): res.get<s64>() = result; break;
|
case thing_type::S16:
|
||||||
default: throw std::runtime_error("unreachable");
|
res.get<thing_type::s16>() = Op{}(cast_to<thing_type::s16>(), rhs.cast_to<thing_type::s16>());
|
||||||
|
return res;
|
||||||
|
case thing_type::S32:
|
||||||
|
res.get<thing_type::s32>() = Op{}(cast_to<thing_type::s32>(), rhs.cast_to<thing_type::s32>());
|
||||||
|
return res;
|
||||||
|
case thing_type::S64:
|
||||||
|
res.get<thing_type::s64>() = Op{}(cast_to<thing_type::s64>(), rhs.cast_to<thing_type::s64>());
|
||||||
|
return res;
|
||||||
|
case thing_type::U8:
|
||||||
|
res.get<thing_type::u8>() = Op{}(cast_to<thing_type::u8>(), rhs.cast_to<thing_type::u8>());
|
||||||
|
return res;
|
||||||
|
case thing_type::U16:
|
||||||
|
res.get<thing_type::u16>() = Op{}(cast_to<thing_type::u16>(), rhs.cast_to<thing_type::u16>());
|
||||||
|
return res;
|
||||||
|
case thing_type::U32:
|
||||||
|
res.get<thing_type::u32>() = Op{}(cast_to<thing_type::u32>(), rhs.cast_to<thing_type::u32>());
|
||||||
|
return res;
|
||||||
|
case thing_type::U64:
|
||||||
|
res.get<thing_type::u64>() = Op{}(cast_to<thing_type::u64>(), rhs.cast_to<thing_type::u64>());
|
||||||
|
return res;
|
||||||
|
case thing_type::Array:
|
||||||
|
case thing_type::Count: break;
|
||||||
}
|
}
|
||||||
return std::move(res);
|
throw std::runtime_error("unreachable");
|
||||||
}
|
}
|
||||||
|
|
||||||
throw std::runtime_error("unexpected operation");
|
throw std::runtime_error("unexpected operation");
|
||||||
|
|||||||
+33
-20
@@ -21,7 +21,14 @@ thing_type executor::thing_type_impl(mod_h mod, mod_type type) const {
|
|||||||
type = *mod->type_at(imprt.typeId);
|
type = *mod->type_at(imprt.typeId);
|
||||||
}
|
}
|
||||||
switch (static_cast<enum thing_type::type>(type.type)) {
|
switch (static_cast<enum thing_type::type>(type.type)) {
|
||||||
case thing_type::Primitive: return { static_cast<enum thing_type::type>(type.type), type.value.primitive };
|
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: return { static_cast<enum thing_type::type>(type.type) };
|
||||||
case thing_type::Array: {
|
case thing_type::Array: {
|
||||||
return { static_cast<enum thing_type::type>(type.type),
|
return { static_cast<enum thing_type::type>(type.type),
|
||||||
{ thing_type(mod, *mod->type_at(type.value.array.typeId)), type.value.array.size } };
|
{ thing_type(mod, *mod->type_at(type.value.array.typeId)), type.value.array.size } };
|
||||||
@@ -117,7 +124,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({ (struct thing_type){ thing_type::Primitive, { 4 } }, m_context->thing_alloc() })->get<int>() =
|
push_thing({ (struct thing_type){ thing_type::S32 }, m_context->thing_alloc() })->get<int>() =
|
||||||
frame.mod->byte(frame.position++);
|
frame.mod->byte(frame.position++);
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::Array: {
|
case instruction_t::Array: {
|
||||||
@@ -215,30 +222,36 @@ void executor::step() {
|
|||||||
push_thing(lhs->greater_equals(*rhs));
|
push_thing(lhs->greater_equals(*rhs));
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::Pointerof: {
|
case instruction_t::Pointerof: {
|
||||||
auto thing = pop_thing();
|
// auto thing = pop_thing();
|
||||||
auto ptr = push_thing(
|
// auto ptr = push_thing(
|
||||||
{ (struct thing_type){ thing_type::Primitive, { sizeof(std::uintptr_t) } }, m_context->thing_alloc() });
|
//{ (struct thing_type){ thing_type::Primitive, { sizeof(std::uintptr_t) } }, m_context->thing_alloc() });
|
||||||
switch (thing->type().type) {
|
// switch (thing->type().type) {
|
||||||
case thing_type::Primitive: ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(thing->raw()); break;
|
// case thing_type::Primitive: ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(thing->raw());
|
||||||
case thing_type::Array:
|
// break; case thing_type::Array: ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(
|
||||||
ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(
|
// thing->type().value.array.size == 0 ? thing->get<furvm::thing<>::array>().dynamic.data
|
||||||
thing->type().value.array.size == 0 ? thing->get<furvm::thing<>::array>().dynamic.data
|
//: thing->get<furvm::thing<>::array>().flat);
|
||||||
: thing->get<furvm::thing<>::array>().flat);
|
// break;
|
||||||
break;
|
// case thing_type::Count: throw std::runtime_error("unreachable");
|
||||||
case thing_type::Count: throw std::runtime_error("unreachable");
|
// }
|
||||||
}
|
throw std::runtime_error("unimplemented"); // TODO: Implement
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::Sizeof: {
|
case instruction_t::Sizeof: {
|
||||||
auto thing = pop_thing();
|
auto thing = pop_thing();
|
||||||
auto size = push_thing(
|
auto size = push_thing({ (struct thing_type){ thing_type::U64 }, m_context->thing_alloc() });
|
||||||
{ (struct thing_type){ thing_type::Primitive, { sizeof(std::size_t) } }, m_context->thing_alloc() });
|
|
||||||
switch (thing->type().type) {
|
switch (thing->type().type) {
|
||||||
case thing_type::Primitive:
|
case thing_type::S8:
|
||||||
size->get<std::int64_t>() = static_cast<std::int64_t>(thing->type().value.primitive);
|
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:
|
||||||
|
size->get<thing_type::u64>() = static_cast<thing_type::u64>(thing_type::primitive_size(thing->type().type));
|
||||||
break;
|
break;
|
||||||
case thing_type::Array:
|
case thing_type::Array:
|
||||||
size->get<std::int64_t>() = static_cast<std::int64_t>((thing->type().value.array.size == 0)
|
size->get<thing_type::u64>() = static_cast<thing_type::u64>(
|
||||||
? thing->get<furvm::thing<>::array>().dynamic.size
|
(thing->type().value.array.size == 0) ? thing->get<furvm::thing<>::array>().dynamic.size
|
||||||
: thing->type().value.array.size);
|
: thing->type().value.array.size);
|
||||||
break;
|
break;
|
||||||
default: throw std::runtime_error("unreachable");
|
default: throw std::runtime_error("unreachable");
|
||||||
|
|||||||
+20
-10
@@ -11,18 +11,25 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
|
static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
|
||||||
|
using namespace furvm;
|
||||||
|
|
||||||
switch (thing.type().type) {
|
switch (thing.type().type) {
|
||||||
case furvm::thing_type::Primitive: {
|
case thing_type::S8: std::cout << thing.cast_to<thing_type::s16>(); break;
|
||||||
std::cout << thing.integer();
|
case thing_type::S16: std::cout << thing.get<thing_type::s16>(); break;
|
||||||
} break;
|
case thing_type::S32: std::cout << thing.get<thing_type::s32>(); break;
|
||||||
case furvm::thing_type::Array: {
|
case thing_type::S64: std::cout << thing.get<thing_type::s64>(); break;
|
||||||
|
case thing_type::U8: std::cout << thing.get<thing_type::u8>(); break;
|
||||||
|
case thing_type::U16: std::cout << thing.get<thing_type::u16>(); break;
|
||||||
|
case thing_type::U32: std::cout << thing.get<thing_type::u32>(); break;
|
||||||
|
case thing_type::U64: std::cout << thing.get<thing_type::u64>(); break;
|
||||||
|
case furvm::thing_type::Array:
|
||||||
std::cout << "{ ";
|
std::cout << "{ ";
|
||||||
for (furvm::thing<>::s64 i = 0; i < thing.size(); ++i) {
|
for (thing_type::u64 i = 0; i < thing.size(); ++i) {
|
||||||
if (i > 0) std::cout << ", ";
|
if (i > 0) std::cout << ", ";
|
||||||
print_thing(thing.at(i));
|
print_thing(thing.at(i));
|
||||||
}
|
}
|
||||||
std::cout << " }\n";
|
std::cout << " }";
|
||||||
} break;
|
break;
|
||||||
default: std::cerr << "{Type not recognized}";
|
default: std::cerr << "{Type not recognized}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,12 +66,15 @@ int main(int argc, char** argv) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
auto mod = context->emplace("main", s_bytecode, s_bytecode + sizeof(s_bytecode));
|
auto mod = context->emplace("main", s_bytecode, s_bytecode + sizeof(s_bytecode));
|
||||||
auto intType = mod->emplace_type(sizeof(int));
|
auto charType = mod->emplace_type(furvm::mod_type::S8);
|
||||||
auto arrayType = mod->emplace_type(intType.id(), 10);
|
auto arrayType = mod->emplace_type(charType.id(), 10);
|
||||||
auto mainFunc = mod->emplace_function("main", furvm::function_sig{}, 0);
|
auto mainFunc = mod->emplace_function("main", furvm::function_sig{}, 0);
|
||||||
mod->emplace_function(furvm::function_sig{ { arrayType } }, "print").dispatch();
|
mod->emplace_function(furvm::function_sig{ { arrayType } }, "print").dispatch();
|
||||||
#endif
|
#endif
|
||||||
mod->set_native_function("print", [](furvm::executor& executor) { print_thing(*executor.load_thing(0)); });
|
mod->set_native_function("print", [](furvm::executor& executor) {
|
||||||
|
print_thing(*executor.load_thing(0));
|
||||||
|
std::cout << '\n';
|
||||||
|
});
|
||||||
|
|
||||||
furvm::executor_h executor = context->emplace_executor(context);
|
furvm::executor_h executor = context->emplace_executor(context);
|
||||||
executor->push_frame(mod, *mainFunc);
|
executor->push_frame(mod, *mainFunc);
|
||||||
|
|||||||
+16
-7
@@ -7,7 +7,6 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <ios>
|
#include <ios>
|
||||||
#include <memory>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
@@ -27,7 +26,14 @@ std::ostream& mod::serialize(std::ostream& os) const {
|
|||||||
|
|
||||||
auto type = *m_types.at(id);
|
auto type = *m_types.at(id);
|
||||||
switch (type.type) {
|
switch (type.type) {
|
||||||
case mod_type::Primitive: detail::serialize(os, type.value.primitive); break;
|
case mod_type::S8:
|
||||||
|
case mod_type::S16:
|
||||||
|
case mod_type::S32:
|
||||||
|
case mod_type::S64:
|
||||||
|
case mod_type::U8:
|
||||||
|
case mod_type::U16:
|
||||||
|
case mod_type::U32:
|
||||||
|
case mod_type::U64: break;
|
||||||
case mod_type::Array: {
|
case mod_type::Array: {
|
||||||
detail::serialize(os, type.value.array.typeId);
|
detail::serialize(os, type.value.array.typeId);
|
||||||
detail::serialize(os, type.value.array.size);
|
detail::serialize(os, type.value.array.size);
|
||||||
@@ -101,11 +107,14 @@ mod mod::load(std::istream& is) {
|
|||||||
if (type == 0xFF) continue;
|
if (type == 0xFF) continue;
|
||||||
|
|
||||||
switch ((enum mod_type::type)type) {
|
switch ((enum mod_type::type)type) {
|
||||||
case mod_type::Primitive: {
|
case mod_type::S8:
|
||||||
mod_type::primitive primitive = 0;
|
case mod_type::S16:
|
||||||
detail::load(is, primitive);
|
case mod_type::S32:
|
||||||
mod.emplace_type(id, primitive).dispatch();
|
case mod_type::S64:
|
||||||
} break;
|
case mod_type::U8:
|
||||||
|
case mod_type::U16:
|
||||||
|
case mod_type::U32:
|
||||||
|
case mod_type::U64: break;
|
||||||
case mod_type::Array: {
|
case mod_type::Array: {
|
||||||
mod_type_id typeId = 0;
|
mod_type_id typeId = 0;
|
||||||
detail::load(is, typeId);
|
detail::load(is, typeId);
|
||||||
|
|||||||
Reference in New Issue
Block a user