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.h"
17 #include "mir_function.h"
18 #include "printing.h"
19
20 namespace maple {
21 using namespace namemangler;
22
23 uint32 MIRSymbol::lastPrintedLineNum = 0;
24 uint16 MIRSymbol::lastPrintedColumnNum = 0;
25
SetNameStrIdx(const std::string & name)26 void MIRSymbol::SetNameStrIdx(const std::string &name)
27 {
28 nameStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(name);
29 }
30
31 #ifdef ARK_LITECG_DEBUG
HasAddrOfValues() const32 bool MIRSymbol::HasAddrOfValues() const
33 {
34 return StringUtils::StartsWith(GetName(), VTAB_PREFIX_STR) || StringUtils::StartsWith(GetName(), ITAB_PREFIX_STR) ||
35 StringUtils::StartsWith(GetName(), kVtabOffsetTabStr) ||
36 StringUtils::StartsWith(GetName(), kDecoupleStaticKeyStr) || IsClassInitBridge() || IsReflectionInfo() ||
37 IsReflectionHashTabBucket() || IsReflectionStrTab() || IsITabConflictInfo() || IsRegJNITab() ||
38 IsRegJNIFuncTab() || IsLiteral();
39 }
40
IsLiteral() const41 bool MIRSymbol::IsLiteral() const
42 {
43 return StringUtils::StartsWith(GetName(), kConstString);
44 }
45
IsLiteralPtr() const46 bool MIRSymbol::IsLiteralPtr() const
47 {
48 return StringUtils::StartsWith(GetName(), kConstStringPtr);
49 }
50 #endif
51
GetType() const52 MIRType *MIRSymbol::GetType() const
53 {
54 return GlobalTables::GetTypeTable().GetTypeFromTyIdx(tyIdx);
55 }
56
57 #ifdef ARK_LITECG_DEBUG
PointsToConstString() const58 bool MIRSymbol::PointsToConstString() const
59 {
60 MIRType *origType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(tyIdx);
61 if (origType->GetKind() == kTypePointer) {
62 return static_cast<MIRPtrType *>(origType)->PointsToConstString();
63 }
64 return false;
65 }
66
IsConstString() const67 bool MIRSymbol::IsConstString() const
68 {
69 return typeAttrs.GetAttr(ATTR_static) && typeAttrs.GetAttr(ATTR_final) && PointsToConstString();
70 }
71
IsReflectionStrTab() const72 bool MIRSymbol::IsReflectionStrTab() const
73 {
74 return StringUtils::StartsWith(GetName(), kReflectionStrtabPrefixStr) ||
75 StringUtils::StartsWith(GetName(), kReflectionStartHotStrtabPrefixStr) ||
76 StringUtils::StartsWith(GetName(), kReflectionBothHotStrTabPrefixStr) ||
77 StringUtils::StartsWith(GetName(), kReflectionRunHotStrtabPrefixStr) ||
78 StringUtils::StartsWith(GetName(), kReflectionNoEmitStrtabPrefixStr);
79 }
80
IsRegJNITab() const81 bool MIRSymbol::IsRegJNITab() const
82 {
83 return false;
84 }
85
IsRegJNIFuncTab() const86 bool MIRSymbol::IsRegJNIFuncTab() const
87 {
88 return StringUtils::StartsWith(GetName(), kRegJNIFuncTabPrefixStr);
89 }
90
IsMuidTab() const91 bool MIRSymbol::IsMuidTab() const
92 {
93 return StringUtils::StartsWith(GetName(), kMuidPrefixStr);
94 }
95
IsMuidRoTab() const96 bool MIRSymbol::IsMuidRoTab() const
97 {
98 return StringUtils::StartsWith(GetName(), kMuidRoPrefixStr);
99 }
100
IsCodeLayoutInfo() const101 bool MIRSymbol::IsCodeLayoutInfo() const
102 {
103 return StringUtils::StartsWith(GetName(), kFunctionLayoutStr);
104 }
105
GetMuidTabName() const106 std::string MIRSymbol::GetMuidTabName() const
107 {
108 if (!IsMuidTab()) {
109 return "";
110 }
111 size_t idx = GetName().find(kFileNameSplitterStr);
112 return (idx != std::string::npos) ? GetName().substr(0, idx) : "";
113 }
114
IsMuidFuncDefTab() const115 bool MIRSymbol::IsMuidFuncDefTab() const
116 {
117 return StringUtils::StartsWith(GetName(), kMuidFuncDefTabPrefixStr);
118 }
119
IsMuidFuncDefOrigTab() const120 bool MIRSymbol::IsMuidFuncDefOrigTab() const
121 {
122 return StringUtils::StartsWith(GetName(), kMuidFuncDefOrigTabPrefixStr);
123 }
124
IsMuidFuncInfTab() const125 bool MIRSymbol::IsMuidFuncInfTab() const
126 {
127 return StringUtils::StartsWith(GetName(), kMuidFuncInfTabPrefixStr);
128 }
129
IsMuidFuncUndefTab() const130 bool MIRSymbol::IsMuidFuncUndefTab() const
131 {
132 return StringUtils::StartsWith(GetName(), kMuidFuncUndefTabPrefixStr);
133 }
134
IsMuidDataDefTab() const135 bool MIRSymbol::IsMuidDataDefTab() const
136 {
137 return StringUtils::StartsWith(GetName(), kMuidDataDefTabPrefixStr);
138 }
139
IsMuidDataDefOrigTab() const140 bool MIRSymbol::IsMuidDataDefOrigTab() const
141 {
142 return StringUtils::StartsWith(GetName(), kMuidDataDefOrigTabPrefixStr);
143 }
144
IsMuidDataUndefTab() const145 bool MIRSymbol::IsMuidDataUndefTab() const
146 {
147 return StringUtils::StartsWith(GetName(), kMuidDataUndefTabPrefixStr);
148 }
149
IsMuidRangeTab() const150 bool MIRSymbol::IsMuidRangeTab() const
151 {
152 return StringUtils::StartsWith(GetName(), kMuidRangeTabPrefixStr);
153 }
154
IsArrayClassCache() const155 bool MIRSymbol::IsArrayClassCache() const
156 {
157 return StringUtils::StartsWith(GetName(), kArrayClassCacheTable);
158 }
159
IsArrayClassCacheName() const160 bool MIRSymbol::IsArrayClassCacheName() const
161 {
162 return StringUtils::StartsWith(GetName(), kArrayClassCacheNameTable);
163 }
164
IsClassInitBridge() const165 bool MIRSymbol::IsClassInitBridge() const
166 {
167 return StringUtils::StartsWith(GetName(), CLASS_INIT_BRIDGE_PREFIX_STR);
168 }
169
IsReflectionHashTabBucket() const170 bool MIRSymbol::IsReflectionHashTabBucket() const
171 {
172 return StringUtils::StartsWith(GetName(), kMuidClassMetadataBucketPrefixStr);
173 }
174
IsReflectionInfo() const175 bool MIRSymbol::IsReflectionInfo() const
176 {
177 return IsReflectionClassInfo() || IsReflectionClassInfoRO() || IsReflectionFieldsInfo() ||
178 IsReflectionFieldsInfoCompact() || IsReflectionMethodsInfo() || IsReflectionPrimitiveClassInfo() ||
179 IsReflectionSuperclassInfo() || IsReflectionMethodsInfoCompact();
180 }
181
IsReflectionFieldsInfo() const182 bool MIRSymbol::IsReflectionFieldsInfo() const
183 {
184 return StringUtils::StartsWith(GetName(), kFieldsInfoPrefixStr);
185 }
186
IsReflectionFieldsInfoCompact() const187 bool MIRSymbol::IsReflectionFieldsInfoCompact() const
188 {
189 return StringUtils::StartsWith(GetName(), kFieldsInfoCompactPrefixStr);
190 }
191
IsReflectionSuperclassInfo() const192 bool MIRSymbol::IsReflectionSuperclassInfo() const
193 {
194 return StringUtils::StartsWith(GetName(), SUPERCLASSINFO_PREFIX_STR);
195 }
196
IsReflectionFieldOffsetData() const197 bool MIRSymbol::IsReflectionFieldOffsetData() const
198 {
199 return StringUtils::StartsWith(GetName(), kFieldOffsetDataPrefixStr);
200 }
201
IsReflectionMethodAddrData() const202 bool MIRSymbol::IsReflectionMethodAddrData() const
203 {
204 return (GetName().find(kMethodAddrDataPrefixStr) == 0);
205 }
206
IsReflectionMethodSignature() const207 bool MIRSymbol::IsReflectionMethodSignature() const
208 {
209 return (GetName().find(kMethodSignaturePrefixStr) == 0);
210 }
211
IsReflectionClassInfo() const212 bool MIRSymbol::IsReflectionClassInfo() const
213 {
214 return StringUtils::StartsWith(GetName(), CLASSINFO_PREFIX_STR);
215 }
216
IsReflectionClassInfoPtr() const217 bool MIRSymbol::IsReflectionClassInfoPtr() const
218 {
219 return StringUtils::StartsWith(GetName(), kClassINfoPtrPrefixStr);
220 }
221
IsReflectionClassInfoRO() const222 bool MIRSymbol::IsReflectionClassInfoRO() const
223 {
224 return StringUtils::StartsWith(GetName(), CLASSINFO_RO_PREFIX_STR);
225 }
226
IsITabConflictInfo() const227 bool MIRSymbol::IsITabConflictInfo() const
228 {
229 return StringUtils::StartsWith(GetName(), ITAB_CONFLICT_PREFIX_STR);
230 }
231
IsReflectionPrimitiveClassInfo() const232 bool MIRSymbol::IsReflectionPrimitiveClassInfo() const
233 {
234 return StringUtils::StartsWith(GetName(), PRIMITIVECLASSINFO_PREFIX_STR);
235 }
236
IsReflectionMethodsInfo() const237 bool MIRSymbol::IsReflectionMethodsInfo() const
238 {
239 return StringUtils::StartsWith(GetName(), kMethodsInfoPrefixStr);
240 }
241
IsReflectionMethodsInfoCompact() const242 bool MIRSymbol::IsReflectionMethodsInfoCompact() const
243 {
244 return StringUtils::StartsWith(GetName(), kMethodsInfoCompactPrefixStr);
245 }
246
IsGctibSym() const247 bool MIRSymbol::IsGctibSym() const
248 {
249 return StringUtils::StartsWith(GetName(), GCTIB_PREFIX_STR);
250 }
251
Dump(bool isLocal,int32 indent,bool suppressInit,const MIRSymbolTable * localsymtab) const252 void MIRSymbol::Dump(bool isLocal, int32 indent, bool suppressInit, const MIRSymbolTable *localsymtab) const
253 {
254 if (sKind == kStVar || sKind == kStFunc) {
255 srcPosition.DumpLoc(lastPrintedLineNum, lastPrintedColumnNum);
256 }
257 // exclude unused symbols, formal symbols and extern functions
258 if (GetStorageClass() == kScUnused || GetStorageClass() == kScFormal ||
259 (GetStorageClass() == kScExtern && sKind == kStFunc)) {
260 return;
261 }
262 if (GetIsImported() && !GetAppearsInCode()) {
263 return;
264 }
265 if (GetTyIdx() >= GlobalTables::GetTypeTable().GetTypeTable().size()) {
266 FATAL(kLncFatal, "valid maple_ir with illegal type");
267 }
268 if (GetStorageClass() == kScText && GetFunction() != nullptr) {
269 // without body
270 GetFunction()->Dump(true);
271 return;
272 }
273 const char *ids = isLocal ? "%" : "$";
274 PrintIndentation(indent);
275 if (isTmp) {
276 LogInfo::MapleLogger() << "tempvar ";
277 } else {
278 LogInfo::MapleLogger() << "var ";
279 }
280 LogInfo::MapleLogger() << ids << GetName() << " ";
281 if (GetStorageClass() == kScFstatic) {
282 LogInfo::MapleLogger() << "fstatic ";
283 } else if (GetStorageClass() == kScPstatic) {
284 LogInfo::MapleLogger() << "pstatic ";
285 } else if (GetStorageClass() == kScExtern) {
286 LogInfo::MapleLogger() << "extern ";
287 }
288 if (GetTyIdx() != 0u) {
289 GlobalTables::GetTypeTable().GetTypeFromTyIdx(GetTyIdx())->Dump(indent + 1);
290 }
291 if (sectionAttr != UStrIdx(0)) {
292 LogInfo::MapleLogger() << " section (";
293 PrintString(GlobalTables::GetUStrTable().GetStringFromStrIdx(sectionAttr));
294 LogInfo::MapleLogger() << " )";
295 }
296 if (asmAttr != UStrIdx(0)) {
297 LogInfo::MapleLogger() << " asmattr (";
298 PrintString(GlobalTables::GetUStrTable().GetStringFromStrIdx(asmAttr));
299 LogInfo::MapleLogger() << " )";
300 }
301 typeAttrs.DumpAttributes();
302 if (GetStorageClass() == kScTypeInfoName || GetStorageClass() == kScTypeInfo ||
303 GetStorageClass() == kScTypeCxxAbi) {
304 LogInfo::MapleLogger() << '\n';
305 return;
306 }
307 if (IsConst() && !suppressInit && !(IsLiteral() && GetStorageClass() == kScExtern)) {
308 LogInfo::MapleLogger() << " = ";
309 GetKonst()->Dump(localsymtab);
310 }
311 LogInfo::MapleLogger() << '\n';
312 }
313
DumpAsLiteralVar() const314 void MIRSymbol::DumpAsLiteralVar() const
315 {
316 if (IsLiteral()) {
317 LogInfo::MapleLogger() << GetName();
318 }
319 }
320
Dump(bool isLocal,int32 indent,bool printDeleted,MIRFlavor flavor) const321 void MIRSymbolTable::Dump(bool isLocal, int32 indent, bool printDeleted, MIRFlavor flavor) const
322 {
323 size_t size = symbolTable.size();
324 for (size_t i = 0; i < size; ++i) {
325 MIRSymbol *symbol = symbolTable[i];
326 if (symbol == nullptr) {
327 continue;
328 }
329 if (!printDeleted && symbol->IsDeleted()) {
330 continue;
331 }
332 if (flavor == kFlavorLmbc && symbol->LMBCAllocateOffSpecialReg()) {
333 continue;
334 }
335 symbol->Dump(isLocal, indent, false /* suppressinit */, this);
336 }
337 }
338 #endif
339
AddToStringLabelMap(LabelIdx labelIdx)340 void MIRLabelTable::AddToStringLabelMap(LabelIdx labelIdx)
341 {
342 CHECK_FATAL(labelIdx < labelTable.size(), "index out of range in MIRLabelTable::AddToStringLabelMap");
343 if (labelTable[labelIdx] == 0u) {
344 // generate a label name based on lab_idx
345 std::ostringstream labelNameStream;
346 labelNameStream << "@" << labelIdx;
347 std::string labelName = labelNameStream.str();
348 labelTable[labelIdx] = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(labelName);
349 }
350 GStrIdx strIdx = labelTable[labelIdx];
351 strIdxToLabIdxMap[strIdx] = labelIdx;
352 }
353 } // namespace maple
354