Merge remote-tracking branch 'refs/remotes/origin/feature/mir' into feature/mir

feature/mir
ftt 2 days ago
commit 288d0ec3b0

@ -129,6 +129,7 @@ enum class Opcode {
// ---------- 特殊 ----------
Nop, // 空操作: NOP
Label, // 内联标签,不生成实际指令,仅输出标签名
};
// ========== 操作数类 ==========

@ -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;

@ -42,13 +42,19 @@ void RunFrameLowering(MachineFunction& function) {
// 基本块
const auto& blocks = function.GetBasicBlocks();
bool firstBlock = true;
for (const auto& bb : blocks) {
DEBUG_MSG("block");
auto& insts = bb->GetInstructions();
std::vector<MachineInstr> lowered;
DEBUG_MSG("empalace Prologue");
lowered.emplace_back(Opcode::Prologue);
// 输出基本块标签(非第一个基本块)
if (firstBlock) {
DEBUG_MSG("empalace Prologue");
lowered.emplace_back(Opcode::Prologue);
}
firstBlock = false;
// 输出基本块中的指令
for (const auto& inst : insts) {
DEBUG_MSG("inst");

@ -356,14 +356,19 @@ void LowerInstruction(const ir::Instruction& inst, MachineFunction& function,
std::string true_label = ".L_cset_true_" + std::to_string(reinterpret_cast<uintptr_t>(&icmp));
std::string end_label = ".L_cset_end_" + std::to_string(reinterpret_cast<uintptr_t>(&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);
@ -390,13 +395,26 @@ void LowerInstruction(const ir::Instruction& inst, MachineFunction& function,
}
// ========== 跳转指令(使用标签操作数)==========
case ir::Opcode::Br: {
DEBUG_MSG("Processing Br");
auto& br = static_cast<const ir::BranchInst&>(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)});
@ -411,11 +429,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)});

Loading…
Cancel
Save