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