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