Skip to content

Commit

Permalink
Move predecessor/successor information to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeac committed Nov 18, 2019
1 parent e52c117 commit 2c25446
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 173 deletions.
10 changes: 8 additions & 2 deletions src/boomerang-plugins/codegen/c/CCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2754,6 +2754,10 @@ void CCodeGenerator::emitCodeForStmt(const SharedConstStmt &st)
std::list<std::pair<SharedExp, const BasicBlock *>>
CCodeGenerator::computeOptimalCaseOrdering(const BasicBlock *caseHead, const SwitchInfo *psi)
{
if (!caseHead) {
return {};
}

using CaseEntry = std::pair<SharedExp, const BasicBlock *>;
std::list<CaseEntry> result;

Expand All @@ -2770,12 +2774,14 @@ CCodeGenerator::computeOptimalCaseOrdering(const BasicBlock *caseHead, const Swi
}

const BasicBlock *realSucc = origSucc;
while (realSucc->getNumSuccessors() == 1 &&
while (realSucc && realSucc->getNumSuccessors() == 1 &&
(realSucc->isEmpty() || realSucc->isEmptyJump())) {
realSucc = realSucc->getSuccessor(0);
}

result.push_back({ caseVal, realSucc });
if (realSucc) {
result.push_back({ caseVal, realSucc });
}
}

result.sort([](const CaseEntry &left, const CaseEntry &right) {
Expand Down
2 changes: 1 addition & 1 deletion src/boomerang-plugins/decoder/csx86/CapstoneX86Decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ bool CapstoneX86Decoder::disassembleInstruction(Address pc, ptrdiff_t delta,
result.setGroup(MIGroup::Call, isInstructionInGroup(m_insn, cs::CS_GRP_CALL));
result.setGroup(MIGroup::BoolAsgn, result.m_templateName.startsWith("SET"));
result.setGroup(MIGroup::Ret, isInstructionInGroup(m_insn, cs::CS_GRP_RET) ||
isInstructionInGroup(m_insn, cs::CS_GRP_IRET));
isInstructionInGroup(m_insn, cs::CS_GRP_IRET));

if (result.isInGroup(MIGroup::Jump) || result.isInGroup(MIGroup::Call)) {
assert(result.getNumOperands() > 0);
Expand Down
112 changes: 7 additions & 105 deletions src/boomerang/db/BasicBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ BasicBlock::BasicBlock(BBType bbType, std::unique_ptr<RTLList> bbRTLs, Function


BasicBlock::BasicBlock(const BasicBlock &bb)
: m_function(bb.m_function)
: GraphNode(bb)
, m_function(bb.m_function)
, m_lowAddr(bb.m_lowAddr)
, m_highAddr(bb.m_highAddr)
, m_bbType(bb.m_bbType)
// m_labelNeeded is initialized to false, not copied
, m_predecessors(bb.m_predecessors)
, m_successors(bb.m_successors)
{
if (bb.m_listOfRTLs) {
// make a deep copy of the RTL list
Expand All @@ -73,13 +72,13 @@ BasicBlock::~BasicBlock()

BasicBlock &BasicBlock::operator=(const BasicBlock &bb)
{
GraphNode::operator=(bb);

m_function = bb.m_function;
m_lowAddr = bb.m_lowAddr;
m_highAddr = bb.m_highAddr;
m_bbType = bb.m_bbType;
// m_labelNeeded is initialized to false, not copied
m_predecessors = bb.m_predecessors;
m_successors = bb.m_successors;

if (bb.m_listOfRTLs) {
// make a deep copy of the RTL list
Expand Down Expand Up @@ -152,14 +151,14 @@ void BasicBlock::print(OStream &os) const
os << ":\n";
os << " in edges: ";

for (BasicBlock *bb : m_predecessors) {
for (BasicBlock *bb : getPredecessors()) {
os << bb->getHiAddr() << "(" << bb->getLowAddr() << ") ";
}

os << "\n";
os << " out edges: ";

for (BasicBlock *bb : m_successors) {
for (BasicBlock *bb : getSuccessors()) {
os << bb->getLowAddr() << " ";
}

Expand Down Expand Up @@ -196,6 +195,7 @@ const RTLList *BasicBlock::getRTLs() const
return m_listOfRTLs.get();
}


RTL *BasicBlock::getLastRTL()
{
return m_listOfRTLs ? m_listOfRTLs->back().get() : nullptr;
Expand All @@ -208,92 +208,6 @@ const RTL *BasicBlock::getLastRTL() const
}


const std::vector<BasicBlock *> &BasicBlock::getPredecessors() const
{
return m_predecessors;
}


const std::vector<BasicBlock *> &BasicBlock::getSuccessors() const
{
return m_successors;
}


void BasicBlock::setPredecessor(int i, BasicBlock *predecessor)
{
assert(Util::inRange(i, 0, getNumPredecessors()));
m_predecessors[i] = predecessor;
}


void BasicBlock::setSuccessor(int i, BasicBlock *successor)
{
assert(Util::inRange(i, 0, getNumSuccessors()));
m_successors[i] = successor;
}


BasicBlock *BasicBlock::getPredecessor(int i)
{
return Util::inRange(i, 0, getNumPredecessors()) ? m_predecessors[i] : nullptr;
}


const BasicBlock *BasicBlock::getPredecessor(int i) const
{
return Util::inRange(i, 0, getNumPredecessors()) ? m_predecessors[i] : nullptr;
}


BasicBlock *BasicBlock::getSuccessor(int i)
{
return Util::inRange(i, 0, getNumSuccessors()) ? m_successors[i] : nullptr;
}


const BasicBlock *BasicBlock::getSuccessor(int i) const
{
return Util::inRange(i, 0, getNumSuccessors()) ? m_successors[i] : nullptr;
}


void BasicBlock::addPredecessor(BasicBlock *predecessor)
{
m_predecessors.push_back(predecessor);
}


void BasicBlock::addSuccessor(BasicBlock *successor)
{
m_successors.push_back(successor);
}


void BasicBlock::removePredecessor(BasicBlock *pred)
{
// Only remove a single predecessor (prevents issues with double edges)
for (auto it = m_predecessors.begin(); it != m_predecessors.end(); ++it) {
if (*it == pred) {
m_predecessors.erase(it);
return;
}
}
}


void BasicBlock::removeSuccessor(BasicBlock *succ)
{
// Only remove a single successor (prevents issues with double edges)
for (auto it = m_successors.begin(); it != m_successors.end(); ++it) {
if (*it == succ) {
m_successors.erase(it);
return;
}
}
}


Function *BasicBlock::getCallDestProc() const
{
if (!isType(BBType::Call) || !m_listOfRTLs || m_listOfRTLs->empty()) {
Expand Down Expand Up @@ -603,18 +517,6 @@ void BasicBlock::simplify()
}


bool BasicBlock::isPredecessorOf(const BasicBlock *bb) const
{
return std::find(m_successors.begin(), m_successors.end(), bb) != m_successors.end();
}


bool BasicBlock::isSuccessorOf(const BasicBlock *bb) const
{
return std::find(m_predecessors.begin(), m_predecessors.end(), bb) != m_predecessors.end();
}


std::shared_ptr<ImplicitAssign> BasicBlock::addImplicitAssign(const SharedExp &lhs)
{
assert(m_listOfRTLs);
Expand Down
62 changes: 2 additions & 60 deletions src/boomerang/db/BasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once


#include "boomerang/db/GraphNode.h"
#include "boomerang/ssl/RTL.h"
#include "boomerang/util/Address.h"
#include "boomerang/util/StatementList.h"
Expand Down Expand Up @@ -60,7 +61,7 @@ enum class BBType
* During decompilation, a special RTL with a zero address is prepended;
* this RTL contains implicit assigns and phi assigns.
*/
class BOOMERANG_API BasicBlock
class BOOMERANG_API BasicBlock : public GraphNode<BasicBlock>
{
public:
typedef RTLList::iterator RTLIterator;
Expand Down Expand Up @@ -127,61 +128,6 @@ class BOOMERANG_API BasicBlock
/// \returns true if the instructions of this BB have not been decoded yet.
inline bool isIncomplete() const { return getHiAddr() == Address::INVALID; }

// predecessor / successor functions

inline int getNumPredecessors() const { return m_predecessors.size(); }
inline int getNumSuccessors() const { return m_successors.size(); }

/// \returns all predecessors of this BB.
const std::vector<BasicBlock *> &getPredecessors() const;

/// \returns all successors of this BB.
const std::vector<BasicBlock *> &getSuccessors() const;

/// \returns the \p i-th predecessor of this BB.
/// Returns nullptr if \p i is out of range.
BasicBlock *getPredecessor(int i);
const BasicBlock *getPredecessor(int i) const;

/// \returns the \p i-th successor of this BB.
/// Returns nullptr if \p i is out of range.
BasicBlock *getSuccessor(int i);
const BasicBlock *getSuccessor(int i) const;

/// Change the \p i-th predecessor of this BB.
/// \param i index (0-based)
void setPredecessor(int i, BasicBlock *predecessor);

/// Change the \p i-th successor of this BB.
/// \param i index (0-based)
void setSuccessor(int i, BasicBlock *successor);

/// Add a predecessor to this BB.
void addPredecessor(BasicBlock *predecessor);

/// Add a successor to this BB.
void addSuccessor(BasicBlock *successor);

/// Remove a predecessor BB.
void removePredecessor(BasicBlock *predecessor);

/// Remove a successor BB
void removeSuccessor(BasicBlock *successor);

/// Removes all successor BBs.
/// Called when noreturn call is found
void removeAllSuccessors() { m_successors.clear(); }

/// removes all predecessor BBs.
void removeAllPredecessors() { m_predecessors.clear(); }

/// \returns true if this BB is a (direct) predecessor of \p bb,
/// i.e. there is an edge from this BB to \p bb
bool isPredecessorOf(const BasicBlock *bb) const;

/// \returns true if this BB is a (direct) successor of \p bb,
/// i.e. there is an edge from \p bb to this BB.
bool isSuccessorOf(const BasicBlock *bb) const;

// RTL and statement related
public:
Expand Down Expand Up @@ -293,8 +239,4 @@ class BOOMERANG_API BasicBlock
Address m_highAddr = Address::INVALID;

BBType m_bbType = BBType::Invalid; ///< type of basic block

/* in-edges and out-edges */
std::vector<BasicBlock *> m_predecessors; ///< Vector of in-edges
std::vector<BasicBlock *> m_successors; ///< Vector of out-edges
};
1 change: 1 addition & 0 deletions src/boomerang/db/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ list(APPEND boomerang-db-sources
db/DebugInfo
db/DefCollector
db/Global
db/GraphNode
db/Prog
db/UseCollector

Expand Down
10 changes: 10 additions & 0 deletions src/boomerang/db/GraphNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma region License
/*
* This file is part of the Boomerang Decompiler.
*
* See the file "LICENSE.TERMS" for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL
* WARRANTIES.
*/
#pragma endregion License
#include "GraphNode.h"
Loading

0 comments on commit 2c25446

Please sign in to comment.