Invalidate the layout on any relaxation, not just Instructions. Bug found by David...
[oota-llvm.git] / include / llvm / MC / MCAsmLayout.h
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 layed out, or 0 if nothing has been layed
40   /// out. Fragments are always layed out in order, so all fragments with a
41   /// lower ordinal will be up to date.
42   mutable 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 isSectionUpToDate(const MCSectionData *SD) const;
49   bool isFragmentUpToDate(const MCFragment *F) const;
50
51 public:
52   MCAsmLayout(MCAssembler &_Assembler);
53
54   /// Get the assembler object this is a layout for.
55   MCAssembler &getAssembler() const { return Assembler; }
56
57   /// \brief Invalidate all following fragments because a fragment has been resized. The
58   /// fragments size should have already been updated.
59   void Invalidate(MCFragment *F);
60
61   /// \brief Update the layout, replacing Src with Dst. The contents
62   /// of Src and Dst are not modified, and must be copied by the caller.
63   /// Src will be removed from the layout, but not deleted.
64   void ReplaceFragment(MCFragment *Src, MCFragment *Dst);
65
66   /// \brief Update the layout to coalesce Src into Dst. The contents
67   /// of Src and Dst are not modified, and must be coalesced by the caller.
68   /// Src will be removed from the layout, but not deleted.
69   void CoalesceFragments(MCFragment *Src, MCFragment *Dst);
70
71   /// \brief Perform a full layout.
72   void LayoutFile();
73
74   /// \brief Perform layout for a single fragment, assuming that the previous
75   /// fragment has already been layed out correctly, and the parent section has
76   /// been initialized.
77   void LayoutFragment(MCFragment *Fragment);
78
79   /// \brief Performs initial layout for a single section, assuming that the
80   /// previous section (including its fragments) has already been layed out
81   /// correctly.
82   void LayoutSection(MCSectionData *SD);
83
84   /// @name Section Access (in layout order)
85   /// @{
86
87   llvm::SmallVectorImpl<MCSectionData*> &getSectionOrder() {
88     return SectionOrder;
89   }
90   const llvm::SmallVectorImpl<MCSectionData*> &getSectionOrder() const {
91     return SectionOrder;
92   }
93
94   /// @}
95   /// @name Fragment Layout Data
96   /// @{
97
98   /// \brief Get the effective size of the given fragment, as computed in the
99   /// current layout.
100   uint64_t getFragmentEffectiveSize(const MCFragment *F) const;
101
102   /// \brief Get the offset of the given fragment inside its containing section.
103   uint64_t getFragmentOffset(const MCFragment *F) const;
104
105   /// @}
106   /// @name Section Layout Data
107   /// @{
108
109   /// \brief Get the computed address of the given section.
110   uint64_t getSectionAddress(const MCSectionData *SD) const;
111
112   /// @}
113   /// @name Utility Functions
114   /// @{
115
116   /// \brief Get the address of the given fragment, as computed in the current
117   /// layout.
118   uint64_t getFragmentAddress(const MCFragment *F) const;
119
120   /// \brief Get the address space size of the given section, as it effects
121   /// layout. This may differ from the size reported by \see getSectionSize() by
122   /// not including section tail padding.
123   uint64_t getSectionAddressSize(const MCSectionData *SD) const;
124
125   /// \brief Get the data size of the given section, as emitted to the object
126   /// file. This may include additional padding, or be 0 for virtual sections.
127   uint64_t getSectionFileSize(const MCSectionData *SD) const;
128
129   /// \brief Get the logical data size of the given section.
130   uint64_t getSectionSize(const MCSectionData *SD) const;
131
132   /// \brief Get the address of the given symbol, as computed in the current
133   /// layout.
134   uint64_t getSymbolAddress(const MCSymbolData *SD) const;
135
136   /// @}
137 };
138
139 } // end namespace llvm
140
141 #endif