From b4ee89aa3c0e692337f723673bbe2079f8359c55 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Fri, 22 Jul 2022 07:20:24 -0700 Subject: [PATCH] support enum type for ALIAS --- .../bytecode_input/common/src/bc_function.cpp | 3 +- src/hir2mpl/common/src/fe_function.cpp | 9 +++-- src/mapleall/maple_ir/include/mir_scope.h | 10 ++++-- src/mapleall/maple_ir/src/bin_func_export.cpp | 3 +- src/mapleall/maple_ir/src/bin_func_import.cpp | 3 +- src/mapleall/maple_ir/src/debug_info.cpp | 5 +-- src/mapleall/maple_ir/src/mir_scope.cpp | 24 ++++++++++--- src/mapleall/maple_ir/src/parser.cpp | 35 ++++++++++++++----- 8 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/hir2mpl/bytecode_input/common/src/bc_function.cpp b/src/hir2mpl/bytecode_input/common/src/bc_function.cpp index f316a33783..e6e816f6e9 100644 --- a/src/hir2mpl/bytecode_input/common/src/bc_function.cpp +++ b/src/hir2mpl/bytecode_input/common/src/bc_function.cpp @@ -86,7 +86,8 @@ bool BCFunction::GenerateAliasVars(const std::string &phaseName) { namemangler::EncodeName(std::get<0>(item))); MIRAliasVars aliasVar; aliasVar.mplStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(localVar->GetName(*mirType)); - aliasVar.tyIdx = mirType->GetTypeIndex(); + aliasVar.atk = ATK_type; + aliasVar.index = mirType->GetTypeIndex().GetIdx(); aliasVar.isLocal = !localVar->IsGlobal(); if (!std::get<2>(item).empty()) { aliasVar.sigStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(std::get<2>(item)); diff --git a/src/hir2mpl/common/src/fe_function.cpp b/src/hir2mpl/common/src/fe_function.cpp index 9a8e01aab3..b9cad3e24b 100644 --- a/src/hir2mpl/common/src/fe_function.cpp +++ b/src/hir2mpl/common/src/fe_function.cpp @@ -858,11 +858,14 @@ void FEFunction::AddAliasInMIRScope(MIRScope *scope, const std::string &srcVarNa aliasVar.mplStrIdx = symbol->GetNameStrIdx(); aliasVar.isLocal = symbol->IsLocal(); if (sourceType != nullptr) { - aliasVar.tyIdx = sourceType->GetTypeIndex(); + aliasVar.atk = ATK_type; + aliasVar.index = sourceType->GetTypeIndex().GetIdx(); } else if (typeNameIdx != 0) { - aliasVar.srcTypeStrIdx = typeNameIdx; + aliasVar.atk = ATK_string; + aliasVar.index = typeNameIdx.GetIdx(); } else { - aliasVar.tyIdx = symbol->GetTyIdx(); + aliasVar.atk = ATK_type; + aliasVar.index = symbol->GetTyIdx().GetIdx(); } scope->SetAliasVarMap(nameIdx, aliasVar); }; diff --git a/src/mapleall/maple_ir/include/mir_scope.h b/src/mapleall/maple_ir/include/mir_scope.h index d36e55e87a..1d86431359 100644 --- a/src/mapleall/maple_ir/include/mir_scope.h +++ b/src/mapleall/maple_ir/include/mir_scope.h @@ -21,10 +21,16 @@ namespace maple { // mapping src variable to mpl variables to display debug info +enum AliasTypeKind { + ATK_type, + ATK_string, + ATK_enum, +}; + struct MIRAliasVars { GStrIdx mplStrIdx; // maple varialbe name - TyIdx tyIdx; - GStrIdx srcTypeStrIdx; // src type name + AliasTypeKind atk; + unsigned index; bool isLocal; GStrIdx sigStrIdx; }; diff --git a/src/mapleall/maple_ir/src/bin_func_export.cpp b/src/mapleall/maple_ir/src/bin_func_export.cpp index cffdf1e75e..cf06821546 100644 --- a/src/mapleall/maple_ir/src/bin_func_export.cpp +++ b/src/mapleall/maple_ir/src/bin_func_export.cpp @@ -138,7 +138,8 @@ void BinaryMplExport::OutputAliasMap(MapleMap &aliasVarMa for (std::pair it : aliasVarMap) { OutputStr(it.first); OutputStr(it.second.mplStrIdx); - OutputType(it.second.tyIdx); + WriteNum(static_cast(it.second.atk)); + WriteNum(it.second.index); OutputStr(it.second.sigStrIdx); } } diff --git a/src/mapleall/maple_ir/src/bin_func_import.cpp b/src/mapleall/maple_ir/src/bin_func_import.cpp index b83f41af7f..58f792bfa4 100644 --- a/src/mapleall/maple_ir/src/bin_func_import.cpp +++ b/src/mapleall/maple_ir/src/bin_func_import.cpp @@ -153,7 +153,8 @@ void BinaryMplImport::ImportAliasMap(MIRFunction *func) { MIRAliasVars aliasvars; GStrIdx strIdx = ImportStr(); aliasvars.mplStrIdx = ImportStr(); - aliasvars.tyIdx = ImportType(); + aliasvars.atk = static_cast(ReadNum()); + aliasvars.index = static_cast(ReadNum()); (void)ImportStr(); // not assigning to mimic parser func->GetAliasVarMap()[strIdx] = aliasvars; } diff --git a/src/mapleall/maple_ir/src/debug_info.cpp b/src/mapleall/maple_ir/src/debug_info.cpp index ebba03fd8f..b3043f6327 100644 --- a/src/mapleall/maple_ir/src/debug_info.cpp +++ b/src/mapleall/maple_ir/src/debug_info.cpp @@ -352,8 +352,9 @@ void DebugInfo::AddAliasDies(MapleMap &aliasMap) { DBGDie *vdie = CreateVarDie(var, i.first); // use src code type name for type if provided - if (i.second.srcTypeStrIdx.GetIdx()) { - DBGDie *typedefDie = GetOrCreateTypedefDie(i.second.srcTypeStrIdx, var->GetTyIdx()); + if (i.second.atk == ATK_string) { + GStrIdx idx(i.second.index); + DBGDie *typedefDie = GetOrCreateTypedefDie(idx, var->GetTyIdx()); // use negtive number to indicate DIE id instead of tyidx in normal cases (void)(vdie->SetAttr(DW_AT_type, -typedefDie->GetId())); } diff --git a/src/mapleall/maple_ir/src/mir_scope.cpp b/src/mapleall/maple_ir/src/mir_scope.cpp index c0882a8238..0f5659dad2 100644 --- a/src/mapleall/maple_ir/src/mir_scope.cpp +++ b/src/mapleall/maple_ir/src/mir_scope.cpp @@ -124,11 +124,25 @@ void MIRScope::Dump(int32 indent) const { LogInfo::MapleLogger() << "ALIAS %" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.first) << ((it.second.isLocal) ? " %" : " $") << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.mplStrIdx) << " "; - if (it.second.tyIdx) { - GlobalTables::GetTypeTable().GetTypeFromTyIdx(it.second.tyIdx)->Dump(0); - } else { - LogInfo::MapleLogger() << "\"" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.srcTypeStrIdx) - << "\""; + switch (it.second.atk) { + case ATK_type: { + TyIdx idx(it.second.index); + GlobalTables::GetTypeTable().GetTypeFromTyIdx(TyIdx(idx))->Dump(0); + break; + } + case ATK_string: { + GStrIdx idx(it.second.index); + LogInfo::MapleLogger() << "\"" << GlobalTables::GetStrTable().GetStringFromStrIdx(idx) + << "\""; + break; + } + case ATK_enum: { + MIREnum *mirEnum = GlobalTables::GetEnumTable().enumTable[it.second.index]; + LogInfo::MapleLogger() << "$" << GlobalTables::GetStrTable().GetStringFromStrIdx(mirEnum->nameStrIdx); + break; + } + default : + break; } 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 746e1c8d64..552d22e162 100644 --- a/src/mapleall/maple_ir/src/parser.cpp +++ b/src/mapleall/maple_ir/src/parser.cpp @@ -2596,31 +2596,48 @@ bool MIRParser::ParseOneAlias(GStrIdx &strIdx, MIRAliasVars &aliasVar) { } strIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(lexer.GetName()); nameTk = lexer.NextToken(); - bool isLocal; if (nameTk == TK_lname) { - isLocal = true; + aliasVar.isLocal = true; } else if (nameTk == TK_gname) { - isLocal = false; + aliasVar.isLocal = false; } else { Error("expect name in ALIAS but get "); 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 (tk == TK_string || tk == TK_gname) { + if (tk == TK_string) { // original type name from source code std::string typeName = lexer.GetName(); GStrIdx srcTypeStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(typeName); - aliasVar.tyIdx = TyIdx(0); - aliasVar.srcTypeStrIdx = srcTypeStrIdx; + aliasVar.atk = ATK_string; + aliasVar.index = srcTypeStrIdx.GetIdx(); + lexer.NextToken(); + } else if (tk == TK_gname) { + // type name or enum name + std::string typeName = lexer.GetName(); + GStrIdx strIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(typeName); + TyIdx tyIdx = mod.GetTypeNameTab()->GetTyIdxFromGStrIdx(strIdx); + if (tyIdx.GetIdx() != 0) { + aliasVar.atk = ATK_type; + aliasVar.index = tyIdx.GetIdx(); + } else { + unsigned size = GlobalTables::GetEnumTable().enumTable.size(); + for (unsigned i = 0; i < size; ++i) { + MIREnum *mirEnum = GlobalTables::GetEnumTable().enumTable[i]; + if (strIdx == mirEnum->nameStrIdx) { + aliasVar.atk = ATK_enum; + aliasVar.index = i; + } + } + } lexer.NextToken(); } else if (ParseType(tyIdx)) { - aliasVar.tyIdx = tyIdx; - aliasVar.srcTypeStrIdx = GStrIdx(0); + aliasVar.atk = ATK_type; + aliasVar.index = tyIdx.GetIdx(); } else { Error("parseType failed when parsing ALIAS "); return false; -- Gitee