LCOV - code coverage report
Current view: top level - lib/IR - Mangler.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 83 83 100.0 %
Date: 2017-09-14 15:23:50 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // Unified name mangler for assembly backends.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/IR/Mangler.h"
      15             : #include "llvm/ADT/SmallString.h"
      16             : #include "llvm/ADT/Triple.h"
      17             : #include "llvm/ADT/Twine.h"
      18             : #include "llvm/IR/DataLayout.h"
      19             : #include "llvm/IR/DerivedTypes.h"
      20             : #include "llvm/IR/Function.h"
      21             : #include "llvm/IR/Module.h"
      22             : #include "llvm/Support/raw_ostream.h"
      23             : using namespace llvm;
      24             : 
      25             : namespace {
      26             : enum ManglerPrefixTy {
      27             :   Default,      ///< Emit default string before each symbol.
      28             :   Private,      ///< Emit "private" prefix before each symbol.
      29             :   LinkerPrivate ///< Emit "linker private" prefix before each symbol.
      30             : };
      31             : }
      32             : 
      33     1114907 : static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
      34             :                                   ManglerPrefixTy PrefixTy,
      35             :                                   const DataLayout &DL, char Prefix) {
      36     2228240 :   SmallString<256> TmpData;
      37     1114907 :   StringRef Name = GVName.toStringRef(TmpData);
      38             :   assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
      39             : 
      40             :   // No need to do anything special if the global has the special "do not
      41             :   // mangle" flag in the name.
      42     2229814 :   if (Name[0] == '\1') {
      43        1574 :     OS << Name.substr(1);
      44        1574 :     return;
      45             :   }
      46             : 
      47     1113333 :   if (PrefixTy == Private)
      48       98005 :     OS << DL.getPrivateGlobalPrefix();
      49     1015328 :   else if (PrefixTy == LinkerPrivate)
      50         560 :     OS << DL.getLinkerPrivateGlobalPrefix();
      51             : 
      52     1113333 :   if (Prefix != '\0')
      53       34812 :     OS << Prefix;
      54             : 
      55             :   // If this is a simple string that doesn't need escaping, just append it.
      56     1113333 :   OS << Name;
      57             : }
      58             : 
      59       63568 : static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
      60             :                                   const DataLayout &DL,
      61             :                                   ManglerPrefixTy PrefixTy) {
      62      127136 :   char Prefix = DL.getGlobalPrefix();
      63       63568 :   return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
      64             : }
      65             : 
      66         661 : void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
      67             :                                 const DataLayout &DL) {
      68         661 :   return getNameWithPrefixImpl(OS, GVName, DL, Default);
      69             : }
      70             : 
      71       16814 : void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
      72             :                                 const Twine &GVName, const DataLayout &DL) {
      73       33628 :   raw_svector_ostream OS(OutName);
      74       33628 :   char Prefix = DL.getGlobalPrefix();
      75       33628 :   return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
      76             : }
      77             : 
      78             : static bool hasByteCountSuffix(CallingConv::ID CC) {
      79             :   switch (CC) {
      80             :   case CallingConv::X86_FastCall:
      81             :   case CallingConv::X86_StdCall:
      82             :   case CallingConv::X86_VectorCall:
      83             :     return true;
      84             :   default:
      85             :     return false;
      86             :   }
      87             : }
      88             : 
      89             : /// Microsoft fastcall and stdcall functions require a suffix on their name
      90             : /// indicating the number of words of arguments they take.
      91         194 : static void addByteCountSuffix(raw_ostream &OS, const Function *F,
      92             :                                const DataLayout &DL) {
      93             :   // Calculate arguments size total.
      94         194 :   unsigned ArgWords = 0;
      95         649 :   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
      96         455 :        AI != AE; ++AI) {
      97         261 :     Type *Ty = AI->getType();
      98             :     // 'Dereference' type in case of byval or inalloca parameter attribute.
      99         261 :     if (AI->hasByValOrInAllocaAttr())
     100           9 :       Ty = cast<PointerType>(Ty)->getElementType();
     101             :     // Size should be aligned to pointer size.
     102         261 :     unsigned PtrSize = DL.getPointerSize();
     103         522 :     ArgWords += alignTo(DL.getTypeAllocSize(Ty), PtrSize);
     104             :   }
     105             : 
     106         388 :   OS << '@' << ArgWords;
     107         194 : }
     108             : 
     109     1097432 : void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
     110             :                                 bool CannotUsePrivateLabel) const {
     111     1097432 :   ManglerPrefixTy PrefixTy = Default;
     112     2194864 :   if (GV->hasPrivateLinkage()) {
     113       98517 :     if (CannotUsePrivateLabel)
     114             :       PrefixTy = LinkerPrivate;
     115             :     else
     116       98144 :       PrefixTy = Private;
     117             :   }
     118             : 
     119     1097432 :   const DataLayout &DL = GV->getParent()->getDataLayout();
     120     2194864 :   if (!GV->hasName()) {
     121             :     // Get the ID for the global, assigning a new one if we haven't got one
     122             :     // already.
     123      125814 :     unsigned &ID = AnonGlobalIDs[GV];
     124       62907 :     if (ID == 0)
     125       42076 :       ID = AnonGlobalIDs.size();
     126             : 
     127             :     // Must mangle the global into a unique ID.
     128      251628 :     getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
     129     1158554 :     return;
     130             :   }
     131             : 
     132     1034525 :   StringRef Name = GV->getName();
     133     2069050 :   char Prefix = DL.getGlobalPrefix();
     134             : 
     135             :   // Mangle functions with Microsoft calling conventions specially.  Only do
     136             :   // this mangling for x86_64 vectorcall and 32-bit x86.
     137     2069050 :   const Function *MSFunc = dyn_cast<Function>(GV);
     138     1034525 :   if (Name.startswith("\01"))
     139             :     MSFunc = nullptr; // Don't mangle when \01 is present.
     140             :   CallingConv::ID CC =
     141     1370731 :       MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
     142     1034525 :   if (!DL.hasMicrosoftFastStdCallMangling() &&
     143             :       CC != CallingConv::X86_VectorCall)
     144             :     MSFunc = nullptr;
     145        2749 :   if (MSFunc) {
     146        1785 :     if (CC == CallingConv::X86_FastCall)
     147             :       Prefix = '@'; // fastcall functions have an @ prefix instead of _.
     148        1680 :     else if (CC == CallingConv::X86_VectorCall)
     149          56 :       Prefix = '\0'; // vectorcall functions have no prefix.
     150             :   }
     151             : 
     152     2069050 :   getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
     153             : 
     154     1034525 :   if (!MSFunc)
     155             :     return;
     156             : 
     157             :   // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
     158             :   // or vectorcall, add it.  These functions have a suffix of @N where N is the
     159             :   // cumulative byte size of all of the parameters to the function in decimal.
     160        1785 :   if (CC == CallingConv::X86_VectorCall)
     161             :     OS << '@'; // vectorcall functions use a double @ suffix.
     162        1785 :   FunctionType *FT = MSFunc->getFunctionType();
     163         398 :   if (hasByteCountSuffix(CC) &&
     164             :       // "Pure" variadic functions do not receive @0 suffix.
     165         217 :       (!FT->isVarArg() || FT->getNumParams() == 0 ||
     166           4 :        (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
     167         194 :     addByteCountSuffix(OS, MSFunc, DL);
     168             : }
     169             : 
     170     1088689 : void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
     171             :                                 const GlobalValue *GV,
     172             :                                 bool CannotUsePrivateLabel) const {
     173     2177378 :   raw_svector_ostream OS(OutName);
     174     1088689 :   getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
     175     1088689 : }
     176             : 
     177        4552 : void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
     178             :                                         const Triple &TT, Mangler &Mangler) {
     179        4552 :   if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
     180             :     return;
     181             : 
     182         104 :   if (TT.isKnownWindowsMSVCEnvironment())
     183         104 :     OS << " /EXPORT:";
     184             :   else
     185         143 :     OS << " -export:";
     186             : 
     187             :   if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
     188         256 :     std::string Flag;
     189         256 :     raw_string_ostream FlagOS(Flag);
     190         128 :     Mangler.getNameWithPrefix(FlagOS, GV, false);
     191         128 :     FlagOS.flush();
     192         256 :     if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
     193         192 :       OS << Flag.substr(1);
     194             :     else
     195             :       OS << Flag;
     196             :   } else {
     197         119 :     Mangler.getNameWithPrefix(OS, GV, false);
     198             :   }
     199             : 
     200         494 :   if (!GV->getValueType()->isFunctionTy()) {
     201          34 :     if (TT.isKnownWindowsMSVCEnvironment())
     202          34 :       OS << ",DATA";
     203             :     else
     204          47 :       OS << ",data";
     205             :   }
     206             : }

Generated by: LCOV version 1.13