Use a PointerUnion in MCSymbol for Section and Fragment. NFC.
[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   SymbolKind Kind : 2;
94
95   /// Index field, for use by the object file implementation.
96   mutable uint32_t Index = 0;
97
98   union {
99     /// The offset to apply to the fragment address to form this symbol's value.
100     uint64_t Offset;
101
102     /// The size of the symbol, if it is 'common'.
103     uint64_t CommonSize;
104   };
105
106   /// The alignment of the symbol, if it is 'common', or -1.
107   //
108   // FIXME: Pack this in with other fields?
109   unsigned CommonAlign = -1U;
110
111   /// The Flags field is used by object file implementations to store
112   /// additional per symbol information which is not easily classified.
113   mutable uint32_t Flags = 0;
114
115 protected: // MCContext creates and uniques these.
116   friend class MCExpr;
117   friend class MCContext;
118   MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
119       : Name(Name), Value(nullptr), IsTemporary(isTemporary),
120         IsRedefinable(false), IsUsed(false), IsRegistered(false),
121         IsExternal(false), IsPrivateExtern(false),
122         Kind(Kind) {
123     Offset = 0;
124   }
125
126 private:
127   MCSymbol(const MCSymbol &) = delete;
128   void operator=(const MCSymbol &) = delete;
129   MCSection *getSectionPtr() const {
130     if (MCFragment *F = getFragment())
131       return F->getParent();
132     assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
133     MCSection *Section = SectionOrFragment.dyn_cast<MCSection *>();
134     if (Section || !Value)
135       return Section;
136     return Section = Value->findAssociatedSection();
137   }
138
139 public:
140   /// getName - Get the symbol name.
141   StringRef getName() const { return Name ? Name->first() : ""; }
142
143   bool isRegistered() const { return IsRegistered; }
144   void setIsRegistered(bool Value) const { IsRegistered = Value; }
145
146   /// \name Accessors
147   /// @{
148
149   /// isTemporary - Check if this is an assembler temporary symbol.
150   bool isTemporary() const { return IsTemporary; }
151
152   /// isUsed - Check if this is used.
153   bool isUsed() const { return IsUsed; }
154   void setUsed(bool Value) const { IsUsed = Value; }
155
156   /// \brief Check if this symbol is redefinable.
157   bool isRedefinable() const { return IsRedefinable; }
158   /// \brief Mark this symbol as redefinable.
159   void setRedefinable(bool Value) { IsRedefinable = Value; }
160   /// \brief Prepare this symbol to be redefined.
161   void redefineIfPossible() {
162     if (IsRedefinable) {
163       Value = nullptr;
164       SectionOrFragment = nullptr;
165       IsRedefinable = false;
166     }
167   }
168
169   /// @}
170   /// \name Associated Sections
171   /// @{
172
173   /// isDefined - Check if this symbol is defined (i.e., it has an address).
174   ///
175   /// Defined symbols are either absolute or in some section.
176   bool isDefined() const { return getSectionPtr() != nullptr; }
177
178   /// isInSection - Check if this symbol is defined in some section (i.e., it
179   /// is defined but not absolute).
180   bool isInSection() const { return isDefined() && !isAbsolute(); }
181
182   /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
183   bool isUndefined() const { return !isDefined(); }
184
185   /// isAbsolute - Check if this is an absolute symbol.
186   bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
187
188   /// Get the section associated with a defined, non-absolute symbol.
189   MCSection &getSection() const {
190     assert(isInSection() && "Invalid accessor!");
191     return *getSectionPtr();
192   }
193
194   /// Mark the symbol as defined in the section \p S.
195   void setSection(MCSection &S) {
196     assert(!isVariable() && "Cannot set section of variable");
197     assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
198     SectionOrFragment = &S;
199   }
200
201   /// Mark the symbol as undefined.
202   void setUndefined() {
203     SectionOrFragment = nullptr;
204   }
205
206   bool isELF() const { return Kind == SymbolKindELF; }
207
208   bool isCOFF() const { return Kind == SymbolKindCOFF; }
209
210   bool isMachO() const { return Kind == SymbolKindMachO; }
211
212   /// @}
213   /// \name Variable Symbols
214   /// @{
215
216   /// isVariable - Check if this is a variable symbol.
217   bool isVariable() const { return Value != nullptr; }
218
219   /// getVariableValue() - Get the value for variable symbols.
220   const MCExpr *getVariableValue() const {
221     assert(isVariable() && "Invalid accessor!");
222     IsUsed = true;
223     return Value;
224   }
225
226   void setVariableValue(const MCExpr *Value);
227
228   /// @}
229
230   /// Get the (implementation defined) index.
231   uint32_t getIndex() const {
232     return Index;
233   }
234
235   /// Set the (implementation defined) index.
236   void setIndex(uint32_t Value) const {
237     Index = Value;
238   }
239
240   uint64_t getOffset() const {
241     assert(!isCommon());
242     return Offset;
243   }
244   void setOffset(uint64_t Value) {
245     assert(!isCommon());
246     Offset = Value;
247   }
248
249   /// Return the size of a 'common' symbol.
250   uint64_t getCommonSize() const {
251     assert(isCommon() && "Not a 'common' symbol!");
252     return CommonSize;
253   }
254
255   /// Mark this symbol as being 'common'.
256   ///
257   /// \param Size - The size of the symbol.
258   /// \param Align - The alignment of the symbol.
259   void setCommon(uint64_t Size, unsigned Align) {
260     assert(getOffset() == 0);
261     CommonSize = Size;
262     CommonAlign = Align;
263   }
264
265   ///  Return the alignment of a 'common' symbol.
266   unsigned getCommonAlignment() const {
267     assert(isCommon() && "Not a 'common' symbol!");
268     return CommonAlign;
269   }
270
271   /// Declare this symbol as being 'common'.
272   ///
273   /// \param Size - The size of the symbol.
274   /// \param Align - The alignment of the symbol.
275   /// \return True if symbol was already declared as a different type
276   bool declareCommon(uint64_t Size, unsigned Align) {
277     assert(isCommon() || getOffset() == 0);
278     if(isCommon()) {
279       if(CommonSize != Size || CommonAlign != Align)
280        return true;
281     } else
282       setCommon(Size, Align);
283     return false;
284   }
285
286   /// Is this a 'common' symbol.
287   bool isCommon() const { return CommonAlign != -1U; }
288
289   MCFragment *getFragment() const {
290     return SectionOrFragment.dyn_cast<MCFragment *>();
291   }
292   void setFragment(MCFragment *Value) const {
293     SectionOrFragment = Value;
294   }
295
296   bool isExternal() const { return IsExternal; }
297   void setExternal(bool Value) const { IsExternal = Value; }
298
299   bool isPrivateExtern() const { return IsPrivateExtern; }
300   void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
301
302   /// print - Print the value to the stream \p OS.
303   void print(raw_ostream &OS) const;
304
305   /// dump - Print the value to stderr.
306   void dump() const;
307
308 protected:
309   /// Get the (implementation defined) symbol flags.
310   uint32_t getFlags() const { return Flags; }
311
312   /// Set the (implementation defined) symbol flags.
313   void setFlags(uint32_t Value) const { Flags = Value; }
314
315   /// Modify the flags via a mask
316   void modifyFlags(uint32_t Value, uint32_t Mask) const {
317     Flags = (Flags & ~Mask) | Value;
318   }
319 };
320
321 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
322   Sym.print(OS);
323   return OS;
324 }
325 } // end namespace llvm
326
327 #endif