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