Correctly handle an ELF symbol defined with "a = b + expr".
[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/Support/Compiler.h"
19
20 namespace llvm {
21   class MCAsmLayout;
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 unique'd 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.
47     const MCSection *Section;
48
49     /// Value - If non-null, the value for a variable symbol.
50     const MCExpr *Value;
51
52     /// IsTemporary - True if this is an assembler temporary label, which
53     /// typically does not survive in the .o file's symbol table.  Usually
54     /// "Lfoo" or ".foo".
55     unsigned IsTemporary : 1;
56
57     /// IsUsed - True if this symbol has been used.
58     mutable unsigned IsUsed : 1;
59
60   private:  // MCContext creates and uniques these.
61     friend class MCExpr;
62     friend class MCContext;
63     MCSymbol(StringRef name, bool isTemporary)
64       : Name(name), Section(0), Value(0),
65         IsTemporary(isTemporary), IsUsed(false) {}
66
67     MCSymbol(const MCSymbol&) LLVM_DELETED_FUNCTION;
68     void operator=(const MCSymbol&) LLVM_DELETED_FUNCTION;
69   public:
70     /// getName - Get the symbol name.
71     StringRef getName() const { return Name; }
72
73     /// @name Accessors
74     /// @{
75
76     /// isTemporary - Check if this is an assembler temporary symbol.
77     bool isTemporary() const { return IsTemporary; }
78
79     /// isUsed - Check if this is used.
80     bool isUsed() const { return IsUsed; }
81     void setUsed(bool Value) const { IsUsed = Value; }
82
83     /// @}
84     /// @name Associated Sections
85     /// @{
86
87     /// isDefined - Check if this symbol is defined (i.e., it has an address).
88     ///
89     /// Defined symbols are either absolute or in some section.
90     bool isDefined() const {
91       return Section != 0;
92     }
93
94     /// isInSection - Check if this symbol is defined in some section (i.e., it
95     /// is defined but not absolute).
96     bool isInSection() const {
97       return isDefined() && !isAbsolute();
98     }
99
100     /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
101     bool isUndefined() const {
102       return !isDefined();
103     }
104
105     /// isAbsolute - Check if this is an absolute symbol.
106     bool isAbsolute() const {
107       return Section == AbsolutePseudoSection;
108     }
109
110     /// getSection - Get the section associated with a defined, non-absolute
111     /// symbol.
112     const MCSection &getSection() const {
113       assert(isInSection() && "Invalid accessor!");
114       return *Section;
115     }
116
117     /// setSection - Mark the symbol as defined in the section \p S.
118     void setSection(const MCSection &S) { Section = &S; }
119
120     /// setUndefined - Mark the symbol as undefined.
121     void setUndefined() {
122       Section = 0;
123     }
124
125     /// setAbsolute - Mark the symbol as absolute.
126     void setAbsolute() { Section = AbsolutePseudoSection; }
127
128     /// @}
129     /// @name Variable Symbols
130     /// @{
131
132     /// isVariable - Check if this is a variable symbol.
133     bool isVariable() const {
134       return Value != 0;
135     }
136
137     /// getVariableValue() - Get the value for variable symbols.
138     const MCExpr *getVariableValue() const {
139       assert(isVariable() && "Invalid accessor!");
140       IsUsed = true;
141       return Value;
142     }
143
144     // AliasedSymbol() - If this is an alias (a = b), return the symbol
145     // we ultimately point to. For a non-alias, this just returns the symbol
146     // itself.
147     const MCSymbol &AliasedSymbol() const;
148
149     // If this symbol is not a variable, return itself. If it is a variable,
150     // evaluate it and check if it is of the form Base + ConstantOffset. If so,
151     // return Base, if not, return nullptr.
152     const MCSymbol *getBaseSymbol(const MCAsmLayout &Layout) const;
153
154     void setVariableValue(const MCExpr *Value);
155
156     /// @}
157
158     /// print - Print the value to the stream \p OS.
159     void print(raw_ostream &OS) const;
160
161     /// dump - Print the value to stderr.
162     void dump() const;
163   };
164
165   inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
166     Sym.print(OS);
167     return OS;
168   }
169 } // end namespace llvm
170
171 #endif