[MC] Common symbols weren't being checked for redeclaration which allowed an assembly...
[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   unsigned IsELF : 1;
87
88   /// Index field, for use by the object file implementation.
89   mutable uint32_t Index = 0;
90
91   union {
92     /// The offset to apply to the fragment address to form this symbol's value.
93     uint64_t Offset;
94
95     /// The size of the symbol, if it is 'common'.
96     uint64_t CommonSize;
97   };
98
99   /// The alignment of the symbol, if it is 'common', or -1.
100   //
101   // FIXME: Pack this in with other fields?
102   unsigned CommonAlign = -1U;
103
104   /// The Flags field is used by object file implementations to store
105   /// additional per symbol information which is not easily classified.
106   mutable uint32_t Flags = 0;
107
108 protected: // MCContext creates and uniques these.
109   friend class MCExpr;
110   friend class MCContext;
111   MCSymbol(bool IsELF, const StringMapEntry<bool> *Name, bool isTemporary)
112       : Name(Name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
113         IsRedefinable(false), IsUsed(false), IsRegistered(false),
114         IsExternal(false), IsPrivateExtern(false), HasFragment(false),
115         IsELF(IsELF) {
116     Offset = 0;
117   }
118
119 private:
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   bool isELF() const { return IsELF; }
201
202   /// @}
203   /// \name Variable Symbols
204   /// @{
205
206   /// isVariable - Check if this is a variable symbol.
207   bool isVariable() const { return Value != nullptr; }
208
209   /// getVariableValue() - Get the value for variable symbols.
210   const MCExpr *getVariableValue() const {
211     assert(isVariable() && "Invalid accessor!");
212     IsUsed = true;
213     return Value;
214   }
215
216   void setVariableValue(const MCExpr *Value);
217
218   /// @}
219
220   /// Get the (implementation defined) index.
221   uint32_t getIndex() const {
222     return Index;
223   }
224
225   /// Set the (implementation defined) index.
226   void setIndex(uint32_t Value) const {
227     Index = Value;
228   }
229
230   uint64_t getOffset() const {
231     assert(!isCommon());
232     return Offset;
233   }
234   void setOffset(uint64_t Value) {
235     assert(!isCommon());
236     Offset = Value;
237   }
238
239   /// Return the size of a 'common' symbol.
240   uint64_t getCommonSize() const {
241     assert(isCommon() && "Not a 'common' symbol!");
242     return CommonSize;
243   }
244
245   /// Mark this symbol as being 'common'.
246   ///
247   /// \param Size - The size of the symbol.
248   /// \param Align - The alignment of the symbol.
249   void setCommon(uint64_t Size, unsigned Align) {
250     assert(getOffset() == 0);
251     CommonSize = Size;
252     CommonAlign = Align;
253   }
254
255   ///  Return the alignment of a 'common' symbol.
256   unsigned getCommonAlignment() const {
257     assert(isCommon() && "Not a 'common' symbol!");
258     return CommonAlign;
259   }
260
261   /// Declare this symbol as being 'common'.
262   ///
263   /// \param Size - The size of the symbol.
264   /// \param Align - The alignment of the symbol.
265   /// \return True if symbol was already declared as a different type
266   bool declareCommon(uint64_t Size, unsigned Align) {
267     assert(isCommon() || getOffset() == 0);
268     if(isCommon()) {
269       if(CommonSize != Size || CommonAlign != Align)
270        return true;
271     } else
272       setCommon(Size, Align);
273     return false;
274   }
275
276   /// Is this a 'common' symbol.
277   bool isCommon() const { return CommonAlign != -1U; }
278
279   /// Get the (implementation defined) symbol flags.
280   uint32_t getFlags() const { return Flags; }
281
282   /// Set the (implementation defined) symbol flags.
283   void setFlags(uint32_t Value) const { Flags = Value; }
284
285   /// Modify the flags via a mask
286   void modifyFlags(uint32_t Value, uint32_t Mask) const {
287     Flags = (Flags & ~Mask) | Value;
288   }
289
290   MCFragment *getFragment() const {
291     if (!HasFragment)
292       return nullptr;
293     return Fragment;
294   }
295   void setFragment(MCFragment *Value) const {
296     HasFragment = true;
297     Fragment = Value;
298   }
299
300   bool isExternal() const { return IsExternal; }
301   void setExternal(bool Value) const { IsExternal = Value; }
302
303   bool isPrivateExtern() const { return IsPrivateExtern; }
304   void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
305
306   /// print - Print the value to the stream \p OS.
307   void print(raw_ostream &OS) const;
308
309   /// dump - Print the value to stderr.
310   void dump() const;
311 };
312
313 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
314   Sym.print(OS);
315   return OS;
316 }
317 } // end namespace llvm
318
319 #endif