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   if (PrefixTy == Mangler::Private)
00031     OS << DL.getPrivateGlobalPrefix();
00032   else if (PrefixTy == Mangler::LinkerPrivate)
00033     OS << DL.getLinkerPrivateGlobalPrefix();
00034 
00035   if (UseAt) {
00036     OS << '@';
00037   } else {
00038     char Prefix = DL.getGlobalPrefix();
00039     if (Prefix != '\0')
00040       OS << Prefix;
00041   }
00042 
00043   // If this is a simple string that doesn't need escaping, just append it.
00044   OS << Name;
00045 }
00046 
00047 void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
00048                                 ManglerPrefixTy PrefixTy) const {
00049   return getNameWithPrefixx(OS, GVName, PrefixTy, *DL, false);
00050 }
00051 
00052 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
00053                                 const Twine &GVName,
00054                                 ManglerPrefixTy PrefixTy) const {
00055   raw_svector_ostream OS(OutName);
00056   return getNameWithPrefix(OS, GVName, PrefixTy);
00057 }
00058 
00059 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
00060 /// a suffix on their name indicating the number of words of arguments they
00061 /// take.
00062 static void AddFastCallStdCallSuffix(raw_ostream &OS, const Function *F,
00063                                      const DataLayout &TD) {
00064   // Calculate arguments size total.
00065   unsigned ArgWords = 0;
00066   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
00067        AI != AE; ++AI) {
00068     Type *Ty = AI->getType();
00069     // 'Dereference' type in case of byval or inalloca parameter attribute.
00070     if (AI->hasByValOrInAllocaAttr())
00071       Ty = cast<PointerType>(Ty)->getElementType();
00072     // Size should be aligned to DWORD boundary
00073     ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
00074   }
00075 
00076   OS << '@' << ArgWords;
00077 }
00078 
00079 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
00080                                 bool CannotUsePrivateLabel) const {
00081   ManglerPrefixTy PrefixTy = Mangler::Default;
00082   if (GV->hasPrivateLinkage()) {
00083     if (CannotUsePrivateLabel)
00084       PrefixTy = Mangler::LinkerPrivate;
00085     else
00086       PrefixTy = Mangler::Private;
00087   }
00088 
00089   if (!GV->hasName()) {
00090     // Get the ID for the global, assigning a new one if we haven't got one
00091     // already.
00092     unsigned &ID = AnonGlobalIDs[GV];
00093     if (ID == 0)
00094       ID = NextAnonGlobalID++;
00095 
00096     // Must mangle the global into a unique ID.
00097     getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
00098     return;
00099   }
00100 
00101   StringRef Name = GV->getName();
00102 
00103   // No need to do anything special if the global has the special "do not
00104   // mangle" flag in the name.
00105   if (Name[0] == '\1') {
00106     OS << Name.substr(1);
00107     return;
00108   }
00109 
00110   bool UseAt = false;
00111   const Function *MSFunc = nullptr;
00112   CallingConv::ID CC;
00113   if (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 }