LLVM API Documentation

Mangler.cpp
Go to the documentation of this file.
00001 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
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 assembly backends.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Target/Mangler.h"
00015 #include "llvm/ADT/SmallString.h"
00016 #include "llvm/ADT/Twine.h"
00017 #include "llvm/IR/DataLayout.h"
00018 #include "llvm/IR/DerivedTypes.h"
00019 #include "llvm/IR/Function.h"
00020 #include "llvm/MC/MCAsmInfo.h"
00021 #include "llvm/MC/MCContext.h"
00022 #include "llvm/Support/raw_ostream.h"
00023 using namespace llvm;
00024 
00025 static bool isAcceptableChar(char C, bool AllowPeriod, bool AllowUTF8) {
00026   if ((C < 'a' || C > 'z') &&
00027       (C < 'A' || C > 'Z') &&
00028       (C < '0' || C > '9') &&
00029       C != '_' && C != '$' && C != '@' &&
00030       !(AllowPeriod && C == '.') &&
00031       !(AllowUTF8 && (C & 0x80)))
00032     return false;
00033   return true;
00034 }
00035 
00036 static char HexDigit(int V) {
00037   return V < 10 ? V+'0' : V+'A'-10;
00038 }
00039 
00040 static void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) {
00041   OutName.push_back('_');
00042   OutName.push_back(HexDigit(C >> 4));
00043   OutName.push_back(HexDigit(C & 15));
00044   OutName.push_back('_');
00045 }
00046 
00047 /// NameNeedsEscaping - Return true if the identifier \p Str needs quotes
00048 /// for this assembler.
00049 static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
00050   assert(!Str.empty() && "Cannot create an empty MCSymbol");
00051   
00052   // If the first character is a number and the target does not allow this, we
00053   // need quotes.
00054   if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
00055     return true;
00056   
00057   // If any of the characters in the string is an unacceptable character, force
00058   // quotes.
00059   bool AllowPeriod = MAI.doesAllowPeriodsInName();
00060   bool AllowUTF8 = MAI.doesAllowUTF8();
00061   for (unsigned i = 0, e = Str.size(); i != e; ++i)
00062     if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8))
00063       return true;
00064   return false;
00065 }
00066 
00067 /// appendMangledName - Add the specified string in mangled form if it uses
00068 /// any unusual characters.
00069 static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str,
00070                               const MCAsmInfo &MAI) {
00071   // The first character is not allowed to be a number unless the target
00072   // explicitly allows it.
00073   if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
00074     MangleLetter(OutName, Str[0]);
00075     Str = Str.substr(1);
00076   }
00077 
00078   bool AllowPeriod = MAI.doesAllowPeriodsInName();
00079   bool AllowUTF8 = MAI.doesAllowUTF8();
00080   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
00081     if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8))
00082       MangleLetter(OutName, Str[i]);
00083     else
00084       OutName.push_back(Str[i]);
00085   }
00086 }
00087 
00088 
00089 /// appendMangledQuotedName - On systems that support quoted symbols, we still
00090 /// have to escape some (obscure) characters like " and \n which would break the
00091 /// assembler's lexing.
00092 static void appendMangledQuotedName(SmallVectorImpl<char> &OutName,
00093                                    StringRef Str) {
00094   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
00095     if (Str[i] == '"' || Str[i] == '\n')
00096       MangleLetter(OutName, Str[i]);
00097     else
00098       OutName.push_back(Str[i]);
00099   }
00100 }
00101 
00102 
00103 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
00104 /// and the specified name as the global variable name.  GVName must not be
00105 /// empty.
00106 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
00107                                 const Twine &GVName, ManglerPrefixTy PrefixTy) {
00108   SmallString<256> TmpData;
00109   StringRef Name = GVName.toStringRef(TmpData);
00110   assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
00111   
00112   const MCAsmInfo &MAI = Context.getAsmInfo();
00113   
00114   // If the global name is not led with \1, add the appropriate prefixes.
00115   if (Name[0] == '\1') {
00116     Name = Name.substr(1);
00117   } else {
00118     if (PrefixTy == Mangler::Private) {
00119       const char *Prefix = MAI.getPrivateGlobalPrefix();
00120       OutName.append(Prefix, Prefix+strlen(Prefix));
00121     } else if (PrefixTy == Mangler::LinkerPrivate) {
00122       const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
00123       OutName.append(Prefix, Prefix+strlen(Prefix));
00124     }
00125 
00126     const char *Prefix = MAI.getGlobalPrefix();
00127     if (Prefix[0] == 0)
00128       ; // Common noop, no prefix.
00129     else if (Prefix[1] == 0)
00130       OutName.push_back(Prefix[0]);  // Common, one character prefix.
00131     else
00132       OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
00133   }
00134   
00135   // If this is a simple string that doesn't need escaping, just append it.
00136   if (!NameNeedsEscaping(Name, MAI) ||
00137       // If quotes are supported, they can be used unless the string contains
00138       // a quote or newline.
00139       (MAI.doesAllowQuotesInName() &&
00140        Name.find_first_of("\n\"") == StringRef::npos)) {
00141     OutName.append(Name.begin(), Name.end());
00142     return;
00143   }
00144   
00145   // On systems that do not allow quoted names, we need to mangle most
00146   // strange characters.
00147   if (!MAI.doesAllowQuotesInName())
00148     return appendMangledName(OutName, Name, MAI);
00149   
00150   // Okay, the system allows quoted strings.  We can quote most anything, the
00151   // only characters that need escaping are " and \n.
00152   assert(Name.find_first_of("\n\"") != StringRef::npos);
00153   return appendMangledQuotedName(OutName, Name);
00154 }
00155 
00156 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
00157 /// a suffix on their name indicating the number of words of arguments they
00158 /// take.
00159 static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
00160                                      const Function *F, const DataLayout &TD) {
00161   // Calculate arguments size total.
00162   unsigned ArgWords = 0;
00163   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
00164        AI != AE; ++AI) {
00165     Type *Ty = AI->getType();
00166     // 'Dereference' type in case of byval parameter attribute
00167     if (AI->hasByValAttr())
00168       Ty = cast<PointerType>(Ty)->getElementType();
00169     // Size should be aligned to DWORD boundary
00170     ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
00171   }
00172   
00173   raw_svector_ostream(OutName) << '@' << ArgWords;
00174 }
00175 
00176 
00177 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
00178 /// and the specified global variable's name.  If the global variable doesn't
00179 /// have a name, this fills in a unique name for the global.
00180 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
00181                                 const GlobalValue *GV,
00182                                 bool isImplicitlyPrivate) {
00183   ManglerPrefixTy PrefixTy = Mangler::Default;
00184   if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
00185     PrefixTy = Mangler::Private;
00186   else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
00187     PrefixTy = Mangler::LinkerPrivate;
00188   
00189   // If this global has a name, handle it simply.
00190   if (GV->hasName()) {
00191     StringRef Name = GV->getName();
00192     getNameWithPrefix(OutName, Name, PrefixTy);
00193     // No need to do anything else if the global has the special "do not mangle"
00194     // flag in the name.
00195     if (Name[0] == 1)
00196       return;
00197   } else {
00198     // Get the ID for the global, assigning a new one if we haven't got one
00199     // already.
00200     unsigned &ID = AnonGlobalIDs[GV];
00201     if (ID == 0) ID = NextAnonGlobalID++;
00202   
00203     // Must mangle the global into a unique ID.
00204     getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
00205   }
00206   
00207   // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
00208   // add it.
00209   if (Context.getAsmInfo().hasMicrosoftFastStdCallMangling()) {
00210     if (const Function *F = dyn_cast<Function>(GV)) {
00211       CallingConv::ID CC = F->getCallingConv();
00212     
00213       // fastcall functions need to start with @.
00214       // FIXME: This logic seems unlikely to be right.
00215       if (CC == CallingConv::X86_FastCall) {
00216         if (OutName[0] == '_')
00217           OutName[0] = '@';
00218         else
00219           OutName.insert(OutName.begin(), '@');
00220       }
00221     
00222       // fastcall and stdcall functions usually need @42 at the end to specify
00223       // the argument info.
00224       FunctionType *FT = F->getFunctionType();
00225       if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
00226           // "Pure" variadic functions do not receive @0 suffix.
00227           (!FT->isVarArg() || FT->getNumParams() == 0 ||
00228            (FT->getNumParams() == 1 && F->hasStructRetAttr())))
00229         AddFastCallStdCallSuffix(OutName, F, TD);
00230     }
00231   }
00232 }
00233 
00234 /// getSymbol - Return the MCSymbol for the specified global value.  This
00235 /// symbol is the main label that is the address of the global.
00236 MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
00237   SmallString<60> NameStr;
00238   getNameWithPrefix(NameStr, GV, false);
00239   return Context.GetOrCreateSymbol(NameStr.str());
00240 }
00241 
00242