1195098Sed//===- MCSymbol.h - Machine Code Symbols ------------------------*- C++ -*-===//
2195098Sed//
3195098Sed//                     The LLVM Compiler Infrastructure
4195098Sed//
5195098Sed// This file is distributed under the University of Illinois Open Source
6195098Sed// License. See LICENSE.TXT for details.
7195098Sed//
8195098Sed//===----------------------------------------------------------------------===//
9195340Sed//
10195340Sed// This file contains the declaration of the MCSymbol class.
11195340Sed//
12195340Sed//===----------------------------------------------------------------------===//
13195098Sed
14195098Sed#ifndef LLVM_MC_MCSYMBOL_H
15195098Sed#define LLVM_MC_MCSYMBOL_H
16195098Sed
17198090Srdivacky#include "llvm/ADT/StringRef.h"
18245431Sdim#include "llvm/Support/Compiler.h"
19195098Sed
20195098Sednamespace llvm {
21198396Srdivacky  class MCExpr;
22195340Sed  class MCSection;
23195340Sed  class MCContext;
24198090Srdivacky  class raw_ostream;
25195340Sed
26195340Sed  /// MCSymbol - Instances of this class represent a symbol name in the MC file,
27202878Srdivacky  /// and MCSymbols are created and unique'd by the MCContext class.  MCSymbols
28202878Srdivacky  /// should only be constructed with valid names for the object file.
29195340Sed  ///
30195340Sed  /// If the symbol is defined/emitted into the current translation unit, the
31195340Sed  /// Section member is set to indicate what section it lives in.  Otherwise, if
32205407Srdivacky  /// it is a reference to an external entity, it has a null section.
33195098Sed  class MCSymbol {
34198090Srdivacky    // Special sentinal value for the absolute pseudo section.
35198090Srdivacky    //
36198090Srdivacky    // FIXME: Use a PointerInt wrapper for this?
37198090Srdivacky    static const MCSection *AbsolutePseudoSection;
38198090Srdivacky
39205218Srdivacky    /// Name - The name of the symbol.  The referred-to string data is actually
40205218Srdivacky    /// held by the StringMap that lives in MCContext.
41205218Srdivacky    StringRef Name;
42198090Srdivacky
43198090Srdivacky    /// Section - The section the symbol is defined in. This is null for
44198090Srdivacky    /// undefined symbols, and the special AbsolutePseudoSection value for
45198090Srdivacky    /// absolute symbols.
46198090Srdivacky    const MCSection *Section;
47198090Srdivacky
48198396Srdivacky    /// Value - If non-null, the value for a variable symbol.
49198396Srdivacky    const MCExpr *Value;
50198396Srdivacky
51195340Sed    /// IsTemporary - True if this is an assembler temporary label, which
52195340Sed    /// typically does not survive in the .o file's symbol table.  Usually
53195340Sed    /// "Lfoo" or ".foo".
54195098Sed    unsigned IsTemporary : 1;
55205407Srdivacky
56218893Sdim    /// IsUsed - True if this symbol has been used.
57218893Sdim    mutable unsigned IsUsed : 1;
58208599Srdivacky
59195340Sed  private:  // MCContext creates and uniques these.
60221345Sdim    friend class MCExpr;
61195340Sed    friend class MCContext;
62205218Srdivacky    MCSymbol(StringRef name, bool isTemporary)
63208599Srdivacky      : Name(name), Section(0), Value(0),
64218893Sdim        IsTemporary(isTemporary), IsUsed(false) {}
65198396Srdivacky
66245431Sdim    MCSymbol(const MCSymbol&) LLVM_DELETED_FUNCTION;
67245431Sdim    void operator=(const MCSymbol&) LLVM_DELETED_FUNCTION;
68195098Sed  public:
69198090Srdivacky    /// getName - Get the symbol name.
70205218Srdivacky    StringRef getName() const { return Name; }
71195098Sed
72208599Srdivacky    /// @name Accessors
73198090Srdivacky    /// @{
74195340Sed
75198090Srdivacky    /// isTemporary - Check if this is an assembler temporary symbol.
76208599Srdivacky    bool isTemporary() const { return IsTemporary; }
77198090Srdivacky
78218893Sdim    /// isUsed - Check if this is used.
79218893Sdim    bool isUsed() const { return IsUsed; }
80218893Sdim    void setUsed(bool Value) const { IsUsed = Value; }
81208599Srdivacky
82198396Srdivacky    /// @}
83198396Srdivacky    /// @name Associated Sections
84198396Srdivacky    /// @{
85198396Srdivacky
86198090Srdivacky    /// isDefined - Check if this symbol is defined (i.e., it has an address).
87198090Srdivacky    ///
88198090Srdivacky    /// Defined symbols are either absolute or in some section.
89198090Srdivacky    bool isDefined() const {
90198090Srdivacky      return Section != 0;
91198090Srdivacky    }
92198090Srdivacky
93205407Srdivacky    /// isInSection - Check if this symbol is defined in some section (i.e., it
94205407Srdivacky    /// is defined but not absolute).
95205407Srdivacky    bool isInSection() const {
96205407Srdivacky      return isDefined() && !isAbsolute();
97205407Srdivacky    }
98205407Srdivacky
99198090Srdivacky    /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
100198090Srdivacky    bool isUndefined() const {
101198090Srdivacky      return !isDefined();
102198090Srdivacky    }
103198090Srdivacky
104203954Srdivacky    /// isAbsolute - Check if this is an absolute symbol.
105198090Srdivacky    bool isAbsolute() const {
106198090Srdivacky      return Section == AbsolutePseudoSection;
107198090Srdivacky    }
108198090Srdivacky
109198090Srdivacky    /// getSection - Get the section associated with a defined, non-absolute
110198090Srdivacky    /// symbol.
111198090Srdivacky    const MCSection &getSection() const {
112205407Srdivacky      assert(isInSection() && "Invalid accessor!");
113198090Srdivacky      return *Section;
114198090Srdivacky    }
115198090Srdivacky
116245431Sdim    /// setSection - Mark the symbol as defined in the section \p S.
117198090Srdivacky    void setSection(const MCSection &S) { Section = &S; }
118198090Srdivacky
119198090Srdivacky    /// setUndefined - Mark the symbol as undefined.
120198090Srdivacky    void setUndefined() {
121198090Srdivacky      Section = 0;
122198090Srdivacky    }
123198090Srdivacky
124198090Srdivacky    /// setAbsolute - Mark the symbol as absolute.
125198090Srdivacky    void setAbsolute() { Section = AbsolutePseudoSection; }
126198090Srdivacky
127198090Srdivacky    /// @}
128198396Srdivacky    /// @name Variable Symbols
129198396Srdivacky    /// @{
130198090Srdivacky
131198396Srdivacky    /// isVariable - Check if this is a variable symbol.
132198396Srdivacky    bool isVariable() const {
133198396Srdivacky      return Value != 0;
134198396Srdivacky    }
135198396Srdivacky
136245431Sdim    /// getVariableValue() - Get the value for variable symbols.
137208599Srdivacky    const MCExpr *getVariableValue() const {
138208599Srdivacky      assert(isVariable() && "Invalid accessor!");
139218893Sdim      IsUsed = true;
140208599Srdivacky      return Value;
141198396Srdivacky    }
142198396Srdivacky
143218893Sdim    // AliasedSymbol() - If this is an alias (a = b), return the symbol
144218893Sdim    // we ultimately point to. For a non alias, this just returns the symbol
145218893Sdim    // itself.
146218893Sdim    const MCSymbol &AliasedSymbol() const;
147218893Sdim
148208599Srdivacky    void setVariableValue(const MCExpr *Value);
149208599Srdivacky
150198396Srdivacky    /// @}
151198396Srdivacky
152245431Sdim    /// print - Print the value to the stream \p OS.
153202878Srdivacky    void print(raw_ostream &OS) const;
154198090Srdivacky
155198090Srdivacky    /// dump - Print the value to stderr.
156198090Srdivacky    void dump() const;
157195098Sed  };
158195098Sed
159202878Srdivacky  inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
160202878Srdivacky    Sym.print(OS);
161202878Srdivacky    return OS;
162202878Srdivacky  }
163195098Sed} // end namespace llvm
164195098Sed
165195098Sed#endif
166