From 06dd8848befb99e121717d2d1d8a873d10248e0c Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 30 May 2025 17:06:21 +0300 Subject: [PATCH] improved CFG functions style --- src/CFGraph/CFGraph.cpp | 21 +++++++++++---------- src/CFGraph/CFGraph.h | 5 ++--- src/CFGraph/IRSSAForm.cpp | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/CFGraph/CFGraph.cpp b/src/CFGraph/CFGraph.cpp index 81ac7c1..ab6e929 100644 --- a/src/CFGraph/CFGraph.cpp +++ b/src/CFGraph/CFGraph.cpp @@ -50,27 +50,28 @@ BBlock::BasicBlock(const BBlock& copyFrom) prev = copyFrom.prev; } -void BBlock::addInstruction(IR_Block* item) +void BBlock::addInstruction(IR_Block* item, bool pushFront) { - instructions.push_back(item); + if (pushFront) + instructions.insert(instructions.begin(), item); + else + instructions.push_back(item); item->setBasicBlock(this); } -void BBlock::addInstructionInFront(IR_Block* item) +void BBlock::addInstructionBefore(IR_Block* item, Instruction* before) { - instructions.insert(instructions.begin(), item); - item->setBasicBlock(this); -} + checkNull(before, convertFileName(__FILE__).c_str(), __LINE__); + checkNull(item, convertFileName(__FILE__).c_str(), __LINE__); -void BBlock::addInstructionBeforeInstruction(IR_Block* item, Instruction* instruction) -{ for (auto it = instructions.begin(); it != instructions.end(); ++it) { - if ((*it)->getInstruction() == instruction) { + if ((*it)->getInstruction() == before) { instructions.insert(it, item); item->setBasicBlock(this); return; } } + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); } int BBlock::removePrev(BBlock* removed) @@ -529,7 +530,7 @@ static int buildReachingDefs(const vector& CFG, const FuncInfo* currF, return iter; } -//Kosaraju�Sharir algorithm +//Kosaraju-Sharir algorithm static vector getStronglyConnectedComps(vector>& g) { // 1. For each vertex u of the graph, mark u as unvisited. Let l be empty. auto size = g.size(); diff --git a/src/CFGraph/CFGraph.h b/src/CFGraph/CFGraph.h index c231f3d..31ceb2b 100644 --- a/src/CFGraph/CFGraph.h +++ b/src/CFGraph/CFGraph.h @@ -39,9 +39,8 @@ 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 addInstructionBefore(IR_Block* item, Instruction* istruction); + void addInstruction(IR_Block* item, bool pushFront = false); void addPrev(BasicBlock* prev_) { prev.push_back(prev_); } void addNext(BasicBlock* next_) { next.push_back(next_); } diff --git a/src/CFGraph/IRSSAForm.cpp b/src/CFGraph/IRSSAForm.cpp index cfa7371..fbbed60 100644 --- a/src/CFGraph/IRSSAForm.cpp +++ b/src/CFGraph/IRSSAForm.cpp @@ -354,7 +354,7 @@ static void getBlocksWithFiFunctions(vector blocks, set glo Instruction* phiInstruction = new Instruction(CFG_OP::F_CALL, new BArgument(*fiFunc), new BArgument(*paramCount), var, dfBlock->getInstructions()[0]->getInstruction()->getOperator()); IR_Block* phiBlock = new IR_Block(phiInstruction); - dfBlock->addInstructionInFront(phiBlock); + dfBlock->addInstruction(phiBlock, true); //blocksWithFiFunctions.push_back(dfBlock); } @@ -477,7 +477,7 @@ void RenameFiFunctionArgsVar(BBlock* block, map>& stac } paramInstruction->setOperator(block->getInstructions()[0]->getInstruction()->getOperator()); - block->addInstructionBeforeInstruction(new IR_Block(paramInstruction), instruction); + block->addInstructionBefore(new IR_Block(paramInstruction), instruction); instruction->getArg2()->setValue(to_string(stoi(instruction->getArg2()->getValue()) + 1)); i++;