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 #include "cg_irbuilder.h"
17 #include "isa.h"
18 #include "cg.h"
19 #include "cfi.h"
20 #include "dbg.h"
21
22 namespace maplebe {
BuildInsn(MOperator opCode,const InsnDesc & idesc)23 Insn &InsnBuilder::BuildInsn(MOperator opCode, const InsnDesc &idesc)
24 {
25 auto *newInsn = mp->New<Insn>(*mp, opCode);
26 newInsn->SetInsnDescrption(idesc);
27 IncreaseInsnNum();
28 return *newInsn;
29 }
30
BuildInsn(MOperator opCode,Operand & o0)31 Insn &InsnBuilder::BuildInsn(MOperator opCode, Operand &o0)
32 {
33 const InsnDesc &tMd = Globals::GetInstance()->GetTarget()->GetTargetMd(opCode);
34 return BuildInsn(opCode, tMd).AddOpndChain(o0);
35 }
BuildInsn(MOperator opCode,Operand & o0,Operand & o1)36 Insn &InsnBuilder::BuildInsn(MOperator opCode, Operand &o0, Operand &o1)
37 {
38 const InsnDesc &tMd = Globals::GetInstance()->GetTarget()->GetTargetMd(opCode);
39 return BuildInsn(opCode, tMd).AddOpndChain(o0).AddOpndChain(o1);
40 }
BuildInsn(MOperator opCode,Operand & o0,Operand & o1,Operand & o2)41 Insn &InsnBuilder::BuildInsn(MOperator opCode, Operand &o0, Operand &o1, Operand &o2)
42 {
43 const InsnDesc &tMd = Globals::GetInstance()->GetTarget()->GetTargetMd(opCode);
44 return BuildInsn(opCode, tMd).AddOpndChain(o0).AddOpndChain(o1).AddOpndChain(o2);
45 }
46
BuildInsn(MOperator opCode,Operand & o0,Operand & o1,Operand & o2,Operand & o3)47 Insn &InsnBuilder::BuildInsn(MOperator opCode, Operand &o0, Operand &o1, Operand &o2, Operand &o3)
48 {
49 const InsnDesc &tMd = Globals::GetInstance()->GetTarget()->GetTargetMd(opCode);
50 return BuildInsn(opCode, tMd).AddOpndChain(o0).AddOpndChain(o1).AddOpndChain(o2).AddOpndChain(o3);
51 }
52
BuildInsn(MOperator opCode,Operand & o0,Operand & o1,Operand & o2,Operand & o3,Operand & o4)53 Insn &InsnBuilder::BuildInsn(MOperator opCode, Operand &o0, Operand &o1, Operand &o2, Operand &o3, Operand &o4)
54 {
55 const InsnDesc &tMd = Globals::GetInstance()->GetTarget()->GetTargetMd(opCode);
56 Insn &nI = BuildInsn(opCode, tMd);
57 return nI.AddOpndChain(o0).AddOpndChain(o1).AddOpndChain(o2).AddOpndChain(o3).AddOpndChain(o4);
58 }
59
BuildInsn(MOperator opCode,std::vector<Operand * > & opnds)60 Insn &InsnBuilder::BuildInsn(MOperator opCode, std::vector<Operand *> &opnds)
61 {
62 const InsnDesc &tMd = Globals::GetInstance()->GetTarget()->GetTargetMd(opCode);
63 Insn &nI = BuildInsn(opCode, tMd);
64 for (auto *opnd : opnds) {
65 nI.AddOperand(*opnd);
66 }
67 return nI;
68 }
69
BuildCfiInsn(MOperator opCode)70 Insn &InsnBuilder::BuildCfiInsn(MOperator opCode)
71 {
72 auto *nI = mp->New<cfi::CfiInsn>(*mp, opCode);
73 IncreaseInsnNum();
74 return *nI;
75 }
BuildDbgInsn(MOperator opCode)76 Insn &InsnBuilder::BuildDbgInsn(MOperator opCode)
77 {
78 auto *nI = mp->New<mpldbg::DbgInsn>(*mp, opCode);
79 IncreaseInsnNum();
80 return *nI;
81 }
82
BuildVectorInsn(MOperator opCode,const InsnDesc & idesc)83 VectorInsn &InsnBuilder::BuildVectorInsn(MOperator opCode, const InsnDesc &idesc)
84 {
85 auto *newInsn = mp->New<VectorInsn>(*mp, opCode);
86 newInsn->SetInsnDescrption(idesc);
87 IncreaseInsnNum();
88 return *newInsn;
89 }
90
CreateImm(uint32 size,int64 value,MemPool * mp)91 ImmOperand &OperandBuilder::CreateImm(uint32 size, int64 value, MemPool *mp)
92 {
93 return mp ? *mp->New<ImmOperand>(value, size, false) : *alloc.New<ImmOperand>(value, size, false);
94 }
95
CreateImm(const MIRSymbol & symbol,int64 offset,int32 relocs,MemPool * mp)96 ImmOperand &OperandBuilder::CreateImm(const MIRSymbol &symbol, int64 offset, int32 relocs, MemPool *mp)
97 {
98 return mp ? *mp->New<ImmOperand>(symbol, offset, relocs, false)
99 : *alloc.New<ImmOperand>(symbol, offset, relocs, false);
100 }
101
CreateMem(uint32 size,MemPool * mp)102 MemOperand &OperandBuilder::CreateMem(uint32 size, MemPool *mp)
103 {
104 return mp ? *mp->New<MemOperand>(size) : *alloc.New<MemOperand>(size);
105 }
106
CreateMem(RegOperand & baseOpnd,int64 offset,uint32 size)107 MemOperand &OperandBuilder::CreateMem(RegOperand &baseOpnd, int64 offset, uint32 size)
108 {
109 MemOperand *memOprand = &CreateMem(size);
110 memOprand->SetBaseRegister(baseOpnd);
111 memOprand->SetOffsetOperand(CreateImm(baseOpnd.GetSize(), offset));
112 return *memOprand;
113 }
114
CreateVReg(uint32 size,RegType type,MemPool * mp)115 RegOperand &OperandBuilder::CreateVReg(uint32 size, RegType type, MemPool *mp)
116 {
117 virtualRegNum++;
118 regno_t vRegNO = baseVirtualRegNO + virtualRegNum;
119 return mp ? *mp->New<RegOperand>(vRegNO, size, type) : *alloc.New<RegOperand>(vRegNO, size, type);
120 }
121
CreateVReg(regno_t vRegNO,uint32 size,RegType type,MemPool * mp)122 RegOperand &OperandBuilder::CreateVReg(regno_t vRegNO, uint32 size, RegType type, MemPool *mp)
123 {
124 return mp ? *mp->New<RegOperand>(vRegNO, size, type) : *alloc.New<RegOperand>(vRegNO, size, type);
125 }
126
CreatePReg(regno_t pRegNO,uint32 size,RegType type,MemPool * mp)127 RegOperand &OperandBuilder::CreatePReg(regno_t pRegNO, uint32 size, RegType type, MemPool *mp)
128 {
129 return mp ? *mp->New<RegOperand>(pRegNO, size, type) : *alloc.New<RegOperand>(pRegNO, size, type);
130 }
131
CreateList(MemPool * mp)132 ListOperand &OperandBuilder::CreateList(MemPool *mp)
133 {
134 return mp ? *mp->New<ListOperand>(alloc) : *alloc.New<ListOperand>(alloc);
135 }
136
CreateFuncNameOpnd(MIRSymbol & symbol,MemPool * mp)137 FuncNameOperand &OperandBuilder::CreateFuncNameOpnd(MIRSymbol &symbol, MemPool *mp)
138 {
139 return mp ? *mp->New<FuncNameOperand>(symbol) : *alloc.New<FuncNameOperand>(symbol);
140 }
141
CreateLabel(const char * parent,LabelIdx idx,MemPool * mp)142 LabelOperand &OperandBuilder::CreateLabel(const char *parent, LabelIdx idx, MemPool *mp)
143 {
144 return mp ? *mp->New<LabelOperand>(parent, idx) : *alloc.New<LabelOperand>(parent, idx);
145 }
146
CreateComment(const std::string & s,MemPool * mp)147 CommentOperand &OperandBuilder::CreateComment(const std::string &s, MemPool *mp)
148 {
149 return mp ? *mp->New<CommentOperand>(s, *mp) : *alloc.New<CommentOperand>(s, *mp);
150 }
151
CreateComment(const MapleString & s,MemPool * mp)152 CommentOperand &OperandBuilder::CreateComment(const MapleString &s, MemPool *mp)
153 {
154 return mp ? *mp->New<CommentOperand>(s.c_str(), *mp) : *alloc.New<CommentOperand>(s.c_str(), *mp);
155 }
156
157 } // namespace maplebe
158