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