From a04ee160237115110dce6faebc9c3feee628b14e Mon Sep 17 00:00:00 2001 From: ALEXks Date: Thu, 19 Feb 2026 08:48:44 +0300 Subject: [PATCH] improved fix_common_blocks --- .../GlobalVariables/fix_common_blocks.cpp | 171 +++++++++--------- .../GlobalVariables/fix_common_blocks.h | 8 +- src/Utils/version.h | 2 +- 3 files changed, 89 insertions(+), 92 deletions(-) diff --git a/src/Transformations/GlobalVariables/fix_common_blocks.cpp b/src/Transformations/GlobalVariables/fix_common_blocks.cpp index 80ce1ec..103ab17 100644 --- a/src/Transformations/GlobalVariables/fix_common_blocks.cpp +++ b/src/Transformations/GlobalVariables/fix_common_blocks.cpp @@ -18,69 +18,66 @@ using std::tuple; /// main function: // renew unions for all common blocks in the file -static void BuildNewCommDecls(SgFile* file, const map allCommonBlocks, - map>& newCommonDecls, map>>& commDecls, - set& badCommon, map>& notUsedVars, vector& programUnits); +//void BuildNewCommDecls // get names of variables and array elements, which were referenced in programm unit -static set getUses(SgStatement* firstSt, const set& commonVarNames); -static void getUsesFromExpr(SgExpression* expr, const set& commonVarNames, set& used); +//set getUses +//void getUsesFromExpr // splits arrays into elements and replaces not used vars with empty constraints -static bool splitType(deque& d, bool check_use, const set& namesOfUsedVars); +//bool splitType + // create constraits set -static deque makeConstraints(deque& constraints, const set& namesOfUsedVars, set& notUsedVars); +//deque makeConstraints + // build union -static bool buildConstraintsUnion(deque& U, deque B, const set& namesOfUsedVars, pair& problemConstraints); -static bool docheckUnequalConstraints(deque& U, deque& B, deque& newU, pair& problemConstraints); -static bool check(deque& A, deque& B, pair& problemConstraints); +//bool buildConstraintsUnion +//bool docheckUnequalConstraints +//bool check /// small help functions: -static string getParentName(const string& name); -static bool equalDims(const CommConstraint& a, const CommConstraint& b); -static bool equalConstraints(const CommConstraint& a, const CommConstraint& b); -static void addElem(deque& comm, const CommConstraint& elem); -////// +//string getParentName +//bool equalDims +//bool equalConstraints +//void addElem // change names of variables in 'constraints' -static void fixNames(deque& constraints, const string& commName); +//void fixNames ////// step2: transformation - /// main function // peform transformation on every program unit in the file -static void fixFunctions(SgFile* file, vector programUnits, map>& newCommonDecls, - map>>& commDecls, const set& badCommon, map>& notUsedVars); +//void fixFunctions // get pairs of names (namesOldToNew) for renaming -static bool getNamesOldToNew(deque newDecl, deque oldDecl, map& namesOldToNew); +//bool getNamesOldToNew // create new symbols for new variables in new common declaration (constraints) -static void makeCommVarSymbs(const deque& constraints, SgFile* file, SgStatement* func, string commName, - map& symbs, vector& needNewDecl); +//void makeCommVarSymbs + // delete from program unit all references to names in commVarNames -static void deleteOldVars(SgStatement* firstSt, const set& commVarNames); +//void deleteOldVars // calls fixExpression for each statement, replaces names in data statement -static void renameVariables(SgStatement* firstSt, const map& newVarSymbs, const map& namesOldToNew); +//void renameVariables + // replacing variables or array elements in expression expr if their names are in namesOldToNew -static SgExpression* fixExpression(SgExpression* expr, const map& newSymbs, const map& namesOldToNew); +//SgExpression* fixExpression // make new exprList exprssion for new declaration decl with symbols from newSymbs -static SgExpression* makeExprListForCommon(const deque& decl, const map& newSymbs, - SgFile* file, SgStatement* firstSt); +//SgExpression* makeExprListForCommon + // replace old common declarations with new ones -static void rewriteCommon(SgStatement* firstSt, map& commListExprs); +//void rewriteCommon /// help functions: -static SgExpression* makeIdxFromStr(const string& str); +//SgExpression* makeIdxFromStr(const string& str); + // make new expression of array element -static SgExpression* newArrElemExpr(const string& newName, const map& newSymbs); -static bool variablePositionComp(const Variable* lhs, const Variable* rhs); +//SgExpression* newArrElemExpr +//bool variablePositionComp ////// - - CommConstraint::CommConstraint(const Variable* var, bool u, const string& funcName, const string& fileName) { used = u; @@ -125,7 +122,6 @@ CommConstraint::CommConstraint(const Variable* var, bool u, const string& funcNa } } - CommConstraint::CommConstraint(const string& name, SgType* t, bool u) : used(u), type(t), identifier(name) { typeVariant = type->variant(); @@ -139,8 +135,7 @@ CommConstraint::CommConstraint(const string& name, SgType* t, bool u, vector& commonVarNames, set& used) +static void getUsesFromExpr(SgExpression* expr, const set& commonVarNames, set& used) { if (expr == NULL) return; @@ -194,8 +188,7 @@ void getUsesFromExpr(SgExpression* expr, const set& commonVarNames, set< getUsesFromExpr(expr->rhs(), commonVarNames, used); } - -set getUses(SgStatement* firstSt, const set& commonVarNames) +static set getUses(SgStatement* firstSt, const set& commonVarNames) { set used; SgStatement* lastSt = firstSt->lastNodeOfStmt(); @@ -212,8 +205,7 @@ set getUses(SgStatement* firstSt, const set& commonVarNames) return used; } - -bool equalDims(const CommConstraint& a, const CommConstraint& b) +static bool equalDims(const CommConstraint& a, const CommConstraint& b) { const vector>& adim = a.arrayInfo->GetSizes(); const vector>& bdim = b.arrayInfo->GetSizes(); @@ -228,9 +220,8 @@ bool equalDims(const CommConstraint& a, const CommConstraint& b) return true; } - // TODO: add attributes to CommConstraints, check if a and b have equal attributes -bool equalConstraints(const CommConstraint& a, const CommConstraint& b) +static bool equalConstraints(const CommConstraint& a, const CommConstraint& b) { if ((a.arrayInfo != NULL && b.arrayInfo == NULL) || ((a.arrayInfo == NULL && b.arrayInfo != NULL))) return false; @@ -241,8 +232,7 @@ bool equalConstraints(const CommConstraint& a, const CommConstraint& b) return true; } - -void addElem(deque& comm, const CommConstraint& elem) +static void addElem(deque& comm, const CommConstraint& elem) { if (elem.typeVariant == 0 && !comm.empty() && comm.back().typeVariant == 0) comm.back().size += elem.size; @@ -250,9 +240,8 @@ void addElem(deque& comm, const CommConstraint& elem) comm.push_back(elem); } - // TODO: check attributes: do not split arrays with pointer or target attributes if check_use == true -bool splitType(deque& d, bool check_use, const set& namesOfUsedVars = {}) +static bool splitType(deque& d, bool check_use, const set& namesOfUsedVars = {}) { CommConstraint var = d.front(); string name = var.identifier; @@ -300,8 +289,7 @@ bool splitType(deque& d, bool check_use, const set& name } } - -deque makeConstraints(deque& constraints, const set& namesOfUsedVars, set& notUsedVars) +static deque makeConstraints(deque& constraints, const set& namesOfUsedVars, set& notUsedVars) { deque res; while (!constraints.empty()) @@ -318,8 +306,7 @@ deque makeConstraints(deque& constraints, const return res; } - -bool check(deque& A, deque& B, pair& problemConstraints) +static bool check(deque& A, deque& B, pair& problemConstraints) { while (!A.empty() && !B.empty()) { @@ -358,8 +345,8 @@ bool check(deque& A, deque& B, pair& U, deque& B, deque& newU, pair& problemConstraints) +static bool docheckUnequalConstraints(deque& U, deque& B, deque& newU, + pair& problemConstraints) { if (U.front().typeVariant == 0) { @@ -382,8 +369,8 @@ bool docheckUnequalConstraints(deque& U, deque& return true; } - -bool buildConstraintsUnion(deque& U, deque B, const set& namesOfUsedVars, pair& problemConstraints) +static bool buildConstraintsUnion(deque& U, deque B, + const set& namesOfUsedVars, pair& problemConstraints) { deque newU; while (!U.empty() && !B.empty()) @@ -438,8 +425,8 @@ bool buildConstraintsUnion(deque& U, deque B, co return true; } - -bool getNamesOldToNew(deque newDecl, deque oldDecl, map& namesOldToNew) +//TODO: check this: newDecl and oldDecl => do these variables need references? +static bool getNamesOldToNew(deque newDecl, deque oldDecl, map& namesOldToNew) { bool needChange = false; map rename; @@ -504,16 +491,18 @@ bool getNamesOldToNew(deque newDecl, deque oldDe newDecl.pop_front(); } } + if (!oldDecl.empty() || !newDecl.empty()) needChange = true; + if (needChange) namesOldToNew.insert(rename.begin(), rename.end()); + return needChange; } - -void makeCommVarSymbs(const deque& constraints, SgFile* file, SgStatement* func, string commName, - map& symbs, vector& needNewDecl) +static void makeCommVarSymbs(const deque& constraints, SgFile* file, SgStatement* func, const string& commName, + map& symbs, vector& needNewDecl) { for (const CommConstraint& var : constraints) { @@ -526,8 +515,7 @@ void makeCommVarSymbs(const deque& constraints, SgFile* file, Sg } } - -void deleteOldVars(SgStatement* firstSt, const set& commVarNames) +static void deleteOldVars(SgStatement* firstSt, const set& commVarNames) { SgStatement* lastSt = firstSt->lastNodeOfStmt(); vector stmtsToDelete; @@ -573,12 +561,12 @@ void deleteOldVars(SgStatement* firstSt, const set& commVarNames) } // TODO: delete common variables form attributes statements (like DIM_STAT) } + for (SgStatement* st : stmtsToDelete) st->deleteStmt(); } - -SgExpression* makeIdxFromStr(const string& str) +static SgExpression* makeIdxFromStr(const string& str) { vector items; int num = 0; @@ -593,13 +581,13 @@ SgExpression* makeIdxFromStr(const string& str) num = 0; } } + reverse(items.begin(), items.end()); SgExpression* exprList = makeExprList(items, false); return exprList; } - -SgExpression* newArrElemExpr(const string& newName, const map& newSymbs) +static SgExpression* newArrElemExpr(const string& newName, const map& newSymbs) { size_t pos = newName.find('('); SgExpression* newExpr = new SgArrayRefExp(*newSymbs.at(newName.substr(0, pos))); @@ -607,11 +595,11 @@ SgExpression* newArrElemExpr(const string& newName, const map return newExpr; } - -SgExpression* fixExpression(SgExpression* expr, const map& newSymbs, const map& namesOldToNew) +static SgExpression* fixExpression(SgExpression* expr, const map& newSymbs, const map& namesOldToNew) { if (expr == NULL) return NULL; + if (expr->variant() == VAR_REF || expr->variant() == ARRAY_REF) { string name = expr->symbol()->identifier(); @@ -647,17 +635,18 @@ SgExpression* fixExpression(SgExpression* expr, const map& ne } } } + SgExpression* lhs = fixExpression(expr->lhs(), newSymbs, namesOldToNew); if (lhs != NULL) expr->setLhs(lhs); + SgExpression* rhs = fixExpression(expr->rhs(), newSymbs, namesOldToNew); if (rhs != NULL) expr->setRhs(rhs); return NULL; } - -void renameVariables(SgStatement* firstSt, const map& newVarSymbs, const map& namesOldToNew) +static void renameVariables(SgStatement* firstSt, const map& newVarSymbs, const map& namesOldToNew) { SgStatement* lastSt = firstSt->lastNodeOfStmt(); for (SgStatement* curSt = firstSt; curSt != NULL && curSt != lastSt; curSt = curSt->lexNext()) @@ -705,8 +694,7 @@ void renameVariables(SgStatement* firstSt, const map& newVarS } } - -SgExpression* makeExprListForCommon(const deque& decl, const map& newSymbs, +static SgExpression* makeExprListForCommon(const deque& decl, const map& newSymbs, SgFile* file, SgStatement* firstSt) { vector items; @@ -740,8 +728,7 @@ SgExpression* makeExprListForCommon(const deque& decl, const map return exprList; } - -void rewriteCommon(SgStatement* firstSt, map& commListExprs) +static void rewriteCommon(SgStatement* firstSt, map& commListExprs) { vector commonStmtsToDelete; for (SgStatement* st = firstSt; st != firstSt->lastDeclaration()->lexNext(); st = st->lexNext()) @@ -792,12 +779,12 @@ void rewriteCommon(SgStatement* firstSt, map& commListExp commonStmtsToDelete.push_back(st); } } + for (SgStatement* st : commonStmtsToDelete) st->deleteStmt(); } - -void fixNames(deque& constraints, const string& commName) +static void fixNames(deque& constraints, const string& commName) { for (auto& var : constraints) { @@ -812,26 +799,26 @@ void fixNames(deque& constraints, const string& commName) } } - -bool variablePositionComp(const Variable* lhs, const Variable* rhs) +static bool variablePositionComp(const Variable* lhs, const Variable* rhs) { return lhs->getPosition() < rhs->getPosition(); } - -void fixFunctions(SgFile* file, vector programUnits, map>& newCommonDecls, - map>>& commDecls, const set& badCommon, map>& notUsedVars) +static void fixFunctions(SgFile* file, const vector& programUnits, map>& newCommonDecls, + map>>& commDecls, const set& badCommon, map>& notUsedVars) { for (SgStatement* unitSt : programUnits) { string funcName = unitSt->symbol()->identifier(); if (commDecls.find(funcName) == commDecls.end()) continue; + SgStatement* firstSt = unitSt; map commListExprs; map newVarSymbs; // new symbols for new variables map namesOldToNew; // for ranaming: old name -> new name vector needNewDecl; + for (auto& common : commDecls[funcName]) { string commName = common.first; @@ -842,9 +829,11 @@ void fixFunctions(SgFile* file, vector programUnits, map programUnits, map allCommonBlocks, - map>& newCommonDecls, map>>& commDecls, - set& badCommon, map>& notUsedVars, vector& programUnits) +static void buildNewCommDecls(SgFile* file, const map& allCommonBlocks, + map>& newCommonDecls, map>>& commDecls, + set& badCommon, map>& notUsedVars, vector& programUnits) { string fileName = file->filename(); SgStatement* curSt = file->firstStatement(); @@ -900,7 +888,8 @@ void BuildNewCommDecls(SgFile* file, const map allCommonBl constraints.push_back(newConstr); } if (hasChar && hasNotChar) // TDOO: make proper warning message or separate such common blocks - __spf_print(1, "common block '%s' ('%s':%d) contains variables of symbolic and numeric types. It is required to divide\n", commName.c_str(), fileName.c_str(), constraints.back().uses.back().lineNum); + __spf_print(1, "common block '%s' ('%s':%d) contains variables of symbolic and numeric types. It is required to divide\n", + commName.c_str(), fileName.c_str(), constraints.back().uses.back().getLineNum()); if (hasChar) { badCommon.insert(commName); @@ -917,7 +906,7 @@ void BuildNewCommDecls(SgFile* file, const map allCommonBl for (auto x : problemConstraints.first.uses) // TODO: make proper warning message for (auto y : problemConstraints.second.uses) __spf_print(1, "variables '%s' and '%s' in one storage association (common block '%s') have different types (files - %s:%d and %s:%d)\n", - x.varName.c_str(), y.varName.c_str(), commName.c_str(), x.fileName.c_str(), x.lineNum, y.fileName.c_str(), y.lineNum); + x.getVarName(), y.getVarName(), commName.c_str(), x.getFileName(), x.getLineNum(), y.getFileName(), y.getLineNum()); } } curSt = curSt->lastNodeOfStmt(); @@ -929,7 +918,7 @@ void BuildNewCommDecls(SgFile* file, const map allCommonBl // main function -void fixCommonBlocks(const map> allFuncInfo, const map allCommonBlocks, SgProject* project) // TODO: separate into 2 steps? +void fixCommonBlocks(const map>& allFuncInfo, const map& allCommonBlocks, SgProject* project) // TODO: separate into 2 steps? { int filesNum = project->numberOfFiles(); map>>> commDecls; // file_name -> function_name -> common block name -> old declaration of common block @@ -943,10 +932,12 @@ void fixCommonBlocks(const map> allFuncInfo, const map SgFile* file = &project->file(i); string fileName = file->filename(); file->switchToFile(fileName); - BuildNewCommDecls(file, allCommonBlocks, newCommonDecls, commDecls[fileName], badCommon, notUsedVars, programUnitsInFile[fileName]); + buildNewCommDecls(file, allCommonBlocks, newCommonDecls, commDecls[fileName], badCommon, notUsedVars, programUnitsInFile[fileName]); } + for (auto& elem : newCommonDecls) fixNames(elem.second, elem.first); + for (int i = 0; i < filesNum; i++) // second step { SgFile* file = &project->file(i); diff --git a/src/Transformations/GlobalVariables/fix_common_blocks.h b/src/Transformations/GlobalVariables/fix_common_blocks.h index 13bbd3e..2fe0f67 100644 --- a/src/Transformations/GlobalVariables/fix_common_blocks.h +++ b/src/Transformations/GlobalVariables/fix_common_blocks.h @@ -16,12 +16,18 @@ struct DeclInfo // for error messages { +private: std::string varName; std::string fileName; int lineNum; +public: DeclInfo() : varName(""), fileName(""), lineNum(0) {}; DeclInfo(const std::string& vn, const std::string& fn, int ln) : varName(vn), fileName(fn), lineNum(ln) {}; + + const char* getVarName() const { return varName.c_str(); } + const char* getFileName() const { return fileName.c_str(); } + int getLineNum() const { return lineNum; } }; struct CommConstraint // TODO: add variable attributes @@ -42,4 +48,4 @@ struct CommConstraint // TODO: add variable attributes }; -void fixCommonBlocks(const std::map> allFuncInfo, const std::map allCommonBlocks, SgProject* project); \ No newline at end of file +void fixCommonBlocks(const std::map>& allFuncInfo, const std::map& allCommonBlocks, SgProject* project); \ No newline at end of file diff --git a/src/Utils/version.h b/src/Utils/version.h index 0892062..92f77c0 100644 --- a/src/Utils/version.h +++ b/src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2464" +#define VERSION_SPF "2465"