1226584Sdim//===-- llvm/MC/MCModule.h - MCModule class ---------------------*- C++ -*-===//
2226584Sdim//
3226584Sdim//                     The LLVM Compiler Infrastructure
4226584Sdim//
5226584Sdim// This file is distributed under the University of Illinois Open Source
6226584Sdim// License. See LICENSE.TXT for details.
7226584Sdim//
8226584Sdim//===----------------------------------------------------------------------===//
9226584Sdim//
10226584Sdim// This file contains the declaration of the MCModule class, which is used to
11226584Sdim// represent a complete, disassembled object file or executable.
12226584Sdim//
13226584Sdim//===----------------------------------------------------------------------===//
14226584Sdim
15226584Sdim#ifndef LLVM_MC_MCMODULE_H
16226584Sdim#define LLVM_MC_MCMODULE_H
17226584Sdim
18263508Sdim#include "llvm/ADT/StringRef.h"
19263508Sdim#include "llvm/Support/Compiler.h"
20226584Sdim#include "llvm/Support/DataTypes.h"
21263508Sdim#include <vector>
22226584Sdim
23226584Sdimnamespace llvm {
24226584Sdim
25226584Sdimclass MCAtom;
26263508Sdimclass MCBasicBlock;
27263508Sdimclass MCDataAtom;
28263508Sdimclass MCFunction;
29263508Sdimclass MCObjectDisassembler;
30263508Sdimclass MCTextAtom;
31226584Sdim
32263508Sdim/// \brief A completely disassembled object file or executable.
33263508Sdim/// It comprises a list of MCAtom's, each representing a contiguous range of
34263508Sdim/// either instructions or data.
35263508Sdim/// An MCModule is created using MCObjectDisassembler::buildModule.
36226584Sdimclass MCModule {
37263508Sdim  /// \name Atom tracking
38263508Sdim  /// @{
39226584Sdim
40263508Sdim  /// \brief Atoms in this module, sorted by begin address.
41263508Sdim  /// FIXME: This doesn't handle overlapping atoms (which happen when a basic
42263508Sdim  /// block starts in the middle of an instruction of another basic block.)
43263508Sdim  typedef std::vector<MCAtom*> AtomListTy;
44263508Sdim  AtomListTy Atoms;
45226584Sdim
46263508Sdim  // For access to map/remap.
47226584Sdim  friend class MCAtom;
48226584Sdim
49263508Sdim  /// \brief Remap \p Atom to the given range, and update its Begin/End fields.
50263508Sdim  /// \param Atom An atom belonging to this module.
51263508Sdim  /// An atom should always use this method to update its bounds, because this
52263508Sdim  /// enables the owning MCModule to keep track of its atoms.
53226584Sdim  void remap(MCAtom *Atom, uint64_t NewBegin, uint64_t NewEnd);
54226584Sdim
55263508Sdim  /// \brief Insert an atom in the module, using its Begin and End addresses.
56263508Sdim  void map(MCAtom *NewAtom);
57263508Sdim  /// @}
58263508Sdim
59263508Sdim  /// \name Basic block tracking
60263508Sdim  /// @{
61263508Sdim  typedef std::vector<MCBasicBlock*> BBsByAtomTy;
62263508Sdim  BBsByAtomTy BBsByAtom;
63263508Sdim
64263508Sdim  // For access to basic block > atom tracking.
65263508Sdim  friend class MCBasicBlock;
66263508Sdim  friend class MCTextAtom;
67263508Sdim
68263508Sdim  /// \brief Keep track of \p BBBackedByAtom as being backed by \p Atom.
69263508Sdim  /// This is used to update succs/preds when \p Atom is split.
70263508Sdim  void trackBBForAtom(const MCTextAtom *Atom, MCBasicBlock *BBBackedByAtom);
71263508Sdim  void splitBasicBlocksForAtom(const MCTextAtom *TA, const MCTextAtom *NewTA);
72263508Sdim  /// @}
73263508Sdim
74263508Sdim  /// \name Function tracking
75263508Sdim  /// @{
76263508Sdim  typedef std::vector<MCFunction*> FunctionListTy;
77263508Sdim  FunctionListTy Functions;
78263508Sdim  /// @}
79263508Sdim
80263508Sdim  /// The address of the entrypoint function.
81263508Sdim  uint64_t Entrypoint;
82263508Sdim
83263508Sdim  MCModule           (const MCModule &) LLVM_DELETED_FUNCTION;
84263508Sdim  MCModule& operator=(const MCModule &) LLVM_DELETED_FUNCTION;
85263508Sdim
86263508Sdim  // MCObjectDisassembler creates MCModules.
87263508Sdim  friend class MCObjectDisassembler;
88263508Sdim
89226584Sdimpublic:
90263508Sdim  MCModule() : Entrypoint(0) { }
91263508Sdim  ~MCModule();
92226584Sdim
93263508Sdim  /// \name Create a new MCAtom covering the specified offset range.
94263508Sdim  /// @{
95263508Sdim  MCTextAtom *createTextAtom(uint64_t Begin, uint64_t End);
96263508Sdim  MCDataAtom *createDataAtom(uint64_t Begin, uint64_t End);
97263508Sdim  /// @}
98263508Sdim
99263508Sdim  /// \name Access to the owned atom list, ordered by begin address.
100263508Sdim  /// @{
101263508Sdim  const MCAtom *findAtomContaining(uint64_t Addr) const;
102263508Sdim        MCAtom *findAtomContaining(uint64_t Addr);
103263508Sdim  const MCAtom *findFirstAtomAfter(uint64_t Addr) const;
104263508Sdim        MCAtom *findFirstAtomAfter(uint64_t Addr);
105263508Sdim
106263508Sdim  typedef AtomListTy::const_iterator const_atom_iterator;
107263508Sdim  typedef AtomListTy::      iterator       atom_iterator;
108263508Sdim  const_atom_iterator atom_begin() const { return Atoms.begin(); }
109263508Sdim        atom_iterator atom_begin()       { return Atoms.begin(); }
110263508Sdim  const_atom_iterator atom_end()   const { return Atoms.end(); }
111263508Sdim        atom_iterator atom_end()         { return Atoms.end(); }
112263508Sdim  /// @}
113263508Sdim
114263508Sdim  /// \brief Create a new MCFunction.
115263508Sdim  MCFunction *createFunction(StringRef Name);
116263508Sdim
117263508Sdim  /// \name Access to the owned function list.
118263508Sdim  /// @{
119263508Sdim  typedef FunctionListTy::const_iterator const_func_iterator;
120263508Sdim  typedef FunctionListTy::      iterator       func_iterator;
121263508Sdim  const_func_iterator func_begin() const { return Functions.begin(); }
122263508Sdim        func_iterator func_begin()       { return Functions.begin(); }
123263508Sdim  const_func_iterator func_end()   const { return Functions.end(); }
124263508Sdim        func_iterator func_end()         { return Functions.end(); }
125263508Sdim  /// @}
126263508Sdim
127263508Sdim  /// \brief Get the address of the entrypoint function, or 0 if there is none.
128263508Sdim  uint64_t getEntrypoint() const { return Entrypoint; }
129226584Sdim};
130226584Sdim
131226584Sdim}
132226584Sdim
133226584Sdim#endif
134