refactored

This commit is contained in:
ALEXks
2026-04-19 20:25:20 +03:00
parent 7d45f5babc
commit d77b6d5288
7 changed files with 170 additions and 137 deletions

View File

@@ -4,9 +4,7 @@
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <string.h>
@@ -21,12 +19,11 @@
#include "SgUtils.h"
#include "expr_transform.h"
#include "FunctionPurifying/function_purifying.h"
#include "uniq_name_creator.h"
using std::map;
using std::pair;
using std::set;
using std::unordered_set;
using std::unordered_map;
using std::vector;
using std::stack;
using std::string;
@@ -34,9 +31,6 @@ using std::wstring;
using std::to_string;
using std::make_pair;
static const string COMMON_ARRAY_SUFFIX = "_c";
static const string COMMON_BLOCK_SUFFIX = "_r";
static inline int getRegionExplicitLine(SgStatement *startR)
{
checkNull(startR, convertFileName(__FILE__).c_str(), __LINE__);
@@ -78,7 +72,7 @@ static bool isSPF_reg(SgStatement *st)
return st->variant() == SPF_PARALLEL_REG_DIR || st->variant() == SPF_END_PARALLEL_REG_DIR;
}
static const vector<const Variable*> getArraySynonyms(DIST::Array *array)
const vector<const Variable*> getArraySynonyms(DIST::Array *array)
{
auto arrayBlock = allUsedCommonArrays.find(array);
if (arrayBlock == allUsedCommonArrays.end())
@@ -113,103 +107,6 @@ static const vector<const Variable*> getArraySynonyms(DIST::Array *array)
return arrayBlock->second->getVariables(pos);
}
class UniqueNameCreator
{
map<string, vector<FuncInfo*>> func_info;
unordered_set<string> all_declarations;
bool declarations_analyzed = false;
unordered_map<const DIST::Array*, pair<string, string>> generated_names;
static void GetSymbolsRec(SgExpression* exp, unordered_set<string>& add_to)
{
if (!exp)
return;
if (isArrayRef(exp) && exp->symbol() && exp->symbol()->identifier())
add_to.emplace(exp->symbol()->identifier());
GetSymbolsRec(exp->lhs(), add_to);
GetSymbolsRec(exp->rhs(), add_to);
}
void FillDeclarations()
{
all_declarations.clear();
auto* file_before = current_file;
for (const auto& by_file : func_info)
{
if (SgFile::switchToFile(by_file.first) != -1)
{
for (const auto* by_func : by_file.second)
{
SgStatement* st = by_func->funcPointer;
if (st)
st = st->lexNext();
while (st)
{
for (int i = 0; i < 3; i++)
GetSymbolsRec(st->expr(i), all_declarations);
st = st->lexNext();
}
}
}
}
SgFile::switchToFile(file_before->filename());
declarations_analyzed = true;
}
public:
UniqueNameCreator(const map<string, vector<FuncInfo*>> &allFuncInfo)
{
declarations_analyzed = false;
func_info = allFuncInfo;
}
void GetUniqueName(DIST::Array* array, string& array_name, string& common_block_name)
{
auto varsOnPos = getArraySynonyms(array);
if (!varsOnPos.size())
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
auto array_plain_name = varsOnPos[0]->getName();
auto it = generated_names.find(array);
if (it != generated_names.end())
{
array_name.assign(it->second.first);
common_block_name.assign(it->second.second);
return;
}
if (!declarations_analyzed)
FillDeclarations();
int v = 1;
auto created_array_name = array_plain_name + COMMON_ARRAY_SUFFIX;
auto created_common_name = array_plain_name + COMMON_BLOCK_SUFFIX;
while (all_declarations.find(created_array_name) != all_declarations.end() ||
all_declarations.find(created_common_name) != all_declarations.end())
{
created_array_name = array_plain_name + "_v" + std::to_string(v) + COMMON_ARRAY_SUFFIX;
created_common_name = array_plain_name + "_v" + std::to_string(v) + COMMON_BLOCK_SUFFIX;
v++;
}
all_declarations.insert(created_array_name);
all_declarations.insert(created_common_name);
generated_names.emplace(array, std::make_pair(created_array_name, created_common_name));
array_name.assign(created_array_name);
common_block_name.assign(created_common_name);
}
};
static string getStringDeclaration(SgSymbol *symb)
{
string decl;