fix(ir): 修改了一下context的管理
This commit is contained in:
@@ -6,19 +6,8 @@
|
||||
#include <stdexcept>
|
||||
|
||||
namespace ir {
|
||||
namespace {
|
||||
|
||||
bool IsArithmeticType(const std::shared_ptr<Type>& ty) {
|
||||
return ty && ty->kind() == Type::Kind::Int32;
|
||||
}
|
||||
|
||||
bool IsPtrInt32Type(const std::shared_ptr<Type>& ty) {
|
||||
return ty && ty->kind() == Type::Kind::PtrInt32;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
IRBuilder::IRBuilder(BasicBlock* bb) : insertBlock_(bb) {}
|
||||
IRBuilder::IRBuilder(Context& ctx, BasicBlock* bb)
|
||||
: ctx_(ctx), insertBlock_(bb) {}
|
||||
|
||||
void IRBuilder::SetInsertPoint(BasicBlock* bb) { insertBlock_ = bb; }
|
||||
|
||||
@@ -26,7 +15,7 @@ BasicBlock* IRBuilder::GetInsertBlock() const { return insertBlock_; }
|
||||
|
||||
ConstantInt* IRBuilder::CreateConstInt(int v) {
|
||||
// 常量不需要挂在基本块里,由 Context 负责去重与生命周期。
|
||||
return DefaultContext().GetConstInt(v);
|
||||
return ctx_.GetConstInt(v);
|
||||
}
|
||||
|
||||
BinaryInst* IRBuilder::CreateBinary(Opcode op, Value* lhs, Value* rhs,
|
||||
@@ -40,16 +29,6 @@ BinaryInst* IRBuilder::CreateBinary(Opcode op, Value* lhs, Value* rhs,
|
||||
if (!rhs) {
|
||||
throw std::runtime_error("IRBuilder::CreateBinary 缺少 rhs");
|
||||
}
|
||||
if (op != Opcode::Add) {
|
||||
throw std::runtime_error("IRBuilder::CreateBinary 当前只支持 Add");
|
||||
}
|
||||
if (!lhs->type() || !rhs->type() ||
|
||||
lhs->type()->kind() != rhs->type()->kind()) {
|
||||
throw std::runtime_error("IRBuilder::CreateBinary 操作数类型不匹配");
|
||||
}
|
||||
if (!IsArithmeticType(lhs->type())) {
|
||||
throw std::runtime_error("IRBuilder::CreateBinary 当前只支持 i32 二元运算");
|
||||
}
|
||||
return insertBlock_->Append<BinaryInst>(op, lhs->type(), lhs, rhs, name);
|
||||
}
|
||||
|
||||
@@ -62,7 +41,7 @@ AllocaInst* IRBuilder::CreateAllocaI32(const std::string& name) {
|
||||
if (!insertBlock_) {
|
||||
throw std::runtime_error("IRBuilder 未设置插入点");
|
||||
}
|
||||
return insertBlock_->Append<AllocaInst>(name);
|
||||
return insertBlock_->Append<AllocaInst>(ctx_.PtrInt32(), name);
|
||||
}
|
||||
|
||||
LoadInst* IRBuilder::CreateLoad(Value* ptr, const std::string& name) {
|
||||
@@ -72,10 +51,7 @@ LoadInst* IRBuilder::CreateLoad(Value* ptr, const std::string& name) {
|
||||
if (!ptr) {
|
||||
throw std::runtime_error("IRBuilder::CreateLoad 缺少 ptr");
|
||||
}
|
||||
if (!IsPtrInt32Type(ptr->type())) {
|
||||
throw std::runtime_error("IRBuilder::CreateLoad 当前只支持从 i32* 加载");
|
||||
}
|
||||
return insertBlock_->Append<LoadInst>(ptr, name);
|
||||
return insertBlock_->Append<LoadInst>(ctx_.Int32(), ptr, name);
|
||||
}
|
||||
|
||||
StoreInst* IRBuilder::CreateStore(Value* val, Value* ptr) {
|
||||
@@ -88,13 +64,7 @@ StoreInst* IRBuilder::CreateStore(Value* val, Value* ptr) {
|
||||
if (!ptr) {
|
||||
throw std::runtime_error("IRBuilder::CreateStore 缺少 ptr");
|
||||
}
|
||||
if (!IsArithmeticType(val->type())) {
|
||||
throw std::runtime_error("IRBuilder::CreateStore 当前只支持存储 i32");
|
||||
}
|
||||
if (!IsPtrInt32Type(ptr->type())) {
|
||||
throw std::runtime_error("IRBuilder::CreateStore 当前只支持写入 i32*");
|
||||
}
|
||||
return insertBlock_->Append<StoreInst>(val, ptr);
|
||||
return insertBlock_->Append<StoreInst>(ctx_.Void(), val, ptr);
|
||||
}
|
||||
|
||||
ReturnInst* IRBuilder::CreateRet(Value* v) {
|
||||
@@ -104,7 +74,7 @@ ReturnInst* IRBuilder::CreateRet(Value* v) {
|
||||
if (!v) {
|
||||
throw std::runtime_error("IRBuilder::CreateRet 缺少返回值");
|
||||
}
|
||||
return insertBlock_->Append<ReturnInst>(v);
|
||||
return insertBlock_->Append<ReturnInst>(ctx_.Void(), v);
|
||||
}
|
||||
|
||||
} // namespace ir
|
||||
|
||||
Reference in New Issue
Block a user