LLVM  mainline
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/IR/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/IR/Module.h"
00021 #include "llvm/Support/raw_ostream.h"
00022 using namespace llvm;
00023 
00024 namespace {
00025 enum ManglerPrefixTy {
00026   Default,      ///< Emit default string before each symbol.
00027   Private,      ///< Emit "private" prefix before each symbol.
00028   LinkerPrivate ///< Emit "linker private" prefix before each symbol.
00029 };
00030 }
00031 
00032 static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
00033                                   ManglerPrefixTy PrefixTy,
00034                                   const DataLayout &DL, char Prefix) {
00035   SmallString<256> TmpData;
00036   StringRef Name = GVName.toStringRef(TmpData);
00037   assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
00038 
00039   // No need to do anything special if the global has the special "do not
00040   // mangle" flag in the name.
00041   if (Name[0] == '\1') {
00042     OS << Name.substr(1);
00043     return;
00044   }
00045 
00046   if (PrefixTy == Private)
00047     OS << DL.getPrivateGlobalPrefix();
00048   else if (PrefixTy == LinkerPrivate)
00049     OS << DL.getLinkerPrivateGlobalPrefix();
00050 
00051   if (Prefix != '\0')
00052     OS << Prefix;
00053 
00054   // If this is a simple string that doesn't need escaping, just append it.
00055   OS << Name;
00056 }
00057 
00058 static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
00059                                   const DataLayout &DL,
00060                                   ManglerPrefixTy PrefixTy) {
00061   char Prefix = DL.getGlobalPrefix();
00062   return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
00063 }
00064 
00065 void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
00066                                 const DataLayout &DL) {
00067   return getNameWithPrefixImpl(OS, GVName, DL, Default);
00068 }
00069 
00070 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
00071                                 const Twine &GVName, const DataLayout &DL) {
00072   raw_svector_ostream OS(OutName);
00073   char Prefix = DL.getGlobalPrefix();
00074   return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
00075 }
00076 
00077 static bool hasByteCountSuffix(CallingConv::ID CC) {
00078   switch (CC) {
00079   case CallingConv::X86_FastCall:
00080   case CallingConv::X86_StdCall:
00081   case CallingConv::X86_VectorCall:
00082     return true;
00083   default:
00084     return false;
00085   }
00086 }
00087 
00088 /// Microsoft fastcall and stdcall functions require a suffix on their name
00089 /// indicating the number of words of arguments they take.
00090 static void addByteCountSuffix(raw_ostream &OS, const Function *F,
00091                                const DataLayout &DL) {
00092   // Calculate arguments size total.
00093   unsigned ArgWords = 0;
00094   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
00095        AI != AE; ++AI) {
00096     Type *Ty = AI->getType();
00097     // 'Dereference' type in case of byval or inalloca parameter attribute.
00098     if (AI->hasByValOrInAllocaAttr())
00099       Ty = cast<PointerType>(Ty)->getElementType();
00100     // Size should be aligned to pointer size.
00101     unsigned PtrSize = DL.getPointerSize();
00102     ArgWords += RoundUpToAlignment(DL.getTypeAllocSize(Ty), PtrSize);
00103   }
00104 
00105   OS << '@' << ArgWords;
00106 }
00107 
00108 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
00109                                 bool CannotUsePrivateLabel) const {
00110   ManglerPrefixTy PrefixTy = Default;
00111   if (GV->hasPrivateLinkage()) {
00112     if (CannotUsePrivateLabel)
00113       PrefixTy = LinkerPrivate;
00114     else
00115       PrefixTy = Private;
00116   }
00117 
00118   const DataLayout &DL = GV->getParent()->getDataLayout();
00119   if (!GV->hasName()) {
00120     // Get the ID for the global, assigning a new one if we haven't got one
00121     // already.
00122     unsigned &ID = AnonGlobalIDs[GV];
00123     if (ID == 0)
00124       ID = NextAnonGlobalID++;
00125 
00126     // Must mangle the global into a unique ID.
00127     getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
00128     return;
00129   }
00130 
00131   StringRef Name = GV->getName();
00132   char Prefix = DL.getGlobalPrefix();
00133 
00134   // Mangle functions with Microsoft calling conventions specially.  Only do
00135   // this mangling for x86_64 vectorcall and 32-bit x86.
00136   const Function *MSFunc = dyn_cast<Function>(GV);
00137   if (Name.startswith("\01"))
00138     MSFunc = nullptr; // Don't mangle when \01 is present.
00139   CallingConv::ID CC =
00140       MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
00141   if (!DL.hasMicrosoftFastStdCallMangling() &&
00142       CC != CallingConv::X86_VectorCall)
00143     MSFunc = nullptr;
00144   if (MSFunc) {
00145     if (CC == CallingConv::X86_FastCall)
00146       Prefix = '@'; // fastcall functions have an @ prefix instead of _.
00147     else if (CC == CallingConv::X86_VectorCall)
00148       Prefix = '\0'; // vectorcall functions have no prefix.
00149   }
00150 
00151   getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
00152 
00153   if (!MSFunc)
00154     return;
00155 
00156   // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
00157   // or vectorcall, add it.  These functions have a suffix of @N where N is the
00158   // cumulative byte size of all of the parameters to the function in decimal.
00159   if (CC == CallingConv::X86_VectorCall)
00160     OS << '@'; // vectorcall functions use a double @ suffix.
00161   FunctionType *FT = MSFunc->getFunctionType();
00162   if (hasByteCountSuffix(CC) &&
00163       // "Pure" variadic functions do not receive @0 suffix.
00164       (!FT->isVarArg() || FT->getNumParams() == 0 ||
00165        (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
00166     addByteCountSuffix(OS, MSFunc, DL);
00167 }
00168 
00169 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
00170                                 const GlobalValue *GV,
00171                                 bool CannotUsePrivateLabel) const {
00172   raw_svector_ostream OS(OutName);
00173   getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
00174 }