MCModule.h revision 263508
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
23namespace llvm {
24
25class MCAtom;
26class MCBasicBlock;
27class MCDataAtom;
28class MCFunction;
29class MCObjectDisassembler;
30class 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.
36class 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 Basic block tracking
60  /// @{
61  typedef std::vector<MCBasicBlock*> BBsByAtomTy;
62  BBsByAtomTy BBsByAtom;
63
64  // For access to basic block > atom tracking.
65  friend class MCBasicBlock;
66  friend class MCTextAtom;
67
68  /// \brief Keep track of \p BBBackedByAtom as being backed by \p Atom.
69  /// This is used to update succs/preds when \p Atom is split.
70  void trackBBForAtom(const MCTextAtom *Atom, MCBasicBlock *BBBackedByAtom);
71  void splitBasicBlocksForAtom(const MCTextAtom *TA, const MCTextAtom *NewTA);
72  /// @}
73
74  /// \name Function tracking
75  /// @{
76  typedef std::vector<MCFunction*> FunctionListTy;
77  FunctionListTy Functions;
78  /// @}
79
80  /// The address of the entrypoint function.
81  uint64_t Entrypoint;
82
83  MCModule           (const MCModule &) LLVM_DELETED_FUNCTION;
84  MCModule& operator=(const MCModule &) LLVM_DELETED_FUNCTION;
85
86  // MCObjectDisassembler creates MCModules.
87  friend class MCObjectDisassembler;
88
89public:
90  MCModule() : Entrypoint(0) { }
91  ~MCModule();
92
93  /// \name Create a new MCAtom covering the specified offset range.
94  /// @{
95  MCTextAtom *createTextAtom(uint64_t Begin, uint64_t End);
96  MCDataAtom *createDataAtom(uint64_t Begin, uint64_t End);
97  /// @}
98
99  /// \name Access to the owned atom list, ordered by begin address.
100  /// @{
101  const MCAtom *findAtomContaining(uint64_t Addr) const;
102        MCAtom *findAtomContaining(uint64_t Addr);
103  const MCAtom *findFirstAtomAfter(uint64_t Addr) const;
104        MCAtom *findFirstAtomAfter(uint64_t Addr);
105
106  typedef AtomListTy::const_iterator const_atom_iterator;
107  typedef AtomListTy::      iterator       atom_iterator;
108  const_atom_iterator atom_begin() const { return Atoms.begin(); }
109        atom_iterator atom_begin()       { return Atoms.begin(); }
110  const_atom_iterator atom_end()   const { return Atoms.end(); }
111        atom_iterator atom_end()         { return Atoms.end(); }
112  /// @}
113
114  /// \brief Create a new MCFunction.
115  MCFunction *createFunction(StringRef Name);
116
117  /// \name Access to the owned function list.
118  /// @{
119  typedef FunctionListTy::const_iterator const_func_iterator;
120  typedef FunctionListTy::      iterator       func_iterator;
121  const_func_iterator func_begin() const { return Functions.begin(); }
122        func_iterator func_begin()       { return Functions.begin(); }
123  const_func_iterator func_end()   const { return Functions.end(); }
124        func_iterator func_end()         { return Functions.end(); }
125  /// @}
126
127  /// \brief Get the address of the entrypoint function, or 0 if there is none.
128  uint64_t getEntrypoint() const { return Entrypoint; }
129};
130
131}
132
133#endif
134