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