From 9c9f5a2013fceb98fa4e0b6f37eb0504ee014c5a Mon Sep 17 00:00:00 2001 From: mxr <> Date: Thu, 9 Apr 2026 16:23:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(mir)=E8=B0=83=E8=AF=95=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E6=B5=81=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/mir/MIR.h | 1 + src/mir/AsmPrinter.cpp | 9 +++------ src/mir/Lowering.cpp | 40 ++++++++++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/include/mir/MIR.h b/include/mir/MIR.h index f44df47..ff1ce75 100644 --- a/include/mir/MIR.h +++ b/include/mir/MIR.h @@ -129,6 +129,7 @@ enum class Opcode { // ---------- 特殊 ---------- Nop, // 空操作: NOP + Label, // 内联标签,不生成实际指令,仅输出标签名 }; // ========== 操作数类 ========== diff --git a/src/mir/AsmPrinter.cpp b/src/mir/AsmPrinter.cpp index 3ea7d37..3c6ca93 100644 --- a/src/mir/AsmPrinter.cpp +++ b/src/mir/AsmPrinter.cpp @@ -31,12 +31,6 @@ void PrintStackAccess(std::ostream& os, const char* mnemonic, PhysReg reg, << "]\n"; } -void PrintStackPairAccess(std::ostream& os, const char* mnemonic, PhysReg reg0, PhysReg reg1, - int offset) { - os << " " << mnemonic << " " << PhysRegName(reg0) << ", " << PhysRegName(reg1) << ", [x29, #" << offset - << "]\n"; -} - // 打印单个操作数 void PrintOperand(std::ostream& os, const Operand& op) { switch (op.GetKind()) { @@ -248,6 +242,9 @@ void PrintInstruction(std::ostream& os, const MachineInstr& instr, case Opcode::Nop: os << " nop\n"; break; + case Opcode::Label: + os << ops.at(0).GetLabel() << ":\n"; + break; default: os << " // unknown instruction\n"; break; diff --git a/src/mir/Lowering.cpp b/src/mir/Lowering.cpp index 4d6f513..dfdbd6d 100644 --- a/src/mir/Lowering.cpp +++ b/src/mir/Lowering.cpp @@ -341,14 +341,19 @@ void LowerInstruction(const ir::Instruction& inst, MachineFunction& function, std::string true_label = ".L_cset_true_" + std::to_string(reinterpret_cast(&icmp)); std::string end_label = ".L_cset_end_" + std::to_string(reinterpret_cast(&icmp)); - // 条件跳转到 true_label - block.Append(Opcode::BCond, {Operand::Cond(cc), Operand::Imm(0)}); - // 条件不成立:W8 = 0 + // 设置条件成立时的值(1) + block.Append(Opcode::MovImm, {Operand::Reg(PhysReg::W8), Operand::Imm(1)}); + // 条件成立时跳转到 true_label + block.Append(Opcode::BCond, {Operand::Cond(cc), Operand::Label(true_label)}); + // 条件不成立:设置 W8 = 0 block.Append(Opcode::MovImm, {Operand::Reg(PhysReg::W8), Operand::Imm(0)}); - block.Append(Opcode::B, {Operand::Imm(0)}); // 跳转到 end_label - // true_label: W8 = 1(已经在上面设置了) - // end_label: 继续 - + // 跳转到 end_label + block.Append(Opcode::B, {Operand::Label(end_label)}); + // true_label: 值已经是 1,直接落入 end_label + block.Append(Opcode::Label, {Operand::Label(true_label)}); + // end_label: 存储结果 + block.Append(Opcode::Label, {Operand::Label(end_label)}); + block.Append(Opcode::StoreStack, {Operand::Reg(PhysReg::W8), Operand::FrameIndex(dst_slot)}); slots.emplace(&inst, dst_slot); @@ -375,13 +380,26 @@ void LowerInstruction(const ir::Instruction& inst, MachineFunction& function, } // ========== 跳转指令(使用标签操作数)========== case ir::Opcode::Br: { + DEBUG_MSG("Processing Br"); auto& br = static_cast(inst); - + + auto* cond = br.GetCondition(); + DEBUG_MSG("Condition value ptr: " << cond); + DEBUG_MSG("Condition name: " << cond->GetName()); + auto it = slots.find(cond); + if (it == slots.end()) { + DEBUG_MSG("Condition not found in slots!"); + // 输出所有 slots 的键名 + for (auto& p : slots) { + DEBUG_MSG(" Slot key: " << p.first->GetName()); + } + } + if (br.IsConditional()) { // 条件跳转: br i1 %cond, label %then, label %else // 加载条件值到 w8 EmitValueToReg(br.GetCondition(), PhysReg::W8, slots, block, function); - + // 比较条件值是否为 0 block.Append(Opcode::CmpRI, {Operand::Reg(PhysReg::W8), Operand::Imm(0)}); @@ -396,11 +414,13 @@ void LowerInstruction(const ir::Instruction& inst, MachineFunction& function, block.Append(Opcode::BCond, {Operand::Cond(CondCode::NE), Operand::Label(trueLabel)}); // 生成 B false_label block.Append(Opcode::B, {Operand::Label(falseLabel)}); + DEBUG_MSG("Generating conditional branch: cond=" << br.GetCondition()->GetName() + << ", true=" << trueLabel << ", false=" << falseLabel); } else { // 无条件跳转: br label %target const ir::BasicBlock* irTarget = br.GetTarget(); std::string targetLabel = GetBlockLabel(irTarget); - DEBUG_MSG("br: targetLabel is " << GetBlockLabel(irTarget)); + DEBUG_MSG("b: targetLabel is " << GetBlockLabel(irTarget)); // 生成 B target_label block.Append(Opcode::B, {Operand::Label(targetLabel)});