From 668851f6bf57cd24a63c219772db43f21d7d18c1 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Tue, 6 Jan 2026 12:45:12 +0300 Subject: [PATCH] Aarch64 jit compiler wip: fix loading/storing floating-point values --- apps/interpreter/source/main.cpp | 2 +- examples/jit_test.psl | 5 +++-- libs/jit/source/arch/aarch64/compiler.cpp | 6 ++---- .../arch/aarch64/instruction_builder.cpp | 4 ++-- libs/types/include/pslang/types/type_fwd.hpp | 2 ++ libs/types/source/type.cpp | 18 ++++++++++++++++++ 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/apps/interpreter/source/main.cpp b/apps/interpreter/source/main.cpp index 48b888d..afad3f0 100644 --- a/apps/interpreter/source/main.cpp +++ b/apps/interpreter/source/main.cpp @@ -170,7 +170,7 @@ int main(int argc, char ** argv) { // TODO: remove, testing-only code; should execute entry point instead auto offset = module.code.symbol_table.at("test"); - auto fptr = (int(*)())(module.code.memory.data.get() + offset); + auto fptr = (float(*)())(module.code.memory.data.get() + offset); auto x = fptr(); std::cout << "Result: " << std::boolalpha << x << std::endl; } diff --git a/examples/jit_test.psl b/examples/jit_test.psl index 04c8f30..6620a08 100644 --- a/examples/jit_test.psl +++ b/examples/jit_test.psl @@ -1,2 +1,3 @@ -func test() -> i32: - return 3.141592h as i32 +func test() -> f32: + let pi : f16 = 3.141592h + return (pi * pi) as f32 diff --git a/libs/jit/source/arch/aarch64/compiler.cpp b/libs/jit/source/arch/aarch64/compiler.cpp index fd44e57..9101129 100644 --- a/libs/jit/source/arch/aarch64/compiler.cpp +++ b/libs/jit/source/arch/aarch64/compiler.cpp @@ -334,7 +334,7 @@ namespace pslang::jit::aarch64 if (auto jt = it->variables.find(node.name); jt != it->variables.end()) { if (types::is_floating_point_type(*node.inferred_type)) - builder.ldr_fp(0, fp_mode_for(*node.inferred_type), 31, (stack_offset - jt->second.frame_offset) / 4); + builder.ldr_fp(0, fp_mode_for(*node.inferred_type), 31, (stack_offset - jt->second.frame_offset) / builtin_type_size(*node.inferred_type)); else builder.ldr(0, 31, (stack_offset - jt->second.frame_offset) / 8); break; @@ -601,7 +601,7 @@ namespace pslang::jit::aarch64 { auto type = ast::get_type(*node.rhs); if (types::is_floating_point_type(*type)) - builder.str_fp(0, fp_mode_for(*type), 31, (stack_offset - jt->second.frame_offset) / 4); + builder.str_fp(0, fp_mode_for(*type), 31, (stack_offset - jt->second.frame_offset) / builtin_type_size(*type)); else builder.str(0, 31, (stack_offset - jt->second.frame_offset) / 8); break; @@ -753,8 +753,6 @@ namespace pslang::jit::aarch64 scopes.back().stack_offset -= 16; } - // @mode = 0: 32-bit - // @mode = 1: 64-bit void push_fp(std::uint8_t reg, std::uint8_t mode) { builder.sub_imm(31, 31, 16); diff --git a/libs/jit/source/arch/aarch64/instruction_builder.cpp b/libs/jit/source/arch/aarch64/instruction_builder.cpp index ebfc136..b3e61d8 100644 --- a/libs/jit/source/arch/aarch64/instruction_builder.cpp +++ b/libs/jit/source/arch/aarch64/instruction_builder.cpp @@ -166,12 +166,12 @@ namespace pslang::jit::aarch64 void instruction_builder::ldr_fp(std::uint8_t reg_dst, std::uint8_t mode, std::uint8_t reg_addr, std::uint16_t offset) { - do_push(0xbd400000u | (reg_dst & REG_MASK) | ((reg_addr & REG_MASK) << 5) | ((offset & 0xfffu) << 10) | ((mode & 0x3u) << 30)); + do_push(0x3d400000u | (reg_dst & REG_MASK) | ((reg_addr & REG_MASK) << 5) | ((offset & 0xfffu) << 10) | ((mode & 0x3u) << 30)); } void instruction_builder::str_fp(std::uint8_t reg_src, std::uint8_t mode, std::uint8_t reg_addr, std::uint16_t offset) { - do_push(0xbd000000u | (reg_src & REG_MASK) | ((reg_addr & REG_MASK) << 5) | ((offset & 0xfffu) << 10) | ((mode & 0x3u) << 30)); + do_push(0x3d000000u | (reg_src & REG_MASK) | ((reg_addr & REG_MASK) << 5) | ((offset & 0xfffu) << 10) | ((mode & 0x3u) << 30)); } void instruction_builder::fcvt(std::uint8_t reg_src, std::uint8_t mode_src, std::uint8_t reg_dst, std::uint8_t mode_dst) diff --git a/libs/types/include/pslang/types/type_fwd.hpp b/libs/types/include/pslang/types/type_fwd.hpp index 7197c70..284e368 100644 --- a/libs/types/include/pslang/types/type_fwd.hpp +++ b/libs/types/include/pslang/types/type_fwd.hpp @@ -23,4 +23,6 @@ namespace pslang::types bool is_numeric_type(type const & type); bool is_builtin_type(type const & type); + std::size_t builtin_type_size(type const & type); + } diff --git a/libs/types/source/type.cpp b/libs/types/source/type.cpp index 1d32638..eedc91e 100644 --- a/libs/types/source/type.cpp +++ b/libs/types/source/type.cpp @@ -123,4 +123,22 @@ namespace pslang::types return false; } + std::size_t builtin_type_size(type const & type) + { + if (std::get_if(&type)) + return 1; + + if (auto ptype = std::get_if(&type)) + { + return std::visit([](primitive_type_base const &) -> std::size_t + { + if constexpr (std::is_same_v) + return 2; + return sizeof(T); + }, *ptype); + } + + return 0; + } + }