Brian Gaeke | d4dff19 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 1 | //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Brian Gaeke | d4dff19 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 1376b02 | 2010-01-16 21:08:46 +0000 | [diff] [blame] | 10 | // Unified name mangler for assembly backends. |
Brian Gaeke | d4dff19 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | f62e3ee | 2010-01-16 21:57:06 +0000 | [diff] [blame] | 14 | #include "llvm/Target/Mangler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
| 16 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DataLayout.h" |
| 18 | #include "llvm/IR/DerivedTypes.h" |
| 19 | #include "llvm/IR/Function.h" |
Chris Lattner | b4ffc89 | 2010-01-17 18:22:35 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | 2eff505 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCContext.h" |
Bill Wendling | 70b1400 | 2013-05-29 20:37:19 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 8d99c76 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c9499b6 | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 26 | /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix |
| 27 | /// and the specified name as the global variable name. GVName must not be |
| 28 | /// empty. |
| 29 | void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, |
Rafael Espindola | d0ed730 | 2013-11-27 02:25:20 +0000 | [diff] [blame^] | 30 | const Twine &GVName, ManglerPrefixTy PrefixTy) { |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 31 | SmallString<256> TmpData; |
Benjamin Kramer | 2e06b93 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 32 | StringRef Name = GVName.toStringRef(TmpData); |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 33 | assert(!Name.empty() && "getNameWithPrefix requires non-empty name"); |
| 34 | |
Rafael Espindola | e133ed8 | 2013-10-29 17:28:26 +0000 | [diff] [blame] | 35 | const MCAsmInfo *MAI = TM->getMCAsmInfo(); |
Chris Lattner | 2eff505 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 37 | // If the global name is not led with \1, add the appropriate prefixes. |
Chris Lattner | 83e872e | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 38 | if (Name[0] == '\1') { |
| 39 | Name = Name.substr(1); |
| 40 | } else { |
Chris Lattner | b4ffc89 | 2010-01-17 18:22:35 +0000 | [diff] [blame] | 41 | if (PrefixTy == Mangler::Private) { |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 42 | const char *Prefix = MAI->getPrivateGlobalPrefix(); |
Chris Lattner | b4ffc89 | 2010-01-17 18:22:35 +0000 | [diff] [blame] | 43 | OutName.append(Prefix, Prefix+strlen(Prefix)); |
| 44 | } else if (PrefixTy == Mangler::LinkerPrivate) { |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 45 | const char *Prefix = MAI->getLinkerPrivateGlobalPrefix(); |
Chris Lattner | b4ffc89 | 2010-01-17 18:22:35 +0000 | [diff] [blame] | 46 | OutName.append(Prefix, Prefix+strlen(Prefix)); |
| 47 | } |
| 48 | |
Rafael Espindola | d0ed730 | 2013-11-27 02:25:20 +0000 | [diff] [blame^] | 49 | |
| 50 | const char *Prefix = MAI->getGlobalPrefix(); |
| 51 | if (Prefix[0] == 0) |
| 52 | ; // Common noop, no prefix. |
| 53 | else if (Prefix[1] == 0) |
| 54 | OutName.push_back(Prefix[0]); // Common, one character prefix. |
| 55 | else |
| 56 | // Arbitrary length prefix. |
| 57 | OutName.append(Prefix, Prefix+strlen(Prefix)); |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 58 | } |
Rafael Espindola | fdc8813 | 2013-11-13 14:01:59 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 83e872e | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 60 | // If this is a simple string that doesn't need escaping, just append it. |
Rafael Espindola | fe4e088 | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 61 | OutName.append(Name.begin(), Name.end()); |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | 8d99c76 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 64 | /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require |
| 65 | /// a suffix on their name indicating the number of words of arguments they |
| 66 | /// take. |
| 67 | static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 68 | const Function *F, const DataLayout &TD) { |
Chris Lattner | 8d99c76 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 69 | // Calculate arguments size total. |
| 70 | unsigned ArgWords = 0; |
| 71 | for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 72 | AI != AE; ++AI) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 73 | Type *Ty = AI->getType(); |
Chris Lattner | 8d99c76 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 74 | // 'Dereference' type in case of byval parameter attribute |
| 75 | if (AI->hasByValAttr()) |
| 76 | Ty = cast<PointerType>(Ty)->getElementType(); |
| 77 | // Size should be aligned to DWORD boundary |
| 78 | ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4; |
| 79 | } |
| 80 | |
| 81 | raw_svector_ostream(OutName) << '@' << ArgWords; |
| 82 | } |
| 83 | |
Chris Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 84 | |
| 85 | /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix |
| 86 | /// and the specified global variable's name. If the global variable doesn't |
| 87 | /// have a name, this fills in a unique name for the global. |
| 88 | void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, |
Rafael Espindola | d0ed730 | 2013-11-27 02:25:20 +0000 | [diff] [blame^] | 89 | const GlobalValue *GV, |
| 90 | bool isImplicitlyPrivate) { |
Chris Lattner | c25475e | 2010-01-17 18:52:16 +0000 | [diff] [blame] | 91 | ManglerPrefixTy PrefixTy = Mangler::Default; |
| 92 | if (GV->hasPrivateLinkage() || isImplicitlyPrivate) |
| 93 | PrefixTy = Mangler::Private; |
Bill Wendling | 34bc34e | 2012-08-17 18:33:14 +0000 | [diff] [blame] | 94 | else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage()) |
Chris Lattner | c25475e | 2010-01-17 18:52:16 +0000 | [diff] [blame] | 95 | PrefixTy = Mangler::LinkerPrivate; |
| 96 | |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 97 | // If this global has a name, handle it simply. |
Chris Lattner | 8d99c76 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 98 | if (GV->hasName()) { |
Anton Korobeynikov | 9c0df16 | 2013-04-19 21:20:56 +0000 | [diff] [blame] | 99 | StringRef Name = GV->getName(); |
Rafael Espindola | d0ed730 | 2013-11-27 02:25:20 +0000 | [diff] [blame^] | 100 | getNameWithPrefix(OutName, Name, PrefixTy); |
Anton Korobeynikov | 9c0df16 | 2013-04-19 21:20:56 +0000 | [diff] [blame] | 101 | // No need to do anything else if the global has the special "do not mangle" |
| 102 | // flag in the name. |
| 103 | if (Name[0] == 1) |
| 104 | return; |
Chris Lattner | 8d99c76 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 105 | } else { |
| 106 | // Get the ID for the global, assigning a new one if we haven't got one |
| 107 | // already. |
| 108 | unsigned &ID = AnonGlobalIDs[GV]; |
| 109 | if (ID == 0) ID = NextAnonGlobalID++; |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 8d99c76 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 111 | // Must mangle the global into a unique ID. |
Rafael Espindola | d0ed730 | 2013-11-27 02:25:20 +0000 | [diff] [blame^] | 112 | getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy); |
Chris Lattner | 8d99c76 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 113 | } |
Chris Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 8d99c76 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 115 | // If we are supposed to add a microsoft-style suffix for stdcall/fastcall, |
| 116 | // add it. |
Rafael Espindola | e133ed8 | 2013-10-29 17:28:26 +0000 | [diff] [blame] | 117 | if (TM->getMCAsmInfo()->hasMicrosoftFastStdCallMangling()) { |
Chris Lattner | 8d99c76 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 118 | if (const Function *F = dyn_cast<Function>(GV)) { |
| 119 | CallingConv::ID CC = F->getCallingConv(); |
| 120 | |
| 121 | // fastcall functions need to start with @. |
| 122 | // FIXME: This logic seems unlikely to be right. |
| 123 | if (CC == CallingConv::X86_FastCall) { |
| 124 | if (OutName[0] == '_') |
| 125 | OutName[0] = '@'; |
| 126 | else |
| 127 | OutName.insert(OutName.begin(), '@'); |
| 128 | } |
| 129 | |
| 130 | // fastcall and stdcall functions usually need @42 at the end to specify |
| 131 | // the argument info. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 132 | FunctionType *FT = F->getFunctionType(); |
Chris Lattner | 8d99c76 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 133 | if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) && |
| 134 | // "Pure" variadic functions do not receive @0 suffix. |
| 135 | (!FT->isVarArg() || FT->getNumParams() == 0 || |
| 136 | (FT->getNumParams() == 1 && F->hasStructRetAttr()))) |
Bill Wendling | 70b1400 | 2013-05-29 20:37:19 +0000 | [diff] [blame] | 137 | AddFastCallStdCallSuffix(OutName, F, *TM->getDataLayout()); |
Chris Lattner | 8d99c76 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
Chris Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 140 | } |