fix(dev): 统一 IR/MIR 异常前缀
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user