00001 #ifndef VIRTUAL_BIN_CFG_H
00002 #define VIRTUAL_BIN_CFG_H
00003
00004 #include <stdint.h>
00005
00006
00007 class SgAsmInstruction;
00008 class SgAsmStatement;
00009
00010 namespace VirtualBinCFG {
00011
00012 class CFGEdge;
00013
00014 enum EdgeConditionKind
00015 {
00016 eckUnconditional,
00017 eckTrue,
00018 eckFalse,
00019 eckCaseLabel,
00020 eckDefault
00021 };
00022
00023 typedef std::set<rose_addr_t> AddressSet;
00024 typedef std::map<rose_addr_t, SgAsmInstruction*> AddressToInstructionMap;
00025 typedef std::map<SgAsmInstruction*, AddressSet> InstructionToAddressesMap;
00026 typedef std::map<SgAsmStatement*, AddressSet> StatementToAddressesMap;
00027
00028 struct AuxiliaryInformation {
00029 AddressToInstructionMap addressToInstructionMap;
00030 InstructionToAddressesMap indirectJumpTargets;
00031 StatementToAddressesMap returnTargets;
00032 InstructionToAddressesMap incomingEdges;
00033
00034 public:
00035
00036 AuxiliaryInformation() {}
00037 AuxiliaryInformation(SgNode* top);
00038
00040 SgAsmInstruction *getInstructionAtAddress(rose_addr_t addr) const {
00041 AddressToInstructionMap::const_iterator i = addressToInstructionMap.find(addr);
00042 if (i == addressToInstructionMap.end()) return NULL;
00043 return i->second;
00044 }
00045
00046
00047 const AddressSet& getPossibleSuccessors(SgAsmInstruction* insn) const;
00048
00049 const AddressSet& getPossiblePredecessors(SgAsmInstruction* insn) const {
00050 static const AddressSet emptySet;
00051 InstructionToAddressesMap::const_iterator predsIter = incomingEdges.find(insn);
00052 if (predsIter == incomingEdges.end()) {
00053 return emptySet;
00054 } else {
00055 return predsIter->second;
00056 }
00057 }
00058 };
00059
00060 class CFGNode {
00061 SgAsmInstruction *node;
00062 const AuxiliaryInformation *info;
00063 public:
00064 explicit CFGNode(SgAsmInstruction *node, const AuxiliaryInformation *info = NULL)
00065 : node(node), info(info) {
00066 #ifdef _MSC_VER
00067
00068 #endif
00069 assert(node);
00070 }
00071 std::string toString() const;
00072
00073 std::string toStringForDebugging() const;
00074
00075 std::string id() const;
00076
00077 SgAsmInstruction *getNode() const {
00078 return node;
00079 }
00080
00081 std::vector<CFGEdge> outEdges() const;
00082 std::vector<CFGEdge> inEdges() const;
00083 bool operator==(const CFGNode& o) const {
00084 return node == o.node;
00085 }
00086 bool operator!=(const CFGNode& o) const {
00087 return !(*this == o);
00088 }
00089 bool operator<(const CFGNode& o) const {
00090 return node < o.node;
00091 }
00092 };
00093
00094 class CFGEdge {
00095 CFGNode src, tgt;
00096 const AuxiliaryInformation *info;
00097 public:
00098 CFGEdge(CFGNode src, CFGNode tgt, const AuxiliaryInformation *info = NULL)
00099 : src(src), tgt(tgt), info(info)
00100 {}
00101 std::string toString() const;
00102 std::string toStringForDebugging() const;
00103 std::string id() const;
00104 CFGNode source() const {
00105 return src;
00106 }
00107 CFGNode target() const {
00108 return tgt;
00109 }
00110 EdgeConditionKind condition() const;
00111
00112
00113
00114
00115 bool operator==(const CFGEdge& o) const {
00116 return src == o.src && tgt == o.tgt;
00117 }
00118 bool operator!=(const CFGEdge& o) const {
00119 return src != o.src || tgt != o.tgt;
00120 }
00121 bool operator<(const CFGEdge& o) const {
00122 return src < o.src || (src == o.src && tgt < o.tgt);
00123 }
00124 };
00125
00126
00127 void makeEdge(SgAsmInstruction *from, SgAsmInstruction *to, const AuxiliaryInformation *info, std::vector<CFGEdge> &result);
00128 }
00129
00130 #endif