feat(sem)补充类型系统Label类型

mxr 3 weeks ago
parent c21c066e4d
commit c450277c83

@ -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<Type>& GetFloatType();
static const std::shared_ptr<Type>& GetPtrInt32Type();
static const std::shared_ptr<Type>& GetPtrFloatType();
static const std::shared_ptr<Type>& GetLabelType();
static std::shared_ptr<ArrayType> GetArrayType(std::shared_ptr<Type> elem, std::vector<int> dims);
static std::shared_ptr<FunctionType> GetFunctionType(std::shared_ptr<Type> ret, std::vector<std::shared_ptr<Type>> 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; }

@ -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";

@ -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>& Type::GetVoidType() {
@ -56,6 +57,11 @@ const std::shared_ptr<Type>& Type::GetPtrFloatType() {
return type;
}
const std::shared_ptr<Type>& Type::GetLabelType() {
static const std::shared_ptr<Type> type(new Type(Kind::Label));
return type;
}
// ---------- 数组类型缓存 ----------
// 使用自定义键类型保证唯一性:元素类型指针 + 维度向量
struct ArrayKey {

Loading…
Cancel
Save