From e9774ebc467ed5fd7014c81cd64acd5803413997 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Wed, 22 May 2024 21:02:04 +0300 Subject: [PATCH] improved checkpoints --- .../_src/Transformations/checkpoints.cpp | 77 ++++++++++++++++++- .../Transformations/function_purifying.cpp | 15 +++- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 3 files changed, 89 insertions(+), 5 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp index 27b92fb..7fa8f05 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp @@ -16,6 +16,75 @@ enum class typeEvery { TIME, ITER }; static const vector iosNames = { "spf_close_all", "spf_open_all", "spf_inc_num_part" }; +static SgStatement* replaceForWithWhile(SgStatement* forSt) +{ + checkNull(forSt, convertFileName(__FILE__).c_str(), __LINE__); + + auto forStat = isSgForStmt(forSt); + checkNull(forStat, convertFileName(__FILE__).c_str(), __LINE__); + + auto start = forStat->start(); + auto end = forStat->end(); + auto step = forStat->step(); + if (step == NULL) + step = new SgValueExp(1); + + auto stepCalc = CalculateInteger(step->copyPtr()); + + auto doName = forStat->doName(); + + checkNull(start, convertFileName(__FILE__).c_str(), __LINE__); + checkNull(end, convertFileName(__FILE__).c_str(), __LINE__); + + SgStatement* insert = forSt->lexPrev(); + SgStatement* doWhile = NULL; + + if (stepCalc->isInteger()) + { + const int stepValue = stepCalc->valueInteger(); + auto cond = (stepValue > 0) ? (*new SgVarRefExp(doName) < end->copy()) : (*new SgVarRefExp(doName) > end->copy()); + auto cond2 = (stepValue > 0) ? (*new SgVarRefExp(doName) > end->copy()) : (*new SgVarRefExp(doName) < end->copy()); + SgLogIfStmt* logIf = new SgLogIfStmt(cond2, *new SgStatement(EXIT_STMT)); + + doWhile = new SgWhileStmt(&cond, NULL); + insert->insertStmtAfter(*doWhile, *insert->controlParent()); + auto insertTo = doWhile->lastNodeOfStmt(); + + SgStatement* last = forSt->lastNodeOfStmt(); + while (forSt->lexNext() != last) + { + auto body = forSt->lexNext(); + insertTo->insertStmtBefore(*body->extractStmt(), *doWhile); + } + + doWhile->insertStmtBefore(*new SgAssignStmt(*new SgVarRefExp(doName), start->copy() - step->copy()), *insert->controlParent()); + + doWhile->insertStmtAfter(*logIf, *doWhile); + doWhile->insertStmtAfter(*new SgAssignStmt(*new SgVarRefExp(doName), (*new SgVarRefExp(doName) + *step)), *doWhile); + + doWhile->addComment(forSt->comments()); + forSt->extractStmt(); + } + else + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + return doWhile; +} + +static void checkForToReplace(SgStatement* gotoBlock, SgStatement* goPoint) +{ + //return; + auto cp = gotoBlock->controlParent(); + while (goPoint->controlParent() != cp) + { + auto top = goPoint->controlParent(); + if (top->variant() == FOR_NODE) + goPoint = replaceForWithWhile(top); + else + goPoint = top; + } +} + static void createModule(SgStatement*& module, const string& name, bool withFile = true) { if (module == NULL) @@ -950,7 +1019,7 @@ static SgStatement* createSaveBlock(const vector& loadS, FuncInfo*& f const vector& moduleNames, const int unitNum, const vector>& chainLocalVarNoParams, const vector& chainLabel) { - SgStatement* storeBlock = new SgIfStmt(IF_NODE); + SgStatement* storeBlock = new SgIfStmt((SgExpression*)NULL); vector listSpec; listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("form"), *new SgValueExp("unformatted"))); @@ -1116,6 +1185,8 @@ static void processAllCalls(SgStatement* firstExec, const string funcToName, con gotoBlock->insertStmtBefore(*gotoNextPU, *gotoBlock); saveVarToModule(localVarNoParams, execStmt, funcFromName.c_str(), procLabelSymb, labNum); + + checkForToReplace(gotoBlock, execStmt); } execStmt = execStmt->lexNext(); @@ -1225,7 +1296,7 @@ static void processFunctionCallChain(SgStatement* func, const vector& SgStatement* gotoBlock = new SgStatement(IF_NODE); - gotoBlock->addComment("! goto next program unit\n"); + gotoBlock->addComment("! GOTO NEXT PROGRAM UNIT\n"); gotoBlock->setExpression(0, *new SgVarRefExp(loadS[0]) == *new SgValueExp(1)); loadBlock->insertStmtAfter(*gotoBlock, *loadBlock->controlParent()); @@ -1565,6 +1636,8 @@ void createCheckpoints(SgFile* file, const map& commonBloc SgAssignStmt* init = new SgAssignStmt(*new SgVarRefExp(loadS[0]), *new SgValueExp(0)); gotoBlock->insertStmtAfter(*init, *gotoBlock); loadBlock->insertStmtAfter(*gotoBlock, *loadBlock->controlParent()); + + checkForToReplace(gotoBlock, point); } processModules(file, allFuncInfo); diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/function_purifying.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/function_purifying.cpp index f78d726..0e01cfa 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/function_purifying.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/function_purifying.cpp @@ -188,6 +188,17 @@ static void removeExternalStat(SgStatement* func, const set& addedInterf rem->deleteStmt(); } +template +static vector sortByName(const T &funcs) +{ + vector funcList; + for (auto& elem : funcs) + funcList.push_back(elem); + + std::sort(funcList.begin(), funcList.end(), [](FuncInfo* a, FuncInfo* b) { return a->funcName > b->funcName; }); + return funcList; +} + void createInterfacesForAssumedSize(const map>& allFuncInfo) { set hasAssumedSizeArrays; @@ -251,14 +262,14 @@ void createInterfacesForAssumedSize(const map>& allFun if (SgFile::switchToFile(funcByFile.first) == -1) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - for (auto& func : funcByFile.second) + for (auto& func : sortByName(funcByFile.second)) { SgProgHedrStmt* prog = isSgProgHedrStmt(func->funcPointer->GetOriginal()); if (prog == NULL) continue; set addedInterfaceFor; - for (auto& elem : func->callsFromV) + for (auto& elem : sortByName(func->callsFromV)) { auto it = hasAssumedSizeArrays.find(elem); if (it != hasAssumedSizeArrays.end()) diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 4a763c9..431ac92 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 "2341" +#define VERSION_SPF "2343"