feat: complete Lab3 instruction selection and assembly generation

This commit is contained in:
2026-04-25 14:30:22 +08:00
parent 979d271ebe
commit 0b0bc04be3
13 changed files with 1078 additions and 160 deletions

View File

@@ -18,10 +18,10 @@ void RunFrameLowering(MachineFunction& function) {
int cursor = 0;
for (const auto& slot : function.GetFrameSlots()) {
cursor += slot.size;
if (-cursor < -256) {
throw std::runtime_error(FormatError("mir", "暂不支持过大的栈帧"));
}
}
// Align stack frames to 16 bytes for AArch64
cursor = AlignTo(cursor, 16);
cursor = 0;
for (const auto& slot : function.GetFrameSlots()) {
@@ -30,16 +30,25 @@ void RunFrameLowering(MachineFunction& function) {
}
function.SetFrameSize(AlignTo(cursor, 16));
auto& insts = function.GetEntry().GetInstructions();
std::vector<MachineInstr> lowered;
lowered.emplace_back(Opcode::Prologue);
for (const auto& inst : insts) {
if (inst.GetOpcode() == Opcode::Ret) {
lowered.emplace_back(Opcode::Epilogue);
auto& blocks = function.GetBlocks();
if (blocks.empty()) return;
// Insert Prologue at the start of the first block
auto& entry_insts = blocks.front().GetInstructions();
entry_insts.insert(entry_insts.begin(), MachineInstr(Opcode::Prologue));
// Insert Epilogue before every Ret in all blocks
for (auto& block : blocks) {
auto& insts = block.GetInstructions();
std::vector<MachineInstr> lowered;
for (const auto& inst : insts) {
if (inst.GetOpcode() == Opcode::Ret) {
lowered.emplace_back(Opcode::Epilogue);
}
lowered.push_back(inst);
}
lowered.push_back(inst);
insts = std::move(lowered);
}
insts = std::move(lowered);
}
} // namespace mir