From 63c32b08daa4281ebd4156fb152d871f9d503a9b Mon Sep 17 00:00:00 2001 From: Egor Mayorov Date: Wed, 11 Dec 2024 23:44:25 +0300 Subject: [PATCH] fix some troubles --- .../ProjectParameters/projectParameters.cpp | 163 +++++++++--------- .../ProjectParameters/projectParameters.h | 11 +- sapfor/experts/Sapfor_2017/_src/SapforData.h | 2 +- 3 files changed, 90 insertions(+), 86 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectParameters/projectParameters.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectParameters/projectParameters.cpp index 524f67d..fd295ce 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectParameters/projectParameters.cpp +++ b/sapfor/experts/Sapfor_2017/_src/ProjectParameters/projectParameters.cpp @@ -69,6 +69,7 @@ SAPFOR::BasicBlock* GetBasicBlocksForLoop(LoopGraph* loop, vector FindFuncInfoVec(string funcname, const map> &allFuncInfo){ for(pair> p: allFuncInfo) { if(p.first == funcname){ @@ -79,6 +80,7 @@ vector FindFuncInfoVec(string funcname, const map FindBlocksVec(FuncInfo* func, const map> &fullIR){ for(const pair>& p: fullIR) { if(p.first == func){ @@ -89,6 +91,7 @@ vector FindBlocksVec(FuncInfo* func, const map FindLoopsWithFilename(string filename, const map> loopGraph){ for(pair> p: loopGraph) { if(p.first == filename){ @@ -99,25 +102,26 @@ vector FindLoopsWithFilename(string filename, const map> found, int block_num, SAPFOR::Instruction* instr){ -// for(pair p: found){ -// if(p.first == block_num && p.second == instr){ -// return true; -// } -// } -// return -// } +/*checks if instruction is found for result*/ +bool CheckIfInstructionIsFound(vector> found, int block_num, SAPFOR::Instruction* instr){ + for(pair p: found){ + if(p.first == block_num && p.second == instr){ + return true; + } + } + return false; +} -map< pair, set> +/*main pass function*/ +map>> findParameters(const map> &defUseByFunctions, const map &commonBlocks, const map> &allFuncInfo, const map> &fullIR, const map>& loopGraph) { - map, set> foundParameters; - // print and look part (Yes, I am stupid) /* + // print and look part (Yes, I am stupid) printf("\n\n\n\nPRINT EXAMPLE\n\n\n\n\n"); cout << "DEF USE INFO" << endl; for(pair> p: defUseByFunctions) { @@ -183,57 +187,58 @@ map< pair, set> */ - // Находим для каждой функции блоки, с которых начинаются циклы + /*find blocks with loops start for each function*/ map> mapFuncLoops; - for(const pair>& p: fullIR) { - vector loopsForFile = FindLoopsWithFilename(p.first->fileName, loopGraph); + for(const pair>& partIR: fullIR) { + vector loopsForFile = FindLoopsWithFilename(partIR.first->fileName, loopGraph); SAPFOR::BasicBlock* head = nullptr; - for(auto v: loopsForFile){ + for(auto vec: loopsForFile){ head = nullptr; - head = GetBasicBlocksForLoop(v, p.second); + head = GetBasicBlocksForLoop(vec, partIR.second); if(head != nullptr){ - mapFuncLoops[p.first].push_back(head); + mapFuncLoops[partIR.first].push_back(head); } } } - // На всякий случай выводим их - cout << "ALL LOOPS" << endl; - for(auto m: mapFuncLoops){ - cout << "func: " << m.first->funcName << endl; - for(auto v: m.second){ - cout << "block num: " << v->getNumber() << endl; - cout << v->getInstructions().front()->getInstruction()->getOperator()->sunparse() << endl; - } - } + /*print found blocks with loops for each function*/ + // cout << "ALL LOOPS" << endl; + // for(auto m: mapFuncLoops){ + // cout << "func: " << m.first->funcName << endl; + // for(auto v: m.second){ + // cout << "block num: " << v->getNumber() << endl; + // cout << v->getInstructions().front()->getInstruction()->getOperator()->sunparse() << endl; + // } + // } - map> finalInstructions; + /*find instructions with read operaton which affects on number iterations for loops*/ + map>> finalInstructions; + /*for each function*/ for(const auto lookedFunc: mapFuncLoops){ - for(const auto& v: lookedFunc.second){ - // cout << "NEW LOOP SEARCH" << endl; - SAPFOR::Instruction* instr = v->getInstructions().front()->getInstruction(); - // cout << instr->getOperator()->sunparse() << endl;; - vector blocks = v->getPrev(); + /*for each loop in function*/ + for(const auto& vectElem: lookedFunc.second){ + SAPFOR::Instruction* instr = vectElem->getInstructions().front()->getInstruction(); + vector blocks = vectElem->getPrev(); vector argumentsForLook; argumentsForLook.push_back(instr->getArg2()); - int block_ind = 0; - // for(auto pr: argumentsForLook){cout << "FIRST ARGUMENT TO FIND: " << pr->getValue() << endl;} - while(block_ind < blocks.size()){ - vector blockInstructions = blocks[block_ind]->getInstructions(); - int vec_size = blockInstructions.size(); - // cout << "NUMBER OF INSTRUCTIONS IN BLOCK " << blocks[block_ind]->getNumber() << " IS: " << vec_size << endl; - for(int i=vec_size-1; i>=0; i--){ - // cout << "ALL ARGUMENTS TO FIND: " << endl; - // for(auto pr: argumentsForLook){cout << pr->getValue() << " ";} - // cout << endl; + int blockInd = 0; + /*run through blocks from first found to first in program taking previous blocks on each step*/ + while(blockInd < blocks.size()){ + if(argumentsForLook.empty()){ + break; + } + vector blockInstructions = blocks[blockInd]->getInstructions(); + int numberOfInstructions = blockInstructions.size(); + /*run through all instructions of block from bottom to up*/ + for(int i=numberOfInstructions-1; i>=0; i--){ SAPFOR::Instruction* cur = blockInstructions[i]->getInstruction(); - // cout << "CUR INSTRUCTION: " << cur->dump() << endl; - // cout << "Instruction Operation (type): " << SAPFOR::CFG_OP_S[(int)cur->getOperation()] << endl; + /*if instuction type is CALL*/ if(cur->getOperation() == SAPFOR::CFG_OP::F_CALL){ + /*if it is CALL of READ function*/ if(cur->getArg1()->getValue() == "_READ"){ - cout << "READ OPERATION FOUND" << endl; int numParams = std::stoi(cur->getArg2()->getValue()); bool flag = false; + /*drop all found arguments from argumentsForLook*/ for(int j=i-1; j>i-numParams-1; j--){ auto iter = find(argumentsForLook.begin(), argumentsForLook.end(), blockInstructions[j]->getInstruction()->getArg1()); if(iter != argumentsForLook.end()){ @@ -241,17 +246,17 @@ map< pair, set> flag = true; } } - if(flag && find(finalInstructions[lookedFunc.first].begin(), finalInstructions[lookedFunc.first].end(), cur) == finalInstructions[lookedFunc.first].end()){ - finalInstructions[lookedFunc.first].push_back(cur); + /*add read instruction to result vector if needed*/ + if(flag && !CheckIfInstructionIsFound(finalInstructions[lookedFunc.first], blocks[blockInd]->getNumber(), cur)){ + finalInstructions[lookedFunc.first].push_back({blocks[blockInd]->getNumber(), cur}); } - // if(flag && CheckIfInstructionIsFound(finalInstructions[lookedFunc.first], blocks[block_ind]->getNumber(), cur)){ - // finalInstructions[lookedFunc.first].push_back({blocks[block_ind]->getNumber(), cur}); - // } i = i-numParams; continue; } + /*if it is CALL of another function than find it's arguments and result to change argumentsForLook vector if needed*/ auto it = find(argumentsForLook.begin(), argumentsForLook.end(), cur->getResult()); if(cur->getResult() && it != argumentsForLook.end()){ + int dist = distance(argumentsForLook.begin(), it); int numParams = std::stoi(cur->getArg2()->getValue()); for(int j=i-1; j>i-numParams-1; j--){ if(find(argumentsForLook.begin(), argumentsForLook.end(), blockInstructions[j]->getInstruction()->getArg1()) == argumentsForLook.end()){ @@ -259,68 +264,66 @@ map< pair, set> } } i = i-numParams; - argumentsForLook.erase(it); + argumentsForLook.erase(argumentsForLook.begin()+dist); continue; } } - // cout << "NOT F_CALL" << endl; - // cout << "CUR INSTRUCTION AGAIN: " << cur->dump() << endl; + /*check only some operation types because other types are useless for this task*/ if(instructionsToLook.find(cur->getOperation()) == instructionsToLook.end()){ - // cout << "NOT ARITH" << endl; continue; } - // cout << "ARITH OR ACCESS" << endl; + /*check if result of operation in argumentsForLook*/ auto it = find(argumentsForLook.begin(), argumentsForLook.end(), cur->getResult()); - // cout << "RESULT OF INSTRUCTION:" << *it->getValue() << endl; - if(it != argumentsForLook.end()){ - if(find(argumentsForLook.begin(), argumentsForLook.end(), cur->getArg1()) == argumentsForLook.end()){ - // Проверка типа аргумента. Добавляется только если это переменная или регистр + if(*it != NULL && it != argumentsForLook.end()){ + int dist = distance(argumentsForLook.begin(), it); + /*add arguments of operation to argumentsForLook vector if they are not there*/ + if(cur->getArg1() != NULL && find(argumentsForLook.begin(), argumentsForLook.end(), cur->getArg1()) == argumentsForLook.end()){ + /*add argument only if it's type is register or variable because constants are nnot needed here*/ if(goodArgTypes.find(cur->getArg1()->getType()) != goodArgTypes.end()){ argumentsForLook.push_back(cur->getArg1()); } } if(cur->getArg2() != NULL && find(argumentsForLook.begin(), argumentsForLook.end(), cur->getArg2()) == argumentsForLook.end()){ + /*add argument only if it's type is register or variable because constants are nnot needed here*/ if(goodArgTypes.find(cur->getArg2()->getType()) != goodArgTypes.end()){ argumentsForLook.push_back(cur->getArg2()); } } - argumentsForLook.erase(it); + /*drop found result of operation from argumentsForLook vector*/ + argumentsForLook.erase(argumentsForLook.begin()+dist); }; } - for(auto b: blocks[block_ind]->getPrev()){ + /*add prev blocks to run through*/ + for(auto b: blocks[blockInd]->getPrev()){ if(find(blocks.begin(), blocks.end(), b) == blocks.end()){ blocks.push_back(b); } } - block_ind++; - - // cout << "BLOCKS: "; - // for(auto bl: blocks){cout << bl->getNumber() << " ";} - // cout << endl; - // cout << "block_ind: " << block_ind << endl; + blockInd++; } - // cout << "END WHILE" << endl; - // В векторе argumentsForLook должны быть все аргументы инструкций, которые не получилось найти в блоках программы - // Возможно стоит еще их проверить в параметрах текущей функции, если они там есть + /*next step is next function that means next structures are not needed -> clear them*/ argumentsForLook.clear(); blocks.clear(); - // cout << "END CLEAR" << endl; + /*In the argumentsForLook vector may be arguments of instructions that could not be found in the blocks of current program or function. + Perhaps I need to look for them in the arguments of the current function, they should be there. + That means that I will need more complex processing of function calls*/ } } - - // Попытка напечатать полученные инструкции - cout << "\n\n\n\n\nFINAL INSTRUCTIONS" << endl; + /*print found instructions for each function or program*/ + cout << "\n\nFINAL INSTRUCTIONS" << endl; for(auto m: finalInstructions){ cout << "func: " << m.first->funcName << endl; for(auto v: m.second){ - cout << "instruction num: " << v->getNumber() << endl; - cout << v->dump() << endl; - cout << v->getOperator()->sunparse() << endl; + cout << "\tFilename: " << v.second->getOperator()->fileName() << endl; + cout << "\tLine number: " << v.second->getLine() << endl; + cout << "\tBlock num: " << v.first << endl; + cout << "\tIstruction num in block: " << v.second->getNumber() << endl; + cout << "\tInstruction dump: " << v.second->dump() << endl; + cout << "\tOperator" << v.second->getOperator()->sunparse() << endl; + cout << "\n"; } } - - - return foundParameters; + return finalInstructions; } \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectParameters/projectParameters.h b/sapfor/experts/Sapfor_2017/_src/ProjectParameters/projectParameters.h index 28e06ec..f461a52 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectParameters/projectParameters.h +++ b/sapfor/experts/Sapfor_2017/_src/ProjectParameters/projectParameters.h @@ -21,8 +21,9 @@ #include "../CFGraph/CFGraph.h" #include "../CFGraph/IR.h" -std::map< std::pair, std::set> findParameters(const std::map> &defUseByFunctions, - const std::map &commonBlocks, - const std::map> &allFuncInfo, - const std::map> &fullIR, - const std::map>& loopGraph); \ No newline at end of file +std::map>> + findParameters(const std::map> &defUseByFunctions, + const std::map &commonBlocks, + const std::map> &allFuncInfo, + const std::map> &fullIR, + const std::map>& loopGraph); \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/SapforData.h b/sapfor/experts/Sapfor_2017/_src/SapforData.h index 97fc31e..eb8613a 100644 --- a/sapfor/experts/Sapfor_2017/_src/SapforData.h +++ b/sapfor/experts/Sapfor_2017/_src/SapforData.h @@ -168,7 +168,7 @@ std::map filesInfo; // information about open,close,write and re // //for FIND_PARAMETERS -std::map< std::pair, std::set> parametersOfProject; // [file, line] -> set[vars] +std::map>> parametersOfProject; // FuncInfo -> vector[BlockNum, Instruction] // //for GET_MIN_MAX_BLOCK_DIST