Compare commits
3 Commits
8d3859ce70
...
9b280fab22
| Author | SHA1 | Date | |
|---|---|---|---|
|
9b280fab22
|
|||
|
e1b75ccc8e
|
|||
|
cd715ae779
|
@@ -125,6 +125,11 @@ enum class instruction_t : byte {
|
|||||||
*/
|
*/
|
||||||
Sizeof,
|
Sizeof,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Pushes a length of popped-off thing onto the stack.
|
||||||
|
*/
|
||||||
|
Lengthof,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Pushes a variable onto the stack.
|
* @brief Pushes a variable onto the stack.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ struct mod_type {
|
|||||||
U16,
|
U16,
|
||||||
U32,
|
U32,
|
||||||
U64,
|
U64,
|
||||||
|
Ptr,
|
||||||
|
Ref,
|
||||||
Array,
|
Array,
|
||||||
|
|
||||||
Import,
|
Import,
|
||||||
@@ -44,11 +46,15 @@ struct mod_type {
|
|||||||
} type;
|
} type;
|
||||||
union value {
|
union value {
|
||||||
std::nullptr_t null = nullptr;
|
std::nullptr_t null = nullptr;
|
||||||
|
mod_type_id typeRef;
|
||||||
array array;
|
array array;
|
||||||
imprt imprt;
|
imprt imprt;
|
||||||
|
|
||||||
value() = default;
|
value() = default;
|
||||||
|
|
||||||
|
value(mod_type_id id)
|
||||||
|
: typeRef(id) {}
|
||||||
|
|
||||||
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;
|
||||||
@@ -73,6 +79,9 @@ struct mod_type {
|
|||||||
mod_type(enum type type)
|
mod_type(enum type type)
|
||||||
: type(type) {}
|
: type(type) {}
|
||||||
|
|
||||||
|
mod_type(enum type type, mod_type_id typeRef)
|
||||||
|
: type(type), value(typeRef) {}
|
||||||
|
|
||||||
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) {}
|
||||||
|
|
||||||
|
|||||||
@@ -44,16 +44,22 @@ struct thing_type {
|
|||||||
U16,
|
U16,
|
||||||
U32,
|
U32,
|
||||||
U64,
|
U64,
|
||||||
|
Ptr,
|
||||||
|
Ref,
|
||||||
Array,
|
Array,
|
||||||
|
|
||||||
Count,
|
Count,
|
||||||
} type;
|
} type;
|
||||||
union value {
|
union value {
|
||||||
std::nullptr_t null = nullptr;
|
std::nullptr_t null = nullptr;
|
||||||
|
thing_type* typeRef;
|
||||||
array array;
|
array array;
|
||||||
|
|
||||||
value() = default;
|
value() = default;
|
||||||
|
|
||||||
|
value(thing_type* type)
|
||||||
|
: typeRef(type) {}
|
||||||
|
|
||||||
value(thing_type* type, std::size_t size)
|
value(thing_type* type, std::size_t size)
|
||||||
: array({}) {
|
: array({}) {
|
||||||
array.type = type;
|
array.type = type;
|
||||||
@@ -76,6 +82,8 @@ struct thing_type {
|
|||||||
case U16:
|
case U16:
|
||||||
case U32:
|
case U32:
|
||||||
case U64: return true;
|
case U64: return true;
|
||||||
|
case Ptr:
|
||||||
|
case Ref: return *value.typeRef == *other.value.typeRef;
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -94,10 +102,12 @@ struct thing_type {
|
|||||||
case U16:
|
case U16:
|
||||||
case U32:
|
case U32:
|
||||||
case U64: return true;
|
case U64: return true;
|
||||||
case Array:
|
case Ptr:
|
||||||
|
case Ref:
|
||||||
|
case Array: return false;
|
||||||
case Count: break;
|
case Count: break;
|
||||||
}
|
}
|
||||||
return false;
|
throw std::runtime_error("unreachable");
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::size_t primitive_size(enum type type) {
|
static std::size_t primitive_size(enum type type) {
|
||||||
@@ -110,10 +120,12 @@ struct thing_type {
|
|||||||
case thing_type::U16: return sizeof(u16);
|
case thing_type::U16: return sizeof(u16);
|
||||||
case thing_type::U32: return sizeof(u32);
|
case thing_type::U32: return sizeof(u32);
|
||||||
case thing_type::U64: return sizeof(u64);
|
case thing_type::U64: return sizeof(u64);
|
||||||
case Array:
|
case Ptr:
|
||||||
|
case Ref:
|
||||||
|
case Array: return 0;
|
||||||
case Count: break;
|
case Count: break;
|
||||||
}
|
}
|
||||||
return 0;
|
throw std::runtime_error("unreachable");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -131,6 +143,8 @@ struct thing_type_hash {
|
|||||||
case thing_type::U16:
|
case thing_type::U16:
|
||||||
case thing_type::U32:
|
case thing_type::U32:
|
||||||
case thing_type::U64: return seed;
|
case thing_type::U64: return seed;
|
||||||
|
case thing_type::Ptr:
|
||||||
|
case thing_type::Ref: return furlang::utility::hash_combine(seed, thing_type_hash{}(*type.value.typeRef));
|
||||||
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,
|
||||||
@@ -156,6 +170,16 @@ public:
|
|||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
thing_type* insert(const thing_type& type) {
|
||||||
|
if (auto it = m_typeMap.find(type); it != m_typeMap.end()) return m_map[it->second];
|
||||||
|
|
||||||
|
thing_type_id id = m_counter++;
|
||||||
|
thing_type* ptr = m_arena.allocate<thing_type>(type);
|
||||||
|
m_map[id] = ptr;
|
||||||
|
m_typeMap[type] = id;
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
thing_type* at(thing_type_id id) const {
|
thing_type* at(thing_type_id id) const {
|
||||||
if (auto it = m_map.find(id); it != m_map.end()) return it->second;
|
if (auto it = m_map.find(id); it != m_map.end()) return it->second;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -189,6 +213,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
thing(const thing_type& type, const allocator_type& allocator = {})
|
thing(const thing_type& type, const allocator_type& allocator = {})
|
||||||
: m_type(type), m_size(compute_size(type)), m_allocator(allocator) {
|
: m_type(type), m_size(compute_size(type)), m_allocator(allocator) {
|
||||||
|
if (m_type.type == thing_type::Ref) return;
|
||||||
// TODO: Account for alignment
|
// TODO: Account for alignment
|
||||||
m_data = m_allocator.allocate(m_size);
|
m_data = m_allocator.allocate(m_size);
|
||||||
std::memset(m_data, 0, m_size);
|
std::memset(m_data, 0, m_size);
|
||||||
@@ -198,7 +223,7 @@ public:
|
|||||||
* @brief Destructs a thing.
|
* @brief Destructs a thing.
|
||||||
*/
|
*/
|
||||||
~thing() {
|
~thing() {
|
||||||
if (m_data != nullptr && m_size > 0) m_allocator.deallocate(m_data, m_size);
|
if (m_type.type != thing_type::Ref && m_data != nullptr && m_size > 0) m_allocator.deallocate(m_data, m_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -234,8 +259,6 @@ public:
|
|||||||
* @return A clone of this thing.
|
* @return A clone of this thing.
|
||||||
*/
|
*/
|
||||||
thing clone() const {
|
thing clone() const {
|
||||||
if (m_size == 0) return reference();
|
|
||||||
|
|
||||||
thing res(m_type, m_allocator);
|
thing res(m_type, m_allocator);
|
||||||
switch (m_type.type) {
|
switch (m_type.type) {
|
||||||
case thing_type::S8:
|
case thing_type::S8:
|
||||||
@@ -245,19 +268,40 @@ public:
|
|||||||
case thing_type::U8:
|
case thing_type::U8:
|
||||||
case thing_type::U16:
|
case thing_type::U16:
|
||||||
case thing_type::U32:
|
case thing_type::U32:
|
||||||
case thing_type::U64: std::memcpy(res.m_data, m_data, m_size); return std::move(res);
|
case thing_type::U64:
|
||||||
|
case thing_type::Ptr: 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::Ref: throw std::runtime_error("cannot clone references");
|
||||||
case thing_type::Count: break;
|
case thing_type::Count: break;
|
||||||
}
|
}
|
||||||
throw std::runtime_error("unreachable");
|
throw std::runtime_error("unreachable");
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Returns the thing's type reference.
|
* @brief Returns the thing's type.
|
||||||
*
|
*
|
||||||
* @return The type reference.
|
* @return The type.
|
||||||
*/
|
*/
|
||||||
constexpr auto type() const { return m_type; }
|
constexpr thing_type type() const { return m_type; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns the thing's true type.
|
||||||
|
*
|
||||||
|
* If the thing is a reference, returns the referenced type.
|
||||||
|
*
|
||||||
|
* @return The true type.
|
||||||
|
*/
|
||||||
|
constexpr thing_type true_type() const { return (m_type.type == thing_type::Ref) ? *m_type.value.typeRef : m_type; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Checks if the thing is of a specified type.
|
||||||
|
*
|
||||||
|
* Compares the true type.
|
||||||
|
*
|
||||||
|
* @param type Type to compare.
|
||||||
|
* @return true if the types match.
|
||||||
|
*/
|
||||||
|
constexpr bool is(enum thing_type::type type) const { return true_type().type == type; }
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Returns a raw data pointer.
|
* @brief Returns a raw data pointer.
|
||||||
@@ -402,12 +446,8 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
thing reference() const { return { m_type, m_data, m_allocator }; }
|
|
||||||
|
|
||||||
constexpr bool is_reference() const { return m_size == 0; }
|
|
||||||
|
|
||||||
void resize(thing_type::u64 newSize) {
|
void resize(thing_type::u64 newSize) {
|
||||||
if (m_type.type != thing_type::Array) throw bad_thing_access();
|
if (!is(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");
|
||||||
|
|
||||||
auto& array = get<union array>();
|
auto& array = get<union array>();
|
||||||
@@ -423,29 +463,45 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
thing at(thing_type::u64 index) const {
|
thing at(thing_type::u64 index) const {
|
||||||
if (m_type.type != thing_type::Array) throw bad_thing_access();
|
if (!is(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);
|
||||||
if (m_type.value.array.size == 0) {
|
if (m_type.value.array.size == 0) {
|
||||||
auto& array = get<union array>();
|
auto& array = get<union array>();
|
||||||
if (index < 0 || index >= array.dynamic.size) throw std::out_of_range("index out of range");
|
if (index < 0 || index >= array.dynamic.size) throw std::out_of_range("index out of range");
|
||||||
return { *m_type.value.array.type, array.dynamic.data + (index * elementSize), m_allocator };
|
thing ref = { { thing_type::Ref, m_type.value.array.type }, m_allocator };
|
||||||
|
ref.m_data = array.dynamic.data + (index * elementSize);
|
||||||
|
return ref;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::byte* data = reinterpret_cast<array*>(m_data)->flat;
|
std::byte* data = reinterpret_cast<array*>(m_data)->flat;
|
||||||
if (index < 0 || index >= m_type.value.array.size) throw std::out_of_range("index out of range");
|
if (index < 0 || index >= m_type.value.array.size) throw std::out_of_range("index out of range");
|
||||||
return { *m_type.value.array.type, data + (index * elementSize), m_allocator };
|
thing ref = { { thing_type::Ref, m_type.value.array.type }, m_allocator };
|
||||||
|
ref.m_data = data + (index * elementSize);
|
||||||
|
return ref;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t size() const {
|
thing_type::u64 length() const {
|
||||||
if (m_type.type != thing_type::Array) throw bad_thing_access();
|
if (!is(thing_type::Array)) throw bad_thing_access();
|
||||||
return m_type.value.array.size == 0 ? get<array>().dynamic.size : m_type.value.array.size;
|
return true_type().value.array.size == 0 ? get<array>().dynamic.size : true_type().value.array.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
|
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
|
||||||
T cast_to() const {
|
T cast_to() const {
|
||||||
return visit_primitive([](auto value) { return static_cast<T>(value); });
|
return visit_primitive([](auto value) { return static_cast<T>(value); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Changes reference thing's referenced thing.
|
||||||
|
*
|
||||||
|
* Yes.
|
||||||
|
*
|
||||||
|
* @param thing Thing.
|
||||||
|
*/
|
||||||
|
void reference(const thing& thing) {
|
||||||
|
if (m_type.type != thing_type::Ref || *m_type.value.typeRef != thing.type()) throw bad_thing_access();
|
||||||
|
m_data = thing.m_data;
|
||||||
|
}
|
||||||
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)
|
||||||
@@ -480,7 +536,9 @@ private:
|
|||||||
case thing_type::U8:
|
case thing_type::U8:
|
||||||
case thing_type::U16:
|
case thing_type::U16:
|
||||||
case thing_type::U32:
|
case thing_type::U32:
|
||||||
case thing_type::U64: std::memcpy(dst.flat, src.flat, size); return;
|
case thing_type::U64:
|
||||||
|
case thing_type::Ptr:
|
||||||
|
case thing_type::Ref: 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,
|
||||||
@@ -503,6 +561,8 @@ private:
|
|||||||
case thing_type::U16: return sizeof(thing_type::u16);
|
case thing_type::U16: return sizeof(thing_type::u16);
|
||||||
case thing_type::U32: return sizeof(thing_type::u32);
|
case thing_type::U32: return sizeof(thing_type::u32);
|
||||||
case thing_type::U64: return sizeof(thing_type::u64);
|
case thing_type::U64: return sizeof(thing_type::u64);
|
||||||
|
case thing_type::Ptr: return sizeof(void*);
|
||||||
|
case thing_type::Ref: return compute_size_na(*type.value.typeRef);
|
||||||
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;
|
||||||
@@ -514,9 +574,6 @@ private:
|
|||||||
|
|
||||||
// NOTE: Align to 4 bytes
|
// NOTE: Align to 4 bytes
|
||||||
static std::size_t compute_size(const thing_type& type) { return (compute_size_na(type) + 3) & ~3; }
|
static std::size_t compute_size(const thing_type& type) { return (compute_size_na(type) + 3) & ~3; }
|
||||||
private:
|
|
||||||
thing(const thing_type& type, std::byte* data, const allocator_type& allocator = {})
|
|
||||||
: m_type(type), m_size(0), m_data(data), m_allocator(allocator) {}
|
|
||||||
private:
|
private:
|
||||||
template <typename Func>
|
template <typename Func>
|
||||||
decltype(auto) visit_primitive(Func&& func) const {
|
decltype(auto) visit_primitive(Func&& func) const {
|
||||||
@@ -611,7 +668,7 @@ private:
|
|||||||
thing_type::U64,
|
thing_type::U64,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum thing_type::type resultType = promotions[m_type.type + (rhs.m_type.type * 8)];
|
enum thing_type::type resultType = promotions[true_type().type + (rhs.true_type().type * 8)];
|
||||||
|
|
||||||
thing res = { thing_type{ resultType }, m_allocator };
|
thing res = { thing_type{ resultType }, m_allocator };
|
||||||
switch (resultType) {
|
switch (resultType) {
|
||||||
@@ -639,6 +696,8 @@ private:
|
|||||||
case thing_type::U64:
|
case thing_type::U64:
|
||||||
res.get<thing_type::u64>() = Op{}(cast_to<thing_type::u64>(), rhs.cast_to<thing_type::u64>());
|
res.get<thing_type::u64>() = Op{}(cast_to<thing_type::u64>(), rhs.cast_to<thing_type::u64>());
|
||||||
return res;
|
return res;
|
||||||
|
case thing_type::Ptr: // TODO: Pointer arithmetics
|
||||||
|
case thing_type::Ref:
|
||||||
case thing_type::Array:
|
case thing_type::Array:
|
||||||
case thing_type::Count: break;
|
case thing_type::Count: break;
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-16
@@ -29,6 +29,7 @@ thing_type executor::thing_type_impl(mod_h mod, mod_type type) const {
|
|||||||
case thing_type::U16:
|
case thing_type::U16:
|
||||||
case thing_type::U32:
|
case thing_type::U32:
|
||||||
case thing_type::U64: return { static_cast<enum thing_type::type>(type.type) };
|
case thing_type::U64: return { static_cast<enum thing_type::type>(type.type) };
|
||||||
|
case thing_type::Ptr: return { thing_type::Ptr, thing_type(mod, *mod->type_at(type.value.typeRef)) };
|
||||||
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 } };
|
||||||
@@ -164,7 +165,9 @@ void executor::step() {
|
|||||||
} break;
|
} break;
|
||||||
case instruction_t::Reference: {
|
case instruction_t::Reference: {
|
||||||
auto thing = pop_thing();
|
auto thing = pop_thing();
|
||||||
push_thing(std::move(thing->reference()));
|
push_thing({ (struct thing_type){ thing_type::Ref, m_context->thing_type_store().insert(thing->type()) },
|
||||||
|
m_context->thing_alloc() })
|
||||||
|
->reference(*thing);
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::Add: {
|
case instruction_t::Add: {
|
||||||
auto rhs = pop_thing();
|
auto rhs = pop_thing();
|
||||||
@@ -222,18 +225,11 @@ 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 =
|
||||||
//{ (struct thing_type){ thing_type::Primitive, { sizeof(std::uintptr_t) } }, m_context->thing_alloc() });
|
push_thing({ (struct thing_type){ thing_type::Ptr, m_context->thing_type_store().at(thing->type().id) },
|
||||||
// switch (thing->type().type) {
|
m_context->thing_alloc() });
|
||||||
// case thing_type::Primitive: ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(thing->raw());
|
ptr->get<void*>() = thing->raw();
|
||||||
// break; case thing_type::Array: ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(
|
|
||||||
// thing->type().value.array.size == 0 ? thing->get<furvm::thing<>::array>().dynamic.data
|
|
||||||
//: thing->get<furvm::thing<>::array>().flat);
|
|
||||||
// break;
|
|
||||||
// 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();
|
||||||
@@ -249,14 +245,21 @@ void executor::step() {
|
|||||||
case thing_type::U64:
|
case thing_type::U64:
|
||||||
size->get<thing_type::u64>() = static_cast<thing_type::u64>(thing_type::primitive_size(thing->type().type));
|
size->get<thing_type::u64>() = static_cast<thing_type::u64>(thing_type::primitive_size(thing->type().type));
|
||||||
break;
|
break;
|
||||||
|
case thing_type::Ptr: size->get<thing_type::u64>() = static_cast<thing_type::u64>(sizeof(void*)); break;
|
||||||
case thing_type::Array:
|
case thing_type::Array:
|
||||||
size->get<thing_type::u64>() = static_cast<thing_type::u64>(
|
/* TODO: Return actual memory size of the array
|
||||||
(thing->type().value.array.size == 0) ? thing->get<furvm::thing<>::array>().dynamic.size
|
* By the memory size I mean the length times sizeof single element.
|
||||||
: thing->type().value.array.size);
|
*/
|
||||||
|
size->get<thing_type::u64>() = thing->length();
|
||||||
break;
|
break;
|
||||||
default: throw std::runtime_error("unreachable");
|
default: throw std::runtime_error("unreachable");
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
case instruction_t::Lengthof: {
|
||||||
|
auto thing = pop_thing();
|
||||||
|
auto length = push_thing({ (struct thing_type){ thing_type::U64 }, m_context->thing_alloc() });
|
||||||
|
length->get<thing_type::u64>() = thing->length();
|
||||||
|
} break;
|
||||||
case instruction_t::Load: {
|
case instruction_t::Load: {
|
||||||
variable_t variable = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
|
variable_t variable = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
|
||||||
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8);
|
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8);
|
||||||
|
|||||||
+4
-2
@@ -24,7 +24,7 @@ static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
|
|||||||
case thing_type::U64: std::cout << thing.get<thing_type::u64>(); break;
|
case thing_type::U64: std::cout << thing.get<thing_type::u64>(); break;
|
||||||
case furvm::thing_type::Array:
|
case furvm::thing_type::Array:
|
||||||
std::cout << "{ ";
|
std::cout << "{ ";
|
||||||
for (thing_type::u64 i = 0; i < thing.size(); ++i) {
|
for (thing_type::u64 i = 0; i < thing.length(); ++i) {
|
||||||
if (i > 0) std::cout << ", ";
|
if (i > 0) std::cout << ", ";
|
||||||
print_thing(thing.at(i));
|
print_thing(thing.at(i));
|
||||||
}
|
}
|
||||||
@@ -59,6 +59,7 @@ int main(int argc, char** argv) {
|
|||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
static_cast<furvm::byte>(furvm::instruction_t::Reference),
|
static_cast<furvm::byte>(furvm::instruction_t::Reference),
|
||||||
|
static_cast<furvm::byte>(furvm::instruction_t::Lengthof),
|
||||||
static_cast<furvm::byte>(furvm::instruction_t::Call),
|
static_cast<furvm::byte>(furvm::instruction_t::Call),
|
||||||
1,
|
1,
|
||||||
0,
|
0,
|
||||||
@@ -68,8 +69,9 @@ 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 charType = mod->emplace_type(furvm::mod_type::S8);
|
auto charType = mod->emplace_type(furvm::mod_type::S8);
|
||||||
auto arrayType = mod->emplace_type(charType.id(), 10);
|
auto arrayType = mod->emplace_type(charType.id(), 10);
|
||||||
|
auto u64Type = mod->emplace_type(furvm::mod_type::U64);
|
||||||
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{ { u64Type } }, "print").dispatch();
|
||||||
#endif
|
#endif
|
||||||
mod->set_native_function("print", [](furvm::executor& executor) {
|
mod->set_native_function("print", [](furvm::executor& executor) {
|
||||||
print_thing(*executor.load_thing(0));
|
print_thing(*executor.load_thing(0));
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ std::ostream& mod::serialize(std::ostream& os) const {
|
|||||||
case mod_type::U16:
|
case mod_type::U16:
|
||||||
case mod_type::U32:
|
case mod_type::U32:
|
||||||
case mod_type::U64: break;
|
case mod_type::U64: break;
|
||||||
|
case mod_type::Ptr:
|
||||||
|
case mod_type::Ref: detail::serialize(os, type.value.typeRef); 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);
|
||||||
@@ -115,6 +117,11 @@ mod mod::load(std::istream& is) {
|
|||||||
case mod_type::U16:
|
case mod_type::U16:
|
||||||
case mod_type::U32:
|
case mod_type::U32:
|
||||||
case mod_type::U64: break;
|
case mod_type::U64: break;
|
||||||
|
case mod_type::Ptr: {
|
||||||
|
mod_type_id typeId = 0;
|
||||||
|
detail::load(is, typeId);
|
||||||
|
mod.emplace_type(id, typeId).dispatch();
|
||||||
|
} 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