blob: f86428cff2555b61591d725d0bb38062257c60f0 [file] [log] [blame]
Brian Gaekeb198ca32003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaekeb198ca32003-07-24 20:20:58 +00009//
Chris Lattnerc94c8252010-01-16 21:08:46 +000010// Unified name mangler for assembly backends.
Brian Gaekeb198ca32003-07-24 20:20:58 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner45111d12010-01-16 21:57:06 +000014#include "llvm/Target/Mangler.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Twine.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/Function.h"
Chris Lattnerc0dba722010-01-17 18:22:35 +000020#include "llvm/MC/MCAsmInfo.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000021#include "llvm/MC/MCContext.h"
Bill Wendlingcc5a8822013-05-29 20:37:19 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattner8a29fa62010-03-12 21:03:47 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Sean Hunt3420e7f2012-04-07 00:37:53 +000026static bool isAcceptableChar(char C, bool AllowPeriod, bool AllowUTF8) {
Chris Lattneracd03ae2010-01-17 19:23:46 +000027 if ((C < 'a' || C > 'z') &&
28 (C < 'A' || C > 'Z') &&
29 (C < '0' || C > '9') &&
Mon P Wangb9a01bc2010-04-29 04:00:56 +000030 C != '_' && C != '$' && C != '@' &&
Sean Hunt3420e7f2012-04-07 00:37:53 +000031 !(AllowPeriod && C == '.') &&
32 !(AllowUTF8 && (C & 0x80)))
Chris Lattneracd03ae2010-01-17 19:23:46 +000033 return false;
34 return true;
35}
36
37static char HexDigit(int V) {
38 return V < 10 ? V+'0' : V+'A'-10;
39}
40
41static void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) {
42 OutName.push_back('_');
43 OutName.push_back(HexDigit(C >> 4));
44 OutName.push_back(HexDigit(C & 15));
45 OutName.push_back('_');
46}
47
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +000048/// NameNeedsEscaping - Return true if the identifier \p Str needs quotes
Chris Lattneracd03ae2010-01-17 19:23:46 +000049/// for this assembler.
Bill Wendling99cb6222013-06-18 07:20:20 +000050static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo *MAI) {
Chris Lattneracd03ae2010-01-17 19:23:46 +000051 assert(!Str.empty() && "Cannot create an empty MCSymbol");
52
53 // If the first character is a number and the target does not allow this, we
54 // need quotes.
Bill Wendling99cb6222013-06-18 07:20:20 +000055 if (!MAI->doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
Chris Lattneracd03ae2010-01-17 19:23:46 +000056 return true;
57
58 // If any of the characters in the string is an unacceptable character, force
59 // quotes.
Bill Wendling99cb6222013-06-18 07:20:20 +000060 bool AllowPeriod = MAI->doesAllowPeriodsInName();
61 bool AllowUTF8 = MAI->doesAllowUTF8();
Chris Lattneracd03ae2010-01-17 19:23:46 +000062 for (unsigned i = 0, e = Str.size(); i != e; ++i)
Sean Hunt3420e7f2012-04-07 00:37:53 +000063 if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8))
Chris Lattneracd03ae2010-01-17 19:23:46 +000064 return true;
65 return false;
66}
67
68/// appendMangledName - Add the specified string in mangled form if it uses
69/// any unusual characters.
Chris Lattner0bd58b02010-01-17 19:32:29 +000070static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str,
Bill Wendling99cb6222013-06-18 07:20:20 +000071 const MCAsmInfo *MAI) {
Chris Lattneracd03ae2010-01-17 19:23:46 +000072 // The first character is not allowed to be a number unless the target
73 // explicitly allows it.
Bill Wendling99cb6222013-06-18 07:20:20 +000074 if (!MAI->doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
Chris Lattneracd03ae2010-01-17 19:23:46 +000075 MangleLetter(OutName, Str[0]);
76 Str = Str.substr(1);
77 }
Mon P Wangb9a01bc2010-04-29 04:00:56 +000078
Bill Wendling99cb6222013-06-18 07:20:20 +000079 bool AllowPeriod = MAI->doesAllowPeriodsInName();
80 bool AllowUTF8 = MAI->doesAllowUTF8();
Chris Lattneracd03ae2010-01-17 19:23:46 +000081 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
Sean Hunt3420e7f2012-04-07 00:37:53 +000082 if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8))
Chris Lattneracd03ae2010-01-17 19:23:46 +000083 MangleLetter(OutName, Str[i]);
84 else
85 OutName.push_back(Str[i]);
86 }
87}
88
89
90/// appendMangledQuotedName - On systems that support quoted symbols, we still
91/// have to escape some (obscure) characters like " and \n which would break the
92/// assembler's lexing.
93static void appendMangledQuotedName(SmallVectorImpl<char> &OutName,
94 StringRef Str) {
95 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
96 if (Str[i] == '"' || Str[i] == '\n')
97 MangleLetter(OutName, Str[i]);
98 else
99 OutName.push_back(Str[i]);
100 }
101}
102
103
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000104/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
105/// and the specified name as the global variable name. GVName must not be
106/// empty.
107void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Nico Rieckfdbea512013-07-29 13:58:39 +0000108 const Twine &GVName, ManglerPrefixTy PrefixTy,
109 bool UseGlobalPrefix) {
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000110 SmallString<256> TmpData;
Benjamin Kramerb357e062010-01-13 12:45:23 +0000111 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000112 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
113
Bill Wendling99cb6222013-06-18 07:20:20 +0000114 const MCAsmInfo *MAI = Context.getAsmInfo();
Chris Lattner5ef31a02010-03-12 18:44:54 +0000115
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000116 // If the global name is not led with \1, add the appropriate prefixes.
Chris Lattneracd03ae2010-01-17 19:23:46 +0000117 if (Name[0] == '\1') {
118 Name = Name.substr(1);
119 } else {
Chris Lattnerc0dba722010-01-17 18:22:35 +0000120 if (PrefixTy == Mangler::Private) {
Bill Wendling99cb6222013-06-18 07:20:20 +0000121 const char *Prefix = MAI->getPrivateGlobalPrefix();
Chris Lattnerc0dba722010-01-17 18:22:35 +0000122 OutName.append(Prefix, Prefix+strlen(Prefix));
123 } else if (PrefixTy == Mangler::LinkerPrivate) {
Bill Wendling99cb6222013-06-18 07:20:20 +0000124 const char *Prefix = MAI->getLinkerPrivateGlobalPrefix();
Chris Lattnerc0dba722010-01-17 18:22:35 +0000125 OutName.append(Prefix, Prefix+strlen(Prefix));
126 }
127
Nico Rieckfdbea512013-07-29 13:58:39 +0000128 if (UseGlobalPrefix) {
129 const char *Prefix = MAI->getGlobalPrefix();
130 if (Prefix[0] == 0)
131 ; // Common noop, no prefix.
132 else if (Prefix[1] == 0)
133 OutName.push_back(Prefix[0]); // Common, one character prefix.
134 else
135 // Arbitrary length prefix.
136 OutName.append(Prefix, Prefix+strlen(Prefix));
137 }
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000138 }
139
Chris Lattneracd03ae2010-01-17 19:23:46 +0000140 // If this is a simple string that doesn't need escaping, just append it.
141 if (!NameNeedsEscaping(Name, MAI) ||
142 // If quotes are supported, they can be used unless the string contains
143 // a quote or newline.
Bill Wendling99cb6222013-06-18 07:20:20 +0000144 (MAI->doesAllowQuotesInName() &&
Chris Lattneracd03ae2010-01-17 19:23:46 +0000145 Name.find_first_of("\n\"") == StringRef::npos)) {
146 OutName.append(Name.begin(), Name.end());
147 return;
148 }
149
150 // On systems that do not allow quoted names, we need to mangle most
151 // strange characters.
Bill Wendling99cb6222013-06-18 07:20:20 +0000152 if (!MAI->doesAllowQuotesInName())
Chris Lattner5ef31a02010-03-12 18:44:54 +0000153 return appendMangledName(OutName, Name, MAI);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000154
155 // Okay, the system allows quoted strings. We can quote most anything, the
156 // only characters that need escaping are " and \n.
157 assert(Name.find_first_of("\n\"") != StringRef::npos);
158 return appendMangledQuotedName(OutName, Name);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000159}
160
Chris Lattner8a29fa62010-03-12 21:03:47 +0000161/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
162/// a suffix on their name indicating the number of words of arguments they
163/// take.
164static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
Micah Villmow3574eca2012-10-08 16:38:25 +0000165 const Function *F, const DataLayout &TD) {
Chris Lattner8a29fa62010-03-12 21:03:47 +0000166 // Calculate arguments size total.
167 unsigned ArgWords = 0;
168 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
169 AI != AE; ++AI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000170 Type *Ty = AI->getType();
Chris Lattner8a29fa62010-03-12 21:03:47 +0000171 // 'Dereference' type in case of byval parameter attribute
172 if (AI->hasByValAttr())
173 Ty = cast<PointerType>(Ty)->getElementType();
174 // Size should be aligned to DWORD boundary
175 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
176 }
177
178 raw_svector_ostream(OutName) << '@' << ArgWords;
179}
180
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000181
182/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
183/// and the specified global variable's name. If the global variable doesn't
184/// have a name, this fills in a unique name for the global.
185void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Nico Rieckfdbea512013-07-29 13:58:39 +0000186 const GlobalValue *GV, bool isImplicitlyPrivate,
187 bool UseGlobalPrefix) {
Chris Lattnerff240052010-01-17 18:52:16 +0000188 ManglerPrefixTy PrefixTy = Mangler::Default;
189 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
190 PrefixTy = Mangler::Private;
Bill Wendling32811be2012-08-17 18:33:14 +0000191 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
Chris Lattnerff240052010-01-17 18:52:16 +0000192 PrefixTy = Mangler::LinkerPrivate;
193
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000194 // If this global has a name, handle it simply.
Chris Lattner8a29fa62010-03-12 21:03:47 +0000195 if (GV->hasName()) {
Anton Korobeynikov8caffc12013-04-19 21:20:56 +0000196 StringRef Name = GV->getName();
Nico Rieckfdbea512013-07-29 13:58:39 +0000197 getNameWithPrefix(OutName, Name, PrefixTy, UseGlobalPrefix);
Anton Korobeynikov8caffc12013-04-19 21:20:56 +0000198 // No need to do anything else if the global has the special "do not mangle"
199 // flag in the name.
200 if (Name[0] == 1)
201 return;
Chris Lattner8a29fa62010-03-12 21:03:47 +0000202 } else {
203 // Get the ID for the global, assigning a new one if we haven't got one
204 // already.
205 unsigned &ID = AnonGlobalIDs[GV];
206 if (ID == 0) ID = NextAnonGlobalID++;
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000207
Chris Lattner8a29fa62010-03-12 21:03:47 +0000208 // Must mangle the global into a unique ID.
Nico Rieckfdbea512013-07-29 13:58:39 +0000209 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy,
210 UseGlobalPrefix);
Chris Lattner8a29fa62010-03-12 21:03:47 +0000211 }
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000212
Chris Lattner8a29fa62010-03-12 21:03:47 +0000213 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
214 // add it.
Bill Wendling99cb6222013-06-18 07:20:20 +0000215 if (Context.getAsmInfo()->hasMicrosoftFastStdCallMangling()) {
Chris Lattner8a29fa62010-03-12 21:03:47 +0000216 if (const Function *F = dyn_cast<Function>(GV)) {
217 CallingConv::ID CC = F->getCallingConv();
218
219 // fastcall functions need to start with @.
220 // FIXME: This logic seems unlikely to be right.
221 if (CC == CallingConv::X86_FastCall) {
222 if (OutName[0] == '_')
223 OutName[0] = '@';
224 else
225 OutName.insert(OutName.begin(), '@');
226 }
227
228 // fastcall and stdcall functions usually need @42 at the end to specify
229 // the argument info.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000230 FunctionType *FT = F->getFunctionType();
Chris Lattner8a29fa62010-03-12 21:03:47 +0000231 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
232 // "Pure" variadic functions do not receive @0 suffix.
233 (!FT->isVarArg() || FT->getNumParams() == 0 ||
234 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
Bill Wendlingcc5a8822013-05-29 20:37:19 +0000235 AddFastCallStdCallSuffix(OutName, F, *TM->getDataLayout());
Chris Lattner8a29fa62010-03-12 21:03:47 +0000236 }
237 }
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000238}
239
Chris Lattner73ff5642010-03-12 18:55:20 +0000240/// getSymbol - Return the MCSymbol for the specified global value. This
241/// symbol is the main label that is the address of the global.
242MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
243 SmallString<60> NameStr;
244 getNameWithPrefix(NameStr, GV, false);
Chris Lattner9b97a732010-03-30 18:10:53 +0000245 return Context.GetOrCreateSymbol(NameStr.str());
Chris Lattner73ff5642010-03-12 18:55:20 +0000246}
247
248