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