|
|
|
|
@ -6,6 +6,14 @@
|
|
|
|
|
#include "ir/IR.h"
|
|
|
|
|
#include "utils/Log.h"
|
|
|
|
|
|
|
|
|
|
#define DEBUG_Lower
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_Lower
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#define DEBUG_MSG(msg) std::cerr << "[Lower Debug] " << msg << std::endl
|
|
|
|
|
#else
|
|
|
|
|
#define DEBUG_MSG(msg)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace mir {
|
|
|
|
|
namespace {
|
|
|
|
|
@ -392,7 +400,8 @@ void LowerInstruction(const ir::Instruction& inst, MachineFunction& function,
|
|
|
|
|
// 无条件跳转: br label %target
|
|
|
|
|
const ir::BasicBlock* irTarget = br.GetTarget();
|
|
|
|
|
std::string targetLabel = GetBlockLabel(irTarget);
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG("br: targetLabel is " << GetBlockLabel(irTarget));
|
|
|
|
|
|
|
|
|
|
// 生成 B target_label
|
|
|
|
|
block.Append(Opcode::B, {Operand::Label(targetLabel)});
|
|
|
|
|
}
|
|
|
|
|
@ -646,8 +655,10 @@ std::unique_ptr<MachineFunction> LowerFunction(const ir::Function& func) {
|
|
|
|
|
std::unordered_map<const ir::BasicBlock*, MachineBasicBlock*> blockMap;
|
|
|
|
|
|
|
|
|
|
// 第一遍:为每个 IR 基本块创建 MIR 基本块
|
|
|
|
|
std::string func_name = func.GetName();
|
|
|
|
|
for (const auto& bb : func.GetBlocks()) {
|
|
|
|
|
auto mirBB = std::make_unique<MachineBasicBlock>(bb->GetName());
|
|
|
|
|
// 格式: .L函数名_基本块名
|
|
|
|
|
auto mirBB = std::make_unique<MachineBasicBlock>(".L" + func_name + "_" + bb->GetName());
|
|
|
|
|
blockMap[bb.get()] = mirBB.get();
|
|
|
|
|
machine_func->AddBasicBlock(std::move(mirBB));
|
|
|
|
|
}
|
|
|
|
|
|