2007-08-22 01:42:03 +04:00
|
|
|
//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Ted Kremenek and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the CFG and CFGBuilder classes for representing and
|
|
|
|
// building Control-Flow Graphs (CFGs) from ASTs.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/CFG.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
2007-08-22 02:06:14 +04:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2007-08-22 03:26:17 +04:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2007-08-22 01:42:03 +04:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <algorithm>
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2007-08-24 01:26:19 +04:00
|
|
|
// SaveAndRestore - A utility class that uses RIIA to save and restore
|
|
|
|
// the value of a variable.
|
|
|
|
template<typename T>
|
|
|
|
struct SaveAndRestore {
|
|
|
|
SaveAndRestore(T& x) : X(x), old_value(x) {}
|
|
|
|
~SaveAndRestore() { X = old_value; }
|
|
|
|
|
|
|
|
T& X;
|
|
|
|
T old_value;
|
|
|
|
};
|
2007-08-22 01:42:03 +04:00
|
|
|
|
|
|
|
/// CFGBuilder - This class is implements CFG construction from an AST.
|
|
|
|
/// The builder is stateful: an instance of the builder should be used to only
|
|
|
|
/// construct a single CFG.
|
|
|
|
///
|
|
|
|
/// Example usage:
|
|
|
|
///
|
|
|
|
/// CFGBuilder builder;
|
|
|
|
/// CFG* cfg = builder.BuildAST(stmt1);
|
|
|
|
///
|
2007-08-22 02:06:14 +04:00
|
|
|
/// CFG construction is done via a recursive walk of an AST.
|
|
|
|
/// We actually parse the AST in reverse order so that the successor
|
|
|
|
/// of a basic block is constructed prior to its predecessor. This
|
|
|
|
/// allows us to nicely capture implicit fall-throughs without extra
|
|
|
|
/// basic blocks.
|
|
|
|
///
|
|
|
|
class CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
|
2007-08-22 01:42:03 +04:00
|
|
|
CFG* cfg;
|
|
|
|
CFGBlock* Block;
|
|
|
|
CFGBlock* Succ;
|
2007-08-23 01:36:54 +04:00
|
|
|
CFGBlock* ContinueTargetBlock;
|
2007-08-23 01:51:58 +04:00
|
|
|
CFGBlock* BreakTargetBlock;
|
2007-08-23 22:43:24 +04:00
|
|
|
CFGBlock* SwitchTerminatedBlock;
|
2007-08-22 01:42:03 +04:00
|
|
|
unsigned NumBlocks;
|
|
|
|
|
2007-08-22 03:26:17 +04:00
|
|
|
typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
|
|
|
|
LabelMapTy LabelMap;
|
|
|
|
|
2007-08-22 19:40:58 +04:00
|
|
|
typedef std::vector<CFGBlock*> BackpatchBlocksTy;
|
2007-08-22 03:26:17 +04:00
|
|
|
BackpatchBlocksTy BackpatchBlocks;
|
|
|
|
|
2007-08-22 01:42:03 +04:00
|
|
|
public:
|
2007-08-23 20:51:22 +04:00
|
|
|
explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
|
2007-08-23 01:51:58 +04:00
|
|
|
ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
|
2007-08-23 22:43:24 +04:00
|
|
|
SwitchTerminatedBlock(NULL),
|
2007-08-22 01:42:03 +04:00
|
|
|
NumBlocks(0) {
|
|
|
|
// Create an empty CFG.
|
|
|
|
cfg = new CFG();
|
|
|
|
}
|
|
|
|
|
|
|
|
~CFGBuilder() { delete cfg; }
|
2007-08-24 01:42:29 +04:00
|
|
|
|
|
|
|
// buildCFG - Used by external clients to construct the CFG.
|
|
|
|
CFG* buildCFG(Stmt* Statement);
|
|
|
|
|
|
|
|
// Visitors to walk an AST and construct the CFG. Called by
|
|
|
|
// buildCFG. Do not call directly!
|
|
|
|
|
|
|
|
CFGBlock* VisitStmt(Stmt* Statement);
|
|
|
|
CFGBlock* VisitNullStmt(NullStmt* Statement);
|
2007-08-28 01:27:44 +04:00
|
|
|
CFGBlock* VisitParenExpr(ParenExpr* Statement);
|
2007-08-24 01:42:29 +04:00
|
|
|
CFGBlock* VisitCompoundStmt(CompoundStmt* C);
|
|
|
|
CFGBlock* VisitIfStmt(IfStmt* I);
|
|
|
|
CFGBlock* VisitReturnStmt(ReturnStmt* R);
|
|
|
|
CFGBlock* VisitLabelStmt(LabelStmt* L);
|
|
|
|
CFGBlock* VisitGotoStmt(GotoStmt* G);
|
|
|
|
CFGBlock* VisitForStmt(ForStmt* F);
|
|
|
|
CFGBlock* VisitWhileStmt(WhileStmt* W);
|
|
|
|
CFGBlock* VisitDoStmt(DoStmt* D);
|
|
|
|
CFGBlock* VisitContinueStmt(ContinueStmt* C);
|
|
|
|
CFGBlock* VisitBreakStmt(BreakStmt* B);
|
|
|
|
CFGBlock* VisitSwitchStmt(SwitchStmt* S);
|
|
|
|
CFGBlock* VisitSwitchCase(SwitchCase* S);
|
|
|
|
|
|
|
|
private:
|
|
|
|
CFGBlock* createBlock(bool add_successor = true);
|
2007-08-28 01:27:44 +04:00
|
|
|
CFGBlock* addStmt(Stmt* S);
|
|
|
|
CFGBlock* WalkAST(Stmt* S, bool AlwaysAddStmt);
|
|
|
|
CFGBlock* WalkAST_VisitChildren(Stmt* S);
|
2007-08-24 01:42:29 +04:00
|
|
|
void FinishBlock(CFGBlock* B);
|
|
|
|
|
|
|
|
};
|
2007-08-22 22:22:34 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
|
|
|
|
/// represent an arbitrary statement. Examples include a single expression
|
|
|
|
/// or a function body (compound statement). The ownership of the returned
|
|
|
|
/// CFG is transferred to the caller. If CFG construction fails, this method
|
|
|
|
/// returns NULL.
|
|
|
|
CFG* CFGBuilder::buildCFG(Stmt* Statement) {
|
|
|
|
if (!Statement) return NULL;
|
|
|
|
|
|
|
|
// Create an empty block that will serve as the exit block for the CFG.
|
|
|
|
// Since this is the first block added to the CFG, it will be implicitly
|
|
|
|
// registered as the exit block.
|
2007-08-27 23:46:09 +04:00
|
|
|
Succ = createBlock();
|
|
|
|
assert (Succ == &cfg->getExit());
|
|
|
|
Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
|
2007-08-24 01:42:29 +04:00
|
|
|
|
|
|
|
// Visit the statements and create the CFG.
|
|
|
|
if (CFGBlock* B = Visit(Statement)) {
|
|
|
|
// Finalize the last constructed block. This usually involves
|
|
|
|
// reversing the order of the statements in the block.
|
2007-08-27 23:46:09 +04:00
|
|
|
if (Block) FinishBlock(B);
|
2007-08-24 01:42:29 +04:00
|
|
|
|
|
|
|
// Backpatch the gotos whose label -> block mappings we didn't know
|
|
|
|
// when we encountered them.
|
|
|
|
for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
|
|
|
|
E = BackpatchBlocks.end(); I != E; ++I ) {
|
|
|
|
|
|
|
|
CFGBlock* B = *I;
|
|
|
|
GotoStmt* G = cast<GotoStmt>(B->getTerminator());
|
|
|
|
LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
|
|
|
|
|
|
|
|
// If there is no target for the goto, then we are looking at an
|
|
|
|
// incomplete AST. Handle this by not registering a successor.
|
|
|
|
if (LI == LabelMap.end()) continue;
|
2007-08-22 03:26:17 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
B->addSuccessor(LI->second);
|
|
|
|
}
|
|
|
|
|
2007-08-27 23:46:09 +04:00
|
|
|
if (B->pred_size() > 0) {
|
|
|
|
// create an empty entry block that has no predecessors.
|
|
|
|
Succ = B;
|
|
|
|
cfg->setEntry(createBlock());
|
|
|
|
}
|
|
|
|
else cfg->setEntry(B);
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// NULL out cfg so that repeated calls
|
|
|
|
CFG* t = cfg;
|
|
|
|
cfg = NULL;
|
|
|
|
return t;
|
2007-08-22 01:42:03 +04:00
|
|
|
}
|
2007-08-24 01:42:29 +04:00
|
|
|
else return NULL;
|
|
|
|
}
|
2007-08-22 02:06:14 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
/// createBlock - Used to lazily create blocks that are connected
|
|
|
|
/// to the current (global) succcessor.
|
|
|
|
CFGBlock* CFGBuilder::createBlock(bool add_successor) {
|
|
|
|
CFGBlock* B = cfg->createBlock(NumBlocks++);
|
|
|
|
if (add_successor && Succ) B->addSuccessor(Succ);
|
|
|
|
return B;
|
|
|
|
}
|
2007-08-22 22:22:34 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
/// FinishBlock - When the last statement has been added to the block,
|
2007-08-27 23:46:09 +04:00
|
|
|
/// we must reverse the statements because they have been inserted
|
|
|
|
/// in reverse order.
|
2007-08-24 01:42:29 +04:00
|
|
|
void CFGBuilder::FinishBlock(CFGBlock* B) {
|
|
|
|
assert (B);
|
2007-08-27 23:46:09 +04:00
|
|
|
B->reverseStmts();
|
2007-08-24 01:42:29 +04:00
|
|
|
}
|
|
|
|
|
2007-08-28 01:27:44 +04:00
|
|
|
/// addStmt - Used to add statements/expressions to the current CFGBlock
|
|
|
|
/// "Block". This method calls WalkAST on the passed statement to see if it
|
|
|
|
/// contains any short-circuit expressions. If so, it recursively creates
|
|
|
|
/// the necessary blocks for such expressions. It returns the "topmost" block
|
|
|
|
/// of the created blocks, or the original value of "Block" when this method
|
|
|
|
/// was called if no additional blocks are created.
|
|
|
|
CFGBlock* CFGBuilder::addStmt(Stmt* S) {
|
|
|
|
assert (Block);
|
|
|
|
return WalkAST(S,true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// WalkAST - Used by addStmt to walk the subtree of a statement and
|
|
|
|
/// add extra blocks for ternary operators, &&, and ||.
|
|
|
|
CFGBlock* CFGBuilder::WalkAST(Stmt* S, bool AlwaysAddStmt = false) {
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
case Stmt::ConditionalOperatorClass: {
|
|
|
|
ConditionalOperator* C = cast<ConditionalOperator>(S);
|
|
|
|
|
|
|
|
CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
|
|
|
|
ConfluenceBlock->appendStmt(C);
|
|
|
|
FinishBlock(ConfluenceBlock);
|
|
|
|
|
|
|
|
Succ = ConfluenceBlock;
|
|
|
|
Block = NULL;
|
|
|
|
CFGBlock* LHSBlock = Visit(C->getLHS());
|
|
|
|
|
|
|
|
Succ = ConfluenceBlock;
|
|
|
|
Block = NULL;
|
|
|
|
CFGBlock* RHSBlock = Visit(C->getRHS());
|
|
|
|
|
|
|
|
Block = createBlock(false);
|
|
|
|
Block->addSuccessor(LHSBlock);
|
|
|
|
Block->addSuccessor(RHSBlock);
|
|
|
|
Block->setTerminator(C);
|
|
|
|
return addStmt(C->getCond());
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if (AlwaysAddStmt) Block->appendStmt(S);
|
|
|
|
return WalkAST_VisitChildren(S);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// WalkAST_VisitChildren - Utility method to call WalkAST on the
|
|
|
|
/// children of a Stmt.
|
|
|
|
CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* S)
|
|
|
|
{
|
|
|
|
CFGBlock* B = Block;
|
|
|
|
for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ;
|
|
|
|
I != E; ++I)
|
|
|
|
B = WalkAST(*I);
|
|
|
|
|
|
|
|
return B;
|
|
|
|
}
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
/// VisitStmt - Handle statements with no branching control flow.
|
|
|
|
CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
|
|
|
|
// We cannot assume that we are in the middle of a basic block, since
|
|
|
|
// the CFG might only be constructed for this single statement. If
|
|
|
|
// we have no current basic block, just create one lazily.
|
|
|
|
if (!Block) Block = createBlock();
|
2007-08-22 02:06:14 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Simply add the statement to the current block. We actually
|
|
|
|
// insert statements in reverse order; this order is reversed later
|
|
|
|
// when processing the containing element in the AST.
|
2007-08-27 23:46:09 +04:00
|
|
|
addStmt(Statement);
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
|
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
2007-08-28 01:27:44 +04:00
|
|
|
CFGBlock* CFGBuilder::VisitParenExpr(ParenExpr* Statement) {
|
|
|
|
return Visit(Statement->getSubExpr());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
|
|
|
|
// The value returned from this function is the last created CFGBlock
|
|
|
|
// that represents the "entry" point for the translated AST node.
|
|
|
|
CFGBlock* LastBlock;
|
|
|
|
|
|
|
|
for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(),
|
|
|
|
E = C->body_rend(); I != E; ++I )
|
|
|
|
// Add the statement to the current block.
|
|
|
|
if (!(LastBlock=Visit(*I)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return LastBlock;
|
|
|
|
}
|
2007-08-22 01:42:03 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
|
|
|
|
// We may see an if statement in the middle of a basic block, or
|
|
|
|
// it may be the first statement we are processing. In either case,
|
|
|
|
// we create a new basic block. First, we create the blocks for
|
|
|
|
// the then...else statements, and then we create the block containing
|
|
|
|
// the if statement. If we were in the middle of a block, we
|
|
|
|
// stop processing that block and reverse its statements. That block
|
|
|
|
// is then the implicit successor for the "then" and "else" clauses.
|
|
|
|
|
|
|
|
// The block we were proccessing is now finished. Make it the
|
|
|
|
// successor block.
|
|
|
|
if (Block) {
|
|
|
|
Succ = Block;
|
|
|
|
FinishBlock(Block);
|
2007-08-22 02:06:14 +04:00
|
|
|
}
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Process the false branch. NULL out Block so that the recursive
|
|
|
|
// call to Visit will create a new basic block.
|
|
|
|
// Null out Block so that all successor
|
|
|
|
CFGBlock* ElseBlock = Succ;
|
2007-08-22 02:06:14 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
if (Stmt* Else = I->getElse()) {
|
|
|
|
SaveAndRestore<CFGBlock*> sv(Succ);
|
2007-08-22 02:06:14 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// NULL out Block so that the recursive call to Visit will
|
|
|
|
// create a new basic block.
|
|
|
|
Block = NULL;
|
|
|
|
ElseBlock = Visit(Else);
|
|
|
|
if (!ElseBlock) return NULL;
|
2007-08-27 23:46:09 +04:00
|
|
|
if (Block) FinishBlock(ElseBlock);
|
2007-08-22 02:06:14 +04:00
|
|
|
}
|
2007-08-24 01:42:29 +04:00
|
|
|
|
|
|
|
// Process the true branch. NULL out Block so that the recursive
|
|
|
|
// call to Visit will create a new basic block.
|
|
|
|
// Null out Block so that all successor
|
|
|
|
CFGBlock* ThenBlock;
|
|
|
|
{
|
|
|
|
Stmt* Then = I->getThen();
|
|
|
|
assert (Then);
|
|
|
|
SaveAndRestore<CFGBlock*> sv(Succ);
|
|
|
|
Block = NULL;
|
|
|
|
ThenBlock = Visit(Then);
|
|
|
|
if (!ThenBlock) return NULL;
|
2007-08-27 23:46:09 +04:00
|
|
|
if (Block) FinishBlock(ThenBlock);
|
2007-08-22 03:26:17 +04:00
|
|
|
}
|
2007-08-24 01:42:29 +04:00
|
|
|
|
|
|
|
// Now create a new block containing the if statement.
|
|
|
|
Block = createBlock(false);
|
2007-08-22 03:26:17 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Set the terminator of the new block to the If statement.
|
|
|
|
Block->setTerminator(I);
|
|
|
|
|
|
|
|
// Now add the successors.
|
|
|
|
Block->addSuccessor(ThenBlock);
|
|
|
|
Block->addSuccessor(ElseBlock);
|
2007-08-27 23:46:09 +04:00
|
|
|
|
|
|
|
// Add the condition as the last statement in the new block. This
|
|
|
|
// may create new blocks as the condition may contain control-flow. Any
|
|
|
|
// newly created blocks will be pointed to be "Block".
|
|
|
|
return addStmt(I->getCond());
|
2007-08-24 01:42:29 +04:00
|
|
|
}
|
2007-08-22 03:26:17 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
|
|
|
|
// If we were in the middle of a block we stop processing that block
|
|
|
|
// and reverse its statements.
|
|
|
|
//
|
|
|
|
// NOTE: If a "return" appears in the middle of a block, this means
|
|
|
|
// that the code afterwards is DEAD (unreachable). We still
|
|
|
|
// keep a basic block for that code; a simple "mark-and-sweep"
|
|
|
|
// from the entry block will be able to report such dead
|
|
|
|
// blocks.
|
|
|
|
if (Block) FinishBlock(Block);
|
|
|
|
|
|
|
|
// Create the new block.
|
|
|
|
Block = createBlock(false);
|
2007-08-22 03:26:17 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// The Exit block is the only successor.
|
|
|
|
Block->addSuccessor(&cfg->getExit());
|
2007-08-27 23:46:09 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Also add the return statement as the terminator.
|
|
|
|
Block->setTerminator(R);
|
2007-08-27 23:46:09 +04:00
|
|
|
|
|
|
|
// Add the return statement to the block. This may create new blocks
|
|
|
|
// if R contains control-flow (short-circuit operations).
|
|
|
|
return addStmt(R);
|
2007-08-24 01:42:29 +04:00
|
|
|
}
|
2007-08-22 03:26:17 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
|
|
|
|
// Get the block of the labeled statement. Add it to our map.
|
|
|
|
CFGBlock* LabelBlock = Visit(L->getSubStmt());
|
|
|
|
assert (LabelBlock);
|
|
|
|
|
|
|
|
assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
|
|
|
|
LabelMap[ L ] = LabelBlock;
|
2007-08-22 22:22:34 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Labels partition blocks, so this is the end of the basic block
|
2007-08-27 23:46:09 +04:00
|
|
|
// we were processing (the label is the first statement). Add the label
|
|
|
|
// the to end (really the beginning) of the block. Because this is
|
|
|
|
// label (and we have already processed the substatement) there is no
|
|
|
|
// extra control-flow to worry about.
|
2007-08-24 01:42:29 +04:00
|
|
|
LabelBlock->appendStmt(L);
|
|
|
|
FinishBlock(LabelBlock);
|
|
|
|
|
|
|
|
// We set Block to NULL to allow lazy creation of a new block
|
|
|
|
// (if necessary);
|
|
|
|
Block = NULL;
|
|
|
|
|
|
|
|
// This block is now the implicit successor of other blocks.
|
|
|
|
Succ = LabelBlock;
|
|
|
|
|
|
|
|
return LabelBlock;
|
|
|
|
}
|
2007-08-23 02:35:28 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
|
|
|
|
// Goto is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block and create a new one.
|
|
|
|
if (Block) FinishBlock(Block);
|
|
|
|
Block = createBlock(false);
|
|
|
|
Block->setTerminator(G);
|
|
|
|
|
|
|
|
// If we already know the mapping to the label block add the
|
|
|
|
// successor now.
|
|
|
|
LabelMapTy::iterator I = LabelMap.find(G->getLabel());
|
|
|
|
|
|
|
|
if (I == LabelMap.end())
|
|
|
|
// We will need to backpatch this block later.
|
|
|
|
BackpatchBlocks.push_back(Block);
|
|
|
|
else
|
|
|
|
Block->addSuccessor(I->second);
|
|
|
|
|
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
|
|
|
|
// "for" is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block.
|
|
|
|
|
|
|
|
CFGBlock* LoopSuccessor = NULL;
|
|
|
|
|
|
|
|
if (Block) {
|
|
|
|
FinishBlock(Block);
|
|
|
|
LoopSuccessor = Block;
|
2007-08-22 22:22:34 +04:00
|
|
|
}
|
2007-08-24 01:42:29 +04:00
|
|
|
else LoopSuccessor = Succ;
|
|
|
|
|
2007-08-27 23:46:09 +04:00
|
|
|
// Because of short-circuit evaluation, the condition of the loop
|
|
|
|
// can span multiple basic blocks. Thus we need the "Entry" and "Exit"
|
|
|
|
// blocks that evaluate the condition.
|
|
|
|
CFGBlock* ExitConditionBlock = createBlock(false);
|
|
|
|
CFGBlock* EntryConditionBlock = ExitConditionBlock;
|
|
|
|
|
|
|
|
// Set the terminator for the "exit" condition block.
|
|
|
|
ExitConditionBlock->setTerminator(F);
|
|
|
|
|
|
|
|
// Now add the actual condition to the condition block. Because the
|
|
|
|
// condition itself may contain control-flow, new blocks may be created.
|
|
|
|
if (Stmt* C = F->getCond()) {
|
|
|
|
Block = ExitConditionBlock;
|
|
|
|
EntryConditionBlock = addStmt(C);
|
|
|
|
if (Block) FinishBlock(EntryConditionBlock);
|
|
|
|
}
|
2007-08-24 01:42:29 +04:00
|
|
|
|
|
|
|
// The condition block is the implicit successor for the loop body as
|
|
|
|
// well as any code above the loop.
|
2007-08-27 23:46:09 +04:00
|
|
|
Succ = EntryConditionBlock;
|
2007-08-23 01:05:42 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Now create the loop body.
|
|
|
|
{
|
|
|
|
assert (F->getBody());
|
2007-08-23 02:35:28 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Save the current values for Block, Succ, and continue and break targets
|
|
|
|
SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
|
|
|
|
save_continue(ContinueTargetBlock),
|
|
|
|
save_break(BreakTargetBlock);
|
|
|
|
|
|
|
|
// All continues within this loop should go to the condition block
|
2007-08-27 23:46:09 +04:00
|
|
|
ContinueTargetBlock = EntryConditionBlock;
|
2007-08-23 02:35:28 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// All breaks should go to the code following the loop.
|
|
|
|
BreakTargetBlock = LoopSuccessor;
|
2007-08-23 01:05:42 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Create a new block to contain the (bottom) of the loop body.
|
|
|
|
Block = createBlock();
|
2007-08-23 01:36:54 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// If we have increment code, insert it at the end of the body block.
|
2007-08-27 23:46:09 +04:00
|
|
|
if (Stmt* I = F->getInc()) Block = addStmt(I);
|
2007-08-23 01:05:42 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Now populate the body block, and in the process create new blocks
|
|
|
|
// as we walk the body of the loop.
|
|
|
|
CFGBlock* BodyBlock = Visit(F->getBody());
|
|
|
|
assert (BodyBlock);
|
2007-08-27 23:46:09 +04:00
|
|
|
if (Block) FinishBlock(BodyBlock);
|
2007-08-23 01:05:42 +04:00
|
|
|
|
2007-08-27 23:46:09 +04:00
|
|
|
// This new body block is a successor to our "exit" condition block.
|
|
|
|
ExitConditionBlock->addSuccessor(BodyBlock);
|
2007-08-24 01:42:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Link up the condition block with the code that follows the loop.
|
|
|
|
// (the false branch).
|
2007-08-27 23:46:09 +04:00
|
|
|
ExitConditionBlock->addSuccessor(LoopSuccessor);
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// If the loop contains initialization, create a new block for those
|
|
|
|
// statements. This block can also contain statements that precede
|
|
|
|
// the loop.
|
|
|
|
if (Stmt* I = F->getInit()) {
|
|
|
|
Block = createBlock();
|
2007-08-27 23:46:09 +04:00
|
|
|
return addStmt(I);
|
2007-08-24 01:42:29 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// There is no loop initialization. We are thus basically a while
|
|
|
|
// loop. NULL out Block to force lazy block construction.
|
2007-08-23 01:05:42 +04:00
|
|
|
Block = NULL;
|
2007-08-27 23:46:09 +04:00
|
|
|
return EntryConditionBlock;
|
2007-08-23 01:05:42 +04:00
|
|
|
}
|
2007-08-24 01:42:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
|
|
|
|
// "while" is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block.
|
2007-08-23 01:05:42 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
CFGBlock* LoopSuccessor = NULL;
|
|
|
|
|
|
|
|
if (Block) {
|
|
|
|
FinishBlock(Block);
|
|
|
|
LoopSuccessor = Block;
|
|
|
|
}
|
|
|
|
else LoopSuccessor = Succ;
|
2007-08-27 23:46:09 +04:00
|
|
|
|
|
|
|
// Because of short-circuit evaluation, the condition of the loop
|
|
|
|
// can span multiple basic blocks. Thus we need the "Entry" and "Exit"
|
|
|
|
// blocks that evaluate the condition.
|
|
|
|
CFGBlock* ExitConditionBlock = createBlock(false);
|
|
|
|
CFGBlock* EntryConditionBlock = ExitConditionBlock;
|
|
|
|
|
|
|
|
// Set the terminator for the "exit" condition block.
|
|
|
|
ExitConditionBlock->setTerminator(W);
|
|
|
|
|
|
|
|
// Now add the actual condition to the condition block. Because the
|
|
|
|
// condition itself may contain control-flow, new blocks may be created.
|
|
|
|
// Thus we update "Succ" after adding the condition.
|
|
|
|
if (Stmt* C = W->getCond()) {
|
|
|
|
Block = ExitConditionBlock;
|
|
|
|
EntryConditionBlock = addStmt(C);
|
|
|
|
if (Block) FinishBlock(EntryConditionBlock);
|
|
|
|
}
|
2007-08-24 01:42:29 +04:00
|
|
|
|
|
|
|
// The condition block is the implicit successor for the loop body as
|
|
|
|
// well as any code above the loop.
|
2007-08-27 23:46:09 +04:00
|
|
|
Succ = EntryConditionBlock;
|
2007-08-24 01:42:29 +04:00
|
|
|
|
|
|
|
// Process the loop body.
|
|
|
|
{
|
|
|
|
assert (W->getBody());
|
|
|
|
|
|
|
|
// Save the current values for Block, Succ, and continue and break targets
|
|
|
|
SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
|
|
|
|
save_continue(ContinueTargetBlock),
|
|
|
|
save_break(BreakTargetBlock);
|
|
|
|
|
|
|
|
// All continues within this loop should go to the condition block
|
2007-08-27 23:46:09 +04:00
|
|
|
ContinueTargetBlock = EntryConditionBlock;
|
2007-08-23 21:15:32 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// All breaks should go to the code following the loop.
|
|
|
|
BreakTargetBlock = LoopSuccessor;
|
2007-08-23 21:15:32 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// NULL out Block to force lazy instantiation of blocks for the body.
|
2007-08-23 21:15:32 +04:00
|
|
|
Block = NULL;
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Create the body. The returned block is the entry to the loop body.
|
|
|
|
CFGBlock* BodyBlock = Visit(W->getBody());
|
|
|
|
assert (BodyBlock);
|
2007-08-27 23:46:09 +04:00
|
|
|
if (Block) FinishBlock(BodyBlock);
|
2007-08-23 01:36:54 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Add the loop body entry as a successor to the condition.
|
2007-08-27 23:46:09 +04:00
|
|
|
ExitConditionBlock->addSuccessor(BodyBlock);
|
2007-08-23 01:36:54 +04:00
|
|
|
}
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Link up the condition block with the code that follows the loop.
|
|
|
|
// (the false branch).
|
2007-08-27 23:46:09 +04:00
|
|
|
ExitConditionBlock->addSuccessor(LoopSuccessor);
|
2007-08-24 01:42:29 +04:00
|
|
|
|
|
|
|
// There can be no more statements in the condition block
|
|
|
|
// since we loop back to this block. NULL out Block to force
|
|
|
|
// lazy creation of another block.
|
|
|
|
Block = NULL;
|
|
|
|
|
|
|
|
// Return the condition block, which is the dominating block for the loop.
|
2007-08-27 23:46:09 +04:00
|
|
|
return EntryConditionBlock;
|
2007-08-24 01:42:29 +04:00
|
|
|
}
|
2007-08-23 21:29:58 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
|
|
|
|
// "do...while" is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block.
|
|
|
|
|
|
|
|
CFGBlock* LoopSuccessor = NULL;
|
|
|
|
|
|
|
|
if (Block) {
|
|
|
|
FinishBlock(Block);
|
|
|
|
LoopSuccessor = Block;
|
2007-08-23 01:51:58 +04:00
|
|
|
}
|
2007-08-24 01:42:29 +04:00
|
|
|
else LoopSuccessor = Succ;
|
2007-08-23 01:51:58 +04:00
|
|
|
|
2007-08-27 23:46:09 +04:00
|
|
|
// Because of short-circuit evaluation, the condition of the loop
|
|
|
|
// can span multiple basic blocks. Thus we need the "Entry" and "Exit"
|
|
|
|
// blocks that evaluate the condition.
|
|
|
|
CFGBlock* ExitConditionBlock = createBlock(false);
|
|
|
|
CFGBlock* EntryConditionBlock = ExitConditionBlock;
|
|
|
|
|
|
|
|
// Set the terminator for the "exit" condition block.
|
|
|
|
ExitConditionBlock->setTerminator(D);
|
|
|
|
|
|
|
|
// Now add the actual condition to the condition block. Because the
|
|
|
|
// condition itself may contain control-flow, new blocks may be created.
|
|
|
|
if (Stmt* C = D->getCond()) {
|
|
|
|
Block = ExitConditionBlock;
|
|
|
|
EntryConditionBlock = addStmt(C);
|
|
|
|
if (Block) FinishBlock(EntryConditionBlock);
|
|
|
|
}
|
2007-08-24 01:42:29 +04:00
|
|
|
|
2007-08-27 23:46:09 +04:00
|
|
|
// The condition block is the implicit successor for the loop body as
|
|
|
|
// well as any code above the loop.
|
|
|
|
Succ = EntryConditionBlock;
|
|
|
|
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Process the loop body.
|
2007-08-27 23:46:09 +04:00
|
|
|
CFGBlock* BodyBlock = NULL;
|
2007-08-24 01:42:29 +04:00
|
|
|
{
|
|
|
|
assert (D->getBody());
|
2007-08-23 22:43:24 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Save the current values for Block, Succ, and continue and break targets
|
|
|
|
SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
|
|
|
|
save_continue(ContinueTargetBlock),
|
|
|
|
save_break(BreakTargetBlock);
|
2007-08-23 22:43:24 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// All continues within this loop should go to the condition block
|
2007-08-27 23:46:09 +04:00
|
|
|
ContinueTargetBlock = EntryConditionBlock;
|
2007-08-23 22:43:24 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// All breaks should go to the code following the loop.
|
|
|
|
BreakTargetBlock = LoopSuccessor;
|
2007-08-23 22:43:24 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// NULL out Block to force lazy instantiation of blocks for the body.
|
2007-08-23 22:43:24 +04:00
|
|
|
Block = NULL;
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Create the body. The returned block is the entry to the loop body.
|
|
|
|
BodyBlock = Visit(D->getBody());
|
|
|
|
assert (BodyBlock);
|
2007-08-27 23:46:09 +04:00
|
|
|
if (Block) FinishBlock(BodyBlock);
|
2007-08-23 22:43:24 +04:00
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Add the loop body entry as a successor to the condition.
|
2007-08-27 23:46:09 +04:00
|
|
|
ExitConditionBlock->addSuccessor(BodyBlock);
|
2007-08-23 22:43:24 +04:00
|
|
|
}
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
// Link up the condition block with the code that follows the loop.
|
|
|
|
// (the false branch).
|
2007-08-27 23:46:09 +04:00
|
|
|
ExitConditionBlock->addSuccessor(LoopSuccessor);
|
2007-08-24 01:42:29 +04:00
|
|
|
|
2007-08-27 23:46:09 +04:00
|
|
|
// There can be no more statements in the body block(s)
|
|
|
|
// since we loop back to the body. NULL out Block to force
|
2007-08-24 01:42:29 +04:00
|
|
|
// lazy creation of another block.
|
|
|
|
Block = NULL;
|
|
|
|
|
|
|
|
// Return the loop body, which is the dominating block for the loop.
|
|
|
|
return BodyBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
|
|
|
|
// "continue" is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block.
|
|
|
|
if (Block) FinishBlock(Block);
|
|
|
|
|
|
|
|
// Now create a new block that ends with the continue statement.
|
|
|
|
Block = createBlock(false);
|
|
|
|
Block->setTerminator(C);
|
|
|
|
|
|
|
|
// If there is no target for the continue, then we are looking at an
|
|
|
|
// incomplete AST. Handle this by not registering a successor.
|
|
|
|
if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
|
|
|
|
|
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
|
|
|
|
// "break" is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block.
|
|
|
|
if (Block) FinishBlock(Block);
|
|
|
|
|
|
|
|
// Now create a new block that ends with the continue statement.
|
|
|
|
Block = createBlock(false);
|
|
|
|
Block->setTerminator(B);
|
|
|
|
|
|
|
|
// If there is no target for the break, then we are looking at an
|
|
|
|
// incomplete AST. Handle this by not registering a successor.
|
|
|
|
if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
|
|
|
|
|
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* S) {
|
|
|
|
// "switch" is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block.
|
|
|
|
CFGBlock* SwitchSuccessor = NULL;
|
|
|
|
|
|
|
|
if (Block) {
|
|
|
|
FinishBlock(Block);
|
|
|
|
SwitchSuccessor = Block;
|
2007-08-23 22:43:24 +04:00
|
|
|
}
|
2007-08-24 01:42:29 +04:00
|
|
|
else SwitchSuccessor = Succ;
|
|
|
|
|
|
|
|
// Save the current "switch" context.
|
|
|
|
SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
|
|
|
|
save_break(BreakTargetBlock);
|
|
|
|
|
|
|
|
// Create a new block that will contain the switch statement.
|
|
|
|
SwitchTerminatedBlock = createBlock(false);
|
|
|
|
|
|
|
|
// Now process the switch body. The code after the switch is the implicit
|
|
|
|
// successor.
|
|
|
|
Succ = SwitchSuccessor;
|
|
|
|
BreakTargetBlock = SwitchSuccessor;
|
|
|
|
|
|
|
|
// When visiting the body, the case statements should automatically get
|
|
|
|
// linked up to the switch. We also don't keep a pointer to the body,
|
|
|
|
// since all control-flow from the switch goes to case/default statements.
|
2007-08-27 23:46:09 +04:00
|
|
|
assert (S->getBody() && "switch must contain a non-NULL body");
|
|
|
|
Block = NULL;
|
|
|
|
CFGBlock *BodyBlock = Visit(S->getBody());
|
|
|
|
if (Block) FinishBlock(BodyBlock);
|
|
|
|
|
|
|
|
// Add the terminator and condition in the switch block.
|
|
|
|
SwitchTerminatedBlock->setTerminator(S);
|
|
|
|
assert (S->getCond() && "switch condition must be non-NULL");
|
2007-08-24 01:42:29 +04:00
|
|
|
Block = SwitchTerminatedBlock;
|
2007-08-27 23:46:09 +04:00
|
|
|
return addStmt(S->getCond());
|
2007-08-24 01:42:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CFGBlock* CFGBuilder::VisitSwitchCase(SwitchCase* S) {
|
|
|
|
// A SwitchCase is either a "default" or "case" statement. We handle
|
|
|
|
// both in the same way. They are essentially labels, so they are the
|
|
|
|
// first statement in a block.
|
|
|
|
CFGBlock* CaseBlock = Visit(S->getSubStmt());
|
|
|
|
assert (CaseBlock);
|
|
|
|
|
|
|
|
// Cases/Default statements parition block, so this is the top of
|
|
|
|
// the basic block we were processing (the case/default is the first stmt).
|
|
|
|
CaseBlock->appendStmt(S);
|
|
|
|
FinishBlock(CaseBlock);
|
|
|
|
|
|
|
|
// Add this block to the list of successors for the block with the
|
|
|
|
// switch statement.
|
|
|
|
if (SwitchTerminatedBlock) SwitchTerminatedBlock->addSuccessor(CaseBlock);
|
|
|
|
|
|
|
|
// We set Block to NULL to allow lazy creation of a new block (if necessary)
|
|
|
|
Block = NULL;
|
|
|
|
|
|
|
|
// This block is now the implicit successor of other blocks.
|
|
|
|
Succ = CaseBlock;
|
|
|
|
|
|
|
|
return CaseBlock;
|
|
|
|
}
|
|
|
|
|
2007-08-23 22:43:24 +04:00
|
|
|
|
2007-08-24 01:26:19 +04:00
|
|
|
} // end anonymous namespace
|
2007-08-23 20:51:22 +04:00
|
|
|
|
|
|
|
/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
|
|
|
|
/// block has no successors or predecessors. If this is the first block
|
|
|
|
/// created in the CFG, it is automatically set to be the Entry and Exit
|
|
|
|
/// of the CFG.
|
|
|
|
CFGBlock* CFG::createBlock(unsigned blockID) {
|
|
|
|
bool first_block = begin() == end();
|
|
|
|
|
|
|
|
// Create the block.
|
|
|
|
Blocks.push_front(CFGBlock(blockID));
|
|
|
|
|
|
|
|
// If this is the first block, set it as the Entry and Exit.
|
|
|
|
if (first_block) Entry = Exit = &front();
|
|
|
|
|
|
|
|
// Return the block.
|
|
|
|
return &front();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
|
|
|
|
/// CFG is returned to the caller.
|
|
|
|
CFG* CFG::buildCFG(Stmt* Statement) {
|
2007-08-22 01:42:03 +04:00
|
|
|
CFGBuilder Builder;
|
2007-08-23 20:51:22 +04:00
|
|
|
return Builder.buildCFG(Statement);
|
2007-08-22 01:42:03 +04:00
|
|
|
}
|
|
|
|
|
2007-08-23 20:51:22 +04:00
|
|
|
/// reverseStmts - Reverses the orders of statements within a CFGBlock.
|
2007-08-22 01:42:03 +04:00
|
|
|
void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
|
|
|
|
|
2007-08-23 20:51:22 +04:00
|
|
|
/// dump - A simple pretty printer of a CFG that outputs to stderr.
|
2007-08-22 01:42:03 +04:00
|
|
|
void CFG::dump() { print(std::cerr); }
|
|
|
|
|
2007-08-23 20:51:22 +04:00
|
|
|
/// print - A simple pretty printer of a CFG that outputs to an ostream.
|
2007-08-22 01:42:03 +04:00
|
|
|
void CFG::print(std::ostream& OS) {
|
2007-08-23 20:51:22 +04:00
|
|
|
// Print the Entry block.
|
2007-08-23 01:05:42 +04:00
|
|
|
if (begin() != end()) {
|
|
|
|
CFGBlock& Entry = getEntry();
|
|
|
|
OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n";
|
|
|
|
Entry.print(OS);
|
|
|
|
}
|
|
|
|
|
2007-08-23 20:51:22 +04:00
|
|
|
// Iterate through the CFGBlocks and print them one by one.
|
2007-08-22 01:42:03 +04:00
|
|
|
for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
|
2007-08-23 01:05:42 +04:00
|
|
|
// Skip the entry block, because we already printed it.
|
2007-08-23 20:51:22 +04:00
|
|
|
if (&(*I) == &getEntry() || &(*I) == &getExit()) continue;
|
2007-08-23 01:05:42 +04:00
|
|
|
|
2007-08-23 20:51:22 +04:00
|
|
|
OS << "\n [ B" << I->getBlockID() << " ]\n";
|
2007-08-22 01:42:03 +04:00
|
|
|
I->print(OS);
|
|
|
|
}
|
2007-08-24 01:42:29 +04:00
|
|
|
|
2007-08-23 20:51:22 +04:00
|
|
|
// Print the Exit Block.
|
|
|
|
if (begin() != end()) {
|
|
|
|
CFGBlock& Exit = getExit();
|
|
|
|
OS << "\n [ B" << Exit.getBlockID() << " (EXIT) ]\n";
|
|
|
|
Exit.print(OS);
|
|
|
|
}
|
2007-08-24 01:42:29 +04:00
|
|
|
|
2007-08-22 01:42:03 +04:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2007-08-22 22:22:34 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint,
|
|
|
|
void > {
|
|
|
|
std::ostream& OS;
|
|
|
|
public:
|
|
|
|
CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {}
|
|
|
|
|
|
|
|
void VisitIfStmt(IfStmt* I) {
|
|
|
|
OS << "if ";
|
|
|
|
I->getCond()->printPretty(std::cerr);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default case.
|
|
|
|
void VisitStmt(Stmt* S) { S->printPretty(OS); }
|
|
|
|
|
|
|
|
void VisitForStmt(ForStmt* F) {
|
|
|
|
OS << "for (" ;
|
|
|
|
if (Stmt* I = F->getInit()) I->printPretty(OS);
|
|
|
|
OS << " ; ";
|
|
|
|
if (Stmt* C = F->getCond()) C->printPretty(OS);
|
|
|
|
OS << " ; ";
|
|
|
|
if (Stmt* I = F->getInc()) I->printPretty(OS);
|
|
|
|
OS << ")\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitWhileStmt(WhileStmt* W) {
|
|
|
|
OS << "while " ;
|
|
|
|
if (Stmt* C = W->getCond()) C->printPretty(OS);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitDoStmt(DoStmt* D) {
|
|
|
|
OS << "do ... while ";
|
|
|
|
if (Stmt* C = D->getCond()) C->printPretty(OS);
|
2007-08-28 01:27:44 +04:00
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitSwitchStmt(SwitchStmt* S) {
|
|
|
|
OS << "switch ";
|
|
|
|
S->getCond()->printPretty(OS);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitConditionalOperator(ConditionalOperator* C) {
|
|
|
|
C->printPretty(OS);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2007-08-24 01:42:29 +04:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-08-22 22:22:34 +04:00
|
|
|
|
2007-08-23 20:51:22 +04:00
|
|
|
/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
|
2007-08-22 01:42:03 +04:00
|
|
|
void CFGBlock::dump() { print(std::cerr); }
|
|
|
|
|
2007-08-23 20:51:22 +04:00
|
|
|
/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
|
|
|
|
/// Generally this will only be called from CFG::print.
|
2007-08-22 01:42:03 +04:00
|
|
|
void CFGBlock::print(std::ostream& OS) {
|
|
|
|
|
|
|
|
// Iterate through the statements in the block and print them.
|
|
|
|
OS << " ------------------------\n";
|
|
|
|
unsigned j = 1;
|
|
|
|
for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) {
|
2007-08-22 03:26:17 +04:00
|
|
|
// Print the statement # in the basic block.
|
|
|
|
OS << " " << std::setw(3) << j << ": ";
|
|
|
|
|
|
|
|
// Print the statement/expression.
|
|
|
|
Stmt* S = *I;
|
|
|
|
|
|
|
|
if (LabelStmt* L = dyn_cast<LabelStmt>(S))
|
|
|
|
OS << L->getName() << ": (LABEL)\n";
|
|
|
|
else
|
|
|
|
(*I)->printPretty(OS);
|
|
|
|
|
|
|
|
// Expressions need a newline.
|
2007-08-22 01:42:03 +04:00
|
|
|
if (isa<Expr>(*I)) OS << '\n';
|
|
|
|
}
|
|
|
|
OS << " ------------------------\n";
|
|
|
|
|
|
|
|
// Print the predecessors of this block.
|
|
|
|
OS << " Predecessors (" << pred_size() << "):";
|
|
|
|
unsigned i = 0;
|
|
|
|
for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) {
|
|
|
|
if (i == 8 || (i-8) == 0) {
|
|
|
|
OS << "\n ";
|
|
|
|
}
|
|
|
|
OS << " B" << (*I)->getBlockID();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the terminator of this block.
|
|
|
|
OS << "\n Terminator: ";
|
2007-08-22 22:22:34 +04:00
|
|
|
if (ControlFlowStmt)
|
|
|
|
CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt);
|
|
|
|
else
|
|
|
|
OS << "<NULL>\n";
|
2007-08-22 01:42:03 +04:00
|
|
|
|
|
|
|
// Print the successors of this block.
|
|
|
|
OS << " Successors (" << succ_size() << "):";
|
|
|
|
i = 0;
|
|
|
|
for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) {
|
|
|
|
if (i == 8 || (i-8) % 10 == 0) {
|
|
|
|
OS << "\n ";
|
|
|
|
}
|
|
|
|
OS << " B" << (*I)->getBlockID();
|
|
|
|
}
|
|
|
|
OS << '\n';
|
2007-08-23 20:51:22 +04:00
|
|
|
}
|