Add MCInstrAnalysis class. This allows the targets to specify own versions of MCInstr...
[oota-llvm.git] / tools / llvm-objdump / MCFunction.h
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
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/MC/MCInst.h"
18 #include <map>
19
20 namespace llvm {
21
22 class MCDisassembler;
23 class MCInstrAnalysis;
24 class MemoryObject;
25 class raw_ostream;
26
27 /// MCDecodedInst - Small container to hold an MCInst and associated info like
28 /// address and size.
29 struct MCDecodedInst {
30   uint64_t Address;
31   uint64_t Size;
32   MCInst Inst;
33
34   MCDecodedInst(uint64_t Address, uint64_t Size, MCInst Inst)
35     : Address(Address), Size(Size), Inst(Inst) {}
36 };
37
38 /// MCBasicBlock - Consists of multiple MCDecodedInsts and a list of successing
39 /// MCBasicBlocks.
40 class MCBasicBlock {
41   SmallVector<MCDecodedInst, 8> Insts;
42   typedef SmallPtrSet<MCBasicBlock*, 8> SetTy;
43   SetTy Succs;
44 public:
45   ArrayRef<MCDecodedInst> getInsts() const { return Insts; }
46
47   typedef SetTy::const_iterator succ_iterator;
48   succ_iterator succ_begin() const { return Succs.begin(); }
49   succ_iterator succ_end() const { return Succs.end(); }
50
51   bool contains(MCBasicBlock *BB) const { return Succs.count(BB); }
52
53   void addInst(const MCDecodedInst &Inst) { Insts.push_back(Inst); }
54   void addSucc(MCBasicBlock *BB) { Succs.insert(BB); }
55 };
56
57 /// MCFunction - Represents a named function in machine code, containing
58 /// multiple MCBasicBlocks.
59 class MCFunction {
60   const StringRef Name;
61   // Keep BBs sorted by address.
62   typedef std::map<uint64_t, MCBasicBlock> MapTy;
63   MapTy Blocks;
64 public:
65   MCFunction(StringRef Name) : Name(Name) {}
66
67   // Create an MCFunction from a region of binary machine code.
68   static MCFunction
69   createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
70                        const MemoryObject &Region, uint64_t Start, uint64_t End,
71                        const MCInstrAnalysis *Ana, raw_ostream &DebugOut);
72
73   typedef MapTy::iterator iterator;
74   iterator begin() { return Blocks.begin(); }
75   iterator end() { return Blocks.end(); }
76
77   StringRef getName() const { return Name; }
78
79   MCBasicBlock &addBlock(uint64_t Address, const MCBasicBlock &BB) {
80     assert(!Blocks.count(Address) && "Already a BB at address.");
81     return Blocks[Address] = BB;
82   }
83
84   MCBasicBlock &getBlockAtAddress(uint64_t Address) {
85     assert(Blocks.count(Address) && "No BB at address.");
86     return Blocks[Address];
87   }
88 };
89
90 }