156 lines
7.9 KiB
Markdown
156 lines
7.9 KiB
Markdown
`IRBuilder.h` 文件定义了一个 `IRBuilder` 类,用于简化中间表示(IR)的构建过程。`IRBuilder` 提供了创建各种 IR 指令的便捷方法,并将这些指令插入到指定的基本块中。以下是对文件中主要内容的整理和解释:
|
||
|
||
---
|
||
|
||
### **1. `IRBuilder` 类的作用**
|
||
`IRBuilder` 是一个工具类,用于在生成中间表示(IR)时简化指令的创建和插入操作。它的主要功能包括:
|
||
- 提供创建各种 IR 指令的工厂方法。
|
||
- 将创建的指令插入到指定的基本块中。
|
||
- 支持在基本块的任意位置插入指令。
|
||
|
||
---
|
||
|
||
### **2. 主要成员**
|
||
|
||
#### **2.1 成员变量**
|
||
- **`block`**:当前操作的基本块。
|
||
- **`position`**:当前操作的插入位置(基本块中的迭代器)。
|
||
|
||
#### **2.2 构造函数**
|
||
- **默认构造函数**:`IRBuilder()`。
|
||
- **带参数的构造函数**:
|
||
- `IRBuilder(BasicBlock *block)`:初始化 `IRBuilder`,并设置当前基本块和插入位置(默认在基本块末尾)。
|
||
- `IRBuilder(BasicBlock *block, BasicBlock::iterator position)`:初始化 `IRBuilder`,并设置当前基本块和插入位置。
|
||
|
||
#### **2.3 设置方法**
|
||
- **`setPosition(BasicBlock *block, BasicBlock::iterator position)`**:设置当前基本块和插入位置。
|
||
- **`setPosition(BasicBlock::iterator position)`**:设置当前插入位置。
|
||
|
||
#### **2.4 获取方法**
|
||
- **`getBasicBlock()`**:获取当前基本块。
|
||
- **`getPosition()`**:获取当前插入位置。
|
||
|
||
---
|
||
|
||
### **3. 指令创建方法**
|
||
`IRBuilder` 提供了多种工厂方法,用于创建不同类型的 IR 指令。这些方法会将创建的指令插入到当前基本块的指定位置。
|
||
|
||
#### **3.1 函数调用指令**
|
||
- **`createCallInst(Function *callee, const std::vector<Value *> &args, const std::string &name)`**:
|
||
- 创建一个函数调用指令。
|
||
- 参数:
|
||
- `callee`:被调用的函数。
|
||
- `args`:函数参数列表。
|
||
- `name`:指令的名称(可选)。
|
||
- 返回:`CallInst*`。
|
||
|
||
#### **3.2 一元操作指令**
|
||
- **`createUnaryInst(Instruction::Kind kind, Type *type, Value *operand, const std::string &name)`**:
|
||
- 创建一个一元操作指令(如取反、类型转换)。
|
||
- 参数:
|
||
- `kind`:指令的类型(如 `kNeg`、`kFtoI` 等)。
|
||
- `type`:指令的结果类型。
|
||
- `operand`:操作数。
|
||
- `name`:指令的名称(可选)。
|
||
- 返回:`UnaryInst*`。
|
||
|
||
- **具体一元操作指令**:
|
||
- `createNegInst(Value *operand, const std::string &name)`:创建整数取反指令。
|
||
- `createNotInst(Value *operand, const std::string &name)`:创建逻辑取反指令。
|
||
- `createFtoIInst(Value *operand, const std::string &name)`:创建浮点数转整数指令。
|
||
- `createFNegInst(Value *operand, const std::string &name)`:创建浮点数取反指令。
|
||
- `createIToFInst(Value *operand, const std::string &name)`:创建整数转浮点数指令。
|
||
|
||
#### **3.3 二元操作指令**
|
||
- **`createBinaryInst(Instruction::Kind kind, Type *type, Value *lhs, Value *rhs, const std::string &name)`**:
|
||
- 创建一个二元操作指令(如加法、减法)。
|
||
- 参数:
|
||
- `kind`:指令的类型(如 `kAdd`、`kSub` 等)。
|
||
- `type`:指令的结果类型。
|
||
- `lhs` 和 `rhs`:左操作数和右操作数。
|
||
- `name`:指令的名称(可选)。
|
||
- 返回:`BinaryInst*`。
|
||
|
||
- **具体二元操作指令**:
|
||
- 整数运算:
|
||
- `createAddInst(Value *lhs, Value *rhs, const std::string &name)`:创建整数加法指令。
|
||
- `createSubInst(Value *lhs, Value *rhs, const std::string &name)`:创建整数减法指令。
|
||
- `createMulInst(Value *lhs, Value *rhs, const std::string &name)`:创建整数乘法指令。
|
||
- `createDivInst(Value *lhs, Value *rhs, const std::string &name)`:创建整数除法指令。
|
||
- `createRemInst(Value *lhs, Value *rhs, const std::string &name)`:创建整数取余指令。
|
||
- 整数比较:
|
||
- `createICmpEQInst(Value *lhs, Value *rhs, const std::string &name)`:创建整数相等比较指令。
|
||
- `createICmpNEInst(Value *lhs, Value *rhs, const std::string &name)`:创建整数不等比较指令。
|
||
- `createICmpLTInst(Value *lhs, Value *rhs, const std::string &name)`:创建整数小于比较指令。
|
||
- `createICmpLEInst(Value *lhs, Value *rhs, const std::string &name)`:创建整数小于等于比较指令。
|
||
- `createICmpGTInst(Value *lhs, Value *rhs, const std::string &name)`:创建整数大于比较指令。
|
||
- `createICmpGEInst(Value *lhs, Value *rhs, const std::string &name)`:创建整数大于等于比较指令。
|
||
- 浮点数运算:
|
||
- `createFAddInst(Value *lhs, Value *rhs, const std::string &name)`:创建浮点数加法指令。
|
||
- `createFSubInst(Value *lhs, Value *rhs, const std::string &name)`:创建浮点数减法指令。
|
||
- `createFMulInst(Value *lhs, Value *rhs, const std::string &name)`:创建浮点数乘法指令。
|
||
- `createFDivInst(Value *lhs, Value *rhs, const std::string &name)`:创建浮点数除法指令。
|
||
- `createFRemInst(Value *lhs, Value *rhs, const std::string &name)`:创建浮点数取余指令。
|
||
- 浮点数比较:
|
||
- `createFCmpEQInst(Value *lhs, Value *rhs, const std::string &name)`:创建浮点数相等比较指令。
|
||
- `createFCmpNEInst(Value *lhs, Value *rhs, const std::string &name)`:创建浮点数不等比较指令。
|
||
- `createFCmpLTInst(Value *lhs, Value *rhs, const std::string &name)`:创建浮点数小于比较指令。
|
||
- `createFCmpLEInst(Value *lhs, Value *rhs, const std::string &name)`:创建浮点数小于等于比较指令。
|
||
- `createFCmpGTInst(Value *lhs, Value *rhs, const std::string &name)`:创建浮点数大于比较指令。
|
||
- `createFCmpGEInst(Value *lhs, Value *rhs, const std::string &name)`:创建浮点数大于等于比较指令。
|
||
|
||
#### **3.4 控制流指令**
|
||
- **`createReturnInst(Value *value)`**:
|
||
- 创建返回指令。
|
||
- 参数:
|
||
- `value`:返回值(可选)。
|
||
- 返回:`ReturnInst*`。
|
||
|
||
- **`createUncondBrInst(BasicBlock *block, std::vector<Value *> args)`**:
|
||
- 创建无条件跳转指令。
|
||
- 参数:
|
||
- `block`:目标基本块。
|
||
- `args`:跳转参数(可选)。
|
||
- 返回:`UncondBrInst*`。
|
||
|
||
- **`createCondBrInst(Value *condition, BasicBlock *thenBlock, BasicBlock *elseBlock, const std::vector<Value *> &thenArgs, const std::vector<Value *> &elseArgs)`**:
|
||
- 创建条件跳转指令。
|
||
- 参数:
|
||
- `condition`:跳转条件。
|
||
- `thenBlock`:条件为真时的目标基本块。
|
||
- `elseBlock`:条件为假时的目标基本块。
|
||
- `thenArgs` 和 `elseArgs`:跳转参数(可选)。
|
||
- 返回:`CondBrInst*`。
|
||
|
||
#### **3.5 内存操作指令**
|
||
- **`createAllocaInst(Type *type, const std::vector<Value *> &dims, const std::string &name)`**:
|
||
- 创建栈内存分配指令。
|
||
- 参数:
|
||
- `type`:分配的类型。
|
||
- `dims`:数组维度(可选)。
|
||
- `name`:指令的名称(可选)。
|
||
- 返回:`AllocaInst*`。
|
||
|
||
- **`createLoadInst(Value *pointer, const std::vector<Value *> &indices, const std::string &name)`**:
|
||
- 创建加载指令。
|
||
- 参数:
|
||
- `pointer`:指针值。
|
||
- `indices`:数组索引(可选)。
|
||
- `name`:指令的名称(可选)。
|
||
- 返回:`LoadInst*`。
|
||
|
||
- **`createStoreInst(Value *value, Value *pointer, const std::vector<Value *> &indices, const std::string &name)`**:
|
||
- 创建存储指令。
|
||
- 参数:
|
||
- `value`:要存储的值。
|
||
- `pointer`:指针值。
|
||
- `indices`:数组索引(可选)。
|
||
- `name`:指令的名称(可选)。
|
||
- 返回:`StoreInst*`。
|
||
|
||
---
|
||
|
||
### **4. 总结**
|
||
- `IRBuilder` 是一个用于简化 IR 构建的工具类,提供了创建各种 IR 指令的工厂方法。
|
||
- 通过 `IRBuilder`,可以方便地在指定基本块的任意位置插入指令。
|
||
- 该类的设计使得 IR 的生成更加模块化和易于维护。 |