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