LLVM API Documentation

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/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     /// \brief True if this symbol can be redefined.
00057     unsigned IsRedefinable : 1;
00058 
00059     /// IsUsed - True if this symbol has been used.
00060     mutable unsigned IsUsed : 1;
00061 
00062   private:  // MCContext creates and uniques these.
00063     friend class MCExpr;
00064     friend class MCContext;
00065     MCSymbol(StringRef name, bool isTemporary)
00066       : Name(name), Section(nullptr), Value(nullptr),
00067         IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false) {}
00068 
00069     MCSymbol(const MCSymbol&) LLVM_DELETED_FUNCTION;
00070     void operator=(const MCSymbol&) LLVM_DELETED_FUNCTION;
00071   public:
00072     /// getName - Get the symbol name.
00073     StringRef getName() const { return Name; }
00074 
00075     /// @name Accessors
00076     /// @{
00077 
00078     /// isTemporary - Check if this is an assembler temporary symbol.
00079     bool isTemporary() const { return IsTemporary; }
00080 
00081     /// isUsed - Check if this is used.
00082     bool isUsed() const { return IsUsed; }
00083     void setUsed(bool Value) const { IsUsed = Value; }
00084 
00085     /// \brief Check if this symbol is redefinable.
00086     bool isRedefinable() const { return IsRedefinable; }
00087     /// \brief Mark this symbol as redefinable.
00088     void setRedefinable(bool Value) { IsRedefinable = Value; }
00089     /// \brief Prepare this symbol to be redefined.
00090     void redefineIfPossible() {
00091       if (IsRedefinable) {
00092         Value = nullptr;
00093         Section = nullptr;
00094         IsRedefinable = false;
00095       }
00096     }
00097 
00098     /// @}
00099     /// @name Associated Sections
00100     /// @{
00101 
00102     /// isDefined - Check if this symbol is defined (i.e., it has an address).
00103     ///
00104     /// Defined symbols are either absolute or in some section.
00105     bool isDefined() const {
00106       return Section != nullptr;
00107     }
00108 
00109     /// isInSection - Check if this symbol is defined in some section (i.e., it
00110     /// is defined but not absolute).
00111     bool isInSection() const {
00112       return isDefined() && !isAbsolute();
00113     }
00114 
00115     /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
00116     bool isUndefined() const {
00117       return !isDefined();
00118     }
00119 
00120     /// isAbsolute - Check if this is an absolute symbol.
00121     bool isAbsolute() const {
00122       return Section == AbsolutePseudoSection;
00123     }
00124 
00125     /// getSection - Get the section associated with a defined, non-absolute
00126     /// symbol.
00127     const MCSection &getSection() const {
00128       assert(isInSection() && "Invalid accessor!");
00129       return *Section;
00130     }
00131 
00132     /// setSection - Mark the symbol as defined in the section \p S.
00133     void setSection(const MCSection &S) { Section = &S; }
00134 
00135     /// setUndefined - Mark the symbol as undefined.
00136     void setUndefined() {
00137       Section = nullptr;
00138     }
00139 
00140     /// setAbsolute - Mark the symbol as absolute.
00141     void setAbsolute() { Section = AbsolutePseudoSection; }
00142 
00143     /// @}
00144     /// @name Variable Symbols
00145     /// @{
00146 
00147     /// isVariable - Check if this is a variable symbol.
00148     bool isVariable() const {
00149       return Value != nullptr;
00150     }
00151 
00152     /// getVariableValue() - Get the value for variable symbols.
00153     const MCExpr *getVariableValue() const {
00154       assert(isVariable() && "Invalid accessor!");
00155       IsUsed = true;
00156       return Value;
00157     }
00158 
00159     // AliasedSymbol() - If this is an alias (a = b), return the symbol
00160     // we ultimately point to. For a non-alias, this just returns the symbol
00161     // itself.
00162     const MCSymbol &AliasedSymbol() const;
00163 
00164     void setVariableValue(const MCExpr *Value);
00165 
00166     /// @}
00167 
00168     /// print - Print the value to the stream \p OS.
00169     void print(raw_ostream &OS) const;
00170 
00171     /// dump - Print the value to stderr.
00172     void dump() const;
00173   };
00174 
00175   inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
00176     Sym.print(OS);
00177     return OS;
00178   }
00179 } // end namespace llvm
00180 
00181 #endif