1 //===- MCSymbol.h - Machine Code Symbols ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the MCSymbol class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_MC_MCSYMBOL_H
15 #define LLVM_MC_MCSYMBOL_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Compiler.h"
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.
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.
34 // Special sentinal value for the absolute pseudo section.
36 // FIXME: Use a PointerInt wrapper for this?
37 static const MCSection *AbsolutePseudoSection;
39 /// Name - The name of the symbol. The referred-to string data is actually
40 /// held by the StringMap that lives in MCContext.
43 /// Section - The section the symbol is defined in. This is null for
44 /// undefined symbols, and the special AbsolutePseudoSection value for
46 const MCSection *Section;
48 /// Value - If non-null, the value for a variable symbol.
51 /// IsTemporary - True if this is an assembler temporary label, which
52 /// typically does not survive in the .o file's symbol table. Usually
54 unsigned IsTemporary : 1;
56 /// \brief True if this symbol can be redefined.
57 unsigned IsRedefinable : 1;
59 /// IsUsed - True if this symbol has been used.
60 mutable unsigned IsUsed : 1;
62 private: // MCContext creates and uniques these.
64 friend class MCContext;
65 MCSymbol(StringRef name, bool isTemporary)
66 : Name(name), Section(nullptr), Value(nullptr),
67 IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false) {}
69 MCSymbol(const MCSymbol&) = delete;
70 void operator=(const MCSymbol&) = delete;
72 /// getName - Get the symbol name.
73 StringRef getName() const { return Name; }
78 /// isTemporary - Check if this is an assembler temporary symbol.
79 bool isTemporary() const { return IsTemporary; }
81 /// isUsed - Check if this is used.
82 bool isUsed() const { return IsUsed; }
83 void setUsed(bool Value) const { IsUsed = Value; }
85 /// \brief Check if this symbol is redefinable.
86 bool isRedefinable() const { return IsRedefinable; }
87 /// \brief Mark this symbol as redefinable.
88 void setRedefinable(bool Value) { IsRedefinable = Value; }
89 /// \brief Prepare this symbol to be redefined.
90 void redefineIfPossible() {
94 IsRedefinable = false;
99 /// @name Associated Sections
102 /// isDefined - Check if this symbol is defined (i.e., it has an address).
104 /// Defined symbols are either absolute or in some section.
105 bool isDefined() const {
106 return Section != nullptr;
109 /// isInSection - Check if this symbol is defined in some section (i.e., it
110 /// is defined but not absolute).
111 bool isInSection() const {
112 return isDefined() && !isAbsolute();
115 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
116 bool isUndefined() const {
120 /// isAbsolute - Check if this is an absolute symbol.
121 bool isAbsolute() const {
122 return Section == AbsolutePseudoSection;
125 /// getSection - Get the section associated with a defined, non-absolute
127 const MCSection &getSection() const {
128 assert(isInSection() && "Invalid accessor!");
132 /// setSection - Mark the symbol as defined in the section \p S.
133 void setSection(const MCSection &S) { Section = &S; }
135 /// setUndefined - Mark the symbol as undefined.
136 void setUndefined() {
140 /// setAbsolute - Mark the symbol as absolute.
141 void setAbsolute() { Section = AbsolutePseudoSection; }
144 /// @name Variable Symbols
147 /// isVariable - Check if this is a variable symbol.
148 bool isVariable() const {
149 return Value != nullptr;
152 /// getVariableValue() - Get the value for variable symbols.
153 const MCExpr *getVariableValue() const {
154 assert(isVariable() && "Invalid accessor!");
159 // AliasedSymbol() - If this is an alias (a = b), return the symbol
160 // we ultimately point to. For a non-alias, this just returns the symbol
162 const MCSymbol &AliasedSymbol() const;
164 void setVariableValue(const MCExpr *Value);
168 /// print - Print the value to the stream \p OS.
169 void print(raw_ostream &OS) const;
171 /// dump - Print the value to stderr.
175 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
179 } // end namespace llvm