LCOV - code coverage report
Current view: top level - lib/IR - Mangler.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 72 72 100.0 %
Date: 2018-06-17 00:07:59 Functions: 9 9 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     1427452 : static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
      34             :                                   ManglerPrefixTy PrefixTy,
      35             :                                   const DataLayout &DL, char Prefix) {
      36             :   SmallString<256> TmpData;
      37     1427452 :   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     2854904 :   if (Name[0] == '\1') {
      43        2067 :     OS << Name.substr(1);
      44             :     return;
      45             :   }
      46             : 
      47     2850770 :   if (DL.doNotMangleLeadingQuestionMark() && Name[0] == '?')
      48             :     Prefix = '\0';
      49             : 
      50     1425385 :   if (PrefixTy == Private)
      51      145598 :     OS << DL.getPrivateGlobalPrefix();
      52     1279787 :   else if (PrefixTy == LinkerPrivate)
      53         273 :     OS << DL.getLinkerPrivateGlobalPrefix();
      54             : 
      55     1425385 :   if (Prefix != '\0')
      56             :     OS << Prefix;
      57             : 
      58             :   // If this is a simple string that doesn't need escaping, just append it.
      59     1425385 :   OS << Name;
      60             : }
      61             : 
      62       72372 : static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
      63             :                                   const DataLayout &DL,
      64             :                                   ManglerPrefixTy PrefixTy) {
      65       72372 :   char Prefix = DL.getGlobalPrefix();
      66       72372 :   return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
      67             : }
      68             : 
      69         673 : void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
      70             :                                 const DataLayout &DL) {
      71         673 :   return getNameWithPrefixImpl(OS, GVName, DL, Default);
      72             : }
      73             : 
      74       19589 : void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
      75             :                                 const Twine &GVName, const DataLayout &DL) {
      76             :   raw_svector_ostream OS(OutName);
      77       19589 :   char Prefix = DL.getGlobalPrefix();
      78       39178 :   return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
      79             : }
      80             : 
      81             : static bool hasByteCountSuffix(CallingConv::ID CC) {
      82             :   switch (CC) {
      83             :   case CallingConv::X86_FastCall:
      84             :   case CallingConv::X86_StdCall:
      85             :   case CallingConv::X86_VectorCall:
      86             :     return true;
      87             :   default:
      88             :     return false;
      89             :   }
      90             : }
      91             : 
      92             : /// Microsoft fastcall and stdcall functions require a suffix on their name
      93             : /// indicating the number of words of arguments they take.
      94         212 : static void addByteCountSuffix(raw_ostream &OS, const Function *F,
      95             :                                const DataLayout &DL) {
      96             :   // Calculate arguments size total.
      97             :   unsigned ArgWords = 0;
      98         287 :   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
      99         499 :        AI != AE; ++AI) {
     100         287 :     Type *Ty = AI->getType();
     101             :     // 'Dereference' type in case of byval or inalloca parameter attribute.
     102         287 :     if (AI->hasByValOrInAllocaAttr())
     103           9 :       Ty = cast<PointerType>(Ty)->getElementType();
     104             :     // Size should be aligned to pointer size.
     105         287 :     unsigned PtrSize = DL.getPointerSize();
     106         574 :     ArgWords += alignTo(DL.getTypeAllocSize(Ty), PtrSize);
     107             :   }
     108             : 
     109             :   OS << '@' << ArgWords;
     110         212 : }
     111             : 
     112     1407190 : void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
     113             :                                 bool CannotUsePrivateLabel) const {
     114             :   ManglerPrefixTy PrefixTy = Default;
     115     2814380 :   if (GV->hasPrivateLinkage()) {
     116      146114 :     if (CannotUsePrivateLabel)
     117             :       PrefixTy = LinkerPrivate;
     118             :     else
     119             :       PrefixTy = Private;
     120             :   }
     121             : 
     122     1407190 :   const DataLayout &DL = GV->getParent()->getDataLayout();
     123     2814380 :   if (!GV->hasName()) {
     124             :     // Get the ID for the global, assigning a new one if we haven't got one
     125             :     // already.
     126       71699 :     unsigned &ID = AnonGlobalIDs[GV];
     127       71699 :     if (ID == 0)
     128       24105 :       ID = AnonGlobalIDs.size();
     129             : 
     130             :     // Must mangle the global into a unique ID.
     131      143398 :     getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
     132     1476752 :     return;
     133             :   }
     134             : 
     135     1335491 :   StringRef Name = GV->getName();
     136     1335491 :   char Prefix = DL.getGlobalPrefix();
     137             : 
     138             :   // Mangle functions with Microsoft calling conventions specially.  Only do
     139             :   // this mangling for x86_64 vectorcall and 32-bit x86.
     140     1335491 :   const Function *MSFunc = dyn_cast<Function>(GV);
     141             : 
     142             :   // Don't add byte count suffixes when '\01' or '?' are in the first
     143             :   // character.
     144     1333427 :   if (Name.startswith("\01") ||
     145             :       (DL.doNotMangleLeadingQuestionMark() && Name.startswith("?")))
     146             :     MSFunc = nullptr;
     147             : 
     148             :   CallingConv::ID CC =
     149     1333374 :       MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
     150     1335491 :   if (!DL.hasMicrosoftFastStdCallMangling() &&
     151             :       CC != CallingConv::X86_VectorCall)
     152             :     MSFunc = nullptr;
     153        3670 :   if (MSFunc) {
     154        2137 :     if (CC == CallingConv::X86_FastCall)
     155             :       Prefix = '@'; // fastcall functions have an @ prefix instead of _.
     156        2021 :     else if (CC == CallingConv::X86_VectorCall)
     157             :       Prefix = '\0'; // vectorcall functions have no prefix.
     158             :   }
     159             : 
     160     2670982 :   getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
     161             : 
     162     1335491 :   if (!MSFunc)
     163             :     return;
     164             : 
     165             :   // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
     166             :   // or vectorcall, add it.  These functions have a suffix of @N where N is the
     167             :   // cumulative byte size of all of the parameters to the function in decimal.
     168        2137 :   if (CC == CallingConv::X86_VectorCall)
     169             :     OS << '@'; // vectorcall functions use a double @ suffix.
     170             :   FunctionType *FT = MSFunc->getFunctionType();
     171         217 :   if (hasByteCountSuffix(CC) &&
     172             :       // "Pure" variadic functions do not receive @0 suffix.
     173          18 :       (!FT->isVarArg() || FT->getNumParams() == 0 ||
     174           4 :        (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
     175         212 :     addByteCountSuffix(OS, MSFunc, DL);
     176             : }
     177             : 
     178     1388725 : void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
     179             :                                 const GlobalValue *GV,
     180             :                                 bool CannotUsePrivateLabel) const {
     181             :   raw_svector_ostream OS(OutName);
     182     1388725 :   getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
     183     1388725 : }
     184             : 
     185        5697 : void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
     186             :                                         const Triple &TT, Mangler &Mangler) {
     187        5697 :   if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
     188             :     return;
     189             : 
     190             :   if (TT.isKnownWindowsMSVCEnvironment())
     191         111 :     OS << " /EXPORT:";
     192             :   else
     193         143 :     OS << " -export:";
     194             : 
     195             :   if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
     196             :     std::string Flag;
     197         128 :     raw_string_ostream FlagOS(Flag);
     198         128 :     Mangler.getNameWithPrefix(FlagOS, GV, false);
     199             :     FlagOS.flush();
     200         256 :     if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
     201         128 :       OS << Flag.substr(1);
     202             :     else
     203             :       OS << Flag;
     204             :   } else {
     205         126 :     Mangler.getNameWithPrefix(OS, GV, false);
     206             :   }
     207             : 
     208         508 :   if (!GV->getValueType()->isFunctionTy()) {
     209             :     if (TT.isKnownWindowsMSVCEnvironment())
     210          35 :       OS << ",DATA";
     211             :     else
     212          47 :       OS << ",data";
     213             :   }
     214             : }
     215             : 
     216           8 : void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV,
     217             :                                       const Triple &T, Mangler &M) {
     218             :   if (!T.isKnownWindowsMSVCEnvironment())
     219             :     return;
     220             : 
     221           8 :   OS << " /INCLUDE:";
     222           8 :   M.getNameWithPrefix(OS, GV, false);
     223             : }
     224             : 

Generated by: LCOV version 1.13