@ -37,6 +37,7 @@
# include <unordered_map>
# include <unordered_map>
# include <utility>
# include <utility>
# include <vector>
# include <vector>
# include <variant>
namespace ir {
namespace ir {
@ -45,17 +46,15 @@ class Value;
class User ;
class User ;
class ConstantValue ;
class ConstantValue ;
class ConstantInt ;
class ConstantInt ;
class ConstantFloat ;
class ConstantArray ;
class GlobalValue ;
class GlobalValue ;
class Instruction ;
class Instruction ;
class BasicBlock ;
class BasicBlock ;
class Function ;
class Function ;
class Module ;
// Use 表示一个 Value 的一次使用记录。
// ======================== Use 类 ========================
// 当前实现设计:
// - value: 被使用的值
// - user: 使用该值的 User
// - operand_index: 该值在 user 操作数列表中的位置
class Use {
class Use {
public :
public :
Use ( ) = default ;
Use ( ) = default ;
@ -76,54 +75,130 @@ class Use {
size_t operand_index_ = 0 ;
size_t operand_index_ = 0 ;
} ;
} ;
// IR 上下文:集中管理类型、常量等共享资源,便于复用与扩展。
// ======================== Context 类 ========================
class Context {
class Context {
public :
public :
Context ( ) = default ;
Context ( ) = default ;
~ Context ( ) ;
~ Context ( ) ;
// 去重创建 i32 常量。
// 常量创建
ConstantInt * GetConstInt ( int v ) ;
ConstantInt * GetConstInt ( int v ) ;
ConstantFloat * GetConstFloat ( float v ) ;
// 临时变量名生成
std : : string NextTemp ( ) ;
std : : string NextTemp ( ) ;
private :
private :
std : : unordered_map < int , std : : unique_ptr < ConstantInt > > const_ints_ ;
std : : unordered_map < int , std : : unique_ptr < ConstantInt > > const_ints_ ;
std : : unordered_map < float , std : : unique_ptr < ConstantFloat > > const_floats_ ;
int temp_index_ = - 1 ;
int temp_index_ = - 1 ;
} ;
} ;
// ======================== Type 类型体系 ========================
// 类型基类,支持参数化类型
class Type {
class Type {
public :
public :
enum class Kind { Void , Int32 , PtrInt32 } ;
enum class Kind {
Void ,
Int32 ,
Float32 ,
Pointer ,
Array ,
Function ,
Label
} ;
explicit Type ( Kind k ) ;
explicit Type ( Kind k ) ;
// 使用静态共享对象获取类型。
virtual ~ Type ( ) = default ;
// 同一类型可直接比较返回值是否相等,例如:
// Type::GetInt32Type() == Type::GetInt32Type()
static const std : : shared_ptr < Type > & GetVoidType ( ) ;
static const std : : shared_ptr < Type > & GetInt32Type ( ) ;
static const std : : shared_ptr < Type > & GetPtrInt32Type ( ) ;
Kind GetKind ( ) const ;
Kind GetKind ( ) const ;
bool IsVoid ( ) const ;
bool IsVoid ( ) const ;
bool IsInt32 ( ) const ;
bool IsInt32 ( ) const ;
bool IsPtrInt32 ( ) const ;
bool IsFloat32 ( ) const ;
bool IsPointer ( ) const ;
bool IsArray ( ) const ;
bool IsFunction ( ) const ;
bool IsLabel ( ) const ;
bool IsPtrInt32 ( ) const ; // 兼容旧接口
bool IsPtrFloat32 ( ) const ; // 判断是否为 float32*
bool operator = = ( const Type & other ) const ;
bool operator ! = ( const Type & other ) const ;
// 静态单例获取基础类型
static const std : : shared_ptr < Type > & GetVoidType ( ) ;
static const std : : shared_ptr < Type > & GetInt32Type ( ) ;
static const std : : shared_ptr < Type > & GetFloat32Type ( ) ;
static const std : : shared_ptr < Type > & GetLabelType ( ) ;
static const std : : shared_ptr < Type > & GetPtrInt32Type ( ) ;
// 复合类型工厂方法
static std : : shared_ptr < Type > GetPointerType ( std : : shared_ptr < Type > pointee ) ;
static std : : shared_ptr < Type > GetArrayType ( std : : shared_ptr < Type > elem , size_t size ) ;
static std : : shared_ptr < Type > GetFunctionType ( std : : shared_ptr < Type > ret ,
std : : vector < std : : shared_ptr < Type > > params ) ;
private :
private :
Kind kind_ ;
Kind kind_ ;
} ;
} ;
// 指针类型
class PointerType : public Type {
public :
PointerType ( std : : shared_ptr < Type > pointee )
: Type ( Type : : Kind : : Pointer ) , pointee_ ( std : : move ( pointee ) ) { }
const std : : shared_ptr < Type > & GetPointeeType ( ) const { return pointee_ ; }
private :
std : : shared_ptr < Type > pointee_ ;
} ;
// 数组类型
class ArrayType : public Type {
public :
ArrayType ( std : : shared_ptr < Type > elem , size_t size )
: Type ( Type : : Kind : : Array ) , elem_type_ ( std : : move ( elem ) ) , size_ ( size ) { }
const std : : shared_ptr < Type > & GetElementType ( ) const { return elem_type_ ; }
size_t GetSize ( ) const { return size_ ; }
private :
std : : shared_ptr < Type > elem_type_ ;
size_t size_ ;
} ;
// 函数类型
class FunctionType : public Type {
public :
FunctionType ( std : : shared_ptr < Type > ret , std : : vector < std : : shared_ptr < Type > > params )
: Type ( Type : : Kind : : Function ) , ret_type_ ( std : : move ( ret ) ) , param_types_ ( std : : move ( params ) ) { }
const std : : shared_ptr < Type > & GetReturnType ( ) const { return ret_type_ ; }
const std : : vector < std : : shared_ptr < Type > > & GetParamTypes ( ) const { return param_types_ ; }
private :
std : : shared_ptr < Type > ret_type_ ;
std : : vector < std : : shared_ptr < Type > > param_types_ ;
} ;
// ======================== Value 类 ========================
class Value {
class Value {
public :
public :
Value ( std : : shared_ptr < Type > ty , std : : string name ) ;
Value ( std : : shared_ptr < Type > ty , std : : string name ) ;
virtual ~ Value ( ) = default ;
virtual ~ Value ( ) = default ;
const std : : shared_ptr < Type > & GetType ( ) const ;
const std : : shared_ptr < Type > & GetType ( ) const ;
const std : : string & GetName ( ) const ;
const std : : string & GetName ( ) const ;
void SetName ( std : : string n ) ;
void SetName ( std : : string n ) ;
bool IsVoid ( ) const ;
bool IsVoid ( ) const ;
bool IsInt32 ( ) const ;
bool IsInt32 ( ) const ;
bool IsPtrInt32 ( ) const ;
bool IsFloat32 ( ) const ;
bool IsPtrInt32 ( ) const ; // 兼容旧接口,实际上判断是否为 i32*
bool IsPtrFloat32 ( ) const ; // 判断是否为 float32*
bool IsConstant ( ) const ;
bool IsConstant ( ) const ;
bool IsInstruction ( ) const ;
bool IsInstruction ( ) const ;
bool IsUser ( ) const ;
bool IsUser ( ) const ;
bool IsFunction ( ) const ;
bool IsFunction ( ) const ;
bool IsGlobalValue ( ) const ;
void AddUse ( User * user , size_t operand_index ) ;
void AddUse ( User * user , size_t operand_index ) ;
void RemoveUse ( User * user , size_t operand_index ) ;
void RemoveUse ( User * user , size_t operand_index ) ;
const std : : vector < Use > & GetUses ( ) const ;
const std : : vector < Use > & GetUses ( ) const ;
@ -135,8 +210,7 @@ class Value {
std : : vector < Use > uses_ ;
std : : vector < Use > uses_ ;
} ;
} ;
// ConstantValue 是常量体系的基类。
// ======================== 常量体系 ========================
// 当前只实现了 ConstantInt, 后续可继续扩展更多常量种类。
class ConstantValue : public Value {
class ConstantValue : public Value {
public :
public :
ConstantValue ( std : : shared_ptr < Type > ty , std : : string name = " " ) ;
ConstantValue ( std : : shared_ptr < Type > ty , std : : string name = " " ) ;
@ -151,11 +225,26 @@ class ConstantInt : public ConstantValue {
int value_ { } ;
int value_ { } ;
} ;
} ;
// 后续还需要扩展更多指令类型。
class ConstantFloat : public ConstantValue {
enum class Opcode { Add , Sub , Mul , Alloca , Load , Store , Ret } ;
public :
ConstantFloat ( std : : shared_ptr < Type > ty , float v ) ;
float GetValue ( ) const { return value_ ; }
private :
float value_ { } ;
} ;
// User 是所有“会使用其他 Value 作为输入”的 IR 对象的抽象基类。
// 常量数组(简单聚合,可存储常量元素)
// 当前实现中只有 Instruction 继承自 User。
class ConstantArray : public ConstantValue {
public :
ConstantArray ( std : : shared_ptr < Type > ty , std : : vector < ConstantValue * > elems ) ;
const std : : vector < ConstantValue * > & GetElements ( ) const { return elements_ ; }
private :
std : : vector < ConstantValue * > elements_ ;
} ;
// ======================== User 类 ========================
class User : public Value {
class User : public Value {
public :
public :
User ( std : : shared_ptr < Type > ty , std : : string name ) ;
User ( std : : shared_ptr < Type > ty , std : : string name ) ;
@ -164,20 +253,44 @@ class User : public Value {
void SetOperand ( size_t index , Value * value ) ;
void SetOperand ( size_t index , Value * value ) ;
protected :
protected :
// 统一的 operand 入口。
void AddOperand ( Value * value ) ;
void AddOperand ( Value * value ) ;
private :
private :
std : : vector < Value * > operands_ ;
std : : vector < Value * > operands_ ;
} ;
} ;
// GlobalValue 是全局值/全局变量体系的空壳占位类。
// ======================== GlobalValue 类 ========================
// 当前只补齐类层次,具体初始化器、打印和链接语义后续再补。
class GlobalValue : public User {
class GlobalValue : public User {
public :
public :
GlobalValue ( std : : shared_ptr < Type > ty , std : : string name ) ;
GlobalValue ( std : : shared_ptr < Type > ty , std : : string name ) ;
ConstantValue * GetInitializer ( ) const { return init_ ; }
void SetInitializer ( ConstantValue * init ) { init_ = init ; }
private :
ConstantValue * init_ = nullptr ;
} ;
// ======================== 指令操作码 ========================
enum class Opcode {
// 算术
Add , Sub , Mul , Div , Mod ,
// 位运算
And , Or , Xor , Shl , LShr , AShr ,
// 比较
ICmp , FCmp ,
// 内存
Alloca , Load , Store ,
// 控制流
Ret , Br , CondBr ,
// 函数调用
Call ,
// 数组访问
GEP ,
// Phi
Phi
} ;
} ;
// ======================== Instruction 类 ========================
class Instruction : public User {
class Instruction : public User {
public :
public :
Instruction ( Opcode op , std : : shared_ptr < Type > ty , std : : string name = " " ) ;
Instruction ( Opcode op , std : : shared_ptr < Type > ty , std : : string name = " " ) ;
@ -191,31 +304,94 @@ class Instruction : public User {
BasicBlock * parent_ = nullptr ;
BasicBlock * parent_ = nullptr ;
} ;
} ;
// 二元运算指令
class BinaryInst : public Instruction {
class BinaryInst : public Instruction {
public :
public :
BinaryInst ( Opcode op , std : : shared_ptr < Type > ty , Value * lhs , Value * rhs ,
BinaryInst ( Opcode op , std : : shared_ptr < Type > ty , Value * lhs , Value * rhs ,
std : : string name ) ;
std : : string name ) ;
Value * GetLhs ( ) const ;
Value * GetLhs ( ) const ;
Value * GetRhs ( ) const ;
Value * GetRhs ( ) const ;
} ;
// 比较指令( icmp/fcmp)
class CmpInst : public Instruction {
public :
enum Predicate {
EQ , NE , LT , LE , GT , GE
} ;
CmpInst ( Opcode op , Predicate pred , Value * lhs , Value * rhs , std : : string name ) ;
Predicate GetPredicate ( ) const { return pred_ ; }
Value * GetLhs ( ) const { return lhs_ ; }
Value * GetRhs ( ) const { return rhs_ ; }
private :
Predicate pred_ ;
Value * lhs_ ;
Value * rhs_ ;
} ;
} ;
// 返回指令
class ReturnInst : public Instruction {
class ReturnInst : public Instruction {
public :
public :
ReturnInst ( std : : shared_ptr < Type > void_ty , Value * val ) ;
ReturnInst ( std : : shared_ptr < Type > void_ty , Value * val = nullptr ) ;
Value * GetValue ( ) const ;
Value * GetValue ( ) const ;
} ;
} ;
// 无条件分支
class BranchInst : public Instruction {
public :
BranchInst ( BasicBlock * target ) ;
BasicBlock * GetTarget ( ) const ;
} ;
// 条件分支
class CondBranchInst : public Instruction {
public :
CondBranchInst ( Value * cond , BasicBlock * true_bb , BasicBlock * false_bb ) ;
Value * GetCond ( ) const ;
BasicBlock * GetTrueBlock ( ) const ;
BasicBlock * GetFalseBlock ( ) const ;
} ;
// 函数调用
class CallInst : public Instruction {
public :
CallInst ( Function * callee , std : : vector < Value * > args , std : : string name ) ;
Function * GetCallee ( ) const ;
const std : : vector < Value * > & GetArgs ( ) const ;
} ;
// Phi 指令(用于 SSA)
class PhiInst : public Instruction {
public :
PhiInst ( std : : shared_ptr < Type > ty , std : : string name ) ;
void AddIncoming ( Value * val , BasicBlock * block ) ;
const std : : vector < std : : pair < Value * , BasicBlock * > > & GetIncomings ( ) const ;
} ;
// GetElementPtr 指令(数组/结构体指针计算)
class GetElementPtrInst : public Instruction {
public :
GetElementPtrInst ( std : : shared_ptr < Type > ty , Value * ptr ,
std : : vector < Value * > indices , std : : string name ) ;
Value * GetPtr ( ) const ;
const std : : vector < Value * > & GetIndices ( ) const ;
} ;
// 分配栈内存指令
class AllocaInst : public Instruction {
class AllocaInst : public Instruction {
public :
public :
AllocaInst ( std : : shared_ptr < Type > ptr_ty , std : : string name ) ;
AllocaInst ( std : : shared_ptr < Type > ptr_ty , std : : string name ) ;
} ;
} ;
// 加载指令
class LoadInst : public Instruction {
class LoadInst : public Instruction {
public :
public :
LoadInst ( std : : shared_ptr < Type > val_ty , Value * ptr , std : : string name ) ;
LoadInst ( std : : shared_ptr < Type > val_ty , Value * ptr , std : : string name ) ;
Value * GetPtr ( ) const ;
Value * GetPtr ( ) const ;
} ;
} ;
// 存储指令
class StoreInst : public Instruction {
class StoreInst : public Instruction {
public :
public :
StoreInst ( std : : shared_ptr < Type > void_ty , Value * val , Value * ptr ) ;
StoreInst ( std : : shared_ptr < Type > void_ty , Value * val , Value * ptr ) ;
@ -223,8 +399,7 @@ class StoreInst : public Instruction {
Value * GetPtr ( ) const ;
Value * GetPtr ( ) const ;
} ;
} ;
// BasicBlock 已纳入 Value 体系,便于后续向更完整 IR 类图靠拢。
// ======================== BasicBlock 类 ========================
// 当前其类型仍使用 void 作为占位,后续可替换为专门的 label type。
class BasicBlock : public Value {
class BasicBlock : public Value {
public :
public :
explicit BasicBlock ( std : : string name ) ;
explicit BasicBlock ( std : : string name ) ;
@ -234,6 +409,11 @@ class BasicBlock : public Value {
const std : : vector < std : : unique_ptr < Instruction > > & GetInstructions ( ) const ;
const std : : vector < std : : unique_ptr < Instruction > > & GetInstructions ( ) const ;
const std : : vector < BasicBlock * > & GetPredecessors ( ) const ;
const std : : vector < BasicBlock * > & GetPredecessors ( ) const ;
const std : : vector < BasicBlock * > & GetSuccessors ( ) const ;
const std : : vector < BasicBlock * > & GetSuccessors ( ) const ;
void AddPredecessor ( BasicBlock * pred ) ;
void AddSuccessor ( BasicBlock * succ ) ;
void RemovePredecessor ( BasicBlock * pred ) ;
void RemoveSuccessor ( BasicBlock * succ ) ;
template < typename T , typename . . . Args >
template < typename T , typename . . . Args >
T * Append ( Args & & . . . args ) {
T * Append ( Args & & . . . args ) {
if ( HasTerminator ( ) ) {
if ( HasTerminator ( ) ) {
@ -254,65 +434,119 @@ class BasicBlock : public Value {
std : : vector < BasicBlock * > successors_ ;
std : : vector < BasicBlock * > successors_ ;
} ;
} ;
// Function 当前也采用了最小实现。
// ======================== Function 类 ========================
// 需要特别注意:由于项目里还没有单独的 FunctionType,
// Function 继承自 Value 后,其 type_ 目前只保存“返回类型”,
// 并不能完整表达“返回类型 + 形参列表”这一整套函数签名。
// 这对当前只支持 int main() 的最小 IR 足够,但后续若补普通函数、
// 形参和调用,通常需要引入专门的函数类型表示。
class Function : public Value {
class Function : public Value {
public :
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 * CreateBlock ( const std : : string & name ) ;
BasicBlock * GetEntry ( ) ;
BasicBlock * GetEntry ( ) ;
const BasicBlock * GetEntry ( ) const ;
const BasicBlock * GetEntry ( ) const ;
const std : : vector < std : : unique_ptr < BasicBlock > > & GetBlocks ( ) const ;
const std : : vector < std : : unique_ptr < BasicBlock > > & GetBlocks ( ) const ;
// 参数管理
const std : : vector < Value * > & GetParams ( ) const { return params_ ; }
void AddParam ( Value * param ) ;
// 函数类型(完整签名)
std : : shared_ptr < FunctionType > GetFunctionType ( ) const ;
private :
private :
BasicBlock * entry_ = nullptr ;
BasicBlock * entry_ = nullptr ;
std : : vector < std : : unique_ptr < BasicBlock > > blocks_ ;
std : : vector < std : : unique_ptr < BasicBlock > > blocks_ ;
std : : vector < Value * > params_ ; // 参数值(通常是 Argument 类型,后续可定义)
// Owned parameter storage to keep argument Values alive
std : : vector < std : : unique_ptr < Value > > owned_params_ ;
std : : shared_ptr < FunctionType > func_type_ ; // 缓存函数类型
} ;
} ;
// ======================== Module 类 ========================
class Module {
class Module {
public :
public :
Module ( ) = default ;
Module ( ) = default ;
Context & GetContext ( ) ;
Context & GetContext ( ) ;
const Context & GetContext ( ) const ;
const Context & GetContext ( ) const ;
// 创建函数时当前只显式传入返回类型,尚未接入完整的 FunctionType。
// 创建函数,支持参数类型
Function * CreateFunction ( const std : : string & name ,
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 = { } ) ;
// 创建全局变量
GlobalValue * CreateGlobalVariable ( const std : : string & name ,
std : : shared_ptr < Type > ty ,
ConstantValue * init = nullptr ) ;
const std : : vector < std : : unique_ptr < Function > > & GetFunctions ( ) const ;
const std : : vector < std : : unique_ptr < Function > > & GetFunctions ( ) const ;
const std : : vector < std : : unique_ptr < GlobalValue > > & GetGlobalVariables ( ) const ;
private :
private :
Context context_ ;
Context context_ ;
std : : vector < std : : unique_ptr < Function > > functions_ ;
std : : vector < std : : unique_ptr < Function > > functions_ ;
std : : vector < std : : unique_ptr < GlobalValue > > global_vars_ ;
} ;
} ;
// ======================== IRBuilder 类 ========================
class IRBuilder {
class IRBuilder {
public :
public :
IRBuilder ( Context & ctx , BasicBlock * bb ) ;
IRBuilder ( Context & ctx , BasicBlock * bb = nullptr ) ;
void SetInsertPoint ( BasicBlock * bb ) ;
void SetInsertPoint ( BasicBlock * bb ) ;
BasicBlock * GetInsertBlock ( ) const ;
BasicBlock * GetInsertBlock ( ) const ;
// 构造常量、二元运算、返回指令的最小集合。
// 常量创建
ConstantInt * CreateConstInt ( int v ) ;
ConstantInt * CreateConstInt ( int v ) ;
ConstantFloat * CreateConstFloat ( float v ) ;
// 算术指令
BinaryInst * CreateBinary ( Opcode op , Value * lhs , Value * rhs ,
BinaryInst * CreateBinary ( Opcode op , Value * lhs , Value * rhs ,
const std : : string & name ) ;
const std : : string & name ) ;
BinaryInst * CreateAdd ( 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 ) ;
// 比较指令
CmpInst * CreateICmp ( CmpInst : : Predicate pred , Value * lhs , Value * rhs ,
const std : : string & name ) ;
CmpInst * CreateFCmp ( CmpInst : : Predicate pred , Value * lhs , Value * rhs ,
const std : : string & name ) ;
// 内存指令
AllocaInst * CreateAllocaI32 ( const std : : string & name ) ;
AllocaInst * CreateAllocaI32 ( const std : : string & name ) ;
AllocaInst * CreateAllocaFloat ( const std : : string & name ) ;
AllocaInst * CreateAlloca ( std : : shared_ptr < Type > ty , const std : : string & name ) ;
LoadInst * CreateLoad ( Value * ptr , const std : : string & name ) ;
LoadInst * CreateLoad ( Value * ptr , const std : : string & name ) ;
StoreInst * CreateStore ( Value * val , Value * ptr ) ;
StoreInst * CreateStore ( Value * val , Value * ptr ) ;
// 控制流指令
ReturnInst * CreateRet ( Value * v ) ;
ReturnInst * CreateRet ( Value * v ) ;
BranchInst * CreateBr ( BasicBlock * target ) ;
CondBranchInst * CreateCondBr ( Value * cond , BasicBlock * true_bb ,
BasicBlock * false_bb ) ;
// 函数调用
CallInst * CreateCall ( Function * callee , std : : vector < Value * > args ,
const std : : string & name ) ;
// 数组访问
GetElementPtrInst * CreateGEP ( Value * ptr , std : : vector < Value * > indices ,
const std : : string & name ) ;
// Phi 指令
PhiInst * CreatePhi ( std : : shared_ptr < Type > ty , const std : : string & name ) ;
private :
private :
Context & ctx_ ;
Context & ctx_ ;
BasicBlock * insert_block_ ;
BasicBlock * insert_block_ ;
} ;
} ;
// ======================== IRPrinter 类 ========================
class IRPrinter {
class IRPrinter {
public :
public :
void Print ( const Module & module , std : : ostream & os ) ;
void Print ( const Module & module , std : : ostream & os ) ;
} ;
} ;
} // namespace ir
} // namespace ir