From 37f2a017838c3ec4af475e62524c96b78212254b Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Wed, 6 Aug 2025 15:28:54 +0800 Subject: [PATCH] =?UTF-8?q?[midend-llvmirprint]=E4=BF=AE=E5=A4=8D=E4=BA=86?= =?UTF-8?q?gep=E6=8C=87=E4=BB=A4=E5=AF=B9=E4=B8=8D=E5=90=AB=E7=BB=B4?= =?UTF-8?q?=E5=BA=A6=E4=BF=A1=E6=81=AF=E7=9A=84=E6=95=B0=E7=BB=84=E6=8C=87?= =?UTF-8?q?=E9=92=88=E7=9A=84=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8B=A5=E5=B9=B2=E6=89=93=E5=8D=B0bug?= =?UTF-8?q?=EF=BC=8C=E5=9C=A8-s=20ir/ird=20-o=20?= =?UTF-8?q?=E7=9A=84=E5=8F=82=E6=95=B0=E4=B8=8B=E6=9C=80=E7=BB=88=E4=BC=9A?= =?UTF-8?q?=E6=89=93=E5=8D=B0ir=E5=88=B0file=E4=B8=AD=EF=BC=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E8=BF=87=E7=A8=8B=E4=B8=AD=E7=9A=84=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=BE=85=E6=9B=B4=E6=94=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/midend/IR.cpp | 55 +++++++++++++++++++++------------- src/midend/SysYIRGenerator.cpp | 29 +++++++++++++++--- src/sysyc.cpp | 18 +++++++++-- 3 files changed, 75 insertions(+), 27 deletions(-) diff --git a/src/midend/IR.cpp b/src/midend/IR.cpp index 2a41637..fffab0c 100644 --- a/src/midend/IR.cpp +++ b/src/midend/IR.cpp @@ -10,6 +10,14 @@ #include "IRBuilder.h" using namespace std; + +inline std::string getMachineCode(float fval) { + uint32_t mrf = *reinterpret_cast(&fval); + std::stringstream ss; + ss << std::hex << std::uppercase << std::setfill('0') << std::setw(8) << mrf; + return "0x" + ss.str(); +} + /** * @file IR.cpp * @@ -44,9 +52,9 @@ ostream &interleave_call(std::ostream &os, const T &container, const std::string static inline ostream &printVarName(ostream &os, const Value *var) { - if (dynamic_cast(var) != nullptr) { - auto globalVal = dynamic_cast(var); - return os << "@" << globalVal->getName(); + if (dynamic_cast(var) != nullptr || + dynamic_cast(var) != nullptr) { + return os << "@" << var->getName(); } else { return os << "%" << var->getName(); } @@ -60,7 +68,14 @@ static inline ostream &printFunctionName(ostream &os, const Function *fn) { static inline ostream &printOperand(ostream &os, const Value *value) { auto constValue = dynamic_cast(value); if (constValue != nullptr) { - constValue->print(os); + // 对于常量,只打印值,不打印类型(类型已经在指令中单独打印了) + if (auto constInt = dynamic_cast(constValue)) { + os << constInt->getInt(); + } else if (auto constFloat = dynamic_cast(constValue)) { + os << getMachineCode(constFloat->getFloat()); + } else if (auto undefVal = dynamic_cast(constValue)) { + os << "undef"; + } return os; } return printVarName(os, value); @@ -267,11 +282,11 @@ void GlobalValue::print(std::ostream& os) const { os << "zeroinitializer"; } else { // 非零值或非基本类型,打印实际值 - singleVal->print(os); + printOperand(os, singleVal); } } else { // 非常量值,打印实际值 - singleVal->print(os); + printOperand(os, singleVal); } } else { // 数组初始值 - 需要展开ValueCounter中的压缩表示 @@ -283,7 +298,7 @@ void GlobalValue::print(std::ostream& os) const { for (unsigned j = 0; j < numbers[i]; ++j) { if (!first) os << ", "; os << *values[i]->getType() << " "; - values[i]->print(os); + printOperand(os, values[i]); first = false; } } @@ -331,11 +346,11 @@ void ConstantVariable::print(std::ostream& os) const { os << "zeroinitializer"; } else { // 非零值或非基本类型,打印实际值 - singleVal->print(os); + printOperand(os, singleVal); } } else { // 非常量值,打印实际值 - singleVal->print(os); + printOperand(os, singleVal); } } else { // 数组初始值 - 需要展开ValueCounter中的压缩表示 @@ -347,7 +362,7 @@ void ConstantVariable::print(std::ostream& os) const { for (unsigned j = 0; j < numbers[i]; ++j) { if (!first) os << ", "; os << *values[i]->getType() << " "; - values[i]->print(os); + printOperand(os, values[i]); first = false; } } @@ -470,13 +485,6 @@ UndefinedValue* UndefinedValue::get(Type* type) { return newUndef; } -inline std::string getMachineCode(float fval) { - uint32_t mrf = *reinterpret_cast(&fval); - std::stringstream ss; - ss << std::hex << std::uppercase << std::setfill('0') << std::setw(8) << mrf; - return "0x" + ss.str(); -} - void ConstantValue::print(std::ostream &os) const { if(dynamic_cast(this)) { dynamic_cast(this)->print(os); @@ -540,10 +548,17 @@ void CallInst::print(std::ostream &os) const { if(!getType()->isVoid()) { printVarName(os, this) << " = "; } - os << getKindString() << *getType() << " " ; + os << getKindString() << " " << *getType() << " " ; printFunctionName(os, getCallee()); os << "("; - interleave_call(os, getOperands()); + + // 打印参数,跳过第一个操作数(函数本身) + for (unsigned i = 1; i < getNumOperands(); ++i) { + if (i > 1) os << ", "; + auto arg = getOperand(i); + os << *arg->getType() << " "; + printOperand(os, arg); + } os << ")"; } @@ -639,7 +654,7 @@ void UncondBrInst::print(std::ostream &os) const { } void CondBrInst::print(std::ostream &os) const { - os << "br " << *getCondition()->getType() << " "; + os << "br i1 "; // 条件分支的条件总是假定为i1类型 printOperand(os, getCondition()); os << ", label %"; printBlockName(os, getThenBlock()); diff --git a/src/midend/SysYIRGenerator.cpp b/src/midend/SysYIRGenerator.cpp index 2e0e910..fb75465 100644 --- a/src/midend/SysYIRGenerator.cpp +++ b/src/midend/SysYIRGenerator.cpp @@ -1652,11 +1652,13 @@ std::any SysYIRGenerator::visitLValue(SysYParser::LValueContext *ctx) { break; } } - if (allIndicesConstant) { + // 如果是常量变量且所有索引都是常量,并且不是数组名单独出现的情况 + if (allIndicesConstant && !dims.empty()) { // 如果是常量变量且所有索引都是常量,直接通过 getByIndices 获取编译时值 // 这个方法会根据索引深度返回最终的标量值或指向子数组的指针 (作为 ConstantValue/Variable) return constVar->getByIndices(dims); } + // 如果dims为空(数组名单独出现),需要走GEP路径来实现数组到指针的退化 } // 3. 处理可变变量 (AllocaInst/GlobalValue) 或带非常量索引的常量变量 @@ -1681,16 +1683,35 @@ std::any SysYIRGenerator::visitLValue(SysYParser::LValueContext *ctx) { } else { gepBasePointer = alloc; gepIndices.push_back(ConstantInteger::get(0)); - gepIndices.insert(gepIndices.end(), dims.begin(), dims.end()); + if (dims.empty() && declaredNumDims > 0) { + // 数组名单独出现(没有索引):在SysY中,数组名应该退化为指向第一个元素的指针 + // 需要添加额外的0索引来获取第一个元素的地址 + gepIndices.push_back(ConstantInteger::get(0)); + } else { + // 正常的数组元素访问 + gepIndices.insert(gepIndices.end(), dims.begin(), dims.end()); + } } } else if (GlobalValue *glob = dynamic_cast(variable)) { gepBasePointer = glob; gepIndices.push_back(ConstantInteger::get(0)); - gepIndices.insert(gepIndices.end(), dims.begin(), dims.end()); + if (dims.empty() && declaredNumDims > 0) { + // 全局数组名单独出现(没有索引):应该退化为指向第一个元素的指针 + gepIndices.push_back(ConstantInteger::get(0)); + } else { + // 正常的数组元素访问 + gepIndices.insert(gepIndices.end(), dims.begin(), dims.end()); + } } else if (ConstantVariable *constV = dynamic_cast(variable)) { gepBasePointer = constV; gepIndices.push_back(ConstantInteger::get(0)); - gepIndices.insert(gepIndices.end(), dims.begin(), dims.end()); + if (dims.empty() && declaredNumDims > 0) { + // 常量数组名单独出现(没有索引):应该退化为指向第一个元素的指针 + gepIndices.push_back(ConstantInteger::get(0)); + } else { + // 正常的数组元素访问 + gepIndices.insert(gepIndices.end(), dims.begin(), dims.end()); + } } else { assert(false && "LValue variable type not supported for GEP base pointer."); return static_cast(nullptr); diff --git a/src/sysyc.cpp b/src/sysyc.cpp index 747eb89..be17fd3 100644 --- a/src/sysyc.cpp +++ b/src/sysyc.cpp @@ -132,7 +132,7 @@ int main(int argc, char **argv) { if (DEBUG) { cout << "=== Init IR ===\n"; - SysYPrinter(moduleIR).printIR(); // 临时打印器用于调试 + moduleIR->print(cout); // 使用新实现的print方法直接打印IR } // 创建 Pass 管理器并运行优化管道 @@ -145,9 +145,21 @@ int main(int argc, char **argv) { if (argStopAfter == "ir" || argStopAfter == "ird") { // 打印最终 IR cout << "=== Final IR ===\n"; - SysYPrinter printer(moduleIR); // 在这里创建打印器,因为可能之前调试时用过临时打印器 - printer.printIR(); + if (!argOutputFilename.empty()) { + // 输出到指定文件 + ofstream fout(argOutputFilename); + if (not fout.is_open()) { + cerr << "Failed to open output file: " << argOutputFilename << endl; + return EXIT_FAILURE; + } + moduleIR->print(fout); + fout.close(); + } else { + // 输出到标准输出 + moduleIR->print(cout); + } return EXIT_SUCCESS; + } // b) 如果未停止在 IR 阶段,则继续生成汇编 (后端)