From 67efb2b9395b11133061c515a622b34675cacfb9 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Fri, 3 Jun 2022 11:48:31 -0700 Subject: [PATCH 1/2] add MIRScope --- src/mapleall/maple_ir/BUILD.gn | 1 + src/mapleall/maple_ir/include/mir_function.h | 32 +++--- src/mapleall/maple_ir/include/mir_scope.h | 73 ++++++++++++ src/mapleall/maple_ir/include/src_position.h | 7 ++ src/mapleall/maple_ir/src/mir_function.cpp | 4 + src/mapleall/maple_ir/src/mir_nodes.cpp | 11 +- src/mapleall/maple_ir/src/mir_scope.cpp | 110 +++++++++++++++++++ 7 files changed, 209 insertions(+), 29 deletions(-) create mode 100644 src/mapleall/maple_ir/include/mir_scope.h create mode 100644 src/mapleall/maple_ir/src/mir_scope.cpp diff --git a/src/mapleall/maple_ir/BUILD.gn b/src/mapleall/maple_ir/BUILD.gn index fcef9c96c1..e9d028ba0e 100755 --- a/src/mapleall/maple_ir/BUILD.gn +++ b/src/mapleall/maple_ir/BUILD.gn @@ -33,6 +33,7 @@ src_libmplir = [ "src/mir_symbol_builder.cpp", "src/mir_builder.cpp", "src/mir_const.cpp", + "src/mir_scope.cpp", "src/mir_function.cpp", "src/mir_lower.cpp", "src/mir_module.cpp", diff --git a/src/mapleall/maple_ir/include/mir_function.h b/src/mapleall/maple_ir/include/mir_function.h index 4f51ceaeca..721ad098db 100644 --- a/src/mapleall/maple_ir/include/mir_function.h +++ b/src/mapleall/maple_ir/include/mir_function.h @@ -23,19 +23,13 @@ #include "file_layout.h" #include "mir_nodes.h" #include "mir_type.h" +#include "mir_scope.h" #include "profile.h" #include "func_desc.h" #define DEBUGME true namespace maple { -// mapping src (java) variable to mpl variables to display debug info -struct MIRAliasVars { - GStrIdx memPoolStrIdx; - TyIdx tyIdx; - GStrIdx sigStrIdx; -}; - enum PointerAttr: uint32_t { kPointerUndeiced = 0x1, kPointerNull = 0x2, @@ -74,7 +68,9 @@ class MIRFunction { public: MIRFunction(MIRModule *mod, StIdx idx) : module(mod), - symbolTableIdx(idx) {} + symbolTableIdx(idx) { + scope = module->GetMemPool()->New(mod); + } ~MIRFunction() = default; @@ -82,6 +78,7 @@ class MIRFunction { void DumpUpFormal(int32 indent) const; void DumpFrame(int32 indent) const; void DumpFuncBody(int32 indent); + void DumpScope(); const MIRSymbol *GetFuncSymbol() const; MIRSymbol *GetFuncSymbol(); @@ -722,21 +719,19 @@ class MIRFunction { infoIsString.push_back(isString); } + MIRScope *GetScope() { + return scope; + } + bool NeedEmitAliasInfo() const { - return aliasVarMap != nullptr; + return scope->NeedEmitAliasInfo(); } MapleMap &GetAliasVarMap() { - if (aliasVarMap == nullptr) { - aliasVarMap = module->GetMemPool()->New>(module->GetMPAllocator().Adapter()); - } - return *aliasVarMap; + return scope->aliasVarMap; } void SetAliasVarMap(GStrIdx idx, const MIRAliasVars &vars) { - if (aliasVarMap == nullptr) { - aliasVarMap = module->GetMemPool()->New>(module->GetMPAllocator().Adapter()); - } - (*aliasVarMap)[idx] = vars; + scope->SetAliasVarMap(idx, vars); } bool HasVlaOrAlloca() const { @@ -1289,8 +1284,7 @@ class MIRFunction { uint32 fileIndex = 0; // this function belongs to which file, used by VM for plugin manager MIRInfoVector info{module->GetMPAllocator().Adapter()}; MapleVector infoIsString{module->GetMPAllocator().Adapter()}; // tells if an entry has string value - MapleMap *aliasVarMap = nullptr; // source code alias variables - // for debuginfo + MIRScope *scope = nullptr; MapleMap *freqFirstMap = nullptr; // save bb frequency in its first_stmt, key is stmtId MapleMap *freqLastMap = nullptr; // save bb frequency in its last_stmt, key is stmtId MapleSet referedPregs{module->GetMPAllocator().Adapter()}; diff --git a/src/mapleall/maple_ir/include/mir_scope.h b/src/mapleall/maple_ir/include/mir_scope.h new file mode 100644 index 0000000000..a2d45c53cc --- /dev/null +++ b/src/mapleall/maple_ir/include/mir_scope.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) [2022] Futurewei Technologies, Inc. All rights reverved. + * + * OpenArkCompiler is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR + * FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ +#ifndef MAPLE_IR_INCLUDE_MIR_SCOPE_H +#define MAPLE_IR_INCLUDE_MIR_SCOPE_H +#include "mir_module.h" +#include "mir_type.h" + +namespace maple { +// mapping src variable to mpl variables to display debug info +struct MIRAliasVars { + GStrIdx memPoolStrIdx; + TyIdx tyIdx; + GStrIdx sigStrIdx; +}; + +class MIRScope { + public: + MIRModule *module; + unsigned level = 0; + std::pair range; + // source to maple variable alias + MapleMap aliasVarMap{module->GetMPAllocator().Adapter()}; + MapleVector subScopes{module->GetMPAllocator().Adapter()}; + + MIRScope(MIRModule *mod) : module(mod), level(0) {} + MIRScope(MIRModule *mod, unsigned l) : module(mod), level(l) {} + ~MIRScope() {} + + bool NeedEmitAliasInfo() const { + return aliasVarMap.size() != 0 || subScopes.size() != 0; + } + + bool IsSubScope(MIRScope *s); + bool HasJoinScope(MIRScope *s1, MIRScope *s2); + + SrcPosition GetRangeLow() { + return range.first; + } + + SrcPosition GetRangeHigh() { + return range.second; + } + + void SetRange(SrcPosition low, SrcPosition high) { + ASSERT(low.IsBfOrEq(high), "wrong order of low and high"); + range.first = low; + range.second = high; + } + + void SetAliasVarMap(GStrIdx idx, const MIRAliasVars &vars) { + aliasVarMap[idx] = vars; + } + + void IncLevel(); + bool AddScope(MIRScope *scope); + void Dump(int32 indent) const; + void D() const; +}; + +} // namespace maple +#endif // MAPLE_IR_INCLUDE_MIR_SCOPE_H diff --git a/src/mapleall/maple_ir/include/src_position.h b/src/mapleall/maple_ir/include/src_position.h index 3f7373dd9b..48351ca529 100644 --- a/src/mapleall/maple_ir/include/src_position.h +++ b/src/mapleall/maple_ir/include/src_position.h @@ -74,6 +74,13 @@ class SrcPosition { u.fileColumn.fileNum = i ? i : n; } + // as you read: this->IsBfOrEq(pos) + bool IsBfOrEq(SrcPosition pos) { + return (pos.FileNum() == FileNum() && + ((LineNum() < pos.LineNum()) || + ((LineNum() == pos.LineNum()) && (Column() <= pos.Column())))); + } + private: union { struct { diff --git a/src/mapleall/maple_ir/src/mir_function.cpp b/src/mapleall/maple_ir/src/mir_function.cpp index fa41433771..b90b35f874 100644 --- a/src/mapleall/maple_ir/src/mir_function.cpp +++ b/src/mapleall/maple_ir/src/mir_function.cpp @@ -449,6 +449,10 @@ void MIRFunction::DumpFrame(int32 indent) const { } } +void MIRFunction::DumpScope() { + scope->Dump(0); +} + void MIRFunction::DumpFuncBody(int32 indent) { LogInfo::MapleLogger() << " funcid " << GetPuidxOrigin() << '\n'; diff --git a/src/mapleall/maple_ir/src/mir_nodes.cpp b/src/mapleall/maple_ir/src/mir_nodes.cpp index 0dc8ac87a3..a3088730c5 100755 --- a/src/mapleall/maple_ir/src/mir_nodes.cpp +++ b/src/mapleall/maple_ir/src/mir_nodes.cpp @@ -1310,16 +1310,7 @@ void BlockNode::Dump(int32 indent, const MIRSymbolTable *theSymTab, MIRPregTable } LogInfo::MapleLogger() << '\n'; if (theMIRModule->CurFunction()->NeedEmitAliasInfo()) { - for (std::pair it : theMIRModule->CurFunction()->GetAliasVarMap()) { - LogInfo::MapleLogger() << "ALIAS %" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.first) << " %" - << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.memPoolStrIdx) << " "; - GlobalTables::GetTypeTable().GetTypeFromTyIdx(it.second.tyIdx)->Dump(0); - if (it.second.sigStrIdx) { - LogInfo::MapleLogger() << " \"" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.sigStrIdx) - << "\""; - } - LogInfo::MapleLogger() << '\n'; - } + theMIRModule->CurFunction()->DumpScope(); } } if (srcPosition.FileNum() != 0 && srcPosition.LineNum() != 0 && srcPosition.LineNum() != lastPrintedLineNum && diff --git a/src/mapleall/maple_ir/src/mir_scope.cpp b/src/mapleall/maple_ir/src/mir_scope.cpp new file mode 100644 index 0000000000..2293ce2ee1 --- /dev/null +++ b/src/mapleall/maple_ir/src/mir_scope.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) [2022] Futurewei Technologies, Inc. All rights reverved. + * + * OpenArkCompiler is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR + * FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ +#include "mir_function.h" +#include "printing.h" + +namespace maple { + +// scp is a sub scope +// (low (scp.low, scp.high] high] +bool MIRScope::IsSubScope(MIRScope *scp) { + // special case for function level scope which might not have range specified + if (level == 0) { + return true; + } + SrcPosition l = GetRangeLow(); + SrcPosition h = GetRangeHigh(); + SrcPosition l1 = scp->GetRangeLow(); + SrcPosition h1 = scp->GetRangeHigh(); + return l.IsBfOrEq(l1) && h1.IsBfOrEq(h); +} + +// s1 and s2 has join +// (s1.low (s2.low s1.high] s2.high] +// (s2.low (s1.low s2.high] s1.high] +bool MIRScope::HasJoinScope(MIRScope *scp1, MIRScope *scp2) { + SrcPosition l1 = scp1->GetRangeLow(); + SrcPosition h1 = scp1->GetRangeHigh(); + SrcPosition l2 = scp2->GetRangeLow(); + SrcPosition h2 = scp1->GetRangeHigh(); + return (l1.IsBfOrEq(l2) && l2.IsBfOrEq(h1)) || + (l2.IsBfOrEq(l1) && l1.IsBfOrEq(h2)); +} + +void MIRScope::IncLevel() { + level++; + for (auto s : subScopes) { + s->IncLevel(); + } +} + +bool MIRScope::AddScope(MIRScope *scope) { + // check first if it is valid with parent scope and sibling sub scopes + ASSERT(IsSubScope(scope), "is not a subscope"); + for (auto s : subScopes) { + if (HasJoinScope(s, scope)) { + ASSERT(false, "has join range with another subscope"); + } + } + if (this != module->CurFunction()->GetScope()) { + // skip level incremental if this is function-scope, of level 0, + // as scope is aready starting from 1 + scope->IncLevel(); + } + subScopes.push_back(scope); + return true; +} + +void MIRScope::Dump(int32 indent) const { + unsigned ind = (level != 0); + if (level != 0) { + SrcPosition low = range.first; + SrcPosition high = range.second; + PrintIndentation(indent); + // LogInfo::MapleLogger() << level << " "; + LogInfo::MapleLogger() << "SCOPE <(" << + low.FileNum() << ", " << + low.LineNum() << ", " << + low.Column() << "), (" << + high.FileNum() << ", " << + high.LineNum() << ", " << + high.Column() << ")> {\n"; + } + + for (auto it : aliasVarMap) { + PrintIndentation(indent + ind); + LogInfo::MapleLogger() << "ALIAS %" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.first) << " %" + << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.memPoolStrIdx) << " "; + GlobalTables::GetTypeTable().GetTypeFromTyIdx(it.second.tyIdx)->Dump(0); + if (it.second.sigStrIdx) { + LogInfo::MapleLogger() << " \"" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.sigStrIdx) << "\""; + } + LogInfo::MapleLogger() << '\n'; + } + + for (auto it : subScopes) { + it->Dump(indent + ind); + } + + if (level != 0) { + PrintIndentation(indent); + LogInfo::MapleLogger() << "}\n"; + } +} + +void MIRScope::D() const { + Dump(0); +} +} // namespace maple -- Gitee From c655a8db3ce6dde8546d6aa99708fa91ed773caa Mon Sep 17 00:00:00 2001 From: Wen HU Date: Fri, 3 Jun 2022 12:18:11 -0700 Subject: [PATCH 2/2] update parser to support MIRScope --- src/mapleall/maple_ir/include/keywords.def | 3 +- src/mapleall/maple_ir/include/mir_parser.h | 5 + src/mapleall/maple_ir/src/mir_parser.cpp | 1 + src/mapleall/maple_ir/src/parser.cpp | 161 ++++++++++++++++++++- 4 files changed, 164 insertions(+), 6 deletions(-) diff --git a/src/mapleall/maple_ir/include/keywords.def b/src/mapleall/maple_ir/include/keywords.def index 7aa2960aac..eddff2f80a 100644 --- a/src/mapleall/maple_ir/include/keywords.def +++ b/src/mapleall/maple_ir/include/keywords.def @@ -95,7 +95,8 @@ KEYWORD(importpath) // source position information KEYWORD(LOC) - // debug info source var and mpl var mapping + // scope and source var to mpl var mapping + KEYWORD(SCOPE) KEYWORD(ALIAS) // storage class KEYWORD(pstatic) diff --git a/src/mapleall/maple_ir/include/mir_parser.h b/src/mapleall/maple_ir/include/mir_parser.h index d05c844f31..c699b107d8 100755 --- a/src/mapleall/maple_ir/include/mir_parser.h +++ b/src/mapleall/maple_ir/include/mir_parser.h @@ -18,6 +18,7 @@ #include "lexer.h" #include "mir_nodes.h" #include "mir_preg.h" +#include "mir_scope.h" #include "parser_opt.h" namespace maple { @@ -45,6 +46,10 @@ class MIRParser { bool ParseLoc(); bool ParseLocStmt(StmtNodePtr &stmt); + bool ParsePosition(SrcPosition &pos); + bool ParseOneScope(MIRScope &scope); + bool ParseScope(StmtNodePtr &stmt); + bool ParseOneAlias(GStrIdx &stridx, MIRAliasVars &var); bool ParseAlias(StmtNodePtr &stmt); uint8 *ParseWordsInfo(uint32 size); bool ParseSwitchCase(int64&, LabelIdx&); diff --git a/src/mapleall/maple_ir/src/mir_parser.cpp b/src/mapleall/maple_ir/src/mir_parser.cpp index b4bf8cf4ce..8501dbd106 100755 --- a/src/mapleall/maple_ir/src/mir_parser.cpp +++ b/src/mapleall/maple_ir/src/mir_parser.cpp @@ -3446,6 +3446,7 @@ 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_asm] = &MIRParser::ParseStmtAsm; funcPtrMap[TK_safe] = &MIRParser::ParseStmtSafeRegion; diff --git a/src/mapleall/maple_ir/src/parser.cpp b/src/mapleall/maple_ir/src/parser.cpp index 66c5098672..fdb586fcb0 100644 --- a/src/mapleall/maple_ir/src/parser.cpp +++ b/src/mapleall/maple_ir/src/parser.cpp @@ -17,6 +17,7 @@ #include #include #include "mir_parser.h" +#include "mir_scope.h" #include "mir_function.h" #include "namemangler.h" #include "opcode_info.h" @@ -122,7 +123,7 @@ static bool IsClassInterfaceTypeName(const std::string &nameStr) { } bool MIRParser::IsStatement(TokenKind tk) const { - if (tk == TK_LOC || tk == TK_ALIAS) { + if (tk == TK_LOC || tk == TK_ALIAS || tk == TK_SCOPE) { return true; } Opcode op = GetOpFromToken(tk); @@ -2352,13 +2353,155 @@ bool MIRParser::ParseFuncInfo() { return false; } -bool MIRParser::ParseAlias(StmtNodePtr&) { +bool MIRParser::ParsePosition(SrcPosition &pos) { TokenKind nameTk = lexer.NextToken(); + if (nameTk != TK_lparen) { + Error("expect ( in SrcPos but get "); + return false; + } + nameTk = lexer.NextToken(); + if (nameTk != TK_intconst) { + Error("expect int in SrcPos but get "); + return false; + } + int i = static_cast(lexer.GetTheIntVal()); + pos.SetFileNum(i); + nameTk = lexer.NextToken(); + if (nameTk != TK_coma) { + Error("expect comma in SrcPos but get "); + return false; + } + + nameTk = lexer.NextToken(); + if (nameTk != TK_intconst) { + Error("expect int in SrcPos but get "); + return false; + } + i = static_cast(lexer.GetTheIntVal()); + pos.SetLineNum(i); + nameTk = lexer.NextToken(); + if (nameTk != TK_coma) { + Error("expect comma in SrcPos but get "); + return false; + } + + nameTk = lexer.NextToken(); + if (nameTk != TK_intconst) { + Error("expect int in SrcPos but get "); + return false; + } + i = static_cast(lexer.GetTheIntVal()); + pos.SetColumn(i); + nameTk = lexer.NextToken(); + if (nameTk != TK_rparen) { + Error("expect ) in SrcPos but get "); + return false; + } + + return true; +} + +bool MIRParser::ParseOneScope(MIRScope &scope) { + TokenKind nameTk = lexer.GetTokenKind(); + if (nameTk != TK_SCOPE) { + Error("expect SCOPE but get "); + return false; + } + nameTk = lexer.NextToken(); + if (nameTk != TK_langle) { + Error("expect < in SCOPE but get "); + return false; + } + SrcPosition low; + if (!ParsePosition(low)) { + Error("parsePosition low failed when parsing SCOPE "); + return false; + } + nameTk = lexer.NextToken(); + if (nameTk != TK_coma) { + Error("expect comma in SCOPE but get "); + return false; + } + SrcPosition high; + if (!ParsePosition(high)) { + Error("parsePosition high failed when parsing SCOPE "); + return false; + } + nameTk = lexer.NextToken(); + if (nameTk != TK_rangle) { + Error("expect > in SCOPE but get "); + return false; + } + nameTk = lexer.NextToken(); + if (nameTk != TK_lbrace) { + Error("expect { in SCOPE but get "); + return false; + } + scope.SetRange(low, high); + nameTk = lexer.NextToken(); + while (1) { + bool status = false; + switch (lexer.GetTokenKind()) { + case TK_ALIAS: { + GStrIdx strIdx; + MIRAliasVars aliasVar; + status = ParseOneAlias(strIdx, aliasVar); + if (status) { + scope.SetAliasVarMap(strIdx, aliasVar); + } + break; + } + case TK_SCOPE: { + // initial level 1 + MIRScope *scp = mod.GetMemPool()->New(&mod, 1); + status = ParseOneScope(*scp); + if (status) { + scope.AddScope(scp); + } else { + delete scp; + } + break; + } + default: + break; + } + if (!status) { + break; + } + } + nameTk = lexer.GetTokenKind(); + if (nameTk != TK_rbrace) { + Error("expect } in SCOPE but get "); + return false; + } + lexer.NextToken(); + return true; +} + +bool MIRParser::ParseScope(StmtNodePtr &) { + // initial level 1 + MIRScope *scp = mod.GetMemPool()->New(&mod, 1); + bool status = ParseOneScope(*scp); + if (status) { + mod.CurFunction()->GetScope()->AddScope(scp); + } else { + delete scp; + } + return true; +} + +bool MIRParser::ParseOneAlias(GStrIdx &strIdx, MIRAliasVars &aliasVar) { + TokenKind nameTk = lexer.GetTokenKind(); + if (nameTk != TK_ALIAS) { + Error("expect ALIAS but get "); + return false; + } + nameTk = lexer.NextToken(); if (nameTk != TK_lname) { Error("expect local in ALIAS but get "); return false; } - GStrIdx srcStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(lexer.GetName()); + strIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(lexer.GetName()); nameTk = lexer.NextToken(); if (nameTk != TK_lname) { Error("expect local in ALIAS but get "); @@ -2376,11 +2519,19 @@ bool MIRParser::ParseAlias(StmtNodePtr&) { signStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(lexer.GetName()); lexer.NextToken(); } - MIRAliasVars aliasVar; aliasVar.memPoolStrIdx = mplStrIdx; aliasVar.tyIdx = tyIdx; aliasVar.sigStrIdx = signStrIdx; - mod.CurFunction()->SetAliasVarMap(srcStrIdx, aliasVar); + return true; +} + +bool MIRParser::ParseAlias(StmtNodePtr &) { + GStrIdx strIdx; + MIRAliasVars aliasVar; + + ParseOneAlias(strIdx, aliasVar); + + mod.CurFunction()->SetAliasVarMap(strIdx, aliasVar); return true; } -- Gitee