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