forked from NUDT-compiler/nudt-compiler-cpp
- 核心功能支持:完整实现 if-else, while, break, continue 语句的 IR 生成及跳出块维护 - 扩展指令系统:新增 Cmp, Zext, Br, CondBr 指令,补齐 Builder/Printer 接口 - 支持逻辑表达式:完成 RelExp, EqExp, 逻辑短路求值 (LAndExp, LOrExp) 及逻辑非的翻译 - 架构调整机制 BUG 修复: 1. 修复 Sema.cpp 关系表达式 AST 丢失部分分支变量追踪的解析 Bug 2. 修复 IRGenDecl 中打断判定缺失导致生成冗余死块终止符的问题 3. 优化临时变量名为 %tN,解决纯数值标号引发的 llc 命名序列检查报错 - 文档更新:于 lab2剩余任务分工.md 中标记相关进度为已完成并新增测试运行说明lyy
parent
5cc0bf587a
commit
2de1561210
@ -0,0 +1,83 @@
|
||||
--- include/ir/IR.h
|
||||
+++ include/ir/IR.h
|
||||
@@ -93,6 +93,7 @@
|
||||
class Type {
|
||||
public:
|
||||
- enum class Kind { Void, Int32, PtrInt32 };
|
||||
+ enum class Kind { Void, Int1, Int32, PtrInt32 };
|
||||
explicit Type(Kind k);
|
||||
// 使用静态共享对象获取类型。
|
||||
// 同一类型可直接比较返回值是否相等,例如:
|
||||
// Type::GetInt32Type() == Type::GetInt32Type()
|
||||
static const std::shared_ptr<Type>& GetVoidType();
|
||||
+ static const std::shared_ptr<Type>& GetInt1Type();
|
||||
static const std::shared_ptr<Type>& GetInt32Type();
|
||||
static const std::shared_ptr<Type>& GetPtrInt32Type();
|
||||
Kind GetKind() const;
|
||||
bool IsVoid() const;
|
||||
+ bool IsInt1() const;
|
||||
bool IsInt32() const;
|
||||
bool IsPtrInt32() const;
|
||||
@@ -118,6 +119,7 @@
|
||||
const std::string& GetName() const;
|
||||
void SetName(std::string n);
|
||||
bool IsVoid() const;
|
||||
+ bool IsInt1() const;
|
||||
bool IsInt32() const;
|
||||
bool IsPtrInt32() const;
|
||||
bool IsConstant() const;
|
||||
@@ -153,7 +155,9 @@
|
||||
|
||||
// 后续还需要扩展更多指令类型。
|
||||
-// enum class Opcode { Add, Sub, Mul, Alloca, Load, Store, Ret };
|
||||
-enum class Opcode { Add, Sub, Mul, Div, Mod, Neg, Alloca, Load, Store, Ret };
|
||||
+enum class Opcode { Add, Sub, Mul, Div, Mod, Neg, Alloca, Load, Store, Ret, Cmp, Zext, Br, CondBr };
|
||||
+
|
||||
+enum class CmpOp { Eq, Ne, Lt, Gt, Le, Ge };
|
||||
|
||||
// User 是所有“会使用其他 Value 作为输入”的 IR 对象的抽象基类。
|
||||
@@ -231,6 +235,33 @@
|
||||
Value* GetPtr() const;
|
||||
};
|
||||
|
||||
+class CmpInst : public Instruction {
|
||||
+ public:
|
||||
+ CmpInst(CmpOp cmp_op, Value* lhs, Value* rhs, std::string name);
|
||||
+ CmpOp GetCmpOp() const;
|
||||
+ Value* GetLhs() const;
|
||||
+ Value* GetRhs() const;
|
||||
+ private:
|
||||
+ CmpOp cmp_op_;
|
||||
+};
|
||||
+
|
||||
+class ZextInst : public Instruction {
|
||||
+ public:
|
||||
+ ZextInst(std::shared_ptr<Type> dest_ty, Value* val, std::string name);
|
||||
+ Value* GetValue() const;
|
||||
+};
|
||||
+
|
||||
+class BranchInst : public Instruction {
|
||||
+ public:
|
||||
+ BranchInst(BasicBlock* dest);
|
||||
+ BasicBlock* GetDest() const;
|
||||
+};
|
||||
+
|
||||
+class CondBranchInst : public Instruction {
|
||||
+ public:
|
||||
+ CondBranchInst(Value* cond, BasicBlock* true_bb, BasicBlock* false_bb);
|
||||
+ Value* GetCond() const;
|
||||
+ BasicBlock* GetTrueBlock() const;
|
||||
+ BasicBlock* GetFalseBlock() const;
|
||||
+};
|
||||
+
|
||||
// BasicBlock 已纳入 Value 体系,便于后续向更完整 IR 类图靠拢。
|
||||
@@ -315,6 +346,10 @@
|
||||
LoadInst* CreateLoad(Value* ptr, const std::string& name);
|
||||
StoreInst* CreateStore(Value* val, Value* ptr);
|
||||
ReturnInst* CreateRet(Value* v);
|
||||
+ CmpInst* CreateCmp(CmpOp op, Value* lhs, Value* rhs, const std::string& name);
|
||||
+ ZextInst* CreateZext(Value* val, const std::string& name);
|
||||
+ BranchInst* CreateBr(BasicBlock* dest);
|
||||
+ CondBranchInst* CreateCondBr(Value* cond, BasicBlock* true_bb, BasicBlock* false_bb);
|
||||
|
||||
private:
|
||||
Loading…
Reference in new issue