ROSE  0.11.2.0
virtualBinCFG.h
1 #ifndef VIRTUAL_BIN_CFG_H
2 #define VIRTUAL_BIN_CFG_H
3 #include <rosePublicConfig.h>
4 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
5 
6 #include <stdint.h>
7 #include "rosedll.h"
8 
9 //class AttachedPreprocessingInfoType;
10 class SgAsmInstruction;
11 class SgAsmStatement;
12 
13 namespace VirtualBinCFG {
14 
15  class CFGEdge;
16 
17  enum EdgeConditionKind
18  {
19  eckUnconditional, // Normal, unconditional edge
20  eckTrue, // True case of a two-way branch
21  eckFalse, // False case of a two-way branch
22  eckCaseLabel, // Case label (constant is given by caseLabel())
23  eckDefault // Default label
24  };
25 
26  typedef std::set<rose_addr_t> AddressSet;
27  typedef std::map<rose_addr_t, SgAsmInstruction*> AddressToInstructionMap;
28  typedef std::map<SgAsmInstruction*, AddressSet> InstructionToAddressesMap;
29  typedef std::map<SgAsmStatement*, AddressSet> StatementToAddressesMap;
30 
31  struct ROSE_DLL_API AuxiliaryInformation {
32  AddressToInstructionMap addressToInstructionMap;
33  InstructionToAddressesMap indirectJumpTargets;
34  StatementToAddressesMap returnTargets; /* statement is SgAsmBlock or SgAsmFunction */
35  InstructionToAddressesMap incomingEdges;
36 
37  public:
38 
41 
43  SgAsmInstruction *getInstructionAtAddress(rose_addr_t addr) const {
44  AddressToInstructionMap::const_iterator i = addressToInstructionMap.find(addr);
45  if (i == addressToInstructionMap.end()) return NULL;
46  return i->second;
47  }
48 
49  /* NOTE: this is not the transpose of getPossiblePredecessors()! */
50  const AddressSet& getPossibleSuccessors(SgAsmInstruction* insn) const;
51 
52  const AddressSet& getPossiblePredecessors(SgAsmInstruction* insn) const {
53  static const AddressSet emptySet;
54  InstructionToAddressesMap::const_iterator predsIter = incomingEdges.find(insn);
55  if (predsIter == incomingEdges.end()) {
56  return emptySet;
57  } else {
58  return predsIter->second;
59  }
60  }
61  };
62 
63  class CFGNode {
64  SgAsmInstruction *node;
65  const AuxiliaryInformation *info;
66  public:
67  explicit CFGNode(SgAsmInstruction *node, const AuxiliaryInformation *info = NULL)
68  : node(node), info(info) {
69 #ifdef _MSC_VER
70 //#define __builtin_constant_p(exp) (0)
71 #endif
72  assert(node);
73  }
74  std::string toString() const;
75  // String for debugging graphs
76  std::string toStringForDebugging() const;
77  // ID to use for Dot, etc.
78  std::string id() const;
79 
80  SgAsmInstruction *getNode() const {
81  return node;
82  }
83 
84  std::vector<CFGEdge> outEdges() const;
85  std::vector<CFGEdge> inEdges() const;
86  bool operator==(const CFGNode& o) const {
87  return node == o.node;
88  }
89  bool operator!=(const CFGNode& o) const {
90  return !(*this == o);
91  }
92  bool operator<(const CFGNode& o) const {
93  return node < o.node;
94  }
95  };
96 
97  class CFGEdge {
98  CFGNode src, tgt;
99  const AuxiliaryInformation *info;
100  public:
101  CFGEdge(CFGNode src, CFGNode tgt, const AuxiliaryInformation *info = NULL)
102  : src(src), tgt(tgt), info(info)
103  {}
104  std::string toString() const; // Pretty string for Dot node labels, etc.
105  std::string toStringForDebugging() const; // String for debugging graphs
106  std::string id() const; // ID to use for Dot, etc.
107  CFGNode source() const {
108  return src;
109  }
110  CFGNode target() const {
111  return tgt;
112  }
113  EdgeConditionKind condition() const;
114  //SgExpression* caseLabel() const;
115  //SgExpression* conditionBasedOn() const;
116  //std::vector<SgInitializedName*> scopesBeingExited() const;
117  //std::vector<SgInitializedName*> scopesBeingEntered() const;
118  bool operator==(const CFGEdge& o) const {
119  return src == o.src && tgt == o.tgt;
120  }
121  bool operator!=(const CFGEdge& o) const {
122  return src != o.src || tgt != o.tgt;
123  }
124  bool operator<(const CFGEdge& o) const {
125  return src < o.src || (src == o.src && tgt < o.tgt);
126  }
127  };
128 
129  // Used in inEdges() and outEdges() methods
130  void makeEdge(SgAsmInstruction *from, SgAsmInstruction *to, const AuxiliaryInformation *info, std::vector<CFGEdge> &result);
131 }
132 
133 #endif
134 #endif
Base class for machine instructions.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9311
SgAsmInstruction * getInstructionAtAddress(rose_addr_t addr) const
Returns the instruction (if any) disassembled at the specified address.
Definition: virtualBinCFG.h:43
Base class for statement-like subclasses.