Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Control Flow Analysis #237

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/boomerang-plugins/codegen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ include_directories(
BOOMERANG_ADD_CODEGEN(
NAME "C"
SOURCES
c/ast/ASTNode.cpp
c/ast/ASTNode.h
c/ast/StmtASTNode.cpp
c/ast/StmtASTNode.h

c/CCodeGenerator.cpp
c/CCodeGenerator.h
c/CodeWriter.cpp
Expand Down
198 changes: 94 additions & 104 deletions src/boomerang-plugins/codegen/c/CCodeGenerator.cpp

Large diffs are not rendered by default.

49 changes: 24 additions & 25 deletions src/boomerang-plugins/codegen/c/CCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <unordered_set>


class BasicBlock;
class Exp;
class LocationSet;
class BinaryImage;
Expand Down Expand Up @@ -210,7 +209,7 @@ class BOOMERANG_PLUGIN_API CCodeGenerator : public ICodeGenerator
void addIfElseCondEnd();

// goto, break, continue, etc
void addGoto(const BasicBlock *bb);
void addGoto(const StmtASTNode *bb);

/// Adds: continue;
void addContinue();
Expand All @@ -220,7 +219,7 @@ class BOOMERANG_PLUGIN_API CCodeGenerator : public ICodeGenerator

// labels
/// Adds: L \a ord :
void addLabel(const BasicBlock *bb);
void addLabel(const StmtASTNode *bb);

// proc related
/**
Expand Down Expand Up @@ -280,32 +279,32 @@ class BOOMERANG_PLUGIN_API CCodeGenerator : public ICodeGenerator
void closeParen(OStream &str, OpPrec outer, OpPrec inner);


void generateCode(const BasicBlock *bb, const BasicBlock *latch,
std::list<const BasicBlock *> &followSet,
std::list<const BasicBlock *> &gotoSet, UserProc *proc);
void generateCode_Loop(const BasicBlock *bb, std::list<const BasicBlock *> &gotoSet,
UserProc *proc, const BasicBlock *latch,
std::list<const BasicBlock *> &followSet);
void generateCode_Branch(const BasicBlock *bb, std::list<const BasicBlock *> &gotoSet,
UserProc *proc, const BasicBlock *latch,
std::list<const BasicBlock *> &followSet);
void generateCode_Seq(const BasicBlock *bb, std::list<const BasicBlock *> &gotoSet,
UserProc *proc, const BasicBlock *latch,
std::list<const BasicBlock *> &followSet);
void generateCode(const StmtASTNode *bb, const StmtASTNode *latch,
std::list<const StmtASTNode *> &followSet,
std::list<const StmtASTNode *> &gotoSet, UserProc *proc);
void generateCode_Loop(const StmtASTNode *bb, std::list<const StmtASTNode *> &gotoSet,
UserProc *proc, const StmtASTNode *latch,
std::list<const StmtASTNode *> &followSet);
void generateCode_Branch(const StmtASTNode *bb, std::list<const StmtASTNode *> &gotoSet,
UserProc *proc, const StmtASTNode *latch,
std::list<const StmtASTNode *> &followSet);
void generateCode_Seq(const StmtASTNode *bb, std::list<const StmtASTNode *> &gotoSet,
UserProc *proc, const StmtASTNode *latch,
std::list<const StmtASTNode *> &followSet);

/// Emits a goto statement (at the correct indentation level) with the destination label for
/// dest. Also places the label just before the destination code if it isn't already there. If
/// the goto is to the return block, it would be nice to emit a 'return' instead (but would have
/// to duplicate the other code in that return BB). Also, 'continue' and 'break' statements
/// are used instead if possible
void emitGotoAndLabel(const BasicBlock *bb, const BasicBlock *dest);
void emitGotoAndLabel(const StmtASTNode *bb, const StmtASTNode *dest);

/// Generates code for each non-CTI (except procedure calls) statement within the block.
void writeBB(const BasicBlock *bb);
void writeBB(const StmtASTNode *bb);

/// \returns true if all predecessors of this BB have had their code generated.
bool isAllParentsGenerated(const BasicBlock *bb) const;
bool isGenerated(const BasicBlock *bb) const;
bool isAllParentsGenerated(const StmtASTNode *bb) const;
bool isGenerated(const StmtASTNode *bb) const;

void emitCodeForStmt(const SharedConstStmt &stmt);

Expand All @@ -316,8 +315,8 @@ class BOOMERANG_PLUGIN_API CCodeGenerator : public ICodeGenerator
* The value or the case label is determined by the value of the first part of the pair,
* the jump destination for the case is determined by the second part of the pair.
*/
std::list<std::pair<SharedExp, const BasicBlock *>>
computeOptimalCaseOrdering(const BasicBlock *caseHead, const SwitchInfo *switchInfo);
std::list<std::pair<SharedExp, const StmtASTNode *>>
computeOptimalCaseOrdering(const StmtASTNode *caseHead, const SwitchInfo *switchInfo);

private:
void print(const Module *module);
Expand All @@ -330,10 +329,10 @@ 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<Address::value_type> m_usedLabels; ///< All used goto labels. (lowAddr of BB)
std::unordered_set<const BasicBlock *> m_generatedBBs;
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;
ControlFlowAnalyzer m_analyzer;
Expand Down
Loading