• 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, std::vector<Operand *> &opnds);
44 
45     Insn &BuildCfiInsn(MOperator opCode);
46 
GetCreatedInsnNum()47     uint32 GetCreatedInsnNum() const
48     {
49         return createdInsnNum;
50     }
51 
SetDebugComment(const MapleString * comment)52     void SetDebugComment(const MapleString* comment)
53     {
54         currDedugComment = comment;
55     }
56 
ClearDebugComment()57     void ClearDebugComment()
58     {
59         currDedugComment = nullptr;
60     }
61 
62 protected:
63     MemPool *mp;
64 
65 private:
IncreaseInsnNum()66     void IncreaseInsnNum()
67     {
68         createdInsnNum++;
69     }
70     uint32 createdInsnNum = 0;
71     const MapleString *currDedugComment { nullptr };
72 };
73 
74 constexpr uint32 baseVirtualRegNO = 200; /* avoid conflicts between virtual and physical */
75 class OperandBuilder {
76 public:
77     explicit OperandBuilder(MemPool &mp, uint32 mirPregNum = 0) : alloc(&mp)
78     {
79         virtualReg.SetCount(mirPregNum);
80     }
81 
82     /* create an operand in cgfunc when no mempool is supplied */
83     ImmOperand &CreateImm(uint32 size, int64 value, MemPool *mp = nullptr);
84     ImmOperand &CreateImm(uint32 size, int64 value, bool isSigned, MemPool *mp = nullptr);
85     ImmOperand &CreateImm(const MIRSymbol &symbol, int64 offset, int32 relocs, MemPool *mp = nullptr);
86     OfstOperand &CreateOfst(int64 offset, uint32 size, MemPool *mp = nullptr);
87     MemOperand &CreateMem(uint32 size, MemPool *mp = nullptr);
88     MemOperand &CreateMem(RegOperand &baseOpnd, int64 offset, uint32 size, MemPool *mp = nullptr);
89     MemOperand &CreateMem(uint32 size, RegOperand &baseOpnd, ImmOperand &ofstOperand, MemPool *mp = nullptr);
90     MemOperand &CreateMem(uint32 size, RegOperand &baseOpnd, ImmOperand &ofstOperand, const MIRSymbol &symbol,
91                           MemPool *mp = nullptr);
92     BitShiftOperand &CreateBitShift(BitShiftOperand::ShiftOp op, uint32 amount, uint32 bitLen, MemPool *mp = nullptr);
93     RegOperand &CreateVReg(uint32 size, RegType type, MemPool *mp = nullptr);
94     RegOperand &CreateVReg(regno_t vRegNO, uint32 size, RegType type, MemPool *mp = nullptr);
95     RegOperand &CreatePReg(regno_t pRegNO, uint32 size, RegType type, MemPool *mp = nullptr);
96     ListOperand &CreateList(MemPool *mp = nullptr);
97     FuncNameOperand &CreateFuncNameOpnd(MIRSymbol &symbol, MemPool *mp = nullptr);
98     LabelOperand &CreateLabel(const char *parent, LabelIdx idx, MemPool *mp = nullptr);
99 
GetCurrentVRegNum()100     uint32 GetCurrentVRegNum() const
101     {
102         return virtualReg.GetCount();
103     }
104 
105 protected:
106     MapleAllocator alloc;
107 
108 private:
109     VregInfo virtualReg;
110     /* reg bank for multiple use */
111 };
112 }  // namespace maplebe
113 #endif  // MAPLEBE_INCLUDE_CG_IRBUILDER_H
114