From 0435b41a1bc1ef0aa1e13a8614653ae889c62a37 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Mon, 1 Aug 2022 11:42:58 -0700 Subject: [PATCH] support type attributes in alias --- src/mapleall/maple_ir/include/debug_info.h | 11 ++-- src/mapleall/maple_ir/include/mir_scope.h | 1 + src/mapleall/maple_ir/src/debug_info.cpp | 68 +++++++++++++++++++--- src/mapleall/maple_ir/src/mir_scope.cpp | 1 + src/mapleall/maple_ir/src/parser.cpp | 5 ++ 5 files changed, 74 insertions(+), 12 deletions(-) diff --git a/src/mapleall/maple_ir/include/debug_info.h b/src/mapleall/maple_ir/include/debug_info.h index a79fbb431e..7b98a175af 100644 --- a/src/mapleall/maple_ir/include/debug_info.h +++ b/src/mapleall/maple_ir/include/debug_info.h @@ -588,7 +588,6 @@ class DebugInfo { dummyTypeDie(nullptr), lexer(nullptr), maxId(1), - builder(nullptr), mplSrcIdx(0), debugInfoLength(0), curFunction(nullptr), @@ -610,6 +609,8 @@ class DebugInfo { funcScopeIdStatus(std::less(), m->GetMPAllocator().Adapter()), typedefStrIdxDieIdMap(std::less(), m->GetMPAllocator().Adapter()), typedefStrIdxTyIdxMap(std::less(), m->GetMPAllocator().Adapter()), + constTypeDieMap(std::less(), m->GetMPAllocator().Adapter()), + volatileTypeDieMap(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); @@ -757,14 +758,13 @@ class DebugInfo { DBGDie *CreatePointedFuncTypeDie(MIRFuncType *fType); DBGDie *GetOrCreateLabelDie(LabelIdx labid); - DBGDie *GetOrCreateTypeAttrDie(MIRSymbol *sym); - DBGDie *GetOrCreateConstTypeDie(TypeAttrs attr, DBGDie *typedie); - DBGDie *GetOrCreateVolatileTypeDie(TypeAttrs attr, DBGDie *typedie); DBGDie *GetOrCreateFuncDeclDie(MIRFunction *func); DBGDie *GetOrCreateFuncDefDie(MIRFunction *func, uint32 lnum); DBGDie *GetOrCreatePrimTypeDie(MIRType *ty); DBGDie *GetOrCreateTypeDie(TyIdx tyidx); DBGDie *GetOrCreateTypeDie(MIRType *type); + DBGDie *GetOrCreateTypeDie(AttrKind attr, DBGDie *typedie); + DBGDie *GetOrCreateTypeDie(TypeAttrs attrs, DBGDie *typedie); DBGDie *GetOrCreatePointTypeDie(const MIRPtrType *ptrType); DBGDie *GetOrCreateArrayTypeDie(const MIRArrayType *arrayType); DBGDie *GetOrCreateStructTypeDie(const MIRType *type); @@ -821,7 +821,6 @@ class DebugInfo { DBGDie *dummyTypeDie; // workaround for unknown types MIRLexer *lexer; uint32 maxId; - DBGBuilder *builder; GStrIdx mplSrcIdx; uint32 debugInfoLength; MIRFunction *curFunction; @@ -854,6 +853,8 @@ class DebugInfo { /* alias type */ MapleMap typedefStrIdxDieIdMap; MapleMap typedefStrIdxTyIdxMap; + MapleMap constTypeDieMap; + MapleMap volatileTypeDieMap; 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 2832dedb22..b889c5cefd 100644 --- a/src/mapleall/maple_ir/include/mir_scope.h +++ b/src/mapleall/maple_ir/include/mir_scope.h @@ -33,6 +33,7 @@ struct MIRAliasVars { unsigned index; bool isLocal; GStrIdx sigStrIdx; + TypeAttrs attrs; }; class MIRScope { diff --git a/src/mapleall/maple_ir/src/debug_info.cpp b/src/mapleall/maple_ir/src/debug_info.cpp index f9c23cf8bf..ad8d3aeafe 100644 --- a/src/mapleall/maple_ir/src/debug_info.cpp +++ b/src/mapleall/maple_ir/src/debug_info.cpp @@ -359,23 +359,30 @@ void DebugInfo::AddAliasDies(MapleMap &aliasMap, bool isL uint32 index = i.second.index; switch (i.second.atk) { case kATKType: { - (void)(vdie->SetAttr(DW_AT_type, index)); + DBGDie *tdie = GetOrCreateTypeDie(TyIdx(index)); + DBGDie *newdie = GetOrCreateTypeDie(i.second.attrs, tdie); + (void)(vdie->SetAttr(DW_AT_type, -newdie->GetId())); break; } case kATKString: { // use src code type - DBGDie *typedefDie = GetOrCreateTypedefDie(GStrIdx(index), var->GetTyIdx()); + DBGDie *tdie = GetOrCreateTypedefDie(GStrIdx(index), var->GetTyIdx()); + DBGDie *newdie = GetOrCreateTypeDie(i.second.attrs, tdie); // use negtive number to indicate DIE id instead of tyidx in normal cases - (void)(vdie->SetAttr(DW_AT_type, -typedefDie->GetId())); + (void)(vdie->SetAttr(DW_AT_type, -newdie->GetId())); break; } case kATKEnum: { // use src code enum type - DBGDie *enumDie = GetOrCreateEnumTypeDie(index); + DBGDie *tdie = GetOrCreateEnumTypeDie(index); + DBGDie *newdie = GetOrCreateTypeDie(i.second.attrs, tdie); // use negtive number to indicate DIE id instead of tyidx in normal cases - (void)(vdie->SetAttr(DW_AT_type, -enumDie->GetId())); + (void)(vdie->SetAttr(DW_AT_type, -newdie->GetId())); break; } + default: + ASSERT(false, "unknown alias type kind"); + break; } // for local scope @@ -701,8 +708,10 @@ DBGDie *DebugInfo::CreateVarDie(MIRSymbol *sym, GStrIdx strIdx) { } MIRType *type = sym->GetType(); - (void)GetOrCreateTypeDie(type); - die->AddAttr(DW_AT_type, DW_FORM_ref4, type->GetTypeIndex().GetIdx()); + DBGDie *tdie = GetOrCreateTypeDie(type); + DBGDie *newdie = GetOrCreateTypeDie(sym->GetAttrs(), tdie); + int index = (newdie == tdie ? type->GetTypeIndex().GetIdx() : -newdie->GetId()); + die->AddAttr(DW_AT_type, DW_FORM_ref4, index); return die; } @@ -885,6 +894,51 @@ DBGDie *DebugInfo::CreatePointedFuncTypeDie(MIRFuncType *fType) { return die; } +DBGDie *DebugInfo::GetOrCreateTypeDie(AttrKind attr, DBGDie *tdie) { + DBGDie *die = tdie; + uint32 did = tdie->GetId(); + uint32 newid = 0; + switch (attr) { + case ATTR_const: + if (constTypeDieMap.find(did) != constTypeDieMap.end()) { + newid = constTypeDieMap[did]; + die = idDieMap[newid]; + } else { + die = module->GetMemPool()->New(module, DW_TAG_const_type); + (void)(die->AddAttr(DW_AT_type, DW_FORM_ref4, -did)); + compUnit->AddSubVec(die); + newid = die->GetId(); + constTypeDieMap[did] = newid; + } + break; + case ATTR_volatile: + if (volatileTypeDieMap.find(did) != volatileTypeDieMap.end()) { + newid = volatileTypeDieMap[did]; + die = idDieMap[newid]; + } else { + die = module->GetMemPool()->New(module, DW_TAG_volatile_type); + (void)(die->AddAttr(DW_AT_type, DW_FORM_ref4, -did)); + compUnit->AddSubVec(die); + newid = die->GetId(); + volatileTypeDieMap[did] = newid; + } + break; + default: + break; + } + return die; +} + +DBGDie *DebugInfo::GetOrCreateTypeDie(TypeAttrs attrs, DBGDie *tdie) { + if (attrs.GetAttr(ATTR_const)) { + tdie = GetOrCreateTypeDie(ATTR_const, tdie); + } + if (attrs.GetAttr(ATTR_volatile)) { + tdie = GetOrCreateTypeDie(ATTR_volatile, tdie); + } + return tdie; +} + DBGDie *DebugInfo::GetOrCreateTypeDie(TyIdx tyidx) { MIRType *type = GlobalTables::GetTypeTable().GetTypeFromTyIdx(tyidx); return GetOrCreateTypeDie(type); diff --git a/src/mapleall/maple_ir/src/mir_scope.cpp b/src/mapleall/maple_ir/src/mir_scope.cpp index a7e739b9d4..2883ecc556 100644 --- a/src/mapleall/maple_ir/src/mir_scope.cpp +++ b/src/mapleall/maple_ir/src/mir_scope.cpp @@ -147,6 +147,7 @@ void MIRScope::Dump(int32 indent, bool isLocal) const { default : break; } + it.second.attrs.DumpAttributes(); 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 8df870d462..c04bf3b41c 100644 --- a/src/mapleall/maple_ir/src/parser.cpp +++ b/src/mapleall/maple_ir/src/parser.cpp @@ -2652,6 +2652,11 @@ bool MIRParser::ParseOneAlias(GStrIdx &strIdx, MIRAliasVars &aliasVar) { Error("parseType failed when parsing ALIAS "); return false; } + // parse type attributes + if (!ParseTypeAttrs(aliasVar.attrs)) { + Error("bad type attribute for alias at "); + return false; + } GStrIdx signStrIdx(0); if (lexer.GetTokenKind() == TK_string) { signStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(lexer.GetName()); -- Gitee