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