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