From 3ca7c5a5d8bc39b94ca52fa9fc49235e6405f834 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Wed, 17 Aug 2022 07:33:23 -0700 Subject: [PATCH 1/2] update field type with alias info --- src/mapleall/maple_ir/include/debug_info.h | 1 + src/mapleall/maple_ir/src/debug_info.cpp | 93 +++++++++++++--------- 2 files changed, 57 insertions(+), 37 deletions(-) diff --git a/src/mapleall/maple_ir/include/debug_info.h b/src/mapleall/maple_ir/include/debug_info.h index 8e53919198..1bb7ef2dcd 100644 --- a/src/mapleall/maple_ir/include/debug_info.h +++ b/src/mapleall/maple_ir/include/debug_info.h @@ -770,6 +770,7 @@ class DebugInfo { GStrIdx GetPrimTypeCName(PrimType pty); void AddScopeDie(MIRScope *scope, bool isLocal); + DBGDie *GetAliasVarTypeDie(MIRAliasVars &a, TyIdx tyidx); void AddAliasDies(MapleMap &aliasMap, bool isLocal); void CollectScopePos(MIRFunction *func, MIRScope *scope); void GetCrossScopeId(MIRFunction *func, diff --git a/src/mapleall/maple_ir/src/debug_info.cpp b/src/mapleall/maple_ir/src/debug_info.cpp index 2614f99df8..8f57258b9c 100644 --- a/src/mapleall/maple_ir/src/debug_info.cpp +++ b/src/mapleall/maple_ir/src/debug_info.cpp @@ -332,6 +332,28 @@ void DebugInfo::AddScopeDie(MIRScope *scope, bool isLocal) { } } +DBGDie *DebugInfo::GetAliasVarTypeDie(MIRAliasVars &a, TyIdx tyidx) { + DBGDie *tdie = nullptr; + uint32 index = a.index; + switch (a.atk) { + case kATKType: + tdie = GetOrCreateTypeDie(TyIdx(index)); + break; + case kATKString: + tdie = GetOrCreateTypedefDie(GStrIdx(index), tyidx); + break; + case kATKEnum: + tdie = GetOrCreateEnumTypeDie(index); + break; + default: + ASSERT(false, "unknown alias type kind"); + break; + } + + ASSERT(tdie, "null alias type DIE"); + return GetOrCreateTypeDie(a.attrs, tdie); +} + void DebugInfo::AddAliasDies(MapleMap &aliasMap, bool isLocal) { MIRFunction *func = GetCurFunction(); for (auto &i : aliasMap) { @@ -358,26 +380,8 @@ void DebugInfo::AddAliasDies(MapleMap &aliasMap, bool isL DBGDie *vdie = updateOnly ? mplDie : CreateVarDie(mplVar, i.first); // get type from alias - uint32 index = i.second.index; - DBGDie *typeDie = nullptr; - switch (i.second.atk) { - case kATKType: - typeDie = GetOrCreateTypeDie(TyIdx(index)); - break; - case kATKString: - typeDie = GetOrCreateTypedefDie(GStrIdx(index), mplVar->GetTyIdx()); - break; - case kATKEnum: - typeDie = GetOrCreateEnumTypeDie(index); - break; - default: - ASSERT(false, "unknown alias type kind"); - break; - } - - ASSERT(typeDie, "null typeDie"); - DBGDie *newDie = GetOrCreateTypeDie(i.second.attrs, typeDie); - (void)(vdie->SetAttr(DW_AT_type, newDie->GetId())); + DBGDie *tdie = GetAliasVarTypeDie(i.second, mplVar->GetTyIdx()); + (void)(vdie->SetAttr(DW_AT_type, tdie->GetId())); // for new var if (!updateOnly) { @@ -450,6 +454,23 @@ void DebugInfo::BuildDebugInfo() { Init(); + // setup debug info for enum types + auto size = GlobalTables::GetEnumTable().enumTable.size(); + for (size_t i = 0; i < size; ++i) { + DBGDie *die = GetOrCreateEnumTypeDie(i); + compUnit->AddSubVec(die); + } + + // setup debug info for typedef types + for (auto it : GlobalTables::GetTypedefTable().GetTypedefNameMap()) { + module->GetDbgInfo()->AddTypedefMap(it.first, it.second); + } + for (auto it : typedefStrIdxTyIdxMap) { + (void)GetOrCreateTypeDie(TyIdx(it.second)); + DBGDie *die = GetOrCreateTypedefDie(GStrIdx(it.first), TyIdx(it.second)); + compUnit->AddSubVec(die); + } + // containner types for (auto it : module->GetTypeNameTab()->GetGStrIdxToTyIdxMap()) { TyIdx tyIdx = it.second; @@ -473,16 +494,6 @@ void DebugInfo::BuildDebugInfo() { } } - // setup debug info for typedef types - for (auto it : GlobalTables::GetTypedefTable().GetTypedefNameMap()) { - module->GetDbgInfo()->AddTypedefMap(it.first, it.second); - } - for (auto it : typedefStrIdxTyIdxMap) { - (void)GetOrCreateTypeDie(TyIdx(it.second)); - DBGDie *die = GetOrCreateTypedefDie(GStrIdx(it.first), TyIdx(it.second)); - compUnit->AddSubVec(die); - } - // setup debug info for global symbols for (size_t i = 0; i < GlobalTables::GetGsymTable().GetSymbolTableSize(); ++i) { MIRSymbol *mirSymbol = GlobalTables::GetGsymTable().GetSymbolFromStidx(static_cast(i)); @@ -496,13 +507,6 @@ void DebugInfo::BuildDebugInfo() { } } - // setup debug info for enum types - auto size = GlobalTables::GetEnumTable().enumTable.size(); - for (size_t i = 0; i < size; ++i) { - DBGDie *die = GetOrCreateEnumTypeDie(i); - compUnit->AddSubVec(die); - } - // handle global scope MIRScope *scope = module->GetScope(); AddScopeDie(scope, false); @@ -1237,6 +1241,7 @@ DBGDie *DebugInfo::GetOrCreateStructTypeDie(const MIRType *type) { } GlobalTables::GetTypeNameTable().SetGStrIdxToTyIdx(strIdx, type->GetTypeIndex()); + return die; } @@ -1285,6 +1290,20 @@ DBGDie *DebugInfo::CreateStructTypeDie(GStrIdx strIdx, const MIRStructType *stru } else { prevBits = 0; DBGDie *fdie = CreateFieldDie(fp, 0); + + // update field type with alias info + MIRAlias *alias = structType->GetAlias(); + if (alias) { + for (auto &a : alias->GetAliasVarMap()) { + GStrIdx mplIdx = a.first; + if (a.first == fp.first) { + DBGDie *tdie = GetAliasVarTypeDie(a.second, fp.second.first); + (void)(fdie->SetAttr(DW_AT_type, tdie->GetId())); + break; + } + } + } + die->AddSubVec(fdie); } } -- Gitee From 897c08cd2a20b9dd81d8e302f2bde1f7643d9195 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Wed, 17 Aug 2022 14:52:40 -0700 Subject: [PATCH 2/2] debuginfo code refactoring --- src/mapleall/maple_ir/include/debug_info.h | 267 ++++++++++----------- src/mapleall/maple_ir/src/debug_info.cpp | 222 ++++++++++------- 2 files changed, 266 insertions(+), 223 deletions(-) diff --git a/src/mapleall/maple_ir/include/debug_info.h b/src/mapleall/maple_ir/include/debug_info.h index 1bb7ef2dcd..2e393c11f2 100644 --- a/src/mapleall/maple_ir/include/debug_info.h +++ b/src/mapleall/maple_ir/include/debug_info.h @@ -619,58 +619,67 @@ class DebugInfo { virtual ~DebugInfo() {} + void BuildDebugInfo(); void ClearDebugInfo(); + void Dump(int indent); - void InitMsg() { - compileMsg = module->GetMemPool()->New(); - } + DBGDie *GetDie(const MIRFunction *func); + DBGDie *GetLocalDie(MIRFunction *func, GStrIdx strIdx); + DBGDieAttr *CreateAttr(DwAt at, DwForm form, uint64 val) const; - void UpdateMsg(uint32 lnum, const char *line) { - compileMsg->UpdateMsg(lnum, line); - } + bool IsScopeIdEmited(MIRFunction *func, uint32 scopeId); - void SetErrPos(uint32 lnum, uint32 cnum) { - compileMsg->SetErrPos(lnum, cnum); - } + void GetCrossScopeId(MIRFunction *func, + std::unordered_set &idSet, + bool isLow, + const SrcPosition &oldSrcPos, + const SrcPosition &newSrcPos); - void EmitMsg() { - compileMsg->EmitMsg(); - } + void ComputeSizeAndOffsets(); DBGDie *GetDie(uint32 id) { return idDieMap[id]; } + void SetIdDieMap(uint32 i, DBGDie *die) { + idDieMap[i] = die; + } + DBGDie *GetDummyTypeDie() { return dummyTypeDie; } - DBGDie *GetDie(const MIRFunction *func); + DBGDie *GetParentDie() { + return parentDieStack.top(); + } - void Init(); - void Finish(); - void SetupCU(); - void BuildDebugInfo(); - void Dump(int indent); + void ResetParentDie() { + parentDieStack.clear(); + parentDieStack.push(compUnit); + } - // build tree to populate withChildren, sibling, firstChild - // also insert DW_AT_sibling attributes when needed - void BuildDieTree(); + uint32 GetDebugInfoLength() const { + return debugInfoLength; + } - void BuildAbbrev(); - uint32 GetAbbrevId(DBGAbbrevEntryVec *vec, DBGAbbrevEntry *entry) const; + void SetFuncScopeIdStatus(MIRFunction *func, uint32 scopeId, EmitStatus status) { + if (funcScopeIdStatus[func].find(scopeId) == funcScopeIdStatus[func].end()) { + funcScopeIdStatus[func][scopeId] = 0; + } + funcScopeIdStatus[func][scopeId] |= (1 << status); + } - DBGDie *GetGlobalDie(const GStrIdx &strIdx); + MapleVector &GetAbbrevVec() { + return abbrevVec; + } - void SetLocalDie(GStrIdx strIdx, const DBGDie *die); - void SetLocalDie(MIRFunction *func, GStrIdx strIdx, const DBGDie *die); - DBGDie *GetLocalDie(GStrIdx strIdx); - DBGDie *GetLocalDie(MIRFunction *func, GStrIdx strIdx); + void AddStrps(uint32 val) { + strps.insert(val); + } - LabelIdx GetLabelIdx(GStrIdx strIdx); - LabelIdx GetLabelIdx(MIRFunction *func, GStrIdx strIdx); - void SetLabelIdx(GStrIdx strIdx, LabelIdx labIdx); - void SetLabelIdx(MIRFunction *func, GStrIdx strIdx, LabelIdx labIdx); + MapleSet &GetStrps() { + return strps; + } uint32 GetMaxId() const { return maxId; @@ -680,53 +689,112 @@ class DebugInfo { return maxId++; } - DBGDie *GetIdDieMapAt(uint32 i) { - return idDieMap[i]; - } - - void SetIdDieMap(uint32 i, DBGDie *die) { - idDieMap[i] = die; + DBGDie *GetCompUnit() const { + return compUnit; } size_t GetParentDieSize() const { return parentDieStack.size(); } - DBGDie *GetParentDie() { - return parentDieStack.top(); + void SetErrPos(uint32 lnum, uint32 cnum) { + compileMsg->SetErrPos(lnum, cnum); } - void PushParentDie(DBGDie *die) { - parentDieStack.push(die); + void UpdateMsg(uint32 lnum, const char *line) { + compileMsg->UpdateMsg(lnum, line); } - void PopParentDie() { - parentDieStack.pop(); + void EmitMsg() { + compileMsg->EmitMsg(); } - void ResetParentDie() { - parentDieStack.clear(); - parentDieStack.push(compUnit); - } + private: + MIRModule *module; + DBGDie *compUnit; // root die: compilation unit + DBGDie *dummyTypeDie; // workaround for unknown types + MIRLexer *lexer; + uint32 maxId; + GStrIdx mplSrcIdx; + uint32 debugInfoLength; + MIRFunction *curFunction; - void AddStrps(uint32 val) { - strps.insert(val); - } + // for compilation messages + DBGCompileMsgInfo *compileMsg; - MapleSet &GetStrps() { - return strps; + MapleStack parentDieStack; + MapleMap idDieMap; + MapleVector abbrevVec; // valid entry starting from index 1 + MapleMap tagAbbrevMap; + + // to be used when derived type references a base type die + MapleMap tyIdxDieIdMap; + MapleMap stridxDieIdMap; + // save global var die, global var string idx to die id + MapleMap globalStridxDieIdMap; + MapleMap funcDefStrIdxDieIdMap; + MapleMap typeDefTyIdxMap; // prevtyIdxtypidx_map + MapleMap pointedPointerMap; + MapleMap> funcLstrIdxDieIdMap; + MapleMap> funcLstrIdxLabIdxMap; + + MapleMap> funcScopeLows; + MapleMap> funcScopeHighs; + + /* save functions's scope id that has been emited */ + MapleMap> funcScopeIdStatus; + + /* alias type */ + MapleMap typedefStrIdxTyIdxMap; + MapleMap constTypeDieMap; + MapleMap volatileTypeDieMap; + + MapleSet strps; + std::string varPtrPrefix; + + void InitMsg() { + compileMsg = module->GetMemPool()->New(); } - uint32 GetDebugInfoLength() const { - return debugInfoLength; + void Init(); + void Finish(); + void SetupCU(); + + void BuildDebugInfoEnums(); + void BuildDebugInfoTypedefs(); + void BuildDebugInfoContainers(); + void BuildDebugInfoGlobalSymbols(); + void BuildDebugInfoFunctions(); + + // build tree to populate withChildren, sibling, firstChild + // also insert DW_AT_sibling attributes when needed + void BuildDieTree(); + + void BuildAbbrev(); + + uint32 GetAbbrevId(DBGAbbrevEntryVec *vec, DBGAbbrevEntry *entry) const; + + DBGDie *GetGlobalDie(const GStrIdx &strIdx); + + void SetLocalDie(GStrIdx strIdx, const DBGDie *die); + void SetLocalDie(MIRFunction *func, GStrIdx strIdx, const DBGDie *die); + DBGDie *GetLocalDie(GStrIdx strIdx); + + LabelIdx GetLabelIdx(GStrIdx strIdx); + LabelIdx GetLabelIdx(MIRFunction *func, GStrIdx strIdx); + void SetLabelIdx(GStrIdx strIdx, LabelIdx labIdx); + void SetLabelIdx(MIRFunction *func, GStrIdx strIdx, LabelIdx labIdx); + + DBGDie *GetIdDieMapAt(uint32 i) { + return idDieMap[i]; } - MapleVector &GetAbbrevVec() { - return abbrevVec; + void PushParentDie(DBGDie *die) { + parentDieStack.push(die); } - DBGDie *GetCompUnit() const { - return compUnit; + void PopParentDie() { + parentDieStack.pop(); } MIRFunction *GetCurFunction() { @@ -741,21 +809,23 @@ class DebugInfo { tyIdxDieIdMap[tyIdx.GetIdx()] = die->GetId(); } - DBGDieAttr *CreateAttr(DwAt at, DwForm form, uint64 val) const; - DBGDie *CreateVarDie(MIRSymbol *sym); DBGDie *CreateVarDie(MIRSymbol *sym, GStrIdx strIdx); // use alt name DBGDie *CreateFormalParaDie(MIRFunction *func, MIRType *type, MIRSymbol *sym); DBGDie *CreateFieldDie(maple::FieldPair pair, uint32 lnum); DBGDie *CreateBitfieldDie(const MIRBitFieldType *type, GStrIdx sidx, uint32 prevBits); + void CreateStructTypeFieldsDies(const MIRStructType *structType, DBGDie *die); + void CreateStructTypeParentFieldsDies(const MIRStructType *structType, DBGDie *die); + void CreateStructTypeMethodsDies(const MIRStructType *structType, DBGDie *die); DBGDie *CreateStructTypeDie(GStrIdx strIdx, const MIRStructType *structType, bool update = false); DBGDie *CreateClassTypeDie(GStrIdx strIdx, const MIRClassType *classType); DBGDie *CreateInterfaceTypeDie(GStrIdx strIdx, const MIRInterfaceType *interfaceType); DBGDie *CreatePointedFuncTypeDie(MIRFuncType *fType); + void CreateFuncLocalSymbolsDies(MIRFunction *func, DBGDie *die); DBGDie *GetOrCreateLabelDie(LabelIdx labid); DBGDie *GetOrCreateFuncDeclDie(MIRFunction *func); - DBGDie *GetOrCreateFuncDefDie(MIRFunction *func, uint32 lnum); + DBGDie *GetOrCreateFuncDefDie(MIRFunction *func); DBGDie *GetOrCreatePrimTypeDie(MIRType *ty); DBGDie *GetOrCreateTypeDie(TyIdx tyidx); DBGDie *GetOrCreateTypeDie(MIRType *type); @@ -773,87 +843,14 @@ class DebugInfo { DBGDie *GetAliasVarTypeDie(MIRAliasVars &a, TyIdx tyidx); void AddAliasDies(MapleMap &aliasMap, bool isLocal); void CollectScopePos(MIRFunction *func, MIRScope *scope); - void GetCrossScopeId(MIRFunction *func, - std::unordered_set &idSet, - bool isLow, - const SrcPosition &oldSrcPos, - const SrcPosition &newSrcPos); // Functions for calculating the size and offset of each DW_TAG_xxx and DW_AT_xxx - void ComputeSizeAndOffsets(); void ComputeSizeAndOffset(DBGDie *die, uint32 &cuOffset); - void SetFuncScopeIdStatus(MIRFunction *func, uint32 scopeId, EmitStatus status) { - if (funcScopeIdStatus[func].find(scopeId) == funcScopeIdStatus[func].end()) { - funcScopeIdStatus[func][scopeId] = 0; - } - funcScopeIdStatus[func][scopeId] |= (1 << status); - } - - bool IsScopeIdEmited(MIRFunction *func, uint32 scopeId) { - auto it = funcScopeIdStatus.find(func); - if (it == funcScopeIdStatus.end()) { - return false; - } - - auto scopeIdIt = it->second.find(scopeId); - if (scopeIdIt == it->second.end()) { - return false; - } - - if (scopeIdIt->second != kAllEmited) { - return false; - } - return true; - } - void AddTypedefMap(GStrIdx stridx, TyIdx tyidx) { typedefStrIdxTyIdxMap[stridx.GetIdx()] = tyidx.GetIdx(); } void DumpTypedefMap() const; - - private: - MIRModule *module; - DBGDie *compUnit; // root die: compilation unit - DBGDie *dummyTypeDie; // workaround for unknown types - MIRLexer *lexer; - uint32 maxId; - GStrIdx mplSrcIdx; - uint32 debugInfoLength; - MIRFunction *curFunction; - - // for compilation messages - DBGCompileMsgInfo *compileMsg; - - MapleStack parentDieStack; - MapleMap idDieMap; - MapleVector abbrevVec; // valid entry starting from index 1 - MapleMap tagAbbrevMap; - - // to be used when derived type references a base type die - MapleMap tyIdxDieIdMap; - MapleMap stridxDieIdMap; - // save global var die, global var string idx to die id - MapleMap globalStridxDieIdMap; - MapleMap funcDefStrIdxDieIdMap; - MapleMap typeDefTyIdxMap; // prevtyIdxtypidx_map - MapleMap pointedPointerMap; - MapleMap> funcLstrIdxDieIdMap; - MapleMap> funcLstrIdxLabIdxMap; - - MapleMap> funcScopeLows; - MapleMap> funcScopeHighs; - - /* save functions's scope id that has been emited */ - MapleMap> funcScopeIdStatus; - - /* alias type */ - MapleMap typedefStrIdxTyIdxMap; - MapleMap constTypeDieMap; - MapleMap volatileTypeDieMap; - - MapleSet strps; - std::string varPtrPrefix; }; } // namespace maple #endif // MAPLE_IR_INCLUDE_DBG_INFO_H diff --git a/src/mapleall/maple_ir/src/debug_info.cpp b/src/mapleall/maple_ir/src/debug_info.cpp index 8f57258b9c..90f2bc454d 100644 --- a/src/mapleall/maple_ir/src/debug_info.cpp +++ b/src/mapleall/maple_ir/src/debug_info.cpp @@ -449,29 +449,27 @@ void DebugInfo::Finish() { ComputeSizeAndOffsets(); } -void DebugInfo::BuildDebugInfo() { - ASSERT(module->GetDbgInfo(), "null dbgInfo"); - - Init(); - - // setup debug info for enum types +void DebugInfo::BuildDebugInfoEnums() { auto size = GlobalTables::GetEnumTable().enumTable.size(); for (size_t i = 0; i < size; ++i) { DBGDie *die = GetOrCreateEnumTypeDie(i); compUnit->AddSubVec(die); } +} - // setup debug info for typedef types +void DebugInfo::BuildDebugInfoTypedefs() { for (auto it : GlobalTables::GetTypedefTable().GetTypedefNameMap()) { module->GetDbgInfo()->AddTypedefMap(it.first, it.second); } + for (auto it : typedefStrIdxTyIdxMap) { (void)GetOrCreateTypeDie(TyIdx(it.second)); DBGDie *die = GetOrCreateTypedefDie(GStrIdx(it.first), TyIdx(it.second)); compUnit->AddSubVec(die); } +} - // containner types +void DebugInfo::BuildDebugInfoContainers() { for (auto it : module->GetTypeNameTab()->GetGStrIdxToTyIdxMap()) { TyIdx tyIdx = it.second; MIRType *type = GlobalTables::GetTypeTable().GetTypeFromTyIdx(tyIdx.GetIdx()); @@ -493,8 +491,9 @@ void DebugInfo::BuildDebugInfo() { break; } } +} - // setup debug info for global symbols +void DebugInfo::BuildDebugInfoGlobalSymbols() { for (size_t i = 0; i < GlobalTables::GetGsymTable().GetSymbolTableSize(); ++i) { MIRSymbol *mirSymbol = GlobalTables::GetGsymTable().GetSymbolFromStidx(static_cast(i)); if (mirSymbol == nullptr || mirSymbol->IsDeleted() || mirSymbol->GetStorageClass() == kScUnused || @@ -506,12 +505,9 @@ void DebugInfo::BuildDebugInfo() { compUnit->AddSubVec(vdie); } } +} - // handle global scope - MIRScope *scope = module->GetScope(); - AddScopeDie(scope, false); - - // setup debug info for functions +void DebugInfo::BuildDebugInfoFunctions() { for (auto func : GlobalTables::GetFunctionTable().GetFuncTable()) { // the first one in funcTable is nullptr if (!func) { @@ -527,12 +523,36 @@ void DebugInfo::BuildDebugInfo() { } // function def if (funcDefStrIdxDieIdMap.find(func->GetNameStrIdx().GetIdx()) == funcDefStrIdxDieIdMap.end()) { - DBGDie *fdie = GetOrCreateFuncDefDie(func, 0); + DBGDie *fdie = GetOrCreateFuncDefDie(func); if (!func->GetClassTyIdx().GetIdx() && func->GetBody()) { compUnit->AddSubVec(fdie); } } } +} + +void DebugInfo::BuildDebugInfo() { + ASSERT(module->GetDbgInfo(), "null dbgInfo"); + + Init(); + + // setup debug info for enum types + BuildDebugInfoEnums(); + + // setup debug info for typedef types + BuildDebugInfoTypedefs(); + + // containner types + BuildDebugInfoContainers(); + + // setup debug info for global symbols + BuildDebugInfoGlobalSymbols(); + + // handle global scope + AddScopeDie(module->GetScope(), false); + + // setup debug info for functions + BuildDebugInfoFunctions(); // finalize debug info Finish(); @@ -770,7 +790,25 @@ bool LIsCompilerGenerated(const MIRFunction *func) { return ((func->GetName().c_str())[0] != 'L'); } -DBGDie *DebugInfo::GetOrCreateFuncDefDie(MIRFunction *func, uint32 lnum) { +void DebugInfo::CreateFuncLocalSymbolsDies(MIRFunction *func, DBGDie *die) { + if (func->GetSymTab()) { + // local variables, start from 1 + for (uint32 i = 1; i < func->GetSymTab()->GetSymbolTableSize(); i++) { + MIRSymbol *var = func->GetSymTab()->GetSymbolFromStIdx(i); + DBGDie *vdie = CreateVarDie(var); + if (vdie == nullptr) { + continue; + } + die->AddSubVec(vdie); + // for C, source variable names will be used instead of mangloed maple variables + if (module->IsCModule()) { + vdie->SetKeep(false); + } + } + } +} + +DBGDie *DebugInfo::GetOrCreateFuncDefDie(MIRFunction *func) { uint32 funcnameidx = func->GetNameStrIdx().GetIdx(); if (funcDefStrIdxDieIdMap.find(funcnameidx) != funcDefStrIdxDieIdMap.end()) { uint32 id = funcDefStrIdxDieIdMap[funcnameidx]; @@ -783,7 +821,7 @@ DBGDie *DebugInfo::GetOrCreateFuncDefDie(MIRFunction *func, uint32 lnum) { funcDefStrIdxDieIdMap[funcnameidx] = die->GetId(); die->AddAttr(DW_AT_specification, DW_FORM_ref4, funcdecldie->GetId()); - die->AddAttr(DW_AT_decl_line, DW_FORM_data4, lnum); + die->AddAttr(DW_AT_decl_line, DW_FORM_data4, 0); if (!func->IsReturnVoid()) { auto returnType = func->GetReturnType(); @@ -808,21 +846,7 @@ DBGDie *DebugInfo::GetOrCreateFuncDefDie(MIRFunction *func, uint32 lnum) { die->AddSubVec(pdie); } - if (func->GetSymTab()) { - // local variables, start from 1 - for (uint32 i = 1; i < func->GetSymTab()->GetSymbolTableSize(); i++) { - MIRSymbol *var = func->GetSymTab()->GetSymbolFromStIdx(i); - DBGDie *vdie = CreateVarDie(var); - if (vdie == nullptr) { - continue; - } - die->AddSubVec(vdie); - // for C, source variable names will be used instead of mangloed maple variables - if (module->IsCModule()) { - vdie->SetKeep(false); - } - } - } + CreateFuncLocalSymbolsDies(func, die); // add scope die AddScopeDie(func->GetScope(), true); @@ -952,11 +976,10 @@ DBGDie *DebugInfo::GetOrCreateTypeDie(MIRType *type) { } uint32 sid = type->GetNameStrIdx().GetIdx(); - if (sid != 0) - if (stridxDieIdMap.find(sid) != stridxDieIdMap.end()) { - uint32 id = stridxDieIdMap[sid]; - return idDieMap[id]; - } + if (sid != 0 && stridxDieIdMap.find(sid) != stridxDieIdMap.end()) { + uint32 id = stridxDieIdMap[sid]; + return idDieMap[id]; + } if (type->GetTypeIndex() == static_cast(type->GetPrimType())) { return GetOrCreatePrimTypeDie(type); @@ -1245,34 +1268,7 @@ DBGDie *DebugInfo::GetOrCreateStructTypeDie(const MIRType *type) { return die; } -// shared between struct and union -DBGDie *DebugInfo::CreateStructTypeDie(GStrIdx strIdx, const MIRStructType *structType, bool update) { - DBGDie *die = nullptr; - - if (update) { - uint32 id = tyIdxDieIdMap[structType->GetTypeIndex().GetIdx()]; - die = idDieMap[id]; - ASSERT(die, "update type die not exist"); - } else { - DwTag tag = structType->GetKind() == kTypeStruct ? DW_TAG_structure_type : DW_TAG_union_type; - die = module->GetMemPool()->New(module, tag); - tyIdxDieIdMap[structType->GetTypeIndex().GetIdx()] = die->GetId(); - } - - if (strIdx.GetIdx() != 0) { - stridxDieIdMap[strIdx.GetIdx()] = die->GetId(); - } - - compUnit->AddSubVec(die); - - die->AddAttr(DW_AT_decl_line, DW_FORM_data4, kStructDBGSize); - die->AddAttr(DW_AT_name, DW_FORM_strp, strIdx.GetIdx()); - die->AddAttr(DW_AT_byte_size, DW_FORM_data4, kDbgDefaultVal); - die->AddAttr(DW_AT_decl_file, DW_FORM_data4, mplSrcIdx.GetIdx()); - - PushParentDie(die); - - // fields +void DebugInfo::CreateStructTypeFieldsDies(const MIRStructType *structType, DBGDie *die) { uint32 prevBits = 0; for (size_t i = 0; i < structType->GetFieldsSize(); i++) { MIRType *ety = structType->GetElemType(static_cast(i)); @@ -1290,31 +1286,35 @@ DBGDie *DebugInfo::CreateStructTypeDie(GStrIdx strIdx, const MIRStructType *stru } else { prevBits = 0; DBGDie *fdie = CreateFieldDie(fp, 0); + die->AddSubVec(fdie); // update field type with alias info MIRAlias *alias = structType->GetAlias(); - if (alias) { - for (auto &a : alias->GetAliasVarMap()) { - GStrIdx mplIdx = a.first; - if (a.first == fp.first) { - DBGDie *tdie = GetAliasVarTypeDie(a.second, fp.second.first); - (void)(fdie->SetAttr(DW_AT_type, tdie->GetId())); - break; - } - } + if (!alias) { + continue; } - die->AddSubVec(fdie); + for (auto &a : alias->GetAliasVarMap()) { + GStrIdx mplIdx = a.first; + if (a.first == fp.first) { + DBGDie *tdie = GetAliasVarTypeDie(a.second, fp.second.first); + (void)(fdie->SetAttr(DW_AT_type, tdie->GetId())); + break; + } + } } } +} - // parentFields +void DebugInfo::CreateStructTypeParentFieldsDies(const MIRStructType *structType, DBGDie *die) { for (size_t i = 0; i < structType->GetParentFieldsSize(); i++) { FieldPair fp = structType->GetParentFieldsElemt(i); DBGDie *fdie = CreateFieldDie(fp, 0); die->AddSubVec(fdie); } +} +void DebugInfo::CreateStructTypeMethodsDies(const MIRStructType *structType, DBGDie *die) { // member functions decl for (auto fp : structType->GetMethods()) { MIRSymbol *symbol = GlobalTables::GetGsymTable().GetSymbolFromStidx(fp.first.Idx()); @@ -1323,23 +1323,52 @@ DBGDie *DebugInfo::CreateStructTypeDie(GStrIdx strIdx, const MIRStructType *stru ASSERT(func, "member function not exist"); DBGDie *fdie = GetOrCreateFuncDeclDie(func); die->AddSubVec(fdie); + if (func->GetBody()) { + // member functions defination, these die are global + DBGDie *fdie = GetOrCreateFuncDefDie(func); + compUnit->AddSubVec(fdie); + } } +} - PopParentDie(); +// shared between struct and union, also used as part by class and interface +DBGDie *DebugInfo::CreateStructTypeDie(GStrIdx strIdx, const MIRStructType *structType, bool update) { + DBGDie *die = nullptr; - // member functions defination, these die are global - for (auto fp : structType->GetMethods()) { - MIRSymbol *symbol = GlobalTables::GetGsymTable().GetSymbolFromStidx(fp.first.Idx()); - ASSERT(symbol && symbol->GetSKind() == kStFunc, "member function symbol not exist"); - MIRFunction *func = symbol->GetValue().mirFunc; - if (!func->GetBody()) { - continue; - } - ASSERT(func, "member function not exist"); - DBGDie *fdie = GetOrCreateFuncDefDie(func, 0); - compUnit->AddSubVec(fdie); + if (update) { + uint32 id = tyIdxDieIdMap[structType->GetTypeIndex().GetIdx()]; + die = idDieMap[id]; + ASSERT(die, "update type die not exist"); + } else { + DwTag tag = structType->GetKind() == kTypeStruct ? DW_TAG_structure_type : DW_TAG_union_type; + die = module->GetMemPool()->New(module, tag); + tyIdxDieIdMap[structType->GetTypeIndex().GetIdx()] = die->GetId(); + } + + if (strIdx.GetIdx() != 0) { + stridxDieIdMap[strIdx.GetIdx()] = die->GetId(); } + compUnit->AddSubVec(die); + + die->AddAttr(DW_AT_decl_line, DW_FORM_data4, kStructDBGSize); + die->AddAttr(DW_AT_name, DW_FORM_strp, strIdx.GetIdx()); + die->AddAttr(DW_AT_byte_size, DW_FORM_data4, kDbgDefaultVal); + die->AddAttr(DW_AT_decl_file, DW_FORM_data4, mplSrcIdx.GetIdx()); + + PushParentDie(die); + + // fields + CreateStructTypeFieldsDies(structType, die); + + // parentFields + CreateStructTypeParentFieldsDies(structType, die); + + // member functions + CreateStructTypeMethodsDies(structType, die); + + PopParentDie(); + return die; } @@ -1589,6 +1618,23 @@ void DebugInfo::ComputeSizeAndOffset(DBGDie *die, uint32 &cuOffset) { } } +bool DebugInfo::IsScopeIdEmited(MIRFunction *func, uint32 scopeId) { + auto it = funcScopeIdStatus.find(func); + if (it == funcScopeIdStatus.end()) { + return false; + } + + auto scopeIdIt = it->second.find(scopeId); + if (scopeIdIt == it->second.end()) { + return false; + } + + if (scopeIdIt->second != kAllEmited) { + return false; + } + return true; +} + void DebugInfo::ClearDebugInfo() { for (auto &funcLstrIdxDieId : funcLstrIdxDieIdMap) { funcLstrIdxDieId.second.clear(); -- Gitee