From 4a13250d1c31db2dc819f6d9f2353be43d2e3a80 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Sun, 12 May 2024 13:37:42 +0300 Subject: [PATCH] improved intent insertion --- .../_src/GraphCall/graph_calls.cpp | 38 ++++-- .../Sapfor_2017/_src/GraphCall/graph_calls.h | 2 + sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 18 --- .../Transformations/function_purifying.cpp | 125 +++++++++++------- .../Transformations/set_implicit_none.cpp | 21 ++- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 6 files changed, 122 insertions(+), 84 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.cpp b/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.cpp index c318bf0..c117c5c 100644 --- a/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.cpp +++ b/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.cpp @@ -736,13 +736,13 @@ static void fillIn(FuncInfo *currF, SgExpression *ex, const map &pa } } -static void fillOut(FuncInfo* currF, const string& symb, const map& parNames) +static void fillType(FuncInfo* currF, const string& symb, const map& parNames, const int type) { if (symb != "") { auto it = parNames.find(symb); if (it != parNames.end()) - currF->funcParams.inout_types[it->second] |= OUT_BIT; + currF->funcParams.inout_types[it->second] |= type; } } @@ -751,7 +751,7 @@ static void checkSpecList(SgExpression* pair, FuncInfo* currF, const maplhs()); if (valExp->value() == string("unit")) if (pair->rhs() && pair->rhs()->symbol()) - fillOut(currF, pair->rhs()->symbol()->identifier(), parNames); + fillType(currF, pair->rhs()->symbol()->identifier(), parNames, INOUT_BIT); } static void checkSpecList(SgExpression *spec, FuncInfo* currF, const map& parNames) @@ -814,7 +814,7 @@ static void fillInOut(FuncInfo *currF, SgStatement *start, SgStatement *last, co if (left->lhs()->symbol()) symb = left->lhs()->symbol()->identifier(); } - fillOut(currF, symb, parNames); + fillType(currF, symb, parNames, OUT_BIT); } // TODO: need to extend else if (st->variant() == READ_STAT) { @@ -830,7 +830,7 @@ static void fillInOut(FuncInfo *currF, SgStatement *start, SgStatement *last, co string symb = ""; if (item->symbol()) symb = item->symbol()->identifier(); - fillOut(currF, symb, parNames); + fillType(currF, symb, parNames, OUT_BIT); } else if (item->variant() == IOACCESS) { @@ -851,7 +851,7 @@ static void fillInOut(FuncInfo *currF, SgStatement *start, SgStatement *last, co string symb = ""; if (item->symbol()) symb = item->symbol()->identifier(); - fillOut(currF, symb, parNames); + fillType(currF, symb, parNames, OUT_BIT); } } } @@ -887,7 +887,7 @@ static void fillInOut(FuncInfo *currF, SgStatement *start, SgStatement *last, co printInternalError(convertFileName(__FILE__).c_str(), __LINE__); if (types[z] == OUT_BIT || types[z] == INOUT_BIT) - fillOut(currF, arg->symbol()->identifier(), parNames); + fillType(currF, arg->symbol()->identifier(), parNames, OUT_BIT); if (types[z] == IN_BIT || types[z] == INOUT_BIT) fillIn(currF, arg, parNames); } @@ -997,11 +997,11 @@ static FuncInfo* createNewFuction(const string& funcName, SgStatement *st, SgSta return currInfo; } -static void analyzeFunction(const string& funcName, const string& containsPrefix, - SgStatement *function, SgStatement* entry, map>& allFuncInfo, - const map& mapLoopGraph, vector& messagesForFile, - vector& containsFunctions, - const set& activeOps) +static FuncInfo* analyzeFunction(const string& funcName, const string& containsPrefix, + SgStatement *function, SgStatement* entry, map>& allFuncInfo, + const map& mapLoopGraph, vector& messagesForFile, + vector& containsFunctions, + const set& activeOps) { SgStatement* st = function; SgStatement* lastNode = st->lastNodeOfStmt(); @@ -1193,6 +1193,8 @@ static void analyzeFunction(const string& funcName, const string& containsPrefix st = st->lexNext(); } + + return procInfo; } static set fillActiveOperators(const vector& blocks) @@ -1269,6 +1271,7 @@ void functionAnalyzer(SgFile *file, map> &allFuncInfo, } } + FuncInfo* lastNonEntry = NULL; for (auto& function : functions) { bool isEntry = (function->variant() == ENTRY_STAT); @@ -1322,7 +1325,16 @@ void functionAnalyzer(SgFile *file, map> &allFuncInfo, activeOps.insert(function->controlParent()); } - analyzeFunction(funcName, containsPrefix, isEntry ? function->controlParent() : function, function, allFuncInfo, mapLoopGraph, messagesForFile, containsFunctions, activeOps); + auto procInfo = analyzeFunction(funcName, containsPrefix, isEntry ? function->controlParent() : function, function, allFuncInfo, mapLoopGraph, messagesForFile, containsFunctions, activeOps); + + if (isEntry) + { + if (!lastNonEntry) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + lastNonEntry->entry.push_back(procInfo); + } + else + lastNonEntry = procInfo; } //fill INTERFACE block from modules diff --git a/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.h b/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.h index 8428117..c4597f0 100644 --- a/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.h +++ b/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.h @@ -149,6 +149,8 @@ struct FuncInfo std::map interfaceBlocks; std::map interfaceSynonims; + std::vector entry; // all entry points + std::set externalCalls; bool isPure; // does this func or funcs called from this have common block[s] and have no side effects diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index ada72a4..eab676b 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -1038,24 +1038,6 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne } else if (curr_regime == TEST_PASS) { - /*int funcNum = file->numberOfFunctions(); - for (int z = 0; z < funcNum; ++z) - { - SgStatement* f = file->functions(z); - for (auto st = f->lexNext(); st != f->lastNodeOfStmt(); st = st->lexNext()) - { - if (st->variant() == CONTAINS_STMT) - break; - if (isSgExecutableStatement(st)) - break; - - string key = st->unparse(); - if (same_decls.find(key) == same_decls.end()) - same_decls[key] = 1; - else - same_decls[key]++; - } - }*/ //test pass } diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/function_purifying.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/function_purifying.cpp index 2f1a5b5..03316f2 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/function_purifying.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/function_purifying.cpp @@ -369,8 +369,11 @@ map> fillFromIntent(SgStatement* header) return intentS; } -static void insertIntents(vector identificators, SgStatement* header, map parSym, int intentVariant, int intentBit) +static void insertIntents(set& identificators, SgStatement* header, const map& parSym, int intentVariant, int intentBit) { + if (identificators.size() == 0) + return; + if (header->variant() == ENTRY_STAT) while (isSgProgHedrStmt(header) == NULL) header = header->controlParent(); @@ -388,67 +391,58 @@ static void insertIntents(vector identificators, SgStatement* header, ma if (stmt->variant() != ENTRY_STAT) lastDecl = stmt; - if (stmt->variant() == VAR_DECL_90) + if (stmt->variant() == VAR_DECL_90) { SgVarDeclStmt* s = (SgVarDeclStmt*)stmt; - for (int i = 0; i < s->numberOfAttributes(); i++) + for (int i = 0; i < s->numberOfAttributes(); i++) { if (s->attribute(i)->variant() == intentVariant) { - for (int i = 0; i < s->numberOfVars(); i++) + for (int i = 0; i < s->numberOfVars(); i++) { - for (auto it = identificators.begin(); it != identificators.end(); it++) - { - if (*it == s->var(i)->symbol()->identifier()) - { - identificators.erase(it); - break; - } - } + auto sname = s->var(i)->symbol()->identifier(); + if (identificators.count(sname)) + identificators.erase(sname); } } } } - else if (stmt->variant() == INTENT_STMT) + else if (stmt->variant() == INTENT_STMT) { SgIntentStmt* s = (SgIntentStmt*)stmt; - if (s->attribute()->variant() == intentVariant) + if (s->attribute()->variant() == intentVariant) { - for (int i = 0; i < s->numberOfVars(); i++) + for (int i = 0; i < s->numberOfVars(); i++) { - for (auto it = identificators.begin(); it != identificators.end(); it++) - { - if (*it == s->var(i)->symbol()->identifier()) - { - identificators.erase(it); - break; - } - } + auto sname = s->var(i)->symbol()->identifier(); + if (identificators.count(sname)) + identificators.erase(sname); } } } } - if (identificators.size() > 0) + SgExpression* attr = new SgExpression(intentVariant); + SgExpression* args = NULL; + for (auto& par : identificators) { - SgExpression* attr = new SgExpression(intentVariant); - SgExpression* args = NULL; - for (auto& par : identificators) - { - SgExprListExp* tempArgs = new SgExprListExp(); - SgVarRefExp* tempPar = new SgVarRefExp(parSym[par]); - tempArgs->setLhs(tempPar); - if (args) - tempArgs->setRhs(args); - args = tempArgs; - parSym[par]->setAttribute(parSym[par]->attributes() | intentBit); - } + if (parSym.count(par) == 0) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + auto s = parSym.at(par); + SgExprListExp* tempArgs = new SgExprListExp(); + SgVarRefExp* tempPar = new SgVarRefExp(s); + tempArgs->setLhs(tempPar); if (args) - { - SgIntentStmt* intent = new SgIntentStmt(*args, *attr); - lastDecl->insertStmtAfter(*intent, (header == lastDecl) ? *header : *lastDecl->controlParent()); - } + tempArgs->setRhs(args); + args = tempArgs; + s->setAttribute(s->attributes() | intentBit); + } + + if (args) + { + SgIntentStmt* intent = new SgIntentStmt(*args, *attr); + lastDecl->insertStmtAfter(*intent, (header == lastDecl) ? *header : *lastDecl->controlParent()); } } @@ -476,12 +470,14 @@ static SgSymbol* getParameter(SgStatement* stat, int n) static void intentInsert(const FuncInfo* func, SgStatement* headerSt) { - vector InIdentificators; - vector OutIdentificators; - vector InOutIdentificators; - + if (func->funcPointer->variant() == ENTRY_STAT) + return; - map parSym; + set InIdentificators; + set OutIdentificators; + set InOutIdentificators; + + map parSym; set intentS; auto intentsByStat = fillFromIntent(headerSt); for (auto& elem : intentsByStat) @@ -494,8 +490,7 @@ static void intentInsert(const FuncInfo* func, SgStatement* headerSt) SgSymbol* parS = getParameter(headerSt, i); const string ident = parS->identifier(); - if (ident == "*" || - parS->attributes() & EXTERNAL_BIT) + if (ident == "*" || parS->attributes() & EXTERNAL_BIT) continue; parSym[ident] = parS; @@ -503,11 +498,41 @@ static void intentInsert(const FuncInfo* func, SgStatement* headerSt) continue; if (func->funcParams.isArgInOut(i)) - InOutIdentificators.push_back(ident); + InOutIdentificators.insert(ident); else if (func->funcParams.isArgIn(i)) - InIdentificators.push_back(ident); + InIdentificators.insert(ident); else if (func->funcParams.isArgOut(i)) - OutIdentificators.push_back(ident); + OutIdentificators.insert(ident); + } + + //remove conflicted intents + for (auto& entry : func->entry) + { + for (int i = 0; i < entry->funcParams.countOfPars; i++) + { + const auto& ident = entry->funcParams.identificators[i]; + if (entry->funcParams.isArgInOut(i)) + { + if (InIdentificators.count(ident)) + InIdentificators.erase(ident); + if (OutIdentificators.count(ident)) + OutIdentificators.erase(ident); + } + else if (entry->funcParams.isArgIn(i)) + { + if (InOutIdentificators.count(ident)) + InOutIdentificators.erase(ident); + if (OutIdentificators.count(ident)) + OutIdentificators.erase(ident); + } + else if (entry->funcParams.isArgOut(i)) + { + if (InIdentificators.count(ident)) + InIdentificators.erase(ident); + if (InOutIdentificators.count(ident)) + InOutIdentificators.erase(ident); + } + } } insertIntents(InOutIdentificators, headerSt, parSym, INOUT_OP, INOUT_BIT); diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp index 730b36c..2be3c33 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp @@ -36,7 +36,7 @@ static void FindAllVars(SgExpression* expr, set& allVars, setvariant(); - if (var == VAR_REF || var == ARRAY_REF) + if (var == VAR_REF || var == ARRAY_REF || var == FUNC_CALL) allVars.insert(expr->symbol()); if (var == CONST_REF) allVarsConst.insert(expr->symbol()); @@ -180,11 +180,21 @@ static map FunctionImplicitCheck(SgStatement* function, const map else if (st->variant() == CONTAINS_STMT || isSgExecutableStatement(st) != NULL) break; } + + set skip = { EXTERN_STAT }; for (auto st = function; st != endOfFunc && st->variant() != CONTAINS_STMT; st = st->lexNext()) + { + if (skip.count(st->variant())) + continue; + for (int i = 0; i < 3; ++i) FindAllVars(st->expr(i), allVars, allVarsConst); + if (st->variant() == FOR_NODE) + allVars.insert(isSgForStmt(st)->doName()); + } + varsWithoutDecl = getVars(function->symbol()->identifier(), toRename, allVars, types); varsWithoutDeclConst = getVars(function->symbol()->identifier(), toRename, allVarsConst, types); @@ -215,6 +225,13 @@ static map FunctionImplicitCheck(SgStatement* function, const map insertPlace->insertStmtAfter(*implNone, *function); insertPlace = insertPlace->lexNext(); + if (function->variant() == FUNC_HEDR) + { + auto type_op = function->expr(1); + if (type_op == NULL) + varsWithoutDecl.push_back(function->symbol()); + } + makeDeclaration(varsWithoutDecl, function); auto declList = makeDeclaration(varsWithoutDeclConst, NULL); @@ -224,7 +241,7 @@ static map FunctionImplicitCheck(SgStatement* function, const map for (auto& s : toRename) s->changeName(TestAndCorrectName((string(s->identifier()) + "_").c_str())); - + return types; } diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 9915e40..5ecc2e3 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/version.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2331" +#define VERSION_SPF "2333"