Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 1 | //===-- MCFunction.h ------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the data structures to hold a CFG reconstructed from |
| 11 | // machine code. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 15 | #ifndef LLVM_OBJECTDUMP_MCFUNCTION_H |
| 16 | #define LLVM_OBJECTDUMP_MCFUNCTION_H |
| 17 | |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/ArrayRef.h" |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCInst.h" |
| 21 | #include <map> |
| 22 | |
| 23 | namespace llvm { |
| 24 | |
| 25 | class MCDisassembler; |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame] | 26 | class MCInstrAnalysis; |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 27 | class MemoryObject; |
| 28 | class raw_ostream; |
| 29 | |
| 30 | /// MCDecodedInst - Small container to hold an MCInst and associated info like |
| 31 | /// address and size. |
| 32 | struct MCDecodedInst { |
| 33 | uint64_t Address; |
| 34 | uint64_t Size; |
| 35 | MCInst Inst; |
| 36 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 37 | MCDecodedInst() {} |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 38 | MCDecodedInst(uint64_t Address, uint64_t Size, MCInst Inst) |
| 39 | : Address(Address), Size(Size), Inst(Inst) {} |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 40 | |
| 41 | bool operator<(const MCDecodedInst &RHS) const { |
| 42 | return Address < RHS.Address; |
| 43 | } |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /// MCBasicBlock - Consists of multiple MCDecodedInsts and a list of successing |
| 47 | /// MCBasicBlocks. |
| 48 | class MCBasicBlock { |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 49 | std::vector<MCDecodedInst> Insts; |
| 50 | typedef DenseSet<uint64_t> SetTy; |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 51 | SetTy Succs; |
| 52 | public: |
| 53 | ArrayRef<MCDecodedInst> getInsts() const { return Insts; } |
| 54 | |
| 55 | typedef SetTy::const_iterator succ_iterator; |
| 56 | succ_iterator succ_begin() const { return Succs.begin(); } |
| 57 | succ_iterator succ_end() const { return Succs.end(); } |
| 58 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 59 | bool contains(uint64_t Addr) const { return Succs.count(Addr); } |
Benjamin Kramer | dbc46d7 | 2011-07-25 23:10:23 +0000 | [diff] [blame] | 60 | |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 61 | void addInst(const MCDecodedInst &Inst) { Insts.push_back(Inst); } |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 62 | void addSucc(uint64_t Addr) { Succs.insert(Addr); } |
| 63 | |
| 64 | bool operator<(const MCBasicBlock &RHS) const { |
| 65 | return Insts.size() < RHS.Insts.size(); |
| 66 | } |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /// MCFunction - Represents a named function in machine code, containing |
| 70 | /// multiple MCBasicBlocks. |
| 71 | class MCFunction { |
| 72 | const StringRef Name; |
| 73 | // Keep BBs sorted by address. |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 74 | typedef std::vector<std::pair<uint64_t, MCBasicBlock> > MapTy; |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 75 | MapTy Blocks; |
| 76 | public: |
| 77 | MCFunction(StringRef Name) : Name(Name) {} |
| 78 | |
| 79 | // Create an MCFunction from a region of binary machine code. |
| 80 | static MCFunction |
| 81 | createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm, |
Derek Schuff | adef06a | 2012-02-29 01:09:06 +0000 | [diff] [blame] | 82 | const MemoryObject &Region, uint64_t Start, uint64_t End, |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 83 | const MCInstrAnalysis *Ana, raw_ostream &DebugOut, |
| 84 | SmallVectorImpl<uint64_t> &Calls); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 85 | |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 86 | typedef MapTy::const_iterator iterator; |
| 87 | iterator begin() const { return Blocks.begin(); } |
| 88 | iterator end() const { return Blocks.end(); } |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 89 | |
| 90 | StringRef getName() const { return Name; } |
| 91 | |
| 92 | MCBasicBlock &addBlock(uint64_t Address, const MCBasicBlock &BB) { |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 93 | Blocks.push_back(std::make_pair(Address, BB)); |
| 94 | return Blocks.back().second; |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 95 | } |
| 96 | }; |
| 97 | |
| 98 | } |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 99 | |
| 100 | #endif |