MCSection.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. };
  49. /// Express the state of bundle locked groups while emitting code.
  50. enum BundleLockStateType {
  51. NotBundleLocked,
  52. BundleLocked,
  53. BundleLockedAlignToEnd
  54. };
  55. using FragmentListType = iplist<MCFragment>;
  56. using const_iterator = FragmentListType::const_iterator;
  57. using iterator = FragmentListType::iterator;
  58. using const_reverse_iterator = FragmentListType::const_reverse_iterator;
  59. using reverse_iterator = FragmentListType::reverse_iterator;
  60. private:
  61. MCSymbol *Begin;
  62. MCSymbol *End = nullptr;
  63. /// The alignment requirement of this section.
  64. Align Alignment;
  65. /// The section index in the assemblers section list.
  66. unsigned Ordinal = 0;
  67. /// The index of this section in the layout order.
  68. unsigned LayoutOrder;
  69. /// Keeping track of bundle-locked state.
  70. BundleLockStateType BundleLockState = NotBundleLocked;
  71. /// Current nesting depth of bundle_lock directives.
  72. unsigned BundleLockNestingDepth = 0;
  73. /// We've seen a bundle_lock directive but not its first instruction
  74. /// yet.
  75. bool BundleGroupBeforeFirstInst : 1;
  76. /// Whether this section has had instructions emitted into it.
  77. bool HasInstructions : 1;
  78. bool IsRegistered : 1;
  79. MCDummyFragment DummyFragment;
  80. FragmentListType Fragments;
  81. /// Mapping from subsection number to insertion point for subsection numbers
  82. /// below that number.
  83. SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
  84. /// State for tracking labels that don't yet have Fragments
  85. struct PendingLabel {
  86. MCSymbol* Sym;
  87. unsigned Subsection;
  88. PendingLabel(MCSymbol* Sym, unsigned Subsection = 0)
  89. : Sym(Sym), Subsection(Subsection) {}
  90. };
  91. SmallVector<PendingLabel, 2> PendingLabels;
  92. protected:
  93. // TODO Make Name private when possible.
  94. StringRef Name;
  95. SectionVariant Variant;
  96. SectionKind Kind;
  97. MCSection(SectionVariant V, StringRef Name, SectionKind K, MCSymbol *Begin);
  98. ~MCSection();
  99. public:
  100. MCSection(const MCSection &) = delete;
  101. MCSection &operator=(const MCSection &) = delete;
  102. StringRef getName() const { return Name; }
  103. SectionKind getKind() const { return Kind; }
  104. SectionVariant getVariant() const { return Variant; }
  105. MCSymbol *getBeginSymbol() { return Begin; }
  106. const MCSymbol *getBeginSymbol() const {
  107. return const_cast<MCSection *>(this)->getBeginSymbol();
  108. }
  109. void setBeginSymbol(MCSymbol *Sym) {
  110. assert(!Begin);
  111. Begin = Sym;
  112. }
  113. MCSymbol *getEndSymbol(MCContext &Ctx);
  114. bool hasEnded() const;
  115. unsigned getAlignment() const { return Alignment.value(); }
  116. void setAlignment(Align Value) { Alignment = Value; }
  117. unsigned getOrdinal() const { return Ordinal; }
  118. void setOrdinal(unsigned Value) { Ordinal = Value; }
  119. unsigned getLayoutOrder() const { return LayoutOrder; }
  120. void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
  121. BundleLockStateType getBundleLockState() const { return BundleLockState; }
  122. void setBundleLockState(BundleLockStateType NewState);
  123. bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
  124. bool isBundleGroupBeforeFirstInst() const {
  125. return BundleGroupBeforeFirstInst;
  126. }
  127. void setBundleGroupBeforeFirstInst(bool IsFirst) {
  128. BundleGroupBeforeFirstInst = IsFirst;
  129. }
  130. bool hasInstructions() const { return HasInstructions; }
  131. void setHasInstructions(bool Value) { HasInstructions = Value; }
  132. bool isRegistered() const { return IsRegistered; }
  133. void setIsRegistered(bool Value) { IsRegistered = Value; }
  134. MCSection::FragmentListType &getFragmentList() { return Fragments; }
  135. const MCSection::FragmentListType &getFragmentList() const {
  136. return const_cast<MCSection *>(this)->getFragmentList();
  137. }
  138. /// Support for MCFragment::getNextNode().
  139. static FragmentListType MCSection::*getSublistAccess(MCFragment *) {
  140. return &MCSection::Fragments;
  141. }
  142. const MCDummyFragment &getDummyFragment() const { return DummyFragment; }
  143. MCDummyFragment &getDummyFragment() { return DummyFragment; }
  144. iterator begin() { return Fragments.begin(); }
  145. const_iterator begin() const { return Fragments.begin(); }
  146. iterator end() { return Fragments.end(); }
  147. const_iterator end() const { return Fragments.end(); }
  148. MCSection::iterator getSubsectionInsertionPoint(unsigned Subsection);
  149. void dump() const;
  150. virtual void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
  151. raw_ostream &OS,
  152. const MCExpr *Subsection) const = 0;
  153. /// Return true if a .align directive should use "optimized nops" to fill
  154. /// instead of 0s.
  155. virtual bool UseCodeAlign() const = 0;
  156. /// Check whether this section is "virtual", that is has no actual object
  157. /// file contents.
  158. virtual bool isVirtualSection() const = 0;
  159. virtual StringRef getVirtualSectionKind() const;
  160. /// Add a pending label for the requested subsection. This label will be
  161. /// associated with a fragment in flushPendingLabels()
  162. void addPendingLabel(MCSymbol* label, unsigned Subsection = 0);
  163. /// Associate all pending labels in a subsection with a fragment.
  164. void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0,
  165. unsigned Subsection = 0);
  166. /// Associate all pending labels with empty data fragments. One fragment
  167. /// will be created for each subsection as necessary.
  168. void flushPendingLabels();
  169. };
  170. } // end namespace llvm
  171. #endif // LLVM_MC_MCSECTION_H
  172. #ifdef __GNUC__
  173. #pragma GCC diagnostic pop
  174. #endif