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 <set>
17 #include "emit.h"
18 
19 namespace maplebe {
20 using namespace maple;
21 
22 uint32 VregInfo::virtualRegCount = kBaseVirtualRegNO;
23 uint32 VregInfo::maxRegCount = 0;
24 std::vector<VirtualRegNode> VregInfo::vRegTable;
25 std::unordered_map<regno_t, RegOperand *> VregInfo::vRegOperandTable;
26 /*  There are two builders, cgfunc builder (original code selector) and irbuilder (abstract).
27  *  This is to prevent conflict between the two for VregInfo as for arm64 both co-exists.
28  *  When switching to irbuilder completely, then this bool can go away.
29  */
30 bool VregInfo::initialized = false;
31 
SetTarget(CG &target)32 void Globals::SetTarget(CG &target)
33 {
34     cg = &target;
35 }
GetTarget() const36 const CG *Globals::GetTarget() const
37 {
38     DEBUG_ASSERT(cg, " set target info please ");
39     return cg;
40 }
41 
42 CGFunc *CG::currentCGFunction = nullptr;
43 std::map<MIRFunction *, std::pair<LabelIdx, LabelIdx>> CG::funcWrapLabels;
44 
~CG()45 CG::~CG()
46 {
47     Emit([](Emitter *emitter) {
48         emitter->CloseOutput();
49     });
50     delete memPool;
51     memPool = nullptr;
52     mirModule = nullptr;
53     emitters.clear();
54     currentCGFunction = nullptr;
55     dbgTraceEnter = nullptr;
56     dbgTraceExit = nullptr;
57     dbgFuncProfile = nullptr;
58 }
59 
EmitAllEmitters(const std::function<void(Emitter *)>& cb) const60 void CG::EmitAllEmitters(const std::function<void(Emitter *)>& cb) const
61 {
62     DEBUG_ASSERT(!emitters.empty(), "Emitter were not set");
63     DEBUG_ASSERT(emitters.size() <= 2U, "Emitters number isn't correct");
64     for (auto emitter: emitters) {
65         cb(emitter);
66     }
67 }
68 
EmitAsmEmitters(const std::function<void(Emitter *)>& cb) const69 void CG::EmitAsmEmitters(const std::function<void(Emitter *)>& cb) const
70 {
71     if (emitters.size() == 2U) {
72         cb(emitters[1]);
73     }
74 }
75 
EmitObjEmitters(const std::function<void(Emitter *)>& cb) const76 void CG::EmitObjEmitters(const std::function<void(Emitter *)>& cb) const
77 {
78     DEBUG_ASSERT(!emitters.empty(), "ObjEmmiter wasn't set");
79     cb(emitters[0]);
80 }
81 
82 } /* namespace maplebe */
83