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"
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.
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
47 const MCSection *Section;
49 /// Value - If non-null, the value for a variable symbol.
52 /// IsTemporary - True if this is an assembler temporary label, which
53 /// typically does not survive in the .o file's symbol table. Usually
55 unsigned IsTemporary : 1;
57 /// IsUsed - True if this symbol has been used.
58 mutable unsigned IsUsed : 1;
60 private: // MCContext creates and uniques these.
62 friend class MCContext;
63 MCSymbol(StringRef name, bool isTemporary)
64 : Name(name), Section(0), Value(0),
65 IsTemporary(isTemporary), IsUsed(false) {}
67 MCSymbol(const MCSymbol&) LLVM_DELETED_FUNCTION;
68 void operator=(const MCSymbol&) LLVM_DELETED_FUNCTION;
70 /// getName - Get the symbol name.
71 StringRef getName() const { return Name; }
76 /// isTemporary - Check if this is an assembler temporary symbol.
77 bool isTemporary() const { return IsTemporary; }
79 /// isUsed - Check if this is used.
80 bool isUsed() const { return IsUsed; }
81 void setUsed(bool Value) const { IsUsed = Value; }
84 /// @name Associated Sections
87 /// isDefined - Check if this symbol is defined (i.e., it has an address).
89 /// Defined symbols are either absolute or in some section.
90 bool isDefined() const {
94 /// isInSection - Check if this symbol is defined in some section (i.e., it
95 /// is defined but not absolute).
96 bool isInSection() const {
97 return isDefined() && !isAbsolute();
100 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
101 bool isUndefined() const {
105 /// isAbsolute - Check if this is an absolute symbol.
106 bool isAbsolute() const {
107 return Section == AbsolutePseudoSection;
110 /// getSection - Get the section associated with a defined, non-absolute
112 const MCSection &getSection() const {
113 assert(isInSection() && "Invalid accessor!");
117 /// setSection - Mark the symbol as defined in the section \p S.
118 void setSection(const MCSection &S) { Section = &S; }
120 /// setUndefined - Mark the symbol as undefined.
121 void setUndefined() {
125 /// setAbsolute - Mark the symbol as absolute.
126 void setAbsolute() { Section = AbsolutePseudoSection; }
129 /// @name Variable Symbols
132 /// isVariable - Check if this is a variable symbol.
133 bool isVariable() const {
137 /// getVariableValue() - Get the value for variable symbols.
138 const MCExpr *getVariableValue() const {
139 assert(isVariable() && "Invalid accessor!");
144 // AliasedSymbol() - If this is an alias (a = b), return the symbol
145 // we ultimately point to. For a non-alias, this just returns the symbol
147 const MCSymbol &AliasedSymbol() const;
149 // If this symbol is not a variable, return itself. If it is a variable,
150 // evaluate it and check if it is of the form Base + ConstantOffset. If so,
151 // return Base, if not, return nullptr.
152 const MCSymbol *getBaseSymbol(const MCAsmLayout &Layout) const;
154 void setVariableValue(const MCExpr *Value);
158 /// print - Print the value to the stream \p OS.
159 void print(raw_ostream &OS) const;
161 /// dump - Print the value to stderr.
165 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
169 } // end namespace llvm