feat(mir): 增加 Lab3 AArch64 MVP 后端与 --emit-asm 支持

This commit is contained in:
Lane0218
2026-03-07 22:41:53 +08:00
parent 0ff3d918d9
commit b939fc40ee
15 changed files with 592 additions and 33 deletions

View File

@@ -1,4 +1,44 @@
// 栈帧构建与序言尾声插入:
// - 计算栈大小与对齐需求,分配栈槽
// - 插入 prologue/epilogue保存/恢复 callee-saved 等
#include "mir/MIR.h"
#include <stdexcept>
#include <vector>
namespace mir {
namespace {
int AlignTo(int value, int align) {
return ((value + align - 1) / align) * align;
}
} // namespace
void RunFrameLowering(MachineFunction& function) {
int cursor = 0;
for (const auto& slot : function.frame_slots()) {
cursor += slot.size;
if (-cursor < -256) {
throw std::runtime_error(
"Lab3 MVP 后端暂不支持超过 64 个 i32 栈槽的函数");
}
}
cursor = 0;
for (const auto& slot : function.frame_slots()) {
cursor += slot.size;
function.frame_slot(slot.index).offset = -cursor;
}
function.set_frame_size(AlignTo(cursor, 16));
auto& insts = function.entry().instructions();
std::vector<MachineInstr> lowered;
lowered.emplace_back(Opcode::Prologue);
for (const auto& inst : insts) {
if (inst.opcode() == Opcode::Ret) {
lowered.emplace_back(Opcode::Epilogue);
}
lowered.push_back(inst);
}
insts = std::move(lowered);
}
} // namespace mir