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/MC/MCExpr.h"
19 #include "llvm/Support/Compiler.h"
27 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
28 /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
29 /// should only be constructed with valid names for the object file.
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.
35 // Special sentinal value for the absolute pseudo section.
37 // FIXME: Use a PointerInt wrapper for this?
38 static const MCSection *AbsolutePseudoSection;
40 /// Name - The name of the symbol. The referred-to string data is actually
41 /// held by the StringMap that lives in MCContext.
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. If this is a variable symbol, this caches the
47 /// variable value's section.
48 mutable const MCSection *Section;
50 /// Value - If non-null, the value for a variable symbol.
53 /// IsTemporary - True if this is an assembler temporary label, which
54 /// typically does not survive in the .o file's symbol table. Usually
56 unsigned IsTemporary : 1;
58 /// \brief True if this symbol can be redefined.
59 unsigned IsRedefinable : 1;
61 /// IsUsed - True if this symbol has been used.
62 mutable unsigned IsUsed : 1;
64 private: // MCContext creates and uniques these.
66 friend class MCContext;
67 MCSymbol(StringRef name, bool isTemporary)
68 : Name(name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
69 IsRedefinable(false), IsUsed(false) {}
71 MCSymbol(const MCSymbol &) = delete;
72 void operator=(const MCSymbol &) = delete;
73 const MCSection *getSectionPtr() const {
74 if (Section || !Value)
76 return Section = Value->FindAssociatedSection();
80 /// getName - Get the symbol name.
81 StringRef getName() const { return Name; }
86 /// isTemporary - Check if this is an assembler temporary symbol.
87 bool isTemporary() const { return IsTemporary; }
89 /// isUsed - Check if this is used.
90 bool isUsed() const { return IsUsed; }
91 void setUsed(bool Value) const { IsUsed = Value; }
93 /// \brief Check if this symbol is redefinable.
94 bool isRedefinable() const { return IsRedefinable; }
95 /// \brief Mark this symbol as redefinable.
96 void setRedefinable(bool Value) { IsRedefinable = Value; }
97 /// \brief Prepare this symbol to be redefined.
98 void redefineIfPossible() {
102 IsRedefinable = false;
107 /// \name Associated Sections
110 /// isDefined - Check if this symbol is defined (i.e., it has an address).
112 /// Defined symbols are either absolute or in some section.
113 bool isDefined() const { return getSectionPtr() != nullptr; }
115 /// isInSection - Check if this symbol is defined in some section (i.e., it
116 /// is defined but not absolute).
117 bool isInSection() const { return isDefined() && !isAbsolute(); }
119 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
120 bool isUndefined() const { return !isDefined(); }
122 /// isAbsolute - Check if this is an absolute symbol.
123 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
125 /// getSection - Get the section associated with a defined, non-absolute
127 const MCSection &getSection() const {
128 assert(isInSection() && "Invalid accessor!");
129 return *getSectionPtr();
132 /// setSection - Mark the symbol as defined in the section \p S.
133 void setSection(const MCSection &S) {
134 assert(!isVariable() && "Cannot set section of variable");
138 /// setUndefined - Mark the symbol as undefined.
139 void setUndefined() { Section = nullptr; }
142 /// \name Variable Symbols
145 /// isVariable - Check if this is a variable symbol.
146 bool isVariable() const { return Value != nullptr; }
148 /// getVariableValue() - Get the value for variable symbols.
149 const MCExpr *getVariableValue() const {
150 assert(isVariable() && "Invalid accessor!");
155 void setVariableValue(const MCExpr *Value);
159 /// print - Print the value to the stream \p OS.
160 void print(raw_ostream &OS) const;
162 /// dump - Print the value to stderr.
166 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
170 } // end namespace llvm