From 3d68399bc46bd690e0759d206063df0162e10f21 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Thu, 21 Jul 2022 07:31:23 -0700 Subject: [PATCH] add module scope for global alias --- src/mapleall/maple_ir/include/mir_module.h | 6 +++++ src/mapleall/maple_ir/include/mir_parser.h | 6 +++-- src/mapleall/maple_ir/include/mir_scope.h | 2 +- src/mapleall/maple_ir/src/mir_module.cpp | 4 ++++ src/mapleall/maple_ir/src/mir_parser.cpp | 4 ++-- src/mapleall/maple_ir/src/mir_scope.cpp | 6 +++-- src/mapleall/maple_ir/src/parser.cpp | 27 ++++++++++++++++++---- 7 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/mapleall/maple_ir/include/mir_module.h b/src/mapleall/maple_ir/include/mir_module.h index 15609b2e3a..37c54a99b3 100644 --- a/src/mapleall/maple_ir/include/mir_module.h +++ b/src/mapleall/maple_ir/include/mir_module.h @@ -40,6 +40,7 @@ namespace maple { class CallInfo; // circular dependency exists, no other choice class MIRModule; // circular dependency exists, no other choice class MIRBuilder; // circular dependency exists, no other choice +class MIRScope; using MIRModulePtr = MIRModule*; using MIRBuilderPtr = MIRBuilder*; @@ -641,6 +642,10 @@ class MIRModule { return dbgInfo; } + MIRScope *GetScope() const { + return scope; + } + void SetWithDbgInfo(bool v) { withDbgInfo = v; } @@ -717,6 +722,7 @@ class MIRModule { DebugInfo *dbgInfo = nullptr; bool withDbgInfo = false; + MIRScope *scope = nullptr; // for cg in mplt BinaryMplt *binMplt = nullptr; diff --git a/src/mapleall/maple_ir/include/mir_parser.h b/src/mapleall/maple_ir/include/mir_parser.h index c68bec1438..d3d353cddc 100755 --- a/src/mapleall/maple_ir/include/mir_parser.h +++ b/src/mapleall/maple_ir/include/mir_parser.h @@ -48,9 +48,11 @@ class MIRParser { bool ParseLocStmt(StmtNodePtr &stmt); bool ParsePosition(SrcPosition &pos); bool ParseOneScope(MIRScope &scope); - bool ParseScope(StmtNodePtr &stmt); + bool ParseScope(); + bool ParseScopeStmt(StmtNodePtr &stmt); bool ParseOneAlias(GStrIdx &strIdx, MIRAliasVars &aliasVar); - bool ParseAlias(StmtNodePtr &stmt); + bool ParseAlias(); + bool ParseAliasStmt(StmtNodePtr &stmt); uint8 *ParseWordsInfo(uint32 size); bool ParseSwitchCase(int64&, LabelIdx&); bool ParseExprOneOperand(BaseNodePtr &expr); diff --git a/src/mapleall/maple_ir/include/mir_scope.h b/src/mapleall/maple_ir/include/mir_scope.h index d36e55e87a..2c3d883f70 100644 --- a/src/mapleall/maple_ir/include/mir_scope.h +++ b/src/mapleall/maple_ir/include/mir_scope.h @@ -92,7 +92,7 @@ class MIRScope { SrcPosition GetScopeEndPos(const SrcPosition &pos); bool AddScope(MIRScope *scope); - void Dump(int32 indent) const; + void Dump(int32 indent, bool isLocal = true) const; void Dump() const; private: diff --git a/src/mapleall/maple_ir/src/mir_module.cpp b/src/mapleall/maple_ir/src/mir_module.cpp index 1b0771c0dd..6776604e67 100644 --- a/src/mapleall/maple_ir/src/mir_module.cpp +++ b/src/mapleall/maple_ir/src/mir_module.cpp @@ -55,6 +55,7 @@ MIRModule::MIRModule(const std::string &fn) typeNameTab = memPool->New(memPoolAllocator); mirBuilder = memPool->New(this); dbgInfo = memPool->New(this); + scope = memPool->New(this, nullptr); IntrinDesc::InitMIRModule(this); } @@ -290,6 +291,9 @@ void MIRModule::DumpGlobals(bool emitStructureType) const { s->Dump(false, 0); } } + if (scope) { + scope->Dump(0, false); + } } } diff --git a/src/mapleall/maple_ir/src/mir_parser.cpp b/src/mapleall/maple_ir/src/mir_parser.cpp index 53b0c45bee..7b5cb05561 100755 --- a/src/mapleall/maple_ir/src/mir_parser.cpp +++ b/src/mapleall/maple_ir/src/mir_parser.cpp @@ -3479,8 +3479,8 @@ std::map MIRParser::InitFuncPtrMapForPar funcPtrMap[TK_assignassertle] = &MIRParser::ParseNaryStmtAssignAssertLE; funcPtrMap[TK_label] = &MIRParser::ParseStmtLabel; funcPtrMap[TK_LOC] = &MIRParser::ParseLocStmt; - funcPtrMap[TK_SCOPE] = &MIRParser::ParseScope; - funcPtrMap[TK_ALIAS] = &MIRParser::ParseAlias; + funcPtrMap[TK_ALIAS] = &MIRParser::ParseAliasStmt; + funcPtrMap[TK_SCOPE] = &MIRParser::ParseScopeStmt; funcPtrMap[TK_asm] = &MIRParser::ParseStmtAsm; funcPtrMap[TK_safe] = &MIRParser::ParseStmtSafeRegion; funcPtrMap[TK_endsafe] = &MIRParser::ParseStmtSafeRegion; diff --git a/src/mapleall/maple_ir/src/mir_scope.cpp b/src/mapleall/maple_ir/src/mir_scope.cpp index c0882a8238..97e9086e41 100644 --- a/src/mapleall/maple_ir/src/mir_scope.cpp +++ b/src/mapleall/maple_ir/src/mir_scope.cpp @@ -106,7 +106,7 @@ bool MIRScope::AddScope(MIRScope *scope) { return true; } -void MIRScope::Dump(int32 indent) const { +void MIRScope::Dump(int32 indent, bool isLocal) const { SrcPosition low = range.first; SrcPosition high = range.second; PrintIndentation(indent); @@ -121,7 +121,9 @@ void MIRScope::Dump(int32 indent) const { for (auto it : aliasVarMap) { PrintIndentation(indent + 1); - LogInfo::MapleLogger() << "ALIAS %" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.first) + LogInfo::MapleLogger() << "ALIAS " + << (isLocal ? " %" : " $") + << GlobalTables::GetStrTable().GetStringFromStrIdx(it.first) << ((it.second.isLocal) ? " %" : " $") << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.mplStrIdx) << " "; if (it.second.tyIdx) { diff --git a/src/mapleall/maple_ir/src/parser.cpp b/src/mapleall/maple_ir/src/parser.cpp index 088ab64858..e12afd65e1 100644 --- a/src/mapleall/maple_ir/src/parser.cpp +++ b/src/mapleall/maple_ir/src/parser.cpp @@ -2500,12 +2500,23 @@ bool MIRParser::ParseOneScope(MIRScope &scope) { return true; } -bool MIRParser::ParseScope(StmtNodePtr &stmt [[maybe_unused]]) { - MIRScope *scp = mod.CurFunction()->GetScope(); +bool MIRParser::ParseScope() { + MIRScope *scp = nullptr; + MIRFunction *func = mod.CurFunction(); + // check if the first dummy function + if (func->GetPuidxOrigin() == 1) { + scp = mod.GetScope(); + } else { + scp = func->GetScope(); + } bool status = ParseOneScope(*scp); return status; } +bool MIRParser::ParseScopeStmt(StmtNodePtr&) { + return ParseScope(); +} + bool MIRParser::ParseOneAlias(GStrIdx &strIdx, MIRAliasVars &aliasVar) { TokenKind nameTk = lexer.GetTokenKind(); if (nameTk != TK_ALIAS) { @@ -2513,8 +2524,8 @@ bool MIRParser::ParseOneAlias(GStrIdx &strIdx, MIRAliasVars &aliasVar) { return false; } nameTk = lexer.NextToken(); - if (nameTk != TK_lname) { - Error("expect local in ALIAS but get "); + if (nameTk != TK_lname && nameTk != TK_gname) { + Error("expect local or global in ALIAS but get "); return false; } strIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(lexer.GetName()); @@ -2557,7 +2568,7 @@ bool MIRParser::ParseOneAlias(GStrIdx &strIdx, MIRAliasVars &aliasVar) { return true; } -bool MIRParser::ParseAlias(StmtNodePtr &stmt [[maybe_unused]]) { +bool MIRParser::ParseAlias() { GStrIdx strIdx; MIRAliasVars aliasVar; @@ -2567,6 +2578,10 @@ bool MIRParser::ParseAlias(StmtNodePtr &stmt [[maybe_unused]]) { return true; } +bool MIRParser::ParseAliasStmt(StmtNodePtr&) { + return ParseAlias(); +} + uint8 *MIRParser::ParseWordsInfo(uint32 size) { lexer.NextToken(); if (lexer.GetTokenKind() != TK_eqsign) { @@ -2750,6 +2765,8 @@ std::map MIRParser::InitFuncPtrMap funcPtrMap[TK_importpath] = &MIRParser::ParseMIRForImportPath; funcPtrMap[TK_asmdecl] = &MIRParser::ParseMIRForAsmdecl; funcPtrMap[TK_LOC] = &MIRParser::ParseLoc; + funcPtrMap[TK_ALIAS] = &MIRParser::ParseAlias; + funcPtrMap[TK_SCOPE] = &MIRParser::ParseScope; return funcPtrMap; } -- Gitee