From 6b92020bc40f273e8aa8b3fa8af10bf683d91cdd Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Sat, 9 Aug 2025 22:41:32 +0800 Subject: [PATCH] =?UTF-8?q?[midend-llvmirprint]=E4=BF=AE=E5=A4=8D=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E6=95=B0=E7=BB=84=E6=89=93=E5=8D=B0=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/midend/IR.cpp | 114 ++++++++++++++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 40 deletions(-) diff --git a/src/midend/IR.cpp b/src/midend/IR.cpp index d7a796a..0c105cf 100644 --- a/src/midend/IR.cpp +++ b/src/midend/IR.cpp @@ -274,6 +274,55 @@ void Argument::print(std::ostream& os) const { os << *getType() << " %" << getName(); } +// 辅助函数:递归生成多维数组的初始化格式 +static void printArrayInitializer(std::ostream& os, Type* arrayType, const ValueCounter& initValues, size_t& valueIndex) { + if (auto arrType = arrayType->as()) { + auto elementType = arrType->getElementType(); + auto numElements = arrType->getNumElements(); + + os << "["; + for (unsigned i = 0; i < numElements; ++i) { + if (i > 0) os << ", "; + + if (elementType->isArray()) { + // 嵌套数组,递归处理 + os << *elementType << " "; + printArrayInitializer(os, elementType, initValues, valueIndex); + } else { + // 基础类型元素 + os << *elementType << " "; + if (valueIndex < initValues.size()) { + const auto& values = initValues.getValues(); + const auto& numbers = initValues.getNumbers(); + + // 找到当前应该使用的值 + size_t currentValueIdx = 0; + size_t currentCount = 0; + for (size_t j = 0; j < values.size(); ++j) { + if (currentCount + numbers[j] > valueIndex) { + currentValueIdx = j; + break; + } + currentCount += numbers[j]; + } + + printOperand(os, values[currentValueIdx]); + valueIndex++; + } else { + // 没有更多初始化值,使用默认值 + if (elementType->isInt()) { + os << "0"; + } else if (elementType->isFloat()) { + os << "0.0"; + } + valueIndex++; + } + } + } + os << "]"; + } +} + void GlobalValue::print(std::ostream& os) const { // 输出全局变量的LLVM IR格式 os << "@" << getName() << " = global "; @@ -320,20 +369,15 @@ void GlobalValue::print(std::ostream& os) const { printOperand(os, singleVal); } } else { - // 数组初始值 - 需要展开ValueCounter中的压缩表示 - os << "["; - bool first = true; - const auto& numbers = initValues.getNumbers(); - - for (size_t i = 0; i < values.size(); ++i) { - for (unsigned j = 0; j < numbers[i]; ++j) { - if (!first) os << ", "; - os << *values[i]->getType() << " "; - printOperand(os, values[i]); - first = false; - } + // 数组初始值 - 根据数组维度生成正确的初始化格式 + if (baseType->isArray()) { + size_t valueIndex = 0; + printArrayInitializer(os, baseType, initValues, valueIndex); + } else { + // 单个非零标量值 + auto singleVal = initValues.getValue(0); + printOperand(os, singleVal); } - os << "]"; } } } @@ -384,20 +428,15 @@ void ConstantVariable::print(std::ostream& os) const { printOperand(os, singleVal); } } else { - // 数组初始值 - 需要展开ValueCounter中的压缩表示 - os << "["; - bool first = true; - const auto& numbers = initValues.getNumbers(); - - for (size_t i = 0; i < values.size(); ++i) { - for (unsigned j = 0; j < numbers[i]; ++j) { - if (!first) os << ", "; - os << *values[i]->getType() << " "; - printOperand(os, values[i]); - first = false; - } + // 数组初始值 - 根据数组维度生成正确的初始化格式 + if (baseType->isArray()) { + size_t valueIndex = 0; + printArrayInitializer(os, baseType, initValues, valueIndex); + } else { + // 单个非零标量值 + auto singleVal = initValues.getValue(0); + printOperand(os, singleVal); } - os << "]"; } } } @@ -409,21 +448,16 @@ void ConstantVariable::print_init(std::ostream& os) const { // 单个初始值 initValues.getValue(0)->print(os); } else { - // 数组初始值 - 需要展开ValueCounter中的压缩表示 - os << "["; - bool first = true; - const auto& values = initValues.getValues(); - const auto& numbers = initValues.getNumbers(); - - for (size_t i = 0; i < values.size(); ++i) { - for (unsigned j = 0; j < numbers[i]; ++j) { - if (!first) os << ", "; - os << *values[i]->getType() << " "; - values[i]->print(os); - first = false; - } + // 数组初始值 - 根据数组维度生成正确的初始化格式 + auto baseType = getType()->as()->getBaseType(); + if (baseType->isArray()) { + size_t valueIndex = 0; + printArrayInitializer(os, baseType, initValues, valueIndex); + } else { + // 单个非零标量值 + auto singleVal = initValues.getValue(0); + singleVal->print(os); } - os << "]"; } } else { os << "zeroinitializer";