[midend]修改GEP指令定义,更靠近llvm ir设计,增加自动推断类型函数,修复generator中错误生成ir的逻辑

This commit is contained in:
rain2133
2025-07-24 17:02:29 +08:00
parent c68b031c01
commit 9c56bc1310
3 changed files with 70 additions and 69 deletions

View File

@@ -1124,24 +1124,24 @@ public:
class GetElementPtrInst : public Instruction {
friend class IRBuilder; // 如果您有IRBuilder来创建指令需要friend
friend class IRBuilder;
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) {
GetElementPtrInst(Type *resultType,
Value *basePointer,
const std::vector<Value *> &indices = {},
BasicBlock *parent = nullptr, const std::string &name = "")
: Instruction(Kind::kGetElementPtr, resultType, parent, name) {
assert(basePointer && "GEP base pointer cannot be null!");
// TODO : 安全检查
assert(basePointer->getType()->isPointer() );
addOperand(basePointer); // 第一个操作数是基指针
addOperands(indices); // 随后的操作数是索引
}
public:
Value* getBasePointer() const { return getOperand(0); }
unsigned getNumIndices() const { return getNumOperands() - 1; }
@@ -1155,7 +1155,7 @@ public:
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);
return new GetElementPtrInst(resultType, basePointer, indices, parent, name);
}
};