Aarch64 jit compiler wip: suppport floating-point variables

This commit is contained in:
Nikita Lisitsa 2026-01-05 10:46:53 +03:00
parent c9f4a9c19d
commit 12a4f64aed
3 changed files with 27 additions and 10 deletions

View file

@ -14,8 +14,6 @@
#include <cstring> #include <cstring>
#include <cmath> #include <cmath>
#include <chrono>
std::string extract_nth_line(std::filesystem::path const & path, std::size_t n) std::string extract_nth_line(std::filesystem::path const & path, std::size_t n)
{ {
std::ifstream file(path); std::ifstream file(path);
@ -171,9 +169,9 @@ int main(int argc, char ** argv)
for (auto const & module : modules) for (auto const & module : modules)
{ {
// TODO: remove, testing-only code; should execute entry point instead // TODO: remove, testing-only code; should execute entry point instead
auto offset = module.code.symbol_table.at("test"); auto offset = module.code.symbol_table.at("pow");
auto fptr = (float(*)(float))(module.code.memory.data.get() + offset); auto fptr = (float(*)(float, unsigned))(module.code.memory.data.get() + offset);
auto x = fptr(6.f); auto x = fptr(1.5f, 12);
std::cout << "Result: " << std::boolalpha << x << std::endl; std::cout << "Result: " << std::boolalpha << x << std::endl;
} }
} }

View file

@ -1,2 +1,10 @@
func test(x : f32) -> f32: func pow(x : f32, n : u32) -> f32:
return x * 1.5 mut r = 1.0
mut f = x // x^k
mut k = 1u
while k <= n:
if (n & k) != 0u:
r = r * f
f = f * f
k = k + k
return r

View file

@ -333,7 +333,10 @@ namespace pslang::jit::aarch64
{ {
if (auto jt = it->variables.find(node.name); jt != it->variables.end()) if (auto jt = it->variables.find(node.name); jt != it->variables.end())
{ {
builder.ldr(0, 31, (stack_offset - jt->second.frame_offset) / 8); 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);
else
builder.ldr(0, 31, (stack_offset - jt->second.frame_offset) / 8);
break; break;
} }
} }
@ -537,7 +540,11 @@ namespace pslang::jit::aarch64
{ {
if (auto jt = it->variables.find(identifier->name); jt != it->variables.end()) if (auto jt = it->variables.find(identifier->name); jt != it->variables.end())
{ {
builder.str(0, 31, (stack_offset - jt->second.frame_offset) / 8); 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);
else
builder.str(0, 31, (stack_offset - jt->second.frame_offset) / 8);
break; break;
} }
} }
@ -546,7 +553,11 @@ namespace pslang::jit::aarch64
void apply(ast::variable_declaration const & node) void apply(ast::variable_declaration const & node)
{ {
apply(*node.initializer); apply(*node.initializer);
push(0); auto type = ast::get_type(*node.initializer);
if (types::is_floating_point_type(*type))
push_fp(0, fp_mode_for(*type));
else
push(0);
scopes.back().variables[node.name] = {.frame_offset = stack_offset}; scopes.back().variables[node.name] = {.frame_offset = stack_offset};
} }