diff --git a/src/mapleall/maple_ir/include/debug_info.h b/src/mapleall/maple_ir/include/debug_info.h index 58188a05dcf121c00866471e1bb020b608a76aa5..fd5e6874b747107d4198c2f35aa0be3715889711 100644 --- a/src/mapleall/maple_ir/include/debug_info.h +++ b/src/mapleall/maple_ir/include/debug_info.h @@ -21,6 +21,7 @@ #include "types_def.h" #include "prim_types.h" #include "mir_nodes.h" +#include "mir_scope.h" #include "namemangler.h" #include "lexer.h" #include "Dwarf.h" @@ -601,7 +602,6 @@ class DebugInfo { void Finish(); void SetupCU(); void BuildDebugInfo(); - void BuildAliasDIEs(); void Dump(int indent); // build tree to populate withChildren, sibling, firstChild @@ -696,6 +696,7 @@ class DebugInfo { DBGDieAttr *CreateAttr(DwAt attr, DwForm form, uint64 val); 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 idx); @@ -716,6 +717,9 @@ class DebugInfo { DBGDie *GetOrCreateArrayTypeDie(const MIRArrayType *type); DBGDie *GetOrCreateStructTypeDie(const MIRType *type); + void AddAliasDies(MapleMap &aliasMap); + void AddScopeDie(MIRScope *scope); + // Functions for calculating the size and offset of each DW_TAG_xxx and DW_AT_xxx void ComputeSizeAndOffsets(); void ComputeSizeAndOffset(DBGDie *die, uint32 &offset); diff --git a/src/mapleall/maple_ir/include/mir_scope.h b/src/mapleall/maple_ir/include/mir_scope.h index 2d144d4f258402c05f375539b1850b995b7db0b8..2ba08c45f360a61da7057e1b1eb6631376138c29 100644 --- a/src/mapleall/maple_ir/include/mir_scope.h +++ b/src/mapleall/maple_ir/include/mir_scope.h @@ -38,6 +38,10 @@ class MIRScope { bool IsSubScope(const MIRScope *s) const; bool HasJoinScope(const MIRScope *s1, const MIRScope *s2) const; + unsigned GetLevel() const { + return level; + } + const SrcPosition &GetRangeLow() const { return range.first; } @@ -60,6 +64,10 @@ class MIRScope { return aliasVarMap; } + MapleVector &GetSubScopes() { + return subScopes; + } + void IncLevel(); bool AddScope(MIRScope *scope); void Dump(int32 indent) const; diff --git a/src/mapleall/maple_ir/src/debug_info.cpp b/src/mapleall/maple_ir/src/debug_info.cpp index 7e4ffa173fec4b0a7b6fd842ff0ce5f651f57c62..e9836992cf8d7675ef63cf37a1b08f26c3b50507 100644 --- a/src/mapleall/maple_ir/src/debug_info.cpp +++ b/src/mapleall/maple_ir/src/debug_info.cpp @@ -260,41 +260,56 @@ void DebugInfo::SetupCU() { compUnit->AddAttr(DW_AT_stmt_list, DW_FORM_sec_offset, kDbgDefaultVal); } -void DebugInfo::BuildAliasDIEs() { - for (auto it : funcLstrIdxDieIdMap) { - MIRFunction *func = it.first; - for (std::pair i : func->GetAliasVarMap()) { - DBGDie *die = GetLocalDie(func, i.second.memPoolStrIdx); - // this local mpl variable does not exist because it is optimized away - // by register renameing. please use mplme -O1 instead of -O2 - if (!die) { - continue; - } - DBGDie *aliasdie = module->GetMemPool()->New(module, DW_TAG_variable); +void DebugInfo::AddScopeDie(MIRScope *scope) { + if (!scope->NeedEmitAliasInfo()) { + return; + } - // clone attributes, note DBGExprLoc pointer is copied as well - // so the fboffset are the same as aliased maple variable - for (auto attr : die->GetAttrVec()) { - aliasdie->AddAttr(attr->GetDwAt(), attr->GetDwForm(), attr->GetU()); - } - // update name with aliased src variable name - aliasdie->SetAttr(DW_AT_name, i.first.GetIdx()); - aliasdie->SetParent(die->GetParent()); + if (scope->GetLevel() != 0) { + DBGDie *die = module->GetMemPool()->New(module, DW_TAG_lexical_block); + die->AddAttr(DW_AT_low_pc, DW_FORM_addr, kDbgDefaultVal); + die->AddAttr(DW_AT_high_pc, DW_FORM_data8, kDbgDefaultVal); - // add alias var name to debug_str section - strps.insert(i.first.GetIdx()); + // add die to parent + GetParentDie()->AddSubVec(die); + + PushParentDie(die); + } - uint32 funcdieid = stridxDieIdMap[func->GetNameStrIdx().GetIdx()]; - DBGDie *funcdie = idDieMap[funcdieid]; - funcdie->AddSubVec(aliasdie); + // process aliasVarMap + AddAliasDies(scope->GetAliasVarMap()); + + if (scope->GetSubScopes().size() > 0) { + // process subScopes + for (auto it : scope->GetSubScopes()) { + AddScopeDie(it); } } + + if (scope->GetLevel() != 0) { + PopParentDie(); + } +} + +void DebugInfo::AddAliasDies(MapleMap &aliasMap) { + MIRFunction *func = GetCurFunction(); + for (std::pair i : aliasMap) { + // maple var + MIRSymbol *var = func->GetSymTab()->GetSymbolFromStrIdx(i.second.memPoolStrIdx); + + // create alias die using maple var except name + DBGDie *vdie = CreateVarDie(var, i.first); + + GetParentDie()->AddSubVec(vdie); + + // add alias var name to debug_str section + strps.insert(i.first.GetIdx()); + } } void DebugInfo::Finish() { SetupCU(); FillTypeAttrWithDieId(); - BuildAliasDIEs(); // build tree from root DIE compUnit BuildDieTree(); BuildAbbrev(); @@ -467,32 +482,49 @@ DBGDie *DebugInfo::CreateVarDie(MIRSymbol *sym) { } bool isLocal = sym->IsLocal(); + GStrIdx strIdx = sym->GetNameStrIdx(); + if (isLocal) { MIRFunction *func = GetCurFunction(); if ((funcLstrIdxDieIdMap[func]).size() && - (funcLstrIdxDieIdMap[func]).find(sym->GetNameStrIdx().GetIdx()) != (funcLstrIdxDieIdMap[func]).end()) { - return GetLocalDie(sym->GetNameStrIdx()); + (funcLstrIdxDieIdMap[func]).find(strIdx.GetIdx()) != (funcLstrIdxDieIdMap[func]).end()) { + return GetLocalDie(strIdx); } } else { - if (stridxDieIdMap.find(sym->GetNameStrIdx().GetIdx()) != stridxDieIdMap.end()) { - uint32 id = stridxDieIdMap[sym->GetNameStrIdx().GetIdx()]; + if (stridxDieIdMap.find(strIdx.GetIdx()) != stridxDieIdMap.end()) { + uint32 id = stridxDieIdMap[strIdx.GetIdx()]; return idDieMap[id]; } } + DBGDie *die = CreateVarDie(sym, strIdx); + + GetParentDie()->AddSubVec(die); + if (isLocal) { + SetLocalDie(strIdx, die); + } else { + stridxDieIdMap[strIdx.GetIdx()] = die->GetId(); + } + + return die; +} + + +DBGDie *DebugInfo::CreateVarDie(MIRSymbol *sym, GStrIdx strIdx) { DBGDie *die = module->GetMemPool()->New(module, DW_TAG_variable); /* var Name */ - die->AddAttr(DW_AT_name, DW_FORM_strp, sym->GetNameStrIdx().GetIdx()); + die->AddAttr(DW_AT_name, DW_FORM_strp, strIdx.GetIdx()); die->AddAttr(DW_AT_decl_file, DW_FORM_data4, sym->GetSrcPosition().FileNum()); die->AddAttr(DW_AT_decl_line, DW_FORM_data4, sym->GetSrcPosition().LineNum()); die->AddAttr(DW_AT_decl_column, DW_FORM_data4, sym->GetSrcPosition().Column()); + bool isLocal = sym->IsLocal(); if (isLocal) { die->AddSimpLocAttr(DW_AT_location, DW_FORM_exprloc, kDbgDefaultVal); } else { // global var just use its name as address in .s - uint64 idx = sym->GetNameStrIdx().GetIdx(); + uint64 idx = strIdx.GetIdx(); if ((sym->IsReflectionClassInfo() && !sym->IsReflectionArrayClassInfo()) || sym->IsStatic()) { std::string ptrName = varPtrPrefix + sym->GetName(); idx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(ptrName).GetIdx(); @@ -504,12 +536,6 @@ DBGDie *DebugInfo::CreateVarDie(MIRSymbol *sym) { (void)GetOrCreateTypeDie(type); die->AddAttr(DW_AT_type, DW_FORM_ref4, type->GetTypeIndex().GetIdx()); - GetParentDie()->AddSubVec(die); - if (isLocal) { - SetLocalDie(sym->GetNameStrIdx(), die); - } else { - stridxDieIdMap[sym->GetNameStrIdx().GetIdx()] = die->GetId(); - } return die; } @@ -606,6 +632,9 @@ DBGDie *DebugInfo::GetOrCreateFuncDefDie(MIRFunction *func, uint32 lnum) { } } + // add scope die + AddScopeDie(func->GetScope()); + PopParentDie(); return die;