• 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 #include "mir_builder.h"
17 #include "mir_symbol_builder.h"
18 
19 namespace maple {
20 // create a function named str
GetOrCreateFunction(const std::string & str,TyIdx retTyIdx)21 MIRFunction *MIRBuilder::GetOrCreateFunction(const std::string &str, TyIdx retTyIdx)
22 {
23     GStrIdx strIdx = GetStringIndex(str);
24     MIRSymbol *funcSt = nullptr;
25     if (strIdx != 0u) {
26         funcSt = GlobalTables::GetGsymTable().GetSymbolFromStrIdx(strIdx);
27         if (funcSt == nullptr) {
28             funcSt = CreateSymbol(TyIdx(0), strIdx, kStFunc, kScText, nullptr, kScopeGlobal);
29         } else {
30             DEBUG_ASSERT(funcSt->GetSKind() == kStFunc, "runtime check error");
31             return funcSt->GetFunction();
32         }
33     } else {
34         strIdx = GetOrCreateStringIndex(str);
35         funcSt = CreateSymbol(TyIdx(0), strIdx, kStFunc, kScText, nullptr, kScopeGlobal);
36     }
37     auto *fn = mirModule->GetMemPool()->New<MIRFunction>(mirModule, funcSt->GetStIdx());
38     fn->SetPuidx(GlobalTables::GetFunctionTable().GetFuncTable().size());
39     MIRFuncType funcType;
40     funcType.SetRetTyIdx(retTyIdx);
41     auto funcTyIdx = GlobalTables::GetTypeTable().GetOrCreateMIRType(&funcType);
42     auto *funcTypeInTypeTable = static_cast<MIRFuncType *>(GlobalTables::GetTypeTable().GetTypeFromTyIdx(funcTyIdx));
43     fn->SetMIRFuncType(funcTypeInTypeTable);
44     fn->SetReturnTyIdx(retTyIdx);
45     GlobalTables::GetFunctionTable().GetFuncTable().push_back(fn);
46     funcSt->SetFunction(fn);
47     funcSt->SetTyIdx(funcTyIdx);
48     return fn;
49 }
50 
GetFunctionFromSymbol(const MIRSymbol & funcSymbol)51 MIRFunction *MIRBuilder::GetFunctionFromSymbol(const MIRSymbol &funcSymbol)
52 {
53     DEBUG_ASSERT(funcSymbol.GetSKind() == kStFunc, "Symbol %s is not a function symbol", funcSymbol.GetName().c_str());
54     return funcSymbol.GetFunction();
55 }
56 
GetFunctionFromName(const std::string & str)57 MIRFunction *MIRBuilder::GetFunctionFromName(const std::string &str)
58 {
59     auto *funcSymbol =
60         GlobalTables::GetGsymTable().GetSymbolFromStrIdx(GlobalTables::GetStrTable().GetStrIdxFromName(str));
61     return funcSymbol != nullptr ? GetFunctionFromSymbol(*funcSymbol) : nullptr;
62 }
63 
CreateFunction(const std::string & name,const MIRType & returnType,const ArgVector & arguments,bool isVarg,bool createBody) const64 MIRFunction *MIRBuilder::CreateFunction(const std::string &name, const MIRType &returnType, const ArgVector &arguments,
65                                         bool isVarg, bool createBody) const
66 {
67     MIRSymbol *funcSymbol = GlobalTables::GetGsymTable().CreateSymbol(kScopeGlobal);
68     GStrIdx strIdx = GetOrCreateStringIndex(name);
69     funcSymbol->SetNameStrIdx(strIdx);
70     if (!GlobalTables::GetGsymTable().AddToStringSymbolMap(*funcSymbol)) {
71         return nullptr;
72     }
73     funcSymbol->SetStorageClass(kScText);
74     funcSymbol->SetSKind(kStFunc);
75     auto *fn = mirModule->GetMemPool()->New<MIRFunction>(mirModule, funcSymbol->GetStIdx());
76     fn->SetPuidx(GlobalTables::GetFunctionTable().GetFuncTable().size());
77     GlobalTables::GetFunctionTable().GetFuncTable().push_back(fn);
78     std::vector<TyIdx> funcVecType;
79     std::vector<TypeAttrs> funcVecAttrs;
80     for (size_t i = 0; i < arguments.size(); ++i) {
81         MIRType *ty = arguments[i].second;
82         FormalDef formalDef(GetOrCreateStringIndex(arguments[i].first.c_str()), nullptr, ty->GetTypeIndex(),
83                             TypeAttrs());
84         fn->GetFormalDefVec().push_back(formalDef);
85         funcVecType.push_back(ty->GetTypeIndex());
86         funcVecAttrs.push_back(TypeAttrs());
87         if (fn->GetSymTab() != nullptr && formalDef.formalSym != nullptr) {
88             (void)fn->GetSymTab()->AddToStringSymbolMap(*formalDef.formalSym);
89         }
90     }
91     funcSymbol->SetTyIdx(GlobalTables::GetTypeTable()
92                              .GetOrCreateFunctionType(returnType.GetTypeIndex(), funcVecType, funcVecAttrs, isVarg)
93                              ->GetTypeIndex());
94     auto *funcType = static_cast<MIRFuncType *>(funcSymbol->GetType());
95     fn->SetMIRFuncType(funcType);
96     funcSymbol->SetFunction(fn);
97     if (createBody) {
98         fn->NewBody();
99     }
100     return fn;
101 }
102 
GetOrCreateLocalDecl(const std::string & str,TyIdx tyIdx,MIRSymbolTable & symbolTable,bool & created) const103 MIRSymbol *MIRBuilder::GetOrCreateLocalDecl(const std::string &str, TyIdx tyIdx, MIRSymbolTable &symbolTable,
104                                             bool &created) const
105 {
106     GStrIdx strIdx = GetStringIndex(str);
107     if (strIdx != 0u) {
108         StIdx stIdx = symbolTable.GetStIdxFromStrIdx(strIdx);
109         if (stIdx.Idx() != 0) {
110             created = false;
111             return symbolTable.GetSymbolFromStIdx(stIdx.Idx());
112         }
113     }
114     created = true;
115     strIdx = GetOrCreateStringIndex(str);
116     MIRSymbol *st = symbolTable.CreateSymbol(kScopeLocal);
117     DEBUG_ASSERT(st != nullptr, "null ptr check");
118     st->SetNameStrIdx(strIdx);
119     st->SetTyIdx(tyIdx);
120     (void)symbolTable.AddToStringSymbolMap(*st);
121     return st;
122 }
123 
GetOrCreateDeclInFunc(const std::string & str,const MIRType & type,MIRFunction & func)124 MIRSymbol *MIRBuilder::GetOrCreateDeclInFunc(const std::string &str, const MIRType &type, MIRFunction &func)
125 {
126     MIRSymbolTable *symbolTable = func.GetSymTab();
127     DEBUG_ASSERT(symbolTable != nullptr, "symbol_table is null");
128     bool isCreated = false;
129     MIRSymbol *st = GetOrCreateLocalDecl(str, type.GetTypeIndex(), *symbolTable, isCreated);
130     DEBUG_ASSERT(st != nullptr, "st is null");
131     if (isCreated) {
132         st->SetStorageClass(kScAuto);
133         st->SetSKind(kStVar);
134     }
135     return st;
136 }
137 
GetOrCreateLocalDecl(const std::string & str,const MIRType & type)138 MIRSymbol *MIRBuilder::GetOrCreateLocalDecl(const std::string &str, const MIRType &type)
139 {
140     MIRFunction *currentFunc = GetCurrentFunction();
141     CHECK_FATAL(currentFunc != nullptr, "null ptr check");
142     return GetOrCreateDeclInFunc(str, type, *currentFunc);
143 }
144 
GetLocalDecl(const std::string & str)145 MIRSymbol *MIRBuilder::GetLocalDecl(const std::string &str)
146 {
147     MIRFunction *currentFunctionInner = GetCurrentFunctionNotNull();
148     return MIRSymbolBuilder::Instance().GetLocalDecl(*currentFunctionInner->GetSymTab(), GetStringIndex(str));
149 }
150 
151 // when func is null, create global symbol, otherwise create local symbol
CreateSymbol(TyIdx tyIdx,const std::string & name,MIRSymKind mClass,MIRStorageClass sClass,MIRFunction * func,uint8 scpID) const152 MIRSymbol *MIRBuilder::CreateSymbol(TyIdx tyIdx, const std::string &name, MIRSymKind mClass, MIRStorageClass sClass,
153                                     MIRFunction *func, uint8 scpID) const
154 {
155     return CreateSymbol(tyIdx, GetOrCreateStringIndex(name), mClass, sClass, func, scpID);
156 }
157 
158 // when func is null, create global symbol, otherwise create local symbol
CreateSymbol(TyIdx tyIdx,GStrIdx strIdx,MIRSymKind mClass,MIRStorageClass sClass,MIRFunction * func,uint8 scpID) const159 MIRSymbol *MIRBuilder::CreateSymbol(TyIdx tyIdx, GStrIdx strIdx, MIRSymKind mClass, MIRStorageClass sClass,
160                                     MIRFunction *func, uint8 scpID) const
161 {
162     return MIRSymbolBuilder::Instance().CreateSymbol(tyIdx, strIdx, mClass, sClass, func, scpID);
163 }
164 
CreatePregFormalSymbol(TyIdx tyIdx,PregIdx pRegIdx,MIRFunction & func) const165 MIRSymbol *MIRBuilder::CreatePregFormalSymbol(TyIdx tyIdx, PregIdx pRegIdx, MIRFunction &func) const
166 {
167     return MIRSymbolBuilder::Instance().CreatePregFormalSymbol(tyIdx, pRegIdx, func);
168 }
169 
CreateConstval(MIRConst * mirConst)170 ConstvalNode *MIRBuilder::CreateConstval(MIRConst *mirConst)
171 {
172     return NewNode<ConstvalNode>(mirConst->GetType().GetPrimType(), mirConst);
173 }
174 
CreateIntConst(uint64 val,PrimType pty)175 ConstvalNode *MIRBuilder::CreateIntConst(uint64 val, PrimType pty)
176 {
177     auto *mirConst =
178         GlobalTables::GetIntConstTable().GetOrCreateIntConst(val, *GlobalTables::GetTypeTable().GetPrimType(pty));
179     return NewNode<ConstvalNode>(pty, mirConst);
180 }
181 
CreateExprAddrof(FieldID fieldID,const MIRSymbol & symbol,MemPool * memPool)182 AddrofNode *MIRBuilder::CreateExprAddrof(FieldID fieldID, const MIRSymbol &symbol, MemPool *memPool)
183 {
184     return CreateExprAddrof(fieldID, symbol.GetStIdx(), memPool);
185 }
186 
CreateExprAddrof(FieldID fieldID,StIdx symbolStIdx,MemPool * memPool)187 AddrofNode *MIRBuilder::CreateExprAddrof(FieldID fieldID, StIdx symbolStIdx, MemPool *memPool)
188 {
189     if (memPool == nullptr) {
190         memPool = GetCurrentFuncCodeMp();
191     }
192     auto node = memPool->New<AddrofNode>(OP_addrof, PTY_ptr, symbolStIdx, fieldID);
193     node->SetDebugComment(currComment);
194     return node;
195 }
196 
CreateExprDread(const MIRType & type,FieldID fieldID,const MIRSymbol & symbol)197 AddrofNode *MIRBuilder::CreateExprDread(const MIRType &type, FieldID fieldID, const MIRSymbol &symbol)
198 {
199     return CreateExprDread(type.GetPrimType(), fieldID, symbol);
200 }
201 
CreateExprDread(PrimType ptyp,FieldID fieldID,const MIRSymbol & symbol)202 AddrofNode *MIRBuilder::CreateExprDread(PrimType ptyp, FieldID fieldID, const MIRSymbol &symbol)
203 {
204     auto *node = NewNode<AddrofNode>(OP_dread, kPtyInvalid, symbol.GetStIdx(), fieldID);
205     node->SetPrimType(GetRegPrimType(ptyp));
206     return node;
207 }
208 
CreateExprRegread(PrimType pty,PregIdx regIdx)209 RegreadNode *MIRBuilder::CreateExprRegread(PrimType pty, PregIdx regIdx)
210 {
211     return NewNode<RegreadNode>(pty, regIdx);
212 }
213 
214 
CreateExprDread(MIRSymbol & symbol)215 AddrofNode *MIRBuilder::CreateExprDread(MIRSymbol &symbol)
216 {
217     return CreateExprDread(*symbol.GetType(), 0, symbol);
218 }
219 
CreateExprIread(const MIRType & returnType,const MIRType & ptrType,FieldID fieldID,BaseNode * addr)220 IreadNode *MIRBuilder::CreateExprIread(const MIRType &returnType, const MIRType &ptrType, FieldID fieldID,
221                                        BaseNode *addr)
222 {
223     TyIdx returnTypeIdx = returnType.GetTypeIndex();
224     CHECK(returnTypeIdx < GlobalTables::GetTypeTable().GetTypeTable().size(),
225           "index out of range in MIRBuilder::CreateExprIread");
226     PrimType type = GetRegPrimType(returnType.GetPrimType());
227     return NewNode<IreadNode>(OP_iread, type, ptrType.GetTypeIndex(), fieldID, addr);
228 }
229 
CreateExprUnary(Opcode opcode,const MIRType & type,BaseNode * opnd)230 UnaryNode *MIRBuilder::CreateExprUnary(Opcode opcode, const MIRType &type, BaseNode *opnd)
231 {
232     return NewNode<UnaryNode>(opcode, type.GetPrimType(), opnd);
233 }
234 
CreateExprTypeCvt(Opcode o,PrimType toPrimType,PrimType fromPrimType,BaseNode & opnd)235 TypeCvtNode *MIRBuilder::CreateExprTypeCvt(Opcode o, PrimType toPrimType, PrimType fromPrimType, BaseNode &opnd)
236 {
237     return NewNode<TypeCvtNode>(o, toPrimType, fromPrimType, &opnd);
238 }
239 
CreateExprTypeCvt(Opcode o,const MIRType & type,const MIRType & fromType,BaseNode * opnd)240 TypeCvtNode *MIRBuilder::CreateExprTypeCvt(Opcode o, const MIRType &type, const MIRType &fromType, BaseNode *opnd)
241 {
242     return CreateExprTypeCvt(o, type.GetPrimType(), fromType.GetPrimType(), *opnd);
243 }
244 
CreateExprExtractbits(Opcode o,PrimType type,uint32 bOffset,uint32 bSize,BaseNode * opnd)245 ExtractbitsNode *MIRBuilder::CreateExprExtractbits(Opcode o, PrimType type, uint32 bOffset, uint32 bSize,
246                                                    BaseNode *opnd)
247 {
248     return NewNode<ExtractbitsNode>(o, type, bOffset, bSize, opnd);
249 }
250 
CreateExprRetype(const MIRType & type,const MIRType & fromType,BaseNode * opnd)251 RetypeNode *MIRBuilder::CreateExprRetype(const MIRType &type, const MIRType &fromType, BaseNode *opnd)
252 {
253     return CreateExprRetype(type, fromType.GetPrimType(), opnd);
254 }
255 
CreateExprRetype(const MIRType & type,PrimType fromType,BaseNode * opnd)256 RetypeNode *MIRBuilder::CreateExprRetype(const MIRType &type, PrimType fromType, BaseNode *opnd)
257 {
258     return NewNode<RetypeNode>(type.GetPrimType(), fromType, type.GetTypeIndex(), opnd);
259 }
260 
CreateExprBinary(Opcode opcode,const MIRType & type,BaseNode * opnd0,BaseNode * opnd1)261 BinaryNode *MIRBuilder::CreateExprBinary(Opcode opcode, const MIRType &type, BaseNode *opnd0, BaseNode *opnd1)
262 {
263     return NewNode<BinaryNode>(opcode, type.GetPrimType(), opnd0, opnd1);
264 }
265 
CreateExprCompare(Opcode opcode,const MIRType & type,const MIRType & opndType,BaseNode * opnd0,BaseNode * opnd1)266 CompareNode *MIRBuilder::CreateExprCompare(Opcode opcode, const MIRType &type, const MIRType &opndType, BaseNode *opnd0,
267                                            BaseNode *opnd1)
268 {
269     return NewNode<CompareNode>(opcode, type.GetPrimType(), opndType.GetPrimType(), opnd0, opnd1);
270 }
271 
CreateExprIntrinsicop(MIRIntrinsicID id,Opcode op,PrimType primType,TyIdx tyIdx,const MapleVector<BaseNode * > & ops)272 IntrinsicopNode *MIRBuilder::CreateExprIntrinsicop(MIRIntrinsicID id, Opcode op, PrimType primType, TyIdx tyIdx,
273                                                    const MapleVector<BaseNode *> &ops)
274 {
275     auto *expr = NewNode<IntrinsicopNode>(*GetCurrentFuncCodeMpAllocator(), op, primType);
276     expr->SetIntrinsic(id);
277     expr->SetNOpnd(ops);
278     expr->SetNumOpnds(ops.size());
279     return expr;
280 }
281 
CreateExprIntrinsicop(MIRIntrinsicID idx,Opcode opCode,const MIRType & type,const MapleVector<BaseNode * > & ops)282 IntrinsicopNode *MIRBuilder::CreateExprIntrinsicop(MIRIntrinsicID idx, Opcode opCode, const MIRType &type,
283                                                    const MapleVector<BaseNode *> &ops)
284 {
285     return CreateExprIntrinsicop(idx, opCode, type.GetPrimType(), type.GetTypeIndex(), ops);
286 }
287 
CreateStmtDassign(const MIRSymbol & symbol,FieldID fieldID,BaseNode * src)288 DassignNode *MIRBuilder::CreateStmtDassign(const MIRSymbol &symbol, FieldID fieldID, BaseNode *src)
289 {
290     return NewNode<DassignNode>(src, symbol.GetStIdx(), fieldID);
291 }
292 
CreateStmtRegassign(PrimType pty,PregIdx regIdx,BaseNode * src)293 RegassignNode *MIRBuilder::CreateStmtRegassign(PrimType pty, PregIdx regIdx, BaseNode *src)
294 {
295     return NewNode<RegassignNode>(pty, regIdx, src);
296 }
297 
CreateStmtDassign(StIdx sIdx,FieldID fieldID,BaseNode * src)298 DassignNode *MIRBuilder::CreateStmtDassign(StIdx sIdx, FieldID fieldID, BaseNode *src)
299 {
300     return NewNode<DassignNode>(src, sIdx, fieldID);
301 }
302 
CreateStmtIassign(const MIRType & type,FieldID fieldID,BaseNode * addr,BaseNode * src)303 IassignNode *MIRBuilder::CreateStmtIassign(const MIRType &type, FieldID fieldID, BaseNode *addr, BaseNode *src)
304 {
305     return NewNode<IassignNode>(type.GetTypeIndex(), fieldID, addr, src);
306 }
307 
CreateStmtCall(PUIdx puIdx,const MapleVector<BaseNode * > & args,Opcode opCode)308 CallNode *MIRBuilder::CreateStmtCall(PUIdx puIdx, const MapleVector<BaseNode *> &args, Opcode opCode)
309 {
310     auto *stmt = NewNode<CallNode>(*GetCurrentFuncCodeMpAllocator(), opCode, puIdx, TyIdx());
311     stmt->SetNOpnd(args);
312     stmt->SetNumOpnds(args.size());
313     return stmt;
314 }
315 
CreateStmtTailIcall(const MapleVector<BaseNode * > & args)316 IcallNode *MIRBuilder::CreateStmtTailIcall(const MapleVector<BaseNode *> &args)
317 {
318     auto *stmt = NewNode<IcallNode>(*GetCurrentFuncCodeMpAllocator(), OP_tailicall);
319     DEBUG_ASSERT(stmt != nullptr, "stmt is null");
320     stmt->SetOpnds(args);
321     return stmt;
322 }
323 
CreateStmtIcall(const MapleVector<BaseNode * > & args)324 IcallNode *MIRBuilder::CreateStmtIcall(const MapleVector<BaseNode *> &args)
325 {
326     auto *stmt = NewNode<IcallNode>(*GetCurrentFuncCodeMpAllocator(), OP_icall);
327     DEBUG_ASSERT(stmt != nullptr, "stmt is null");
328     stmt->SetOpnds(args);
329     return stmt;
330 }
331 
CreateStmtIcallproto(const MapleVector<BaseNode * > & args,const TyIdx & prototypeIdx)332 IcallNode *MIRBuilder::CreateStmtIcallproto(const MapleVector<BaseNode *> &args, const TyIdx &prototypeIdx)
333 {
334     auto *stmt = NewNode<IcallNode>(*GetCurrentFuncCodeMpAllocator(), OP_icallproto);
335     DEBUG_ASSERT(stmt != nullptr, "stmt is null");
336     stmt->SetOpnds(args);
337     stmt->SetRetTyIdx(prototypeIdx);
338     return stmt;
339 }
340 
CreateStmtIcallAssigned(const MapleVector<BaseNode * > & args,const MIRSymbol & ret)341 IcallNode *MIRBuilder::CreateStmtIcallAssigned(const MapleVector<BaseNode *> &args, const MIRSymbol &ret)
342 {
343     auto *stmt = NewNode<IcallNode>(*GetCurrentFuncCodeMpAllocator(), OP_icallassigned);
344     CallReturnVector nrets(GetCurrentFuncCodeMpAllocator()->Adapter());
345     CHECK_FATAL((ret.GetStorageClass() == kScAuto || ret.GetStorageClass() == kScFormal ||
346                  ret.GetStorageClass() == kScExtern || ret.GetStorageClass() == kScGlobal),
347                 "unknown classtype! check it!");
348     nrets.emplace_back(CallReturnPair(ret.GetStIdx(), RegFieldPair(0, 0)));
349     stmt->SetNumOpnds(args.size());
350     stmt->GetNopnd().resize(stmt->GetNumOpnds());
351     stmt->SetReturnVec(nrets);
352     for (size_t i = 0; i < stmt->GetNopndSize(); ++i) {
353         stmt->SetNOpndAt(i, args.at(i));
354     }
355     stmt->SetRetTyIdx(ret.GetTyIdx());
356     return stmt;
357 }
358 
CreateStmtIcallAssigned(const MapleVector<BaseNode * > & args,PregIdx pregIdx)359 IcallNode *MIRBuilder::CreateStmtIcallAssigned(const MapleVector<BaseNode *> &args, PregIdx pregIdx)
360 {
361     auto *stmt = NewNode<IcallNode>(*GetCurrentFuncCodeMpAllocator(), OP_icallassigned);
362     CallReturnVector nrets(GetCurrentFuncCodeMpAllocator()->Adapter());
363     nrets.emplace_back(StIdx(), RegFieldPair(0, pregIdx));
364     stmt->SetNumOpnds(args.size());
365     stmt->GetNopnd().resize(stmt->GetNumOpnds());
366     stmt->SetReturnVec(nrets);
367     for (size_t i = 0; i < stmt->GetNopndSize(); ++i) {
368         stmt->SetNOpndAt(i, args.at(i));
369     }
370     auto *preg = GetCurrentFunction()->GetPregTab()->PregFromPregIdx(pregIdx);
371     DEBUG_ASSERT(preg, "preg should be created before used");
372     if (preg->GetMIRType() == nullptr) {
373         stmt->SetRetTyIdx(TyIdx(preg->GetPrimType()));
374     } else {
375         stmt->SetRetTyIdx(preg->GetMIRType()->GetTypeIndex());
376     }
377     return stmt;
378 }
379 
CreateStmtIntrinsicCall(MIRIntrinsicID idx,const MapleVector<BaseNode * > & arguments,TyIdx tyIdx)380 IntrinsiccallNode *MIRBuilder::CreateStmtIntrinsicCall(MIRIntrinsicID idx, const MapleVector<BaseNode *> &arguments,
381                                                        TyIdx tyIdx)
382 {
383     auto *stmt = NewNode<IntrinsiccallNode>(
384         *GetCurrentFuncCodeMpAllocator(), tyIdx == 0u ? OP_intrinsiccall : OP_intrinsiccallwithtype, idx);
385     stmt->SetTyIdx(tyIdx);
386     stmt->SetOpnds(arguments);
387     return stmt;
388 }
389 
CreateStmtCallRegassigned(PUIdx puIdx,const MapleVector<BaseNode * > & args,PregIdx pRegIdx,Opcode opcode)390 CallNode *MIRBuilder::CreateStmtCallRegassigned(PUIdx puIdx, const MapleVector<BaseNode *> &args, PregIdx pRegIdx,
391                                                 Opcode opcode)
392 {
393     auto *stmt = NewNode<CallNode>(*GetCurrentFuncCodeMpAllocator(), opcode, puIdx);
394     DEBUG_ASSERT(stmt != nullptr, "stmt is null");
395     stmt->SetOpnds(args);
396     if (pRegIdx > 0) {
397         stmt->GetReturnVec().push_back(CallReturnPair(StIdx(), RegFieldPair(0, pRegIdx)));
398     }
399     return stmt;
400 }
401 
CreateStmtIntrinsicCallAssigned(MIRIntrinsicID idx,const MapleVector<BaseNode * > & args,PregIdx retPregIdx1,PregIdx retPregIdx2)402 IntrinsiccallNode *MIRBuilder::CreateStmtIntrinsicCallAssigned(MIRIntrinsicID idx, const MapleVector<BaseNode *> &args,
403                                                                PregIdx retPregIdx1, PregIdx retPregIdx2)
404 {
405     auto *stmt =
406         NewNode<IntrinsiccallNode>(*GetCurrentFuncCodeMpAllocator(), OP_intrinsiccallassigned, idx);
407     DEBUG_ASSERT(stmt != nullptr, "stmt is null");
408     stmt->SetOpnds(args);
409     if (retPregIdx1 > 0) {
410         stmt->GetReturnVec().push_back(CallReturnPair(StIdx(), RegFieldPair(0, retPregIdx1)));
411     }
412     if (retPregIdx2 > 0) {
413         stmt->GetReturnVec().push_back(CallReturnPair(StIdx(), RegFieldPair(0, retPregIdx2)));
414     }
415     return stmt;
416 }
417 
CreateStmtReturn(BaseNode * rVal)418 NaryStmtNode *MIRBuilder::CreateStmtReturn(BaseNode *rVal)
419 {
420     auto *stmt = NewNode<NaryStmtNode>(*GetCurrentFuncCodeMpAllocator(), OP_return);
421     DEBUG_ASSERT(stmt != nullptr, "stmt is null");
422     stmt->PushOpnd(rVal);
423     return stmt;
424 }
425 
CreateStmtUnary(Opcode op,BaseNode * rVal)426 UnaryStmtNode *MIRBuilder::CreateStmtUnary(Opcode op, BaseNode *rVal)
427 {
428     return NewNode<UnaryStmtNode>(op, kPtyInvalid, rVal);
429 }
430 
CreateStmtIf(BaseNode * cond)431 IfStmtNode *MIRBuilder::CreateStmtIf(BaseNode *cond)
432 {
433     auto *ifStmt = NewNode<IfStmtNode>();
434     ifStmt->SetOpnd(cond, 0);
435     BlockNode *thenBlock = NewNode<BlockNode>();
436     ifStmt->SetThenPart(thenBlock);
437     return ifStmt;
438 }
439 
CreateStmtSwitch(BaseNode * opnd,LabelIdx defaultLabel,const CaseVector & switchTable)440 SwitchNode *MIRBuilder::CreateStmtSwitch(BaseNode *opnd, LabelIdx defaultLabel, const CaseVector &switchTable)
441 {
442     auto *switchNode = NewNode<SwitchNode>(*GetCurrentFuncCodeMpAllocator(), defaultLabel, opnd);
443     switchNode->SetSwitchTable(switchTable);
444     return switchNode;
445 }
446 
CreateStmtGoto(Opcode o,LabelIdx labIdx)447 GotoNode *MIRBuilder::CreateStmtGoto(Opcode o, LabelIdx labIdx)
448 {
449     return NewNode<GotoNode>(o, labIdx);
450 }
451 
CreateStmtLabel(LabelIdx labIdx)452 LabelNode *MIRBuilder::CreateStmtLabel(LabelIdx labIdx)
453 {
454     return NewNode<LabelNode>(labIdx);
455 }
456 
CreateStmtComment(const std::string & cmnt)457 StmtNode *MIRBuilder::CreateStmtComment(const std::string &cmnt)
458 {
459     return NewNode<CommentNode>(*GetCurrentFuncCodeMpAllocator(), cmnt);
460 }
461 
CreateStmtCondGoto(BaseNode * cond,Opcode op,LabelIdx labIdx)462 CondGotoNode *MIRBuilder::CreateStmtCondGoto(BaseNode *cond, Opcode op, LabelIdx labIdx)
463 {
464     return NewNode<CondGotoNode>(op, labIdx, cond);
465 }
466 
GetCurrentFuncCodeMp()467 MemPool *MIRBuilder::GetCurrentFuncCodeMp()
468 {
469     if (MIRFunction *curFunction = GetCurrentFunction()) {
470         return curFunction->GetCodeMemPool();
471     }
472     return mirModule->GetMemPool();
473 }
474 
GetCurrentFuncCodeMpAllocator()475 MapleAllocator *MIRBuilder::GetCurrentFuncCodeMpAllocator()
476 {
477     if (MIRFunction *curFunction = GetCurrentFunction()) {
478         return &curFunction->GetCodeMPAllocator();
479     }
480     return &mirModule->GetMPAllocator();
481 }
482 
MIRBuilderExt(MIRModule * module)483 MIRBuilderExt::MIRBuilderExt(MIRModule *module) : MIRBuilder(module) {}
484 }  // namespace maple
485