LLVM API Documentation
00001 //===- MCSymbol.h - Machine Code Symbols ------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the declaration of the MCSymbol class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_MC_MCSYMBOL_H 00015 #define LLVM_MC_MCSYMBOL_H 00016 00017 #include "llvm/ADT/StringRef.h" 00018 #include "llvm/Support/Compiler.h" 00019 00020 namespace llvm { 00021 class MCExpr; 00022 class MCSection; 00023 class MCContext; 00024 class raw_ostream; 00025 00026 /// MCSymbol - Instances of this class represent a symbol name in the MC file, 00027 /// and MCSymbols are created and unique'd by the MCContext class. MCSymbols 00028 /// should only be constructed with valid names for the object file. 00029 /// 00030 /// If the symbol is defined/emitted into the current translation unit, the 00031 /// Section member is set to indicate what section it lives in. Otherwise, if 00032 /// it is a reference to an external entity, it has a null section. 00033 class MCSymbol { 00034 // Special sentinal value for the absolute pseudo section. 00035 // 00036 // FIXME: Use a PointerInt wrapper for this? 00037 static const MCSection *AbsolutePseudoSection; 00038 00039 /// Name - The name of the symbol. The referred-to string data is actually 00040 /// held by the StringMap that lives in MCContext. 00041 StringRef Name; 00042 00043 /// Section - The section the symbol is defined in. This is null for 00044 /// undefined symbols, and the special AbsolutePseudoSection value for 00045 /// absolute symbols. 00046 const MCSection *Section; 00047 00048 /// Value - If non-null, the value for a variable symbol. 00049 const MCExpr *Value; 00050 00051 /// IsTemporary - True if this is an assembler temporary label, which 00052 /// typically does not survive in the .o file's symbol table. Usually 00053 /// "Lfoo" or ".foo". 00054 unsigned IsTemporary : 1; 00055 00056 /// IsUsed - True if this symbol has been used. 00057 mutable unsigned IsUsed : 1; 00058 00059 private: // MCContext creates and uniques these. 00060 friend class MCExpr; 00061 friend class MCContext; 00062 MCSymbol(StringRef name, bool isTemporary) 00063 : Name(name), Section(nullptr), Value(nullptr), 00064 IsTemporary(isTemporary), IsUsed(false) {} 00065 00066 MCSymbol(const MCSymbol&) LLVM_DELETED_FUNCTION; 00067 void operator=(const MCSymbol&) LLVM_DELETED_FUNCTION; 00068 public: 00069 /// getName - Get the symbol name. 00070 StringRef getName() const { return Name; } 00071 00072 /// @name Accessors 00073 /// @{ 00074 00075 /// isTemporary - Check if this is an assembler temporary symbol. 00076 bool isTemporary() const { return IsTemporary; } 00077 00078 /// isUsed - Check if this is used. 00079 bool isUsed() const { return IsUsed; } 00080 void setUsed(bool Value) const { IsUsed = Value; } 00081 00082 /// @} 00083 /// @name Associated Sections 00084 /// @{ 00085 00086 /// isDefined - Check if this symbol is defined (i.e., it has an address). 00087 /// 00088 /// Defined symbols are either absolute or in some section. 00089 bool isDefined() const { 00090 return Section != nullptr; 00091 } 00092 00093 /// isInSection - Check if this symbol is defined in some section (i.e., it 00094 /// is defined but not absolute). 00095 bool isInSection() const { 00096 return isDefined() && !isAbsolute(); 00097 } 00098 00099 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined). 00100 bool isUndefined() const { 00101 return !isDefined(); 00102 } 00103 00104 /// isAbsolute - Check if this is an absolute symbol. 00105 bool isAbsolute() const { 00106 return Section == AbsolutePseudoSection; 00107 } 00108 00109 /// getSection - Get the section associated with a defined, non-absolute 00110 /// symbol. 00111 const MCSection &getSection() const { 00112 assert(isInSection() && "Invalid accessor!"); 00113 return *Section; 00114 } 00115 00116 /// setSection - Mark the symbol as defined in the section \p S. 00117 void setSection(const MCSection &S) { Section = &S; } 00118 00119 /// setUndefined - Mark the symbol as undefined. 00120 void setUndefined() { 00121 Section = nullptr; 00122 } 00123 00124 /// setAbsolute - Mark the symbol as absolute. 00125 void setAbsolute() { Section = AbsolutePseudoSection; } 00126 00127 /// @} 00128 /// @name Variable Symbols 00129 /// @{ 00130 00131 /// isVariable - Check if this is a variable symbol. 00132 bool isVariable() const { 00133 return Value != nullptr; 00134 } 00135 00136 /// getVariableValue() - Get the value for variable symbols. 00137 const MCExpr *getVariableValue() const { 00138 assert(isVariable() && "Invalid accessor!"); 00139 IsUsed = true; 00140 return Value; 00141 } 00142 00143 // AliasedSymbol() - If this is an alias (a = b), return the symbol 00144 // we ultimately point to. For a non-alias, this just returns the symbol 00145 // itself. 00146 const MCSymbol &AliasedSymbol() const; 00147 00148 void setVariableValue(const MCExpr *Value); 00149 00150 /// @} 00151 00152 /// print - Print the value to the stream \p OS. 00153 void print(raw_ostream &OS) const; 00154 00155 /// dump - Print the value to stderr. 00156 void dump() const; 00157 }; 00158 00159 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) { 00160 Sym.print(OS); 00161 return OS; 00162 } 00163 } // end namespace llvm 00164 00165 #endif