diff --git a/src/boomerang-plugins/codegen/c/CCodeGenerator.cpp b/src/boomerang-plugins/codegen/c/CCodeGenerator.cpp index 17503fbb7..763dab2f0 100644 --- a/src/boomerang-plugins/codegen/c/CCodeGenerator.cpp +++ b/src/boomerang-plugins/codegen/c/CCodeGenerator.cpp @@ -11,6 +11,7 @@ #include "boomerang/core/Project.h" #include "boomerang/core/Settings.h" +#include "boomerang/db/BasicBlock.h" #include "boomerang/db/Prog.h" #include "boomerang/db/module/Module.h" #include "boomerang/db/proc/UserProc.h" @@ -36,8 +37,6 @@ #include "boomerang/util/ByteUtil.h" #include "boomerang/util/log/Log.h" -#include "boomerang/db/BasicBlock.h" - CCodeGenerator::CCodeGenerator(Project *project) : ICodeGenerator(project) @@ -387,7 +386,7 @@ void CCodeGenerator::removeUnusedLabels() if (it->startsWith("lab_") && it->contains(':')) { QStringRef bbNumberStr = it->midRef(4, it->indexOf(':') - 4); bool ok = false; - int stmtNumber = bbNumberStr.toInt(&ok, 16); + int stmtNumber = bbNumberStr.toInt(&ok, 16); assert(ok); if (m_usedLabels.find(stmtNumber) == m_usedLabels.end()) { @@ -2186,8 +2185,9 @@ void CCodeGenerator::generateCode(const StmtASTNode *bb, const StmtASTNode *latc } -void CCodeGenerator::generateCode_Loop(const StmtASTNode *bb, std::list &gotoSet, - UserProc *proc, const StmtASTNode *latch, +void CCodeGenerator::generateCode_Loop(const StmtASTNode *bb, + std::list &gotoSet, UserProc *proc, + const StmtASTNode *latch, std::list &followSet) { // add the follow of the loop (if it exists) to the follow set @@ -2212,8 +2212,8 @@ void CCodeGenerator::generateCode_Loop(const StmtASTNode *bb, std::listgetSuccessor(BELSE) == m_analyzer.getLoopFollow(bb)) - ? bb->getSuccessor(BTHEN) - : bb->getSuccessor(BELSE); + ? bb->getSuccessor(BTHEN) + : bb->getSuccessor(BELSE); generateCode(loopBody, m_analyzer.getLatchNode(bb), followSet, gotoSet, proc); // if code has not been generated for the latch node, generate it now @@ -2341,9 +2341,10 @@ void CCodeGenerator::generateCode_Branch(const StmtASTNode *bb, else { if (m_analyzer.getUnstructType(bb) == UnstructType::JumpInOutLoop) { // define the loop header to be compared against - const StmtASTNode *myLoopHead = (m_analyzer.getStructType(bb) == StructType::LoopCond - ? bb - : m_analyzer.getLoopHead(bb)); + const StmtASTNode *myLoopHead = (m_analyzer.getStructType(bb) == + StructType::LoopCond + ? bb + : m_analyzer.getLoopHead(bb)); gotoSet.push_back(m_analyzer.getCondFollow(bb)); gotoTotal++; @@ -2378,7 +2379,7 @@ void CCodeGenerator::generateCode_Branch(const StmtASTNode *bb, if (m_analyzer.getCondType(bb) == CondType::Case) { const CaseStatement *cs = bb->getStatement(); - psi = cs->getSwitchInfo(); + psi = cs->getSwitchInfo(); // Write the switch header (i.e. "switch (var) {") addCaseCondHeader(psi->switchExp); @@ -2452,7 +2453,7 @@ void CCodeGenerator::generateCode_Branch(const StmtASTNode *bb, switchDests = computeOptimalCaseOrdering(bb, psi); for (auto it = switchDests.begin(); it != switchDests.end(); ++it) { - SharedExp caseValue = it->first; + SharedExp caseValue = it->first; const StmtASTNode *succ = it->second; addCaseCondOption(caseValue); @@ -2503,8 +2504,9 @@ void CCodeGenerator::generateCode_Branch(const StmtASTNode *bb, } -void CCodeGenerator::generateCode_Seq(const StmtASTNode *bb, std::list &gotoSet, - UserProc *proc, const StmtASTNode *latch, +void CCodeGenerator::generateCode_Seq(const StmtASTNode *bb, + std::list &gotoSet, UserProc *proc, + const StmtASTNode *latch, std::list &followSet) { // generate code for the body of this block @@ -2520,9 +2522,10 @@ void CCodeGenerator::generateCode_Seq(const StmtASTNode *bb, std::listgetNumSuccessors() == 0) { -// LOG_WARN("No out edge for BB at address %1, in proc %2", bb->getLowAddr(), proc->getName()); + // LOG_WARN("No out edge for BB at address %1, in proc %2", bb->getLowAddr(), + // proc->getName()); - if (bb->getStatement()->isGoto() ) { + if (bb->getStatement()->isGoto()) { const GotoStatement *gs = bb->getStatement(); if (gs && gs->getDest()) { addLineComment("goto " + gs->getDest()->toString()); @@ -2540,30 +2543,32 @@ void CCodeGenerator::generateCode_Seq(const StmtASTNode *bb, std::listgetNumSuccessors() > 1) { const StmtASTNode *other = bb->getSuccessor(1); LOG_MSG("Found seq with more than one outedge!"); - std::shared_ptr constDest = bb->getStatement()->getDest()->access(); + std::shared_ptr + constDest = bb->getStatement()->getDest()->access(); -// if (constDest && constDest->isIntConst() && (constDest->getAddr() == succ->getLowAddr())) { -// std::swap(other, succ); -// LOG_MSG("Taken branch is first out edge"); -// } + // if (constDest && constDest->isIntConst() && (constDest->getAddr() == + // succ->getLowAddr())) { + // std::swap(other, succ); + // LOG_MSG("Taken branch is first out edge"); + // } -// SharedExp cond = bb->getCond(); + // SharedExp cond = bb->getCond(); -// if (cond) { -// addIfCondHeader(bb->getCond()); + // if (cond) { + // addIfCondHeader(bb->getCond()); - if (isGenerated(other)) { - emitGotoAndLabel(bb, other); - } - else { - generateCode(other, latch, followSet, gotoSet, proc); - } + if (isGenerated(other)) { + emitGotoAndLabel(bb, other); + } + else { + generateCode(other, latch, followSet, gotoSet, proc); + } - addIfCondEnd(); -// } -// else { - LOG_ERROR("Last statement is not a cond, don't know what to do with this."); -// } + addIfCondEnd(); + // } + // else { + LOG_ERROR("Last statement is not a cond, don't know what to do with this."); + // } } // Generate code for its successor if diff --git a/src/boomerang-plugins/codegen/c/CCodeGenerator.h b/src/boomerang-plugins/codegen/c/CCodeGenerator.h index 6c21d4c3f..fe6d3d588 100644 --- a/src/boomerang-plugins/codegen/c/CCodeGenerator.h +++ b/src/boomerang-plugins/codegen/c/CCodeGenerator.h @@ -328,9 +328,9 @@ class BOOMERANG_PLUGIN_API CCodeGenerator : public ICodeGenerator void appendLine(const QString &s); private: - int m_indent = 0; ///< Current indentation depth - std::map m_locals; ///< All locals in a Proc - std::unordered_set m_usedLabels; ///< All used goto labels. (lowAddr of BB) + int m_indent = 0; ///< Current indentation depth + std::map m_locals; ///< All locals in a Proc + std::unordered_set m_usedLabels; ///< All used goto labels. (lowAddr of BB) std::unordered_set m_generatedBBs; UserProc *m_proc = nullptr; diff --git a/src/boomerang-plugins/codegen/c/ControlFlowAnalyzer.cpp b/src/boomerang-plugins/codegen/c/ControlFlowAnalyzer.cpp index 3ab780f64..234eeda9d 100644 --- a/src/boomerang-plugins/codegen/c/ControlFlowAnalyzer.cpp +++ b/src/boomerang-plugins/codegen/c/ControlFlowAnalyzer.cpp @@ -11,14 +11,13 @@ #include "boomerang/db/BasicBlock.h" #include "boomerang/db/proc/ProcCFG.h" -#include "boomerang/util/log/Log.h" +#include "boomerang/db/proc/UserProc.h" #include "boomerang/ssl/RTL.h" - +#include "boomerang/ssl/statements/BranchStatement.h" #include "boomerang/ssl/statements/CallStatement.h" #include "boomerang/ssl/statements/CaseStatement.h" -#include "boomerang/ssl/statements/BranchStatement.h" #include "boomerang/ssl/statements/ReturnStatement.h" -#include "boomerang/db/proc/UserProc.h" +#include "boomerang/util/log/Log.h" #include @@ -119,7 +118,7 @@ void ControlFlowAnalyzer::updateImmedPDom() const StmtASTNode *ControlFlowAnalyzer::findCommonPDom(const StmtASTNode *currImmPDom, - const StmtASTNode *succImmPDom) + const StmtASTNode *succImmPDom) { if (!currImmPDom) { return succImmPDom; @@ -133,7 +132,7 @@ const StmtASTNode *ControlFlowAnalyzer::findCommonPDom(const StmtASTNode *currIm return currImmPDom; // ordering hasn't been done } - const StmtASTNode *oldCurImmPDom = currImmPDom; + const StmtASTNode *oldCurImmPDom = currImmPDom; int giveup = 0; #define GIVEUP 10000 @@ -228,7 +227,7 @@ void ControlFlowAnalyzer::findLoopFollow(const StmtASTNode *header, bool *&loopN { assert(getStructType(header) == StructType::Loop || getStructType(header) == StructType::LoopCond); - const LoopType loopType = getLoopType(header); + const LoopType loopType = getLoopType(header); const StmtASTNode *latch = getLatchNode(header); if (loopType == LoopType::PreTested) { @@ -413,8 +412,8 @@ void ControlFlowAnalyzer::checkConds() getCondFollow(currNode) && (getCondType(currNode) != CondType::Case)) { // define convenient aliases for the relevant loop and case heads and the out edges const StmtASTNode *myLoopHead = (getStructType(currNode) == StructType::LoopCond) - ? currNode - : getLoopHead(currNode); + ? currNode + : getLoopHead(currNode); const StmtASTNode *follLoopHead = getLoopHead(getCondFollow(currNode)); const StmtASTNode *thenNode = currNode->getSuccessor(BTHEN); const StmtASTNode *elseNode = currNode->getSuccessor(BELSE); @@ -759,7 +758,7 @@ StmtASTNode *ControlFlowAnalyzer::findEntryNode() const } return nullptr; // not found - } +} StmtASTNode *ControlFlowAnalyzer::findExitNode() const @@ -787,8 +786,8 @@ void ControlFlowAnalyzer::rebuildASTForest() if (prev != nullptr) { m_nodes[stmt]->addPredecessor(m_nodes[prev]); m_nodes[prev]->addSuccessor(m_nodes[stmt]); - } - prev = stmt; + } + prev = stmt; } } } @@ -828,7 +827,7 @@ void ControlFlowAnalyzer::dumpStmtCFGToFile() const if (stmt->isCall()) { const Function *proc = node->getStatement()->getDestProc(); - label = QString("CALL ") + (proc ? proc->getName() : "/* no dest */"); + label = QString("CALL ") + (proc ? proc->getName() : "/* no dest */"); label += "("; for (auto &arg : node->getStatement()->getArguments()) { label += static_cast(arg)->getRight()->toString() + ","; @@ -842,11 +841,14 @@ void ControlFlowAnalyzer::dumpStmtCFGToFile() const else if (stmt->isCase()) { label = QString("CASE "); if (node->getStatement()->getSwitchInfo()) { - label += node->getStatement()->getSwitchInfo()->switchExp->toString(); + label += node->getStatement() + ->getSwitchInfo() + ->switchExp->toString(); } } else if (stmt->isBranch()) { - label = "BRANCH if " + static_cast(stmt)->getCondExpr()->toString(); + label = "BRANCH if " + + static_cast(stmt)->getCondExpr()->toString(); } else if (stmt->isReturn()) { label = QString("RET "); @@ -873,13 +875,16 @@ void ControlFlowAnalyzer::dumpStmtCFGToFile() const for (auto &[stmt, node] : m_nodes) { if (stmt->isBranch()) { - ost << "stmt" << stmt->getNumber() << " -> stmt" << node->getSuccessor(BTHEN)->getStatement()->getNumber() << "[color=green];\n"; - ost << "stmt" << stmt->getNumber() << " -> stmt" << node->getSuccessor(BELSE)->getStatement()->getNumber() << "[color=red];\n"; + ost << "stmt" << stmt->getNumber() << " -> stmt" + << node->getSuccessor(BTHEN)->getStatement()->getNumber() << "[color=green];\n"; + ost << "stmt" << stmt->getNumber() << " -> stmt" + << node->getSuccessor(BELSE)->getStatement()->getNumber() << "[color=red];\n"; } else if (stmt->isCase()) { for (int i = 0; i < node->getNumSuccessors(); ++i) { StmtASTNode *succ = node->getSuccessor(i); - ost << "stmt" << stmt->getNumber() << " -> stmt" << succ->getStatement()->getNumber() << "[label=\""; + ost << "stmt" << stmt->getNumber() << " -> stmt" + << succ->getStatement()->getNumber() << "[label=\""; const SwitchInfo *psi = node->getStatement()->getSwitchInfo(); if (psi->switchType == SwitchType::F) { // "Fortran" style? @@ -895,7 +900,8 @@ void ControlFlowAnalyzer::dumpStmtCFGToFile() const } else { for (auto &succ : node->getSuccessors()) { - ost << "stmt" << stmt->getNumber() << " -> stmt" << succ->getStatement()->getNumber() << ";\n"; + ost << "stmt" << stmt->getNumber() << " -> stmt" + << succ->getStatement()->getNumber() << ";\n"; } } } @@ -905,7 +911,8 @@ void ControlFlowAnalyzer::dumpStmtCFGToFile() const } -const Statement *ControlFlowAnalyzer::findSuccessorStmt(const Statement *stmt, const BasicBlock *successorBB) const +const Statement *ControlFlowAnalyzer::findSuccessorStmt(const Statement *stmt, + const BasicBlock *successorBB) const { if (stmt == nullptr) { return nullptr; diff --git a/src/boomerang-plugins/codegen/c/ControlFlowAnalyzer.h b/src/boomerang-plugins/codegen/c/ControlFlowAnalyzer.h index 1c82fd2e3..d983f61a5 100644 --- a/src/boomerang-plugins/codegen/c/ControlFlowAnalyzer.h +++ b/src/boomerang-plugins/codegen/c/ControlFlowAnalyzer.h @@ -182,7 +182,10 @@ class ControlFlowAnalyzer } TravType getTravType(const StmtASTNode *node) const { return m_info[node].m_travType; } - StructType getStructType(const StmtASTNode *node) const { return m_info[node].m_structuringType; } + StructType getStructType(const StmtASTNode *node) const + { + return m_info[node].m_structuringType; + } CondType getCondType(const StmtASTNode *node) const; UnstructType getUnstructType(const StmtASTNode *node) const; LoopType getLoopType(const StmtASTNode *node) const; @@ -197,7 +200,10 @@ class ControlFlowAnalyzer void updateRevLoopStamps(const StmtASTNode *node, int &time); void updateRevOrder(const StmtASTNode *node); - void setLoopHead(const StmtASTNode *node, const StmtASTNode *head) { m_info[node].m_loopHead = head; } + void setLoopHead(const StmtASTNode *node, const StmtASTNode *head) + { + m_info[node].m_loopHead = head; + } void setLatchNode(const StmtASTNode *node, const StmtASTNode *latch) { m_info[node].m_latchNode = latch; @@ -224,7 +230,8 @@ class ControlFlowAnalyzer /// \returns true if \p bb is an ancestor of \p other bool isAncestorOf(const StmtASTNode *node, const StmtASTNode *other) const; - bool isNodeInLoop(const StmtASTNode *node, const StmtASTNode *header, const StmtASTNode *latch) const; + bool isNodeInLoop(const StmtASTNode *node, const StmtASTNode *header, + const StmtASTNode *latch) const; int getPostOrdering(const StmtASTNode *node) const { return m_info[node].m_postOrderIndex; } int getRevOrd(const StmtASTNode *node) const { return m_info[node].m_revPostOrderIndex; } @@ -266,7 +273,8 @@ class ControlFlowAnalyzer /// Finds the common post dominator of the current immediate post dominator and its successor's /// immediate post dominator - const StmtASTNode *findCommonPDom(const StmtASTNode *curImmPDom, const StmtASTNode *succImmPDom); + const StmtASTNode *findCommonPDom(const StmtASTNode *curImmPDom, + const StmtASTNode *succImmPDom); /// \pre The loop induced by (head,latch) has already had all its member nodes tagged /// \post The type of loop has been deduced diff --git a/src/boomerang-plugins/codegen/c/ast/ASTNode.cpp b/src/boomerang-plugins/codegen/c/ast/ASTNode.cpp index 76f0c0990..9879ec70a 100644 --- a/src/boomerang-plugins/codegen/c/ast/ASTNode.cpp +++ b/src/boomerang-plugins/codegen/c/ast/ASTNode.cpp @@ -18,4 +18,3 @@ ASTNode::ASTNode() ASTNode::~ASTNode() { } - diff --git a/src/boomerang-plugins/codegen/c/ast/StmtASTNode.h b/src/boomerang-plugins/codegen/c/ast/StmtASTNode.h index 866c0f8f0..633e95cc8 100644 --- a/src/boomerang-plugins/codegen/c/ast/StmtASTNode.h +++ b/src/boomerang-plugins/codegen/c/ast/StmtASTNode.h @@ -30,8 +30,11 @@ class StmtASTNode : public ASTNode /// \copydoc ASTNode::printAST void printAST(OStream &os) const override; - template - const T *getStatement() const { return static_cast(m_stmt); } + template + const T *getStatement() const + { + return static_cast(m_stmt); + } void addPredecessor(StmtASTNode *pred); void addSuccessor(StmtASTNode *succ);