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

View File

@@ -8,6 +8,8 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include "utils/Log.h"
namespace ir { namespace ir {
static const char* TypeToString(const Type& ty) { static const char* TypeToString(const Type& ty) {
@@ -19,7 +21,7 @@ static const char* TypeToString(const Type& ty) {
case Type::Kind::PtrInt32: case Type::Kind::PtrInt32:
return "i32*"; return "i32*";
} }
throw std::runtime_error("未知类型"); throw std::runtime_error(FormatError("ir", "未知类型"));
} }
static const char* OpcodeToString(Opcode op) { static const char* OpcodeToString(Opcode op) {

View File

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

View File

@@ -3,13 +3,15 @@
#include <ostream> #include <ostream>
#include <stdexcept> #include <stdexcept>
#include "utils/Log.h"
namespace mir { namespace mir {
namespace { namespace {
const FrameSlot& GetFrameSlot(const MachineFunction& function, const FrameSlot& GetFrameSlot(const MachineFunction& function,
const Operand& operand) { const Operand& operand) {
if (operand.kind() != Operand::Kind::FrameIndex) { if (operand.kind() != Operand::Kind::FrameIndex) {
throw std::runtime_error("期望 FrameIndex 操作数"); throw std::runtime_error(FormatError("mir", "期望 FrameIndex 操作数"));
} }
return function.frame_slot(operand.frame_index()); return function.frame_slot(operand.frame_index());
} }

View File

@@ -3,6 +3,8 @@
#include <stdexcept> #include <stdexcept>
#include <utility> #include <utility>
#include "utils/Log.h"
namespace mir { namespace mir {
MachineFunction::MachineFunction(std::string name) MachineFunction::MachineFunction(std::string name)
@@ -16,14 +18,14 @@ int MachineFunction::CreateFrameIndex(int size) {
FrameSlot& MachineFunction::frame_slot(int index) { FrameSlot& MachineFunction::frame_slot(int index) {
if (index < 0 || index >= static_cast<int>(frame_slots_.size())) { if (index < 0 || index >= static_cast<int>(frame_slots_.size())) {
throw std::runtime_error("非法 FrameIndex"); throw std::runtime_error(FormatError("mir", "非法 FrameIndex"));
} }
return frame_slots_[index]; return frame_slots_[index];
} }
const FrameSlot& MachineFunction::frame_slot(int index) const { const FrameSlot& MachineFunction::frame_slot(int index) const {
if (index < 0 || index >= static_cast<int>(frame_slots_.size())) { if (index < 0 || index >= static_cast<int>(frame_slots_.size())) {
throw std::runtime_error("非法 FrameIndex"); throw std::runtime_error(FormatError("mir", "非法 FrameIndex"));
} }
return frame_slots_[index]; return frame_slots_[index];
} }

View File

@@ -2,6 +2,8 @@
#include <stdexcept> #include <stdexcept>
#include "utils/Log.h"
namespace mir { namespace mir {
const char* PhysRegName(PhysReg reg) { const char* PhysRegName(PhysReg reg) {
@@ -19,7 +21,7 @@ const char* PhysRegName(PhysReg reg) {
case PhysReg::SP: case PhysReg::SP:
return "sp"; return "sp";
} }
throw std::runtime_error("未知物理寄存器"); throw std::runtime_error(FormatError("mir", "未知物理寄存器"));
} }
} // namespace mir } // namespace mir