fix(dev): 统一 IR/MIR 异常前缀

This commit is contained in:
Lane0218
2026-03-12 15:01:19 +08:00
parent f3d0102376
commit a7779038ca
6 changed files with 53 additions and 32 deletions

View File

@@ -3,8 +3,11 @@
// - 提供创建各类指令的便捷接口,降低 IRGen 复杂度
#include "ir/IR.h"
#include <stdexcept>
#include "utils/Log.h"
namespace ir {
IRBuilder::IRBuilder(Context& ctx, BasicBlock* bb)
: ctx_(ctx), insertBlock_(bb) {}
@@ -21,13 +24,15 @@ ConstantInt* IRBuilder::CreateConstInt(int v) {
BinaryInst* IRBuilder::CreateBinary(Opcode op, Value* lhs, Value* rhs,
const std::string& name) {
if (!insertBlock_) {
throw std::runtime_error("IRBuilder 未设置插入点");
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
if (!lhs) {
throw std::runtime_error("IRBuilder::CreateBinary 缺少 lhs");
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateBinary 缺少 lhs"));
}
if (!rhs) {
throw std::runtime_error("IRBuilder::CreateBinary 缺少 rhs");
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateBinary 缺少 rhs"));
}
return insertBlock_->Append<BinaryInst>(op, lhs->type(), lhs, rhs, name);
}
@@ -39,40 +44,44 @@ BinaryInst* IRBuilder::CreateAdd(Value* lhs, Value* rhs,
AllocaInst* IRBuilder::CreateAllocaI32(const std::string& name) {
if (!insertBlock_) {
throw std::runtime_error("IRBuilder 未设置插入点");
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
return insertBlock_->Append<AllocaInst>(ctx_.PtrInt32(), name);
}
LoadInst* IRBuilder::CreateLoad(Value* ptr, const std::string& name) {
if (!insertBlock_) {
throw std::runtime_error("IRBuilder 未设置插入点");
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
if (!ptr) {
throw std::runtime_error("IRBuilder::CreateLoad 缺少 ptr");
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateLoad 缺少 ptr"));
}
return insertBlock_->Append<LoadInst>(ctx_.Int32(), ptr, name);
}
StoreInst* IRBuilder::CreateStore(Value* val, Value* ptr) {
if (!insertBlock_) {
throw std::runtime_error("IRBuilder 未设置插入点");
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
if (!val) {
throw std::runtime_error("IRBuilder::CreateStore 缺少 val");
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateStore 缺少 val"));
}
if (!ptr) {
throw std::runtime_error("IRBuilder::CreateStore 缺少 ptr");
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateStore 缺少 ptr"));
}
return insertBlock_->Append<StoreInst>(ctx_.Void(), val, ptr);
}
ReturnInst* IRBuilder::CreateRet(Value* v) {
if (!insertBlock_) {
throw std::runtime_error("IRBuilder 未设置插入点");
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
if (!v) {
throw std::runtime_error("IRBuilder::CreateRet 缺少返回值");
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateRet 缺少返回值"));
}
return insertBlock_->Append<ReturnInst>(ctx_.Void(), v);
}