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