From 7df02737c714701ee2a88b43247032d9edb8cc9b Mon Sep 17 00:00:00 2001 From: Dudarenko Date: Thu, 26 Dec 2024 01:58:15 +0300 Subject: [PATCH] WIP: finishing SSA renaming --- .../Sapfor_2017/_src/CFGraph/CFGraph.cpp | 11 ++++ .../Sapfor_2017/_src/CFGraph/CFGraph.h | 1 + .../Sapfor_2017/_src/CFGraph/IRSSAForm.cpp | 59 +++++++++++++++---- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.cpp index 02455d5..3040d80 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.cpp @@ -62,6 +62,17 @@ void BBlock::addInstructionInFront(IR_Block* item) item->setBasicBlock(this); } +void BBlock::addInstructionBeforeInstruction(IR_Block* item, Instruction* instruction) +{ + for (auto it = instructions.begin(); it != instructions.end(); ++it) { + if ((*it)->getInstruction() == instruction) { + instructions.insert(instructions.begin(), item); + item->setBasicBlock(this); + return; + } + } +} + int BBlock::removePrev(BBlock* removed) { auto it = std::remove(prev.begin(), prev.end(), removed); diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h index 3ea24d3..c231f3d 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h @@ -39,6 +39,7 @@ namespace SAPFOR BasicBlock(IR_Block* item); BasicBlock(const BasicBlock& copyFrom); + void addInstructionBeforeInstruction(IR_Block* item, Instruction* istruction); void addInstructionInFront(IR_Block* item); void addInstruction(IR_Block* item); void addPrev(BasicBlock* prev_) { prev.push_back(prev_); } diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/IRSSAForm.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/IRSSAForm.cpp index 3832a31..ea82f27 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/IRSSAForm.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/IRSSAForm.cpp @@ -34,16 +34,42 @@ static void printBlock(BBlock* block) { for (auto i : block->getInstructions()) { - cout << i->getNumber() << " " << i->getInstruction()->dump() << endl; - - if (i->getInstruction()->getResult() != nullptr) { - cout << "num - " << i->getInstruction()->getResult()->getNumber() << endl; + string resValue = ""; + string arg1Value = ""; + string arg2Value = ""; + if (i->getInstruction()->getResult() != nullptr && i->getInstruction()->getResult()->getType() == CFG_ARG_TYPE::VAR) { + resValue = i->getInstruction()->getResult()->getValue(); + i->getInstruction()->getResult()->setValue(i->getInstruction()->getResult()->getValue() + to_string(i->getInstruction()->getResult()->getNumber())); + } + if (i->getInstruction()->getArg1() != nullptr && i->getInstruction()->getArg1()->getType() == CFG_ARG_TYPE::VAR) { + arg1Value = i->getInstruction()->getArg1()->getValue(); + i->getInstruction()->getArg1()->setValue(i->getInstruction()->getArg1()->getValue() + to_string(i->getInstruction()->getArg1()->getNumber())); } + if (i->getInstruction()->getArg2() != nullptr && i->getInstruction()->getArg2()->getType() == CFG_ARG_TYPE::VAR) { + arg2Value = i->getInstruction()->getArg2()->getValue(); + i->getInstruction()->getArg2()->setValue(i->getInstruction()->getArg2()->getValue() + to_string(i->getInstruction()->getArg2()->getNumber())); + } + + cout << i->getNumber() << " " << i->getInstruction()->dump() << (i->getInstruction()->getResult() != nullptr ? " 1 " : " 0 ") << (i->getInstruction()->getArg1() != nullptr ? "1 " : "0 ") << (i->getInstruction()->getArg2() != nullptr ? "1 " : "0 ") << endl; + + /*if (i->getInstruction()->getResult() != nullptr) { + cout << "num - " << i->getInstruction()->getResult()->getNumber() << endl; + })*/ /*if (i->getInstruction()->getOperation() == CFG_OP::F_CALL) { cout << endl; cout << "arg1 - " << (i->getInstruction()->getArg1()->getType() == CFG_ARG_TYPE::FUNC ? "1" : "0") << "arg2 - " << (i->getInstruction()->getArg2()->getType() == CFG_ARG_TYPE::CONST ? "1" : "0") << "res - " << i->getInstruction()->getResult() << endl; }*/ + + if (i->getInstruction()->getResult() != nullptr && i->getInstruction()->getResult()->getType() == CFG_ARG_TYPE::VAR) { + i->getInstruction()->getResult()->setValue(resValue); + } + if (i->getInstruction()->getArg1() != nullptr && i->getInstruction()->getArg1()->getType() == CFG_ARG_TYPE::VAR) { + i->getInstruction()->getArg1()->setValue(arg1Value); + } + if (i->getInstruction()->getArg2() != nullptr && i->getInstruction()->getArg2()->getType() == CFG_ARG_TYPE::VAR) { + i->getInstruction()->getArg2()->setValue(arg2Value); + } } cout << endl; @@ -290,7 +316,7 @@ static vector getBlocksWithFiFunctions(vector blocks, set& counter, map>& stack) { + auto size = block->getInstructions().size(); + for (auto i = 0; i < size; i++) { + auto irBlock = block->getInstructions()[i]; + auto instruction = irBlock->getInstruction(); + if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != nullptr && instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != nullptr) { + + auto paramInstruction = new Instruction(CFG_OP::PARAM, stack[instruction->getResult()->getValue()].top()); + block->addInstructionBeforeInstruction(new IR_Block(paramInstruction), instruction); + + i++; + } + } +} + void RenameIR(BBlock* block, map>& dominatorTree, map& counter, map>& stack) { @@ -402,14 +443,13 @@ void RenameIR(BBlock* block, map>& dominatorTree, mapgetNext()) { - + RenameFiFunctionArgsVar(successor, stack); } for (auto* child : dominatorTree.at(block)) { RenameIR(child, dominatorTree, counter, stack); } - // Очистка стека после обработки инструкций for (auto& irBlock : block->getInstructions()) { auto instruction = irBlock->getInstruction(); if (instruction->getResult() != nullptr && instruction->getResult()->getType() == CFG_ARG_TYPE::VAR) { @@ -418,7 +458,6 @@ void RenameIR(BBlock* block, map>& dominatorTree, mapgetInstructions()) { auto instruction = irBlock->getInstruction(); if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != nullptr && instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != nullptr) { @@ -428,10 +467,6 @@ void RenameIR(BBlock* block, map>& dominatorTree, map> buildIRSSAForm(map> fullIR) { map> result;