MCSection.h 6.7 KB

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