[midend-llvmirprint]修复了gep指令对不含维度信息的数组指针的处理逻辑,修复若干打印bug,在-s ir/ird -o <llvmir.ll file>的参数下最终会打印ir到file中,优化过程中的打印逻辑待更改。

This commit is contained in:
rain2133
2025-08-06 15:28:54 +08:00
parent 5d343f42a5
commit 37f2a01783
3 changed files with 75 additions and 27 deletions

View File

@@ -10,6 +10,14 @@
#include "IRBuilder.h"
using namespace std;
inline std::string getMachineCode(float fval) {
uint32_t mrf = *reinterpret_cast<uint32_t*>(&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<const GlobalValue*>(var) != nullptr) {
auto globalVal = dynamic_cast<const GlobalValue*>(var);
return os << "@" << globalVal->getName();
if (dynamic_cast<const GlobalValue*>(var) != nullptr ||
dynamic_cast<const ConstantVariable*>(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<const ConstantValue*>(value);
if (constValue != nullptr) {
constValue->print(os);
// 对于常量,只打印值,不打印类型(类型已经在指令中单独打印了)
if (auto constInt = dynamic_cast<const ConstantInteger*>(constValue)) {
os << constInt->getInt();
} else if (auto constFloat = dynamic_cast<const ConstantFloating*>(constValue)) {
os << getMachineCode(constFloat->getFloat());
} else if (auto undefVal = dynamic_cast<const UndefinedValue*>(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<uint32_t*>(&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<const ConstantInteger*>(this)) {
dynamic_cast<const ConstantInteger*>(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());