Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeac committed Aug 5, 2019
1 parent 0d3a803 commit cedb670
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 65 deletions.
75 changes: 40 additions & 35 deletions src/boomerang-plugins/codegen/c/CCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -2186,8 +2185,9 @@ void CCodeGenerator::generateCode(const StmtASTNode *bb, const StmtASTNode *latc
}


void CCodeGenerator::generateCode_Loop(const StmtASTNode *bb, std::list<const StmtASTNode *> &gotoSet,
UserProc *proc, const StmtASTNode *latch,
void CCodeGenerator::generateCode_Loop(const StmtASTNode *bb,
std::list<const StmtASTNode *> &gotoSet, UserProc *proc,
const StmtASTNode *latch,
std::list<const StmtASTNode *> &followSet)
{
// add the follow of the loop (if it exists) to the follow set
Expand All @@ -2212,8 +2212,8 @@ void CCodeGenerator::generateCode_Loop(const StmtASTNode *bb, std::list<const St

// write the code for the body of the loop
const StmtASTNode *loopBody = (bb->getSuccessor(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
Expand Down Expand Up @@ -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++;

Expand Down Expand Up @@ -2378,7 +2379,7 @@ void CCodeGenerator::generateCode_Branch(const StmtASTNode *bb,

if (m_analyzer.getCondType(bb) == CondType::Case) {
const CaseStatement *cs = bb->getStatement<CaseStatement>();
psi = cs->getSwitchInfo();
psi = cs->getSwitchInfo();

// Write the switch header (i.e. "switch (var) {")
addCaseCondHeader(psi->switchExp);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -2503,8 +2504,9 @@ void CCodeGenerator::generateCode_Branch(const StmtASTNode *bb,
}


void CCodeGenerator::generateCode_Seq(const StmtASTNode *bb, std::list<const StmtASTNode *> &gotoSet,
UserProc *proc, const StmtASTNode *latch,
void CCodeGenerator::generateCode_Seq(const StmtASTNode *bb,
std::list<const StmtASTNode *> &gotoSet, UserProc *proc,
const StmtASTNode *latch,
std::list<const StmtASTNode *> &followSet)
{
// generate code for the body of this block
Expand All @@ -2520,9 +2522,10 @@ void CCodeGenerator::generateCode_Seq(const StmtASTNode *bb, std::list<const Stm

// return if this doesn't have any out edges (emit a warning)
if (bb->getNumSuccessors() == 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<GotoStatement>();
if (gs && gs->getDest()) {
addLineComment("goto " + gs->getDest()->toString());
Expand All @@ -2540,30 +2543,32 @@ void CCodeGenerator::generateCode_Seq(const StmtASTNode *bb, std::list<const Stm
if (bb->getNumSuccessors() > 1) {
const StmtASTNode *other = bb->getSuccessor(1);
LOG_MSG("Found seq with more than one outedge!");
std::shared_ptr<Const> constDest = bb->getStatement<GotoStatement>()->getDest()->access<Const>();
std::shared_ptr<Const>
constDest = bb->getStatement<GotoStatement>()->getDest()->access<Const>();

// 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
Expand Down
6 changes: 3 additions & 3 deletions src/boomerang-plugins/codegen/c/CCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<QString, SharedType> m_locals; ///< All locals in a Proc
std::unordered_set<int> m_usedLabels; ///< All used goto labels. (lowAddr of BB)
int m_indent = 0; ///< Current indentation depth
std::map<QString, SharedType> m_locals; ///< All locals in a Proc
std::unordered_set<int> m_usedLabels; ///< All used goto labels. (lowAddr of BB)
std::unordered_set<const StmtASTNode *> m_generatedBBs;

UserProc *m_proc = nullptr;
Expand Down
47 changes: 27 additions & 20 deletions src/boomerang-plugins/codegen/c/ControlFlowAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <QFile>

Expand Down Expand Up @@ -119,7 +118,7 @@ void ControlFlowAnalyzer::updateImmedPDom()


const StmtASTNode *ControlFlowAnalyzer::findCommonPDom(const StmtASTNode *currImmPDom,
const StmtASTNode *succImmPDom)
const StmtASTNode *succImmPDom)
{
if (!currImmPDom) {
return succImmPDom;
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -759,7 +758,7 @@ StmtASTNode *ControlFlowAnalyzer::findEntryNode() const
}

return nullptr; // not found
}
}


StmtASTNode *ControlFlowAnalyzer::findExitNode() const
Expand Down Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -828,7 +827,7 @@ void ControlFlowAnalyzer::dumpStmtCFGToFile() const

if (stmt->isCall()) {
const Function *proc = node->getStatement<CallStatement>()->getDestProc();
label = QString("CALL ") + (proc ? proc->getName() : "/* no dest */");
label = QString("CALL ") + (proc ? proc->getName() : "/* no dest */");
label += "(";
for (auto &arg : node->getStatement<CallStatement>()->getArguments()) {
label += static_cast<Assign *>(arg)->getRight()->toString() + ",";
Expand All @@ -842,11 +841,14 @@ void ControlFlowAnalyzer::dumpStmtCFGToFile() const
else if (stmt->isCase()) {
label = QString("CASE ");
if (node->getStatement<CaseStatement>()->getSwitchInfo()) {
label += node->getStatement<CaseStatement>()->getSwitchInfo()->switchExp->toString();
label += node->getStatement<CaseStatement>()
->getSwitchInfo()
->switchExp->toString();
}
}
else if (stmt->isBranch()) {
label = "BRANCH if " + static_cast<const BranchStatement *>(stmt)->getCondExpr()->toString();
label = "BRANCH if " +
static_cast<const BranchStatement *>(stmt)->getCondExpr()->toString();
}
else if (stmt->isReturn()) {
label = QString("RET ");
Expand All @@ -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<CaseStatement>()->getSwitchInfo();
if (psi->switchType == SwitchType::F) { // "Fortran" style?
Expand All @@ -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";
}
}
}
Expand All @@ -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;
Expand Down
16 changes: 12 additions & 4 deletions src/boomerang-plugins/codegen/c/ControlFlowAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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; }
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/boomerang-plugins/codegen/c/ast/ASTNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ ASTNode::ASTNode()
ASTNode::~ASTNode()
{
}

7 changes: 5 additions & 2 deletions src/boomerang-plugins/codegen/c/ast/StmtASTNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ class StmtASTNode : public ASTNode
/// \copydoc ASTNode::printAST
void printAST(OStream &os) const override;

template<typename T=Statement>
const T *getStatement() const { return static_cast<const T *>(m_stmt); }
template<typename T = Statement>
const T *getStatement() const
{
return static_cast<const T *>(m_stmt);
}

void addPredecessor(StmtASTNode *pred);
void addSuccessor(StmtASTNode *succ);
Expand Down

0 comments on commit cedb670

Please sign in to comment.