270359c63071d8e2cbdb10090dedce86d4fecfa2
[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/StringRef.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/Support/Compiler.h"
20
21 namespace llvm {
22 class MCExpr;
23 class MCSection;
24 class MCContext;
25 class raw_ostream;
26
27 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
28 /// and MCSymbols are created and uniqued by the MCContext class.  MCSymbols
29 /// should only be constructed with valid names for the object file.
30 ///
31 /// If the symbol is defined/emitted into the current translation unit, the
32 /// Section member is set to indicate what section it lives in.  Otherwise, if
33 /// it is a reference to an external entity, it has a null section.
34 class MCSymbol {
35   // Special sentinal value for the absolute pseudo section.
36   //
37   // FIXME: Use a PointerInt wrapper for this?
38   static const MCSection *AbsolutePseudoSection;
39
40   /// Name - The name of the symbol.  The referred-to string data is actually
41   /// held by the StringMap that lives in MCContext.
42   StringRef Name;
43
44   /// Section - The section the symbol is defined in. This is null for
45   /// undefined symbols, and the special AbsolutePseudoSection value for
46   /// absolute symbols. If this is a variable symbol, this caches the
47   /// variable value's section.
48   mutable const MCSection *Section;
49
50   /// Value - If non-null, the value for a variable symbol.
51   const MCExpr *Value;
52
53   /// IsTemporary - True if this is an assembler temporary label, which
54   /// typically does not survive in the .o file's symbol table.  Usually
55   /// "Lfoo" or ".foo".
56   unsigned IsTemporary : 1;
57
58   /// \brief True if this symbol can be redefined.
59   unsigned IsRedefinable : 1;
60
61   /// IsUsed - True if this symbol has been used.
62   mutable unsigned IsUsed : 1;
63
64 private: // MCContext creates and uniques these.
65   friend class MCExpr;
66   friend class MCContext;
67   MCSymbol(StringRef name, bool isTemporary)
68       : Name(name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
69         IsRedefinable(false), IsUsed(false) {}
70
71   MCSymbol(const MCSymbol &) = delete;
72   void operator=(const MCSymbol &) = delete;
73   const MCSection *getSectionPtr() const {
74     if (Section || !Value)
75       return Section;
76     return Section = Value->FindAssociatedSection();
77   }
78
79 public:
80   /// getName - Get the symbol name.
81   StringRef getName() const { return Name; }
82
83   /// \name Accessors
84   /// @{
85
86   /// isTemporary - Check if this is an assembler temporary symbol.
87   bool isTemporary() const { return IsTemporary; }
88
89   /// isUsed - Check if this is used.
90   bool isUsed() const { return IsUsed; }
91   void setUsed(bool Value) const { IsUsed = Value; }
92
93   /// \brief Check if this symbol is redefinable.
94   bool isRedefinable() const { return IsRedefinable; }
95   /// \brief Mark this symbol as redefinable.
96   void setRedefinable(bool Value) { IsRedefinable = Value; }
97   /// \brief Prepare this symbol to be redefined.
98   void redefineIfPossible() {
99     if (IsRedefinable) {
100       Value = nullptr;
101       Section = nullptr;
102       IsRedefinable = false;
103     }
104   }
105
106   /// @}
107   /// \name Associated Sections
108   /// @{
109
110   /// isDefined - Check if this symbol is defined (i.e., it has an address).
111   ///
112   /// Defined symbols are either absolute or in some section.
113   bool isDefined() const { return getSectionPtr() != nullptr; }
114
115   /// isInSection - Check if this symbol is defined in some section (i.e., it
116   /// is defined but not absolute).
117   bool isInSection() const { return isDefined() && !isAbsolute(); }
118
119   /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
120   bool isUndefined() const { return !isDefined(); }
121
122   /// isAbsolute - Check if this is an absolute symbol.
123   bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
124
125   /// getSection - Get the section associated with a defined, non-absolute
126   /// symbol.
127   const MCSection &getSection() const {
128     assert(isInSection() && "Invalid accessor!");
129     return *getSectionPtr();
130   }
131
132   /// setSection - Mark the symbol as defined in the section \p S.
133   void setSection(const MCSection &S) {
134     assert(!isVariable() && "Cannot set section of variable");
135     Section = &S;
136   }
137
138   /// setUndefined - Mark the symbol as undefined.
139   void setUndefined() { Section = nullptr; }
140
141   /// @}
142   /// \name Variable Symbols
143   /// @{
144
145   /// isVariable - Check if this is a variable symbol.
146   bool isVariable() const { return Value != nullptr; }
147
148   /// getVariableValue() - Get the value for variable symbols.
149   const MCExpr *getVariableValue() const {
150     assert(isVariable() && "Invalid accessor!");
151     IsUsed = true;
152     return Value;
153   }
154
155   void setVariableValue(const MCExpr *Value);
156
157   /// @}
158
159   /// print - Print the value to the stream \p OS.
160   void print(raw_ostream &OS) const;
161
162   /// dump - Print the value to stderr.
163   void dump() const;
164 };
165
166 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
167   Sym.print(OS);
168   return OS;
169 }
170 } // end namespace llvm
171
172 #endif