MCSection.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //===- MCSection.h - Machine Code Sections ----------------------*- 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. //
  10. // This file declares the MCSection class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCSECTION_H
  14. #define LLVM_MC_MCSECTION_H
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/ADT/ilist.h"
  18. #include "llvm/ADT/ilist_node.h"
  19. #include "llvm/MC/SectionKind.h"
  20. #include "llvm/Support/Compiler.h"
  21. namespace llvm {
  22. class MCAssembler;
  23. class MCAsmInfo;
  24. class MCContext;
  25. class MCExpr;
  26. class MCFragment;
  27. class MCSection;
  28. class MCSymbol;
  29. class raw_ostream;
  30. template<>
  31. struct ilist_node_traits<MCFragment> {
  32. MCFragment *createNode(const MCFragment &V);
  33. static void deleteNode(MCFragment *V);
  34. void addNodeToList(MCFragment *) {}
  35. void removeNodeFromList(MCFragment *) {}
  36. void transferNodesFromList(ilist_node_traits & /*SrcTraits*/,
  37. ilist_iterator<MCFragment> /*first*/,
  38. ilist_iterator<MCFragment> /*last*/) {}
  39. };
  40. /// Instances of this class represent a uniqued identifier for a section in the
  41. /// current translation unit. The MCContext class uniques and creates these.
  42. class MCSection {
  43. public:
  44. enum SectionVariant { SV_COFF = 0, SV_ELF, SV_MachO };
  45. /// \brief Express the state of bundle locked groups while emitting code.
  46. enum BundleLockStateType {
  47. NotBundleLocked,
  48. BundleLocked,
  49. BundleLockedAlignToEnd
  50. };
  51. typedef iplist<MCFragment> FragmentListType;
  52. typedef FragmentListType::const_iterator const_iterator;
  53. typedef FragmentListType::iterator iterator;
  54. typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
  55. typedef FragmentListType::reverse_iterator reverse_iterator;
  56. private:
  57. MCSection(const MCSection &) = delete;
  58. void operator=(const MCSection &) = delete;
  59. MCSymbol *Begin;
  60. MCSymbol *End = nullptr;
  61. /// The alignment requirement of this section.
  62. unsigned Alignment = 1;
  63. /// The section index in the assemblers section list.
  64. unsigned Ordinal = 0;
  65. /// The index of this section in the layout order.
  66. unsigned LayoutOrder;
  67. /// \brief Keeping track of bundle-locked state.
  68. BundleLockStateType BundleLockState = NotBundleLocked;
  69. /// \brief Current nesting depth of bundle_lock directives.
  70. unsigned BundleLockNestingDepth = 0;
  71. /// \brief We've seen a bundle_lock directive but not its first instruction
  72. /// yet.
  73. unsigned BundleGroupBeforeFirstInst : 1;
  74. /// Whether this section has had instructions emitted into it.
  75. unsigned HasInstructions : 1;
  76. unsigned IsRegistered : 1;
  77. FragmentListType Fragments;
  78. /// Mapping from subsection number to insertion point for subsection numbers
  79. /// below that number.
  80. SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
  81. protected:
  82. MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin);
  83. SectionVariant Variant;
  84. SectionKind Kind;
  85. public:
  86. virtual ~MCSection();
  87. SectionKind getKind() const { return Kind; }
  88. SectionVariant getVariant() const { return Variant; }
  89. MCSymbol *getBeginSymbol() { return Begin; }
  90. const MCSymbol *getBeginSymbol() const {
  91. return const_cast<MCSection *>(this)->getBeginSymbol();
  92. }
  93. void setBeginSymbol(MCSymbol *Sym) {
  94. assert(!Begin);
  95. Begin = Sym;
  96. }
  97. MCSymbol *getEndSymbol(MCContext &Ctx);
  98. bool hasEnded() const;
  99. unsigned getAlignment() const { return Alignment; }
  100. void setAlignment(unsigned Value) { Alignment = Value; }
  101. unsigned getOrdinal() const { return Ordinal; }
  102. void setOrdinal(unsigned Value) { Ordinal = Value; }
  103. unsigned getLayoutOrder() const { return LayoutOrder; }
  104. void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
  105. BundleLockStateType getBundleLockState() const { return BundleLockState; }
  106. void setBundleLockState(BundleLockStateType NewState);
  107. bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
  108. bool isBundleGroupBeforeFirstInst() const {
  109. return BundleGroupBeforeFirstInst;
  110. }
  111. void setBundleGroupBeforeFirstInst(bool IsFirst) {
  112. BundleGroupBeforeFirstInst = IsFirst;
  113. }
  114. bool hasInstructions() const { return HasInstructions; }
  115. void setHasInstructions(bool Value) { HasInstructions = Value; }
  116. bool isRegistered() const { return IsRegistered; }
  117. void setIsRegistered(bool Value) { IsRegistered = Value; }
  118. MCSection::FragmentListType &getFragmentList() { return Fragments; }
  119. const MCSection::FragmentListType &getFragmentList() const {
  120. return const_cast<MCSection *>(this)->getFragmentList();
  121. }
  122. MCSection::iterator begin();
  123. MCSection::const_iterator begin() const {
  124. return const_cast<MCSection *>(this)->begin();
  125. }
  126. MCSection::iterator end();
  127. MCSection::const_iterator end() const {
  128. return const_cast<MCSection *>(this)->end();
  129. }
  130. MCSection::reverse_iterator rbegin();
  131. MCSection::const_reverse_iterator rbegin() const {
  132. return const_cast<MCSection *>(this)->rbegin();
  133. }
  134. MCSection::reverse_iterator rend();
  135. MCSection::const_reverse_iterator rend() const {
  136. return const_cast<MCSection *>(this)->rend();
  137. }
  138. MCSection::iterator getSubsectionInsertionPoint(unsigned Subsection);
  139. void dump();
  140. virtual void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS,
  141. const MCExpr *Subsection) const = 0;
  142. /// Return true if a .align directive should use "optimized nops" to fill
  143. /// instead of 0s.
  144. virtual bool UseCodeAlign() const = 0;
  145. /// Check whether this section is "virtual", that is has no actual object
  146. /// file contents.
  147. virtual bool isVirtualSection() const = 0;
  148. };
  149. } // end namespace llvm
  150. #endif