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