[MC] Use unsigned for the Kind bitfield in MCSymbol
[oota-llvm.git] / include / llvm / MC / MCSymbol.h
1 //===- MCSymbol.h - Machine Code Symbols ------------------------*- 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 contains the declaration of the MCSymbol class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_MC_MCSYMBOL_H
15 #define LLVM_MC_MCSYMBOL_H
16
17 #include "llvm/ADT/PointerUnion.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/Support/Compiler.h"
22
23 namespace llvm {
24 class MCExpr;
25 class MCSymbol;
26 class MCFragment;
27 class MCSection;
28 class MCContext;
29 class raw_ostream;
30
31 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
32 /// and MCSymbols are created and uniqued by the MCContext class.  MCSymbols
33 /// should only be constructed with valid names for the object file.
34 ///
35 /// If the symbol is defined/emitted into the current translation unit, the
36 /// Section member is set to indicate what section it lives in.  Otherwise, if
37 /// it is a reference to an external entity, it has a null section.
38 class MCSymbol {
39 protected:
40   /// The kind of the symbol.  If it is any value other than unset then this
41   /// class is actually one of the appropriate subclasses of MCSymbol.
42   enum SymbolKind {
43     SymbolKindUnset,
44     SymbolKindCOFF,
45     SymbolKindELF,
46     SymbolKindMachO,
47   };
48
49   // Special sentinal value for the absolute pseudo section.
50   //
51   // FIXME: Use a PointerInt wrapper for this?
52   static MCSection *AbsolutePseudoSection;
53
54   /// Name - The name of the symbol.  The referred-to string data is actually
55   /// held by the StringMap that lives in MCContext.
56   const StringMapEntry<bool> *Name;
57
58   /// If a symbol has a Fragment, the section is implied, so we only need
59   /// one pointer.
60   /// FIXME: We might be able to simplify this by having the asm streamer create
61   /// dummy fragments.
62   /// If this is a section, then it gives the symbol is defined in. This is null
63   /// for undefined symbols, and the special AbsolutePseudoSection value for
64   /// absolute symbols. If this is a variable symbol, this caches the variable
65   /// value's section.
66   ///
67   /// If this is a fragment, then it gives the fragment this symbol's value is
68   /// relative to, if any.
69   mutable PointerUnion<MCSection *, MCFragment *> SectionOrFragment;
70
71   /// Value - If non-null, the value for a variable symbol.
72   const MCExpr *Value;
73
74   /// IsTemporary - True if this is an assembler temporary label, which
75   /// typically does not survive in the .o file's symbol table.  Usually
76   /// "Lfoo" or ".foo".
77   unsigned IsTemporary : 1;
78
79   /// \brief True if this symbol can be redefined.
80   unsigned IsRedefinable : 1;
81
82   /// IsUsed - True if this symbol has been used.
83   mutable unsigned IsUsed : 1;
84
85   mutable bool IsRegistered : 1;
86
87   /// This symbol is visible outside this translation unit.
88   mutable unsigned IsExternal : 1;
89
90   /// This symbol is private extern.
91   mutable unsigned IsPrivateExtern : 1;
92
93   /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
94   /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
95   unsigned Kind : 2;
96
97   /// Index field, for use by the object file implementation.
98   mutable uint32_t Index = 0;
99
100   union {
101     /// The offset to apply to the fragment address to form this symbol's value.
102     uint64_t Offset;
103
104     /// The size of the symbol, if it is 'common'.
105     uint64_t CommonSize;
106   };
107
108   /// The alignment of the symbol, if it is 'common', or -1.
109   //
110   // FIXME: Pack this in with other fields?
111   unsigned CommonAlign = -1U;
112
113   /// The Flags field is used by object file implementations to store
114   /// additional per symbol information which is not easily classified.
115   mutable uint32_t Flags = 0;
116
117 protected: // MCContext creates and uniques these.
118   friend class MCExpr;
119   friend class MCContext;
120   MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
121       : Name(Name), Value(nullptr), IsTemporary(isTemporary),
122         IsRedefinable(false), IsUsed(false), IsRegistered(false),
123         IsExternal(false), IsPrivateExtern(false),
124         Kind(Kind) {
125     Offset = 0;
126   }
127
128 private:
129   MCSymbol(const MCSymbol &) = delete;
130   void operator=(const MCSymbol &) = delete;
131   MCSection *getSectionPtr() const {
132     if (MCFragment *F = getFragment())
133       return F->getParent();
134     assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
135     MCSection *Section = SectionOrFragment.dyn_cast<MCSection *>();
136     if (Section || !Value)
137       return Section;
138     return Section = Value->findAssociatedSection();
139   }
140
141 public:
142   /// getName - Get the symbol name.
143   StringRef getName() const { return Name ? Name->first() : ""; }
144
145   bool isRegistered() const { return IsRegistered; }
146   void setIsRegistered(bool Value) const { IsRegistered = Value; }
147
148   /// \name Accessors
149   /// @{
150
151   /// isTemporary - Check if this is an assembler temporary symbol.
152   bool isTemporary() const { return IsTemporary; }
153
154   /// isUsed - Check if this is used.
155   bool isUsed() const { return IsUsed; }
156   void setUsed(bool Value) const { IsUsed = Value; }
157
158   /// \brief Check if this symbol is redefinable.
159   bool isRedefinable() const { return IsRedefinable; }
160   /// \brief Mark this symbol as redefinable.
161   void setRedefinable(bool Value) { IsRedefinable = Value; }
162   /// \brief Prepare this symbol to be redefined.
163   void redefineIfPossible() {
164     if (IsRedefinable) {
165       Value = nullptr;
166       SectionOrFragment = nullptr;
167       IsRedefinable = false;
168     }
169   }
170
171   /// @}
172   /// \name Associated Sections
173   /// @{
174
175   /// isDefined - Check if this symbol is defined (i.e., it has an address).
176   ///
177   /// Defined symbols are either absolute or in some section.
178   bool isDefined() const { return getSectionPtr() != nullptr; }
179
180   /// isInSection - Check if this symbol is defined in some section (i.e., it
181   /// is defined but not absolute).
182   bool isInSection() const { return isDefined() && !isAbsolute(); }
183
184   /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
185   bool isUndefined() const { return !isDefined(); }
186
187   /// isAbsolute - Check if this is an absolute symbol.
188   bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
189
190   /// Get the section associated with a defined, non-absolute symbol.
191   MCSection &getSection() const {
192     assert(isInSection() && "Invalid accessor!");
193     return *getSectionPtr();
194   }
195
196   /// Mark the symbol as defined in the section \p S.
197   void setSection(MCSection &S) {
198     assert(!isVariable() && "Cannot set section of variable");
199     assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
200     SectionOrFragment = &S;
201   }
202
203   /// Mark the symbol as undefined.
204   void setUndefined() {
205     SectionOrFragment = nullptr;
206   }
207
208   bool isELF() const { return Kind == SymbolKindELF; }
209
210   bool isCOFF() const { return Kind == SymbolKindCOFF; }
211
212   bool isMachO() const { return Kind == SymbolKindMachO; }
213
214   /// @}
215   /// \name Variable Symbols
216   /// @{
217
218   /// isVariable - Check if this is a variable symbol.
219   bool isVariable() const { return Value != nullptr; }
220
221   /// getVariableValue() - Get the value for variable symbols.
222   const MCExpr *getVariableValue() const {
223     assert(isVariable() && "Invalid accessor!");
224     IsUsed = true;
225     return Value;
226   }
227
228   void setVariableValue(const MCExpr *Value);
229
230   /// @}
231
232   /// Get the (implementation defined) index.
233   uint32_t getIndex() const {
234     return Index;
235   }
236
237   /// Set the (implementation defined) index.
238   void setIndex(uint32_t Value) const {
239     Index = Value;
240   }
241
242   uint64_t getOffset() const {
243     assert(!isCommon());
244     return Offset;
245   }
246   void setOffset(uint64_t Value) {
247     assert(!isCommon());
248     Offset = Value;
249   }
250
251   /// Return the size of a 'common' symbol.
252   uint64_t getCommonSize() const {
253     assert(isCommon() && "Not a 'common' symbol!");
254     return CommonSize;
255   }
256
257   /// Mark this symbol as being 'common'.
258   ///
259   /// \param Size - The size of the symbol.
260   /// \param Align - The alignment of the symbol.
261   void setCommon(uint64_t Size, unsigned Align) {
262     assert(getOffset() == 0);
263     CommonSize = Size;
264     CommonAlign = Align;
265   }
266
267   ///  Return the alignment of a 'common' symbol.
268   unsigned getCommonAlignment() const {
269     assert(isCommon() && "Not a 'common' symbol!");
270     return CommonAlign;
271   }
272
273   /// Declare this symbol as being 'common'.
274   ///
275   /// \param Size - The size of the symbol.
276   /// \param Align - The alignment of the symbol.
277   /// \return True if symbol was already declared as a different type
278   bool declareCommon(uint64_t Size, unsigned Align) {
279     assert(isCommon() || getOffset() == 0);
280     if(isCommon()) {
281       if(CommonSize != Size || CommonAlign != Align)
282        return true;
283     } else
284       setCommon(Size, Align);
285     return false;
286   }
287
288   /// Is this a 'common' symbol.
289   bool isCommon() const { return CommonAlign != -1U; }
290
291   MCFragment *getFragment() const {
292     return SectionOrFragment.dyn_cast<MCFragment *>();
293   }
294   void setFragment(MCFragment *Value) const {
295     SectionOrFragment = Value;
296   }
297
298   bool isExternal() const { return IsExternal; }
299   void setExternal(bool Value) const { IsExternal = Value; }
300
301   bool isPrivateExtern() const { return IsPrivateExtern; }
302   void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
303
304   /// print - Print the value to the stream \p OS.
305   void print(raw_ostream &OS) const;
306
307   /// dump - Print the value to stderr.
308   void dump() const;
309
310 protected:
311   /// Get the (implementation defined) symbol flags.
312   uint32_t getFlags() const { return Flags; }
313
314   /// Set the (implementation defined) symbol flags.
315   void setFlags(uint32_t Value) const { Flags = Value; }
316
317   /// Modify the flags via a mask
318   void modifyFlags(uint32_t Value, uint32_t Mask) const {
319     Flags = (Flags & ~Mask) | Value;
320   }
321 };
322
323 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
324   Sym.print(OS);
325   return OS;
326 }
327 } // end namespace llvm
328
329 #endif