// IR 基本块: // - 保存指令序列 // - 为后续 CFG 分析预留前驱/后继接口 // // 当前仍是最小实现: // - BasicBlock 已纳入 Value 体系,但类型先用 void 占位; // - 指令追加与 terminator 约束主要在头文件中的 Append 模板里处理; // - 前驱/后继容器已经预留,但当前项目里还没有分支指令与自动维护逻辑。 #include "include/ir/IR.h" #include namespace ir { // 当前 BasicBlock 还没有专门的 label type,因此先用 void 作为占位类型。 BasicBlock::BasicBlock(std::string name) : Value(Type::GetVoidType(), std::move(name)) {} Function* BasicBlock::GetParent() const { return parent_; } void BasicBlock::SetParent(Function* parent) { parent_ = parent; } bool BasicBlock::HasTerminator() const { return !instructions_.empty() && instructions_.back()->IsTerminator(); } // 按插入顺序返回块内指令序列。 const std::vector>& BasicBlock::GetInstructions() const { return instructions_; } // 前驱/后继接口先保留给后续 CFG 扩展使用。 // 当前最小 IR 中还没有 branch 指令,因此这些列表通常为空。 const std::vector& BasicBlock::GetPredecessors() const { return predecessors_; } const std::vector& BasicBlock::GetSuccessors() const { return successors_; } std::unique_ptr BasicBlock::TakeInstruction(Instruction* inst) { for (auto it = instructions_.begin(); it != instructions_.end(); ++it) { if (it->get() == inst) { auto owned = std::move(*it); instructions_.erase(it); owned->SetParent(nullptr); return owned; } } return nullptr; } void BasicBlock::InsertInstructionBeforeTerminator(std::unique_ptr inst) { inst->SetParent(this); size_t pos = instructions_.size(); for (size_t i = 0; i < instructions_.size(); ++i) { if (instructions_[i]->IsTerminator()) { pos = i; break; } } instructions_.insert(instructions_.begin() + pos, std::move(inst)); } } // namespace ir