355ed85deb5b683271396c07e6b76d67fed3d9bc
[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 uint32_t Index = 0;
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   union {
76     /// The offset to apply to the fragment address to form this symbol's value.
77     uint64_t Offset;
78
79     /// The size of the symbol, if it is 'common'.
80     uint64_t CommonSize;
81   };
82
83   /// The alignment of the symbol, if it is 'common', or -1.
84   //
85   // FIXME: Pack this in with other fields?
86   unsigned CommonAlign = -1U;
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) {
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   /// Initialize symbol data.
121   ///
122   /// Nothing really to do here, but this is enables an assertion that \a
123   /// MCAssembler::getOrCreateSymbolData() has actually been called before
124   /// anyone calls \a getData().
125   void initializeData() const { HasData = true; }
126
127   /// \name Accessors
128   /// @{
129
130   /// isTemporary - Check if this is an assembler temporary symbol.
131   bool isTemporary() const { return IsTemporary; }
132
133   /// isUsed - Check if this is used.
134   bool isUsed() const { return IsUsed; }
135   void setUsed(bool Value) const { IsUsed = Value; }
136
137   /// \brief Check if this symbol is redefinable.
138   bool isRedefinable() const { return IsRedefinable; }
139   /// \brief Mark this symbol as redefinable.
140   void setRedefinable(bool Value) { IsRedefinable = Value; }
141   /// \brief Prepare this symbol to be redefined.
142   void redefineIfPossible() {
143     if (IsRedefinable) {
144       Value = nullptr;
145       Section = nullptr;
146       IsRedefinable = false;
147     }
148   }
149
150   /// @}
151   /// \name Associated Sections
152   /// @{
153
154   /// isDefined - Check if this symbol is defined (i.e., it has an address).
155   ///
156   /// Defined symbols are either absolute or in some section.
157   bool isDefined() const { return getSectionPtr() != nullptr; }
158
159   /// isInSection - Check if this symbol is defined in some section (i.e., it
160   /// is defined but not absolute).
161   bool isInSection() const { return isDefined() && !isAbsolute(); }
162
163   /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
164   bool isUndefined() const { return !isDefined(); }
165
166   /// isAbsolute - Check if this is an absolute symbol.
167   bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
168
169   /// Get the section associated with a defined, non-absolute symbol.
170   MCSection &getSection() const {
171     assert(isInSection() && "Invalid accessor!");
172     return *getSectionPtr();
173   }
174
175   /// Mark the symbol as defined in the section \p S.
176   void setSection(MCSection &S) {
177     assert(!isVariable() && "Cannot set section of variable");
178     Section = &S;
179   }
180
181   /// setUndefined - Mark the symbol as undefined.
182   void setUndefined() { Section = nullptr; }
183
184   /// @}
185   /// \name Variable Symbols
186   /// @{
187
188   /// isVariable - Check if this is a variable symbol.
189   bool isVariable() const { return Value != nullptr; }
190
191   /// getVariableValue() - Get the value for variable symbols.
192   const MCExpr *getVariableValue() const {
193     assert(isVariable() && "Invalid accessor!");
194     IsUsed = true;
195     return Value;
196   }
197
198   void setVariableValue(const MCExpr *Value);
199
200   /// @}
201
202   /// Get the (implementation defined) index.
203   uint32_t getIndex() const {
204     assert(HasData && "Uninitialized symbol data");
205     return Index;
206   }
207
208   /// Set the (implementation defined) index.
209   void setIndex(uint32_t Value) const {
210     assert(HasData && "Uninitialized symbol data");
211     Index = Value;
212   }
213
214   void setSize(const MCExpr *SS) { SymbolSize = SS; }
215
216   const MCExpr *getSize() const { return SymbolSize; }
217
218   uint64_t getOffset() const {
219     assert(!isCommon());
220     return Offset;
221   }
222   void setOffset(uint64_t Value) {
223     assert(!isCommon());
224     Offset = Value;
225   }
226
227   /// Return the size of a 'common' symbol.
228   uint64_t getCommonSize() const {
229     assert(isCommon() && "Not a 'common' symbol!");
230     return CommonSize;
231   }
232
233   /// Mark this symbol as being 'common'.
234   ///
235   /// \param Size - The size of the symbol.
236   /// \param Align - The alignment of the symbol.
237   void setCommon(uint64_t Size, unsigned Align) {
238     assert(getOffset() == 0);
239     CommonSize = Size;
240     CommonAlign = Align;
241   }
242
243   ///  Return the alignment of a 'common' symbol.
244   unsigned getCommonAlignment() const {
245     assert(isCommon() && "Not a 'common' symbol!");
246     return CommonAlign;
247   }
248
249   /// Is this a 'common' symbol.
250   bool isCommon() const { return CommonAlign != -1U; }
251
252   /// Get the (implementation defined) symbol flags.
253   uint32_t getFlags() const { return Flags; }
254
255   /// Set the (implementation defined) symbol flags.
256   void setFlags(uint32_t Value) const { Flags = Value; }
257
258   /// Modify the flags via a mask
259   void modifyFlags(uint32_t Value, uint32_t Mask) const {
260     Flags = (Flags & ~Mask) | Value;
261   }
262
263   MCFragment *getFragment() const { return Fragment.getPointer(); }
264   void setFragment(MCFragment *Value) const { Fragment.setPointer(Value); }
265
266   bool isExternal() const { return Fragment.getInt() & 1; }
267   void setExternal(bool Value) const {
268     Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
269   }
270
271   bool isPrivateExtern() const { return Fragment.getInt() & 2; }
272   void setPrivateExtern(bool Value) {
273     Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
274   }
275
276   /// print - Print the value to the stream \p OS.
277   void print(raw_ostream &OS) const;
278
279   /// dump - Print the value to stderr.
280   void dump() const;
281 };
282
283 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
284   Sym.print(OS);
285   return OS;
286 }
287 } // end namespace llvm
288
289 #endif