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