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