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   /// Prefix - This string is added to each symbol that is emitted, unless the
00029   /// symbol is marked as not needing this prefix.
00030   const char *Prefix;
00031   
00032   const char *PrivatePrefix;
00033   /// UseQuotes - If this is set, the target accepts global names in quotes, 
00034   /// e.g. "foo bar" is a legal name.  This syntax is used instead of escaping
00035   /// the space character.  By default, this is false.
00036   bool UseQuotes;
00037   
00038   /// PreserveAsmNames - If this is set, the asm escape character is not removed
00039   /// from names with 'asm' specifiers. 
00040   bool PreserveAsmNames;
00041   
00042   /// Memo - This is used to remember the name that we assign a value.
00043   ///
00044   DenseMap<const Value*, std::string> Memo;
00045 
00046   /// Count - This simple counter is used to unique value names.
00047   ///
00048   unsigned Count;
00049   
00050   /// TypeMap - If the client wants us to unique types, this keeps track of the
00051   /// current assignments and TypeCounter keeps track of the next id to assign.
00052   DenseMap<const Type*, unsigned> TypeMap;
00053   unsigned TypeCounter;
00054 
00055   /// AcceptableChars - This bitfield contains a one for each character that is
00056   /// allowed to be part of an unmangled name.
00057   unsigned AcceptableChars[256/32];
00058 public:
00059 
00060   // Mangler ctor - if a prefix is specified, it will be prepended onto all
00061   // symbols.
00062   Mangler(Module &M, const char *Prefix = "", const char *privatePrefix = "");
00063 
00064   /// setUseQuotes - If UseQuotes is set to true, this target accepts quoted
00065   /// strings for assembler labels.
00066   void setUseQuotes(bool Val) { UseQuotes = Val; }
00067   
00068   /// setPreserveAsmNames - If the mangler should not strip off the asm name
00069   /// @verbatim identifier (\001), this should be set. @endverbatim
00070   void setPreserveAsmNames(bool Val) { PreserveAsmNames = Val; }
00071   
00072   /// Acceptable Characters - This allows the target to specify which characters
00073   /// are acceptable to the assembler without being mangled.  By default we
00074   /// allow letters, numbers, '_', '$', and '.', which is what GAS accepts.
00075   void markCharAcceptable(unsigned char X) {
00076     AcceptableChars[X/32] |= 1 << (X&31);
00077   }
00078   void markCharUnacceptable(unsigned char X) {
00079     AcceptableChars[X/32] &= ~(1 << (X&31));
00080   }
00081   bool isCharAcceptable(unsigned char X) const {
00082     return (AcceptableChars[X/32] & (1 << (X&31))) != 0;
00083   }
00084   
00085   /// getValueName - Returns the mangled name of V, an LLVM Value,
00086   /// in the current module.
00087   ///
00088   std::string getValueName(const GlobalValue *V, const char *Suffix = "");
00089   std::string getValueName(const Value *V);
00090 
00091   /// makeNameProper - We don't want identifier names with ., space, or
00092   /// - in them, so we mangle these characters into the strings "d_",
00093   /// "s_", and "D_", respectively. This is a very simple mangling that
00094   /// doesn't guarantee unique names for values. getValueName already
00095   /// does this for you, so there's no point calling it on the result
00096   /// from getValueName.
00097   ///
00098   std::string makeNameProper(const std::string &x, const char *Prefix = "");
00099 
00100 private:
00101   /// getTypeID - Return a unique ID for the specified LLVM type.
00102   ///
00103   unsigned getTypeID(const Type *Ty);
00104 };
00105 
00106 } // End llvm namespace
00107 
00108 #endif // LLVM_SUPPORT_MANGLER_H