[midend-llvmirprint]修复全局数组打印格式问题

This commit is contained in:
rain2133
2025-08-09 22:41:32 +08:00
parent c867bda9b4
commit 6b92020bc4

View File

@ -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<ArrayType>()) {
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<PointerType>()->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";