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

@@ -5,6 +5,8 @@
#include <stdexcept>
#include "utils/Log.h"
namespace ir {
Instruction::Instruction(Opcode op, std::shared_ptr<Type> ty, std::string name)
: Value(std::move(ty), std::move(name)), opcode_(op) {}
@@ -21,20 +23,20 @@ BinaryInst::BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs,
Value* rhs, std::string name)
: Instruction(op, std::move(ty), std::move(name)), lhs_(lhs), rhs_(rhs) {
if (op != Opcode::Add) {
throw std::runtime_error("BinaryInst 当前只支持 Add");
throw std::runtime_error(FormatError("ir", "BinaryInst 当前只支持 Add"));
}
if (!lhs_ || !rhs_) {
throw std::runtime_error("BinaryInst 缺少操作数");
throw std::runtime_error(FormatError("ir", "BinaryInst 缺少操作数"));
}
if (!type_ || !lhs_->type() || !rhs_->type()) {
throw std::runtime_error("BinaryInst 缺少类型信息");
throw std::runtime_error(FormatError("ir", "BinaryInst 缺少类型信息"));
}
if (lhs_->type()->kind() != rhs_->type()->kind() ||
type_->kind() != lhs_->type()->kind()) {
throw std::runtime_error("BinaryInst 类型不匹配");
throw std::runtime_error(FormatError("ir", "BinaryInst 类型不匹配"));
}
if (!type_->IsInt32()) {
throw std::runtime_error("BinaryInst 当前只支持 i32");
throw std::runtime_error(FormatError("ir", "BinaryInst 当前只支持 i32"));
}
if (lhs_) {
lhs_->AddUser(this);
@@ -52,10 +54,10 @@ ReturnInst::ReturnInst(std::shared_ptr<Type> void_ty, Value* val)
: Instruction(Opcode::Ret, std::move(void_ty), ""),
value_(val) {
if (!value_) {
throw std::runtime_error("ReturnInst 缺少返回值");
throw std::runtime_error(FormatError("ir", "ReturnInst 缺少返回值"));
}
if (!type_ || !type_->IsVoid()) {
throw std::runtime_error("ReturnInst 返回类型必须为 void");
throw std::runtime_error(FormatError("ir", "ReturnInst 返回类型必须为 void"));
}
value_->AddUser(this);
}
@@ -65,7 +67,7 @@ Value* ReturnInst::value() const { return value_; }
AllocaInst::AllocaInst(std::shared_ptr<Type> ptr_ty, std::string name)
: Instruction(Opcode::Alloca, std::move(ptr_ty), std::move(name)) {
if (!type_ || !type_->IsPtrInt32()) {
throw std::runtime_error("AllocaInst 当前只支持 i32*");
throw std::runtime_error(FormatError("ir", "AllocaInst 当前只支持 i32*"));
}
}
@@ -73,13 +75,14 @@ LoadInst::LoadInst(std::shared_ptr<Type> val_ty, Value* ptr, std::string name)
: Instruction(Opcode::Load, std::move(val_ty), std::move(name)),
ptr_(ptr) {
if (!ptr_) {
throw std::runtime_error("LoadInst 缺少 ptr");
throw std::runtime_error(FormatError("ir", "LoadInst 缺少 ptr"));
}
if (!type_ || !type_->IsInt32()) {
throw std::runtime_error("LoadInst 当前只支持加载 i32");
throw std::runtime_error(FormatError("ir", "LoadInst 当前只支持加载 i32"));
}
if (!ptr_->type() || !ptr_->type()->IsPtrInt32()) {
throw std::runtime_error("LoadInst 当前只支持从 i32* 加载");
throw std::runtime_error(
FormatError("ir", "LoadInst 当前只支持从 i32* 加载"));
}
ptr_->AddUser(this);
}
@@ -91,19 +94,20 @@ StoreInst::StoreInst(std::shared_ptr<Type> void_ty, Value* val, Value* ptr)
value_(val),
ptr_(ptr) {
if (!value_) {
throw std::runtime_error("StoreInst 缺少 value");
throw std::runtime_error(FormatError("ir", "StoreInst 缺少 value"));
}
if (!ptr_) {
throw std::runtime_error("StoreInst 缺少 ptr");
throw std::runtime_error(FormatError("ir", "StoreInst 缺少 ptr"));
}
if (!type_ || !type_->IsVoid()) {
throw std::runtime_error("StoreInst 返回类型必须为 void");
throw std::runtime_error(FormatError("ir", "StoreInst 返回类型必须为 void"));
}
if (!value_->type() || !value_->type()->IsInt32()) {
throw std::runtime_error("StoreInst 当前只支持存储 i32");
throw std::runtime_error(FormatError("ir", "StoreInst 当前只支持存储 i32"));
}
if (!ptr_->type() || !ptr_->type()->IsPtrInt32()) {
throw std::runtime_error("StoreInst 当前只支持写入 i32*");
throw std::runtime_error(
FormatError("ir", "StoreInst 当前只支持写入 i32*"));
}
value_->AddUser(this);
ptr_->AddUser(this);