MC CFG: Add a few needed methods, mainly MCModule::findFirstAtomAfter.
[oota-llvm.git] / include / llvm / MC / MCModule.h
1 //===-- llvm/MC/MCModule.h - MCModule class ---------------------*- C++ -*-===//
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 contains the declaration of the MCModule class, which is used to
11 // represent a complete, disassembled object file or executable.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_MC_MCMODULE_H
16 #define LLVM_MC_MCMODULE_H
17
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/DataTypes.h"
21 #include <vector>
22
23 namespace llvm {
24
25 class MCAtom;
26 class MCBasicBlock;
27 class MCDataAtom;
28 class MCFunction;
29 class MCObjectDisassembler;
30 class MCTextAtom;
31
32 /// \brief A completely disassembled object file or executable.
33 /// It comprises a list of MCAtom's, each representing a contiguous range of
34 /// either instructions or data.
35 /// An MCModule is created using MCObjectDisassembler::buildModule.
36 class MCModule {
37   /// \name Atom tracking
38   /// @{
39
40   /// \brief Atoms in this module, sorted by begin address.
41   /// FIXME: This doesn't handle overlapping atoms (which happen when a basic
42   /// block starts in the middle of an instruction of another basic block.)
43   typedef std::vector<MCAtom*> AtomListTy;
44   AtomListTy Atoms;
45
46   // For access to map/remap.
47   friend class MCAtom;
48
49   /// \brief Remap \p Atom to the given range, and update its Begin/End fields.
50   /// \param Atom An atom belonging to this module.
51   /// An atom should always use this method to update its bounds, because this
52   /// enables the owning MCModule to keep track of its atoms.
53   void remap(MCAtom *Atom, uint64_t NewBegin, uint64_t NewEnd);
54
55   /// \brief Insert an atom in the module, using its Begin and End addresses.
56   void map(MCAtom *NewAtom);
57   /// @}
58
59   /// \name Function tracking
60   /// @{
61   typedef std::vector<MCFunction*> FunctionListTy;
62   FunctionListTy Functions;
63   /// @}
64
65   /// The address of the entrypoint function.
66   uint64_t Entrypoint;
67
68   MCModule           (const MCModule &) LLVM_DELETED_FUNCTION;
69   MCModule& operator=(const MCModule &) LLVM_DELETED_FUNCTION;
70
71   // MCObjectDisassembler creates MCModules.
72   friend class MCObjectDisassembler;
73
74 public:
75   MCModule() : Entrypoint(0) { }
76   ~MCModule();
77
78   /// \name Create a new MCAtom covering the specified offset range.
79   /// @{
80   MCTextAtom *createTextAtom(uint64_t Begin, uint64_t End);
81   MCDataAtom *createDataAtom(uint64_t Begin, uint64_t End);
82   /// @}
83
84   /// \name Access to the owned atom list, ordered by begin address.
85   /// @{
86   const MCAtom *findAtomContaining(uint64_t Addr) const;
87         MCAtom *findAtomContaining(uint64_t Addr);
88   const MCAtom *findFirstAtomAfter(uint64_t Addr) const;
89         MCAtom *findFirstAtomAfter(uint64_t Addr);
90
91   typedef AtomListTy::const_iterator const_atom_iterator;
92   typedef AtomListTy::      iterator       atom_iterator;
93   const_atom_iterator atom_begin() const { return Atoms.begin(); }
94         atom_iterator atom_begin()       { return Atoms.begin(); }
95   const_atom_iterator atom_end()   const { return Atoms.end(); }
96         atom_iterator atom_end()         { return Atoms.end(); }
97   /// @}
98
99   /// \brief Create a new MCFunction.
100   MCFunction *createFunction(StringRef Name);
101
102   /// \name Access to the owned function list.
103   /// @{
104   typedef FunctionListTy::const_iterator const_func_iterator;
105   typedef FunctionListTy::      iterator       func_iterator;
106   const_func_iterator func_begin() const { return Functions.begin(); }
107         func_iterator func_begin()       { return Functions.begin(); }
108   const_func_iterator func_end()   const { return Functions.end(); }
109         func_iterator func_end()         { return Functions.end(); }
110   /// @}
111
112   /// \brief Get the address of the entrypoint function, or 0 if there is none.
113   uint64_t getEntrypoint() const { return Entrypoint; }
114 };
115
116 }
117
118 #endif