// IR 指令体系: // - 二元运算/比较、load/store、call、br/condbr、ret、phi、alloca 等 // - 指令操作数与结果类型管理,支持打印与优化 #include "ir/IR.h" #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) {} Opcode Instruction::GetOpcode() const { return opcode_; } bool Instruction::IsTerminator() const { return opcode_ == Opcode::Ret; } BasicBlock* Instruction::GetParent() const { return parent_; } void Instruction::SetParent(BasicBlock* parent) { parent_ = parent; } 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(FormatError("ir", "BinaryInst 当前只支持 Add")); } if (!lhs_ || !rhs_) { throw std::runtime_error(FormatError("ir", "BinaryInst 缺少操作数")); } if (!type_ || !lhs_->GetType() || !rhs_->GetType()) { throw std::runtime_error(FormatError("ir", "BinaryInst 缺少类型信息")); } if (lhs_->GetType()->GetKind() != rhs_->GetType()->GetKind() || type_->GetKind() != lhs_->GetType()->GetKind()) { throw std::runtime_error(FormatError("ir", "BinaryInst 类型不匹配")); } if (!type_->IsInt32()) { throw std::runtime_error(FormatError("ir", "BinaryInst 当前只支持 i32")); } if (lhs_) { lhs_->AddUser(this); } if (rhs_) { rhs_->AddUser(this); } } Value* BinaryInst::GetLhs() const { return lhs_; } Value* BinaryInst::GetRhs() const { return rhs_; } ReturnInst::ReturnInst(std::shared_ptr void_ty, Value* val) : Instruction(Opcode::Ret, std::move(void_ty), ""), value_(val) { if (!value_) { throw std::runtime_error(FormatError("ir", "ReturnInst 缺少返回值")); } if (!type_ || !type_->IsVoid()) { throw std::runtime_error(FormatError("ir", "ReturnInst 返回类型必须为 void")); } value_->AddUser(this); } Value* ReturnInst::GetValue() 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(FormatError("ir", "AllocaInst 当前只支持 i32*")); } } 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(FormatError("ir", "LoadInst 缺少 ptr")); } if (!type_ || !type_->IsInt32()) { throw std::runtime_error(FormatError("ir", "LoadInst 当前只支持加载 i32")); } if (!ptr_->GetType() || !ptr_->GetType()->IsPtrInt32()) { throw std::runtime_error( FormatError("ir", "LoadInst 当前只支持从 i32* 加载")); } ptr_->AddUser(this); } Value* LoadInst::GetPtr() const { return ptr_; } StoreInst::StoreInst(std::shared_ptr void_ty, Value* val, Value* ptr) : Instruction(Opcode::Store, std::move(void_ty), ""), value_(val), ptr_(ptr) { if (!value_) { throw std::runtime_error(FormatError("ir", "StoreInst 缺少 value")); } if (!ptr_) { throw std::runtime_error(FormatError("ir", "StoreInst 缺少 ptr")); } if (!type_ || !type_->IsVoid()) { throw std::runtime_error(FormatError("ir", "StoreInst 返回类型必须为 void")); } if (!value_->GetType() || !value_->GetType()->IsInt32()) { throw std::runtime_error(FormatError("ir", "StoreInst 当前只支持存储 i32")); } if (!ptr_->GetType() || !ptr_->GetType()->IsPtrInt32()) { throw std::runtime_error( FormatError("ir", "StoreInst 当前只支持写入 i32*")); } value_->AddUser(this); ptr_->AddUser(this); } Value* StoreInst::GetValue() const { return value_; } Value* StoreInst::GetPtr() const { return ptr_; } } // namespace ir