MCSection.h 6.9 KB

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