MCAsmLayout.h 3.6 KB

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