feat(sem)补充类型系统浮点类型

mxr 3 weeks ago
parent 2070193fcd
commit c21c066e4d

@ -95,7 +95,7 @@ class Context {
class Type {
public:
enum class Kind { Void, Int32, PtrInt32, Array, Function };
enum class Kind { Void, Int32, Float, PtrInt32, PtrFloat, Array, Function };
virtual ~Type() = default;
@ -104,7 +104,9 @@ class Type {
// Type::GetInt32Type() == Type::GetInt32Type()
static const std::shared_ptr<Type>& GetVoidType();
static const std::shared_ptr<Type>& GetInt32Type();
static const std::shared_ptr<Type>& GetFloatType();
static const std::shared_ptr<Type>& GetPtrInt32Type();
static const std::shared_ptr<Type>& GetPtrFloatType();
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);
@ -112,7 +114,9 @@ class Type {
Kind GetKind() const { return kind_; }
bool IsVoid() const { return kind_ == Kind::Void; }
bool IsInt32() const { return kind_ == Kind::Int32; }
bool IsFloat() const { return kind_ == Kind::Float; }
bool IsPtrInt32() const { return kind_ == Kind::PtrInt32; }
bool IsPtrFloat() const { return kind_ == Kind::PtrFloat; }
bool IsArray() const { return kind_ == Kind::Array; }
bool IsFunction() const { return kind_ == Kind::Function; }

@ -14,12 +14,14 @@ namespace ir {
static const char* TypeToString(const Type& ty) {
switch (ty.GetKind()) {
case Type::Kind::Void:
return "void";
case Type::Kind::Int32:
return "i32";
case Type::Kind::PtrInt32:
return "i32*";
case Type::Kind::Void: return "void";
case Type::Kind::Int32: return "i32";
case Type::Kind::Float: return "float";
case Type::Kind::PtrInt32: return "i32*";
case Type::Kind::PtrFloat: return "float*";
case Type::Kind::Array: return "array";
case Type::Kind::Function: return "function";
default: return "unknown";
}
throw std::runtime_error(FormatError("ir", "未知类型"));
}

@ -10,21 +10,25 @@ size_t Type::Size() const {
switch (kind_) {
case Kind::Void: return 0;
case Kind::Int32: return 4;
case Kind::PtrInt32: return 8; // 假设 64 位平台
default: return 0; // 派生类应重写
case Kind::Float: return 4; // 单精度浮点 4 字节
case Kind::PtrInt32: return 8; // 假设 64 位指针
case Kind::PtrFloat: return 8;
default: return 0; // 派生类应重写
}
}
size_t Type::Alignment() const {
switch (kind_) {
case Kind::Int32: return 4;
case Kind::Float: return 4;
case Kind::PtrInt32: return 8;
default: return 1; // void 和复合类型由派生类处理
case Kind::PtrFloat: return 8;
default: return 1;
}
}
bool Type::IsComplete() const {
// void 视为完整类型(不能有对象int 和指针都是完整类型
// void 视为完整类型(不能有对象但可用于函数返回类型int 和指针都是完整类型
return kind_ != Kind::Void;
}
const std::shared_ptr<Type>& Type::GetVoidType() {
@ -37,11 +41,21 @@ const std::shared_ptr<Type>& Type::GetInt32Type() {
return type;
}
const std::shared_ptr<Type>& Type::GetFloatType() {
static const std::shared_ptr<Type> type(new Type(Kind::Float));
return type;
}
const std::shared_ptr<Type>& Type::GetPtrInt32Type() {
static const std::shared_ptr<Type> type = std::shared_ptr<Type>(new Type(Kind::PtrInt32));
return type;
}
const std::shared_ptr<Type>& Type::GetPtrFloatType() {
static const std::shared_ptr<Type> type(new Type(Kind::PtrFloat));
return type;
}
// ---------- 数组类型缓存 ----------
// 使用自定义键类型保证唯一性:元素类型指针 + 维度向量
struct ArrayKey {

Loading…
Cancel
Save