Remove unnecessary MCExpr.h include from MCSymbol.h
[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/Support/Compiler.h"
21
22 namespace llvm {
23 class MCAsmInfo;
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   /// If a symbol has a Fragment, the section is implied, so we only need
55   /// one pointer.
56   /// FIXME: We might be able to simplify this by having the asm streamer create
57   /// dummy fragments.
58   /// If this is a section, then it gives the symbol is defined in. This is null
59   /// for undefined symbols, and the special AbsolutePseudoSection value for
60   /// absolute symbols. If this is a variable symbol, this caches the variable
61   /// value's section.
62   ///
63   /// If this is a fragment, then it gives the fragment this symbol's value is
64   /// relative to, if any.
65   mutable PointerUnion<MCSection *, MCFragment *> SectionOrFragment;
66
67   /// Value - If non-null, the value for a variable symbol.
68   const MCExpr *Value;
69
70   /// IsTemporary - True if this is an assembler temporary label, which
71   /// typically does not survive in the .o file's symbol table.  Usually
72   /// "Lfoo" or ".foo".
73   unsigned IsTemporary : 1;
74
75   /// \brief True if this symbol can be redefined.
76   unsigned IsRedefinable : 1;
77
78   /// IsUsed - True if this symbol has been used.
79   mutable unsigned IsUsed : 1;
80
81   mutable bool IsRegistered : 1;
82
83   /// This symbol is visible outside this translation unit.
84   mutable unsigned IsExternal : 1;
85
86   /// This symbol is private extern.
87   mutable unsigned IsPrivateExtern : 1;
88
89   /// True if this symbol is named.
90   /// A named symbol will have a pointer to the name allocated in the bytes
91   /// immediately prior to the MCSymbol.
92   unsigned HasName : 1;
93
94   /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
95   /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
96   unsigned Kind : 2;
97
98   /// Index field, for use by the object file implementation.
99   mutable uint32_t Index = 0;
100
101   union {
102     /// The offset to apply to the fragment address to form this symbol's value.
103     uint64_t Offset;
104
105     /// The size of the symbol, if it is 'common'.
106     uint64_t CommonSize;
107   };
108
109   /// The alignment of the symbol, if it is 'common', or -1.
110   //
111   // FIXME: Pack this in with other fields?
112   unsigned CommonAlign = -1U;
113
114   /// The Flags field is used by object file implementations to store
115   /// additional per symbol information which is not easily classified.
116   mutable uint32_t Flags = 0;
117
118 protected: // MCContext creates and uniques these.
119   friend class MCExpr;
120   friend class MCContext;
121
122   /// \brief The name for a symbol.
123   /// MCSymbol contains a uint64_t so is probably aligned to 8.  On a 32-bit
124   /// system, the name is a pointer so isn't going to satisfy the 8 byte
125   /// alignment of uint64_t.  Account for that here.
126   typedef union {
127     const StringMapEntry<bool> *NameEntry;
128     uint64_t AlignmentPadding;
129   } NameEntryStorageTy;
130
131   MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
132       : Value(nullptr), IsTemporary(isTemporary),
133         IsRedefinable(false), IsUsed(false), IsRegistered(false),
134         IsExternal(false), IsPrivateExtern(false), HasName(!!Name),
135         Kind(Kind) {
136     Offset = 0;
137     if (Name)
138       getNameEntryPtr() = Name;
139   }
140
141   // Provide custom new/delete as we will only allocate space for a name
142   // if we need one.
143   void *operator new(size_t s, const StringMapEntry<bool> *Name,
144                      MCContext &Ctx);
145
146 private:
147
148   void operator delete(void *);
149   /// \brief Placement delete - required by std, but never called.
150   void operator delete(void*, unsigned) {
151     llvm_unreachable("Constructor throws?");
152   }
153   /// \brief Placement delete - required by std, but never called.
154   void operator delete(void*, unsigned, bool) {
155     llvm_unreachable("Constructor throws?");
156   }
157
158   MCSymbol(const MCSymbol &) = delete;
159   void operator=(const MCSymbol &) = delete;
160   MCSection *getSectionPtr() const {
161     if (MCFragment *F = getFragment())
162       return F->getParent();
163     assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
164     MCSection *Section = SectionOrFragment.dyn_cast<MCSection *>();
165     if (Section || !Value)
166       return Section;
167     return Section = Value->findAssociatedSection();
168   }
169
170   /// \brief Get a reference to the name field.  Requires that we have a name
171   const StringMapEntry<bool> *&getNameEntryPtr() {
172     assert(HasName && "Name is required");
173     NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
174     return (*(Name - 1)).NameEntry;
175   }
176   const StringMapEntry<bool> *&getNameEntryPtr() const {
177     return const_cast<MCSymbol*>(this)->getNameEntryPtr();
178   }
179
180 public:
181   /// getName - Get the symbol name.
182   StringRef getName() const {
183     if (!HasName)
184       return StringRef();
185
186     return getNameEntryPtr()->first();
187   }
188
189   bool isRegistered() const { return IsRegistered; }
190   void setIsRegistered(bool Value) const { IsRegistered = Value; }
191
192   /// \name Accessors
193   /// @{
194
195   /// isTemporary - Check if this is an assembler temporary symbol.
196   bool isTemporary() const { return IsTemporary; }
197
198   /// isUsed - Check if this is used.
199   bool isUsed() const { return IsUsed; }
200   void setUsed(bool Value) const { IsUsed = Value; }
201
202   /// \brief Check if this symbol is redefinable.
203   bool isRedefinable() const { return IsRedefinable; }
204   /// \brief Mark this symbol as redefinable.
205   void setRedefinable(bool Value) { IsRedefinable = Value; }
206   /// \brief Prepare this symbol to be redefined.
207   void redefineIfPossible() {
208     if (IsRedefinable) {
209       Value = nullptr;
210       SectionOrFragment = nullptr;
211       IsRedefinable = false;
212     }
213   }
214
215   /// @}
216   /// \name Associated Sections
217   /// @{
218
219   /// isDefined - Check if this symbol is defined (i.e., it has an address).
220   ///
221   /// Defined symbols are either absolute or in some section.
222   bool isDefined() const { return getSectionPtr() != nullptr; }
223
224   /// isInSection - Check if this symbol is defined in some section (i.e., it
225   /// is defined but not absolute).
226   bool isInSection() const { return isDefined() && !isAbsolute(); }
227
228   /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
229   bool isUndefined() const { return !isDefined(); }
230
231   /// isAbsolute - Check if this is an absolute symbol.
232   bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
233
234   /// Get the section associated with a defined, non-absolute symbol.
235   MCSection &getSection() const {
236     assert(isInSection() && "Invalid accessor!");
237     return *getSectionPtr();
238   }
239
240   /// Mark the symbol as defined in the section \p S.
241   void setSection(MCSection &S) {
242     assert(!isVariable() && "Cannot set section of variable");
243     assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
244     SectionOrFragment = &S;
245   }
246
247   /// Mark the symbol as undefined.
248   void setUndefined() {
249     SectionOrFragment = nullptr;
250   }
251
252   bool isELF() const { return Kind == SymbolKindELF; }
253
254   bool isCOFF() const { return Kind == SymbolKindCOFF; }
255
256   bool isMachO() const { return Kind == SymbolKindMachO; }
257
258   /// @}
259   /// \name Variable Symbols
260   /// @{
261
262   /// isVariable - Check if this is a variable symbol.
263   bool isVariable() const { return Value != nullptr; }
264
265   /// getVariableValue() - Get the value for variable symbols.
266   const MCExpr *getVariableValue() const {
267     assert(isVariable() && "Invalid accessor!");
268     IsUsed = true;
269     return Value;
270   }
271
272   void setVariableValue(const MCExpr *Value);
273
274   /// @}
275
276   /// Get the (implementation defined) index.
277   uint32_t getIndex() const {
278     return Index;
279   }
280
281   /// Set the (implementation defined) index.
282   void setIndex(uint32_t Value) const {
283     Index = Value;
284   }
285
286   uint64_t getOffset() const {
287     assert(!isCommon());
288     return Offset;
289   }
290   void setOffset(uint64_t Value) {
291     assert(!isCommon());
292     Offset = Value;
293   }
294
295   /// Return the size of a 'common' symbol.
296   uint64_t getCommonSize() const {
297     assert(isCommon() && "Not a 'common' symbol!");
298     return CommonSize;
299   }
300
301   /// Mark this symbol as being 'common'.
302   ///
303   /// \param Size - The size of the symbol.
304   /// \param Align - The alignment of the symbol.
305   void setCommon(uint64_t Size, unsigned Align) {
306     assert(getOffset() == 0);
307     CommonSize = Size;
308     CommonAlign = Align;
309   }
310
311   ///  Return the alignment of a 'common' symbol.
312   unsigned getCommonAlignment() const {
313     assert(isCommon() && "Not a 'common' symbol!");
314     return CommonAlign;
315   }
316
317   /// Declare this symbol as being 'common'.
318   ///
319   /// \param Size - The size of the symbol.
320   /// \param Align - The alignment of the symbol.
321   /// \return True if symbol was already declared as a different type
322   bool declareCommon(uint64_t Size, unsigned Align) {
323     assert(isCommon() || getOffset() == 0);
324     if(isCommon()) {
325       if(CommonSize != Size || CommonAlign != Align)
326        return true;
327     } else
328       setCommon(Size, Align);
329     return false;
330   }
331
332   /// Is this a 'common' symbol.
333   bool isCommon() const { return CommonAlign != -1U; }
334
335   MCFragment *getFragment() const {
336     return SectionOrFragment.dyn_cast<MCFragment *>();
337   }
338   void setFragment(MCFragment *Value) const {
339     SectionOrFragment = Value;
340   }
341
342   bool isExternal() const { return IsExternal; }
343   void setExternal(bool Value) const { IsExternal = Value; }
344
345   bool isPrivateExtern() const { return IsPrivateExtern; }
346   void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
347
348   /// print - Print the value to the stream \p OS.
349   void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
350
351   /// dump - Print the value to stderr.
352   void dump() const;
353
354 protected:
355   /// Get the (implementation defined) symbol flags.
356   uint32_t getFlags() const { return Flags; }
357
358   /// Set the (implementation defined) symbol flags.
359   void setFlags(uint32_t Value) const { Flags = Value; }
360
361   /// Modify the flags via a mask
362   void modifyFlags(uint32_t Value, uint32_t Mask) const {
363     Flags = (Flags & ~Mask) | Value;
364   }
365 };
366
367 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
368   Sym.print(OS, nullptr);
369   return OS;
370 }
371 } // end namespace llvm
372
373 #endif