MC: Shave a pointer off of MCSymbol::Name
[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/PointerIntPair.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/Support/Compiler.h"
21
22 namespace llvm {
23 class MCExpr;
24 class MCSymbol;
25 class MCFragment;
26 class MCSection;
27 class MCContext;
28 class raw_ostream;
29
30 // TODO: Merge completely with MCSymbol.
31 class MCSymbolData {
32   /// Fragment - The fragment this symbol's value is relative to, if any. Also
33   /// stores if this symbol is visible outside this translation unit (bit 0) or
34   /// if it is private extern (bit 1).
35   PointerIntPair<MCFragment *, 2> Fragment;
36
37   union {
38     /// Offset - The offset to apply to the fragment address to form this
39     /// symbol's value.
40     uint64_t Offset;
41
42     /// CommonSize - The size of the symbol, if it is 'common'.
43     uint64_t CommonSize;
44   };
45
46   /// SymbolSize - An expression describing how to calculate the size of
47   /// a symbol. If a symbol has no size this field will be NULL.
48   const MCExpr *SymbolSize = nullptr;
49
50   /// CommonAlign - The alignment of the symbol, if it is 'common', or -1.
51   //
52   // FIXME: Pack this in with other fields?
53   unsigned CommonAlign = -1U;
54
55   /// Flags - The Flags field is used by object file implementations to store
56   /// additional per symbol information which is not easily classified.
57   uint32_t Flags = 0;
58
59 public:
60   MCSymbolData() { Offset = 0; }
61
62   MCFragment *getFragment() const { return Fragment.getPointer(); }
63   void setFragment(MCFragment *Value) { Fragment.setPointer(Value); }
64
65   uint64_t getOffset() const {
66     assert(!isCommon());
67     return Offset;
68   }
69   void setOffset(uint64_t Value) {
70     assert(!isCommon());
71     Offset = Value;
72   }
73
74   /// @}
75   /// \name Symbol Attributes
76   /// @{
77
78   bool isExternal() const { return Fragment.getInt() & 1; }
79   void setExternal(bool Value) {
80     Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
81   }
82
83   bool isPrivateExtern() const { return Fragment.getInt() & 2; }
84   void setPrivateExtern(bool Value) {
85     Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
86   }
87
88   /// isCommon - Is this a 'common' symbol.
89   bool isCommon() const { return CommonAlign != -1U; }
90
91   /// setCommon - Mark this symbol as being 'common'.
92   ///
93   /// \param Size - The size of the symbol.
94   /// \param Align - The alignment of the symbol.
95   void setCommon(uint64_t Size, unsigned Align) {
96     assert(getOffset() == 0);
97     CommonSize = Size;
98     CommonAlign = Align;
99   }
100
101   /// getCommonSize - Return the size of a 'common' symbol.
102   uint64_t getCommonSize() const {
103     assert(isCommon() && "Not a 'common' symbol!");
104     return CommonSize;
105   }
106
107   void setSize(const MCExpr *SS) { SymbolSize = SS; }
108
109   const MCExpr *getSize() const { return SymbolSize; }
110
111   /// getCommonAlignment - Return the alignment of a 'common' symbol.
112   unsigned getCommonAlignment() const {
113     assert(isCommon() && "Not a 'common' symbol!");
114     return CommonAlign;
115   }
116
117   /// getFlags - Get the (implementation defined) symbol flags.
118   uint32_t getFlags() const { return Flags; }
119
120   /// setFlags - Set the (implementation defined) symbol flags.
121   void setFlags(uint32_t Value) { Flags = Value; }
122
123   /// modifyFlags - Modify the flags via a mask
124   void modifyFlags(uint32_t Value, uint32_t Mask) {
125     Flags = (Flags & ~Mask) | Value;
126   }
127
128   /// @}
129
130   void dump() const;
131 };
132
133 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
134 /// and MCSymbols are created and uniqued by the MCContext class.  MCSymbols
135 /// should only be constructed with valid names for the object file.
136 ///
137 /// If the symbol is defined/emitted into the current translation unit, the
138 /// Section member is set to indicate what section it lives in.  Otherwise, if
139 /// it is a reference to an external entity, it has a null section.
140 class MCSymbol {
141   // Special sentinal value for the absolute pseudo section.
142   //
143   // FIXME: Use a PointerInt wrapper for this?
144   static MCSection *AbsolutePseudoSection;
145
146   /// Name - The name of the symbol.  The referred-to string data is actually
147   /// held by the StringMap that lives in MCContext.
148   const StringMapEntry<bool> *Name;
149
150   /// The section the symbol is defined in. This is null for undefined symbols,
151   /// and the special AbsolutePseudoSection value for absolute symbols. If this
152   /// is a variable symbol, this caches the variable value's section.
153   mutable MCSection *Section;
154
155   /// Value - If non-null, the value for a variable symbol.
156   const MCExpr *Value;
157
158   /// IsTemporary - True if this is an assembler temporary label, which
159   /// typically does not survive in the .o file's symbol table.  Usually
160   /// "Lfoo" or ".foo".
161   unsigned IsTemporary : 1;
162
163   /// \brief True if this symbol can be redefined.
164   unsigned IsRedefinable : 1;
165
166   /// IsUsed - True if this symbol has been used.
167   mutable unsigned IsUsed : 1;
168
169   mutable bool HasData : 1;
170
171   /// Index field, for use by the object file implementation.
172   mutable uint64_t Index : 60;
173
174   mutable MCSymbolData Data;
175
176 private: // MCContext creates and uniques these.
177   friend class MCExpr;
178   friend class MCContext;
179   MCSymbol(const StringMapEntry<bool> *Name, bool isTemporary)
180       : Name(Name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
181         IsRedefinable(false), IsUsed(false), HasData(false), Index(0) {}
182
183   MCSymbol(const MCSymbol &) = delete;
184   void operator=(const MCSymbol &) = delete;
185   MCSection *getSectionPtr() const {
186     if (Section || !Value)
187       return Section;
188     return Section = Value->FindAssociatedSection();
189   }
190
191 public:
192   /// getName - Get the symbol name.
193   StringRef getName() const { return Name ? Name->first() : ""; }
194
195   bool hasData() const { return HasData; }
196
197   /// Get associated symbol data.
198   MCSymbolData &getData() const {
199     assert(HasData && "Missing symbol data!");
200     return Data;
201   }
202
203   /// Initialize symbol data.
204   ///
205   /// Nothing really to do here, but this is enables an assertion that \a
206   /// MCAssembler::getOrCreateSymbolData() has actually been called before
207   /// anyone calls \a getData().
208   void initializeData() const { HasData = true; }
209
210   /// \name Accessors
211   /// @{
212
213   /// isTemporary - Check if this is an assembler temporary symbol.
214   bool isTemporary() const { return IsTemporary; }
215
216   /// isUsed - Check if this is used.
217   bool isUsed() const { return IsUsed; }
218   void setUsed(bool Value) const { IsUsed = Value; }
219
220   /// \brief Check if this symbol is redefinable.
221   bool isRedefinable() const { return IsRedefinable; }
222   /// \brief Mark this symbol as redefinable.
223   void setRedefinable(bool Value) { IsRedefinable = Value; }
224   /// \brief Prepare this symbol to be redefined.
225   void redefineIfPossible() {
226     if (IsRedefinable) {
227       Value = nullptr;
228       Section = nullptr;
229       IsRedefinable = false;
230     }
231   }
232
233   /// @}
234   /// \name Associated Sections
235   /// @{
236
237   /// isDefined - Check if this symbol is defined (i.e., it has an address).
238   ///
239   /// Defined symbols are either absolute or in some section.
240   bool isDefined() const { return getSectionPtr() != nullptr; }
241
242   /// isInSection - Check if this symbol is defined in some section (i.e., it
243   /// is defined but not absolute).
244   bool isInSection() const { return isDefined() && !isAbsolute(); }
245
246   /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
247   bool isUndefined() const { return !isDefined(); }
248
249   /// isAbsolute - Check if this is an absolute symbol.
250   bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
251
252   /// Get the section associated with a defined, non-absolute symbol.
253   MCSection &getSection() const {
254     assert(isInSection() && "Invalid accessor!");
255     return *getSectionPtr();
256   }
257
258   /// Mark the symbol as defined in the section \p S.
259   void setSection(MCSection &S) {
260     assert(!isVariable() && "Cannot set section of variable");
261     Section = &S;
262   }
263
264   /// setUndefined - Mark the symbol as undefined.
265   void setUndefined() { Section = nullptr; }
266
267   /// @}
268   /// \name Variable Symbols
269   /// @{
270
271   /// isVariable - Check if this is a variable symbol.
272   bool isVariable() const { return Value != nullptr; }
273
274   /// getVariableValue() - Get the value for variable symbols.
275   const MCExpr *getVariableValue() const {
276     assert(isVariable() && "Invalid accessor!");
277     IsUsed = true;
278     return Value;
279   }
280
281   void setVariableValue(const MCExpr *Value);
282
283   /// @}
284
285   /// Get the (implementation defined) index.
286   uint64_t getIndex() const {
287     assert(HasData && "Uninitialized symbol data");
288     return Index;
289   }
290
291   /// Set the (implementation defined) index.
292   void setIndex(uint64_t Value) const {
293     assert(HasData && "Uninitialized symbol data");
294     assert(!(Value >> 60) && "Not enough bits for value");
295     Index = Value;
296   }
297
298   /// print - Print the value to the stream \p OS.
299   void print(raw_ostream &OS) const;
300
301   /// dump - Print the value to stderr.
302   void dump() const;
303 };
304
305 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
306   Sym.print(OS);
307   return OS;
308 }
309 } // end namespace llvm
310
311 #endif