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