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;
}
/*returns FuncInfo vetcor by filename*/
vector<FuncInfo*> FindFuncInfoVec(string funcname, const map<string, vector<FuncInfo*>> &allFuncInfo){
for(pair<string, vector<FuncInfo*>> p: allFuncInfo) {
if(p.first == funcname){
@@ -79,6 +80,7 @@ vector<FuncInfo*> FindFuncInfoVec(string funcname, const map<string, vector<Func
return a;
}
/*returns BasicBlock vector for function by FuncInfo*/
vector<SAPFOR::BasicBlock*> FindBlocksVec(FuncInfo* func, const map<FuncInfo*, vector<SAPFOR::BasicBlock*>> &fullIR){
for(const pair<FuncInfo*, vector<SAPFOR::BasicBlock*>>& p: fullIR) {
if(p.first == func){
@@ -89,6 +91,7 @@ vector<SAPFOR::BasicBlock*> FindBlocksVec(FuncInfo* func, const map<FuncInfo*, v
return a;
}
/*returns LoopGraph vector by filename*/
vector<LoopGraph*> FindLoopsWithFilename(string filename, const map<string, vector<LoopGraph*>> loopGraph){
for(pair<string, vector<LoopGraph*>> p: loopGraph) {
if(p.first == filename){
@@ -99,25 +102,26 @@ vector<LoopGraph*> FindLoopsWithFilename(string filename, const map<string, vect
return a;
}
// bool CheckIfInstructionIsFound(vector<pair<int, SAPFOR::Instruction*>> found, int block_num, SAPFOR::Instruction* instr){
// for(pair<int, SAPFOR::Instruction*> p: found){
// if(p.first == block_num && p.second == instr){
// return true;
// }
// }
// return
// }
/*checks if instruction is found for result*/
bool CheckIfInstructionIsFound(vector<pair<int, SAPFOR::Instruction*>> found, int block_num, SAPFOR::Instruction* instr){
for(pair<int, SAPFOR::Instruction*> p: found){
if(p.first == block_num && p.second == instr){
return true;
}
}
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,
const map<string, CommonBlock*> &commonBlocks,
const map<string, vector<FuncInfo*>> &allFuncInfo,
const map<FuncInfo*, vector<SAPFOR::BasicBlock*>> &fullIR,
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");
cout << "DEF USE INFO" << endl;
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;
for(const pair<FuncInfo*, vector<SAPFOR::BasicBlock*>>& p: fullIR) {
vector<LoopGraph*> loopsForFile = FindLoopsWithFilename(p.first->fileName, loopGraph);
for(const pair<FuncInfo*, vector<SAPFOR::BasicBlock*>>& partIR: fullIR) {
vector<LoopGraph*> 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<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& v: lookedFunc.second){
// cout << "NEW LOOP SEARCH" << endl;
SAPFOR::Instruction* instr = v->getInstructions().front()->getInstruction();
// cout << instr->getOperator()->sunparse() << endl;;
vector<SAPFOR::BasicBlock*> blocks = v->getPrev();
/*for each loop in function*/
for(const auto& vectElem: lookedFunc.second){
SAPFOR::Instruction* instr = vectElem->getInstructions().front()->getInstruction();
vector<SAPFOR::BasicBlock*> blocks = vectElem->getPrev();
vector<SAPFOR::Argument*> 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<SAPFOR::IR_Block*> 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<SAPFOR::IR_Block*> 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<string, int>, set<string>>
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<string, int>, set<string>>
}
}
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;
}

View File

@@ -21,7 +21,8 @@
#include "../CFGraph/CFGraph.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*>>>
findParameters(const std::map<std::string, std::vector<DefUseList>> &defUseByFunctions,
const std::map<std::string, CommonBlock*> &commonBlocks,
const std::map<std::string, std::vector<FuncInfo*>> &allFuncInfo,
const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>> &fullIR,

View File

@@ -168,7 +168,7 @@ std::map<int, UserFiles> filesInfo; // information about open,close,write and re
//
//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