b39c271335156d159b79e87fc58a970ae1e187a6
[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/StringRef.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   StringRef 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   mutable MCSymbolData Data;
171
172   /// Index field, for use by the object file implementation.
173   mutable uint64_t Index = 0;
174
175 private: // MCContext creates and uniques these.
176   friend class MCExpr;
177   friend class MCContext;
178   MCSymbol(StringRef name, bool isTemporary)
179       : Name(name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
180         IsRedefinable(false), IsUsed(false), HasData(false) {}
181
182   MCSymbol(const MCSymbol &) = delete;
183   void operator=(const MCSymbol &) = delete;
184   MCSection *getSectionPtr() const {
185     if (Section || !Value)
186       return Section;
187     return Section = Value->FindAssociatedSection();
188   }
189
190 public:
191   /// getName - Get the symbol name.
192   StringRef getName() const { return Name; }
193
194   bool hasData() const { return HasData; }
195
196   /// Get associated symbol data.
197   MCSymbolData &getData() const {
198     assert(HasData && "Missing symbol data!");
199     return Data;
200   }
201
202   /// Initialize symbol data.
203   ///
204   /// Nothing really to do here, but this is enables an assertion that \a
205   /// MCAssembler::getOrCreateSymbolData() has actually been called before
206   /// anyone calls \a getData().
207   void initializeData() const { HasData = true; }
208
209   /// \name Accessors
210   /// @{
211
212   /// isTemporary - Check if this is an assembler temporary symbol.
213   bool isTemporary() const { return IsTemporary; }
214
215   /// isUsed - Check if this is used.
216   bool isUsed() const { return IsUsed; }
217   void setUsed(bool Value) const { IsUsed = Value; }
218
219   /// \brief Check if this symbol is redefinable.
220   bool isRedefinable() const { return IsRedefinable; }
221   /// \brief Mark this symbol as redefinable.
222   void setRedefinable(bool Value) { IsRedefinable = Value; }
223   /// \brief Prepare this symbol to be redefined.
224   void redefineIfPossible() {
225     if (IsRedefinable) {
226       Value = nullptr;
227       Section = nullptr;
228       IsRedefinable = false;
229     }
230   }
231
232   /// @}
233   /// \name Associated Sections
234   /// @{
235
236   /// isDefined - Check if this symbol is defined (i.e., it has an address).
237   ///
238   /// Defined symbols are either absolute or in some section.
239   bool isDefined() const { return getSectionPtr() != nullptr; }
240
241   /// isInSection - Check if this symbol is defined in some section (i.e., it
242   /// is defined but not absolute).
243   bool isInSection() const { return isDefined() && !isAbsolute(); }
244
245   /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
246   bool isUndefined() const { return !isDefined(); }
247
248   /// isAbsolute - Check if this is an absolute symbol.
249   bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
250
251   /// Get the section associated with a defined, non-absolute symbol.
252   MCSection &getSection() const {
253     assert(isInSection() && "Invalid accessor!");
254     return *getSectionPtr();
255   }
256
257   /// Mark the symbol as defined in the section \p S.
258   void setSection(MCSection &S) {
259     assert(!isVariable() && "Cannot set section of variable");
260     Section = &S;
261   }
262
263   /// setUndefined - Mark the symbol as undefined.
264   void setUndefined() { Section = nullptr; }
265
266   /// @}
267   /// \name Variable Symbols
268   /// @{
269
270   /// isVariable - Check if this is a variable symbol.
271   bool isVariable() const { return Value != nullptr; }
272
273   /// getVariableValue() - Get the value for variable symbols.
274   const MCExpr *getVariableValue() const {
275     assert(isVariable() && "Invalid accessor!");
276     IsUsed = true;
277     return Value;
278   }
279
280   void setVariableValue(const MCExpr *Value);
281
282   /// @}
283
284   /// Get the (implementation defined) index.
285   uint64_t getIndex() const {
286     assert(HasData && "Uninitialized symbol data");
287     return Index;
288   }
289
290   /// Set the (implementation defined) index.
291   void setIndex(uint64_t Value) const {
292     assert(HasData && "Uninitialized symbol data");
293     Index = Value;
294   }
295
296   /// print - Print the value to the stream \p OS.
297   void print(raw_ostream &OS) const;
298
299   /// dump - Print the value to stderr.
300   void dump() const;
301 };
302
303 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
304   Sym.print(OS);
305   return OS;
306 }
307 } // end namespace llvm
308
309 #endif