#ifndef _SYSYF_IRSTMTBUILDER_H_ #define _SYSYF_IRSTMTBUILDER_H_ #include "BasicBlock.h" #include "Instruction.h" #include "Value.h" namespace SysYF { namespace IR { class IRStmtBuilder { private: Ptr BB_; Ptr m_; explicit IRStmtBuilder(Ptr bb, Ptr m) : BB_(bb), m_(m) {}; void init(Ptr bb, Ptr m) {} public: static Ptr create(Ptr bb, Ptr m) { RET_AFTER_INIT(IRStmtBuilder, bb, m); } ~IRStmtBuilder() = default; Ptr get_module(){return m_;} Ptr get_insert_block() { return this->BB_; } void set_insert_point(Ptr bb) { this->BB_ = bb; } //在某个基本块中插入指令 Ptr create_iadd( Ptr lhs, Ptr rhs){ return BinaryInst::create_add( lhs, rhs, this->BB_, m_);} //创建加法指令(以及其他算术指令) Ptr create_isub( Ptr lhs, Ptr rhs){ return BinaryInst::create_sub( lhs, rhs, this->BB_, m_);} Ptr create_imul( Ptr lhs, Ptr rhs){ return BinaryInst::create_mul( lhs, rhs, this->BB_, m_);} Ptr create_isdiv( Ptr lhs, Ptr rhs){ return BinaryInst::create_sdiv( lhs, rhs, this->BB_, m_);} Ptr create_isrem( Ptr lhs, Ptr rhs){ return BinaryInst::create_srem( lhs, rhs, this->BB_, m_);} Ptr create_icmp_eq( Ptr lhs, Ptr rhs){ return CmpInst::create_cmp(CmpInst::EQ, lhs, rhs, this->BB_, m_); } Ptr create_icmp_ne( Ptr lhs, Ptr rhs){ return CmpInst::create_cmp(CmpInst::NE, lhs, rhs, this->BB_, m_); } Ptr create_icmp_gt( Ptr lhs, Ptr rhs){ return CmpInst::create_cmp(CmpInst::GT, lhs, rhs, this->BB_, m_); } Ptr create_icmp_ge( Ptr lhs, Ptr rhs){ return CmpInst::create_cmp(CmpInst::GE, lhs, rhs, this->BB_, m_); } Ptr create_icmp_lt( Ptr lhs, Ptr rhs){ return CmpInst::create_cmp(CmpInst::LT, lhs, rhs, this->BB_, m_); } Ptr create_icmp_le( Ptr lhs, Ptr rhs){ return CmpInst::create_cmp(CmpInst::LE, lhs, rhs, this->BB_, m_); } Ptr create_fadd( Ptr lhs, Ptr rhs){ return BinaryInst::create_fadd( lhs, rhs, this->BB_, m_);} Ptr create_fsub( Ptr lhs, Ptr rhs){ return BinaryInst::create_fsub( lhs, rhs, this->BB_, m_);} Ptr create_fmul( Ptr lhs, Ptr rhs){ return BinaryInst::create_fmul( lhs, rhs, this->BB_, m_);} Ptr create_fdiv( Ptr lhs, Ptr rhs){ return BinaryInst::create_fdiv( lhs, rhs, this->BB_, m_);} Ptr create_fcmp_eq( Ptr lhs, Ptr rhs){ return FCmpInst::create_fcmp(FCmpInst::EQ, lhs, rhs, this->BB_, m_); } Ptr create_fcmp_ne( Ptr lhs, Ptr rhs){ return FCmpInst::create_fcmp(FCmpInst::NE, lhs, rhs, this->BB_, m_); } Ptr create_fcmp_gt( Ptr lhs, Ptr rhs){ return FCmpInst::create_fcmp(FCmpInst::GT, lhs, rhs, this->BB_, m_); } Ptr create_fcmp_ge( Ptr lhs, Ptr rhs){ return FCmpInst::create_fcmp(FCmpInst::GE, lhs, rhs, this->BB_, m_); } Ptr create_fcmp_lt( Ptr lhs, Ptr rhs){ return FCmpInst::create_fcmp(FCmpInst::LT, lhs, rhs, this->BB_, m_); } Ptr create_fcmp_le( Ptr lhs, Ptr rhs){ return FCmpInst::create_fcmp(FCmpInst::LE, lhs, rhs, this->BB_, m_); } Ptr create_call(Ptr func, PtrVec args) { #ifdef DEBUG assert( dynamic_pointer_cast(func) && "func must be Ptr type"); #endif return CallInst::create(static_pointer_cast(func) ,args, this->BB_); } Ptr create_br(Ptr if_true){ return BranchInst::create_br(if_true, this->BB_); } Ptr create_cond_br(Ptr cond, Ptr if_true, Ptr if_false){ return BranchInst::create_cond_br(cond, if_true, if_false,this->BB_); } Ptr create_ret(Ptr val) { return ReturnInst::create_ret(val,this->BB_); } Ptr create_void_ret() { return ReturnInst::create_void_ret(this->BB_); } Ptr create_gep(Ptr ptr, PtrVec idxs) { return GetElementPtrInst::create_gep(ptr, idxs, this->BB_); } Ptr create_store(Ptr val, Ptr ptr) { return StoreInst::create_store(val, ptr, this->BB_ ); } Ptr create_load(Ptr ty, Ptr ptr) { return LoadInst::create_load(ty, ptr, this->BB_); } Ptr create_load(Ptr ptr) { #ifdef DEBUG assert( ptr->get_type()->is_pointer_type() && "ptr must be pointer type" ); #endif return LoadInst::create_load(ptr->get_type()->get_pointer_element_type(), ptr, this->BB_); } Ptr create_alloca(Ptr ty) { return AllocaInst::create_alloca(ty, this->BB_); } Ptr create_zext(Ptr val, Ptr ty) { return ZextInst::create_zext(val, ty, this->BB_); } Ptr create_fptosi(Ptr val, Ptr ty) { return FpToSiInst::create_fptosi(val, ty, this->BB_); } Ptr create_sitofp(Ptr val, Ptr ty) { return SiToFpInst::create_sitofp(val, ty, this->BB_); } }; } } #endif // _SYSYF_IRSTMTBUILDER_H_