• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "reg_info.h"
20 #include "insn.h"
21 #include "operand.h"
22 
23 namespace maplebe {
24 class InsnBuilder {
25 public:
InsnBuilder(MemPool & memPool)26     explicit InsnBuilder(MemPool &memPool) : mp(&memPool) {}
~InsnBuilder()27     virtual ~InsnBuilder()
28     {
29         mp = nullptr;
30     }
31 
32     template <class Target>
BuildInsn(MOperator opCode)33     Insn &BuildInsn(MOperator opCode)
34     {
35         return BuildInsn(opCode, Target::kMd[opCode]);
36     }
37     Insn &BuildInsn(MOperator opCode, const InsnDesc &idesc);
38     Insn &BuildInsn(MOperator opCode, Operand &o0);
39     Insn &BuildInsn(MOperator opCode, Operand &o0, Operand &o1);
40     Insn &BuildInsn(MOperator opCode, Operand &o0, Operand &o1, Operand &o2);
41     Insn &BuildInsn(MOperator opCode, Operand &o0, Operand &o1, Operand &o2, Operand &o3);
42     Insn &BuildInsn(MOperator opCode, Operand &o0, Operand &o1, Operand &o2, Operand &o3, Operand &o4);
43     Insn &BuildInsn(MOperator opCode, Operand &o0, Operand &o1, Operand &o2, Operand &o3, Operand &o4, Operand &o5);
44     Insn &BuildInsn(MOperator opCode, Operand &o0, Operand &o1, Operand &o2,
45                     Operand &o3, Operand &o4, Operand &o5, Operand &o6);
46     Insn &BuildInsn(MOperator opCode, std::vector<Operand *> &opnds);
47 
48     Insn &BuildCfiInsn(MOperator opCode);
49 
GetCreatedInsnNum()50     uint32 GetCreatedInsnNum() const
51     {
52         return createdInsnNum;
53     }
54 
SetDebugComment(const MapleString * comment)55     void SetDebugComment(const MapleString* comment)
56     {
57         currDedugComment = comment;
58     }
59 
ClearDebugComment()60     void ClearDebugComment()
61     {
62         currDedugComment = nullptr;
63     }
64 
65 protected:
66     MemPool *mp;
67 
68 private:
IncreaseInsnNum()69     void IncreaseInsnNum()
70     {
71         createdInsnNum++;
72     }
73     uint32 createdInsnNum = 0;
74     const MapleString *currDedugComment { nullptr };
75 };
76 
77 constexpr uint32 baseVirtualRegNO = 200; /* avoid conflicts between virtual and physical */
78 class OperandBuilder {
79 public:
80     explicit OperandBuilder(MemPool &mp, uint32 mirPregNum = 0) : alloc(&mp)
81     {
82         virtualReg.SetCount(mirPregNum);
83     }
84 
85     /* create an operand in cgfunc when no mempool is supplied */
86     ImmOperand &CreateImm(uint32 size, int64 value, MemPool *mp = nullptr);
87     ImmOperand &CreateImm(uint32 size, int64 value, bool isSigned, MemPool *mp = nullptr);
88     ImmOperand &CreateImm(const MIRSymbol &symbol, int64 offset, int32 relocs, MemPool *mp = nullptr);
89     OfstOperand &CreateOfst(int64 offset, uint32 size, MemPool *mp = nullptr);
90     MemOperand &CreateMem(uint32 size, MemPool *mp = nullptr);
91     MemOperand &CreateMem(RegOperand &baseOpnd, int64 offset, uint32 size, MemPool *mp = nullptr);
92     MemOperand &CreateMem(uint32 size, RegOperand &baseOpnd, ImmOperand &ofstOperand, MemPool *mp = nullptr);
93     MemOperand &CreateMem(uint32 size, RegOperand &baseOpnd, ImmOperand &ofstOperand, const MIRSymbol &symbol,
94                           MemPool *mp = nullptr);
95     BitShiftOperand &CreateBitShift(BitShiftOperand::ShiftOp op, uint32 amount, uint32 bitLen, MemPool *mp = nullptr);
96     RegOperand &CreateVReg(uint32 size, RegType type, MemPool *mp = nullptr);
97     RegOperand &CreateVReg(regno_t vRegNO, uint32 size, RegType type, MemPool *mp = nullptr);
98     RegOperand &CreatePReg(regno_t pRegNO, uint32 size, RegType type, MemPool *mp = nullptr);
99     ListOperand &CreateList(MemPool *mp = nullptr);
100     FuncNameOperand &CreateFuncNameOpnd(MIRSymbol &symbol, MemPool *mp = nullptr);
101     LabelOperand &CreateLabel(const char *parent, LabelIdx idx, MemPool *mp = nullptr);
102 
GetCurrentVRegNum()103     uint32 GetCurrentVRegNum() const
104     {
105         return virtualReg.GetCount();
106     }
107 
108 protected:
109     MapleAllocator alloc;
110 
111 private:
112     VregInfo virtualReg;
113     /* reg bank for multiple use */
114 };
115 }  // namespace maplebe
116 #endif  // MAPLEBE_INCLUDE_CG_IRBUILDER_H
117