Compare commits
3 Commits
backend
...
deploy-202
| Author | SHA1 | Date | |
|---|---|---|---|
| ba7c581da5 | |||
| f4ba1df93b | |||
| 6dc74b173b |
2732
doc/Doxyfile
2732
doc/Doxyfile
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
Before Width: | Height: | Size: 346 KiB |
@ -1,169 +0,0 @@
|
||||
这个头文件定义了一个用于生成中间表示(IR)的数据结构,主要用于编译器前端将抽象语法树(AST)转换为中间代码。以下是文件中定义的主要类和功能的整理和解释:
|
||||
|
||||
---
|
||||
|
||||
### **1. 类型系统(Type System)**
|
||||
#### **1.1 `Type` 类**
|
||||
- **作用**:表示所有基本标量类型(如 `int`、`float`、`void` 等)以及指针类型和函数类型。
|
||||
- **成员**:
|
||||
- `Kind` 枚举:表示类型的种类(如 `kInt`、`kFloat`、`kPointer` 等)。
|
||||
- `kind`:当前类型的种类。
|
||||
- 构造函数:`Type(Kind kind)`,用于初始化类型。
|
||||
- 静态方法:如 `getIntType()`、`getFloatType()` 等,用于获取特定类型的单例对象。
|
||||
- 类型检查方法:如 `isInt()`、`isFloat()` 等,用于检查当前类型是否为某种类型。
|
||||
- `getSize()`:获取类型的大小。
|
||||
- `as<T>()`:将当前类型动态转换为派生类(如 `PointerType` 或 `FunctionType`)。
|
||||
|
||||
#### **1.2 `PointerType` 类**
|
||||
- **作用**:表示指针类型,派生自 `Type`。
|
||||
- **成员**:
|
||||
- `baseType`:指针指向的基础类型。
|
||||
- 静态方法:`get(Type *baseType)`,用于获取指向 `baseType` 的指针类型。
|
||||
- `getBaseType()`:获取指针指向的基础类型。
|
||||
|
||||
#### **1.3 `FunctionType` 类**
|
||||
- **作用**:表示函数类型,派生自 `Type`。
|
||||
- **成员**:
|
||||
- `returnType`:函数的返回类型。
|
||||
- `paramTypes`:函数的参数类型列表。
|
||||
- 静态方法:`get(Type *returnType, const std::vector<Type *> ¶mTypes)`,用于获取函数类型。
|
||||
- `getReturnType()`:获取函数的返回类型。
|
||||
- `getParamTypes()`:获取函数的参数类型列表。
|
||||
|
||||
---
|
||||
|
||||
### **2. 中间表示(IR)**
|
||||
#### **2.1 `Value` 类**
|
||||
- **作用**:表示 IR 中的所有值(如指令、常量、参数等)。
|
||||
- **成员**:
|
||||
- `Kind` 枚举:表示值的种类(如 `kAdd`、`kSub`、`kConstant` 等)。
|
||||
- `kind`:当前值的种类。
|
||||
- `type`:值的类型。
|
||||
- `name`:值的名称。
|
||||
- `uses`:值的用途列表(表示哪些指令使用了该值)。
|
||||
- 构造函数:`Value(Kind kind, Type *type, const std::string &name)`。
|
||||
- 类型检查方法:如 `isInt()`、`isFloat()` 等。
|
||||
- `getUses()`:获取值的用途列表。
|
||||
- `replaceAllUsesWith(Value *value)`:将该值的所有用途替换为另一个值。
|
||||
- `print(std::ostream &os)`:打印值的表示。
|
||||
|
||||
#### **2.2 `ConstantValue` 类**
|
||||
- **作用**:表示编译时常量(如整数常量、浮点数常量)。
|
||||
- **成员**:
|
||||
- `iScalar` 和 `fScalar`:分别存储整数和浮点数常量的值。
|
||||
- 静态方法:`get(int value)` 和 `get(float value)`,用于获取常量值。
|
||||
- `getInt()` 和 `getFloat()`:获取常量的值。
|
||||
|
||||
#### **2.3 `Argument` 类**
|
||||
- **作用**:表示函数或基本块的参数。
|
||||
- **成员**:
|
||||
- `block`:参数所属的基本块。
|
||||
- `index`:参数的索引。
|
||||
- 构造函数:`Argument(Type *type, BasicBlock *block, int index, const std::string &name)`。
|
||||
- `getParent()`:获取参数所属的基本块。
|
||||
- `getIndex()`:获取参数的索引。
|
||||
|
||||
#### **2.4 `BasicBlock` 类**
|
||||
- **作用**:表示基本块,包含一系列指令。
|
||||
- **成员**:
|
||||
- `parent`:基本块所属的函数。
|
||||
- `instructions`:基本块中的指令列表。
|
||||
- `arguments`:基本块的参数列表。
|
||||
- `successors` 和 `predecessors`:基本块的后继和前驱列表。
|
||||
- 构造函数:`BasicBlock(Function *parent, const std::string &name)`。
|
||||
- `getParent()`:获取基本块所属的函数。
|
||||
- `getInstructions()`:获取基本块中的指令列表。
|
||||
- `createArgument()`:为基本块创建一个参数。
|
||||
|
||||
#### **2.5 `Instruction` 类**
|
||||
- **作用**:表示 IR 中的指令,派生自 `User`。
|
||||
- **成员**:
|
||||
- `Kind` 枚举:表示指令的种类(如 `kAdd`、`kSub`、`kLoad` 等)。
|
||||
- `kind`:当前指令的种类。
|
||||
- `parent`:指令所属的基本块。
|
||||
- 构造函数:`Instruction(Kind kind, Type *type, BasicBlock *parent, const std::string &name)`。
|
||||
- `getParent()`:获取指令所属的基本块。
|
||||
- `getFunction()`:获取指令所属的函数。
|
||||
- 指令分类方法:如 `isBinary()`、`isUnary()`、`isMemory()` 等。
|
||||
|
||||
#### **2.6 `User` 类**
|
||||
- **作用**:表示使用其他值的指令或全局值,派生自 `Value`。
|
||||
- **成员**:
|
||||
- `operands`:指令的操作数列表。
|
||||
- 构造函数:`User(Kind kind, Type *type, const std::string &name)`。
|
||||
- `getOperand(int index)`:获取指定索引的操作数。
|
||||
- `addOperand(Value *value)`:添加一个操作数。
|
||||
- `replaceOperand(int index, Value *value)`:替换指定索引的操作数。
|
||||
|
||||
#### **2.7 具体指令类**
|
||||
- **`CallInst`**:表示函数调用指令。
|
||||
- **`UnaryInst`**:表示一元操作指令(如取反、类型转换)。
|
||||
- **`BinaryInst`**:表示二元操作指令(如加法、减法)。
|
||||
- **`ReturnInst`**:表示返回指令。
|
||||
- **`UncondBrInst`**:表示无条件跳转指令。
|
||||
- **`CondBrInst`**:表示条件跳转指令。
|
||||
- **`AllocaInst`**:表示栈内存分配指令。
|
||||
- **`LoadInst`**:表示从内存加载值的指令。
|
||||
- **`StoreInst`**:表示将值存储到内存的指令。
|
||||
|
||||
---
|
||||
|
||||
### **3. 模块和函数**
|
||||
#### **3.1 `Function` 类**
|
||||
- **作用**:表示函数,包含多个基本块。
|
||||
- **成员**:
|
||||
- `parent`:函数所属的模块。
|
||||
- `blocks`:函数中的基本块列表。
|
||||
- 构造函数:`Function(Module *parent, Type *type, const std::string &name)`。
|
||||
- `getReturnType()`:获取函数的返回类型。
|
||||
- `getParamTypes()`:获取函数的参数类型列表。
|
||||
- `addBasicBlock()`:为函数添加一个基本块。
|
||||
|
||||
#### **3.2 `GlobalValue` 类**
|
||||
- **作用**:表示全局变量或常量。
|
||||
- **成员**:
|
||||
- `parent`:全局值所属的模块。
|
||||
- `hasInit`:是否有初始化值。
|
||||
- `isConst`:是否是常量。
|
||||
- 构造函数:`GlobalValue(Module *parent, Type *type, const std::string &name, const std::vector<Value *> &dims, Value *init)`。
|
||||
- `init()`:获取全局值的初始化值。
|
||||
|
||||
#### **3.3 `Module` 类**
|
||||
- **作用**:表示整个编译单元(如一个源文件)。
|
||||
- **成员**:
|
||||
- `children`:模块中的所有值(如函数、全局变量)。
|
||||
- `functions`:模块中的函数列表。
|
||||
- `globals`:模块中的全局变量列表。
|
||||
- `createFunction()`:创建一个函数。
|
||||
- `createGlobalValue()`:创建一个全局变量。
|
||||
- `getFunction()`:获取指定名称的函数。
|
||||
- `getGlobalValue()`:获取指定名称的全局变量。
|
||||
|
||||
---
|
||||
|
||||
### **4. 工具类**
|
||||
#### **4.1 `Use` 类**
|
||||
- **作用**:表示值与其使用者之间的关系。
|
||||
- **成员**:
|
||||
- `index`:值在使用者操作数列表中的索引。
|
||||
- `user`:使用者。
|
||||
- `value`:被使用的值。
|
||||
- 构造函数:`Use(int index, User *user, Value *value)`。
|
||||
- `getValue()`:获取被使用的值。
|
||||
|
||||
#### **4.2 `range` 类**
|
||||
- **作用**:封装迭代器对 `[begin, end)`,用于遍历容器。
|
||||
- **成员**:
|
||||
- `begin()` 和 `end()`:返回范围的起始和结束迭代器。
|
||||
- `size()`:返回范围的大小。
|
||||
- `empty()`:判断范围是否为空。
|
||||
|
||||
---
|
||||
|
||||
### **5. 总结**
|
||||
- **类型系统**:`Type`、`PointerType`、`FunctionType` 用于表示 IR 中的类型。
|
||||
- **中间表示**:`Value`、`ConstantValue`、`Instruction` 等用于表示 IR 中的值和指令。
|
||||
- **模块和函数**:`Module`、`Function`、`GlobalValue` 用于组织 IR 的结构。
|
||||
- **工具类**:`Use` 和 `range` 用于辅助实现 IR 的数据结构和遍历。
|
||||
|
||||
这个头文件定义了一个完整的 IR 数据结构,适用于编译器前端将 AST 转换为中间代码,并支持后续的优化和目标代码生成。
|
||||
@ -1,156 +0,0 @@
|
||||
`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 的生成更加模块化和易于维护。
|
||||
@ -1,121 +0,0 @@
|
||||
这个 `IR.cpp` 文件实现了 `IR.h` 中定义的中间表示(IR)数据结构的功能。它包含了类型系统、值、指令、基本块、函数和模块的具体实现,以及一些辅助函数用于打印 IR 的内容。以下是对文件中主要内容的整理和解释:
|
||||
|
||||
---
|
||||
|
||||
### **1. 辅助函数**
|
||||
#### **1.1 `interleave` 函数**
|
||||
- **作用**:用于在输出流中插入分隔符(如逗号)来打印容器中的元素。
|
||||
- **示例**:
|
||||
```cpp
|
||||
interleave(os, container, ", ");
|
||||
```
|
||||
|
||||
#### **1.2 打印函数**
|
||||
- **`printVarName`**:打印变量名,全局变量以 `@` 开头,局部变量以 `%` 开头。
|
||||
- **`printBlockName`**:打印基本块名,以 `^` 开头。
|
||||
- **`printFunctionName`**:打印函数名,以 `@` 开头。
|
||||
- **`printOperand`**:打印操作数,如果是常量则直接打印值,否则打印变量名。
|
||||
|
||||
---
|
||||
|
||||
### **2. 类型系统**
|
||||
#### **2.1 `Type` 类的实现**
|
||||
- **静态方法**:
|
||||
- `getIntType()`、`getFloatType()`、`getVoidType()`、`getLabelType()`:返回对应类型的单例对象。
|
||||
- `getPointerType(Type *baseType)`:返回指向 `baseType` 的指针类型。
|
||||
- `getFunctionType(Type *returnType, const vector<Type *> ¶mTypes)`:返回函数类型。
|
||||
- **`getSize()`**:返回类型的大小(如 `int` 和 `float` 为 4 字节,指针为 8 字节)。
|
||||
- **`print()`**:打印类型的表示。
|
||||
|
||||
#### **2.2 `PointerType` 类的实现**
|
||||
- **静态方法**:
|
||||
- `get(Type *baseType)`:返回指向 `baseType` 的指针类型,使用 `std::map` 缓存已创建的指针类型。
|
||||
- **`getBaseType()`**:返回指针指向的基础类型。
|
||||
|
||||
#### **2.3 `FunctionType` 类的实现**
|
||||
- **静态方法**:
|
||||
- `get(Type *returnType, const vector<Type *> ¶mTypes)`:返回函数类型,使用 `std::set` 缓存已创建的函数类型。
|
||||
- **`getReturnType()`** 和 `getParamTypes()`:分别返回函数的返回类型和参数类型列表。
|
||||
|
||||
---
|
||||
|
||||
### **3. 值(Value)**
|
||||
#### **3.1 `Value` 类的实现**
|
||||
- **`replaceAllUsesWith(Value *value)`**:将该值的所有用途替换为另一个值。
|
||||
- **`isConstant()`**:判断值是否为常量(包括常量值、全局值和函数)。
|
||||
|
||||
#### **3.2 `ConstantValue` 类的实现**
|
||||
- **静态方法**:
|
||||
- `get(int value)` 和 `get(float value)`:返回整数或浮点数常量,使用 `std::map` 缓存已创建的常量。
|
||||
- **`getInt()` 和 `getFloat()`**:返回常量的值。
|
||||
- **`print()`**:打印常量的值。
|
||||
|
||||
#### **3.3 `Argument` 类的实现**
|
||||
- **构造函数**:初始化参数的类型、所属基本块和索引。
|
||||
- **`print()`**:打印参数的表示。
|
||||
|
||||
---
|
||||
|
||||
### **4. 基本块(BasicBlock)**
|
||||
#### **4.1 `BasicBlock` 类的实现**
|
||||
- **构造函数**:初始化基本块的名称和所属函数。
|
||||
- **`print()`**:打印基本块的表示,包括参数和指令。
|
||||
|
||||
---
|
||||
|
||||
### **5. 指令(Instruction)**
|
||||
#### **5.1 `Instruction` 类的实现**
|
||||
- **构造函数**:初始化指令的类型、所属基本块和名称。
|
||||
- **`print()`**:由具体指令类实现。
|
||||
|
||||
#### **5.2 具体指令类的实现**
|
||||
- **`CallInst`**:表示函数调用指令。
|
||||
- **`print()`**:打印函数调用的表示。
|
||||
- **`UnaryInst`**:表示一元操作指令(如取反、类型转换)。
|
||||
- **`print()`**:打印一元操作的表示。
|
||||
- **`BinaryInst`**:表示二元操作指令(如加法、减法)。
|
||||
- **`print()`**:打印二元操作的表示。
|
||||
- **`ReturnInst`**:表示返回指令。
|
||||
- **`print()`**:打印返回指令的表示。
|
||||
- **`UncondBrInst`**:表示无条件跳转指令。
|
||||
- **`print()`**:打印无条件跳转的表示。
|
||||
- **`CondBrInst`**:表示条件跳转指令。
|
||||
- **`print()`**:打印条件跳转的表示。
|
||||
- **`AllocaInst`**:表示栈内存分配指令。
|
||||
- **`print()`**:打印内存分配的表示。
|
||||
- **`LoadInst`**:表示从内存加载值的指令。
|
||||
- **`print()`**:打印加载指令的表示。
|
||||
- **`StoreInst`**:表示将值存储到内存的指令。
|
||||
- **`print()`**:打印存储指令的表示。
|
||||
|
||||
---
|
||||
|
||||
### **6. 函数(Function)**
|
||||
#### **6.1 `Function` 类的实现**
|
||||
- **构造函数**:初始化函数的名称、返回类型和参数类型。
|
||||
- **`print()`**:打印函数的表示,包括基本块和指令。
|
||||
|
||||
---
|
||||
|
||||
### **7. 模块(Module)**
|
||||
#### **7.1 `Module` 类的实现**
|
||||
- **`print()`**:打印模块的表示,包括所有函数和全局变量。
|
||||
|
||||
---
|
||||
|
||||
### **8. 用户(User)**
|
||||
#### **8.1 `User` 类的实现**
|
||||
- **`setOperand(int index, Value *value)`**:设置指定索引的操作数。
|
||||
- **`replaceOperand(int index, Value *value)`**:替换指定索引的操作数,并更新用途列表。
|
||||
|
||||
---
|
||||
|
||||
### **9. 总结**
|
||||
- **类型系统**:实现了 `Type`、`PointerType` 和 `FunctionType`,用于表示 IR 中的类型。
|
||||
- **值**:实现了 `Value`、`ConstantValue` 和 `Argument`,用于表示 IR 中的值和参数。
|
||||
- **基本块**:实现了 `BasicBlock`,用于组织指令。
|
||||
- **指令**:实现了多种具体指令类(如 `CallInst`、`BinaryInst` 等),用于表示 IR 中的操作。
|
||||
- **函数和模块**:实现了 `Function` 和 `Module`,用于组织 IR 的结构。
|
||||
- **打印功能**:通过 `print()` 方法,可以将 IR 的内容输出为可读的文本格式。
|
||||
|
||||
这个文件是编译器中间表示的核心实现,能够将抽象语法树(AST)转换为中间代码,并支持后续的优化和目标代码生成。
|
||||
20878
doc/n1124.pdf
20878
doc/n1124.pdf
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -196,139 +196,7 @@ std::string RISCv64CodeGen::module_gen() {
|
||||
}
|
||||
|
||||
std::string RISCv64CodeGen::function_gen(Function* func) {
|
||||
if (DEBUG) {
|
||||
// === 完整的后端处理流水线 ===
|
||||
|
||||
// 阶段 1: 指令选择 (sysy::IR -> LLIR with virtual registers)
|
||||
DEBUG = 0;
|
||||
DEEPDEBUG = 0;
|
||||
|
||||
RISCv64ISel isel;
|
||||
std::unique_ptr<MachineFunction> mfunc = isel.runOnFunction(func);
|
||||
|
||||
// 第一次调试打印输出
|
||||
std::stringstream ss_after_isel;
|
||||
RISCv64AsmPrinter printer_isel(mfunc.get());
|
||||
printer_isel.run(ss_after_isel, true);
|
||||
// DEBUG = 1;
|
||||
// DEEPDEBUG = 1;
|
||||
if (DEBUG) {
|
||||
std::cerr << "====== Intermediate Representation after Instruction Selection ======\n"
|
||||
<< ss_after_isel.str();
|
||||
}
|
||||
|
||||
// 阶段 2: 消除帧索引 (展开伪指令,计算局部变量偏移)
|
||||
// 这个Pass必须在寄存器分配之前运行
|
||||
EliminateFrameIndicesPass efi_pass;
|
||||
efi_pass.runOnMachineFunction(mfunc.get());
|
||||
|
||||
if (DEBUG) {
|
||||
std::cerr << "====== stack info after eliminate frame indices ======\n";
|
||||
mfunc->dumpStackFrameInfo(std::cerr);
|
||||
std::stringstream ss_after_eli;
|
||||
printer_isel.run(ss_after_eli, true);
|
||||
std::cerr << "====== LLIR after eliminate frame indices ======\n"
|
||||
<< ss_after_eli.str();
|
||||
}
|
||||
|
||||
// 阶段 2: 除法强度削弱优化 (Division Strength Reduction)
|
||||
DivStrengthReduction div_strength_reduction;
|
||||
div_strength_reduction.runOnMachineFunction(mfunc.get());
|
||||
|
||||
// // 阶段 2.1: 指令调度 (Instruction Scheduling)
|
||||
// PreRA_Scheduler scheduler;
|
||||
// scheduler.runOnMachineFunction(mfunc.get());
|
||||
|
||||
// 阶段 3: 物理寄存器分配 (Register Allocation)
|
||||
|
||||
DEBUG = 1;
|
||||
// DEEPERDEBUG = 1;
|
||||
|
||||
// 阶段 3: 物理寄存器分配 (带超时回退机制)
|
||||
|
||||
/*
|
||||
* [临时修改]
|
||||
* 为了优先测试线性扫描分配器,暂时注释掉图着色分配器及其超时逻辑。
|
||||
* 原始逻辑是:优先使用图着色,超时(20s)后回退到线性扫描。
|
||||
* 在线性扫描分配器测试稳定后,可以恢复此处的代码。
|
||||
*/
|
||||
/*
|
||||
// 首先尝试图着色分配器
|
||||
if (DEBUG) std::cerr << "Attempting Register Allocation with Graph Coloring...\n";
|
||||
RISCv64RegAlloc gc_alloc(mfunc.get());
|
||||
|
||||
// 异步执行图着色分配
|
||||
auto future = std::async(std::launch::async, [&gc_alloc]{
|
||||
gc_alloc.run();
|
||||
});
|
||||
|
||||
// 等待最多20秒
|
||||
auto status = future.wait_for(std::chrono::seconds(20));
|
||||
|
||||
if (status == std::future_status::timeout) {
|
||||
// 超时,切换到线性扫描分配器
|
||||
std::cerr << "Warning: Graph coloring register allocation timed out for function '"
|
||||
<< func->getName()
|
||||
<< "'. Switching to Linear Scan allocator."
|
||||
<< std::endl;
|
||||
|
||||
// 注意:由于无法安全地停止gc_alloc线程,我们只能放弃它的结果。
|
||||
// 在此项目中,我们假设超时后原mfunc状态未被严重破坏,
|
||||
// 或者线性扫描会基于isel后的状态重新开始。
|
||||
// 为了安全,我们应该用一个新的mfunc或者重置mfunc状态,
|
||||
// 但在这里我们简化处理,直接在同一个mfunc上运行线性扫描。
|
||||
|
||||
RISCv64LinearScan ls_alloc(mfunc.get());
|
||||
ls_alloc.run();
|
||||
|
||||
} else {
|
||||
// 图着色成功完成
|
||||
if (DEBUG) std::cerr << "Graph Coloring allocation completed successfully.\n";
|
||||
// future.get()会重新抛出在线程中发生的任何异常
|
||||
future.get();
|
||||
}
|
||||
*/
|
||||
|
||||
// [临时修改] 直接调用线性扫描分配器进行测试
|
||||
std::cerr << "Info: Directly testing Register Allocation with Linear Scan...\n";
|
||||
RISCv64LinearScan ls_alloc(mfunc.get());
|
||||
ls_alloc.run();
|
||||
|
||||
// 阶段 3.1: 处理被调用者保存寄存器
|
||||
CalleeSavedHandler callee_handler;
|
||||
callee_handler.runOnMachineFunction(mfunc.get());
|
||||
|
||||
if (DEBUG) {
|
||||
std::cerr << "====== stack info after callee handler ======\n";
|
||||
mfunc->dumpStackFrameInfo(std::cerr);
|
||||
}
|
||||
|
||||
// // 阶段 4: 窥孔优化 (Peephole Optimization)
|
||||
// PeepholeOptimizer peephole;
|
||||
// peephole.runOnMachineFunction(mfunc.get());
|
||||
|
||||
// 阶段 5: 局部指令调度 (Local Scheduling)
|
||||
PostRA_Scheduler local_scheduler;
|
||||
local_scheduler.runOnMachineFunction(mfunc.get());
|
||||
|
||||
// 阶段 3.2: 插入序言和尾声
|
||||
PrologueEpilogueInsertionPass pei_pass;
|
||||
pei_pass.runOnMachineFunction(mfunc.get());
|
||||
|
||||
DEBUG = 0;
|
||||
DEEPDEBUG = 0;
|
||||
// 阶段 3.3: 大立即数合法化
|
||||
LegalizeImmediatesPass legalizer;
|
||||
legalizer.runOnMachineFunction(mfunc.get());
|
||||
|
||||
// 阶段 6: 代码发射 (Code Emission)
|
||||
std::stringstream ss;
|
||||
RISCv64AsmPrinter printer(mfunc.get());
|
||||
printer.run(ss);
|
||||
|
||||
return ss.str();
|
||||
} else {
|
||||
// === 完整的后端处理流水线 ===
|
||||
// === 完整的后端处理流水线 ===
|
||||
|
||||
// 阶段 1: 指令选择 (sysy::IR -> LLIR with virtual registers)
|
||||
RISCv64ISel isel;
|
||||
@ -367,12 +235,8 @@ std::string RISCv64CodeGen::function_gen(Function* func) {
|
||||
// scheduler.runOnMachineFunction(mfunc.get());
|
||||
|
||||
// 阶段 3: 物理寄存器分配 (Register Allocation)
|
||||
// RISCv64RegAlloc reg_alloc(mfunc.get());
|
||||
// reg_alloc.run();
|
||||
// [临时修改] 直接调用线性扫描分配器进行测试
|
||||
std::cerr << "Info: Directly testing Register Allocation with Linear Scan...\n";
|
||||
RISCv64LinearScan ls_alloc(mfunc.get());
|
||||
ls_alloc.run();
|
||||
RISCv64RegAlloc reg_alloc(mfunc.get());
|
||||
reg_alloc.run();
|
||||
|
||||
if (DEBUG) {
|
||||
std::cerr << "====== stack info after reg alloc ======\n";
|
||||
@ -410,7 +274,6 @@ std::string RISCv64CodeGen::function_gen(Function* func) {
|
||||
printer.run(ss);
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
1
testdata/functional/00_main.out
vendored
1
testdata/functional/00_main.out
vendored
@ -1 +0,0 @@
|
||||
3
|
||||
3
testdata/functional/00_main.sy
vendored
3
testdata/functional/00_main.sy
vendored
@ -1,3 +0,0 @@
|
||||
int main(){
|
||||
return 3;
|
||||
}
|
||||
1
testdata/functional/01_var_defn2.out
vendored
1
testdata/functional/01_var_defn2.out
vendored
@ -1 +0,0 @@
|
||||
10
|
||||
8
testdata/functional/01_var_defn2.sy
vendored
8
testdata/functional/01_var_defn2.sy
vendored
@ -1,8 +0,0 @@
|
||||
//test domain of global var define and local define
|
||||
int a = 3;
|
||||
int b = 5;
|
||||
|
||||
int main(){
|
||||
int a = 5;
|
||||
return a + b;
|
||||
}
|
||||
1
testdata/functional/02_var_defn3.out
vendored
1
testdata/functional/02_var_defn3.out
vendored
@ -1 +0,0 @@
|
||||
5
|
||||
8
testdata/functional/02_var_defn3.sy
vendored
8
testdata/functional/02_var_defn3.sy
vendored
@ -1,8 +0,0 @@
|
||||
//test local var define
|
||||
int main(){
|
||||
int a, b0, _c;
|
||||
a = 1;
|
||||
b0 = 2;
|
||||
_c = 3;
|
||||
return b0 + _c;
|
||||
}
|
||||
1
testdata/functional/03_arr_defn2.out
vendored
1
testdata/functional/03_arr_defn2.out
vendored
@ -1 +0,0 @@
|
||||
0
|
||||
4
testdata/functional/03_arr_defn2.sy
vendored
4
testdata/functional/03_arr_defn2.sy
vendored
@ -1,4 +0,0 @@
|
||||
int a[10][10];
|
||||
int main(){
|
||||
return 0;
|
||||
}
|
||||
1
testdata/functional/04_arr_defn3.out
vendored
1
testdata/functional/04_arr_defn3.out
vendored
@ -1 +0,0 @@
|
||||
14
|
||||
9
testdata/functional/04_arr_defn3.sy
vendored
9
testdata/functional/04_arr_defn3.sy
vendored
@ -1,9 +0,0 @@
|
||||
//test array define
|
||||
int main(){
|
||||
int a[4][2] = {};
|
||||
int b[4][2] = {1, 2, 3, 4, 5, 6, 7, 8};
|
||||
int c[4][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
|
||||
int d[4][2] = {1, 2, {3}, {5}, 7 , 8};
|
||||
int e[4][2] = {{d[2][1], c[2][1]}, {3, 4}, {5, 6}, {7, 8}};
|
||||
return e[3][1] + e[0][0] + e[0][1] + a[2][0];
|
||||
}
|
||||
1
testdata/functional/05_arr_defn4.out
vendored
1
testdata/functional/05_arr_defn4.out
vendored
@ -1 +0,0 @@
|
||||
21
|
||||
9
testdata/functional/05_arr_defn4.sy
vendored
9
testdata/functional/05_arr_defn4.sy
vendored
@ -1,9 +0,0 @@
|
||||
int main(){
|
||||
const int a[4][2] = {{1, 2}, {3, 4}, {}, 7};
|
||||
|
||||
int b[4][2] = {};
|
||||
int c[4][2] = {1, 2, 3, 4, 5, 6, 7, 8};
|
||||
int d[3 + 1][2] = {1, 2, {3}, {5}, a[3][0], 8};
|
||||
int e[4][2][1] = {{d[2][1], {c[2][1]}}, {3, 4}, {5, 6}, {7, 8}};
|
||||
return e[3][1][0] + e[0][0][0] + e[0][1][0] + d[3][0];
|
||||
}
|
||||
1
testdata/functional/06_const_var_defn2.out
vendored
1
testdata/functional/06_const_var_defn2.out
vendored
@ -1 +0,0 @@
|
||||
5
|
||||
6
testdata/functional/06_const_var_defn2.sy
vendored
6
testdata/functional/06_const_var_defn2.sy
vendored
@ -1,6 +0,0 @@
|
||||
//test const gloal var define
|
||||
const int a = 10, b = 5;
|
||||
|
||||
int main(){
|
||||
return b;
|
||||
}
|
||||
1
testdata/functional/07_const_var_defn3.out
vendored
1
testdata/functional/07_const_var_defn3.out
vendored
@ -1 +0,0 @@
|
||||
5
|
||||
5
testdata/functional/07_const_var_defn3.sy
vendored
5
testdata/functional/07_const_var_defn3.sy
vendored
@ -1,5 +0,0 @@
|
||||
//test const local var define
|
||||
int main(){
|
||||
const int a = 10, b = 5;
|
||||
return b;
|
||||
}
|
||||
1
testdata/functional/08_const_array_defn.out
vendored
1
testdata/functional/08_const_array_defn.out
vendored
@ -1 +0,0 @@
|
||||
4
|
||||
5
testdata/functional/08_const_array_defn.sy
vendored
5
testdata/functional/08_const_array_defn.sy
vendored
@ -1,5 +0,0 @@
|
||||
const int a[5]={0,1,2,3,4};
|
||||
|
||||
int main(){
|
||||
return a[4];
|
||||
}
|
||||
1
testdata/functional/09_func_defn.out
vendored
1
testdata/functional/09_func_defn.out
vendored
@ -1 +0,0 @@
|
||||
9
|
||||
11
testdata/functional/09_func_defn.sy
vendored
11
testdata/functional/09_func_defn.sy
vendored
@ -1,11 +0,0 @@
|
||||
int a;
|
||||
int func(int p){
|
||||
p = p - 1;
|
||||
return p;
|
||||
}
|
||||
int main(){
|
||||
int b;
|
||||
a = 10;
|
||||
b = func(a);
|
||||
return b;
|
||||
}
|
||||
1
testdata/functional/10_var_defn_func.out
vendored
1
testdata/functional/10_var_defn_func.out
vendored
@ -1 +0,0 @@
|
||||
4
|
||||
8
testdata/functional/10_var_defn_func.sy
vendored
8
testdata/functional/10_var_defn_func.sy
vendored
@ -1,8 +0,0 @@
|
||||
int defn(){
|
||||
return 4;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int a=defn();
|
||||
return a;
|
||||
}
|
||||
1
testdata/functional/11_add2.out
vendored
1
testdata/functional/11_add2.out
vendored
@ -1 +0,0 @@
|
||||
9
|
||||
7
testdata/functional/11_add2.sy
vendored
7
testdata/functional/11_add2.sy
vendored
@ -1,7 +0,0 @@
|
||||
//test add
|
||||
int main(){
|
||||
int a, b;
|
||||
a = 10;
|
||||
b = -1;
|
||||
return a + b;
|
||||
}
|
||||
1
testdata/functional/12_addc.out
vendored
1
testdata/functional/12_addc.out
vendored
@ -1 +0,0 @@
|
||||
15
|
||||
5
testdata/functional/12_addc.sy
vendored
5
testdata/functional/12_addc.sy
vendored
@ -1,5 +0,0 @@
|
||||
//test addc
|
||||
const int a = 10;
|
||||
int main(){
|
||||
return a + 5;
|
||||
}
|
||||
1
testdata/functional/13_sub2.out
vendored
1
testdata/functional/13_sub2.out
vendored
@ -1 +0,0 @@
|
||||
248
|
||||
7
testdata/functional/13_sub2.sy
vendored
7
testdata/functional/13_sub2.sy
vendored
@ -1,7 +0,0 @@
|
||||
//test sub
|
||||
const int a = 10;
|
||||
int main(){
|
||||
int b;
|
||||
b = 2;
|
||||
return b - a;
|
||||
}
|
||||
1
testdata/functional/14_subc.out
vendored
1
testdata/functional/14_subc.out
vendored
@ -1 +0,0 @@
|
||||
8
|
||||
6
testdata/functional/14_subc.sy
vendored
6
testdata/functional/14_subc.sy
vendored
@ -1,6 +0,0 @@
|
||||
//test subc
|
||||
int main(){
|
||||
int a;
|
||||
a = 10;
|
||||
return a - 2;
|
||||
}
|
||||
1
testdata/functional/15_mul.out
vendored
1
testdata/functional/15_mul.out
vendored
@ -1 +0,0 @@
|
||||
50
|
||||
7
testdata/functional/15_mul.sy
vendored
7
testdata/functional/15_mul.sy
vendored
@ -1,7 +0,0 @@
|
||||
//test mul
|
||||
int main(){
|
||||
int a, b;
|
||||
a = 10;
|
||||
b = 5;
|
||||
return a * b;
|
||||
}
|
||||
1
testdata/functional/16_mulc.out
vendored
1
testdata/functional/16_mulc.out
vendored
@ -1 +0,0 @@
|
||||
25
|
||||
5
testdata/functional/16_mulc.sy
vendored
5
testdata/functional/16_mulc.sy
vendored
@ -1,5 +0,0 @@
|
||||
//test mulc
|
||||
const int a = 5;
|
||||
int main(){
|
||||
return a * 5;
|
||||
}
|
||||
1
testdata/functional/17_div.out
vendored
1
testdata/functional/17_div.out
vendored
@ -1 +0,0 @@
|
||||
2
|
||||
7
testdata/functional/17_div.sy
vendored
7
testdata/functional/17_div.sy
vendored
@ -1,7 +0,0 @@
|
||||
//test div
|
||||
int main(){
|
||||
int a, b;
|
||||
a = 10;
|
||||
b = 5;
|
||||
return a / b;
|
||||
}
|
||||
1
testdata/functional/18_divc.out
vendored
1
testdata/functional/18_divc.out
vendored
@ -1 +0,0 @@
|
||||
2
|
||||
5
testdata/functional/18_divc.sy
vendored
5
testdata/functional/18_divc.sy
vendored
@ -1,5 +0,0 @@
|
||||
//test divc
|
||||
const int a = 10;
|
||||
int main(){
|
||||
return a / 5;
|
||||
}
|
||||
1
testdata/functional/19_mod.out
vendored
1
testdata/functional/19_mod.out
vendored
@ -1 +0,0 @@
|
||||
3
|
||||
6
testdata/functional/19_mod.sy
vendored
6
testdata/functional/19_mod.sy
vendored
@ -1,6 +0,0 @@
|
||||
//test mod
|
||||
int main(){
|
||||
int a;
|
||||
a = 10;
|
||||
return a / 3;
|
||||
}
|
||||
1
testdata/functional/20_rem.out
vendored
1
testdata/functional/20_rem.out
vendored
@ -1 +0,0 @@
|
||||
1
|
||||
6
testdata/functional/20_rem.sy
vendored
6
testdata/functional/20_rem.sy
vendored
@ -1,6 +0,0 @@
|
||||
//test rem
|
||||
int main(){
|
||||
int a;
|
||||
a = 10;
|
||||
return a % 3;
|
||||
}
|
||||
2
testdata/functional/21_if_test2.out
vendored
2
testdata/functional/21_if_test2.out
vendored
@ -1,2 +0,0 @@
|
||||
-5
|
||||
0
|
||||
25
testdata/functional/21_if_test2.sy
vendored
25
testdata/functional/21_if_test2.sy
vendored
@ -1,25 +0,0 @@
|
||||
// test if-else-if
|
||||
int ifElseIf() {
|
||||
int a;
|
||||
a = 5;
|
||||
int b;
|
||||
b = 10;
|
||||
if(a == 6 || b == 0xb) {
|
||||
return a;
|
||||
}
|
||||
else {
|
||||
if (b == 10 && a == 1)
|
||||
a = 25;
|
||||
else if (b == 10 && a == -5)
|
||||
a = a + 15;
|
||||
else
|
||||
a = -+a;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
int main(){
|
||||
putint(ifElseIf());
|
||||
return 0;
|
||||
}
|
||||
1
testdata/functional/22_if_test3.out
vendored
1
testdata/functional/22_if_test3.out
vendored
@ -1 +0,0 @@
|
||||
25
|
||||
18
testdata/functional/22_if_test3.sy
vendored
18
testdata/functional/22_if_test3.sy
vendored
@ -1,18 +0,0 @@
|
||||
// test if-if-else
|
||||
int ififElse() {
|
||||
int a;
|
||||
a = 5;
|
||||
int b;
|
||||
b = 10;
|
||||
if(a == 5)
|
||||
if (b == 10)
|
||||
a = 25;
|
||||
else
|
||||
a = a + 15;
|
||||
|
||||
return (a);
|
||||
}
|
||||
|
||||
int main(){
|
||||
return (ififElse());
|
||||
}
|
||||
1
testdata/functional/23_if_test4.out
vendored
1
testdata/functional/23_if_test4.out
vendored
@ -1 +0,0 @@
|
||||
25
|
||||
18
testdata/functional/23_if_test4.sy
vendored
18
testdata/functional/23_if_test4.sy
vendored
@ -1,18 +0,0 @@
|
||||
// test if-{if-else}
|
||||
int if_ifElse_() {
|
||||
int a;
|
||||
a = 5;
|
||||
int b;
|
||||
b = 10;
|
||||
if(a == 5){
|
||||
if (b == 10)
|
||||
a = 25;
|
||||
else
|
||||
a = a + 15;
|
||||
}
|
||||
return (a);
|
||||
}
|
||||
|
||||
int main(){
|
||||
return (if_ifElse_());
|
||||
}
|
||||
1
testdata/functional/24_if_test5.out
vendored
1
testdata/functional/24_if_test5.out
vendored
@ -1 +0,0 @@
|
||||
25
|
||||
18
testdata/functional/24_if_test5.sy
vendored
18
testdata/functional/24_if_test5.sy
vendored
@ -1,18 +0,0 @@
|
||||
// test if-{if}-else
|
||||
int if_if_Else() {
|
||||
int a;
|
||||
a = 5;
|
||||
int b;
|
||||
b = 10;
|
||||
if(a == 5){
|
||||
if (b == 10)
|
||||
a = 25;
|
||||
}
|
||||
else
|
||||
a = a + 15;
|
||||
return (a);
|
||||
}
|
||||
|
||||
int main(){
|
||||
return (if_if_Else());
|
||||
}
|
||||
2
testdata/functional/25_while_if.out
vendored
2
testdata/functional/25_while_if.out
vendored
@ -1,2 +0,0 @@
|
||||
88
|
||||
0
|
||||
31
testdata/functional/25_while_if.sy
vendored
31
testdata/functional/25_while_if.sy
vendored
@ -1,31 +0,0 @@
|
||||
int get_one(int a) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int deepWhileBr(int a, int b) {
|
||||
int c;
|
||||
c = a + b;
|
||||
while (c < 75) {
|
||||
int d;
|
||||
d = 42;
|
||||
if (c < 100) {
|
||||
c = c + d;
|
||||
if (c > 99) {
|
||||
int e;
|
||||
e = d * 2;
|
||||
if (get_one(0) == 1) {
|
||||
c = e * 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (c);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int p;
|
||||
p = 2;
|
||||
p = deepWhileBr(p, p);
|
||||
putint(p);
|
||||
return 0;
|
||||
}
|
||||
1
testdata/functional/26_while_test1.out
vendored
1
testdata/functional/26_while_test1.out
vendored
@ -1 +0,0 @@
|
||||
3
|
||||
18
testdata/functional/26_while_test1.sy
vendored
18
testdata/functional/26_while_test1.sy
vendored
@ -1,18 +0,0 @@
|
||||
int doubleWhile() {
|
||||
int i;
|
||||
i = 5;
|
||||
int j;
|
||||
j = 7;
|
||||
while (i < 100) {
|
||||
i = i + 30;
|
||||
while(j < 100){
|
||||
j = j + 6;
|
||||
}
|
||||
j = j - 100;
|
||||
}
|
||||
return (j);
|
||||
}
|
||||
|
||||
int main() {
|
||||
return doubleWhile();
|
||||
}
|
||||
1
testdata/functional/27_while_test2.out
vendored
1
testdata/functional/27_while_test2.out
vendored
@ -1 +0,0 @@
|
||||
54
|
||||
31
testdata/functional/27_while_test2.sy
vendored
31
testdata/functional/27_while_test2.sy
vendored
@ -1,31 +0,0 @@
|
||||
int FourWhile() {
|
||||
int a;
|
||||
a = 5;
|
||||
int b;
|
||||
int c;
|
||||
b = 6;
|
||||
c = 7;
|
||||
int d;
|
||||
d = 10;
|
||||
while (a < 20) {
|
||||
a = a + 3;
|
||||
while(b < 10){
|
||||
b = b + 1;
|
||||
while(c == 7){
|
||||
c = c - 1;
|
||||
while(d < 20){
|
||||
d = d + 3;
|
||||
}
|
||||
d = d - 1;
|
||||
}
|
||||
c = c + 1;
|
||||
}
|
||||
b = b - 2;
|
||||
}
|
||||
|
||||
return (a + (b + d) + c);
|
||||
}
|
||||
|
||||
int main() {
|
||||
return FourWhile();
|
||||
}
|
||||
1
testdata/functional/28_while_test3.out
vendored
1
testdata/functional/28_while_test3.out
vendored
@ -1 +0,0 @@
|
||||
23
|
||||
55
testdata/functional/28_while_test3.sy
vendored
55
testdata/functional/28_while_test3.sy
vendored
@ -1,55 +0,0 @@
|
||||
int g;
|
||||
int h;
|
||||
int f;
|
||||
int e;
|
||||
int EightWhile() {
|
||||
int a;
|
||||
a = 5;
|
||||
int b;
|
||||
int c;
|
||||
b = 6;
|
||||
c = 7;
|
||||
int d;
|
||||
d = 10;
|
||||
while (a < 20) {
|
||||
a = a + 3;
|
||||
while(b < 10){
|
||||
b = b + 1;
|
||||
while(c == 7){
|
||||
c = c - 1;
|
||||
while(d < 20){
|
||||
d = d + 3;
|
||||
while(e > 1){
|
||||
e = e-1;
|
||||
while(f > 2){
|
||||
f = f -2;
|
||||
while(g < 3){
|
||||
g = g +10;
|
||||
while(h < 10){
|
||||
h = h + 8;
|
||||
}
|
||||
h = h-1;
|
||||
}
|
||||
g = g- 8;
|
||||
}
|
||||
f = f + 1;
|
||||
}
|
||||
e = e + 1;
|
||||
}
|
||||
d = d - 1;
|
||||
}
|
||||
c = c + 1;
|
||||
}
|
||||
b = b - 2;
|
||||
}
|
||||
|
||||
return (a + (b + d) + c)-(e + d - g + h);
|
||||
}
|
||||
|
||||
int main() {
|
||||
g = 1;
|
||||
h = 2;
|
||||
e = 4;
|
||||
f = 6;
|
||||
return EightWhile();
|
||||
}
|
||||
1
testdata/functional/29_break.out
vendored
1
testdata/functional/29_break.out
vendored
@ -1 +0,0 @@
|
||||
201
|
||||
15
testdata/functional/29_break.sy
vendored
15
testdata/functional/29_break.sy
vendored
@ -1,15 +0,0 @@
|
||||
//test break
|
||||
int main(){
|
||||
int i;
|
||||
i = 0;
|
||||
int sum;
|
||||
sum = 0;
|
||||
while(i < 100){
|
||||
if(i == 50){
|
||||
break;
|
||||
}
|
||||
sum = sum + i;
|
||||
i = i + 1;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
1
testdata/functional/30_continue.out
vendored
1
testdata/functional/30_continue.out
vendored
@ -1 +0,0 @@
|
||||
36
|
||||
16
testdata/functional/30_continue.sy
vendored
16
testdata/functional/30_continue.sy
vendored
@ -1,16 +0,0 @@
|
||||
//test continue
|
||||
int main(){
|
||||
int i;
|
||||
i = 0;
|
||||
int sum;
|
||||
sum = 0;
|
||||
while(i < 100){
|
||||
if(i == 50){
|
||||
i = i + 1;
|
||||
continue;
|
||||
}
|
||||
sum = sum + i;
|
||||
i = i + 1;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
1
testdata/functional/31_while_if_test1.out
vendored
1
testdata/functional/31_while_if_test1.out
vendored
@ -1 +0,0 @@
|
||||
198
|
||||
25
testdata/functional/31_while_if_test1.sy
vendored
25
testdata/functional/31_while_if_test1.sy
vendored
@ -1,25 +0,0 @@
|
||||
// test while-if
|
||||
int whileIf() {
|
||||
int a;
|
||||
a = 0;
|
||||
int b;
|
||||
b = 0;
|
||||
while (a < 100) {
|
||||
if (a == 5) {
|
||||
b = 25;
|
||||
}
|
||||
else if (a == 10) {
|
||||
b = 42;
|
||||
}
|
||||
else {
|
||||
b = a * 2;
|
||||
}
|
||||
a = a + 1;
|
||||
}
|
||||
return (b);
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
return (whileIf());
|
||||
}
|
||||
1
testdata/functional/32_while_if_test2.out
vendored
1
testdata/functional/32_while_if_test2.out
vendored
@ -1 +0,0 @@
|
||||
96
|
||||
23
testdata/functional/32_while_if_test2.sy
vendored
23
testdata/functional/32_while_if_test2.sy
vendored
@ -1,23 +0,0 @@
|
||||
int ifWhile() {
|
||||
int a;
|
||||
a = 0;
|
||||
int b;
|
||||
b = 3;
|
||||
if (a == 5) {
|
||||
while(b == 2){
|
||||
b = b + 2;
|
||||
}
|
||||
b = b + 25;
|
||||
}
|
||||
else
|
||||
while (a < 5) {
|
||||
b = b * 2;
|
||||
a = a + 1;
|
||||
}
|
||||
return (b);
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
return (ifWhile());
|
||||
}
|
||||
1
testdata/functional/33_while_if_test3.out
vendored
1
testdata/functional/33_while_if_test3.out
vendored
@ -1 +0,0 @@
|
||||
88
|
||||
25
testdata/functional/33_while_if_test3.sy
vendored
25
testdata/functional/33_while_if_test3.sy
vendored
@ -1,25 +0,0 @@
|
||||
int deepWhileBr(int a, int b) {
|
||||
int c;
|
||||
c = a + b;
|
||||
while (c < 75) {
|
||||
int d;
|
||||
d = 42;
|
||||
if (c < 100) {
|
||||
c = c + d;
|
||||
if (c > 99) {
|
||||
int e;
|
||||
e = d * 2;
|
||||
if (1 == 1) {
|
||||
c = e * 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (c);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int p;
|
||||
p = 2;
|
||||
return deepWhileBr(p, p);
|
||||
}
|
||||
1
testdata/functional/34_arr_expr_len.out
vendored
1
testdata/functional/34_arr_expr_len.out
vendored
@ -1 +0,0 @@
|
||||
51
|
||||
11
testdata/functional/34_arr_expr_len.sy
vendored
11
testdata/functional/34_arr_expr_len.sy
vendored
@ -1,11 +0,0 @@
|
||||
//const int N = -1;
|
||||
int arr[-1 + 2 * 4 - 99 / 99] = {1, 2, 33, 4, 5, 6};
|
||||
|
||||
int main() {
|
||||
int i = 0, sum = 0;
|
||||
while (i < 6) {
|
||||
sum = sum + arr[i];
|
||||
i = i + 1;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
1
testdata/functional/35_op_priority1.out
vendored
1
testdata/functional/35_op_priority1.out
vendored
@ -1 +0,0 @@
|
||||
40
|
||||
9
testdata/functional/35_op_priority1.sy
vendored
9
testdata/functional/35_op_priority1.sy
vendored
@ -1,9 +0,0 @@
|
||||
//test the priority of add and mul
|
||||
int main(){
|
||||
int a, b, c, d;
|
||||
a = 10;
|
||||
b = 4;
|
||||
c = 2;
|
||||
d = 2;
|
||||
return c + a * b - d;
|
||||
}
|
||||
1
testdata/functional/36_op_priority2.out
vendored
1
testdata/functional/36_op_priority2.out
vendored
@ -1 +0,0 @@
|
||||
24
|
||||
9
testdata/functional/36_op_priority2.sy
vendored
9
testdata/functional/36_op_priority2.sy
vendored
@ -1,9 +0,0 @@
|
||||
//test the priority of add and mul
|
||||
int main(){
|
||||
int a, b, c, d;
|
||||
a = 10;
|
||||
b = 4;
|
||||
c = 2;
|
||||
d = 2;
|
||||
return (c + a) * (b - d);
|
||||
}
|
||||
1
testdata/functional/37_op_priority3.out
vendored
1
testdata/functional/37_op_priority3.out
vendored
@ -1 +0,0 @@
|
||||
40
|
||||
7
testdata/functional/37_op_priority3.sy
vendored
7
testdata/functional/37_op_priority3.sy
vendored
@ -1,7 +0,0 @@
|
||||
//test the priority of unary operator and binary operator
|
||||
int main(){
|
||||
int a, b;
|
||||
a = 10;
|
||||
b = 30;
|
||||
return a - -5 + b + -5;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user