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