Complete Lab2 IR generation and document process

This commit is contained in:
2026-04-16 00:21:35 +08:00
parent 6fc0c89072
commit 979d271ebe
23 changed files with 2583 additions and 471 deletions

View File

@@ -37,6 +37,7 @@
#include <unordered_map>
#include <utility>
#include <vector>
#include <cstdint>
namespace ir {
@@ -45,6 +46,7 @@ class Value;
class User;
class ConstantValue;
class ConstantInt;
class ConstantFloat;
class GlobalValue;
class Instruction;
class BasicBlock;
@@ -83,17 +85,20 @@ class Context {
~Context();
// 去重创建 i32 常量。
ConstantInt* GetConstInt(int v);
// 去重创建 float 常量。
ConstantFloat* GetConstFloat(float v);
std::string NextTemp();
private:
std::unordered_map<int, std::unique_ptr<ConstantInt>> const_ints_;
std::unordered_map<float, std::unique_ptr<ConstantFloat>> const_floats_;
int temp_index_ = -1;
};
class Type {
class Type : public std::enable_shared_from_this<Type> {
public:
enum class Kind { Void, Int32, PtrInt32 };
enum class Kind { Void, Int32, PtrInt32, Float, PtrFloat, Label, Array };
explicit Type(Kind k);
// 使用静态共享对象获取类型。
// 同一类型可直接比较返回值是否相等,例如:
@@ -101,15 +106,36 @@ class Type {
static const std::shared_ptr<Type>& GetVoidType();
static const std::shared_ptr<Type>& GetInt32Type();
static const std::shared_ptr<Type>& GetPtrInt32Type();
static const std::shared_ptr<Type>& GetFloatType();
static const std::shared_ptr<Type>& GetPtrFloatType();
static const std::shared_ptr<Type>& GetLabelType();
Kind GetKind() const;
bool IsVoid() const;
bool IsInt32() const;
bool IsPtrInt32() const;
bool IsFloat() const;
bool IsPtrFloat() const;
bool IsLabel() const;
bool IsArray() const;
std::shared_ptr<class ArrayType> GetAsArrayType();
private:
Kind kind_;
};
class ArrayType : public Type {
public:
ArrayType(std::shared_ptr<Type> element_type, uint32_t num_elements);
static std::shared_ptr<ArrayType> Get(std::shared_ptr<Type> element_type,
uint32_t num_elements);
std::shared_ptr<Type> GetElementType() const { return element_type_; }
uint32_t GetNumElements() const { return num_elements_; }
private:
std::shared_ptr<Type> element_type_;
uint32_t num_elements_;
};
class Value {
public:
Value(std::shared_ptr<Type> ty, std::string name);
@@ -120,10 +146,15 @@ class Value {
bool IsVoid() const;
bool IsInt32() const;
bool IsPtrInt32() const;
bool IsFloat() const;
bool IsPtrFloat() const;
bool IsLabel() const;
bool IsConstant() const;
bool IsInstruction() const;
bool IsUser() const;
bool IsFunction() const;
bool IsGlobalValue() const;
bool IsArgument() const;
void AddUse(User* user, size_t operand_index);
void RemoveUse(User* user, size_t operand_index);
const std::vector<Use>& GetUses() const;
@@ -135,6 +166,19 @@ class Value {
std::vector<Use> uses_;
};
// Argument represents a function parameter.
class Argument : public Value {
public:
Argument(std::shared_ptr<Type> ty, std::string name, Function* parent,
unsigned arg_no);
Function* GetParent() const { return parent_; }
unsigned GetArgNo() const { return arg_no_; }
private:
Function* parent_;
unsigned arg_no_;
};
// ConstantValue 是常量体系的基类。
// 当前只实现了 ConstantInt后续可继续扩展更多常量种类。
class ConstantValue : public Value {
@@ -151,8 +195,49 @@ class ConstantInt : public ConstantValue {
int value_{};
};
class ConstantFloat : public ConstantValue {
public:
ConstantFloat(std::shared_ptr<Type> ty, float v);
float GetValue() const { return value_; }
private:
float value_{};
};
// 后续还需要扩展更多指令类型。
enum class Opcode { Add, Sub, Mul, Alloca, Load, Store, Ret };
enum class Opcode {
Add,
Sub,
Mul,
Div,
Mod,
FAdd,
FSub,
FMul,
FDiv,
ICmpEQ,
ICmpNE,
ICmpLT,
ICmpGT,
ICmpLE,
ICmpGE,
FCmpEQ,
FCmpNE,
FCmpLT,
FCmpGT,
FCmpLE,
FCmpGE,
Alloca,
Load,
Store,
Ret,
Br,
Call,
GEP,
ZExt,
SIToFP,
FPToSI
};
// User 是所有“会使用其他 Value 作为输入”的 IR 对象的抽象基类。
// 当前实现中只有 Instruction 继承自 User。
@@ -171,11 +256,15 @@ class User : public Value {
std::vector<Value*> operands_;
};
// GlobalValue 是全局值/全局变量体系的空壳占位类。
// 当前只补齐类层次,具体初始化器、打印和链接语义后续再补。
// GlobalValue 是全局值/全局变量体系的类。
class GlobalValue : public User {
public:
GlobalValue(std::shared_ptr<Type> ty, std::string name);
GlobalValue(std::shared_ptr<Type> ty, std::string name, ConstantValue* init = nullptr);
ConstantValue* GetInitializer() const { return init_; }
void SetInitializer(ConstantValue* init) { init_ = init; }
private:
ConstantValue* init_ = nullptr;
};
class Instruction : public User {
@@ -196,7 +285,40 @@ class BinaryInst : public Instruction {
BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs, Value* rhs,
std::string name);
Value* GetLhs() const;
Value* GetRhs() const;
Value* GetRhs() const;
};
class BranchInst : public Instruction {
public:
// Unconditional branch
explicit BranchInst(BasicBlock* dest);
// Conditional branch
BranchInst(Value* cond, BasicBlock* if_true, BasicBlock* if_false);
bool IsConditional() const;
Value* GetCondition() const;
BasicBlock* GetIfTrue() const;
BasicBlock* GetIfFalse() const;
BasicBlock* GetDest() const;
};
class CallInst : public Instruction {
public:
CallInst(Function* func, const std::vector<Value*>& args, std::string name = "");
Function* GetFunction() const;
};
class GetElementPtrInst : public Instruction {
public:
GetElementPtrInst(std::shared_ptr<Type> ptr_ty, Value* ptr,
const std::vector<Value*>& indices, std::string name = "");
Value* GetPtr() const;
};
class CastInst : public Instruction {
public:
CastInst(Opcode op, std::shared_ptr<Type> ty, Value* val, std::string name = "");
Value* GetValue() const;
};
class ReturnInst : public Instruction {
@@ -255,38 +377,41 @@ class BasicBlock : public Value {
};
// Function 当前也采用了最小实现。
// 需要特别注意:由于项目里还没有单独的 FunctionType
// Function 继承自 Value 后,其 type_ 目前只保存“返回类型”,
// 并不能完整表达“返回类型 + 形参列表”这一整套函数签名。
// 这对当前只支持 int main() 的最小 IR 足够,但后续若补普通函数、
// 形参和调用,通常需要引入专门的函数类型表示。
class Function : public Value {
public:
// 当前构造函数接收的也是返回类型,而不是完整函数类型。
Function(std::string name, std::shared_ptr<Type> ret_type);
Function(std::string name, std::shared_ptr<Type> ret_type,
std::vector<std::shared_ptr<Type>> param_types);
BasicBlock* CreateBlock(const std::string& name);
BasicBlock* GetEntry();
const BasicBlock* GetEntry() const;
const std::vector<std::unique_ptr<BasicBlock>>& GetBlocks() const;
const std::vector<std::unique_ptr<Argument>>& GetArguments() const;
private:
BasicBlock* entry_ = nullptr;
std::vector<std::unique_ptr<BasicBlock>> blocks_;
std::vector<std::unique_ptr<Argument>> arguments_;
};
class Module {
public:
Module() = default;
Context& GetContext();
const Context& GetContext() const;
// 创建函数时当前只显式传入返回类型,尚未接入完整的 FunctionType。
Function* CreateFunction(const std::string& name,
std::shared_ptr<Type> ret_type);
std::shared_ptr<Type> ret_type,
std::vector<std::shared_ptr<Type>> param_types = {});
const std::vector<std::unique_ptr<Function>>& GetFunctions() const;
GlobalValue* CreateGlobalValue(const std::string& name,
std::shared_ptr<Type> ty,
ConstantValue* init = nullptr);
const std::vector<std::unique_ptr<GlobalValue>>& GetGlobalValues() const;
private:
Context context_;
std::vector<std::unique_ptr<Function>> functions_;
std::vector<std::unique_ptr<GlobalValue>> global_values_;
};
class IRBuilder {
@@ -297,13 +422,41 @@ class IRBuilder {
// 构造常量、二元运算、返回指令的最小集合。
ConstantInt* CreateConstInt(int v);
ConstantFloat* CreateConstFloat(float v);
BinaryInst* CreateBinary(Opcode op, Value* lhs, Value* rhs,
const std::string& name);
BinaryInst* CreateAdd(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateSub(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateMul(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateDiv(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateMod(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateFAdd(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateFSub(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateFMul(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateFDiv(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateICmp(Opcode op, Value* lhs, Value* rhs,
const std::string& name);
BinaryInst* CreateFCmp(Opcode op, Value* lhs, Value* rhs,
const std::string& name);
AllocaInst* CreateAlloca(std::shared_ptr<Type> ty, const std::string& name);
AllocaInst* CreateAllocaI32(const std::string& name);
LoadInst* CreateLoad(Value* ptr, const std::string& name);
StoreInst* CreateStore(Value* val, Value* ptr);
ReturnInst* CreateRet(Value* v);
BranchInst* CreateBr(BasicBlock* dest);
BranchInst* CreateCondBr(Value* cond, BasicBlock* if_true,
BasicBlock* if_false);
CallInst* CreateCall(Function* func, const std::vector<Value*>& args,
const std::string& name = "");
GetElementPtrInst* CreateGEP(std::shared_ptr<Type> ptr_ty, Value* ptr,
const std::vector<Value*>& indices,
const std::string& name = "");
CastInst* CreateZExt(Value* val, std::shared_ptr<Type> ty,
const std::string& name = "");
CastInst* CreateSIToFP(Value* val, std::shared_ptr<Type> ty,
const std::string& name = "");
CastInst* CreateFPToSI(Value* val, std::shared_ptr<Type> ty,
const std::string& name = "");
private:
Context& ctx_;