MC: Make MCSymbolData::Symbol private
[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   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   void initialize(const MCSymbol &Symbol, MCFragment *Fragment,
68                   uint64_t Offset);
69
70   /// \name Accessors
71   /// @{
72   bool isInitialized() 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   mutable MCSymbolData Data;
189
190 private: // MCContext creates and uniques these.
191   friend class MCExpr;
192   friend class MCContext;
193   MCSymbol(StringRef name, bool isTemporary)
194       : Name(name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
195         IsRedefinable(false), IsUsed(false) {}
196
197   MCSymbol(const MCSymbol &) = delete;
198   void operator=(const MCSymbol &) = delete;
199   const MCSection *getSectionPtr() const {
200     if (Section || !Value)
201       return Section;
202     return Section = Value->FindAssociatedSection();
203   }
204
205 public:
206   /// getName - Get the symbol name.
207   StringRef getName() const { return Name; }
208
209   /// Get associated symbol data.
210   MCSymbolData &getData() const {
211     assert(Data.isInitialized() && "Missing symbol data!");
212     return Data;
213   }
214
215   /// Get unsafe symbol data (even if uninitialized).
216   ///
217   /// Don't assert on uninitialized data; just return it.
218   MCSymbolData &getUnsafeData() const { return Data; }
219
220   /// \name Accessors
221   /// @{
222
223   /// isTemporary - Check if this is an assembler temporary symbol.
224   bool isTemporary() const { return IsTemporary; }
225
226   /// isUsed - Check if this is used.
227   bool isUsed() const { return IsUsed; }
228   void setUsed(bool Value) const { IsUsed = Value; }
229
230   /// \brief Check if this symbol is redefinable.
231   bool isRedefinable() const { return IsRedefinable; }
232   /// \brief Mark this symbol as redefinable.
233   void setRedefinable(bool Value) { IsRedefinable = Value; }
234   /// \brief Prepare this symbol to be redefined.
235   void redefineIfPossible() {
236     if (IsRedefinable) {
237       Value = nullptr;
238       Section = nullptr;
239       IsRedefinable = false;
240     }
241   }
242
243   /// @}
244   /// \name Associated Sections
245   /// @{
246
247   /// isDefined - Check if this symbol is defined (i.e., it has an address).
248   ///
249   /// Defined symbols are either absolute or in some section.
250   bool isDefined() const { return getSectionPtr() != nullptr; }
251
252   /// isInSection - Check if this symbol is defined in some section (i.e., it
253   /// is defined but not absolute).
254   bool isInSection() const { return isDefined() && !isAbsolute(); }
255
256   /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
257   bool isUndefined() const { return !isDefined(); }
258
259   /// isAbsolute - Check if this is an absolute symbol.
260   bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
261
262   /// getSection - Get the section associated with a defined, non-absolute
263   /// symbol.
264   const MCSection &getSection() const {
265     assert(isInSection() && "Invalid accessor!");
266     return *getSectionPtr();
267   }
268
269   /// setSection - Mark the symbol as defined in the section \p S.
270   void setSection(const MCSection &S) {
271     assert(!isVariable() && "Cannot set section of variable");
272     Section = &S;
273   }
274
275   /// setUndefined - Mark the symbol as undefined.
276   void setUndefined() { Section = nullptr; }
277
278   /// @}
279   /// \name Variable Symbols
280   /// @{
281
282   /// isVariable - Check if this is a variable symbol.
283   bool isVariable() const { return Value != nullptr; }
284
285   /// getVariableValue() - Get the value for variable symbols.
286   const MCExpr *getVariableValue() const {
287     assert(isVariable() && "Invalid accessor!");
288     IsUsed = true;
289     return Value;
290   }
291
292   void setVariableValue(const MCExpr *Value);
293
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