LLVM API Documentation

Mangler.h

Go to the documentation of this file.
00001 //===-- llvm/Support/Mangler.h - Self-contained name mangler ----*- 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 // Unified name mangler for various backends.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SUPPORT_MANGLER_H
00015 #define LLVM_SUPPORT_MANGLER_H
00016 
00017 #include "llvm/ADT/DenseMap.h"
00018 #include "llvm/ADT/SmallPtrSet.h"
00019 #include <string>
00020 
00021 namespace llvm {
00022 class Type;
00023 class Module;
00024 class Value;
00025 class GlobalValue;
00026 
00027 class Mangler {
00028 public:
00029   enum ManglerPrefixTy {
00030     Default,               ///< Emit default string before each symbol.
00031     Private,               ///< Emit "private" prefix before each symbol.
00032     LinkerPrivate          ///< Emit "linker private" prefix before each symbol.
00033   };
00034 
00035 private:
00036   /// Prefix - This string is added to each symbol that is emitted, unless the
00037   /// symbol is marked as not needing this prefix.
00038   const char *Prefix;
00039 
00040   /// PrivatePrefix - This string is emitted before each symbol with private
00041   /// linkage.
00042   const char *PrivatePrefix;
00043 
00044   /// LinkerPrivatePrefix - This string is emitted before each symbol with
00045   /// "linker_private" linkage.
00046   const char *LinkerPrivatePrefix;
00047 
00048   /// UseQuotes - If this is set, the target accepts global names in quotes,
00049   /// e.g. "foo bar" is a legal name.  This syntax is used instead of escaping
00050   /// the space character.  By default, this is false.
00051   bool UseQuotes;
00052 
00053   /// AnonGlobalIDs - We need to give global values the same name every time
00054   /// they are mangled.  This keeps track of the number we give to anonymous
00055   /// ones.
00056   ///
00057   DenseMap<const GlobalValue*, unsigned> AnonGlobalIDs;
00058 
00059   /// NextAnonGlobalID - This simple counter is used to unique value names.
00060   ///
00061   unsigned NextAnonGlobalID;
00062 
00063   /// AcceptableChars - This bitfield contains a one for each character that is
00064   /// allowed to be part of an unmangled name.
00065   unsigned AcceptableChars[256 / 32];
00066 
00067 public:
00068   // Mangler ctor - if a prefix is specified, it will be prepended onto all
00069   // symbols.
00070   Mangler(Module &M, const char *Prefix = "", const char *privatePrefix = "",
00071           const char *linkerPrivatePrefix = "");
00072 
00073   /// setUseQuotes - If UseQuotes is set to true, this target accepts quoted
00074   /// strings for assembler labels.
00075   void setUseQuotes(bool Val) { UseQuotes = Val; }
00076 
00077   /// Acceptable Characters - This allows the target to specify which characters
00078   /// are acceptable to the assembler without being mangled.  By default we
00079   /// allow letters, numbers, '_', '$', and '.', which is what GAS accepts.
00080   void markCharAcceptable(unsigned char X) {
00081     AcceptableChars[X/32] |= 1 << (X&31);
00082   }
00083   void markCharUnacceptable(unsigned char X) {
00084     AcceptableChars[X/32] &= ~(1 << (X&31));
00085   }
00086   bool isCharAcceptable(unsigned char X) const {
00087     return (AcceptableChars[X/32] & (1 << (X&31))) != 0;
00088   }
00089 
00090   /// getMangledName - Returns the mangled name of V, an LLVM Value,
00091   /// in the current module.  If 'Suffix' is specified, the name ends with the
00092   /// specified suffix.  If 'ForcePrivate' is specified, the label is specified
00093   /// to have a private label prefix.
00094   ///
00095   std::string getMangledName(const GlobalValue *V, const char *Suffix = "",
00096                              bool ForcePrivate = false);
00097 
00098   /// makeNameProper - We don't want identifier names with ., space, or
00099   /// - in them, so we mangle these characters into the strings "d_",
00100   /// "s_", and "D_", respectively. This is a very simple mangling that
00101   /// doesn't guarantee unique names for values. getValueName already
00102   /// does this for you, so there's no point calling it on the result
00103   /// from getValueName.
00104   ///
00105   std::string makeNameProper(const std::string &x,
00106                              ManglerPrefixTy PrefixTy = Mangler::Default);
00107 };
00108 
00109 } // End llvm namespace
00110 
00111 #endif // LLVM_SUPPORT_MANGLER_H