From 9d8930f5df5ee2044b6c85fe0af48026152f38fe Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Mon, 23 Jun 2025 00:22:15 +0800 Subject: [PATCH] fix % repeat in IR print --- src/Mem2Reg.cpp | 0 src/SysYIRGenerator.cpp | 12 +++++++----- src/SysYIRPrinter.cpp | 4 +--- src/include/IRBuilder.h | 10 +++++----- src/include/Mem2Reg.h | 0 5 files changed, 13 insertions(+), 13 deletions(-) create mode 100644 src/Mem2Reg.cpp create mode 100644 src/include/Mem2Reg.h diff --git a/src/Mem2Reg.cpp b/src/Mem2Reg.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/SysYIRGenerator.cpp b/src/SysYIRGenerator.cpp index 1718aba..912930e 100644 --- a/src/SysYIRGenerator.cpp +++ b/src/SysYIRGenerator.cpp @@ -73,10 +73,12 @@ std::any SysYIRGenerator::visitGlobalVarDecl(SysYParser::GlobalVarDeclContext *c } } - ArrayValueTree* root = std::any_cast(varDef->initVal()->accept(this)); - ValueCounter values; - Utils::tree2Array(type, root, dims, dims.size(), values, &builder); - delete root; + ValueCounter values = {}; + if (varDef->initVal() != nullptr) { + ArrayValueTree* root = std::any_cast(varDef->initVal()->accept(this)); + Utils::tree2Array(type, root, dims, dims.size(), values, &builder); + delete root; + } // 创建全局变量,并更新符号表 module->createGlobalValue(name, Type::getPointerType(type), dims, values); } @@ -456,7 +458,7 @@ std::any SysYIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext *ctx) { returnValue = std::any_cast(visitExp(ctx->exp())); } - Type* funcType = builder.getBasicBlock()->getParent()->getType(); + Type* funcType = builder.getBasicBlock()->getParent()->getReturnType(); if (funcType!= returnValue->getType() && returnValue != nullptr) { ConstantValue * constValue = dynamic_cast(returnValue); if (constValue != nullptr) { diff --git a/src/SysYIRPrinter.cpp b/src/SysYIRPrinter.cpp index 5be7777..4812106 100644 --- a/src/SysYIRPrinter.cpp +++ b/src/SysYIRPrinter.cpp @@ -11,9 +11,7 @@ void SysYPrinter::printIR() { const auto &functions = pModule->getFunctions(); - // Print target datalayout and triple (minimal required by LLVM) - std::cout << "target datalayout = \"e-m:e-p270:32:32-p271:32:32-p272:64:64-f64:32:64-f80:32-n8:16:32-S128\"\n"; - std::cout << "target triple = \"i386-pc-linux-gnu\"\n\n"; + //TODO: Print target datalayout and triple (minimal required by LLVM) printGlobalVariable(); diff --git a/src/include/IRBuilder.h b/src/include/IRBuilder.h index 9189cba..8798a2e 100644 --- a/src/include/IRBuilder.h +++ b/src/include/IRBuilder.h @@ -96,7 +96,7 @@ class IRBuilder { std::string newName; if (name.empty()) { std::stringstream ss; - ss << "%" << tmpIndex; + ss << tmpIndex; newName = ss.str(); tmpIndex++; } else { @@ -136,7 +136,7 @@ class IRBuilder { std::string newName; if (name.empty()) { std::stringstream ss; - ss << "%" << tmpIndex; + ss << tmpIndex; newName = ss.str(); tmpIndex++; } else { @@ -221,7 +221,7 @@ class IRBuilder { std::string newName; if (name.empty() && callee->getReturnType() != Type::getVoidType()) { std::stringstream ss; - ss << "%" << tmpIndex; + ss << tmpIndex; newName = ss.str(); tmpIndex++; } else { @@ -268,7 +268,7 @@ class IRBuilder { std::string newName; if (name.empty()) { std::stringstream ss; - ss << "%" << tmpIndex; + ss << tmpIndex; newName = ss.str(); tmpIndex++; } else { @@ -284,7 +284,7 @@ class IRBuilder { std::string newName; if (name.empty()) { std::stringstream ss; - ss << "%" << tmpIndex; + ss << tmpIndex; newName = ss.str(); tmpIndex++; } else { diff --git a/src/include/Mem2Reg.h b/src/include/Mem2Reg.h new file mode 100644 index 0000000..e69de29