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