added creation of module
This commit is contained in:
@@ -85,7 +85,7 @@ static FuncInfo* findFileInfoByName(SgStatement* func, const vector<FuncInfo*>&
|
||||
}
|
||||
|
||||
static void findLocalData(SgStatement* func, SgStatement* end, vector<SgExpression*>& local,
|
||||
map<string, SgStatement*>& localParams, set<string>& added, const vector<FuncInfo*>& allFuncInfo)
|
||||
map<string, SgStatement*>& localParams, set<string>& added, const vector<FuncInfo*>& allFuncInfo)
|
||||
{
|
||||
SgStatement* start = func->lexNext();
|
||||
|
||||
@@ -782,6 +782,206 @@ static void processModules(SgFile* file, const vector<FuncInfo*>& allFuncInfo)
|
||||
}
|
||||
}
|
||||
|
||||
static SgSymbol* renamedNewSymb(SgSymbol* oldSymb, const char* prefix) {
|
||||
int lenPrefix = strlen(prefix);
|
||||
int lenSymbol = strlen(oldSymb->identifier());
|
||||
char* result = new char[lenPrefix + lenSymbol + 2];
|
||||
|
||||
strcpy(result, prefix);
|
||||
strcat(result, "_");
|
||||
strcat(result, oldSymb->identifier());
|
||||
|
||||
SgSymbol* newSymb = new SgSymbol(oldSymb->variant(), result, oldSymb->type(), oldSymb->scope());
|
||||
|
||||
return newSymb;
|
||||
}
|
||||
|
||||
static void renameEx(SgExpression* sizeEx, const char* funcName) {
|
||||
if (sizeEx->variant() == CONST_REF) {
|
||||
SgSymbol* symbSize = renamedNewSymb(sizeEx->symbol(), funcName);
|
||||
|
||||
sizeEx->setSymbol(symbSize);
|
||||
}
|
||||
|
||||
if (sizeEx->lhs())
|
||||
renameEx(sizeEx->lhs(), funcName);
|
||||
|
||||
if (sizeEx->rhs())
|
||||
renameEx(sizeEx->rhs(), funcName);
|
||||
}
|
||||
|
||||
|
||||
static void findSizeEx(SgExpression* sizeEx, const map<string, SgStatement*>& moduleStmts, const map<string, SgStatement*>& moduleParamStmts, SgStatement* proc_moduleF, set<string>& addedModuleParams) {
|
||||
if (sizeEx->variant() == CONST_REF) {
|
||||
if (moduleParamStmts.count(sizeEx->symbol()->identifier()) == 1) {
|
||||
auto decl = (SgParameterStmt*)moduleParamStmts.at(sizeEx->symbol()->identifier());
|
||||
|
||||
SgStatement* copyParamStmt = moduleParamStmts.at(sizeEx->symbol()->identifier())->copyPtr();
|
||||
proc_moduleF->insertStmtAfter(*copyParamStmt, *proc_moduleF);
|
||||
|
||||
SgStatement* copyStmt = moduleStmts.at(sizeEx->symbol()->identifier())->copyPtr();
|
||||
proc_moduleF->insertStmtAfter(*copyStmt, *proc_moduleF);
|
||||
|
||||
addedModuleParams.insert(sizeEx->symbol()->identifier());
|
||||
|
||||
findSizeEx(decl->value(0), moduleStmts, moduleParamStmts, proc_moduleF, addedModuleParams);
|
||||
}
|
||||
}
|
||||
|
||||
if (sizeEx->lhs())
|
||||
findSizeEx(sizeEx->lhs(), moduleStmts, moduleParamStmts, proc_moduleF, addedModuleParams);
|
||||
|
||||
if (sizeEx->rhs())
|
||||
findSizeEx(sizeEx->rhs(), moduleStmts, moduleParamStmts, proc_moduleF, addedModuleParams);
|
||||
}
|
||||
|
||||
static void processCommonStmt(SgStatement* st, set<string>& commonVariables)
|
||||
{
|
||||
if (st->expr(0))
|
||||
{
|
||||
SgExpression* ex = st->expr(0)->lhs();
|
||||
|
||||
SgExpression* lhs;
|
||||
while (ex && ex->lhs())
|
||||
{
|
||||
lhs = ex->lhs();
|
||||
commonVariables.insert(lhs->sunparse());
|
||||
|
||||
ex = ex->rhs();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void processVarStmt(SgStatement* st, map<string, SgStatement*>& moduleStmts, const char* funcName)
|
||||
{
|
||||
int lenFuncName = strlen(funcName);
|
||||
|
||||
SgExpression* ex = st->expr(0);
|
||||
SgExpression* lhs;
|
||||
|
||||
SgStatement* baseStmt = st->copyPtr();
|
||||
baseStmt->expr(0)->setLhs(NULL);
|
||||
baseStmt->expr(0)->setRhs(NULL);
|
||||
|
||||
while (ex && ex->lhs())
|
||||
{
|
||||
lhs = ex->lhs();
|
||||
|
||||
int lenSymbol = strlen(lhs->symbol()->identifier());
|
||||
char* result = new char[lenFuncName + lenSymbol + 2];
|
||||
|
||||
strcpy(result, funcName);
|
||||
strcat(result, "_");
|
||||
strcat(result, lhs->symbol()->identifier());
|
||||
|
||||
SgSymbol* symb = new SgSymbol(lhs->symbol()->variant(), result, lhs->symbol()->type(), lhs->symbol()->scope());
|
||||
SgExpression* newExpr = new SgExpression(lhs->variant());
|
||||
newExpr->setSymbol(symb);
|
||||
|
||||
if (lhs->variant() == ARRAY_REF)
|
||||
{
|
||||
SgExpression* newArraySizeExpr = lhs->lhs()->copyPtr();
|
||||
|
||||
SgExpression* sizeEx = newArraySizeExpr;
|
||||
SgExpression* sizeLhs;
|
||||
|
||||
while (sizeEx && sizeEx->lhs())
|
||||
{
|
||||
sizeLhs = sizeEx->lhs();
|
||||
|
||||
renameEx(sizeLhs, funcName);
|
||||
|
||||
sizeEx = sizeEx->rhs();
|
||||
}
|
||||
|
||||
|
||||
newExpr->setLhs(newArraySizeExpr);
|
||||
}
|
||||
|
||||
SgStatement* moduleStmt = baseStmt->copyPtr();
|
||||
moduleStmt->setExpression(0, newExpr);
|
||||
|
||||
SgExpression* typeExpr = st->expr(1)->copyPtr();
|
||||
moduleStmt->setExpression(1, typeExpr);
|
||||
|
||||
moduleStmts[newExpr->symbol()->identifier()] = moduleStmt;
|
||||
|
||||
delete[] result;
|
||||
|
||||
ex = ex->rhs();
|
||||
}
|
||||
}
|
||||
|
||||
static void processParamStmt(SgStatement* st, map<string, SgStatement*>& moduleParamStmts, const char* funcName)
|
||||
{
|
||||
auto decl = (SgParameterStmt*)st;
|
||||
int n = decl->numberOfConstants();
|
||||
for (int i = 0; i < n; ++i) {
|
||||
SgSymbol* constSymb = renamedNewSymb(decl->constant(i), funcName);
|
||||
SgExpression* constExpr = new SgExpression(CONST_REF, NULL, NULL, constSymb);
|
||||
|
||||
SgExpression* valueExpr = decl->value(i)->copyPtr();
|
||||
renameEx(valueExpr, funcName);
|
||||
|
||||
SgConstantSymb* paramSymb = new SgConstantSymb(constSymb->identifier(), *decl->controlParent(), *valueExpr);
|
||||
|
||||
SgParameterStmt* paramSt = new SgParameterStmt();
|
||||
paramSt->addConstant(paramSymb);
|
||||
|
||||
moduleParamStmts[constSymb->identifier()] = paramSt;
|
||||
}
|
||||
}
|
||||
|
||||
static void processVarBlock(SgStatement* func, SgStatement* firstExec, map<string, SgStatement*>& moduleStmts, map<string, SgStatement*>& moduleParamStmts, set<string>& commonVariables)
|
||||
{
|
||||
const char* funcName = func->symbol()->identifier();
|
||||
|
||||
for (SgStatement* st = func->lexNext(); st != firstExec; st = st->lexNext())
|
||||
{
|
||||
if (st->variant() == COMM_STAT)
|
||||
processCommonStmt(st, commonVariables);
|
||||
else if (st->variant() == VAR_DECL || st->variant() == VAR_DECL_90)
|
||||
processVarStmt(st, moduleStmts, funcName);
|
||||
else if (st->variant() == PARAM_DECL)
|
||||
processParamStmt(st, moduleParamStmts, funcName);
|
||||
}
|
||||
}
|
||||
|
||||
static void insertStmtToModule(map<string, SgStatement*>& moduleStmts, map<string, SgStatement*>& moduleParamStmts, set<string>& addedModuleParams, set<string>& commonVariables, SgStatement* proc_moduleF)
|
||||
{
|
||||
SgStatement* endProcModuleF = proc_moduleF->lastNodeOfStmt();
|
||||
|
||||
endProcModuleF->unparsestdout();
|
||||
|
||||
for (const auto& [varName, varStmt] : moduleStmts) {
|
||||
string varNameNoPref = varName;
|
||||
|
||||
|
||||
string::size_type pos{};
|
||||
pos = varName.find_first_of("_", pos);
|
||||
varNameNoPref.erase(0, pos + 1);
|
||||
|
||||
if ((commonVariables.count(varNameNoPref) == 0) && (moduleParamStmts.count(varName) == 0)) {
|
||||
|
||||
endProcModuleF->insertStmtBefore(*varStmt, *proc_moduleF);
|
||||
if (varStmt->expr(0)->variant() == ARRAY_REF) {
|
||||
SgExpression* arraySizeExpr = varStmt->expr(0)->lhs();
|
||||
|
||||
SgExpression* sizeEx = arraySizeExpr;
|
||||
SgExpression* sizeLhs;
|
||||
|
||||
while (sizeEx && sizeEx->lhs()) {
|
||||
sizeLhs = sizeEx->lhs();
|
||||
|
||||
findSizeEx(sizeLhs, moduleStmts, moduleParamStmts, proc_moduleF, addedModuleParams);
|
||||
|
||||
sizeEx = sizeEx->rhs();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void createCheckpoints(SgFile* file, const map<string, CommonBlock*>& commonBlocks, const map<int, UserFiles>& filesInfo,
|
||||
const vector<FuncInfo*>& allFuncInfo)
|
||||
{
|
||||
@@ -825,7 +1025,7 @@ void createCheckpoints(SgFile* file, const map<string, CommonBlock*>& commonBloc
|
||||
{
|
||||
func = file->functions(z);
|
||||
point = func->lexNext();
|
||||
while (point && point->lineNumber() < cpLine && point != func->lastNodeOfStmt() && point->variant() != CONTAINS_STMT)
|
||||
while (point && ((point->fileName() != file->filename()) || (point->lineNumber() < cpLine)) && point != func->lastNodeOfStmt() && point->variant() != CONTAINS_STMT)
|
||||
point = point->lexNext();
|
||||
|
||||
//cp place was found
|
||||
@@ -851,6 +1051,30 @@ void createCheckpoints(SgFile* file, const map<string, CommonBlock*>& commonBloc
|
||||
map<string, SgStatement*> localParams;
|
||||
set<string> addedToList;
|
||||
findLocalData(func, firstExec, local, localParams, addedToList, allFuncInfo);
|
||||
|
||||
// external perems?
|
||||
|
||||
const char* funcName = func->symbol()->identifier();
|
||||
int lenFuncName = strlen(funcName);
|
||||
|
||||
SgStatement* proc_moduleF = NULL;
|
||||
const string proc_cpModule = "spf_module_" + string(funcName);
|
||||
SgSymbol* proc_mainModuleCpS = new SgSymbol(MODULE_NAME, proc_cpModule.c_str());
|
||||
proc_moduleF = new SgStatement(MODULE_STMT, NULL, proc_mainModuleCpS);
|
||||
moduleF->insertStmtAfter(*proc_moduleF, *global);
|
||||
|
||||
SgStatement* endProcModuleF = new SgStatement(CONTROL_END);
|
||||
proc_moduleF->insertStmtAfter(*endProcModuleF, *proc_moduleF);
|
||||
|
||||
map<string, SgStatement*> moduleStmts;
|
||||
map<string, SgStatement*> moduleParamStmts;
|
||||
set<string> commonVariables;
|
||||
|
||||
processVarBlock(func, firstExec, moduleStmts, moduleParamStmts, commonVariables);
|
||||
|
||||
set<string> addedModuleParams;
|
||||
insertStmtToModule(moduleStmts, moduleParamStmts, addedModuleParams, commonVariables, proc_moduleF);
|
||||
|
||||
const vector<SgStatement*> useOfMods = findUseOfModules(func->lexNext(), firstExec);
|
||||
|
||||
SgStatement* loadBlock = new SgStatement(IF_NODE);
|
||||
@@ -982,17 +1206,17 @@ void createCheckpoints(SgFile* file, const map<string, CommonBlock*>& commonBloc
|
||||
insertToifLoadOk.push_back(ifLoadOk1);
|
||||
ifLoadOk1->insertStmtAfter(*new SgIfStmt(*fileIdx == *new SgValueExp(numOfFiles + 1), *new SgAssignStmt(*fileIdx, *new SgValueExp(1))), *ifLoadOk1);
|
||||
ifLoadOk1->insertStmtAfter(*new SgAssignStmt(*fileIdx, *fileIdx + *new SgValueExp(1)), *ifLoadOk1);
|
||||
ifLoadOk1->addComment("! LOAD DATA FROM CHECKPOINT\n");
|
||||
ifLoadOk1->addComment("! LOAD DATA FROM CHECKPOINT\n"); // program sleep (frozed) here
|
||||
|
||||
vector<SgExpression*> commentArgs;
|
||||
commentArgs.push_back(new SgValueExp(" SECONDS"));
|
||||
commentArgs.push_back(&(*new SgVarRefExp(profS[1]) - *new SgVarRefExp(profS[0])));
|
||||
commentArgs.push_back(new SgArrayRefExp(*files, *fileIdx));
|
||||
commentArgs.push_back(new SgValueExp("SPF CHECKPOINT LOADED FROM "));
|
||||
|
||||
|
||||
ifLoadOk1->insertStmtAfter(*new SgInputOutputStmt(WRITE_STAT, *makeExprList({ &frmtProf, &unitNull }, false), *makeExprList(commentArgs, false)), *ifLoadOk1);
|
||||
ifLoadOk1->insertStmtAfter(profCallE->copy(), *ifLoadOk1);
|
||||
|
||||
|
||||
//open all files
|
||||
if (createdModuleForIO)
|
||||
{
|
||||
@@ -1128,6 +1352,8 @@ void createCheckpoints(SgFile* file, const map<string, CommonBlock*>& commonBloc
|
||||
|
||||
func->insertStmtAfter(*new SgStatement(USE_STMT, NULL, mainModuleCpS), *func);
|
||||
|
||||
func->insertStmtAfter(*new SgStatement(USE_STMT, NULL, proc_mainModuleCpS), *func);
|
||||
|
||||
//check use
|
||||
map<string, bool> modulesDone;
|
||||
for (auto& elem : moduleNames)
|
||||
|
||||
Reference in New Issue
Block a user