// IR 指令体系: // - 二元运算/比较、load/store、call、br/condbr、ret、phi、alloca 等 // - 指令操作数与结果类型管理,支持打印与优化 #include "ir/IR.h" #include #include "utils/Log.h" namespace ir { User::User(std::shared_ptr ty, std::string name) : Value(std::move(ty), std::move(name)) {} size_t User::GetNumOperands() const { return operands_.size(); } Value* User::GetOperand(size_t index) const { if (index >= operands_.size()) { throw std::out_of_range("User operand index out of range"); } return operands_[index]; } void User::SetOperand(size_t index, Value* value) { if (index >= operands_.size()) { throw std::out_of_range("User operand index out of range"); } if (!value) { throw std::runtime_error(FormatError("ir", "User operand 不能为空")); } auto* old = operands_[index]; if (old == value) { return; } if (old) { old->RemoveUse(this, index); } operands_[index] = value; value->AddUse(this, index); } void User::AddOperand(Value* value) { if (!value) { throw std::runtime_error(FormatError("ir", "User operand 不能为空")); } size_t operand_index = operands_.size(); operands_.push_back(value); value->AddUse(this, operand_index); } Instruction::Instruction(Opcode op, std::shared_ptr ty, std::string name) : User(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)) { 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")); } AddOperand(lhs); AddOperand(rhs); } Value* BinaryInst::GetLhs() const { return GetOperand(0); } Value* BinaryInst::GetRhs() const { return GetOperand(1); } ReturnInst::ReturnInst(std::shared_ptr void_ty, Value* val) : Instruction(Opcode::Ret, std::move(void_ty), "") { if (!val) { throw std::runtime_error(FormatError("ir", "ReturnInst 缺少返回值")); } if (!type_ || !type_->IsVoid()) { throw std::runtime_error(FormatError("ir", "ReturnInst 返回类型必须为 void")); } AddOperand(val); } Value* ReturnInst::GetValue() const { return GetOperand(0); } 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)) { 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* 加载")); } AddOperand(ptr); } Value* LoadInst::GetPtr() const { return GetOperand(0); } StoreInst::StoreInst(std::shared_ptr void_ty, Value* val, Value* ptr) : Instruction(Opcode::Store, std::move(void_ty), "") { if (!val) { 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 (!val->GetType() || !val->GetType()->IsInt32()) { throw std::runtime_error(FormatError("ir", "StoreInst 当前只支持存储 i32")); } if (!ptr->GetType() || !ptr->GetType()->IsPtrInt32()) { throw std::runtime_error( FormatError("ir", "StoreInst 当前只支持写入 i32*")); } AddOperand(val); AddOperand(ptr); } Value* StoreInst::GetValue() const { return GetOperand(0); } Value* StoreInst::GetPtr() const { return GetOperand(1); } GEPInst::GEPInst(std::shared_ptr ptr_ty, Value* base, const std::vector& indices, const std::string& name) : Instruction(Opcode::GEP, ptr_ty, name) { // 添加base作为第一个操作数 AddOperand(base); // 添加所有索引作为后续操作数 for (auto* index : indices) { AddOperand(index); } } Value* GEPInst::GetBase() const { // 第一个操作数是base return GetOperand(0); } const std::vector& GEPInst::GetIndices() const { // 需要返回索引列表,但Instruction只存储操作数 // 这是一个设计问题:要么修改架构,要么提供辅助方法 // 简化实现:返回空vector(或创建临时vector) static std::vector indices; indices.clear(); // 索引从操作数1开始 for (size_t i = 1; i < GetNumOperands(); ++i) { indices.push_back(GetOperand(i)); } return indices; } // CallInst构造函数实现 CallInst::CallInst(std::shared_ptr ret_ty, Function* callee, const std::vector& args, const std::string& name) : Instruction(Opcode::Call, ret_ty, name) { // 添加callee作为第一个操作数 AddOperand(callee); // 添加所有参数作为后续操作数 for (auto* arg : args) { AddOperand(arg); } } Function* CallInst::GetCallee() const { // 第一个操作数是callee return dynamic_cast(GetOperand(0)); } const std::vector& CallInst::GetArgs() const { // 需要返回参数列表,但Instruction只存储操作数 // 简化实现:返回空vector(或创建临时vector) static std::vector args; args.clear(); // 参数从操作数1开始 for (size_t i = 1; i < GetNumOperands(); ++i) { args.push_back(GetOperand(i)); } return args; } } // namespace ir