Aarch64 compiler cleanup

This commit is contained in:
Nikita Lisitsa 2026-07-28 23:20:14 +03:00
parent 2c11ee39de
commit 8aec010bd0

View file

@ -7,8 +7,6 @@
#include <pslang/ast/function.hpp>
#include <pslang/types/type_visitor.hpp>
#include <sstream>
namespace pslang::jit::macos_aarch64
{
@ -342,8 +340,7 @@ namespace pslang::jit::macos_aarch64
void apply(ir::node_ref it, ir::global const & node, types::type_ptr const &)
{
// TODO
throw std::runtime_error("Not implemented");
// Globals are added in a separate pass before code
}
void apply(ir::node_ref it, ir::copy const & node, types::type_ptr const & type)
@ -356,9 +353,19 @@ namespace pslang::jit::macos_aarch64
auto src_address = node_address(node.source, 1);
for (auto field_id : node.path)
{
auto const & field = std::get<types::struct_type>(*src_type).node->fields[field_id];
src_type = field.inferred_type;
src_address.offset += field.layout.offset;
if (auto struct_type = std::get_if<types::struct_type>(src_type.get()))
{
auto struct_node = struct_type->node;
src_type = struct_node->fields[field_id].inferred_type;
src_address.offset += struct_node->fields[field_id].layout.offset;
}
else if (auto array_type = std::get_if<types::array_type>(src_type.get()))
{
src_type = array_type->element_type;
src_address.offset += field_id * ast::type_size(*array_type->element_type);
}
else
throw std::runtime_error("Unknown object type for field copy");
}
copy_memory(src_address.reg, src_address.offset, dst_address.reg, dst_address.offset, size, 2);
@ -611,12 +618,6 @@ namespace pslang::jit::macos_aarch64
builder.csetm(0, 0b1101);
}
break;
default:
{
std::ostringstream os;
os << "binary operation " << node.type << " is not implemented";
throw std::runtime_error(os.str());
}
}
if (result_is_fp)
@ -1010,6 +1011,10 @@ namespace pslang::jit::macos_aarch64
{
stack_position[it] = argument_position[argument->index];
}
else if (std::holds_alternative<ir::global>(it->instruction))
{
// stack position doesn't make sense for globals
}
else if (ir::is_value_instruction(it->instruction))
{
auto size = ast::type_size(*it->inferred_type);
@ -1098,8 +1103,10 @@ namespace pslang::jit::macos_aarch64
// Globals are added in a separate pass before code
if (lcontext.nodes.contains(it))
continue;
// Uncomment to debug per-node instruction generation:
builder.nop();
// builder.nop();
lcontext.nodes[it] = pcontext.storage.size();
std::visit([&](auto const & instruction){ apply(it, instruction, it->inferred_type); }, it->instruction);
}
@ -1219,10 +1226,10 @@ namespace pslang::jit::macos_aarch64
std::visit([&](auto const & instruction){ visitor.apply(it, instruction, it->inferred_type); }, it->instruction);
}
auto data_end = pcontext.storage.align();
pcontext.storage.data.push_back({data_begin, data_end});
if (data_begin != data_end)
pcontext.storage.data.push_back({data_begin, data_end});
auto code_begin = data_end;
instruction_builder builder{pcontext.storage.storage};
{
populate_const_data_visitor visitor{pcontext, lcontext};
@ -1230,6 +1237,8 @@ namespace pslang::jit::macos_aarch64
std::visit([&](auto const & instruction){ visitor.apply(instruction, it->inferred_type); }, it->instruction);
}
instruction_builder builder{pcontext.storage.storage};
for (auto const & symbol : mcontext.symbols)
{
pcontext.symbols[symbol.first] = pcontext.storage.size();
@ -1248,7 +1257,7 @@ namespace pslang::jit::macos_aarch64
for (auto const & resolve : lcontext.adr_resolve)
builder.adr_inject(pcontext.storage.storage.data() + resolve.offset, lcontext.nodes.at(resolve.target) - resolve.offset);
auto code_end = pcontext.storage.align();
auto code_end = pcontext.storage.align();
pcontext.storage.code.push_back({code_begin, code_end});
}