diff --git a/src/ir/IRBuilder.cpp b/src/ir/IRBuilder.cpp index c30c48a..2999dde 100644 --- a/src/ir/IRBuilder.cpp +++ b/src/ir/IRBuilder.cpp @@ -3,8 +3,11 @@ // - 提供创建各类指令的便捷接口,降低 IRGen 复杂度 #include "ir/IR.h" + #include +#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(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(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(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(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(ctx_.Void(), v); } diff --git a/src/ir/IRPrinter.cpp b/src/ir/IRPrinter.cpp index 71b51f3..8709cf3 100644 --- a/src/ir/IRPrinter.cpp +++ b/src/ir/IRPrinter.cpp @@ -8,6 +8,8 @@ #include #include +#include "utils/Log.h" + namespace ir { static const char* TypeToString(const Type& ty) { @@ -19,7 +21,7 @@ static const char* TypeToString(const Type& ty) { case Type::Kind::PtrInt32: return "i32*"; } - throw std::runtime_error("未知类型"); + throw std::runtime_error(FormatError("ir", "未知类型")); } static const char* OpcodeToString(Opcode op) { diff --git a/src/ir/Instruction.cpp b/src/ir/Instruction.cpp index e9cb5c4..7731425 100644 --- a/src/ir/Instruction.cpp +++ b/src/ir/Instruction.cpp @@ -5,6 +5,8 @@ #include +#include "utils/Log.h" + namespace ir { Instruction::Instruction(Opcode op, std::shared_ptr ty, std::string name) : Value(std::move(ty), std::move(name)), opcode_(op) {} @@ -21,20 +23,20 @@ BinaryInst::BinaryInst(Opcode op, std::shared_ptr 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 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 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 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 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); diff --git a/src/mir/AsmPrinter.cpp b/src/mir/AsmPrinter.cpp index 5a712ec..06083ef 100644 --- a/src/mir/AsmPrinter.cpp +++ b/src/mir/AsmPrinter.cpp @@ -3,13 +3,15 @@ #include #include +#include "utils/Log.h" + namespace mir { namespace { const FrameSlot& GetFrameSlot(const MachineFunction& function, const Operand& operand) { 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()); } diff --git a/src/mir/MIRFunction.cpp b/src/mir/MIRFunction.cpp index 0e0c4ec..f9c4de6 100644 --- a/src/mir/MIRFunction.cpp +++ b/src/mir/MIRFunction.cpp @@ -3,6 +3,8 @@ #include #include +#include "utils/Log.h" + namespace mir { MachineFunction::MachineFunction(std::string name) @@ -16,14 +18,14 @@ int MachineFunction::CreateFrameIndex(int size) { FrameSlot& MachineFunction::frame_slot(int index) { if (index < 0 || index >= static_cast(frame_slots_.size())) { - throw std::runtime_error("非法 FrameIndex"); + throw std::runtime_error(FormatError("mir", "非法 FrameIndex")); } return frame_slots_[index]; } const FrameSlot& MachineFunction::frame_slot(int index) const { if (index < 0 || index >= static_cast(frame_slots_.size())) { - throw std::runtime_error("非法 FrameIndex"); + throw std::runtime_error(FormatError("mir", "非法 FrameIndex")); } return frame_slots_[index]; } diff --git a/src/mir/Register.cpp b/src/mir/Register.cpp index a60bcb2..7530470 100644 --- a/src/mir/Register.cpp +++ b/src/mir/Register.cpp @@ -2,6 +2,8 @@ #include +#include "utils/Log.h" + namespace mir { const char* PhysRegName(PhysReg reg) { @@ -19,7 +21,7 @@ const char* PhysRegName(PhysReg reg) { case PhysReg::SP: return "sp"; } - throw std::runtime_error("未知物理寄存器"); + throw std::runtime_error(FormatError("mir", "未知物理寄存器")); } } // namespace mir