diff --git a/include/ir/IR.h b/include/ir/IR.h index c6eb019..4ea12dc 100644 --- a/include/ir/IR.h +++ b/include/ir/IR.h @@ -95,7 +95,7 @@ class Context { class Type { public: - enum class Kind { Void, Int32, Float, PtrInt32, PtrFloat, Array, Function }; + enum class Kind { Void, Int32, Float, PtrInt32, PtrFloat, Label, Array, Function }; virtual ~Type() = default; @@ -107,6 +107,7 @@ class Type { static const std::shared_ptr& GetFloatType(); static const std::shared_ptr& GetPtrInt32Type(); static const std::shared_ptr& GetPtrFloatType(); + static const std::shared_ptr& GetLabelType(); static std::shared_ptr GetArrayType(std::shared_ptr elem, std::vector dims); static std::shared_ptr GetFunctionType(std::shared_ptr ret, std::vector> params); @@ -117,6 +118,7 @@ class Type { bool IsFloat() const { return kind_ == Kind::Float; } bool IsPtrInt32() const { return kind_ == Kind::PtrInt32; } bool IsPtrFloat() const { return kind_ == Kind::PtrFloat; } + bool IsLabel() const { return kind_ == Kind::Label; } bool IsArray() const { return kind_ == Kind::Array; } bool IsFunction() const { return kind_ == Kind::Function; } diff --git a/src/ir/IRPrinter.cpp b/src/ir/IRPrinter.cpp index 86df39f..5b11d63 100644 --- a/src/ir/IRPrinter.cpp +++ b/src/ir/IRPrinter.cpp @@ -19,6 +19,7 @@ static const char* TypeToString(const Type& ty) { case Type::Kind::Float: return "float"; case Type::Kind::PtrInt32: return "i32*"; case Type::Kind::PtrFloat: return "float*"; + case Type::Kind::Label: return "label"; case Type::Kind::Array: return "array"; case Type::Kind::Function: return "function"; default: return "unknown"; diff --git a/src/ir/Type.cpp b/src/ir/Type.cpp index 6ec92ec..8d0f5b9 100644 --- a/src/ir/Type.cpp +++ b/src/ir/Type.cpp @@ -13,6 +13,7 @@ size_t Type::Size() const { case Kind::Float: return 4; // 单精度浮点 4 字节 case Kind::PtrInt32: return 8; // 假设 64 位指针 case Kind::PtrFloat: return 8; + case Kind::Label: return 8; // 标签地址大小(指针大小) default: return 0; // 派生类应重写 } } @@ -23,12 +24,12 @@ size_t Type::Alignment() const { case Kind::Float: return 4; case Kind::PtrInt32: return 8; case Kind::PtrFloat: return 8; + case Kind::Label: return 8; // 与指针相同 default: return 1; } } bool Type::IsComplete() const { - // void 视为完整类型(不能有对象但可用于函数返回类型),int 和指针都是完整类型 return kind_ != Kind::Void; } const std::shared_ptr& Type::GetVoidType() { @@ -56,6 +57,11 @@ const std::shared_ptr& Type::GetPtrFloatType() { return type; } +const std::shared_ptr& Type::GetLabelType() { + static const std::shared_ptr type(new Type(Kind::Label)); + return type; +} + // ---------- 数组类型缓存 ---------- // 使用自定义键类型保证唯一性:元素类型指针 + 维度向量 struct ArrayKey {