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, bool UseAt) {
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 (UseAt) {
00043     OS << '@';
00044   } else {
00045     char Prefix = DL.getGlobalPrefix();
00046     if (Prefix != '\0')
00047       OS << Prefix;
00048   }
00049 
00050   // If this is a simple string that doesn't need escaping, just append it.
00051   OS << Name;
00052 }
00053 
00054 void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
00055                                 ManglerPrefixTy PrefixTy) const {
00056   return getNameWithPrefixx(OS, GVName, PrefixTy, *DL, false);
00057 }
00058 
00059 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
00060                                 const Twine &GVName,
00061                                 ManglerPrefixTy PrefixTy) const {
00062   raw_svector_ostream OS(OutName);
00063   return getNameWithPrefix(OS, GVName, PrefixTy);
00064 }
00065 
00066 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
00067 /// a suffix on their name indicating the number of words of arguments they
00068 /// take.
00069 static void AddFastCallStdCallSuffix(raw_ostream &OS, const Function *F,
00070                                      const DataLayout &TD) {
00071   // Calculate arguments size total.
00072   unsigned ArgWords = 0;
00073   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
00074        AI != AE; ++AI) {
00075     Type *Ty = AI->getType();
00076     // 'Dereference' type in case of byval or inalloca parameter attribute.
00077     if (AI->hasByValOrInAllocaAttr())
00078       Ty = cast<PointerType>(Ty)->getElementType();
00079     // Size should be aligned to DWORD boundary
00080     ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
00081   }
00082 
00083   OS << '@' << ArgWords;
00084 }
00085 
00086 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
00087                                 bool CannotUsePrivateLabel) const {
00088   ManglerPrefixTy PrefixTy = Mangler::Default;
00089   if (GV->hasPrivateLinkage()) {
00090     if (CannotUsePrivateLabel)
00091       PrefixTy = Mangler::LinkerPrivate;
00092     else
00093       PrefixTy = Mangler::Private;
00094   }
00095 
00096   if (!GV->hasName()) {
00097     // Get the ID for the global, assigning a new one if we haven't got one
00098     // already.
00099     unsigned &ID = AnonGlobalIDs[GV];
00100     if (ID == 0)
00101       ID = NextAnonGlobalID++;
00102 
00103     // Must mangle the global into a unique ID.
00104     getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
00105     return;
00106   }
00107 
00108   StringRef Name = GV->getName();
00109 
00110   bool UseAt = false;
00111   const Function *MSFunc = nullptr;
00112   CallingConv::ID CC;
00113   if (Name[0] != '\1' && DL->hasMicrosoftFastStdCallMangling()) {
00114     if ((MSFunc = dyn_cast<Function>(GV))) {
00115       CC = MSFunc->getCallingConv();
00116       // fastcall functions need to start with @ instead of _.
00117       if (CC == CallingConv::X86_FastCall)
00118         UseAt = true;
00119     }
00120   }
00121 
00122   getNameWithPrefixx(OS, Name, PrefixTy, *DL, UseAt);
00123 
00124   if (!MSFunc)
00125     return;
00126 
00127   // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
00128   // add it.
00129   // fastcall and stdcall functions usually need @42 at the end to specify
00130   // the argument info.
00131   FunctionType *FT = MSFunc->getFunctionType();
00132   if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
00133       // "Pure" variadic functions do not receive @0 suffix.
00134       (!FT->isVarArg() || FT->getNumParams() == 0 ||
00135        (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
00136     AddFastCallStdCallSuffix(OS, MSFunc, *DL);
00137 }
00138 
00139 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
00140                                 const GlobalValue *GV,
00141                                 bool CannotUsePrivateLabel) const {
00142   raw_svector_ostream OS(OutName);
00143   getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
00144 }