• 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_symbol_builder.h"
17 
18 namespace maple {
GetLocalDecl(const MIRSymbolTable & symbolTable,const GStrIdx & strIdx) const19 MIRSymbol *MIRSymbolBuilder::GetLocalDecl(const MIRSymbolTable &symbolTable, const GStrIdx &strIdx) const
20 {
21     if (strIdx != 0u) {
22         const StIdx stIdx = symbolTable.GetStIdxFromStrIdx(strIdx);
23         if (stIdx.FullIdx() != 0) {
24             return symbolTable.GetSymbolFromStIdx(stIdx.Idx());
25         }
26     }
27     return nullptr;
28 }
29 
CreateLocalDecl(MIRSymbolTable & symbolTable,GStrIdx strIdx,const MIRType & type) const30 MIRSymbol *MIRSymbolBuilder::CreateLocalDecl(MIRSymbolTable &symbolTable, GStrIdx strIdx, const MIRType &type) const
31 {
32     MIRSymbol *st = symbolTable.CreateSymbol(kScopeLocal);
33     st->SetNameStrIdx(strIdx);
34     st->SetTyIdx(type.GetTypeIndex());
35     (void)symbolTable.AddToStringSymbolMap(*st);
36     st->SetStorageClass(kScAuto);
37     st->SetSKind(kStVar);
38     return st;
39 }
40 
GetGlobalDecl(GStrIdx strIdx) const41 MIRSymbol *MIRSymbolBuilder::GetGlobalDecl(GStrIdx strIdx) const
42 {
43     if (strIdx != 0u) {
44         const StIdx stIdx = GlobalTables::GetGsymTable().GetStIdxFromStrIdx(strIdx);
45         if (stIdx.FullIdx() != 0) {
46             return GlobalTables::GetGsymTable().GetSymbolFromStidx(stIdx.Idx());
47         }
48     }
49     return nullptr;
50 }
51 
CreateGlobalDecl(GStrIdx strIdx,const MIRType & type,MIRStorageClass sc) const52 MIRSymbol *MIRSymbolBuilder::CreateGlobalDecl(GStrIdx strIdx, const MIRType &type, MIRStorageClass sc) const
53 {
54     MIRSymbol *st = GlobalTables::GetGsymTable().CreateSymbol(kScopeGlobal);
55     st->SetNameStrIdx(strIdx);
56     st->SetTyIdx(type.GetTypeIndex());
57     (void)GlobalTables::GetGsymTable().AddToStringSymbolMap(*st);
58     st->SetStorageClass(sc);
59     st->SetSKind(kStVar);
60     return st;
61 }
62 
63 // when sametype is true, it means match everything the of the symbol
GetSymbol(TyIdx tyIdx,GStrIdx strIdx,MIRSymKind mClass,MIRStorageClass sClass,bool sameType) const64 MIRSymbol *MIRSymbolBuilder::GetSymbol(TyIdx tyIdx, GStrIdx strIdx, MIRSymKind mClass, MIRStorageClass sClass,
65                                        bool sameType) const
66 {
67     MIRSymbol *st = GlobalTables::GetGsymTable().GetSymbolFromStrIdx(strIdx);
68     if (st == nullptr || st->GetTyIdx() != tyIdx) {
69         return nullptr;
70     }
71 
72     if (sameType) {
73         if (st->GetStorageClass() == sClass && st->GetSKind() == mClass) {
74             return st;
75         }
76         return nullptr;
77     }
78     DEBUG_ASSERT(mClass == st->GetSKind(),
79                  "trying to create a new symbol that has the same name and GtyIdx. might cause problem");
80     DEBUG_ASSERT(sClass == st->GetStorageClass(),
81                  "trying to create a new symbol that has the same name and tyIdx. might cause problem");
82     return st;
83 }
84 
85 // when func is null, create global symbol, otherwise create local symbol
CreateSymbol(TyIdx tyIdx,GStrIdx strIdx,MIRSymKind mClass,MIRStorageClass sClass,MIRFunction * func,uint8 scpID) const86 MIRSymbol *MIRSymbolBuilder::CreateSymbol(TyIdx tyIdx, GStrIdx strIdx, MIRSymKind mClass, MIRStorageClass sClass,
87                                           MIRFunction *func, uint8 scpID) const
88 {
89     MIRSymbol *st =
90         (func != nullptr) ? func->GetSymTab()->CreateSymbol(scpID) : GlobalTables::GetGsymTable().CreateSymbol(scpID);
91     CHECK_FATAL(st != nullptr, "Failed to create MIRSymbol");
92     st->SetStorageClass(sClass);
93     st->SetSKind(mClass);
94     st->SetNameStrIdx(strIdx);
95     st->SetTyIdx(tyIdx);
96     if (func != nullptr) {
97         (void)func->GetSymTab()->AddToStringSymbolMap(*st);
98     } else {
99         (void)GlobalTables::GetGsymTable().AddToStringSymbolMap(*st);
100     }
101     return st;
102 }
103 
CreatePregFormalSymbol(TyIdx tyIdx,PregIdx pRegIdx,MIRFunction & func) const104 MIRSymbol *MIRSymbolBuilder::CreatePregFormalSymbol(TyIdx tyIdx, PregIdx pRegIdx, MIRFunction &func) const
105 {
106     MIRSymbol *st = func.GetSymTab()->CreateSymbol(kScopeLocal);
107     CHECK_FATAL(st != nullptr, "Failed to create MIRSymbol");
108     st->SetStorageClass(kScFormal);
109     st->SetSKind(kStPreg);
110     st->SetTyIdx(tyIdx);
111     MIRPregTable *pregTab = func.GetPregTab();
112     st->SetPreg(pregTab->PregFromPregIdx(pRegIdx));
113     return st;
114 }
115 
GetSymbolTableSize(const MIRFunction * func) const116 size_t MIRSymbolBuilder::GetSymbolTableSize(const MIRFunction *func) const
117 {
118     return (func == nullptr) ? GlobalTables::GetGsymTable().GetSymbolTableSize()
119                              : func->GetSymTab()->GetSymbolTableSize();
120 }
121 
GetSymbolFromStIdx(uint32 idx,const MIRFunction * func) const122 const MIRSymbol *MIRSymbolBuilder::GetSymbolFromStIdx(uint32 idx, const MIRFunction *func) const
123 {
124     if (func == nullptr) {
125         auto &symTab = GlobalTables::GetGsymTable();
126         return symTab.GetSymbolFromStidx(idx);
127     } else {
128         auto &symTab = *func->GetSymTab();
129         return symTab.GetSymbolFromStIdx(idx);
130     }
131 }
132 }  // namespace maple
133