refactor(furvm): improve primitive types

Improve primitive types and binary operations.

Closes: #55
This commit is contained in:
2026-07-11 02:28:23 +02:00
parent 3c0588e8db
commit 8d3859ce70
5 changed files with 316 additions and 102 deletions
+34 -21
View File
@@ -21,7 +21,14 @@ thing_type executor::thing_type_impl(mod_h mod, mod_type type) const {
type = *mod->type_at(imprt.typeId);
}
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: {
return { static_cast<enum thing_type::type>(type.type),
{ thing_type(mod, *mod->type_at(type.value.array.typeId)), type.value.array.size } };
@@ -117,7 +124,7 @@ void executor::step() {
switch (instr) {
case instruction_t::NoOperation: break;
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++);
} break;
case instruction_t::Array: {
@@ -215,31 +222,37 @@ void executor::step() {
push_thing(lhs->greater_equals(*rhs));
} break;
case instruction_t::Pointerof: {
auto thing = pop_thing();
auto ptr = push_thing(
{ (struct thing_type){ thing_type::Primitive, { sizeof(std::uintptr_t) } }, m_context->thing_alloc() });
switch (thing->type().type) {
case thing_type::Primitive: ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(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");
}
// auto thing = pop_thing();
// auto ptr = push_thing(
//{ (struct thing_type){ thing_type::Primitive, { sizeof(std::uintptr_t) } }, m_context->thing_alloc() });
// switch (thing->type().type) {
// case thing_type::Primitive: ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(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;
case instruction_t::Sizeof: {
auto thing = pop_thing();
auto size = push_thing(
{ (struct thing_type){ thing_type::Primitive, { sizeof(std::size_t) } }, m_context->thing_alloc() });
auto size = push_thing({ (struct thing_type){ thing_type::U64 }, m_context->thing_alloc() });
switch (thing->type().type) {
case thing_type::Primitive:
size->get<std::int64_t>() = static_cast<std::int64_t>(thing->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:
size->get<thing_type::u64>() = static_cast<thing_type::u64>(thing_type::primitive_size(thing->type().type));
break;
case thing_type::Array:
size->get<std::int64_t>() = static_cast<std::int64_t>((thing->type().value.array.size == 0)
? thing->get<furvm::thing<>::array>().dynamic.size
: thing->type().value.array.size);
size->get<thing_type::u64>() = static_cast<thing_type::u64>(
(thing->type().value.array.size == 0) ? thing->get<furvm::thing<>::array>().dynamic.size
: thing->type().value.array.size);
break;
default: throw std::runtime_error("unreachable");
}
+20 -10
View File
@@ -11,18 +11,25 @@
#include <sstream>
static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
using namespace furvm;
switch (thing.type().type) {
case furvm::thing_type::Primitive: {
std::cout << thing.integer();
} break;
case furvm::thing_type::Array: {
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::S32: std::cout << thing.get<thing_type::s32>(); break;
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 << "{ ";
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 << ", ";
print_thing(thing.at(i));
}
std::cout << " }\n";
} break;
std::cout << " }";
break;
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 intType = mod->emplace_type(sizeof(int));
auto arrayType = mod->emplace_type(intType.id(), 10);
auto charType = mod->emplace_type(furvm::mod_type::S8);
auto arrayType = mod->emplace_type(charType.id(), 10);
auto mainFunc = mod->emplace_function("main", furvm::function_sig{}, 0);
mod->emplace_function(furvm::function_sig{ { arrayType } }, "print").dispatch();
#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);
executor->push_frame(mod, *mainFunc);
+16 -7
View File
@@ -7,7 +7,6 @@
#include <cstdint>
#include <cstring>
#include <ios>
#include <memory>
#include <stdexcept>
#include <utility>
@@ -27,7 +26,14 @@ std::ostream& mod::serialize(std::ostream& os) const {
auto type = *m_types.at(id);
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: {
detail::serialize(os, type.value.array.typeId);
detail::serialize(os, type.value.array.size);
@@ -101,11 +107,14 @@ mod mod::load(std::istream& is) {
if (type == 0xFF) continue;
switch ((enum mod_type::type)type) {
case mod_type::Primitive: {
mod_type::primitive primitive = 0;
detail::load(is, primitive);
mod.emplace_type(id, primitive).dispatch();
} 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: {
mod_type_id typeId = 0;
detail::load(is, typeId);