fix: use thing::true_type and fix module serialization
This commit is contained in:
@@ -433,7 +433,7 @@ public:
|
|||||||
* @return The integer value.
|
* @return The integer value.
|
||||||
*/
|
*/
|
||||||
thing_type::s64 integer() const {
|
thing_type::s64 integer() const {
|
||||||
switch (m_type.type) {
|
switch (true_type().type) {
|
||||||
case thing_type::S8: return get<thing_type::s8>();
|
case thing_type::S8: return get<thing_type::s8>();
|
||||||
case thing_type::S16: return get<thing_type::s16>();
|
case thing_type::S16: return get<thing_type::s16>();
|
||||||
case thing_type::S32: return get<thing_type::s32>();
|
case thing_type::S32: return get<thing_type::s32>();
|
||||||
@@ -448,11 +448,11 @@ public:
|
|||||||
|
|
||||||
void resize(thing_type::u64 newSize) {
|
void resize(thing_type::u64 newSize) {
|
||||||
if (!is(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 (true_type().value.array.size > 0) throw std::runtime_error("cannot resize a static array");
|
||||||
|
|
||||||
auto& array = get<union array>();
|
auto& array = get<union array>();
|
||||||
if (newSize < 0 || newSize == array.dynamic.size) return;
|
if (newSize < 0 || newSize == array.dynamic.size) return;
|
||||||
std::size_t innerSize = compute_size_na(*m_type.value.array.type);
|
std::size_t innerSize = compute_size_na(*true_type().value.array.type);
|
||||||
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,
|
||||||
@@ -577,7 +577,7 @@ private:
|
|||||||
private:
|
private:
|
||||||
template <typename Func>
|
template <typename Func>
|
||||||
decltype(auto) visit_primitive(Func&& func) const {
|
decltype(auto) visit_primitive(Func&& func) const {
|
||||||
switch (m_type.type) {
|
switch (true_type().type) {
|
||||||
case thing_type::S8: return std::forward<Func>(func)(get<thing_type::s8>());
|
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::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::S32: return std::forward<Func>(func)(get<thing_type::s32>());
|
||||||
@@ -592,7 +592,7 @@ private:
|
|||||||
|
|
||||||
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 (thing_type::is_primitive(m_type.type) && thing_type::is_primitive(rhs.m_type.type)) {
|
if (thing_type::is_primitive(true_type().type) && thing_type::is_primitive(true_type().type)) {
|
||||||
static constexpr enum thing_type::type promotions[8 * 8] = {
|
static constexpr enum thing_type::type promotions[8 * 8] = {
|
||||||
// S8
|
// S8
|
||||||
thing_type::S8,
|
thing_type::S8,
|
||||||
|
|||||||
+6
-7
@@ -13,7 +13,7 @@
|
|||||||
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;
|
using namespace furvm;
|
||||||
|
|
||||||
switch (thing.type().type) {
|
switch (thing.true_type().type) {
|
||||||
case thing_type::S8: std::cout << thing.cast_to<thing_type::s16>(); break;
|
case thing_type::S8: std::cout << thing.cast_to<thing_type::s16>(); break;
|
||||||
case thing_type::S16: std::cout << thing.get<thing_type::s16>(); break;
|
case thing_type::S16: std::cout << thing.get<thing_type::s16>(); break;
|
||||||
case thing_type::S32: std::cout << thing.get<thing_type::s32>(); break;
|
case thing_type::S32: std::cout << thing.get<thing_type::s32>(); break;
|
||||||
@@ -22,7 +22,7 @@ static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
|
|||||||
case thing_type::U16: std::cout << thing.get<thing_type::u16>(); 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::U32: std::cout << thing.get<thing_type::u32>(); break;
|
||||||
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 thing_type::Array:
|
||||||
std::cout << "{ ";
|
std::cout << "{ ";
|
||||||
for (thing_type::u64 i = 0; i < thing.length(); ++i) {
|
for (thing_type::u64 i = 0; i < thing.length(); ++i) {
|
||||||
if (i > 0) std::cout << ", ";
|
if (i > 0) std::cout << ", ";
|
||||||
@@ -37,20 +37,20 @@ static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
|
|||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
auto context = std::make_shared<furvm::context>();
|
auto context = std::make_shared<furvm::context>();
|
||||||
|
|
||||||
#if 0 // NOLINT
|
#if 1 // NOLINT
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
std::cerr << "Usage: " << argv[0] << " <mod.fmod>\n";
|
std::cerr << "Usage: " << argv[0] << " <mod.fmod>\n";
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::ifstream file(argv[1]);
|
std::ifstream file(argv[1]);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
std::cerr << "Failed to open " << argv[1] << '\n';
|
std::cerr << "Failed to open " << argv[1] << '\n';
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
furvm::mod_h mod = context->emplace("main", std::move(furvm::mod::load(file)));
|
furvm::mod_h mod = context->emplace("main", std::move(furvm::mod::load(file)));
|
||||||
|
auto mainFunc = mod->function_at("main", furvm::function_sig{});
|
||||||
#else
|
#else
|
||||||
static const furvm::byte s_bytecode[] = {
|
static const furvm::byte s_bytecode[] = {
|
||||||
static_cast<furvm::byte>(furvm::instruction_t::Array),
|
static_cast<furvm::byte>(furvm::instruction_t::Array),
|
||||||
@@ -74,11 +74,10 @@ int main(int argc, char** argv) {
|
|||||||
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{ { u64Type }, u64Type }, "print").dispatch();
|
mod->emplace_function(furvm::function_sig{ { u64Type }, u64Type }, "print").dispatch();
|
||||||
#endif
|
#endif
|
||||||
mod->set_native_function("print", [](furvm::executor& executor) {
|
mod->set_native_function("println", [](furvm::executor& executor) {
|
||||||
auto arg = std::move(*executor.load_thing(0));
|
auto arg = std::move(*executor.load_thing(0));
|
||||||
print_thing(arg);
|
print_thing(arg);
|
||||||
std::cout << '\n';
|
std::cout << '\n';
|
||||||
executor.push_thing(std::move(arg));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
furvm::executor_h executor = context->emplace_executor(context);
|
furvm::executor_h executor = context->emplace_executor(context);
|
||||||
|
|||||||
+10
-6
@@ -25,6 +25,7 @@ std::ostream& mod::serialize(std::ostream& os) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto type = *m_types.at(id);
|
auto type = *m_types.at(id);
|
||||||
|
detail::serialize(os, static_cast<std::uint8_t>(type.type));
|
||||||
switch (type.type) {
|
switch (type.type) {
|
||||||
case mod_type::S8:
|
case mod_type::S8:
|
||||||
case mod_type::S16:
|
case mod_type::S16:
|
||||||
@@ -67,7 +68,7 @@ std::ostream& mod::serialize(std::ostream& os) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Function signature
|
// Function signature
|
||||||
detail::serialize(os, func->signature().params.size());
|
detail::serialize(os, static_cast<std::uint32_t>(func->signature().params.size()));
|
||||||
for (std::uint32_t i = 0; i < func->signature().params.size(); ++i)
|
for (std::uint32_t i = 0; i < func->signature().params.size(); ++i)
|
||||||
detail::serialize(os, func->signature().params[i].id());
|
detail::serialize(os, func->signature().params[i].id());
|
||||||
|
|
||||||
@@ -116,7 +117,10 @@ mod mod::load(std::istream& is) {
|
|||||||
case mod_type::U8:
|
case mod_type::U8:
|
||||||
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: {
|
||||||
|
mod_type theType = { (enum mod_type::type)type };
|
||||||
|
mod.emplace_type(id, theType).dispatch();
|
||||||
|
} break;
|
||||||
case mod_type::Ptr: {
|
case mod_type::Ptr: {
|
||||||
mod_type_id typeId = 0;
|
mod_type_id typeId = 0;
|
||||||
detail::load(is, typeId);
|
detail::load(is, typeId);
|
||||||
@@ -165,17 +169,17 @@ mod mod::load(std::istream& is) {
|
|||||||
decltype(std::declval<function>().position()) offset = 0;
|
decltype(std::declval<function>().position()) offset = 0;
|
||||||
detail::load(is, offset);
|
detail::load(is, offset);
|
||||||
if (name.empty())
|
if (name.empty())
|
||||||
mod.emplace_function(std::move(name), id, std::move(signature), offset).dispatch();
|
|
||||||
else
|
|
||||||
mod.emplace_function(id, std::move(signature), offset).dispatch();
|
mod.emplace_function(id, std::move(signature), offset).dispatch();
|
||||||
|
else
|
||||||
|
mod.emplace_function(std::move(name), id, std::move(signature), offset).dispatch();
|
||||||
} break;
|
} break;
|
||||||
case function_t::Native: {
|
case function_t::Native: {
|
||||||
std::string native;
|
std::string native;
|
||||||
detail::load(is, native);
|
detail::load(is, native);
|
||||||
if (name.empty())
|
if (name.empty())
|
||||||
mod.emplace_function(std::move(name), id, std::move(signature), std::move(native)).dispatch();
|
|
||||||
else
|
|
||||||
mod.emplace_function(id, std::move(signature), std::move(native)).dispatch();
|
mod.emplace_function(id, std::move(signature), std::move(native)).dispatch();
|
||||||
|
else
|
||||||
|
mod.emplace_function(std::move(name), id, std::move(signature), std::move(native)).dispatch();
|
||||||
} break;
|
} break;
|
||||||
case function_t::Import: {
|
case function_t::Import: {
|
||||||
std::string modName;
|
std::string modName;
|
||||||
|
|||||||
Reference in New Issue
Block a user