MCSymbol.h revision c28cc093e3b5b8601cb5024a5365a6f31f49839a
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 19namespace llvm { 20 class MCExpr; 21 class MCSection; 22 class MCContext; 23 class raw_ostream; 24 25 /// MCSymbol - Instances of this class represent a symbol name in the MC file, 26 /// and MCSymbols are created and unique'd by the MCContext class. MCSymbols 27 /// should only be constructed with valid names for the object file. 28 /// 29 /// If the symbol is defined/emitted into the current translation unit, the 30 /// Section member is set to indicate what section it lives in. Otherwise, if 31 /// it is a reference to an external entity, it has a null section. 32 /// 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 private: // MCContext creates and uniques these. 57 friend class MCContext; 58 MCSymbol(StringRef name, bool isTemporary) 59 : Name(name), Section(0), Value(0), IsTemporary(isTemporary) {} 60 61 MCSymbol(const MCSymbol&); // DO NOT IMPLEMENT 62 void operator=(const MCSymbol&); // DO NOT IMPLEMENT 63 public: 64 /// getName - Get the symbol name. 65 StringRef getName() const { return Name; } 66 67 /// @name Symbol Type 68 /// @{ 69 70 /// isTemporary - Check if this is an assembler temporary symbol. 71 bool isTemporary() const { 72 return IsTemporary; 73 } 74 75 /// @} 76 /// @name Associated Sections 77 /// @{ 78 79 /// isDefined - Check if this symbol is defined (i.e., it has an address). 80 /// 81 /// Defined symbols are either absolute or in some section. 82 bool isDefined() const { 83 return Section != 0; 84 } 85 86 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined). 87 bool isUndefined() const { 88 return !isDefined(); 89 } 90 91 /// isAbsolute - Check if this is an absolute symbol. 92 bool isAbsolute() const { 93 return Section == AbsolutePseudoSection; 94 } 95 96 /// getSection - Get the section associated with a defined, non-absolute 97 /// symbol. 98 const MCSection &getSection() const { 99 assert(!isUndefined() && !isAbsolute() && "Invalid accessor!"); 100 return *Section; 101 } 102 103 /// setSection - Mark the symbol as defined in the section \arg S. 104 void setSection(const MCSection &S) { Section = &S; } 105 106 /// setUndefined - Mark the symbol as undefined. 107 void setUndefined() { 108 Section = 0; 109 } 110 111 /// setAbsolute - Mark the symbol as absolute. 112 void setAbsolute() { Section = AbsolutePseudoSection; } 113 114 /// @} 115 /// @name Variable Symbols 116 /// @{ 117 118 /// isVariable - Check if this is a variable symbol. 119 bool isVariable() const { 120 return Value != 0; 121 } 122 123 /// getValue() - Get the value for variable symbols, or null if the symbol 124 /// is not a variable. 125 const MCExpr *getValue() const { return Value; } 126 127 void setValue(const MCExpr *Value) { 128 this->Value = Value; 129 } 130 131 /// @} 132 133 /// print - Print the value to the stream \arg OS. 134 void print(raw_ostream &OS) const; 135 136 /// dump - Print the value to stderr. 137 void dump() const; 138 }; 139 140 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) { 141 Sym.print(OS); 142 return OS; 143 } 144} // end namespace llvm 145 146#endif 147