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