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/PointerUnion.h"
00018 #include "llvm/ADT/StringMap.h"
00019 #include "llvm/MC/MCAssembler.h"
00020 #include "llvm/Support/Compiler.h"
00021 
00022 namespace llvm {
00023 class MCAsmInfo;
00024 class MCExpr;
00025 class MCSymbol;
00026 class MCFragment;
00027 class MCSection;
00028 class MCContext;
00029 class raw_ostream;
00030 
00031 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
00032 /// and MCSymbols are created and uniqued by the MCContext class.  MCSymbols
00033 /// should only be constructed with valid names for the object file.
00034 ///
00035 /// If the symbol is defined/emitted into the current translation unit, the
00036 /// Section member is set to indicate what section it lives in.  Otherwise, if
00037 /// it is a reference to an external entity, it has a null section.
00038 class MCSymbol {
00039 protected:
00040   /// The kind of the symbol.  If it is any value other than unset then this
00041   /// class is actually one of the appropriate subclasses of MCSymbol.
00042   enum SymbolKind {
00043     SymbolKindUnset,
00044     SymbolKindCOFF,
00045     SymbolKindELF,
00046     SymbolKindMachO,
00047   };
00048 
00049   /// A symbol can contain an Offset, or Value, or be Common, but never more
00050   /// than one of these.
00051   enum Contents : uint8_t {
00052     SymContentsUnset,
00053     SymContentsOffset,
00054     SymContentsVariable,
00055     SymContentsCommon,
00056   };
00057 
00058   // Special sentinal value for the absolute pseudo section.
00059   //
00060   // FIXME: Use a PointerInt wrapper for this?
00061   static MCSection *AbsolutePseudoSection;
00062 
00063   /// If a symbol has a Fragment, the section is implied, so we only need
00064   /// one pointer.
00065   /// FIXME: We might be able to simplify this by having the asm streamer create
00066   /// dummy fragments.
00067   /// If this is a section, then it gives the symbol is defined in. This is null
00068   /// for undefined symbols, and the special AbsolutePseudoSection value for
00069   /// absolute symbols. If this is a variable symbol, this caches the variable
00070   /// value's section.
00071   ///
00072   /// If this is a fragment, then it gives the fragment this symbol's value is
00073   /// relative to, if any.
00074   mutable PointerUnion<MCSection *, MCFragment *> SectionOrFragment;
00075 
00076   /// IsTemporary - True if this is an assembler temporary label, which
00077   /// typically does not survive in the .o file's symbol table.  Usually
00078   /// "Lfoo" or ".foo".
00079   unsigned IsTemporary : 1;
00080 
00081   /// \brief True if this symbol can be redefined.
00082   unsigned IsRedefinable : 1;
00083 
00084   /// IsUsed - True if this symbol has been used.
00085   mutable unsigned IsUsed : 1;
00086 
00087   mutable bool IsRegistered : 1;
00088 
00089   /// This symbol is visible outside this translation unit.
00090   mutable unsigned IsExternal : 1;
00091 
00092   /// This symbol is private extern.
00093   mutable unsigned IsPrivateExtern : 1;
00094 
00095   /// True if this symbol is named.
00096   /// A named symbol will have a pointer to the name allocated in the bytes
00097   /// immediately prior to the MCSymbol.
00098   unsigned HasName : 1;
00099 
00100   /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
00101   /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
00102   unsigned Kind : 2;
00103 
00104   /// True if we have created a relocation that uses this symbol.
00105   mutable unsigned IsUsedInReloc : 1;
00106 
00107   /// This is actually a Contents enumerator, but is unsigned to avoid sign
00108   /// extension and achieve better bitpacking with MSVC.
00109   unsigned SymbolContents : 2;
00110 
00111   /// Index field, for use by the object file implementation.
00112   mutable uint32_t Index = 0;
00113 
00114   union {
00115     /// The offset to apply to the fragment address to form this symbol's value.
00116     uint64_t Offset;
00117 
00118     /// The size of the symbol, if it is 'common'.
00119     uint64_t CommonSize;
00120 
00121     /// If non-null, the value for a variable symbol.
00122     const MCExpr *Value;
00123   };
00124 
00125   /// The alignment of the symbol, if it is 'common', or -1.
00126   //
00127   // FIXME: Pack this in with other fields?
00128   unsigned CommonAlign = -1U;
00129 
00130   /// The Flags field is used by object file implementations to store
00131   /// additional per symbol information which is not easily classified.
00132   mutable uint32_t Flags = 0;
00133 
00134 protected: // MCContext creates and uniques these.
00135   friend class MCExpr;
00136   friend class MCContext;
00137 
00138   /// \brief The name for a symbol.
00139   /// MCSymbol contains a uint64_t so is probably aligned to 8.  On a 32-bit
00140   /// system, the name is a pointer so isn't going to satisfy the 8 byte
00141   /// alignment of uint64_t.  Account for that here.
00142   typedef union {
00143     const StringMapEntry<bool> *NameEntry;
00144     uint64_t AlignmentPadding;
00145   } NameEntryStorageTy;
00146 
00147   MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
00148       : IsTemporary(isTemporary), IsRedefinable(false),
00149         IsUsed(false), IsRegistered(false), IsExternal(false),
00150         IsPrivateExtern(false), HasName(!!Name), Kind(Kind),
00151         IsUsedInReloc(false), SymbolContents(SymContentsUnset) {
00152     Offset = 0;
00153     if (Name)
00154       getNameEntryPtr() = Name;
00155   }
00156 
00157   // Provide custom new/delete as we will only allocate space for a name
00158   // if we need one.
00159   void *operator new(size_t s, const StringMapEntry<bool> *Name,
00160                      MCContext &Ctx);
00161 
00162 private:
00163 
00164   void operator delete(void *);
00165   /// \brief Placement delete - required by std, but never called.
00166   void operator delete(void*, unsigned) {
00167     llvm_unreachable("Constructor throws?");
00168   }
00169   /// \brief Placement delete - required by std, but never called.
00170   void operator delete(void*, unsigned, bool) {
00171     llvm_unreachable("Constructor throws?");
00172   }
00173 
00174   MCSymbol(const MCSymbol &) = delete;
00175   void operator=(const MCSymbol &) = delete;
00176   MCSection *getSectionPtr() const {
00177     if (MCFragment *F = getFragment())
00178       return F->getParent();
00179     assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
00180     MCSection *Section = SectionOrFragment.dyn_cast<MCSection *>();
00181     if (Section || !isVariable())
00182       return Section;
00183     return Section = getVariableValue()->findAssociatedSection();
00184   }
00185 
00186   /// \brief Get a reference to the name field.  Requires that we have a name
00187   const StringMapEntry<bool> *&getNameEntryPtr() {
00188     assert(HasName && "Name is required");
00189     NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
00190     return (*(Name - 1)).NameEntry;
00191   }
00192   const StringMapEntry<bool> *&getNameEntryPtr() const {
00193     return const_cast<MCSymbol*>(this)->getNameEntryPtr();
00194   }
00195 
00196 public:
00197   /// getName - Get the symbol name.
00198   StringRef getName() const {
00199     if (!HasName)
00200       return StringRef();
00201 
00202     return getNameEntryPtr()->first();
00203   }
00204 
00205   bool isRegistered() const { return IsRegistered; }
00206   void setIsRegistered(bool Value) const { IsRegistered = Value; }
00207 
00208   void setUsedInReloc() const { IsUsedInReloc = true; }
00209   bool isUsedInReloc() const { return IsUsedInReloc; }
00210 
00211   /// \name Accessors
00212   /// @{
00213 
00214   /// isTemporary - Check if this is an assembler temporary symbol.
00215   bool isTemporary() const { return IsTemporary; }
00216 
00217   /// isUsed - Check if this is used.
00218   bool isUsed() const { return IsUsed; }
00219   void setUsed(bool Value) const { IsUsed = Value; }
00220 
00221   /// \brief Check if this symbol is redefinable.
00222   bool isRedefinable() const { return IsRedefinable; }
00223   /// \brief Mark this symbol as redefinable.
00224   void setRedefinable(bool Value) { IsRedefinable = Value; }
00225   /// \brief Prepare this symbol to be redefined.
00226   void redefineIfPossible() {
00227     if (IsRedefinable) {
00228       if (SymbolContents == SymContentsVariable) {
00229         Value = nullptr;
00230         SymbolContents = SymContentsUnset;
00231       }
00232       SectionOrFragment = nullptr;
00233       IsRedefinable = false;
00234     }
00235   }
00236 
00237   /// @}
00238   /// \name Associated Sections
00239   /// @{
00240 
00241   /// isDefined - Check if this symbol is defined (i.e., it has an address).
00242   ///
00243   /// Defined symbols are either absolute or in some section.
00244   bool isDefined() const { return getSectionPtr() != nullptr; }
00245 
00246   /// isInSection - Check if this symbol is defined in some section (i.e., it
00247   /// is defined but not absolute).
00248   bool isInSection() const { return isDefined() && !isAbsolute(); }
00249 
00250   /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
00251   bool isUndefined() const { return !isDefined(); }
00252 
00253   /// isAbsolute - Check if this is an absolute symbol.
00254   bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
00255 
00256   /// Get the section associated with a defined, non-absolute symbol.
00257   MCSection &getSection() const {
00258     assert(isInSection() && "Invalid accessor!");
00259     return *getSectionPtr();
00260   }
00261 
00262   /// Mark the symbol as defined in the section \p S.
00263   void setSection(MCSection &S) {
00264     assert(!isVariable() && "Cannot set section of variable");
00265     assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
00266     SectionOrFragment = &S;
00267   }
00268 
00269   /// Mark the symbol as undefined.
00270   void setUndefined() {
00271     SectionOrFragment = nullptr;
00272   }
00273 
00274   bool isELF() const { return Kind == SymbolKindELF; }
00275 
00276   bool isCOFF() const { return Kind == SymbolKindCOFF; }
00277 
00278   bool isMachO() const { return Kind == SymbolKindMachO; }
00279 
00280   /// @}
00281   /// \name Variable Symbols
00282   /// @{
00283 
00284   /// isVariable - Check if this is a variable symbol.
00285   bool isVariable() const {
00286     return SymbolContents == SymContentsVariable;
00287   }
00288 
00289   /// getVariableValue() - Get the value for variable symbols.
00290   const MCExpr *getVariableValue() const {
00291     assert(isVariable() && "Invalid accessor!");
00292     IsUsed = true;
00293     return Value;
00294   }
00295 
00296   void setVariableValue(const MCExpr *Value);
00297 
00298   /// @}
00299 
00300   /// Get the (implementation defined) index.
00301   uint32_t getIndex() const {
00302     return Index;
00303   }
00304 
00305   /// Set the (implementation defined) index.
00306   void setIndex(uint32_t Value) const {
00307     Index = Value;
00308   }
00309 
00310   uint64_t getOffset() const {
00311     assert((SymbolContents == SymContentsUnset ||
00312             SymbolContents == SymContentsOffset) &&
00313            "Cannot get offset for a common/variable symbol");
00314     return Offset;
00315   }
00316   void setOffset(uint64_t Value) {
00317     assert((SymbolContents == SymContentsUnset ||
00318             SymbolContents == SymContentsOffset) &&
00319            "Cannot set offset for a common/variable symbol");
00320     Offset = Value;
00321     SymbolContents = SymContentsOffset;
00322   }
00323 
00324   /// Return the size of a 'common' symbol.
00325   uint64_t getCommonSize() const {
00326     assert(isCommon() && "Not a 'common' symbol!");
00327     return CommonSize;
00328   }
00329 
00330   /// Mark this symbol as being 'common'.
00331   ///
00332   /// \param Size - The size of the symbol.
00333   /// \param Align - The alignment of the symbol.
00334   void setCommon(uint64_t Size, unsigned Align) {
00335     assert(getOffset() == 0);
00336     CommonSize = Size;
00337     CommonAlign = Align;
00338     SymbolContents = SymContentsCommon;
00339   }
00340 
00341   ///  Return the alignment of a 'common' symbol.
00342   unsigned getCommonAlignment() const {
00343     assert(isCommon() && "Not a 'common' symbol!");
00344     return CommonAlign;
00345   }
00346 
00347   /// Declare this symbol as being 'common'.
00348   ///
00349   /// \param Size - The size of the symbol.
00350   /// \param Align - The alignment of the symbol.
00351   /// \return True if symbol was already declared as a different type
00352   bool declareCommon(uint64_t Size, unsigned Align) {
00353     assert(isCommon() || getOffset() == 0);
00354     if(isCommon()) {
00355       if(CommonSize != Size || CommonAlign != Align)
00356        return true;
00357     } else
00358       setCommon(Size, Align);
00359     return false;
00360   }
00361 
00362   /// Is this a 'common' symbol.
00363   bool isCommon() const {
00364     return SymbolContents == SymContentsCommon;
00365   }
00366 
00367   MCFragment *getFragment() const {
00368     return SectionOrFragment.dyn_cast<MCFragment *>();
00369   }
00370   void setFragment(MCFragment *Value) const {
00371     SectionOrFragment = Value;
00372   }
00373 
00374   bool isExternal() const { return IsExternal; }
00375   void setExternal(bool Value) const { IsExternal = Value; }
00376 
00377   bool isPrivateExtern() const { return IsPrivateExtern; }
00378   void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
00379 
00380   /// print - Print the value to the stream \p OS.
00381   void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
00382 
00383   /// dump - Print the value to stderr.
00384   void dump() const;
00385 
00386 protected:
00387   /// Get the (implementation defined) symbol flags.
00388   uint32_t getFlags() const { return Flags; }
00389 
00390   /// Set the (implementation defined) symbol flags.
00391   void setFlags(uint32_t Value) const { Flags = Value; }
00392 
00393   /// Modify the flags via a mask
00394   void modifyFlags(uint32_t Value, uint32_t Mask) const {
00395     Flags = (Flags & ~Mask) | Value;
00396   }
00397 };
00398 
00399 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
00400   Sym.print(OS, nullptr);
00401   return OS;
00402 }
00403 } // end namespace llvm
00404 
00405 #endif