fix(ir): 规范ir实现

This commit is contained in:
jing
2026-03-11 00:38:05 +08:00
parent c72ffa6e39
commit 8f448df4bc
8 changed files with 132 additions and 22 deletions

View File

@@ -12,6 +12,10 @@ bool IsArithmeticType(const std::shared_ptr<Type>& ty) {
return ty && ty->kind() == Type::Kind::Int32;
}
bool IsPtrInt32Type(const std::shared_ptr<Type>& ty) {
return ty && ty->kind() == Type::Kind::PtrInt32;
}
} // namespace
ConstantInt* IRBuilder::CreateConstInt(int v) {
@@ -54,6 +58,12 @@ LoadInst* IRBuilder::CreateLoad(Value* ptr, const std::string& name) {
if (!insertBlock_) {
throw std::runtime_error("IRBuilder 未设置插入点");
}
if (!ptr) {
throw std::runtime_error("IRBuilder::CreateLoad 缺少 ptr");
}
if (!IsPtrInt32Type(ptr->type())) {
throw std::runtime_error("IRBuilder::CreateLoad 当前只支持从 i32* 加载");
}
return insertBlock_->Append<LoadInst>(ptr, name);
}
@@ -61,6 +71,18 @@ StoreInst* IRBuilder::CreateStore(Value* val, Value* ptr) {
if (!insertBlock_) {
throw std::runtime_error("IRBuilder 未设置插入点");
}
if (!val) {
throw std::runtime_error("IRBuilder::CreateStore 缺少 val");
}
if (!ptr) {
throw std::runtime_error("IRBuilder::CreateStore 缺少 ptr");
}
if (!IsArithmeticType(val->type())) {
throw std::runtime_error("IRBuilder::CreateStore 当前只支持存储 i32");
}
if (!IsPtrInt32Type(ptr->type())) {
throw std::runtime_error("IRBuilder::CreateStore 当前只支持写入 i32*");
}
return insertBlock_->Append<StoreInst>(val, ptr);
}
@@ -68,6 +90,9 @@ ReturnInst* IRBuilder::CreateRet(Value* v) {
if (!insertBlock_) {
throw std::runtime_error("IRBuilder 未设置插入点");
}
if (!v) {
throw std::runtime_error("IRBuilder::CreateRet 缺少返回值");
}
return insertBlock_->Append<ReturnInst>(v);
}