LLVM  mainline
MCSymbol.h
Go to the documentation of this file.
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/MC/MCExpr.h"
00019 #include "llvm/Support/Compiler.h"
00020 
00021 namespace llvm {
00022   class MCExpr;
00023   class MCSection;
00024   class MCContext;
00025   class raw_ostream;
00026 
00027   /// MCSymbol - Instances of this class represent a symbol name in the MC file,
00028   /// and MCSymbols are created and unique'd by the MCContext class.  MCSymbols
00029   /// should only be constructed with valid names for the object file.
00030   ///
00031   /// If the symbol is defined/emitted into the current translation unit, the
00032   /// Section member is set to indicate what section it lives in.  Otherwise, if
00033   /// it is a reference to an external entity, it has a null section.
00034   class MCSymbol {
00035     // Special sentinal value for the absolute pseudo section.
00036     //
00037     // FIXME: Use a PointerInt wrapper for this?
00038     static const MCSection *AbsolutePseudoSection;
00039 
00040     /// Name - The name of the symbol.  The referred-to string data is actually
00041     /// held by the StringMap that lives in MCContext.
00042     StringRef Name;
00043 
00044     /// Section - The section the symbol is defined in. This is null for
00045     /// undefined symbols, and the special AbsolutePseudoSection value for
00046     /// absolute symbols. If this is a variable symbol, this caches the
00047     /// variable value's section.
00048     mutable const MCSection *Section;
00049 
00050     /// Value - If non-null, the value for a variable symbol.
00051     const MCExpr *Value;
00052 
00053     /// IsTemporary - True if this is an assembler temporary label, which
00054     /// typically does not survive in the .o file's symbol table.  Usually
00055     /// "Lfoo" or ".foo".
00056     unsigned IsTemporary : 1;
00057 
00058     /// \brief True if this symbol can be redefined.
00059     unsigned IsRedefinable : 1;
00060 
00061     /// IsUsed - True if this symbol has been used.
00062     mutable unsigned IsUsed : 1;
00063 
00064   private:  // MCContext creates and uniques these.
00065     friend class MCExpr;
00066     friend class MCContext;
00067     MCSymbol(StringRef name, bool isTemporary)
00068       : Name(name), Section(nullptr), Value(nullptr),
00069         IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false) {}
00070 
00071     MCSymbol(const MCSymbol&) = delete;
00072     void operator=(const MCSymbol&) = delete;
00073     const MCSection *getSectionPtr() const {
00074       if (Section || !Value)
00075         return Section;
00076       return Section = Value->FindAssociatedSection();
00077     }
00078 
00079   public:
00080     /// getName - Get the symbol name.
00081     StringRef getName() const { return Name; }
00082 
00083     /// @name Accessors
00084     /// @{
00085 
00086     /// isTemporary - Check if this is an assembler temporary symbol.
00087     bool isTemporary() const { return IsTemporary; }
00088 
00089     /// isUsed - Check if this is used.
00090     bool isUsed() const { return IsUsed; }
00091     void setUsed(bool Value) const { IsUsed = Value; }
00092 
00093     /// \brief Check if this symbol is redefinable.
00094     bool isRedefinable() const { return IsRedefinable; }
00095     /// \brief Mark this symbol as redefinable.
00096     void setRedefinable(bool Value) { IsRedefinable = Value; }
00097     /// \brief Prepare this symbol to be redefined.
00098     void redefineIfPossible() {
00099       if (IsRedefinable) {
00100         Value = nullptr;
00101         Section = nullptr;
00102         IsRedefinable = false;
00103       }
00104     }
00105 
00106     /// @}
00107     /// @name Associated Sections
00108     /// @{
00109 
00110     /// isDefined - Check if this symbol is defined (i.e., it has an address).
00111     ///
00112     /// Defined symbols are either absolute or in some section.
00113     bool isDefined() const {
00114       return getSectionPtr() != nullptr;
00115     }
00116 
00117     /// isInSection - Check if this symbol is defined in some section (i.e., it
00118     /// is defined but not absolute).
00119     bool isInSection() const {
00120       return isDefined() && !isAbsolute();
00121     }
00122 
00123     /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
00124     bool isUndefined() const {
00125       return !isDefined();
00126     }
00127 
00128     /// isAbsolute - Check if this is an absolute symbol.
00129     bool isAbsolute() const {
00130       return getSectionPtr() == AbsolutePseudoSection;
00131     }
00132 
00133     /// getSection - Get the section associated with a defined, non-absolute
00134     /// symbol.
00135     const MCSection &getSection() const {
00136       assert(isInSection() && "Invalid accessor!");
00137       return *getSectionPtr();
00138     }
00139 
00140     /// setSection - Mark the symbol as defined in the section \p S.
00141     void setSection(const MCSection &S) {
00142       assert(!isVariable() && "Cannot set section of variable");
00143       Section = &S;
00144     }
00145 
00146     /// setUndefined - Mark the symbol as undefined.
00147     void setUndefined() {
00148       Section = nullptr;
00149     }
00150 
00151     /// @}
00152     /// @name Variable Symbols
00153     /// @{
00154 
00155     /// isVariable - Check if this is a variable symbol.
00156     bool isVariable() const {
00157       return Value != nullptr;
00158     }
00159 
00160     /// getVariableValue() - Get the value for variable symbols.
00161     const MCExpr *getVariableValue() const {
00162       assert(isVariable() && "Invalid accessor!");
00163       IsUsed = true;
00164       return Value;
00165     }
00166 
00167     void setVariableValue(const MCExpr *Value);
00168 
00169     /// @}
00170 
00171     /// print - Print the value to the stream \p OS.
00172     void print(raw_ostream &OS) const;
00173 
00174     /// dump - Print the value to stderr.
00175     void dump() const;
00176   };
00177 
00178   inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
00179     Sym.print(OS);
00180     return OS;
00181   }
00182 } // end namespace llvm
00183 
00184 #endif