Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
21e963168e
|
|||
|
b067bbb4ea
|
|||
|
fe37bf8058
|
|||
|
977b22d8f5
|
@@ -1,6 +1,7 @@
|
||||
type arr = array $s8 10
|
||||
type arrSlice = slice $s8
|
||||
|
||||
func println ref $arr = native println
|
||||
func println $arrSlice = native println
|
||||
|
||||
public func main = #main
|
||||
main:
|
||||
@@ -30,5 +31,8 @@ body:
|
||||
end:
|
||||
drop
|
||||
load %0
|
||||
call ref $arr println
|
||||
push $u32 0
|
||||
load %1
|
||||
slice
|
||||
call $arrSlice println
|
||||
ret
|
||||
|
||||
@@ -34,6 +34,7 @@ struct token {
|
||||
// Instructions
|
||||
Push,
|
||||
Array,
|
||||
Slice,
|
||||
Get,
|
||||
Set,
|
||||
Drop,
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "furvm/function.hpp"
|
||||
#include "furvm/fwd.hpp"
|
||||
#include "furvm/instruction.hpp"
|
||||
#include "furvm/module.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
@@ -24,6 +25,7 @@ namespace {
|
||||
// NOLINTBEGIN
|
||||
std::unordered_map<enum token::type, furvm::instruction_t> instructions = {
|
||||
{ token::Array, furvm::instruction_t::Array },
|
||||
{ token::Slice, furvm::instruction_t::Slice },
|
||||
{ token::Get, furvm::instruction_t::Get },
|
||||
{ token::Set, furvm::instruction_t::Set },
|
||||
{ token::Drop, furvm::instruction_t::Drop },
|
||||
@@ -77,6 +79,7 @@ const char* token_type(enum token::type type) {
|
||||
case token::Allocate: return "allocate";
|
||||
case token::Push: return "push";
|
||||
case token::Array: return "array";
|
||||
case token::Slice: return "slice";
|
||||
case token::Get: return "get";
|
||||
case token::Set: return "set";
|
||||
case token::Drop: return "drop";
|
||||
@@ -160,6 +163,8 @@ struct mod_context {
|
||||
case furvm::mod_type::Array:
|
||||
return lhs->value.array.size == rhs->value.array.size &&
|
||||
compare_types(mod.type_at(lhs->value.array.typeId), mod.type_at(rhs->value.array.typeId));
|
||||
case furvm::mod_type::Slice:
|
||||
return compare_types(mod.type_at(lhs->value.slice.typeId), mod.type_at(rhs->value.slice.typeId));
|
||||
case furvm::mod_type::Import:
|
||||
return lhs->value.imprt.modId == rhs->value.imprt.modId &&
|
||||
lhs->value.imprt.typeId == rhs->value.imprt.typeId;
|
||||
@@ -252,6 +257,15 @@ struct mod_context {
|
||||
|
||||
return mod.emplace_type(inner.id(), size->value.uint);
|
||||
}
|
||||
case token::Slice: {
|
||||
auto result = next_token(lexer);
|
||||
|
||||
if (!result) throw std::runtime_error("error");
|
||||
|
||||
auto inner = eat_type(lexer, result.value);
|
||||
|
||||
return mod.emplace_type(furvm::mod_type::Slice, inner.id());
|
||||
}
|
||||
default: throw std::runtime_error("error");
|
||||
}
|
||||
}
|
||||
@@ -463,6 +477,7 @@ struct mod_context {
|
||||
}
|
||||
|
||||
case token::Array:
|
||||
case token::Slice:
|
||||
case token::Get:
|
||||
case token::Set:
|
||||
case token::Drop:
|
||||
|
||||
@@ -43,6 +43,7 @@ token_r lexer::next_token() {
|
||||
|
||||
{ "push", token::Push },
|
||||
{ "array", token::Array },
|
||||
{ "slice", token::Slice },
|
||||
{ "get", token::Get },
|
||||
{ "set", token::Set },
|
||||
{ "drop", token::Drop },
|
||||
|
||||
@@ -56,6 +56,7 @@ struct instruction {
|
||||
PushU32,
|
||||
PushConstant,
|
||||
Array,
|
||||
Slice,
|
||||
Get,
|
||||
Set,
|
||||
Drop,
|
||||
|
||||
@@ -27,6 +27,10 @@ struct mod_type {
|
||||
std::size_t size;
|
||||
};
|
||||
|
||||
struct slice_value {
|
||||
mod_type_id typeId;
|
||||
};
|
||||
|
||||
struct import_value {
|
||||
mod_id modId;
|
||||
mod_type_id typeId;
|
||||
@@ -44,6 +48,7 @@ struct mod_type {
|
||||
Ptr,
|
||||
Ref,
|
||||
Array,
|
||||
Slice,
|
||||
|
||||
Import,
|
||||
Count,
|
||||
@@ -52,6 +57,7 @@ struct mod_type {
|
||||
std::nullptr_t null = nullptr;
|
||||
mod_type_id typeRef;
|
||||
array_value array;
|
||||
slice_value slice;
|
||||
import_value imprt;
|
||||
|
||||
value() = default;
|
||||
@@ -96,6 +102,7 @@ struct mod_type {
|
||||
~mod_type() {
|
||||
switch (type) {
|
||||
case Array: value.array.~array_value(); break;
|
||||
case Slice: value.slice.~slice_value(); break;
|
||||
case Import: value.imprt.~import_value(); break;
|
||||
default: break;
|
||||
}
|
||||
@@ -105,6 +112,7 @@ struct mod_type {
|
||||
: type(other.type) {
|
||||
switch (type) {
|
||||
case Array: new (&value.array) array_value(other.value.array); break;
|
||||
case Slice: new (&value.slice) slice_value(other.value.slice); break;
|
||||
case Import: new (&value.imprt) import_value(std::move(other.value.imprt)); break;
|
||||
default: break;
|
||||
}
|
||||
@@ -116,6 +124,7 @@ struct mod_type {
|
||||
type = other.type;
|
||||
switch (type) {
|
||||
case Array: new (&value.array) array_value(other.value.array); break;
|
||||
case Slice: new (&value.slice) slice_value(other.value.slice); break;
|
||||
case Import: new (&value.imprt) import_value(std::move(other.value.imprt)); break;
|
||||
default: break;
|
||||
}
|
||||
@@ -127,6 +136,7 @@ struct mod_type {
|
||||
: type(other.type) {
|
||||
switch (type) {
|
||||
case Array: new (&value.array) array_value(other.value.array); break;
|
||||
case Slice: new (&value.slice) slice_value(other.value.slice); break;
|
||||
case Import: new (&value.imprt) import_value(other.value.imprt); break;
|
||||
default: break;
|
||||
}
|
||||
@@ -137,6 +147,7 @@ struct mod_type {
|
||||
type = other.type;
|
||||
switch (type) {
|
||||
case Array: new (&value.array) array_value(other.value.array); break;
|
||||
case Slice: new (&value.slice) slice_value(other.value.slice); break;
|
||||
case Import: new (&value.imprt) import_value(other.value.imprt); break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#ifndef FURVM_STACK_HPP
|
||||
#define FURVM_STACK_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <new>
|
||||
#include <stack>
|
||||
|
||||
namespace furvm {
|
||||
|
||||
template <typename T>
|
||||
struct stack {
|
||||
stack(std::size_t capacity = (1024ULL * 1024ULL) / sizeof(T))
|
||||
: begin(new T[capacity]()), cursor(begin), capacity(capacity) {}
|
||||
|
||||
T* begin;
|
||||
T* cursor;
|
||||
std::size_t capacity;
|
||||
std::stack<T*> frames;
|
||||
|
||||
void push_frame() { frames.push(cursor); }
|
||||
|
||||
void pop_frame() {
|
||||
cursor = frames.top();
|
||||
frames.pop();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class stack_allocator {
|
||||
public:
|
||||
stack_allocator() = default;
|
||||
|
||||
stack_allocator(stack<T>& stack)
|
||||
: m_ref(&stack) {}
|
||||
|
||||
template <typename U>
|
||||
constexpr stack_allocator(const stack_allocator<U>& other) noexcept
|
||||
: m_ref(other.m_ref) {}
|
||||
public:
|
||||
T* allocate(std::size_t n) {
|
||||
if (m_ref == nullptr) throw std::bad_alloc();
|
||||
if (m_ref->capacity - (m_ref->cursor - m_ref->begin) < n) throw std::bad_alloc();
|
||||
|
||||
T* ptr = m_ref->cursor;
|
||||
m_ref->cursor += n;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void deallocate(T* ptr, std::size_t n) {}
|
||||
private:
|
||||
stack<T>* m_ref = nullptr;
|
||||
};
|
||||
|
||||
} // namespace furvm
|
||||
|
||||
#endif // FURVM_STACK_HPP
|
||||
@@ -35,6 +35,10 @@ struct thing_type {
|
||||
std::size_t size;
|
||||
};
|
||||
|
||||
struct slice_value {
|
||||
thing_type* type;
|
||||
};
|
||||
|
||||
enum type { // NOLINT
|
||||
S8 = 0,
|
||||
S16,
|
||||
@@ -47,6 +51,7 @@ struct thing_type {
|
||||
Ptr,
|
||||
Ref,
|
||||
Array,
|
||||
Slice,
|
||||
|
||||
Count,
|
||||
} type = Count;
|
||||
@@ -54,6 +59,7 @@ struct thing_type {
|
||||
std::nullptr_t null = nullptr;
|
||||
thing_type* typeRef;
|
||||
array_value array;
|
||||
slice_value slice;
|
||||
|
||||
value() = default;
|
||||
|
||||
@@ -85,6 +91,7 @@ struct thing_type {
|
||||
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 Slice: return *value.slice.type == *other.value.slice.type;
|
||||
case Count: break;
|
||||
}
|
||||
return false;
|
||||
@@ -104,7 +111,8 @@ struct thing_type {
|
||||
case U64: return true;
|
||||
case Ptr:
|
||||
case Ref:
|
||||
case Array: return false;
|
||||
case Array:
|
||||
case Slice: return false;
|
||||
case Count: break;
|
||||
}
|
||||
throw std::runtime_error("unreachable");
|
||||
@@ -122,7 +130,8 @@ struct thing_type {
|
||||
case thing_type::U64: return sizeof(u64);
|
||||
case Ptr:
|
||||
case Ref:
|
||||
case Array: return 0;
|
||||
case Array:
|
||||
case Slice: return 0;
|
||||
case Count: break;
|
||||
}
|
||||
throw std::runtime_error("unreachable");
|
||||
@@ -150,6 +159,7 @@ struct thing_type_hash {
|
||||
seed = furlang::utility::hash_combine(seed,
|
||||
std::hash<decltype(type.value.array.size)>{}(type.value.array.size));
|
||||
return seed;
|
||||
case thing_type::Slice: return furlang::utility::hash_combine(seed, thing_type_hash{}(*type.value.slice.type));
|
||||
case thing_type::Count: break;
|
||||
}
|
||||
throw std::runtime_error("unreachable");
|
||||
@@ -205,6 +215,11 @@ public:
|
||||
std::byte* data;
|
||||
};
|
||||
|
||||
struct slice {
|
||||
std::size_t length;
|
||||
std::byte* data;
|
||||
};
|
||||
|
||||
struct header {
|
||||
thing_type type;
|
||||
};
|
||||
@@ -363,7 +378,8 @@ private:
|
||||
case thing_type::U16:
|
||||
case thing_type::U32:
|
||||
case thing_type::U64:
|
||||
case thing_type::Ptr: std::memcpy(dst.m_data, m_data, m_size); return;
|
||||
case thing_type::Ptr:
|
||||
case thing_type::Slice: std::memcpy(dst.m_data, m_data, m_size); return;
|
||||
case thing_type::Array: copy_list(*m_type, dst.m_data, m_data); return;
|
||||
case thing_type::Ref: // TODO: Implement arrays of references (I think they're possible).
|
||||
case thing_type::Count: break;
|
||||
@@ -551,31 +567,69 @@ public:
|
||||
}
|
||||
|
||||
thing at(thing_type::u64 index) const {
|
||||
if (!is(thing_type::Array)) throw bad_thing_access();
|
||||
|
||||
thing ref = { m_allocator };
|
||||
ref.m_reference = true;
|
||||
ref.m_size = compute_size_na(*type().value.array.type);
|
||||
ref.m_size = compute_size_na(*(ref.m_type = &inner_type()));
|
||||
|
||||
if (type().value.array.size == 0) {
|
||||
auto& array = get<dynamic_array>();
|
||||
if (index < 0 || index >= array.size) throw std::out_of_range("index out of range");
|
||||
switch (type().type) {
|
||||
case thing_type::Array: {
|
||||
if (type().value.array.size == 0) {
|
||||
auto& array = get<dynamic_array>();
|
||||
if (index < 0 || index >= array.size) throw std::out_of_range("index out of range");
|
||||
|
||||
ref.m_type = type().value.array.type;
|
||||
ref.m_data = array.data + (index * ref.m_size);
|
||||
ref.m_data = array.data + (index * ref.m_size);
|
||||
return ref;
|
||||
}
|
||||
|
||||
if (index < 0 || index >= type().value.array.size) throw std::out_of_range("index out of range");
|
||||
ref.m_data = m_data + (index * ref.m_size);
|
||||
return ref;
|
||||
}
|
||||
case thing_type::Slice: {
|
||||
const auto& slice = get<struct slice>();
|
||||
if (index < 0 || index >= slice.length) throw std::out_of_range("index out of range");
|
||||
ref.m_data = slice.data + (index * ref.m_size);
|
||||
return ref;
|
||||
}
|
||||
default: throw bad_thing_access();
|
||||
}
|
||||
}
|
||||
|
||||
if (index < 0 || index >= type().value.array.size) throw std::out_of_range("index out of range");
|
||||
thing slice(thing_type::u64 begin, thing_type::u64 len) const {
|
||||
auto& inner = inner_type();
|
||||
|
||||
ref.m_type = type().value.array.type;
|
||||
ref.m_data = m_data + (index * ref.m_size);
|
||||
return ref;
|
||||
thing_type sliceType;
|
||||
sliceType.type = thing_type::Slice;
|
||||
sliceType.value.slice.type = &inner;
|
||||
|
||||
thing slice = { sliceType, m_allocator };
|
||||
auto& data = slice.get<struct slice>();
|
||||
|
||||
if (begin >= length()) throw std::out_of_range("begin index out of range");
|
||||
len = std::min(len, length() - begin);
|
||||
|
||||
switch (type().type) {
|
||||
case thing_type::Array: {
|
||||
data.data = ((type().value.array.size != 0) ? m_data : get<dynamic_array>().data) ;
|
||||
} break;
|
||||
case thing_type::Slice: {
|
||||
data.data = get<struct slice>().data ;
|
||||
} break;
|
||||
default: throw bad_thing_access();
|
||||
}
|
||||
|
||||
data.data += (compute_size_na(inner) * begin);
|
||||
data.length = len;
|
||||
return slice;
|
||||
}
|
||||
|
||||
thing_type::u64 length() const {
|
||||
if (!is(thing_type::Array)) throw bad_thing_access();
|
||||
return type().value.array.size == 0 ? get<dynamic_array>().size : type().value.array.size;
|
||||
switch (type().type) {
|
||||
case thing_type::Array:
|
||||
return type().value.array.size == 0 ? get<dynamic_array>().size : type().value.array.size;
|
||||
case thing_type::Slice: return get<struct slice>().length;
|
||||
default: throw bad_thing_access();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
|
||||
@@ -603,7 +657,8 @@ public:
|
||||
case thing_type::U64: std::memcpy(m_data, rhs.m_data, m_size); return;
|
||||
case thing_type::Ptr:
|
||||
case thing_type::Ref:
|
||||
case thing_type::Array: throw std::runtime_error("unimplemented");
|
||||
case thing_type::Array:
|
||||
case thing_type::Slice: throw std::runtime_error("unimplemented");
|
||||
case thing_type::Count: break;
|
||||
}
|
||||
throw std::runtime_error("unreachable");
|
||||
@@ -643,7 +698,8 @@ private:
|
||||
case thing_type::U32:
|
||||
case thing_type::U64:
|
||||
case thing_type::Ptr:
|
||||
case thing_type::Ref: std::memcpy(dst, src, size * elementSize); return;
|
||||
case thing_type::Ref:
|
||||
case thing_type::Slice: std::memcpy(dst, src, size * elementSize); return;
|
||||
case thing_type::Array:
|
||||
for (std::size_t i = 0; i < size; ++i) {
|
||||
copy_list(*innerType.value.array.type,
|
||||
@@ -670,6 +726,7 @@ private:
|
||||
case thing_type::Array:
|
||||
return type.value.array.size == 0 ? sizeof(dynamic_array)
|
||||
: compute_size_na(*type.value.array.type) * type.value.array.size;
|
||||
case thing_type::Slice: return sizeof(struct slice);
|
||||
case thing_type::Ref:
|
||||
case thing_type::Count: break;
|
||||
}
|
||||
@@ -804,6 +861,7 @@ private:
|
||||
case thing_type::Ptr: // TODO: Pointer arithmetics
|
||||
case thing_type::Ref:
|
||||
case thing_type::Array:
|
||||
case thing_type::Slice:
|
||||
case thing_type::Count: break;
|
||||
}
|
||||
throw std::runtime_error("unreachable");
|
||||
@@ -828,6 +886,14 @@ private:
|
||||
m_data = nullptr;
|
||||
m_type = nullptr;
|
||||
}
|
||||
|
||||
thing_type& inner_type() const {
|
||||
switch (type().type) {
|
||||
case thing_type::Array: return *type().value.array.type;
|
||||
case thing_type::Slice: return *type().value.slice.type;
|
||||
default: throw bad_thing_access();
|
||||
}
|
||||
}
|
||||
private:
|
||||
// A flag indicating whether the thing instance owns the data, or not.
|
||||
bool m_reference = false;
|
||||
|
||||
+10
-1
@@ -38,6 +38,8 @@ thing_type executor::thing_type_impl(mod_h mod, mod_type type) const {
|
||||
return { static_cast<enum thing_type::type>(type.type),
|
||||
{ mod_to_thing_type(mod, *mod->type_at(type.value.array.typeId)), type.value.array.size } };
|
||||
}
|
||||
case thing_type::Slice:
|
||||
return { thing_type::Slice, mod_to_thing_type(mod, *mod->type_at(type.value.slice.typeId)) };
|
||||
case thing_type::Count: break;
|
||||
}
|
||||
throw std::runtime_error("invalid thing type");
|
||||
@@ -64,6 +66,7 @@ bool executor::compare_thing_types(const thing_type& lhs, const thing_type& rhs)
|
||||
case thing_type::Array:
|
||||
return lhs.value.array.size == rhs.value.array.size &&
|
||||
compare_thing_types(*lhs.value.array.type, *rhs.value.array.type);
|
||||
case thing_type::Slice: return compare_thing_types(*lhs.value.slice.type, *rhs.value.slice.type);
|
||||
case thing_type::Count: break;
|
||||
}
|
||||
throw std::runtime_error("unreachable");
|
||||
@@ -83,7 +86,7 @@ void executor::push_frame(const mod_h& mod, function function) {
|
||||
args.reserve(signature.params.size());
|
||||
for (const auto& param : signature.params) {
|
||||
auto arg = pop_thing();
|
||||
if (compare_thing_types(arg.type(), *mod_to_thing_type(mod, *param)))
|
||||
if (!compare_thing_types(arg.type(), *mod_to_thing_type(mod, *param)))
|
||||
throw std::runtime_error("function argument type mismatch");
|
||||
args.emplace_back(std::move(arg));
|
||||
}
|
||||
@@ -265,6 +268,12 @@ void executor::step() {
|
||||
array.resize(size);
|
||||
}
|
||||
} break;
|
||||
case instruction_t::Slice: {
|
||||
auto length = pop_thing();
|
||||
auto start = pop_thing();
|
||||
auto array = pop_thing();
|
||||
push_thing(array.slice(start.integer(), length.integer()));
|
||||
} break;
|
||||
case instruction_t::Get: {
|
||||
auto index = pop_thing();
|
||||
auto array = pop_thing();
|
||||
|
||||
@@ -83,6 +83,8 @@ const instruction_argument_t instruction::s_arguments[instruction::Count] = {
|
||||
instruction_argument::Constant,
|
||||
// Array:
|
||||
instruction_argument::Type,
|
||||
// Slice:
|
||||
instruction_argument::None,
|
||||
// Get:
|
||||
instruction_argument::None,
|
||||
// Set:
|
||||
|
||||
@@ -30,6 +30,14 @@ static void print_thing(const furvm::thing<>& thing) {
|
||||
}
|
||||
std::cout << " }";
|
||||
break;
|
||||
case thing_type::Slice:
|
||||
std::cout << "Slice [" << thing.length() << "] { ";
|
||||
for (thing_type::u64 i = 0; i < thing.length(); ++i) {
|
||||
if (i > 0) std::cout << ", ";
|
||||
print_thing(thing.at(i));
|
||||
}
|
||||
std::cout << " }";
|
||||
break;
|
||||
default: std::cerr << "{Type not recognized}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,9 @@ std::ostream& mod::serialize(std::ostream& os) const {
|
||||
detail::serialize(os, type.value.array.typeId);
|
||||
detail::serialize(os, type.value.array.size);
|
||||
} break;
|
||||
case mod_type::Slice: {
|
||||
detail::serialize(os, type.value.slice.typeId);
|
||||
} break;
|
||||
case mod_type::Import: {
|
||||
detail::serialize(os, type.value.imprt.modId);
|
||||
detail::serialize(os, type.value.imprt.typeId);
|
||||
@@ -169,6 +172,11 @@ mod mod::load(std::istream& is) {
|
||||
detail::load(is, size);
|
||||
mod.emplace_type(id, typeId, size).dispatch();
|
||||
} break;
|
||||
case mod_type::Slice: {
|
||||
mod_type_id typeId = 0;
|
||||
detail::load(is, typeId);
|
||||
mod.emplace_type(id, mod_type::Slice, typeId).dispatch();
|
||||
} break;
|
||||
case mod_type::Import: {
|
||||
std::string modName;
|
||||
detail::load(is, modName);
|
||||
|
||||
Reference in New Issue
Block a user