#ifndef _SYSYF_BASICBLOCK_H_ #define _SYSYF_BASICBLOCK_H_ #include "Value.h" #include "Instruction.h" #include "Module.h" #include "Function.h" #include #include #include namespace SysYF { namespace IR { class Function; class Instruction; class Module; class BasicBlock : public Value { public: static Ptr create(Ptr m, const std::string &name , Ptr parent ) { auto prefix = name.empty() ? "" : "label_"; RET_AFTER_INIT(BasicBlock, m, prefix + name, parent); } // return parent, or null if none. Ptr get_parent() { return parent_; } Ptr get_module(); /****************api about cfg****************/ PtrList &get_pre_basic_blocks() { return pre_bbs_; } PtrList &get_succ_basic_blocks() { return succ_bbs_; } void add_pre_basic_block(Ptr bb) { pre_bbs_.push_back(bb); } void add_succ_basic_block(Ptr bb) { succ_bbs_.push_back(bb); } void remove_pre_basic_block(Ptr bb) { pre_bbs_.remove(bb); } void remove_succ_basic_block(Ptr bb) { succ_bbs_.remove(bb); } /****************api about cfg****************/ /// Returns the terminator instruction if the block is well formed or null /// if the block is not well formed. const Ptr get_terminator() const; Ptr get_terminator() { return const_pointer_cast( static_pointer_cast(shared_from_this())->get_terminator()); } void add_instruction(Ptr instr); void add_instruction(PtrList::iterator instr_pos, Ptr instr); void add_instr_begin(Ptr instr); PtrList::iterator find_instruction(Ptr instr); void delete_instr(Ptr instr); bool empty() { return instr_list_.empty(); } int get_num_of_instr() { return instr_list_.size(); } PtrList &get_instructions() { return instr_list_; } void erase_from_parent(); virtual std::string print() override; private: explicit BasicBlock(Ptr m, const std::string &name , Ptr parent ); void init(Ptr m, const std::string &name , Ptr parent ); PtrList pre_bbs_; PtrList succ_bbs_; PtrList instr_list_; Ptr parent_; }; } } #endif // _SYSYF_BASICBLOCK_H_