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     // Skip arguments in registers to handle typical fastcall lowering.
00076     if (F->getAttributes().hasAttribute(AI->getArgNo() + 1, Attribute::InReg))
00077       continue;
00078     Type *Ty = AI->getType();
00079     // 'Dereference' type in case of byval or inalloca parameter attribute.
00080     if (AI->hasByValOrInAllocaAttr())
00081       Ty = cast<PointerType>(Ty)->getElementType();
00082     // Size should be aligned to DWORD boundary
00083     ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
00084   }
00085 
00086   OS << '@' << ArgWords;
00087 }
00088 
00089 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
00090                                 bool CannotUsePrivateLabel) const {
00091   ManglerPrefixTy PrefixTy = Mangler::Default;
00092   if (GV->hasPrivateLinkage()) {
00093     if (CannotUsePrivateLabel)
00094       PrefixTy = Mangler::LinkerPrivate;
00095     else
00096       PrefixTy = Mangler::Private;
00097   }
00098 
00099   if (!GV->hasName()) {
00100     // Get the ID for the global, assigning a new one if we haven't got one
00101     // already.
00102     unsigned &ID = AnonGlobalIDs[GV];
00103     if (ID == 0)
00104       ID = NextAnonGlobalID++;
00105 
00106     // Must mangle the global into a unique ID.
00107     getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
00108     return;
00109   }
00110 
00111   StringRef Name = GV->getName();
00112 
00113   bool UseAt = false;
00114   const Function *MSFunc = nullptr;
00115   CallingConv::ID CC;
00116   if (Name[0] != '\1' && DL->hasMicrosoftFastStdCallMangling()) {
00117     if ((MSFunc = dyn_cast<Function>(GV))) {
00118       CC = MSFunc->getCallingConv();
00119       // fastcall functions need to start with @ instead of _.
00120       if (CC == CallingConv::X86_FastCall)
00121         UseAt = true;
00122     }
00123   }
00124 
00125   getNameWithPrefixx(OS, Name, PrefixTy, *DL, UseAt);
00126 
00127   if (!MSFunc)
00128     return;
00129 
00130   // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
00131   // add it.
00132   // fastcall and stdcall functions usually need @42 at the end to specify
00133   // the argument info.
00134   FunctionType *FT = MSFunc->getFunctionType();
00135   if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
00136       // "Pure" variadic functions do not receive @0 suffix.
00137       (!FT->isVarArg() || FT->getNumParams() == 0 ||
00138        (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
00139     AddFastCallStdCallSuffix(OS, MSFunc, *DL);
00140 }
00141 
00142 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
00143                                 const GlobalValue *GV,
00144                                 bool CannotUsePrivateLabel) const {
00145   raw_svector_ostream OS(OutName);
00146   getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
00147 }