improved parser, fixed convertation

This commit is contained in:
ALEXks
2024-02-25 11:16:56 +03:00
parent deaeb4a670
commit 4386eb7041
13 changed files with 259 additions and 152 deletions

View File

@@ -80,6 +80,7 @@ extern void BufferAllocate();
int out_free_form;
int out_upper_case;
int out_line_unlimit;
int out_line_length; // out_line_length = 132 for -ffo mode; out_line_length = 72 for -uniForm mode
PTR_SYMB last_file_symbol;
static int CountNullBifNext = 0; /* for internal debugging */
@@ -1312,7 +1313,7 @@ char* filter(char *s)
#ifdef __SPF
addToCollection(__LINE__, __FILE__,temp, 0);
#endif
// out_line_length = 132 if -ffo option is used or out_line_length = 72 if -uniForm option is used
int temp_i = 0;
int buf_i = 0;
int commentline = 0;
@@ -1442,7 +1443,7 @@ char* filter(char *s)
}
else
{
if (((!out_free_form && temp_i == 71) || (out_free_form && !out_line_unlimit && temp_i == 131)) && !commentline && (s[i + 1] != '\n'))
if (((!out_free_form && temp_i == 71) || (out_free_form && !out_line_unlimit && temp_i == out_line_length - 1)) && !commentline && (s[i + 1] != '\n'))
{
if (buf_i + 1 > temp_size)
{
@@ -1479,7 +1480,7 @@ char* filter(char *s)
buf_i = -1;
}
if (((!out_free_form && temp_i == 71) || (out_free_form && !out_line_unlimit && temp_i == 131)) && commentline && (s[i + 1] != '\n') && ((OMP == 1) || (OMP == 2) || (DVM == 1) || (SPF == 1))) /*07.08.17*/
if (((!out_free_form && temp_i == 71) || (out_free_form && !out_line_unlimit && temp_i == out_line_length - 1)) && commentline && (s[i + 1] != '\n') && ((OMP == 1) || (OMP == 2) || (DVM == 1) || (SPF == 1))) /*07.08.17*/
{
if (buf_i + 1 > temp_size)
{

View File

@@ -80,6 +80,7 @@ extern int iacross;
extern "C" int out_free_form;
extern "C" int out_upper_case;
extern "C" int out_line_unlimit;
extern "C" int out_line_length;
extern "C" PTR_SYMB last_file_symbol;
Options options;
@@ -280,6 +281,11 @@ int main(int argc, char *argv[])
out_upper_case = 1;
else if (!strcmp(argv[0], "-noLimitLine"))
out_line_unlimit = 1;
else if (!strcmp(argv[0], "-uniForm"))
{
out_free_form = 1;
out_line_length = 72;
}
else if (!strcmp(argv[0], "-noRemote"))
options.setOn(NO_REMOTE);
else if (!strcmp(argv[0], "-lgstd"))
@@ -365,6 +371,11 @@ int main(int argc, char *argv[])
(void)fprintf(stderr, "Warning: -Ohost option is set to -Opl2 mode\n");
options.setOn(O_HOST);
}
if(out_free_form == 1 && out_line_length == 72 && out_line_unlimit == 1)
{
(void)fprintf(stderr, "Warning: -noLimitLine and -uniForm options are incompatible; -noLimitLine option is ignored\n");
out_line_unlimit = 0;
}
if (v_print)
(void)fprintf(stderr, "<<<<< Translating >>>>>\n");
@@ -392,7 +403,7 @@ int main(int argc, char *argv[])
// for call of function 'tpoint'
//added one symbol to input-file name
//printf("%s",fin_name); //!!! debug
if(a_mode && project.numberOfFiles()>1)
if(a_mode || project.numberOfFiles()>1)
fout_name = doOutFileName(file->filename()); //project.fileName(id);
else if (fout_name && source_name && !strcmp(source_name, fout_name))
{
@@ -582,6 +593,7 @@ void initialize()
out_free_form = 0;
out_upper_case = 0;
out_line_unlimit = 0;
out_line_length = 132;
default_integer_size = 4;
default_real_size = 4;
unparse_functions = 0; //set to 1 by option -byFunUnparse

View File

@@ -507,6 +507,8 @@ int main(int argc, char *argv[])
;
else if (!strcmp(argv[0], "-noLimitLine"))
;
else if (!strcmp(argv[0], "-uniForm"))
;
else if (!strcmp(argv[0], "-noRemote"))
;
else if (!strcmp(argv[0], "-lgstd"))

View File

@@ -1945,6 +1945,8 @@ copy_module_scope(sym_mod,list)
for(source=sym_mod->entry.Template.next; source; source=source->entry.Template.next) {
if((source->attr & PRIVATE_BIT) && (!(source->attr & PUBLIC_BIT)) )
continue;
if(source->variant == FUNCTION_NAME && source->decl != YES) /* intrinsic function called from specification expression */ /* podd 24.02.24 */
continue;
if(list && in_rename_list(source,list))
continue;
if((copy=just_look_up_sym_in_scope(cur_scope(),source->ident)) && copy->id_attr && copy->id_attr->entry.Template.tag==sym_mod->entry.Template.func_hedr->id)

View File

@@ -222,6 +222,9 @@ struct FuncInfo
std::string getCallName(const std::pair<void*, int>& call_info, const std::string& name, int line)
{
if (line <= 0)
return name;
std::set<std::string> names;
for (auto& call : callsFromDetailed)

View File

@@ -269,7 +269,6 @@ static vector<SgExpression*> getLowBounds(SgSymbol* arrayS)
SgExpression* list = NULL;
if (isAllocated(arrayS))
{
SgSymbol* copyFrom = NULL;
@@ -284,7 +283,27 @@ static vector<SgExpression*> getLowBounds(SgSymbol* arrayS)
int consistInAllocates = 0;
const string origName = OriginalSymbol(copyFrom)->identifier();
for (auto data : getAttributes<SgStatement*, SgStatement*>(decl, set<int>{ ALLOCATE_STMT }))
auto allocData = getAttributes<SgStatement*, SgStatement*>(decl, set<int>{ ALLOCATE_STMT });
if (allocData.size() == 0) // try to find statements in original file
{
string declFile(decl->fileName());
int line = decl->lineNumber();
string file_name = current_file->filename();
if (current_file->filename() != declFile)
{
auto trueDecl = SgStatement::getStatementByFileAndLine(declFile, line);
if (trueDecl)
{
auto trueData = getAttributes<SgStatement*, SgStatement*>(trueDecl, set<int>{ ALLOCATE_STMT });
if (trueData.size())
allocData = trueData;
}
}
SgFile::switchToFile(file_name);
}
for (auto data : allocData)
{
if (data->variant() != ALLOCATE_STMT)
continue;
@@ -314,7 +333,7 @@ static vector<SgExpression*> getLowBounds(SgSymbol* arrayS)
list = NULL;
if (list == NULL)
__spf_print(1, "find for %s, consistInAllocates = %d", arrayS->identifier(), consistInAllocates);
__spf_print(1, "find for %s, consistInAllocates = %d\n", arrayS->identifier(), consistInAllocates);
checkNull(list, convertFileName(__FILE__).c_str(), __LINE__);
}
else
@@ -961,10 +980,14 @@ static void insert(SgStatement* callSt, SgStatement* tempHedr, SgStatement* begi
auto next = prev->lexNext();
if (callSt->comments())
{
if (next->comments())
next->setComments((string(callSt->comments()) + next->comments()).c_str());
string callCom(callSt->comments());
if (next->comments()) {
string newCom = callCom + next->comments();
next->setComments(newCom.c_str());
}
else
next->addComment(callSt->comments());
next->addComment(callCom.c_str());
callSt->delComments();
}
auto ident = string(tempHedr->symbol()->identifier());
@@ -1737,7 +1760,7 @@ static bool inliner(const string& fileName_in, const string& funcName, const int
point.currLvl = 0;
point.currCall = func->funcName;
__spf_print(1, " INLINE %s\n", func->funcName.c_str());
__spf_print(1, " INLINE %s - ", func->funcName.c_str());
#ifdef _WIN32
sendMessage_2lvl(wstring(L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> '") + wstring(func->funcName.begin(), func->funcName.end()) + L"'");
#else
@@ -1745,6 +1768,7 @@ static bool inliner(const string& fileName_in, const string& funcName, const int
#endif
//1 level
bool isInlined = run_inliner(funcMap, toInsert, SPF_messages, fileName, func, newSymbsToDeclare, point, commonBlocks);
__spf_print(1, "%s\n", isInlined ? "done" : "fault");
if (isInlined == false)
{
@@ -1776,8 +1800,9 @@ static bool inliner(const string& fileName_in, const string& funcName, const int
point.currLvl = currDeep;
point.currCall = next.first->funcName;
__spf_print(1, " INLINE %s\n", next.first->funcName.c_str());
__spf_print(1, " INLINE %s - ", next.first->funcName.c_str());
bool isInlined = run_inliner(funcMap, next.second, SPF_messages, fileName, next.first, newSymbsToDeclare, point, commonBlocks);
__spf_print(1, "%s\n", isInlined ? "done" : "fault");
changed |= isInlined;
}
}

View File

@@ -17,5 +17,15 @@ extern int convert_file(int argc, char* argv[], const char* proj_name);
void convertFiles(int argc, char* argv[], const char* proj_name)
{
convert_file(argc, argv, proj_name);
vector<char*> args_v;
for (int z = 0; z < argc; ++z)
args_v.push_back(argv[z]);
args_v.push_back("-a");
args_v.push_back((char*)proj_name);
args_v.push_back(NULL);
int code = convert_file(args_v.size() - 1, args_v.data(), proj_name);
exit(code);
}

View File

@@ -8,9 +8,52 @@
#include "FileInfo.h"
#include "../Utils/utils.h"
#include "../Utils/errors.h"
using namespace std;
static int tmp_id = 0;
static string tmp_name = "tmp_conv_";
extern "C" int out_free_form;
extern "C" int out_line_length;
//convert through unparce
void FileInfo::convertToUniform()
{
int old_free = out_free_form;
int old_line = out_line_length;
out_free_form = 1;
out_line_length = 72;
__spf_print(1, "covnert to uniform %s file\n", fileName.c_str());
if (error != 0)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
if (outDepPath == "")
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
vector<char*> filesList;
filesList.push_back((char*)outDepPath.c_str());
const string name = tmp_name + to_string(tmp_id++);
SgProject* tmpProj = new SgProject(name.c_str(), filesList.data(), 1);
if (tmpProj == NULL)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
SgFile* currF = &tmpProj->file(0);
string text = string(currF->firstStatement()->unparse());
writeFileFromStr(fileName, text);
out_free_form = old_free;
out_line_length = old_line;
InitializeTable();
}
string FileInfo::convertStyle(bool needRewrite)
{
string tmp_text = text;

View File

@@ -41,10 +41,11 @@ struct FileInfo
std::string outPath;
std::string outDepPath;
std::string text;
int style; // -1 unk, 0 fixed, 1 fixed ext, 2 free
int style; // -1 unk, 0 fixed, 1 fixed ext, 2 free, 3 uniform
int includesAdded;
std::set<std::string> includes;
int lvl;
std::string convertStyle(bool needRewrite = true);
void convertToUniform();
};

View File

@@ -37,14 +37,13 @@ extern "C" int parse_file(int argc, char* argv[], char* proj_name);
static void findModuleDeclInProject(const string& name, const vector<string>& files, map<string, string>& modDecls)
{
char** filesList = new char* [files.size()];
vector<char*> filesList;
for (int z = 0; z < files.size(); ++z)
filesList[z] = (char*)files[z].c_str();
filesList.push_back((char*)files[z].c_str());
SgProject* tmpProj = new SgProject(name.c_str(), filesList, files.size());
SgProject* tmpProj = new SgProject(name.c_str(), filesList.data(), files.size());
int numF = tmpProj->numberOfFiles();
set<SgFile*> filesSg;
for (int z = 0; z < numF; ++z)
{
vector<SgStatement*> modules;
@@ -52,8 +51,6 @@ static void findModuleDeclInProject(const string& name, const vector<string>& fi
string fileName = currF->filename();
convertToLower(fileName);
filesSg.insert(currF);
findModulesInFile(currF, modules);
for (auto& elem : modules)
{
@@ -72,7 +69,6 @@ static void findModuleDeclInProject(const string& name, const vector<string>& fi
}
}
delete []filesList;
InitializeTable();
}
@@ -119,7 +115,6 @@ static set<FileInfo*> applyModuleDeclsForFile(FileInfo *forFile, const map<strin
//rewrite files to the next iter of parse
set<FileInfo*> allFiles;
bool needToConvertStyle = false;
for (auto& incl : toIncl)
{
if (mapFiles.find(incl) == mapFiles.end())
@@ -128,40 +123,15 @@ static set<FileInfo*> applyModuleDeclsForFile(FileInfo *forFile, const map<strin
}
allFiles.insert(forFile);
int style = forFile->style;
for (auto& elem : allFiles)
{
if (style != elem->style)
{
needToConvertStyle = true;
break;
}
}
set<FileInfo*> toConvert;
int mainStyle = forFile->style;
for (auto& file : allFiles)
if (mainStyle != file->style && file->style != 3)
toConvert.insert(file);
string mainText = forFile->text;
if (needToConvertStyle)
{
for (auto& elem : allFiles)
{
if (elem->style != 2)
{
retFilesMod.insert(elem);
if (elem != forFile)
elem->convertStyle();
else
mainText = elem->convertStyle(false);
}
}
if (forFile->style != 2)
{
for (auto& opt : optSplited)
{
if (opt == "-FI" || opt == "-extend_source")
opt = "-FR";
}
}
}
const string mainText = forFile->text;
for (auto& file : toConvert)
file->convertToUniform();
string include = "";
int includeCount = 0;
@@ -277,10 +247,21 @@ static vector<string> parseList(vector<FileInfo>& listOfProject,
if (options.find("-FI") != string::npos)
elem.style = 0;
else if (options.find("-FR") != string::npos || options.find("-f90") != string::npos)
elem.style = 2;
else if (options.find("-extend_source") != string::npos)
elem.style = 1;
else if (options.find("-FR") != string::npos || options.find("-f90") != string::npos)
elem.style = 2;
else
{ //fdv|f|ftn|for|f90|f95|f03
static set<string> fixed_exts = { "for", "f", "ftn" };
static set<string> free_exts = { "f90", "f95", "f03" };
string ext = OnlyExt(file.c_str());
if (fixed_exts.find(ext) != fixed_exts.end())
elem.style = 0;
else if (free_exts.find(ext) != free_exts.end())
elem.style = 2;
}
for (int z = 0; z < optSplited.size(); ++z)
{
@@ -534,14 +515,115 @@ static void printDebug(const map<string, set<string>>& mapModuleDeps, const map<
__spf_print(1, "%s\n", toPrint.c_str());
}
static void parseFiles(int& iters, vector<string>& errors, vector<FileInfo>& listOfProject,
map<string, set<string>>& mapModuleDeps, map<string, string>& moduleDelc, map<string, set<string>>& modDirectOrder,
int parseForInlining, bool isFromConsole)
{
int changed = 0;
int lastChanged = 0;
const string projName = "tmp";
do
{
#ifdef _WIN32
sendMessage_1lvl(L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> " + std::to_wstring((iters + 1)) + L" <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
#else
sendMessage_1lvl(L"running " + std::to_wstring((iters + 1)) + L" iteration of syntax analisys");
#endif
errors = parseList(listOfProject, iters != 0, parseForInlining, mapModuleDeps, moduleDelc, modDirectOrder, isFromConsole);
changed = createMapOfUse(errors, listOfProject, mapModuleDeps);
if (iters != 0)
if (lastChanged <= changed)
break;
createNeededException();
if (changed)
{
vector<string> files;
for (auto& elem : listOfProject)
if (elem.error == 0)
files.push_back(elem.outDepPath);
if (files.size() == 0)
break;
findModuleDeclInProject(projName + std::to_string(iters++), files, moduleDelc);
modDirectOrder = createModuleOrder(moduleDelc, mapModuleDeps);
}
lastChanged = changed;
//printDebug(mapModuleDeps, modDirectOrder, listOfProject);
} while (changed);
}
static vector<string> finalyzeParsing(const vector<FileInfo>& listOfProject,
const map<string, set<string>> mapModuleDeps,
const map<string, string> moduleDelc)
{
vector<string> filesCompilationOrder;
int added = 0;
int iter = 0;
vector<string> files;
while (added != listOfProject.size())
{
for (auto& elem : listOfProject)
{
if (elem.lvl == iter)
{
files.push_back(elem.fileName);
added++;
}
}
++iter;
}
map<string, set<string>> fileDeps;
for (auto& file : files)
{
fileDeps[file] = set<string>();
if (mapModuleDeps.find(file) == mapModuleDeps.end())
continue;
for (auto& dep : mapModuleDeps.at(file))
{
if (moduleDelc.find(dep) == moduleDelc.end())
continue;
fileDeps[file].insert(moduleDelc.at(dep));
}
}
set<string> addedFiles;
added = 0;
while (added != fileDeps.size())
{
for (auto& file : fileDeps)
{
bool depsAdded = true;
for (auto& dep : file.second)
if (addedFiles.find(dep) == addedFiles.end())
depsAdded = false;
if (depsAdded && addedFiles.find(file.first) == addedFiles.end())
{
filesCompilationOrder.push_back(file.first);
addedFiles.insert(file.first);
added++;
}
}
}
__spf_print(1, "files compilation order:\n");
for (auto& file : filesCompilationOrder)
__spf_print(1, " %s\n", file.c_str());
return filesCompilationOrder;
}
static int parseFiles(vector<string>& errors, vector<FileInfo>& listOfProject, vector<string>& filesCompilationOrder,
int parseForInlining, bool isFromConsole = false)
{
int rethrow = 0;
int iters = 0;
int changed = 0;
int lastChanged = 0;
const string projName = "tmp";
map<string, set<string>> mapModuleDeps;
map<string, string> moduleDelc;
@@ -549,95 +631,15 @@ static int parseFiles(vector<string>& errors, vector<FileInfo>& listOfProject, v
try
{
do
parseFiles(iters, errors, listOfProject, mapModuleDeps, moduleDelc, modDirectOrder, false, isFromConsole);
filesCompilationOrder = finalyzeParsing(listOfProject, mapModuleDeps, moduleDelc);
if (parseForInlining)
{
#ifdef _WIN32
sendMessage_1lvl(L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> " + std::to_wstring((iters + 1)) + L" <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
#else
sendMessage_1lvl(L"running " + std::to_wstring((iters + 1)) + L" iteration of syntax analisys");
#endif
errors = parseList(listOfProject, iters != 0, (parseForInlining != 0), mapModuleDeps, moduleDelc, modDirectOrder, isFromConsole);
changed = createMapOfUse(errors, listOfProject, mapModuleDeps);
if (iters != 0)
if (lastChanged <= changed)
break;
createNeededException();
if (changed)
{
vector<string> files;
for (auto& elem : listOfProject)
if (elem.error == 0)
files.push_back(elem.outDepPath);
if (files.size() == 0)
break;
findModuleDeclInProject(projName + std::to_string(iters++), files, moduleDelc);
modDirectOrder = createModuleOrder(moduleDelc, mapModuleDeps);
}
lastChanged = changed;
//printDebug(mapModuleDeps, modDirectOrder, listOfProject);
} while (changed);
//printDebug(mapModuleDeps, modDirectOrder, listOfProject);
int added = 0;
int iter = 0;
vector<string> files;
while (added != listOfProject.size())
{
for (auto& elem : listOfProject)
{
if (elem.lvl == iter)
{
files.push_back(elem.fileName);
added++;
}
}
++iter;
for (auto& file : listOfProject)
file.outDepPath = "";
parseFiles(iters, errors, listOfProject, mapModuleDeps, moduleDelc, modDirectOrder, true, isFromConsole);
}
map<string, set<string>> fileDeps;
for (auto& file : files)
{
fileDeps[file] = set<string>();
if (mapModuleDeps.find(file) == mapModuleDeps.end())
continue;
for (auto& dep : mapModuleDeps[file])
{
if (moduleDelc.find(dep) == moduleDelc.end())
continue;
fileDeps[file].insert(moduleDelc[dep]);
}
}
set<string> addedFiles;
added = 0;
while (added != fileDeps.size())
{
for (auto& file : fileDeps)
{
bool depsAdded = true;
for (auto& dep : file.second)
if (addedFiles.find(dep) == addedFiles.end())
depsAdded = false;
if (depsAdded && addedFiles.find(file.first) == addedFiles.end())
{
filesCompilationOrder.push_back(file.first);
addedFiles.insert(file.first);
added++;
}
}
}
__spf_print(1, "files compilation order:\n");
for (auto& file : filesCompilationOrder)
__spf_print(1, " %s\n", file.c_str());
}
catch (int err)
{

View File

@@ -2731,9 +2731,9 @@ int main(int argc, char **argv)
else if (string(curr_arg) == "-parse")
parseFiles(argc - (i + 1), argv + (i + 1));
else if (string(curr_arg) == "-pppa")
pppaAnalyzer(argc - (i + 1), argv + (i + 1));
pppaAnalyzer(argc - i, argv + i);
else if (string(curr_arg) == "-fdvm")
convertFiles(argc - (i + 1), argv + (i + 1));
convertFiles(argc - i, argv + i);
else if (string(curr_arg) == "-mpi")
mpiProgram = 1;
else if (string(curr_arg) == "-client")

View File

@@ -1,3 +1,3 @@
#pragma once
#define VERSION_SPF "2277"
#define VERSION_SPF "2282"

View File

@@ -153,6 +153,9 @@ static void setOptions(const short* options, bool isBuildParallel = false, const
staticShadowAnalysis = intOptions[STATIC_SHADOW_ANALYSIS];
staticPrivateAnalysis = intOptions[STATIC_PRIVATE_ANALYSIS];
out_free_form = intOptions[FREE_FORM];
if (out_free_form == 1)
out_line_unlimit = 1;
if (isBuildParallel)
keepSpfDirs = intOptions[KEEP_SPF_DIRECTIVES];
else
@@ -182,7 +185,10 @@ static void setOptions(const short* options, bool isBuildParallel = false, const
else if (elem == STATIC_PRIVATE_ANALYSIS)
staticPrivateAnalysis = 0;
else if (elem == FREE_FORM)
{
out_free_form = 0;
out_line_unlimit = 0;
}
else if (elem == KEEP_DVM_DIRECTIVES)
keepDvmDirectives = 0;
else if (elem == KEEP_SPF_DIRECTIVES)