[IR]重构数组地址相关指令

增加GEP指令以及相关方法
新增数组Array Type
删除无用指令(GetSubArray,LA)
删除冗余类定义(Lval)
修复中间代码生成逻辑
测试通过所以test目录下的文件
TODO:后端展开数组计算地址仅需要针对GEP指令展开
This commit is contained in:
rain2133
2025-07-20 15:33:58 +08:00
parent 18e7cbd413
commit de696b2b53
8 changed files with 455 additions and 467 deletions

View File

@@ -49,6 +49,7 @@ class Type {
kLabel,
kPointer,
kFunction,
kArray,
};
Kind kind; ///< 表示具体类型的变量
@@ -65,6 +66,7 @@ class Type {
static Type* getPointerType(Type *baseType); ///< 返回表示指向baseType类型的Pointer类型的Type指针
static Type* getFunctionType(Type *returnType, const std::vector<Type *> &paramTypes = {});
///< 返回表示返回类型为returnType,形参类型列表为paramTypes的函数类型的Type指针
static Type* getArrayType(Type *elementType, unsigned numElements);
public:
Kind getKind() const { return kind; } ///< 返回Type对象代表原始标量类型
@@ -74,6 +76,7 @@ class Type {
bool isLabel() const { return kind == kLabel; } ///< 判定是否为Label类型
bool isPointer() const { return kind == kPointer; } ///< 判定是否为Pointer类型
bool isFunction() const { return kind == kFunction; } ///< 判定是否为Function类型
bool isArray() const { return kind == Kind::kArray; }
unsigned getSize() const; ///< 返回类型所占的空间大小(字节)
/// 尝试将一个变量转换为给定的Type及其派生类类型的变量
template <typename T>
@@ -115,6 +118,22 @@ class FunctionType : public Type {
unsigned getNumParams() const { return paramTypes.size(); } ///< 获取形参数量
};
class ArrayType : public Type {
public:
// elements数组的元素类型 (例如int[3] 的 elementType 是 int)
// numElements该维度的大小 (例如int[3] 的 numElements 是 3)
static ArrayType *get(Type *elementType, unsigned numElements);
Type *getElementType() const { return elementType; }
unsigned getNumElements() const { return numElements; }
protected:
ArrayType(Type *elementType, unsigned numElements)
: Type(Kind::kArray), elementType(elementType), numElements(numElements) {}
Type *elementType;
unsigned numElements; // 当前维度的大小
};
/*!
* @}
*/
@@ -602,49 +621,6 @@ class User : public Value {
void setOperand(unsigned index, Value *value); ///< 设置操作数
};
class GetSubArrayInst;
/**
* 左值 具有地址的对象
*/
class LVal {
friend class GetSubArrayInst;
protected:
LVal *fatherLVal{}; ///< 父左值
std::list<std::unique_ptr<LVal>> childrenLVals; ///< 子左值
GetSubArrayInst *defineInst{}; /// 定义该左值的GetSubArray指令
protected:
LVal() = default;
public:
virtual ~LVal() = default;
virtual std::vector<Value *> getLValDims() const = 0; ///< 获取左值的维度
virtual unsigned getLValNumDims() const = 0; ///< 获取左值的维度数量
public:
LVal* getFatherLVal() const { return fatherLVal; } ///< 获取父左值
const std::list<std::unique_ptr<LVal>>& getChildrenLVals() const {
return childrenLVals;
} ///< 获取子左值列表
LVal* getAncestorLVal() const {
auto curLVal = const_cast<LVal *>(this);
while (curLVal->getFatherLVal() != nullptr) {
curLVal = curLVal->getFatherLVal();
}
return curLVal;
} ///< 获取祖先左值
void setFatherLVal(LVal *father) { fatherLVal = father; } ///< 设置父左值
void setDefineInst(GetSubArrayInst *inst) { defineInst = inst; } ///< 设置定义指令
void addChild(LVal *child) { childrenLVals.emplace_back(child); } ///< 添加子左值
void removeChild(LVal *child) {
auto iter = std::find_if(childrenLVals.begin(), childrenLVals.end(),
[child](const std::unique_ptr<LVal> &ptr) { return ptr.get() == child; });
childrenLVals.erase(iter);
} ///< 移除子左值
GetSubArrayInst* getDefineInst() const { return defineInst; } ///< 获取定义指令
};
/*!
* Base of all concrete instruction types.
*/
@@ -694,15 +670,15 @@ class Instruction : public User {
kAlloca = 0x1UL << 33,
kLoad = 0x1UL << 34,
kStore = 0x1UL << 35,
kLa = 0x1UL << 36,
kGetElementPtr = 0x1UL << 36,
kMemset = 0x1UL << 37,
kGetSubArray = 0x1UL << 38,
// kGetSubArray = 0x1UL << 38,
// Constant Kind removed as Constants are now Values, not Instructions.
// kConstant = 0x1UL << 37, // Conflicts with kMemset if kept as is
// phi
kPhi = 0x1UL << 39,
kBitItoF = 0x1UL << 40,
kBitFtoI = 0x1UL << 41
kBitFtoI = 0x1UL << 41,
};
protected:
@@ -793,14 +769,12 @@ public:
return "Load";
case kStore:
return "Store";
case kLa:
return "La";
case kGetElementPtr:
return "GetElementPtr";
case kMemset:
return "Memset";
case kPhi:
return "Phi";
case kGetSubArray:
return "GetSubArray";
default:
return "Unknown";
}
@@ -853,9 +827,8 @@ public:
bool isAlloca() const { return kind == kAlloca; }
bool isLoad() const { return kind == kLoad; }
bool isStore() const { return kind == kStore; }
bool isLa() const { return kind == kLa; }
bool isGetElementPtr() const { return kind == kGetElementPtr; }
bool isMemset() const { return kind == kMemset; }
bool isGetSubArray() const { return kind == kGetSubArray; }
bool isCall() const { return kind == kCall; }
bool isReturn() const { return kind == kReturn; }
bool isDefine() const {
@@ -867,26 +840,6 @@ public:
class Function;
//! Function call.
class LaInst : public Instruction {
friend class Function;
friend class IRBuilder;
protected:
explicit LaInst(Value *pointer, const std::vector<Value *> &indices = {}, BasicBlock *parent = nullptr,
const std::string &name = "")
: Instruction(Kind::kLa, pointer->getType(), parent, name) {
assert(pointer);
addOperand(pointer);
addOperands(indices);
}
public:
unsigned getNumIndices() const { return getNumOperands() - 1; } ///< 获取索引长度
Value* getPointer() const { return getOperand(0); } ///< 获取目标变量的Value指针
auto getIndices() const { return make_range(std::next(operand_begin()), operand_end()); } ///< 获取索引列表
Value* getIndex(unsigned index) const { return getOperand(index + 1); } ///< 获取位置为index的索引分量
};
class PhiInst : public Instruction {
friend class IRBuilder;
friend class Function;
@@ -1134,7 +1087,7 @@ public:
}; // class CondBrInst
//! Allocate memory for stack variables, used for non-global variable declartion
class AllocaInst : public Instruction , public LVal {
class AllocaInst : public Instruction {
friend class IRBuilder;
friend class Function;
protected:
@@ -1145,14 +1098,6 @@ protected:
}
public:
std::vector<Value *> getLValDims() const override {
std::vector<Value *> dims;
for (const auto &dim : getOperands()) {
dims.emplace_back(dim->getValue());
}
return dims;
} ///< 获取作为左值的维度数组
unsigned getLValNumDims() const override { return getNumOperands(); }
int getNumDims() const { return getNumOperands(); }
auto getDims() const { return getOperands(); }
@@ -1161,37 +1106,40 @@ public:
}; // class AllocaInst
class GetSubArrayInst : public Instruction {
friend class IRBuilder;
friend class Function;
class GetElementPtrInst : public Instruction {
friend class IRBuilder; // 如果您有IRBuilder来创建指令需要friend
public:
GetSubArrayInst(LVal *fatherArray, LVal *childArray, const std::vector<Value *> &indices,
BasicBlock *parent = nullptr, const std::string &name = "")
: Instruction(Kind::kGetSubArray, Type::getVoidType(), parent, name) {
auto predicate = [childArray](const std::unique_ptr<LVal> &child) -> bool { return child.get() == childArray; };
if (std::find_if(fatherArray->childrenLVals.begin(), fatherArray->childrenLVals.end(), predicate) ==
fatherArray->childrenLVals.end()) {
fatherArray->childrenLVals.emplace_back(childArray);
}
childArray->fatherLVal = fatherArray;
childArray->defineInst = this;
auto fatherArrayValue = dynamic_cast<Value *>(fatherArray);
auto childArrayValue = dynamic_cast<Value *>(childArray);
assert(fatherArrayValue);
assert(childArrayValue);
addOperand(fatherArrayValue);
addOperand(childArrayValue);
addOperands(indices);
protected:
// GEP的构造函数
// resultType: GEP计算出的地址的类型 (通常是指向目标元素类型的指针)
// basePointer: 基指针 (第一个操作数)
// indices: 索引列表 (后续操作数)
GetElementPtrInst(Value *basePointer,
const std::vector<Value *> &indices = {},
BasicBlock *parent = nullptr, const std::string &name = "")
: Instruction(Kind::kGetElementPtr, basePointer->getType(), parent, name) {
assert(basePointer && "GEP base pointer cannot be null!");
// TODO : 安全检查
assert(basePointer->getType()->isPointer() );
addOperand(basePointer); // 第一个操作数是基指针
addOperands(indices); // 随后的操作数是索引
}
public:
Value* getFatherArray() const { return getOperand(0); } ///< 获取父数组
Value* getChildArray() const { return getOperand(1); } ///< 获取子数组
LVal* getFatherLVal() const { return dynamic_cast<LVal *>(getOperand(0)); } ///< 获取父左值
LVal* getChildLVal() const { return dynamic_cast<LVal *>(getOperand(1)); } ///< 获取子左值
auto getIndices() const { return make_range(std::next(operand_begin(), 2), operand_end()); } ///< 获取索引
unsigned getNumIndices() const { return getNumOperands() - 2; } ///< 获取索引数量
public:
Value* getBasePointer() const { return getOperand(0); }
unsigned getNumIndices() const { return getNumOperands() - 1; }
auto getIndices() const { return make_range(std::next(operand_begin()), operand_end());}
Value* getIndex(unsigned index) const {
assert(index < getNumIndices() && "Index out of bounds for GEP!");
return getOperand(index + 1);
}
// 静态工厂方法用于创建GEP指令 (如果需要外部直接创建而非通过IRBuilder)
static GetElementPtrInst* create(Type *resultType, Value *basePointer,
const std::vector<Value *> &indices = {},
BasicBlock *parent = nullptr, const std::string &name = "") {
return new GetElementPtrInst(basePointer, indices, parent, name);
}
};
//! Load a value from memory address specified by a pointer value
@@ -1215,22 +1163,7 @@ public:
return make_range(std::next(operand_begin()), operand_end());
}
Value* getIndex(int index) const { return getOperand(index + 1); }
std::list<Value *> getAncestorIndices() const {
std::list<Value *> indices;
for (const auto &index : getIndices()) {
indices.emplace_back(index->getValue());
}
auto curPointer = dynamic_cast<LVal *>(getPointer());
while (curPointer->getFatherLVal() != nullptr) {
auto inserter = std::next(indices.begin());
for (const auto &index : curPointer->getDefineInst()->getIndices()) {
indices.insert(inserter, index->getValue());
}
curPointer = curPointer->getFatherLVal();
}
return indices;
} ///< 获取相对于祖先数组的索引列表
}; // class LoadInst
//! Store a value to memory address specified by a pointer value
@@ -1256,22 +1189,6 @@ public:
return make_range(std::next(operand_begin(), 2), operand_end());
}
Value* getIndex(int index) const { return getOperand(index + 2); }
std::list<Value *> getAncestorIndices() const {
std::list<Value *> indices;
for (const auto &index : getIndices()) {
indices.emplace_back(index->getValue());
}
auto curPointer = dynamic_cast<LVal *>(getPointer());
while (curPointer->getFatherLVal() != nullptr) {
auto inserter = std::next(indices.begin());
for (const auto &index : curPointer->getDefineInst()->getIndices()) {
indices.insert(inserter, index->getValue());
}
curPointer = curPointer->getFatherLVal();
}
return indices;
} ///< 获取相对于祖先数组的索引列表
}; // class StoreInst
@@ -1373,7 +1290,7 @@ protected:
};
//! Global value declared at file scope
class GlobalValue : public User, public LVal {
class GlobalValue : public User {
friend class Module;
protected:
@@ -1407,16 +1324,6 @@ protected:
}
public:
unsigned getLValNumDims() const override { return numDims; } ///< 获取作为左值的维度数量
std::vector<Value *> getLValDims() const override {
std::vector<Value *> dims;
for (const auto &dim : getOperands()) {
dims.emplace_back(dim->getValue());
}
return dims;
} ///< 获取作为左值的维度列表
unsigned getNumDims() const { return numDims; } ///< 获取维度数量
Value* getDim(unsigned index) const { return getOperand(index); } ///< 获取位置为index的维度
auto getDims() const { return getOperands(); } ///< 获取维度列表
@@ -1438,7 +1345,7 @@ public:
}; // class GlobalValue
class ConstantVariable : public User, public LVal {
class ConstantVariable : public User {
friend class Module;
protected:
@@ -1457,15 +1364,6 @@ class ConstantVariable : public User, public LVal {
}
public:
unsigned getLValNumDims() const override { return numDims; } ///< 获取作为左值的维度数量
std::vector<Value *> getLValDims() const override {
std::vector<Value *> dims;
for (const auto &dim : getOperands()) {
dims.emplace_back(dim->getValue());
}
return dims;
} ///< 获取作为左值的维度列表
Value* getByIndex(unsigned index) const { return initValues.getValue(index); } ///< 通过一维位置index获取值
Value* getByIndices(const std::vector<Value *> &indices) const {
int index = 0;

View File

@@ -280,46 +280,6 @@ class IRBuilder {
block->getInstructions().emplace(position, inst);
return inst;
} ///< 创建load指令
LaInst * createLaInst(Value *pointer, const std::vector<Value *> &indices = {}, const std::string &name = "") {
std::string newName;
if (name.empty()) {
std::stringstream ss;
ss << tmpIndex;
newName = ss.str();
tmpIndex++;
} else {
newName = name;
}
auto inst = new LaInst(pointer, indices, block, newName);
assert(inst);
block->getInstructions().emplace(position, inst);
return inst;
} ///< 创建la指令
GetSubArrayInst * createGetSubArray(LVal *fatherArray, const std::vector<Value *> &indices, const std::string &name = "") {
assert(fatherArray->getLValNumDims() > indices.size());
std::vector<Value *> subDims;
auto dims = fatherArray->getLValDims();
auto iter = std::next(dims.begin(), indices.size());
while (iter != dims.end()) {
subDims.emplace_back(*iter);
iter++;
}
std::string childArrayName;
std::stringstream ss;
ss << "A"
<< "%" << tmpIndex;
childArrayName = ss.str();
tmpIndex++;
auto fatherArrayValue = dynamic_cast<Value *>(fatherArray);
auto childArray = new AllocaInst(fatherArrayValue->getType(), subDims, block, childArrayName);
auto inst = new GetSubArrayInst(fatherArray, childArray, indices, block, childArrayName);
assert(inst);
block->getInstructions().emplace(position, inst);
return inst;
} ///< 创建获取部分数组指令
MemsetInst * createMemsetInst(Value *pointer, Value *begin, Value *size, Value *value, const std::string &name = "") {
auto inst = new MemsetInst(pointer, begin, size, value, block, name);
assert(inst);
@@ -340,6 +300,24 @@ class IRBuilder {
block->getInstructions().emplace(block->begin(), inst);
return inst;
} ///< 创建Phi指令
GetElementPtrInst* createGetElementPtrInst(Value *basePointer,
const std::vector<Value *> &indices = {},
const std::string &name = "") {
std::string newName;
if (name.empty()) {
std::stringstream ss;
ss << tmpIndex;
newName = ss.str();
tmpIndex++;
} else {
newName = name;
}
auto inst = new GetElementPtrInst(basePointer, indices, block, newName);
assert(inst);
block->getInstructions().emplace(position, inst);
return inst;
}
};
} // namespace sysy

View File

@@ -68,6 +68,7 @@ public:
Module *get() const { return module.get(); }
IRBuilder *getBuilder(){ return &builder; }
public:
std::any visitCompUnit(SysYParser::CompUnitContext *ctx) override;
std::any visitGlobalConstDecl(SysYParser::GlobalConstDeclContext *ctx) override;
@@ -134,6 +135,11 @@ public:
// std::any visitConstExp(SysYParser::ConstExpContext *ctx) override;
public:
// 获取GEP指令的地址
Value* getGEPAddressInst(Value* basePointer, const std::vector<Value*>& indices);
// 构建数组类型
Type* buildArrayType(Type* baseType, const std::vector<Value*>& dims);
}; // class SysYIRGenerator