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