From 8a92f305f415174c492ac456bd5f00e33c814b14 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Wed, 28 Feb 2024 17:38:02 +0300 Subject: [PATCH] added convertation to sapfor --- dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp | 111 ++-- dvm/fdvm/trunk/Sage/lib/include/libSage++.h | 4 + .../_src/ProjectManipulation/ConvertFiles.cpp | 481 +++++++++++++++++- .../_src/ProjectManipulation/ParseFiles.cpp | 17 +- .../Sapfor_2017/_src/Utils/SgUtils.cpp | 165 +++--- .../experts/Sapfor_2017/_src/Utils/SgUtils.h | 3 + .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 7 files changed, 637 insertions(+), 146 deletions(-) diff --git a/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp b/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp index 0592ef1..4c39cd8 100644 --- a/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp +++ b/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp @@ -1793,6 +1793,7 @@ bool SgStatement::consistentCheckIsActivated = false; bool SgStatement::deprecatedCheck = false; std::string SgStatement::currProcessFile = ""; int SgStatement::currProcessLine = -1; +bool SgStatement::sapfor_regime = false; void SgStatement::checkConsistence() { @@ -4497,76 +4498,78 @@ SgExecutableStatement* isSgExecutableStatement(SgStatement *pt) return NULL; if (!isADeclBif(BIF_CODE(pt->thebif))) { -#if __SPF - const int var = pt->variant(); - if (var == CONTROL_END) + if (SgStatement::isSapforRegime()) { - SgStatement *cp = pt->controlParent(); - if (cp->variant() == PROG_HEDR || cp->variant() == PROC_HEDR || cp->variant() == FUNC_HEDR) + const int var = pt->variant(); + if (var == CONTROL_END) { - SgStatement* cpcp = cp->controlParent(); - if (cpcp && cpcp->variant() == INTERFACE_STMT) - return NULL; + SgStatement* cp = pt->controlParent(); + if (cp->variant() == PROG_HEDR || cp->variant() == PROC_HEDR || cp->variant() == FUNC_HEDR) + { + SgStatement* cpcp = cp->controlParent(); + if (cpcp && cpcp->variant() == INTERFACE_STMT) + return NULL; + else + return (SgExecutableStatement*)pt; + } else - return (SgExecutableStatement*)pt; + return isSgExecutableStatement(cp); } + else if (var == DVM_INHERIT_DIR || var == DVM_ALIGN_DIR || var == DVM_DYNAMIC_DIR || + var == DVM_DISTRIBUTE_DIR || var == DVM_VAR_DECL || var == DVM_SHADOW_DIR || + var == DVM_HEAP_DIR || var == DVM_CONSISTENT_DIR || var == DVM_POINTER_DIR || + var == HPF_TEMPLATE_STAT || var == HPF_PROCESSORS_STAT || var == DVM_TASK_DIR || + var == DVM_INDIRECT_GROUP_DIR || var == DVM_REMOTE_GROUP_DIR || var == DVM_REDUCTION_GROUP_DIR || + var == DVM_CONSISTENT_GROUP_DIR || var == DVM_ASYNCID_DIR || var == ACC_ROUTINE_DIR) + return NULL; + else if (var == SPF_ANALYSIS_DIR || var == FORMAT_STAT) + return isSgExecutableStatement(pt->lexNext()); else - return isSgExecutableStatement(cp); + return (SgExecutableStatement*)pt; } - else if (var == DVM_INHERIT_DIR || var == DVM_ALIGN_DIR || var == DVM_DYNAMIC_DIR || - var == DVM_DISTRIBUTE_DIR || var == DVM_VAR_DECL || var == DVM_SHADOW_DIR || - var == DVM_HEAP_DIR || var == DVM_CONSISTENT_DIR || var == DVM_POINTER_DIR || - var == HPF_TEMPLATE_STAT || var == HPF_PROCESSORS_STAT || var == DVM_TASK_DIR || - var == DVM_INDIRECT_GROUP_DIR || var == DVM_REMOTE_GROUP_DIR || var == DVM_REDUCTION_GROUP_DIR || - var == DVM_CONSISTENT_GROUP_DIR || var == DVM_ASYNCID_DIR || var == ACC_ROUTINE_DIR) - return NULL; - else if (var == SPF_ANALYSIS_DIR || var == FORMAT_STAT) - return isSgExecutableStatement(pt->lexNext()); else return (SgExecutableStatement*)pt; -#else - return (SgExecutableStatement*)pt; -#endif } else { -#if __SPF - const int var = pt->variant(); - if (var == SPF_PARALLEL_DIR) - return (SgExecutableStatement *)pt; - if (var == SPF_ANALYSIS_DIR || var == SPF_PARALLEL_REG_DIR) - return isSgExecutableStatement(pt->lexNext()); - if (var == SPF_END_PARALLEL_REG_DIR) - return isSgExecutableStatement(pt->lexPrev()); - if (var == SPF_TRANSFORM_DIR) + if (SgStatement::isSapforRegime()) { - SgExpression *ex = pt->expr(0); - while (ex) + const int var = pt->variant(); + if (var == SPF_PARALLEL_DIR) + return (SgExecutableStatement*)pt; + if (var == SPF_ANALYSIS_DIR || var == SPF_PARALLEL_REG_DIR) + return isSgExecutableStatement(pt->lexNext()); + if (var == SPF_END_PARALLEL_REG_DIR) + return isSgExecutableStatement(pt->lexPrev()); + if (var == SPF_TRANSFORM_DIR) { - if (ex->lhs()->variant() == SPF_NOINLINE_OP) - return NULL; - else if (ex->lhs()->variant() == SPF_FISSION_OP || ex->lhs()->variant() == SPF_EXPAND_OP) - return (SgExecutableStatement *)pt; + SgExpression* ex = pt->expr(0); + while (ex) + { + if (ex->lhs()->variant() == SPF_NOINLINE_OP) + return NULL; + else if (ex->lhs()->variant() == SPF_FISSION_OP || ex->lhs()->variant() == SPF_EXPAND_OP) + return (SgExecutableStatement*)pt; - ex = ex->rhs(); + ex = ex->rhs(); + } } - } - if (var == DVM_PARALLEL_ON_DIR || var == ACC_REGION_DIR || var == ACC_END_REGION_DIR || var == DVM_EXIT_INTERVAL_DIR) - return (SgExecutableStatement *)pt; - if (var == DVM_INTERVAL_DIR) - return isSgExecutableStatement(pt->lexNext()); - if (var == DVM_ENDINTERVAL_DIR) - return isSgExecutableStatement(pt->lexPrev()); - if (var == DVM_BARRIER_DIR) - return (SgExecutableStatement *)pt; - if (var == DVM_INHERIT_DIR) - return NULL; - if (var == DVM_INHERIT_DIR || var == DVM_ALIGN_DIR || var == DVM_DYNAMIC_DIR || - var == DVM_DISTRIBUTE_DIR || var == DVM_VAR_DECL || var == DVM_SHADOW_DIR || - var == DVM_HEAP_DIR || var == DVM_CONSISTENT_DIR || var == DVM_POINTER_DIR) - return NULL; -#endif + if (var == DVM_PARALLEL_ON_DIR || var == ACC_REGION_DIR || var == ACC_END_REGION_DIR || var == DVM_EXIT_INTERVAL_DIR) + return (SgExecutableStatement*)pt; + if (var == DVM_INTERVAL_DIR) + return isSgExecutableStatement(pt->lexNext()); + if (var == DVM_ENDINTERVAL_DIR) + return isSgExecutableStatement(pt->lexPrev()); + if (var == DVM_BARRIER_DIR) + return (SgExecutableStatement*)pt; + if (var == DVM_INHERIT_DIR) + return NULL; + if (var == DVM_INHERIT_DIR || var == DVM_ALIGN_DIR || var == DVM_DYNAMIC_DIR || + var == DVM_DISTRIBUTE_DIR || var == DVM_VAR_DECL || var == DVM_SHADOW_DIR || + var == DVM_HEAP_DIR || var == DVM_CONSISTENT_DIR || var == DVM_POINTER_DIR) + return NULL; + } return NULL; } } diff --git a/dvm/fdvm/trunk/Sage/lib/include/libSage++.h b/dvm/fdvm/trunk/Sage/lib/include/libSage++.h index 432ad00..97d6425 100644 --- a/dvm/fdvm/trunk/Sage/lib/include/libSage++.h +++ b/dvm/fdvm/trunk/Sage/lib/include/libSage++.h @@ -126,6 +126,7 @@ private: SgProject *project; bool unparseIgnore; + static bool sapfor_regime; static std::string currProcessFile; static int currProcessLine; static bool deprecatedCheck; @@ -335,6 +336,9 @@ public: static void setCurrProcessLine(const int line) { currProcessLine = line; } static std::string getCurrProcessFile() { return currProcessFile; } static int getCurrProcessLine() { return currProcessLine; } + + static void setSapforRegime() { sapfor_regime = true; } + static bool isSapforRegime() { return sapfor_regime; } }; class SgExpression diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp index 746f53b..aa52cef 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp @@ -8,24 +8,489 @@ #include "../Utils/errors.h" #include "../Utils/SgUtils.h" +#include "../Utils/utils.h" + +#include "../VerificationCode/verifications.h" #include "ConvertFiles.h" +#include "calls.h" using namespace std; -extern int convert_file(int argc, char* argv[], const char* proj_name); +extern int v_print; +extern int warn_all; +extern int unparse_functions; +extern int opt_base; +extern int opt_loop_range; + +extern graph_node* cur_node; +extern graph_node* node_list; + +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; + +static int convertFile(int argc, char* argv[], const string& fileToConv, + const set& filesInProj, const set& moduleDeclsInFiles) +{ + FILE* fout = NULL; + FILE* fout_cuf = NULL, * fout_C_cu = NULL, * fout_info = NULL; /*ACC*/ + const char* fout_name = "out.DVMH.f"; + char* fout_name_cuf; /*ACC*/ + char* fout_name_C_cu; /*ACC*/ + char* fout_name_info_C; /*ACC*/ + + char* source_name; + int level, hpf, openmp, isz, dvm_type_size; + int a_mode = 0; + + initialize(); + + openmp = hpf = 0; dvm_type_size = 0; + + argv++; + while ((argc > 1) && (*argv)[0] == '-') + { + if ((*argv)[1] == 'o' && ((*argv)[2] == '\0')) { + fout_name = argv[1]; + argv++; + argc--; + } + else if (!strcmp(argv[0], "-dc")) + check_regim = 1; + else if (!strcmp(argv[0], "-dbif1")) + dbg_if_regim = 1; + else if (!strcmp(argv[0], "-dbif2")) + dbg_if_regim = 2; + else if (!strcmp(argv[0], "-speedL0")) /* for dedugging ACROSS-scheme */ + options.setOn(SPEED_TEST_L0); /*ACC*/ + else if (!strcmp(argv[0], "-speedL1")) /* for dedugging ACROSS-scheme */ + options.setOn(SPEED_TEST_L1); /*ACC*/ + else if (!strcmp(argv[0], "-dmpi")) + deb_mpi = 1; + else if (!strcmp(argv[0], "-dnoind")) + d_no_index = 1; + else if (!strcmp(argv[0], "-dperf")) { + debug_regim = 1; + omp_debug = DPERF; + } + else if (!strcmp(argv[0], "-dvmLoopAnalysisEC")) /*ACC*/ + { + options.setOn(LOOP_ANALYSIS); + options.setOn(OPT_EXP_COMP); + } + else if (!strcmp(argv[0], "-dvmIrregAnalysis")) /*ACC*/ + { + options.setOn(LOOP_ANALYSIS); + options.setOn(OPT_EXP_COMP); + options.setOn(GPU_IRR_ACC); + } + else if (!strcmp(argv[0], "-dvmLoopAnalysis")) /*ACC*/ + options.setOn(LOOP_ANALYSIS); + else if (!strcmp(argv[0], "-dvmPrivateAnalysis")) /*ACC*/ + options.setOn(PRIVATE_ANALYSIS); + else if ((*argv)[1] == 'd') { + switch ((*argv)[2]) { + case '0': level = 0; break; + case '1': level = 1; omp_debug = D1; /*OMP*/ break; + case '2': level = 2; omp_debug = D2; /*OMP*/ break; + case '3': level = 3; omp_debug = D3; /*OMP*/ break; + case '4': level = 4; omp_debug = D4; /*OMP*/ break; + case '5': level = 5; omp_debug = D5; /*OMP*/ break; + /* case '5': level = -1; many_files=1; break;*/ + default: level = -1; + } + if (level > 0) + debug_regim = 1; + if ((*argv)[3] == '\0') + AddToFragmentList(0, 0, level, -1); + else if ((*argv)[3] == ':') + FragmentList(*argv + 4, level, -1); + } + else if ((*argv)[1] == 'e') { + switch ((*argv)[2]) { + case '0': level = 0; break; + case '1': level = 1; break; + case '2': level = 2; break; + case '3': level = 3; break; + case '4': level = 4; break; + case 'm': omp_perf = 1; break; + default: level = -1; + } + if ((*argv)[3] == '\0') + AddToFragmentList(0, 0, -1, level); + else if ((*argv)[3] == ':') + FragmentList(*argv + 4, -1, level); + } + else if (!strcmp(argv[0], "-spf")) + { + fprintf(stderr, "Illegal option -spf \n"); + return 1; + } + else if (!strcmp(argv[0], "-p")) { + only_debug = 0; hpf = 0; + } + else if (!strcmp(argv[0], "-s")) { + only_debug = 1; hpf = 0; + } + else if (!strcmp(argv[0], "-v")) + v_print = 1; + else if (!strcmp(argv[0], "-w")) + warn_all = 1; + else if (!strcmp(argv[0], "-bind0")) + bind_ = 0; + else if (!strcmp(argv[0], "-bind1")) + bind_ = 1; + else if (!strcmp(argv[0], "-t8")) + dvm_type_size = 8; + else if (!strcmp(argv[0], "-t4")) + dvm_type_size = 4; + else if (!strcmp(argv[0], "-r8")) + default_real_size = 8; + else if (!strcmp(argv[0], "-i8")) + default_integer_size = 8; + else if (!strcmp(argv[0], "-hpf") || !strcmp(argv[0], "-hpf1") || !strcmp(argv[0], "-hpf2")) + hpf = 1; + else if (!strcmp(argv[0], "-mp")) { + OMP_program = 1; /*OMP*/ + openmp = 1; + } + //else if (!strcmp(argv[0],"-ta")) + // ACC_program = 1; + else if (!strcmp(argv[0], "-noH")) + ACC_program = 0; + else if (!strcmp(argv[0], "-noCudaType")) /*ACC*/ + undefined_Tcuda = 1; + else if (!strcmp(argv[0], "-noCuda")) + options.setOn(NO_CUDA); /*ACC*/ + else if (!strcmp(argv[0], "-noPureFunc")) + options.setOn(NO_PURE_FUNC); /*ACC*/ + else if (!strcmp(argv[0], "-C_Cuda")) /*ACC*/ + options.setOn(C_CUDA); + else if (!strcmp(argv[0], "-FTN_Cuda") || !strcmp(argv[0], "-F_Cuda")) /*ACC*/ + options.setOff(C_CUDA); + else if (!strcmp(argv[0], "-no_blocks_info") || !strcmp(argv[0], "-noBI")) + options.setOn(NO_BL_INFO); /*ACC*/ + else if (!strcmp(argv[0], "-cacheIdx")) + options.setOff(NO_BL_INFO); /*ACC*/ + else if (!strcmp(argv[0], "-Ohost")) /*ACC*/ + options.setOn(O_HOST); + else if (!strcmp(argv[0], "-noOhost")) /*ACC*/ + options.setOff(O_HOST); + else if (!strcmp(argv[0], "-Opl2")) /*ACC*/ + { + parloop_by_handler = 2; + options.setOn(O_HOST); + options.setOn(O_PL2); + // options.setOn(NO_CUDA); + } + else if (!strcmp(argv[0], "-Opl")) /*ACC*/ + { + parloop_by_handler = 1; + options.setOn(O_PL); + } + else if (!strcmp(argv[0], "-oneThread")) /*ACC*/ + options.setOn(ONE_THREAD); + else if (!strcmp(argv[0], "-noTfm")) /*ACC*/ + options.setOff(AUTO_TFM); + else if (!strcmp(argv[0], "-autoTfm")) /*ACC*/ + options.setOn(AUTO_TFM); + else if (!strcmp(argv[0], "-gpuO0")) /*ACC*/ + options.setOn(GPU_O0); + else if (!strcmp(argv[0], "-gpuO1")) /*ACC*/ + options.setOn(GPU_O1); + else if (!strcmp(argv[0], "-rtc")) /*ACC*/ + options.setOn(RTC); //for NVRTC compilation and execution + else if (!strcmp(argv[0], "-ffo")) + out_free_form = 1; + else if (!strcmp(argv[0], "-upcase")) + 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")) + { + (void)fprintf(stderr, "Illegal option -lgstd \n"); + return 1; + } + else if (!strcmp(argv[0], "-byFunUnparse")) + unparse_functions = 1; + else if (!strncmp(argv[0], "-bufio", 6)) { + if ((*argv)[6] != '\0' && (isz = is_integer_value(*argv + 6))) + IOBufSize = isz; + } + else if (!strncmp(argv[0], "-bufUnparser", 12)) { + if ((*argv)[12] != '\0' && (isz = is_integer_value(*argv + 12))) + UnparserBufSize = isz * 1024 * 1024; + } + else if (!strcmp(argv[0], "-ioRTS")) + options.setOn(IO_RTS); + else if (!strcmp(argv[0], "-read_all")) + options.setOn(READ_ALL); + else if (!strcmp(argv[0], "-Obase")) + opt_base = 1; + else if (!strcmp(argv[0], "-Oloop_range")) + opt_loop_range = 1; + else if ((*argv)[1] == 'H') { + if ((*argv)[2] == 's' && (*argv)[3] == 'h' && (*argv)[4] == 'w') { + if ((*argv)[5] != '\0' && (all_sh_width = is_integer_value(*argv + 5))) + ; + } + else if (!strcmp(*argv + 2, "nora")) + no_rma = 1; + else if (!strcmp(*argv + 2, "oneq")) + one_inquiry = 1; + else if (!strcmp(*argv + 2, "onlyl")) + only_local = 1; + } + else if (!strncmp(argv[0], "-collapse", 9)) + if ((*argv)[9] != '\0' && (collapse_loop_count = is_integer_value(*argv + 9))); + argc--; + argv++; + } + + // Check options combinations + options.checkCombinations(); + + // definition of DvmType size: len_DvmType + // len_DvmType==0, if DvmType-size == default_integer_size == 4 + if (bind_ == 1) + len_DvmType = 8; //sizeof(long) == 8 + if (dvm_type_size) + len_DvmType = dvm_type_size; + if (len_DvmType == 0 && default_integer_size == 8) + len_DvmType = 4; + + if (ACC_program && debug_regim && !only_debug) + { + fprintf(stderr, "Warning: -noH option is set to debug mode\n"); + ACC_program = 0; + } + if (parloop_by_handler > 0 && debug_regim) + { + fprintf(stderr, "Warning: -Opl/Opl2 option is ignored in debug mode\n"); + parloop_by_handler = 0; + options.setOff(O_PL); + options.setOff(O_PL2); + } + + if (openmp && ACC_program) + { + fprintf(stderr, "Warning: -noH option is set to -mp mode\n"); + ACC_program = 0; + } + if (parloop_by_handler == 2 && !options.isOn(O_HOST)) + { + 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) + { + 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"); + + //------------------------------------------------------------------------------ + + string proj_name = "fdvm__"; + static int id = 0; + + vector filesList; + filesList.push_back((char*)fileToConv.c_str()); + + SgProject project((proj_name + to_string(id++)).c_str(), filesList.data(), filesList.size()); + + shiftLines(&project, false); + for (int z = 0; z < project.numberOfFiles(); ++z) { + vector tmp; + removeExecutableFromModuleDeclaration(&(project.file(z)), filesInProj, tmp); + } + + SgFile* file; + addNumberOfFileToAttribute(&project); + + //---------------------------- + ProjectStructure(project); + Private_Vars_Project_Analyzer(); + //---------------------------- + + initVariantNames(); //for project + initIntrinsicFunctionNames(); //for project + initSupportedVars(); // for project, acc_f2c.cpp + initF2C_FunctionCalls(); // for project, acc_f2c.cpp + for (int id = project.numberOfFiles() - 1; id >= 0; id--) + { + file = &(project.file(id)); //file->unparsestdout(); + fin_name = new char[strlen(project.fileName(id)) + 2]; + sprintf(fin_name, "%s%s", project.fileName(id), " "); + fout_name = doOutFileName(file->filename()); //project.fileName(id); + + if (fout_name && source_name && !strcmp(source_name, fout_name)) + { + fprintf(stderr, "Output file has the same name as source file\n"); + return 1; + } + + fout_name_cuf = ChangeFtoCuf(fout_name); /*ACC*/ + fout_name_C_cu = ChangeFto_C_Cu(fout_name); /*ACC*/ + fout_name_info_C = ChangeFto_info_C(fout_name); /*ACC*/ + + //set the last symbol of file + last_file_symbol = CUR_FILE_CUR_SYMB(); + initLibNames(); //for every file + InitDVM(file); //for every file + current_file = file; // global variable (used in SgTypeComplex) + max_lab = getLastLabelId(); + + if (dbg_if_regim) + GetLabel(); + + TranslateFileDVM(file); + + if (err_cnt) { + fprintf(stderr, "%d error(s)\n", err_cnt); + return 1; + } + + if (!fout_name) { + file->unparsestdout(); + return 0; + } + + //writing result of converting into file + if ((fout = fopen(fout_name, "w")) == NULL) { + fprintf(stderr, "Can't open file %s for write\n", fout_name); + return 1; + } + + if (GeneratedForCuda()) /*ACC*/ + { + if ((fout_C_cu = fopen(fout_name_C_cu, "w")) == NULL) { + fprintf(stderr, "Can't open file %s for write\n", fout_name_C_cu); + return 1; + } + + if (!options.isOn(C_CUDA)) { + if ((fout_cuf = fopen(fout_name_cuf, "w")) == NULL) { + fprintf(stderr, "Can't open file %s for write\n", fout_name_cuf); + return 1; + } + } + + if ((fout_info = fopen(fout_name_info_C, "w")) == NULL) { + fprintf(stderr, "Can't open file %s for write\n", fout_name_info_C); + return 1; + } + } + + + if (v_print) + fprintf(stderr, "<<<<< Unparsing %s >>>>>\n", fout_name); + + if (mod_gpu) /*ACC*/ + UnparseTo_CufAndCu_Files(file, fout_cuf, fout_C_cu, fout_info); + + const string fileN = file->filename(); + hideUnnecessary(file, fileN, moduleDeclsInFiles, true); + + if (unparse_functions) + UnparseFunctionsOfFile(file, fout); + else if (UnparserBufSize) + file->unparseS(fout, UnparserBufSize); + else + file->unparse(fout); + + if ((fclose(fout)) < 0) { + fprintf(stderr, "Could not close %s\n", fout_name); + return 1; + } + + if (GeneratedForCuda()) { + if ((fclose(fout_C_cu)) < 0) { + fprintf(stderr, "Could not close %s\n", fout_name_C_cu); + return 1; + } + + if (!options.isOn(C_CUDA)) { + if ((fclose(fout_cuf)) < 0) { + fprintf(stderr, "Could not close %s\n", fout_name_cuf); + return 1; + } + } + + if ((fclose(fout_info)) < 0) { + fprintf(stderr, "Could not close %s\n", fout_name_info_C); + return 1; + } + } + + } + + if (v_print) + fprintf(stderr, "\n***** Done *****\n"); + return 0; +} + +static void preprocess(const vector& files, map>& moduleUsesByFile, map& moduleDecls) +{ + vector filesList; + for (int z = 0; z < files.size(); ++z) + filesList.push_back((char*)files[z].c_str()); + + SgProject tmpProj("preproc", filesList.data(), files.size()); + + for (int z = 0; z < tmpProj.numberOfFiles(); ++z) + { + SgFile* file = &(tmpProj.file(z)); + fillModuleUse(file, moduleUsesByFile, moduleDecls); + } + + InitializeTable(); +} void convertFiles(int argc, char* argv[], const char* proj_name) { vector 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); + const string fileText = readFileToStr(proj_name); + vector files; + set filesinSet; + splitString(fileText, '\n', files); + + for (auto& file : files) + filesinSet.insert(file); + + map> moduleUsesByFile; + map moduleDecls; + set moduleDeclsInFiles; + + preprocess(files, moduleUsesByFile, moduleDecls); + for (auto& elem : moduleDecls) + moduleDeclsInFiles.insert(elem.second); + + int codes = 0; + for (auto& file : files) + { + codes += convertFile(args_v.size() - 1, args_v.data(), file, filesinSet, moduleDeclsInFiles); + + cur_node = node_list = NULL; + InitializeTable(); + } + + if (codes == 0) + printf("Convertation was completed successfully\n"); + exit(0); } \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp index 198e7bd..cfb469e 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp @@ -186,6 +186,10 @@ static set applyModuleDeclsForFile(FileInfo *forFile, const mapfileName, data); + /*static string tmp = "tmp__"; + static int id = 0; + writeFileFromStr(tmp + to_string(id++) + ".ftn", data);*/ + forFile->includesAdded = included.size(); forFile->includes = included; @@ -275,7 +279,7 @@ static vector parseList(vector& listOfProject, } FILE* depPath = fopen(elem.outDepPath.c_str(), "r"); - if (depPath && !isFromConsole) + if (depPath && !isFromConsole && !needToIncludeForInline) { fclose(depPath); if (elem.error <= 0) @@ -302,7 +306,7 @@ static vector parseList(vector& listOfProject, StdCapture::BeginCapture(); if (needToInclude) filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline); - else if (needToIncludeForInline) // TODO for modules + else if (needToIncludeForInline) filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline); int retCode = parse_file(optSplited.size(), toParse, "dvm.proj"); @@ -521,7 +525,7 @@ static void parseFiles(int& iters, vector& errors, vector& lis { int changed = 0; int lastChanged = 0; - const string projName = "tmp"; + const string projName = (parseForInlining == 0) ? "tmp" : "tmp_inl"; do { @@ -635,11 +639,7 @@ static int parseFiles(vector& errors, vector& listOfProject, v filesCompilationOrder = finalyzeParsing(listOfProject, mapModuleDeps, moduleDelc); if (parseForInlining) - { - for (auto& file : listOfProject) - file.outDepPath = ""; parseFiles(iters, errors, listOfProject, mapModuleDeps, moduleDelc, modDirectOrder, true, isFromConsole); - } } catch (int err) { @@ -756,6 +756,9 @@ void parseFiles(int argc, char** argv) } vector filesCompilationOrder; + if (isInline) + printf(" run parsing for inlining\n"); + int rethrow = parseFiles(errors, listOfProject, filesCompilationOrder, isInline, true); if (rethrow == 0) { diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp index a395ec4..2adeab4 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp @@ -271,6 +271,41 @@ static int reverseVar(SgStatement* st) static map>> insertedIncludes; static set genVersionDone; +set hideUnnecessary(SgFile* file, const string& fileN, const set& moduleDeclsInFiles, bool dontReplaceIncludes) +{ + set changedVars; + for (SgStatement* st = file->firstStatement(); st; st = st->lexNext()) + { + if (dontReplaceIncludes == false) + { + if (st->fileName() != fileN || st->getUnparseIgnore()) + if (st->variant() > 0) + changedVars.insert(reverseVar(st)); + } + else + { + if (st->getUnparseIgnore()) + { + if (st->variant() > 0) + changedVars.insert(reverseVar(st)); + } + else if (st->fileName() != fileN) + { + if (st->variant() == MODULE_STMT && moduleDeclsInFiles.find(st->fileName()) != moduleDeclsInFiles.end()) + { + for (auto mSt = st; mSt != st->lastNodeOfStmt(); mSt = mSt->lexNext()) + changedVars.insert(reverseVar(mSt)); + + st = st->lastNodeOfStmt(); + changedVars.insert(reverseVar(st)); + } + } + } + } + + return changedVars; +} + //TODO: read includes and find last lines, all included files string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const char *fout, set &allIncludeFiles, bool outFree, @@ -573,38 +608,9 @@ string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const ch } } - - set changedVars; //remove unnecessary //XXX: use Sage hack!! - for (SgStatement* st = file->firstStatement(); st; st = st->lexNext()) - { - if (dontReplaceIncludes == false) - { - if (st->fileName() != fileN || st->getUnparseIgnore()) - if (st->variant() > 0) - changedVars.insert(reverseVar(st)); - } - else - { - if (st->getUnparseIgnore()) - { - if (st->variant() > 0) - changedVars.insert(reverseVar(st)); - } - else if (st->fileName() != fileN) - { - if (st->variant() == MODULE_STMT && moduleDeclsInFiles.find(st->fileName()) != moduleDeclsInFiles.end()) - { - for (auto mSt = st; mSt != st->lastNodeOfStmt(); mSt = mSt->lexNext()) - changedVars.insert(reverseVar(mSt)); - - st = st->lastNodeOfStmt(); - changedVars.insert(reverseVar(st)); - } - } - } - } + set changedVars = hideUnnecessary(file, fileN, moduleDeclsInFiles, dontReplaceIncludes); set checkPositions; for (auto& elem : includeFiles) @@ -3069,6 +3075,56 @@ set getAllVariables(SgStatement* stFrom, SgStatement* stTo, const set& v template set getAllVariables(SgStatement* stFrom, SgStatement* stTo, const set& variants); template set getAllVariables(SgStatement* stFrom, SgStatement* stTo, const set& variants); +void shiftLines(SgProject* project, bool print) +{ + map shifts; + //shift lines if modules included + const string shiftInfo = "!SPF NUM FILES"; + for (int z = 0; z < project->numberOfFiles(); ++z) + { + SgFile* file = &(project->file(z)); + const string fileN = file->filename(); + + int shiftN = 0; + for (auto st = file->firstStatement(); st; st = st->lexNext()) + { + if (st->comments()) + { + string comms = st->comments(); + auto it = comms.find(shiftInfo); + if (it != string::npos) + { + it += shiftInfo.size() + 1; + + if (sscanf(comms.c_str() + it, "%d", &shiftN) == -1) + { + __spf_print(print, "comm %s, shifted %s\n", comms.c_str(), comms.c_str() + it); + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + } + break; + } + } + } + + if (shiftN == 0) + continue; + shifts[fileN] = shiftN + 1; // with comment !SPF NUM FILES + __spf_print(print, " shift by %d for %s\n", shiftN, fileN.c_str()); + } + + for (int z = 0; z < project->numberOfFiles(); ++z) + { + SgFile* file = &(project->file(z)); + for (SgStatement* st = file->firstStatement()->lexNext(); st; st = st->lexNext()) + { + string currF = st->fileName(); + auto it = shifts.find(currF); + if (it != shifts.end() && file->filename() == currF) + st->setlineNumber(st->lineNumber() - it->second); + } + } +} + SgProject* createProject(const char* proj_name, vector& parallelRegions, vector& subs_parallelRegions, @@ -3078,6 +3134,7 @@ SgProject* createProject(const char* proj_name, map& moduleDecls, map>>& exctactedModuleStats) { + Statement::setSapforRegime(); Statement::deactiveConsistentchecker(); sortFilesBySize(proj_name); SgProject* project = new SgProject(proj_name); @@ -3245,53 +3302,9 @@ SgProject* createProject(const char* proj_name, } } filterModuleUse(moduleUsesByFile, moduleDecls); - - map shifts; - //shiftLines if modules included - const string shiftInfo = "!SPF NUM FILES"; - for (int z = 0; z < project->numberOfFiles(); ++z) - { - SgFile* file = &(project->file(z)); - const string fileN = file->filename(); - - int shiftN = 0; - for (auto st = file->firstStatement(); st; st = st->lexNext()) - { - if (st->comments()) - { - string comms = st->comments(); - auto it = comms.find(shiftInfo); - if (it != string::npos) - { - it += shiftInfo.size() + 1; - if (sscanf(comms.c_str() + it, "%d", &shiftN) == -1) - { - __spf_print(1, "comm %s, shifted %s\n", comms.c_str(), comms.c_str() + it); - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - } - break; - } - } - } - - if (shiftN == 0) - continue; - shifts[fileN] = shiftN + 1; // with comment !SPF NUM FILES - __spf_print(1, " shift by %d for %s\n", shiftN, fileN.c_str()); - } - - for (int z = 0; z < project->numberOfFiles(); ++z) - { - SgFile* file = &(project->file(z)); - for (SgStatement* st = file->firstStatement()->lexNext(); st; st = st->lexNext()) - { - string currF = st->fileName(); - auto it = shifts.find(currF); - if (it != shifts.end() && file->filename() == currF) - st->setlineNumber(st->lineNumber() - it->second); - } - } + //shift lines if modules included + shiftLines(project); map> functions; for (int z = 0; z < project->numberOfFiles(); ++z) diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h index 0222d18..176a5cf 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h @@ -11,6 +11,7 @@ SgStatement* declaratedInStmt(SgSymbol *toFind, std::vector *allDe #include "DefUseList.h" #include "CommonBlock.h" +std::set hideUnnecessary(SgFile* file, const std::string& fileN, const std::set& moduleDeclsInFiles, const bool dontReplaceIncludes); std::string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const char *fout, std::set &allIncludeFiles, bool outFree, const std::map> &moduleUsesByFile, const std::map& moduleDelcs, const std::map>& exctactedModuleStats, bool toString, bool dontReplaceIncls = false); SgSymbol* findSymbolOrCreate(SgFile *file, const std::string toFind, SgType *type = NULL, SgStatement *scope = NULL); void recExpressionPrint(SgExpression *exp); @@ -107,3 +108,5 @@ void removeSpecialCommentsFromProject(SgFile* file); void getMaxMinBlockDistribution(SgFile* file, std::pair& min_max); void addPrivatesToArraysFromGUI(SgFile* file, const std::map, std::pair>& declaredArrays, const std::map& distrStateFromGUI); + +void shiftLines(SgProject* project, bool print = true); \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 7f5b76e..cab6d43 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 "2282" +#define VERSION_SPF "2285"