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 MAPLE_IR_INCLUDE_MIR_SCOPE_H 17 #define MAPLE_IR_INCLUDE_MIR_SCOPE_H 18 #include "mir_module.h" 19 #include "mir_type.h" 20 #include "src_position.h" 21 22 namespace maple { 23 // mapping src variable to mpl variables to display debug info 24 struct MIRAliasVars { 25 GStrIdx mplStrIdx; // maple varialbe name 26 TyIdx tyIdx; 27 bool isLocal; 28 GStrIdx sigStrIdx; 29 }; 30 31 class MIRScope { 32 public: MIRScope(MIRModule * mod)33 explicit MIRScope(MIRModule *mod) : module(mod) {} MIRScope(MIRModule * mod,unsigned l)34 MIRScope(MIRModule *mod, unsigned l) : module(mod), level(l) {} 35 ~MIRScope() = default; 36 NeedEmitAliasInfo()37 bool NeedEmitAliasInfo() const 38 { 39 return aliasVarMap.size() != 0 || subScopes.size() != 0; 40 } 41 42 bool IsSubScope(const MIRScope *scp) const; 43 bool HasJoinScope(const MIRScope *scp1, const MIRScope *scp2) const; 44 bool HasSameRange(const MIRScope *s1, const MIRScope *s2) const; 45 GetLevel()46 unsigned GetLevel() const 47 { 48 return level; 49 } 50 GetRangeLow()51 const SrcPosition &GetRangeLow() const 52 { 53 return range.first; 54 } 55 GetRangeHigh()56 const SrcPosition &GetRangeHigh() const 57 { 58 return range.second; 59 } 60 SetRange(SrcPosition low,SrcPosition high)61 void SetRange(SrcPosition low, SrcPosition high) 62 { 63 DEBUG_ASSERT(low.IsBfOrEq(high), "wrong order of low and high"); 64 range.first = low; 65 range.second = high; 66 } 67 SetAliasVarMap(GStrIdx idx,const MIRAliasVars & vars)68 void SetAliasVarMap(GStrIdx idx, const MIRAliasVars &vars) 69 { 70 aliasVarMap[idx] = vars; 71 } 72 AddAliasVarMap(GStrIdx idx,const MIRAliasVars & vars)73 void AddAliasVarMap(GStrIdx idx, const MIRAliasVars &vars) 74 { 75 /* allow same idx, save last aliasVars */ 76 aliasVarMap[idx] = vars; 77 } 78 GetAliasVarMap()79 MapleMap<GStrIdx, MIRAliasVars> &GetAliasVarMap() 80 { 81 return aliasVarMap; 82 } 83 GetSubScopes()84 MapleVector<MIRScope *> &GetSubScopes() 85 { 86 return subScopes; 87 } 88 89 void IncLevel(); 90 bool AddScope(MIRScope *scope); 91 void Dump(int32 indent) const; 92 void Dump() const; 93 94 private: 95 MIRModule *module; 96 unsigned level = 0; 97 std::pair<SrcPosition, SrcPosition> range; 98 // source to maple variable alias 99 MapleMap<GStrIdx, MIRAliasVars> aliasVarMap {module->GetMPAllocator().Adapter()}; 100 MapleVector<MIRScope *> subScopes {module->GetMPAllocator().Adapter()}; 101 }; 102 } // namespace maple 103 #endif // MAPLE_IR_INCLUDE_MIR_SCOPE_H 104