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 "llvm/ADT/StringRef.h" 18 #include "llvm/Support/Compiler.h" 19 20 namespace llvm { 21 class MCExpr; 22 class MCSection; 23 class MCContext; 24 class raw_ostream; 25 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. 29 /// 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. 33 class MCSymbol { 34 // Special sentinal value for the absolute pseudo section. 35 // 36 // FIXME: Use a PointerInt wrapper for this? 37 static const MCSection *AbsolutePseudoSection; 38 39 /// Name - The name of the symbol. The referred-to string data is actually 40 /// held by the StringMap that lives in MCContext. 41 StringRef Name; 42 43 /// Section - The section the symbol is defined in. This is null for 44 /// undefined symbols, and the special AbsolutePseudoSection value for 45 /// absolute symbols. 46 const MCSection *Section; 47 48 /// Value - If non-null, the value for a variable symbol. 49 const MCExpr *Value; 50 51 /// IsTemporary - True if this is an assembler temporary label, which 52 /// typically does not survive in the .o file's symbol table. Usually 53 /// "Lfoo" or ".foo". 54 unsigned IsTemporary : 1; 55 56 /// \brief True if this symbol can be redefined. 57 unsigned IsRedefinable : 1; 58 59 /// IsUsed - True if this symbol has been used. 60 mutable unsigned IsUsed : 1; 61 62 private: // MCContext creates and uniques these. 63 friend class MCExpr; 64 friend class MCContext; MCSymbol(StringRef name,bool isTemporary)65 MCSymbol(StringRef name, bool isTemporary) 66 : Name(name), Section(nullptr), Value(nullptr), 67 IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false) {} 68 69 MCSymbol(const MCSymbol&) LLVM_DELETED_FUNCTION; 70 void operator=(const MCSymbol&) LLVM_DELETED_FUNCTION; 71 public: 72 /// getName - Get the symbol name. getName()73 StringRef getName() const { return Name; } 74 75 /// @name Accessors 76 /// @{ 77 78 /// isTemporary - Check if this is an assembler temporary symbol. isTemporary()79 bool isTemporary() const { return IsTemporary; } 80 81 /// isUsed - Check if this is used. isUsed()82 bool isUsed() const { return IsUsed; } setUsed(bool Value)83 void setUsed(bool Value) const { IsUsed = Value; } 84 85 /// \brief Check if this symbol is redefinable. isRedefinable()86 bool isRedefinable() const { return IsRedefinable; } 87 /// \brief Mark this symbol as redefinable. setRedefinable(bool Value)88 void setRedefinable(bool Value) { IsRedefinable = Value; } 89 /// \brief Prepare this symbol to be redefined. redefineIfPossible()90 void redefineIfPossible() { 91 if (IsRedefinable) { 92 Value = nullptr; 93 Section = nullptr; 94 IsRedefinable = false; 95 } 96 } 97 98 /// @} 99 /// @name Associated Sections 100 /// @{ 101 102 /// isDefined - Check if this symbol is defined (i.e., it has an address). 103 /// 104 /// Defined symbols are either absolute or in some section. isDefined()105 bool isDefined() const { 106 return Section != nullptr; 107 } 108 109 /// isInSection - Check if this symbol is defined in some section (i.e., it 110 /// is defined but not absolute). isInSection()111 bool isInSection() const { 112 return isDefined() && !isAbsolute(); 113 } 114 115 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined). isUndefined()116 bool isUndefined() const { 117 return !isDefined(); 118 } 119 120 /// isAbsolute - Check if this is an absolute symbol. isAbsolute()121 bool isAbsolute() const { 122 return Section == AbsolutePseudoSection; 123 } 124 125 /// getSection - Get the section associated with a defined, non-absolute 126 /// symbol. getSection()127 const MCSection &getSection() const { 128 assert(isInSection() && "Invalid accessor!"); 129 return *Section; 130 } 131 132 /// setSection - Mark the symbol as defined in the section \p S. setSection(const MCSection & S)133 void setSection(const MCSection &S) { Section = &S; } 134 135 /// setUndefined - Mark the symbol as undefined. setUndefined()136 void setUndefined() { 137 Section = nullptr; 138 } 139 140 /// setAbsolute - Mark the symbol as absolute. setAbsolute()141 void setAbsolute() { Section = AbsolutePseudoSection; } 142 143 /// @} 144 /// @name Variable Symbols 145 /// @{ 146 147 /// isVariable - Check if this is a variable symbol. isVariable()148 bool isVariable() const { 149 return Value != nullptr; 150 } 151 152 /// getVariableValue() - Get the value for variable symbols. getVariableValue()153 const MCExpr *getVariableValue() const { 154 assert(isVariable() && "Invalid accessor!"); 155 IsUsed = true; 156 return Value; 157 } 158 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 161 // itself. 162 const MCSymbol &AliasedSymbol() const; 163 164 void setVariableValue(const MCExpr *Value); 165 166 /// @} 167 168 /// print - Print the value to the stream \p OS. 169 void print(raw_ostream &OS) const; 170 171 /// dump - Print the value to stderr. 172 void dump() const; 173 }; 174 175 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) { 176 Sym.print(OS); 177 return OS; 178 } 179 } // end namespace llvm 180 181 #endif 182