1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MAPLEBE_INCLUDE_CG_IRBUILDER_H 17 #define MAPLEBE_INCLUDE_CG_IRBUILDER_H 18 19 #include "insn.h" 20 #include "operand.h" 21 22 namespace maplebe { 23 class InsnBuilder { 24 public: InsnBuilder(MemPool & memPool)25 explicit InsnBuilder(MemPool &memPool) : mp(&memPool) {} ~InsnBuilder()26 virtual ~InsnBuilder() 27 { 28 mp = nullptr; 29 } 30 31 template <class Target> BuildInsn(MOperator opCode)32 Insn &BuildInsn(MOperator opCode) 33 { 34 return BuildInsn(opCode, Target::kMd[opCode]); 35 } 36 Insn &BuildInsn(MOperator opCode, const InsnDesc &idesc); 37 Insn &BuildInsn(MOperator opCode, Operand &o0); 38 Insn &BuildInsn(MOperator opCode, Operand &o0, Operand &o1); 39 Insn &BuildInsn(MOperator opCode, Operand &o0, Operand &o1, Operand &o2); 40 Insn &BuildInsn(MOperator opCode, Operand &o0, Operand &o1, Operand &o2, Operand &o3); 41 Insn &BuildInsn(MOperator opCode, Operand &o0, Operand &o1, Operand &o2, Operand &o3, Operand &o4); 42 Insn &BuildInsn(MOperator opCode, std::vector<Operand *> &opnds); 43 44 Insn &BuildCfiInsn(MOperator opCode); 45 Insn &BuildDbgInsn(MOperator opCode); 46 VectorInsn &BuildVectorInsn(MOperator opCode, const InsnDesc &idesc); 47 GetCreatedInsnNum()48 uint32 GetCreatedInsnNum() const 49 { 50 return createdInsnNum; 51 } 52 53 protected: 54 MemPool *mp; 55 56 private: IncreaseInsnNum()57 void IncreaseInsnNum() 58 { 59 createdInsnNum++; 60 } 61 uint32 createdInsnNum = 0; 62 }; 63 64 constexpr uint32 baseVirtualRegNO = 200; /* avoid conflicts between virtual and physical */ 65 class OperandBuilder { 66 public: 67 explicit OperandBuilder(MemPool &mp, uint32 mirPregNum = 0) : alloc(&mp), virtualRegNum(mirPregNum) {} 68 69 /* create an operand in cgfunc when no mempool is supplied */ 70 ImmOperand &CreateImm(uint32 size, int64 value, MemPool *mp = nullptr); 71 ImmOperand &CreateImm(const MIRSymbol &symbol, int64 offset, int32 relocs, MemPool *mp = nullptr); 72 MemOperand &CreateMem(uint32 size, MemPool *mp = nullptr); 73 MemOperand &CreateMem(RegOperand &baseOpnd, int64 offset, uint32 size); 74 RegOperand &CreateVReg(uint32 size, RegType type, MemPool *mp = nullptr); 75 RegOperand &CreateVReg(regno_t vRegNO, uint32 size, RegType type, MemPool *mp = nullptr); 76 RegOperand &CreatePReg(regno_t pRegNO, uint32 size, RegType type, MemPool *mp = nullptr); 77 ListOperand &CreateList(MemPool *mp = nullptr); 78 FuncNameOperand &CreateFuncNameOpnd(MIRSymbol &symbol, MemPool *mp = nullptr); 79 LabelOperand &CreateLabel(const char *parent, LabelIdx idx, MemPool *mp = nullptr); 80 CommentOperand &CreateComment(const std::string &s, MemPool *mp = nullptr); 81 CommentOperand &CreateComment(const MapleString &s, MemPool *mp = nullptr); 82 GetCurrentVRegNum()83 uint32 GetCurrentVRegNum() const 84 { 85 return virtualRegNum; 86 } 87 88 protected: 89 MapleAllocator alloc; 90 91 private: 92 uint32 virtualRegNum = 0; 93 /* reg bank for multiple use */ 94 }; 95 } // namespace maplebe 96 #endif // MAPLEBE_INCLUDE_CG_IRBUILDER_H 97