From 4b181261ce8a9d3b6be8db32478741e9255d03d0 Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Sat, 26 Jul 2025 12:53:21 +0800 Subject: [PATCH] =?UTF-8?q?[midend][backend]=E5=90=8C=E6=AD=A5=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BF=AE=E5=A4=8D=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/RISCv64AsmPrinter.cpp | 66 +++++++++++++++++++-------------------- src/RISCv64ISel.cpp | 12 +++++-- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/RISCv64AsmPrinter.cpp b/src/RISCv64AsmPrinter.cpp index 65dbe5d..8c0b10c 100644 --- a/src/RISCv64AsmPrinter.cpp +++ b/src/RISCv64AsmPrinter.cpp @@ -47,45 +47,45 @@ void RISCv64AsmPrinter::printPrologue() { *OS << " addi s0, sp, " << aligned_stack_size << "\n"; } - // 为函数参数分配寄存器 - Function* F = MFunc->getFunc(); - if (F && F->getEntryBlock()) { - int arg_idx = 0; - RISCv64ISel* isel = MFunc->getISel(); + // // 为函数参数分配寄存器 + // Function* F = MFunc->getFunc(); + // if (F && F->getEntryBlock()) { + // int arg_idx = 0; + // RISCv64ISel* isel = MFunc->getISel(); - // 获取函数所有参数的类型列表 - auto param_types = F->getParamTypes(); + // // 获取函数所有参数的类型列表 + // auto param_types = F->getParamTypes(); - for (AllocaInst* alloca_for_param : F->getEntryBlock()->getArguments()) { - if (arg_idx >= 8) break; + // for (AllocaInst* alloca_for_param : F->getEntryBlock()->getArguments()) { + // if (arg_idx >= 8) break; - unsigned vreg = isel->getVReg(alloca_for_param); - if (frame_info.alloca_offsets.count(vreg)) { - int offset = frame_info.alloca_offsets.at(vreg); - auto arg_reg = static_cast(static_cast(PhysicalReg::A0) + arg_idx); + // unsigned vreg = isel->getVReg(alloca_for_param); + // if (frame_info.alloca_offsets.count(vreg)) { + // int offset = frame_info.alloca_offsets.at(vreg); + // auto arg_reg = static_cast(static_cast(PhysicalReg::A0) + arg_idx); - // 1. 获取当前参数的真实类型 - // 注意:F->getParamTypes() 返回的是一个 range-based view,需要转换为vector或直接使用 - Type* current_param_type = nullptr; - int temp_idx = 0; - for(auto p_type : param_types) { - if (temp_idx == arg_idx) { - current_param_type = p_type; - break; - } - temp_idx++; - } - assert(current_param_type && "Could not find parameter type."); + // // 1. 获取当前参数的真实类型 + // // 注意:F->getParamTypes() 返回的是一个 range-based view,需要转换为vector或直接使用 + // Type* current_param_type = nullptr; + // int temp_idx = 0; + // for(auto p_type : param_types) { + // if (temp_idx == arg_idx) { + // current_param_type = p_type; + // break; + // } + // temp_idx++; + // } + // assert(current_param_type && "Could not find parameter type."); - // 2. 根据类型决定使用 "sw" 还是 "sd" - const char* store_op = current_param_type->isPointer() ? "sd" : "sw"; + // // 2. 根据类型决定使用 "sw" 还是 "sd" + // const char* store_op = current_param_type->isPointer() ? "sd" : "sw"; - // 3. 打印正确的存储指令 - *OS << " " << store_op << " " << regToString(arg_reg) << ", " << offset << "(s0)\n"; - } - arg_idx++; - } - } + // // 3. 打印正确的存储指令 + // *OS << " " << store_op << " " << regToString(arg_reg) << ", " << offset << "(s0)\n"; + // } + // arg_idx++; + // } + // } } void RISCv64AsmPrinter::printEpilogue() { diff --git a/src/RISCv64ISel.cpp b/src/RISCv64ISel.cpp index db07908..6e0b87b 100644 --- a/src/RISCv64ISel.cpp +++ b/src/RISCv64ISel.cpp @@ -51,22 +51,28 @@ std::unique_ptr RISCv64ISel::runOnFunction(Function* func) { // 指令选择主流程 void RISCv64ISel::select() { + // 遍历基本块,为它们创建对应的MachineBasicBlock for (const auto& bb_ptr : F->getBasicBlocks()) { auto mbb = std::make_unique(bb_ptr->getName(), MFunc.get()); bb_map[bb_ptr.get()] = mbb.get(); MFunc->addBlock(std::move(mbb)); } - if (F->getEntryBlock()) { - for (auto* arg_alloca : F->getEntryBlock()->getArguments()) { - getVReg(arg_alloca); + // 遍历Argument对象,为它们分配虚拟寄存器。 + if (F) { + for (Argument* arg : F->getArguments()) { + // getVReg会为每个代表参数的Argument对象在vreg_map中创建一个条目。 + // 这样,当后续的store指令使用这个参数时,就能找到对应的vreg。 + getVReg(arg); } } + // 遍历基本块,进行指令选择 for (const auto& bb_ptr : F->getBasicBlocks()) { selectBasicBlock(bb_ptr.get()); } + // 链接MachineBasicBlock的前驱和后继 for (const auto& bb_ptr : F->getBasicBlocks()) { CurMBB = bb_map.at(bb_ptr.get()); for (auto succ : bb_ptr->getSuccessors()) {