fix(ir): 修改了一下context的管理
This commit is contained in:
@@ -6,18 +6,6 @@
|
||||
#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
|
||||
|
||||
Instruction::Instruction(Opcode op, std::shared_ptr<Type> ty, std::string name)
|
||||
: Value(std::move(ty), std::move(name)), opcode_(op) {}
|
||||
|
||||
@@ -45,7 +33,7 @@ BinaryInst::BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs,
|
||||
type_->kind() != lhs_->type()->kind()) {
|
||||
throw std::runtime_error("BinaryInst 类型不匹配");
|
||||
}
|
||||
if (!IsArithmeticType(type_)) {
|
||||
if (!type_->IsInt32()) {
|
||||
throw std::runtime_error("BinaryInst 当前只支持 i32");
|
||||
}
|
||||
if (lhs_) {
|
||||
@@ -60,25 +48,37 @@ Value* BinaryInst::lhs() const { return lhs_; }
|
||||
|
||||
Value* BinaryInst::rhs() const { return rhs_; }
|
||||
|
||||
ReturnInst::ReturnInst(Value* val)
|
||||
: Instruction(Opcode::Ret, Type::Void(), ""), value_(val) {
|
||||
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 缺少返回值");
|
||||
}
|
||||
if (!type_ || !type_->IsVoid()) {
|
||||
throw std::runtime_error("ReturnInst 返回类型必须为 void");
|
||||
}
|
||||
value_->AddUser(this);
|
||||
}
|
||||
|
||||
Value* ReturnInst::value() const { return value_; }
|
||||
|
||||
AllocaInst::AllocaInst(std::string name)
|
||||
: Instruction(Opcode::Alloca, Type::PtrInt32(), std::move(name)) {}
|
||||
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*");
|
||||
}
|
||||
}
|
||||
|
||||
LoadInst::LoadInst(Value* ptr, std::string name)
|
||||
: Instruction(Opcode::Load, Type::Int32(), std::move(name)), ptr_(ptr) {
|
||||
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");
|
||||
}
|
||||
if (!IsPtrInt32Type(ptr_->type())) {
|
||||
if (!type_ || !type_->IsInt32()) {
|
||||
throw std::runtime_error("LoadInst 当前只支持加载 i32");
|
||||
}
|
||||
if (!ptr_->type() || !ptr_->type()->IsPtrInt32()) {
|
||||
throw std::runtime_error("LoadInst 当前只支持从 i32* 加载");
|
||||
}
|
||||
ptr_->AddUser(this);
|
||||
@@ -86,18 +86,23 @@ LoadInst::LoadInst(Value* ptr, std::string name)
|
||||
|
||||
Value* LoadInst::ptr() const { return ptr_; }
|
||||
|
||||
StoreInst::StoreInst(Value* val, Value* ptr)
|
||||
: Instruction(Opcode::Store, Type::Void(), ""), value_(val), ptr_(ptr) {
|
||||
StoreInst::StoreInst(std::shared_ptr<Type> void_ty, Value* val, Value* ptr)
|
||||
: Instruction(Opcode::Store, std::move(void_ty), ""),
|
||||
value_(val),
|
||||
ptr_(ptr) {
|
||||
if (!value_) {
|
||||
throw std::runtime_error("StoreInst 缺少 value");
|
||||
}
|
||||
if (!ptr_) {
|
||||
throw std::runtime_error("StoreInst 缺少 ptr");
|
||||
}
|
||||
if (!IsArithmeticType(value_->type())) {
|
||||
if (!type_ || !type_->IsVoid()) {
|
||||
throw std::runtime_error("StoreInst 返回类型必须为 void");
|
||||
}
|
||||
if (!value_->type() || !value_->type()->IsInt32()) {
|
||||
throw std::runtime_error("StoreInst 当前只支持存储 i32");
|
||||
}
|
||||
if (!IsPtrInt32Type(ptr_->type())) {
|
||||
if (!ptr_->type() || !ptr_->type()->IsPtrInt32()) {
|
||||
throw std::runtime_error("StoreInst 当前只支持写入 i32*");
|
||||
}
|
||||
value_->AddUser(this);
|
||||
|
||||
Reference in New Issue
Block a user