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