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