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