MCFunction.h revision 41ab14b725c8f2bb3e54553d0d7d96ff184786b1
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
20namespace llvm {
21
22class MCDisassembler;
23class MCInstrAnalysis;
24class MemoryObject;
25class raw_ostream;
26
27/// MCDecodedInst - Small container to hold an MCInst and associated info like
28/// address and size.
29struct 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.
40class MCBasicBlock {
41  SmallVector<MCDecodedInst, 8> Insts;
42  typedef SmallPtrSet<MCBasicBlock*, 8> SetTy;
43  SetTy Succs;
44public:
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.
59class MCFunction {
60  const StringRef Name;
61  // Keep BBs sorted by address.
62  typedef std::map<uint64_t, MCBasicBlock> MapTy;
63  MapTy Blocks;
64public:
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}
91