fix some troubles

This commit is contained in:
Egor Mayorov
2024-12-11 23:44:25 +03:00
parent ba963634c2
commit 63c32b08da
3 changed files with 90 additions and 86 deletions

View File

@@ -69,6 +69,7 @@ SAPFOR::BasicBlock* GetBasicBlocksForLoop(LoopGraph* loop, vector<SAPFOR::BasicB
return head_block; return head_block;
} }
/*returns FuncInfo vetcor by filename*/
vector<FuncInfo*> FindFuncInfoVec(string funcname, const map<string, vector<FuncInfo*>> &allFuncInfo){ vector<FuncInfo*> FindFuncInfoVec(string funcname, const map<string, vector<FuncInfo*>> &allFuncInfo){
for(pair<string, vector<FuncInfo*>> p: allFuncInfo) { for(pair<string, vector<FuncInfo*>> p: allFuncInfo) {
if(p.first == funcname){ if(p.first == funcname){
@@ -79,6 +80,7 @@ vector<FuncInfo*> FindFuncInfoVec(string funcname, const map<string, vector<Func
return a; return a;
} }
/*returns BasicBlock vector for function by FuncInfo*/
vector<SAPFOR::BasicBlock*> FindBlocksVec(FuncInfo* func, const map<FuncInfo*, vector<SAPFOR::BasicBlock*>> &fullIR){ vector<SAPFOR::BasicBlock*> FindBlocksVec(FuncInfo* func, const map<FuncInfo*, vector<SAPFOR::BasicBlock*>> &fullIR){
for(const pair<FuncInfo*, vector<SAPFOR::BasicBlock*>>& p: fullIR) { for(const pair<FuncInfo*, vector<SAPFOR::BasicBlock*>>& p: fullIR) {
if(p.first == func){ if(p.first == func){
@@ -89,6 +91,7 @@ vector<SAPFOR::BasicBlock*> FindBlocksVec(FuncInfo* func, const map<FuncInfo*, v
return a; return a;
} }
/*returns LoopGraph vector by filename*/
vector<LoopGraph*> FindLoopsWithFilename(string filename, const map<string, vector<LoopGraph*>> loopGraph){ vector<LoopGraph*> FindLoopsWithFilename(string filename, const map<string, vector<LoopGraph*>> loopGraph){
for(pair<string, vector<LoopGraph*>> p: loopGraph) { for(pair<string, vector<LoopGraph*>> p: loopGraph) {
if(p.first == filename){ if(p.first == filename){
@@ -99,25 +102,26 @@ vector<LoopGraph*> FindLoopsWithFilename(string filename, const map<string, vect
return a; return a;
} }
// bool CheckIfInstructionIsFound(vector<pair<int, SAPFOR::Instruction*>> found, int block_num, SAPFOR::Instruction* instr){ /*checks if instruction is found for result*/
// for(pair<int, SAPFOR::Instruction*> p: found){ bool CheckIfInstructionIsFound(vector<pair<int, SAPFOR::Instruction*>> found, int block_num, SAPFOR::Instruction* instr){
// if(p.first == block_num && p.second == instr){ for(pair<int, SAPFOR::Instruction*> p: found){
// return true; if(p.first == block_num && p.second == instr){
// } return true;
// } }
// return }
// } return false;
}
map< pair<string, int>, set<string>> /*main pass function*/
map<FuncInfo*, vector<pair<int, SAPFOR::Instruction*>>>
findParameters(const map<string, vector<DefUseList>> &defUseByFunctions, findParameters(const map<string, vector<DefUseList>> &defUseByFunctions,
const map<string, CommonBlock*> &commonBlocks, const map<string, CommonBlock*> &commonBlocks,
const map<string, vector<FuncInfo*>> &allFuncInfo, const map<string, vector<FuncInfo*>> &allFuncInfo,
const map<FuncInfo*, vector<SAPFOR::BasicBlock*>> &fullIR, const map<FuncInfo*, vector<SAPFOR::BasicBlock*>> &fullIR,
const map<string, vector<LoopGraph*>>& loopGraph) const map<string, vector<LoopGraph*>>& loopGraph)
{ {
map<pair<string, int>, set<string>> 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"); printf("\n\n\n\nPRINT EXAMPLE\n\n\n\n\n");
cout << "DEF USE INFO" << endl; cout << "DEF USE INFO" << endl;
for(pair<string, vector<DefUseList>> p: defUseByFunctions) { for(pair<string, vector<DefUseList>> p: defUseByFunctions) {
@@ -183,57 +187,58 @@ map< pair<string, int>, set<string>>
*/ */
// Находим для каждой функции блоки, с которых начинаются циклы /*find blocks with loops start for each function*/
map<FuncInfo*, vector<SAPFOR::BasicBlock*>> mapFuncLoops; map<FuncInfo*, vector<SAPFOR::BasicBlock*>> mapFuncLoops;
for(const pair<FuncInfo*, vector<SAPFOR::BasicBlock*>>& p: fullIR) { for(const pair<FuncInfo*, vector<SAPFOR::BasicBlock*>>& partIR: fullIR) {
vector<LoopGraph*> loopsForFile = FindLoopsWithFilename(p.first->fileName, loopGraph); vector<LoopGraph*> loopsForFile = FindLoopsWithFilename(partIR.first->fileName, loopGraph);
SAPFOR::BasicBlock* head = nullptr; SAPFOR::BasicBlock* head = nullptr;
for(auto v: loopsForFile){ for(auto vec: loopsForFile){
head = nullptr; head = nullptr;
head = GetBasicBlocksForLoop(v, p.second); head = GetBasicBlocksForLoop(vec, partIR.second);
if(head != nullptr){ if(head != nullptr){
mapFuncLoops[p.first].push_back(head); mapFuncLoops[partIR.first].push_back(head);
} }
} }
} }
// На всякий случай выводим их /*print found blocks with loops for each function*/
cout << "ALL LOOPS" << endl; // cout << "ALL LOOPS" << endl;
for(auto m: mapFuncLoops){ // for(auto m: mapFuncLoops){
cout << "func: " << m.first->funcName << endl; // cout << "func: " << m.first->funcName << endl;
for(auto v: m.second){ // for(auto v: m.second){
cout << "block num: " << v->getNumber() << endl; // cout << "block num: " << v->getNumber() << endl;
cout << v->getInstructions().front()->getInstruction()->getOperator()->sunparse() << endl; // cout << v->getInstructions().front()->getInstruction()->getOperator()->sunparse() << endl;
} // }
} // }
map<FuncInfo*, vector<SAPFOR::Instruction*>> finalInstructions; /*find instructions with read operaton which affects on number iterations for loops*/
map<FuncInfo*, vector<pair<int, SAPFOR::Instruction*>>> finalInstructions;
/*for each function*/
for(const auto lookedFunc: mapFuncLoops){ for(const auto lookedFunc: mapFuncLoops){
for(const auto& v: lookedFunc.second){ /*for each loop in function*/
// cout << "NEW LOOP SEARCH" << endl; for(const auto& vectElem: lookedFunc.second){
SAPFOR::Instruction* instr = v->getInstructions().front()->getInstruction(); SAPFOR::Instruction* instr = vectElem->getInstructions().front()->getInstruction();
// cout << instr->getOperator()->sunparse() << endl;; vector<SAPFOR::BasicBlock*> blocks = vectElem->getPrev();
vector<SAPFOR::BasicBlock*> blocks = v->getPrev();
vector<SAPFOR::Argument*> argumentsForLook; vector<SAPFOR::Argument*> argumentsForLook;
argumentsForLook.push_back(instr->getArg2()); argumentsForLook.push_back(instr->getArg2());
int block_ind = 0; int blockInd = 0;
// for(auto pr: argumentsForLook){cout << "FIRST ARGUMENT TO FIND: " << pr->getValue() << endl;} /*run through blocks from first found to first in program taking previous blocks on each step*/
while(block_ind < blocks.size()){ while(blockInd < blocks.size()){
vector<SAPFOR::IR_Block*> blockInstructions = blocks[block_ind]->getInstructions(); if(argumentsForLook.empty()){
int vec_size = blockInstructions.size(); break;
// cout << "NUMBER OF INSTRUCTIONS IN BLOCK " << blocks[block_ind]->getNumber() << " IS: " << vec_size << endl; }
for(int i=vec_size-1; i>=0; i--){ vector<SAPFOR::IR_Block*> blockInstructions = blocks[blockInd]->getInstructions();
// cout << "ALL ARGUMENTS TO FIND: " << endl; int numberOfInstructions = blockInstructions.size();
// for(auto pr: argumentsForLook){cout << pr->getValue() << " ";} /*run through all instructions of block from bottom to up*/
// cout << endl; for(int i=numberOfInstructions-1; i>=0; i--){
SAPFOR::Instruction* cur = blockInstructions[i]->getInstruction(); SAPFOR::Instruction* cur = blockInstructions[i]->getInstruction();
// cout << "CUR INSTRUCTION: " << cur->dump() << endl; /*if instuction type is CALL*/
// cout << "Instruction Operation (type): " << SAPFOR::CFG_OP_S[(int)cur->getOperation()] << endl;
if(cur->getOperation() == SAPFOR::CFG_OP::F_CALL){ if(cur->getOperation() == SAPFOR::CFG_OP::F_CALL){
/*if it is CALL of READ function*/
if(cur->getArg1()->getValue() == "_READ"){ if(cur->getArg1()->getValue() == "_READ"){
cout << "READ OPERATION FOUND" << endl;
int numParams = std::stoi(cur->getArg2()->getValue()); int numParams = std::stoi(cur->getArg2()->getValue());
bool flag = false; bool flag = false;
/*drop all found arguments from argumentsForLook*/
for(int j=i-1; j>i-numParams-1; j--){ for(int j=i-1; j>i-numParams-1; j--){
auto iter = find(argumentsForLook.begin(), argumentsForLook.end(), blockInstructions[j]->getInstruction()->getArg1()); auto iter = find(argumentsForLook.begin(), argumentsForLook.end(), blockInstructions[j]->getInstruction()->getArg1());
if(iter != argumentsForLook.end()){ if(iter != argumentsForLook.end()){
@@ -241,17 +246,17 @@ map< pair<string, int>, set<string>>
flag = true; flag = true;
} }
} }
if(flag && find(finalInstructions[lookedFunc.first].begin(), finalInstructions[lookedFunc.first].end(), cur) == finalInstructions[lookedFunc.first].end()){ /*add read instruction to result vector if needed*/
finalInstructions[lookedFunc.first].push_back(cur); 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; i = i-numParams;
continue; 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()); auto it = find(argumentsForLook.begin(), argumentsForLook.end(), cur->getResult());
if(cur->getResult() && it != argumentsForLook.end()){ if(cur->getResult() && it != argumentsForLook.end()){
int dist = distance(argumentsForLook.begin(), it);
int numParams = std::stoi(cur->getArg2()->getValue()); int numParams = std::stoi(cur->getArg2()->getValue());
for(int j=i-1; j>i-numParams-1; j--){ for(int j=i-1; j>i-numParams-1; j--){
if(find(argumentsForLook.begin(), argumentsForLook.end(), blockInstructions[j]->getInstruction()->getArg1()) == argumentsForLook.end()){ if(find(argumentsForLook.begin(), argumentsForLook.end(), blockInstructions[j]->getInstruction()->getArg1()) == argumentsForLook.end()){
@@ -259,68 +264,66 @@ map< pair<string, int>, set<string>>
} }
} }
i = i-numParams; i = i-numParams;
argumentsForLook.erase(it); argumentsForLook.erase(argumentsForLook.begin()+dist);
continue; continue;
} }
} }
// cout << "NOT F_CALL" << endl; /*check only some operation types because other types are useless for this task*/
// cout << "CUR INSTRUCTION AGAIN: " << cur->dump() << endl;
if(instructionsToLook.find(cur->getOperation()) == instructionsToLook.end()){ if(instructionsToLook.find(cur->getOperation()) == instructionsToLook.end()){
// cout << "NOT ARITH" << endl;
continue; continue;
} }
// cout << "ARITH OR ACCESS" << endl; /*check if result of operation in argumentsForLook*/
auto it = find(argumentsForLook.begin(), argumentsForLook.end(), cur->getResult()); auto it = find(argumentsForLook.begin(), argumentsForLook.end(), cur->getResult());
// cout << "RESULT OF INSTRUCTION:" << *it->getValue() << endl; if(*it != NULL && it != argumentsForLook.end()){
if(it != argumentsForLook.end()){ int dist = distance(argumentsForLook.begin(), it);
if(find(argumentsForLook.begin(), argumentsForLook.end(), cur->getArg1()) == argumentsForLook.end()){ /*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()){ if(goodArgTypes.find(cur->getArg1()->getType()) != goodArgTypes.end()){
argumentsForLook.push_back(cur->getArg1()); argumentsForLook.push_back(cur->getArg1());
} }
} }
if(cur->getArg2() != NULL && find(argumentsForLook.begin(), argumentsForLook.end(), cur->getArg2()) == argumentsForLook.end()){ 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()){ if(goodArgTypes.find(cur->getArg2()->getType()) != goodArgTypes.end()){
argumentsForLook.push_back(cur->getArg2()); 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()){ if(find(blocks.begin(), blocks.end(), b) == blocks.end()){
blocks.push_back(b); blocks.push_back(b);
} }
} }
block_ind++; blockInd++;
// cout << "BLOCKS: ";
// for(auto bl: blocks){cout << bl->getNumber() << " ";}
// cout << endl;
// cout << "block_ind: " << block_ind << endl;
} }
// cout << "END WHILE" << endl; /*next step is next function that means next structures are not needed -> clear them*/
// В векторе argumentsForLook должны быть все аргументы инструкций, которые не получилось найти в блоках программы
// Возможно стоит еще их проверить в параметрах текущей функции, если они там есть
argumentsForLook.clear(); argumentsForLook.clear();
blocks.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*/
} }
} }
// Попытка напечатать полученные инструкции /*print found instructions for each function or program*/
cout << "\n\n\n\n\nFINAL INSTRUCTIONS" << endl; cout << "\n\nFINAL INSTRUCTIONS" << endl;
for(auto m: finalInstructions){ for(auto m: finalInstructions){
cout << "func: " << m.first->funcName << endl; cout << "func: " << m.first->funcName << endl;
for(auto v: m.second){ for(auto v: m.second){
cout << "instruction num: " << v->getNumber() << endl; cout << "\tFilename: " << v.second->getOperator()->fileName() << endl;
cout << v->dump() << endl; cout << "\tLine number: " << v.second->getLine() << endl;
cout << v->getOperator()->sunparse() << 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 finalInstructions;
return foundParameters;
} }

View File

@@ -21,8 +21,9 @@
#include "../CFGraph/CFGraph.h" #include "../CFGraph/CFGraph.h"
#include "../CFGraph/IR.h" #include "../CFGraph/IR.h"
std::map< std::pair<std::string, int>, std::set<std::string>> findParameters(const std::map<std::string, std::vector<DefUseList>> &defUseByFunctions, std::map<FuncInfo*, std::vector<std::pair<int, SAPFOR::Instruction*>>>
const std::map<std::string, CommonBlock*> &commonBlocks, findParameters(const std::map<std::string, std::vector<DefUseList>> &defUseByFunctions,
const std::map<std::string, std::vector<FuncInfo*>> &allFuncInfo, const std::map<std::string, CommonBlock*> &commonBlocks,
const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>> &fullIR, const std::map<std::string, std::vector<FuncInfo*>> &allFuncInfo,
const std::map<std::string, std::vector<LoopGraph*>>& loopGraph); const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>> &fullIR,
const std::map<std::string, std::vector<LoopGraph*>>& loopGraph);

View File

@@ -168,7 +168,7 @@ std::map<int, UserFiles> filesInfo; // information about open,close,write and re
// //
//for FIND_PARAMETERS //for FIND_PARAMETERS
std::map< std::pair<std::string, int>, std::set<std::string>> parametersOfProject; // [file, line] -> set[vars] std::map<FuncInfo*, std::vector<std::pair<int, SAPFOR::Instruction*>>> parametersOfProject; // FuncInfo -> vector[BlockNum, Instruction]
// //
//for GET_MIN_MAX_BLOCK_DIST //for GET_MIN_MAX_BLOCK_DIST