From 3f1ee98ee6f5eead6aa2d9253cc6d62ebe219142 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Mon, 11 Jul 2022 16:21:40 -0700 Subject: [PATCH] enable ALIAS to indicate source type --- src/mapleall/maple_ir/include/debug_info.h | 7 ++++ src/mapleall/maple_ir/include/mir_scope.h | 3 +- src/mapleall/maple_ir/src/debug_info.cpp | 40 ++++++++++++++++++++++ src/mapleall/maple_ir/src/mir_scope.cpp | 6 +++- src/mapleall/maple_ir/src/parser.cpp | 19 +++++++--- 5 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/mapleall/maple_ir/include/debug_info.h b/src/mapleall/maple_ir/include/debug_info.h index e11b3c59dc..bd1eb3acc2 100644 --- a/src/mapleall/maple_ir/include/debug_info.h +++ b/src/mapleall/maple_ir/include/debug_info.h @@ -608,6 +608,7 @@ class DebugInfo { funcScopeLows(std::less(), m->GetMPAllocator().Adapter()), funcScopeHighs(std::less(), m->GetMPAllocator().Adapter()), funcScopeIdStatus(std::less(), m->GetMPAllocator().Adapter()), + typedefStrIdxDieIdMap(std::less(), m->GetMPAllocator().Adapter()), strps(std::less(), m->GetMPAllocator().Adapter()) { /* valid entry starting from index 1 as abbrevid starting from 1 as well */ abbrevVec.push_back(nullptr); @@ -803,6 +804,9 @@ class DebugInfo { return true; } + // src code type name stridx and aliased maple var + DBGDie *GetOrCreateTypeDefDie(GStrIdx stridx, MIRSymbol *var); + private: MIRModule *module; DBGDie *compUnit; // root die: compilation unit @@ -839,6 +843,9 @@ class DebugInfo { /* save functions's scope id that has been emited */ MapleMap> funcScopeIdStatus; + /* alias type */ + MapleMap typedefStrIdxDieIdMap; + MapleSet strps; std::string varPtrPrefix; }; diff --git a/src/mapleall/maple_ir/include/mir_scope.h b/src/mapleall/maple_ir/include/mir_scope.h index dca207e013..38a6f5cc1f 100644 --- a/src/mapleall/maple_ir/include/mir_scope.h +++ b/src/mapleall/maple_ir/include/mir_scope.h @@ -22,8 +22,9 @@ namespace maple { // mapping src variable to mpl variables to display debug info struct MIRAliasVars { - GStrIdx mplStrIdx; // maple varialbe name + GStrIdx mplStrIdx; // maple varialbe name TyIdx tyIdx; + GStrIdx srcTypeStrIdx; // src type name bool isLocal; GStrIdx sigStrIdx; }; diff --git a/src/mapleall/maple_ir/src/debug_info.cpp b/src/mapleall/maple_ir/src/debug_info.cpp index c47baa1a8a..6a33cd7138 100644 --- a/src/mapleall/maple_ir/src/debug_info.cpp +++ b/src/mapleall/maple_ir/src/debug_info.cpp @@ -319,8 +319,18 @@ void DebugInfo::AddAliasDies(MapleMap &aliasMap) { continue; } + // use src code type name if provided + DBGDie *typedefDie = nullptr; + if (i.second.srcTypeStrIdx.GetIdx()) { + typedefDie = GetOrCreateTypeDefDie(i.second.srcTypeStrIdx, var); + } + // create alias die using maple var except name DBGDie *vdie = CreateVarDie(var, i.first); + if (typedefDie) { + // use negtive number to indicate DIE id instead of tyidx in normal cases + vdie->SetAttr(DW_AT_type, -typedefDie->GetId()); + } // link vdie's ExprLoc to mdie's vdie->LinkExprLoc(mdie); @@ -843,6 +853,31 @@ DBGDie *DebugInfo::GetOrCreateTypeDie(MIRType *type) { return die; } +DBGDie *DebugInfo::GetOrCreateTypeDefDie(GStrIdx stridx, MIRSymbol *var) { + uint32 sid = stridx.GetIdx(); + if (typedefStrIdxDieIdMap.find(sid) != typedefStrIdxDieIdMap.end()) { + uint32 id = typedefStrIdxDieIdMap[sid]; + return idDieMap[id]; + } + + DBGDie *die = module->GetMemPool()->New(module, DW_TAG_typedef); + compUnit->AddSubVec(die); + + die->AddAttr(DW_AT_name, DW_FORM_strp, sid); + die->AddAttr(DW_AT_decl_file, DW_FORM_data1, var->GetSrcPosition().FileNum()); + die->AddAttr(DW_AT_decl_line, DW_FORM_data1, var->GetSrcPosition().LineNum()); + die->AddAttr(DW_AT_decl_column, DW_FORM_data1, var->GetSrcPosition().Column()); + + MIRType *type = var->GetType(); + DBGDie *tdie = GetOrCreateTypeDie(type); + + // use negative vaule of Die id + die->AddAttr(DW_AT_type, DW_FORM_ref4, -tdie->GetId()); + + typedefStrIdxDieIdMap[sid] = die->GetId(); + return die; +} + DBGDie *DebugInfo::GetOrCreatePointTypeDie(const MIRPtrType *ptrType) { uint32 tid = ptrType->GetTypeIndex().GetIdx(); if (tyIdxDieIdMap.find(tid) != tyIdxDieIdMap.end()) { @@ -1242,6 +1277,11 @@ void DebugInfo::FillTypeAttrWithDieId() { for (auto at : die->GetAttrVec()) { if (at->GetDwAt() == DW_AT_type) { uint32 tid = at->GetId(); + // handle typedef where die id is already used but with nagetive value + if ((int)tid < 0) { + at->SetId(-(int)tid); + break; + } MIRType *type = GlobalTables::GetTypeTable().GetTypeFromTyIdx(TyIdx(tid)); if (type) { uint32 dieid = tyIdxDieIdMap[tid]; diff --git a/src/mapleall/maple_ir/src/mir_scope.cpp b/src/mapleall/maple_ir/src/mir_scope.cpp index d675529a61..9f0e3e41a6 100644 --- a/src/mapleall/maple_ir/src/mir_scope.cpp +++ b/src/mapleall/maple_ir/src/mir_scope.cpp @@ -124,7 +124,11 @@ void MIRScope::Dump(int32 indent) const { LogInfo::MapleLogger() << "ALIAS %" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.first) << ((it.second.isLocal) ? " %" : " $") << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.mplStrIdx) << " "; - GlobalTables::GetTypeTable().GetTypeFromTyIdx(it.second.tyIdx)->Dump(0); + if (it.second.tyIdx) { + GlobalTables::GetTypeTable().GetTypeFromTyIdx(it.second.tyIdx)->Dump(0); + } else { + LogInfo::MapleLogger() << "\"" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.srcTypeStrIdx) << "\""; + } if (it.second.sigStrIdx) { LogInfo::MapleLogger() << " \"" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.sigStrIdx) << "\""; } diff --git a/src/mapleall/maple_ir/src/parser.cpp b/src/mapleall/maple_ir/src/parser.cpp index d4e3d2828a..7bf9003ae4 100644 --- a/src/mapleall/maple_ir/src/parser.cpp +++ b/src/mapleall/maple_ir/src/parser.cpp @@ -2526,9 +2526,23 @@ bool MIRParser::ParseOneAlias(GStrIdx &strIdx, MIRAliasVars &aliasVar) { return false; } GStrIdx mplStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(lexer.GetName()); + aliasVar.mplStrIdx = mplStrIdx; + aliasVar.isLocal = isLocal; lexer.NextToken(); + TokenKind tk = lexer.GetTokenKind(); TyIdx tyIdx(0); - if (!ParseType(tyIdx)) { + if (ParseType(tyIdx)) { + aliasVar.tyIdx = tyIdx; + aliasVar.srcTypeStrIdx = GStrIdx(0); + } else if (tk == TK_string) { + /* it is original type name from source code */ + std::string typeName = lexer.GetName(); + GStrIdx srcTypeStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(typeName); + + aliasVar.tyIdx = TyIdx(0); + aliasVar.srcTypeStrIdx = srcTypeStrIdx; + lexer.NextToken(); + } else { Error("parseType failed when parsing ALIAS "); return false; } @@ -2537,9 +2551,6 @@ bool MIRParser::ParseOneAlias(GStrIdx &strIdx, MIRAliasVars &aliasVar) { signStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(lexer.GetName()); lexer.NextToken(); } - aliasVar.mplStrIdx = mplStrIdx; - aliasVar.tyIdx = tyIdx; - aliasVar.isLocal = isLocal; aliasVar.sigStrIdx = signStrIdx; return true; } -- Gitee