MCSymbol.h revision f451cb870efcf9e0302d25ed05f4cac6bb494e42
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 <string>
18#include "llvm/ADT/StringRef.h"
19#include "llvm/System/DataTypes.h"
20
21namespace 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 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  ///
35  class MCSymbol {
36    // Special sentinal value for the absolute pseudo section.
37    //
38    // FIXME: Use a PointerInt wrapper for this?
39    static const MCSection *AbsolutePseudoSection;
40
41    /// Name - The name of the symbol.
42    std::string 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  private:  // MCContext creates and uniques these.
58    friend class MCContext;
59    MCSymbol(StringRef _Name, bool _IsTemporary)
60      : Name(_Name), Section(0), Value(0), IsTemporary(_IsTemporary) {}
61
62    MCSymbol(const MCSymbol&);       // DO NOT IMPLEMENT
63    void operator=(const MCSymbol&); // DO NOT IMPLEMENT
64  public:
65    /// getName - Get the symbol name.
66    const std::string &getName() const { return Name; }
67
68    /// @name Symbol Type
69    /// @{
70
71    /// isTemporary - Check if this is an assembler temporary symbol.
72    bool isTemporary() const {
73      return IsTemporary;
74    }
75
76    /// @}
77    /// @name Associated Sections
78    /// @{
79
80    /// isDefined - Check if this symbol is defined (i.e., it has an address).
81    ///
82    /// Defined symbols are either absolute or in some section.
83    bool isDefined() const {
84      return Section != 0;
85    }
86
87    /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
88    bool isUndefined() const {
89      return !isDefined();
90    }
91
92    /// isAbsolute - Check if this is an absolute symbol.
93    bool isAbsolute() const {
94      return Section == AbsolutePseudoSection;
95    }
96
97    /// getSection - Get the section associated with a defined, non-absolute
98    /// symbol.
99    const MCSection &getSection() const {
100      assert(!isUndefined() && !isAbsolute() && "Invalid accessor!");
101      return *Section;
102    }
103
104    /// setSection - Mark the symbol as defined in the section \arg S.
105    void setSection(const MCSection &S) { Section = &S; }
106
107    /// setUndefined - Mark the symbol as undefined.
108    void setUndefined() {
109      Section = 0;
110    }
111
112    /// setAbsolute - Mark the symbol as absolute.
113    void setAbsolute() { Section = AbsolutePseudoSection; }
114
115    /// @}
116    /// @name Variable Symbols
117    /// @{
118
119    /// isVariable - Check if this is a variable symbol.
120    bool isVariable() const {
121      return Value != 0;
122    }
123
124    /// getValue() - Get the value for variable symbols, or null if the symbol
125    /// is not a variable.
126    const MCExpr *getValue() const { return Value; }
127
128    void setValue(const MCExpr *Value) {
129      this->Value = Value;
130    }
131
132    /// @}
133
134    /// print - Print the value to the stream \arg OS.
135    void print(raw_ostream &OS) const;
136
137    /// dump - Print the value to stderr.
138    void dump() const;
139  };
140
141  inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
142    Sym.print(OS);
143    return OS;
144  }
145} // end namespace llvm
146
147#endif
148