[midend-llvmirprint]检查并修复了初始值的打印逻辑

This commit is contained in:
rain2133
2025-08-06 02:02:05 +08:00
parent a4406e0112
commit 5d343f42a5

View File

@@ -164,6 +164,21 @@ void Type::print(ostream &os) const {
}
}
void Use::print(std::ostream& os) const {
os << "Use[" << index << "]: ";
if (value) {
printVarName(os, value);
} else {
os << "null";
}
os << " used by ";
if (user) {
os << "User@" << user;
} else {
os << "null";
}
}
PointerType* PointerType::get(Type *baseType) {
static std::map<Type *, std::unique_ptr<PointerType>> pointerTypes;
auto iter = pointerTypes.find(baseType);
@@ -217,25 +232,63 @@ void GlobalValue::print(std::ostream& os) const {
// 输出全局变量的LLVM IR格式
os << "@" << getName() << " = global ";
auto baseType = getType()->as<PointerType>()->getBaseType();
os << *baseType << " ";
// 输出初始化值
if (initValues.size() > 0) {
os << *getType()->as<PointerType>()->getBaseType() << " ";
if (initValues.size() == 1) {
// 单个初始值
initValues.getValue(0)->print(os);
if (initValues.size() == 0) {
// 没有初始化值使用zeroinitializer
os << "zeroinitializer";
} else {
// 数组初始值
// 检查是否所有值都是零值
bool allZero = true;
const auto& values = initValues.getValues();
for (Value* val : values) {
if (auto constVal = dynamic_cast<ConstantValue*>(val)) {
if (!constVal->isZero()) {
allZero = false;
break;
}
} else {
allZero = false;
break;
}
}
if (allZero) {
// 所有值都是零使用zeroinitializer
os << "zeroinitializer";
} else if (initValues.size() == 1) {
// 单个初始值 - 如果是标量零值也考虑使用zeroinitializer
auto singleVal = initValues.getValue(0);
if (auto constVal = dynamic_cast<ConstantValue*>(singleVal)) {
if (constVal->isZero() && (baseType->isInt() || baseType->isFloat())) {
// 标量零值使用zeroinitializer
os << "zeroinitializer";
} else {
// 非零值或非基本类型,打印实际值
singleVal->print(os);
}
} else {
// 非常量值,打印实际值
singleVal->print(os);
}
} else {
// 数组初始值 - 需要展开ValueCounter中的压缩表示
os << "[";
for (unsigned i = 0; i < initValues.size(); ++i) {
if (i > 0) os << ", ";
auto value = initValues.getValue(i);
os << *value->getType() << " ";
value->print(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() << " ";
values[i]->print(os);
first = false;
}
}
os << "]";
}
} else {
os << *getType()->as<PointerType>()->getBaseType() << " zeroinitializer";
}
}
@@ -243,25 +296,91 @@ void ConstantVariable::print(std::ostream& os) const {
// 输出常量的LLVM IR格式
os << "@" << getName() << " = constant ";
auto baseType = getType()->as<PointerType>()->getBaseType();
os << *baseType << " ";
// 输出初始化值
if (initValues.size() == 0) {
// 没有初始化值使用zeroinitializer
os << "zeroinitializer";
} else {
// 检查是否所有值都是零值
bool allZero = true;
const auto& values = initValues.getValues();
for (Value* val : values) {
if (auto constVal = dynamic_cast<ConstantValue*>(val)) {
if (!constVal->isZero()) {
allZero = false;
break;
}
} else {
allZero = false;
break;
}
}
if (allZero) {
// 所有值都是零使用zeroinitializer
os << "zeroinitializer";
} else if (initValues.size() == 1) {
// 单个初始值 - 如果是标量零值也考虑使用zeroinitializer
auto singleVal = initValues.getValue(0);
if (auto constVal = dynamic_cast<ConstantValue*>(singleVal)) {
if (constVal->isZero() && (baseType->isInt() || baseType->isFloat())) {
// 标量零值使用zeroinitializer
os << "zeroinitializer";
} else {
// 非零值或非基本类型,打印实际值
singleVal->print(os);
}
} else {
// 非常量值,打印实际值
singleVal->print(os);
}
} 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() << " ";
values[i]->print(os);
first = false;
}
}
os << "]";
}
}
}
void ConstantVariable::print_init(std::ostream& os) const {
// 只输出初始化值部分
if (initValues.size() > 0) {
os << *getType()->as<PointerType>()->getBaseType() << " ";
if (initValues.size() == 1) {
// 单个初始值
initValues.getValue(0)->print(os);
} else {
// 数组初始值
// 数组初始值 - 需要展开ValueCounter中的压缩表示
os << "[";
for (unsigned i = 0; i < initValues.size(); ++i) {
if (i > 0) os << ", ";
auto value = initValues.getValue(i);
os << *value->getType() << " ";
value->print(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;
}
}
os << "]";
}
} else {
os << *getType()->as<PointerType>()->getBaseType() << " zeroinitializer";
os << "zeroinitializer";
}
}
@@ -926,39 +1045,13 @@ void Function::print(std::ostream& os) const {
void Module::print(std::ostream& os) const {
// 打印全局变量声明
for (auto& globalVar : const_cast<Module*>(this)->getGlobals()) {
printVarName(os, globalVar.get());
os << " = global " << *globalVar->getType()->as<PointerType>()->getBaseType();
// 打印初始值
const auto& initValues = globalVar->getInitValues();
if (initValues.size() > 0) {
os << " ";
// 简化处理:只打印第一个初始值
initValues.getValue(0)->print(os);
} else {
// 默认初始化
if (globalVar->getType()->as<PointerType>()->getBaseType()->isInt()) {
os << " 0";
} else if (globalVar->getType()->as<PointerType>()->getBaseType()->isFloat()) {
os << " 0.0";
} else {
os << " zeroinitializer";
}
}
globalVar->print(os);
os << "\n";
}
// 打印常量声明
for (auto& constVar : getConsts()) {
printVarName(os, constVar.get());
os << " = constant " << *constVar->getType()->as<PointerType>()->getBaseType();
os << " ";
const auto& initValues = constVar->getInitValues();
if (initValues.size() > 0) {
initValues.getValue(0)->print(os);
} else {
os << "0";
}
constVar->print(os);
os << "\n";
}