MCSection.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //===- MCSection.h - Machine Code Sections ----------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file declares the MCSection class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_MC_MCSECTION_H
  13. #define LLVM_MC_MCSECTION_H
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/ilist.h"
  16. #include "llvm/MC/MCFragment.h"
  17. #include "llvm/MC/SectionKind.h"
  18. #include "llvm/Support/Alignment.h"
  19. #include <cassert>
  20. #include <utility>
  21. namespace llvm {
  22. class MCAsmInfo;
  23. class MCContext;
  24. class MCExpr;
  25. class MCSymbol;
  26. class raw_ostream;
  27. class Triple;
  28. template <> struct ilist_alloc_traits<MCFragment> {
  29. static void deleteNode(MCFragment *V);
  30. };
  31. /// Instances of this class represent a uniqued identifier for a section in the
  32. /// current translation unit. The MCContext class uniques and creates these.
  33. class MCSection {
  34. public:
  35. static constexpr unsigned NonUniqueID = ~0U;
  36. enum SectionVariant { SV_COFF = 0, SV_ELF, SV_MachO, SV_Wasm, SV_XCOFF };
  37. /// Express the state of bundle locked groups while emitting code.
  38. enum BundleLockStateType {
  39. NotBundleLocked,
  40. BundleLocked,
  41. BundleLockedAlignToEnd
  42. };
  43. using FragmentListType = iplist<MCFragment>;
  44. using const_iterator = FragmentListType::const_iterator;
  45. using iterator = FragmentListType::iterator;
  46. using const_reverse_iterator = FragmentListType::const_reverse_iterator;
  47. using reverse_iterator = FragmentListType::reverse_iterator;
  48. private:
  49. MCSymbol *Begin;
  50. MCSymbol *End = nullptr;
  51. /// The alignment requirement of this section.
  52. Align Alignment;
  53. /// The section index in the assemblers section list.
  54. unsigned Ordinal = 0;
  55. /// The index of this section in the layout order.
  56. unsigned LayoutOrder;
  57. /// Keeping track of bundle-locked state.
  58. BundleLockStateType BundleLockState = NotBundleLocked;
  59. /// Current nesting depth of bundle_lock directives.
  60. unsigned BundleLockNestingDepth = 0;
  61. /// We've seen a bundle_lock directive but not its first instruction
  62. /// yet.
  63. bool BundleGroupBeforeFirstInst : 1;
  64. /// Whether this section has had instructions emitted into it.
  65. bool HasInstructions : 1;
  66. bool IsRegistered : 1;
  67. MCDummyFragment DummyFragment;
  68. FragmentListType Fragments;
  69. /// Mapping from subsection number to insertion point for subsection numbers
  70. /// below that number.
  71. SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
  72. /// State for tracking labels that don't yet have Fragments
  73. struct PendingLabel {
  74. MCSymbol* Sym;
  75. unsigned Subsection;
  76. PendingLabel(MCSymbol* Sym, unsigned Subsection = 0)
  77. : Sym(Sym), Subsection(Subsection) {}
  78. };
  79. SmallVector<PendingLabel, 2> PendingLabels;
  80. protected:
  81. // TODO Make Name private when possible.
  82. StringRef Name;
  83. SectionVariant Variant;
  84. SectionKind Kind;
  85. MCSection(SectionVariant V, StringRef Name, SectionKind K, MCSymbol *Begin);
  86. ~MCSection();
  87. public:
  88. MCSection(const MCSection &) = delete;
  89. MCSection &operator=(const MCSection &) = delete;
  90. StringRef getName() const { return Name; }
  91. SectionKind getKind() const { return Kind; }
  92. SectionVariant getVariant() const { return Variant; }
  93. MCSymbol *getBeginSymbol() { return Begin; }
  94. const MCSymbol *getBeginSymbol() const {
  95. return const_cast<MCSection *>(this)->getBeginSymbol();
  96. }
  97. void setBeginSymbol(MCSymbol *Sym) {
  98. assert(!Begin);
  99. Begin = Sym;
  100. }
  101. MCSymbol *getEndSymbol(MCContext &Ctx);
  102. bool hasEnded() const;
  103. unsigned getAlignment() const { return Alignment.value(); }
  104. void setAlignment(Align Value) { Alignment = Value; }
  105. unsigned getOrdinal() const { return Ordinal; }
  106. void setOrdinal(unsigned Value) { Ordinal = Value; }
  107. unsigned getLayoutOrder() const { return LayoutOrder; }
  108. void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
  109. BundleLockStateType getBundleLockState() const { return BundleLockState; }
  110. void setBundleLockState(BundleLockStateType NewState);
  111. bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
  112. bool isBundleGroupBeforeFirstInst() const {
  113. return BundleGroupBeforeFirstInst;
  114. }
  115. void setBundleGroupBeforeFirstInst(bool IsFirst) {
  116. BundleGroupBeforeFirstInst = IsFirst;
  117. }
  118. bool hasInstructions() const { return HasInstructions; }
  119. void setHasInstructions(bool Value) { HasInstructions = Value; }
  120. bool isRegistered() const { return IsRegistered; }
  121. void setIsRegistered(bool Value) { IsRegistered = Value; }
  122. MCSection::FragmentListType &getFragmentList() { return Fragments; }
  123. const MCSection::FragmentListType &getFragmentList() const {
  124. return const_cast<MCSection *>(this)->getFragmentList();
  125. }
  126. /// Support for MCFragment::getNextNode().
  127. static FragmentListType MCSection::*getSublistAccess(MCFragment *) {
  128. return &MCSection::Fragments;
  129. }
  130. const MCDummyFragment &getDummyFragment() const { return DummyFragment; }
  131. MCDummyFragment &getDummyFragment() { return DummyFragment; }
  132. iterator begin() { return Fragments.begin(); }
  133. const_iterator begin() const { return Fragments.begin(); }
  134. iterator end() { return Fragments.end(); }
  135. const_iterator end() const { return Fragments.end(); }
  136. MCSection::iterator getSubsectionInsertionPoint(unsigned Subsection);
  137. void dump() const;
  138. virtual void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
  139. raw_ostream &OS,
  140. const MCExpr *Subsection) const = 0;
  141. /// Return true if a .align directive should use "optimized nops" to fill
  142. /// instead of 0s.
  143. virtual bool UseCodeAlign() const = 0;
  144. /// Check whether this section is "virtual", that is has no actual object
  145. /// file contents.
  146. virtual bool isVirtualSection() const = 0;
  147. virtual StringRef getVirtualSectionKind() const;
  148. /// Add a pending label for the requested subsection. This label will be
  149. /// associated with a fragment in flushPendingLabels()
  150. void addPendingLabel(MCSymbol* label, unsigned Subsection = 0);
  151. /// Associate all pending labels in a subsection with a fragment.
  152. void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0,
  153. unsigned Subsection = 0);
  154. /// Associate all pending labels with empty data fragments. One fragment
  155. /// will be created for each subsection as necessary.
  156. void flushPendingLabels();
  157. };
  158. } // end namespace llvm
  159. #endif // LLVM_MC_MCSECTION_H