• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- MCAsmLayout.h - Assembly Layout Object -------------------*- 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 #ifndef LLVM_MC_MCASMLAYOUT_H
11 #define LLVM_MC_MCASMLAYOUT_H
12 
13 #include "llvm/ADT/SmallVector.h"
14 
15 namespace llvm {
16 class MCAssembler;
17 class MCFragment;
18 class MCSectionData;
19 class MCSymbolData;
20 
21 /// Encapsulates the layout of an assembly file at a particular point in time.
22 ///
23 /// Assembly may requiring compute multiple layouts for a particular assembly
24 /// file as part of the relaxation process. This class encapsulates the layout
25 /// at a single point in time in such a way that it is always possible to
26 /// efficiently compute the exact addresses of any symbol in the assembly file,
27 /// even during the relaxation process.
28 class MCAsmLayout {
29 public:
30   typedef llvm::SmallVectorImpl<MCSectionData*>::const_iterator const_iterator;
31   typedef llvm::SmallVectorImpl<MCSectionData*>::iterator iterator;
32 
33 private:
34   MCAssembler &Assembler;
35 
36   /// List of sections in layout order.
37   llvm::SmallVector<MCSectionData*, 16> SectionOrder;
38 
39   /// The last fragment which was laid out, or 0 if nothing has been laid
40   /// out. Fragments are always laid out in order, so all fragments with a
41   /// lower ordinal will be up to date.
42   mutable DenseMap<const MCSectionData*, MCFragment *> LastValidFragment;
43 
44   /// \brief Make sure that the layout for the given fragment is valid, lazily
45   /// computing it if necessary.
46   void EnsureValid(const MCFragment *F) const;
47 
48   bool isFragmentUpToDate(const MCFragment *F) const;
49 
50 public:
51   MCAsmLayout(MCAssembler &_Assembler);
52 
53   /// Get the assembler object this is a layout for.
getAssembler()54   MCAssembler &getAssembler() const { return Assembler; }
55 
56   /// \brief Invalidate all following fragments because a fragment has been
57   /// resized. The fragments size should have already been updated.
58   void Invalidate(MCFragment *F);
59 
60   /// \brief Perform layout for a single fragment, assuming that the previous
61   /// fragment has already been laid out correctly, and the parent section has
62   /// been initialized.
63   void LayoutFragment(MCFragment *Fragment);
64 
65   /// @name Section Access (in layout order)
66   /// @{
67 
getSectionOrder()68   llvm::SmallVectorImpl<MCSectionData*> &getSectionOrder() {
69     return SectionOrder;
70   }
getSectionOrder()71   const llvm::SmallVectorImpl<MCSectionData*> &getSectionOrder() const {
72     return SectionOrder;
73   }
74 
75   /// @}
76   /// @name Fragment Layout Data
77   /// @{
78 
79   /// \brief Get the offset of the given fragment inside its containing section.
80   uint64_t getFragmentOffset(const MCFragment *F) const;
81 
82   /// @}
83   /// @name Utility Functions
84   /// @{
85 
86   /// \brief Get the address space size of the given section, as it effects
87   /// layout. This may differ from the size reported by \see getSectionSize() by
88   /// not including section tail padding.
89   uint64_t getSectionAddressSize(const MCSectionData *SD) const;
90 
91   /// \brief Get the data size of the given section, as emitted to the object
92   /// file. This may include additional padding, or be 0 for virtual sections.
93   uint64_t getSectionFileSize(const MCSectionData *SD) const;
94 
95   /// \brief Get the offset of the given symbol, as computed in the current
96   /// layout.
97   uint64_t getSymbolOffset(const MCSymbolData *SD) const;
98 
99   /// @}
100 };
101 
102 } // end namespace llvm
103 
104 #endif
105