blob: dc70259880fece0ad4b0b55679fd4c419ee4969a [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,
108 const Twine &GVName, ManglerPrefixTy PrefixTy) {
109 SmallString<256> TmpData;
Benjamin Kramerb357e062010-01-13 12:45:23 +0000110 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000111 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
112
Bill Wendling99cb6222013-06-18 07:20:20 +0000113 const MCAsmInfo *MAI = Context.getAsmInfo();
Chris Lattner5ef31a02010-03-12 18:44:54 +0000114
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000115 // If the global name is not led with \1, add the appropriate prefixes.
Chris Lattneracd03ae2010-01-17 19:23:46 +0000116 if (Name[0] == '\1') {
117 Name = Name.substr(1);
118 } else {
Chris Lattnerc0dba722010-01-17 18:22:35 +0000119 if (PrefixTy == Mangler::Private) {
Bill Wendling99cb6222013-06-18 07:20:20 +0000120 const char *Prefix = MAI->getPrivateGlobalPrefix();
Chris Lattnerc0dba722010-01-17 18:22:35 +0000121 OutName.append(Prefix, Prefix+strlen(Prefix));
122 } else if (PrefixTy == Mangler::LinkerPrivate) {
Bill Wendling99cb6222013-06-18 07:20:20 +0000123 const char *Prefix = MAI->getLinkerPrivateGlobalPrefix();
Chris Lattnerc0dba722010-01-17 18:22:35 +0000124 OutName.append(Prefix, Prefix+strlen(Prefix));
125 }
126
Bill Wendling99cb6222013-06-18 07:20:20 +0000127 const char *Prefix = MAI->getGlobalPrefix();
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000128 if (Prefix[0] == 0)
129 ; // Common noop, no prefix.
130 else if (Prefix[1] == 0)
131 OutName.push_back(Prefix[0]); // Common, one character prefix.
132 else
Chris Lattnerc0dba722010-01-17 18:22:35 +0000133 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000134 }
135
Chris Lattneracd03ae2010-01-17 19:23:46 +0000136 // If this is a simple string that doesn't need escaping, just append it.
137 if (!NameNeedsEscaping(Name, MAI) ||
138 // If quotes are supported, they can be used unless the string contains
139 // a quote or newline.
Bill Wendling99cb6222013-06-18 07:20:20 +0000140 (MAI->doesAllowQuotesInName() &&
Chris Lattneracd03ae2010-01-17 19:23:46 +0000141 Name.find_first_of("\n\"") == StringRef::npos)) {
142 OutName.append(Name.begin(), Name.end());
143 return;
144 }
145
146 // On systems that do not allow quoted names, we need to mangle most
147 // strange characters.
Bill Wendling99cb6222013-06-18 07:20:20 +0000148 if (!MAI->doesAllowQuotesInName())
Chris Lattner5ef31a02010-03-12 18:44:54 +0000149 return appendMangledName(OutName, Name, MAI);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000150
151 // Okay, the system allows quoted strings. We can quote most anything, the
152 // only characters that need escaping are " and \n.
153 assert(Name.find_first_of("\n\"") != StringRef::npos);
154 return appendMangledQuotedName(OutName, Name);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000155}
156
Chris Lattner8a29fa62010-03-12 21:03:47 +0000157/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
158/// a suffix on their name indicating the number of words of arguments they
159/// take.
160static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
Micah Villmow3574eca2012-10-08 16:38:25 +0000161 const Function *F, const DataLayout &TD) {
Chris Lattner8a29fa62010-03-12 21:03:47 +0000162 // Calculate arguments size total.
163 unsigned ArgWords = 0;
164 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
165 AI != AE; ++AI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000166 Type *Ty = AI->getType();
Chris Lattner8a29fa62010-03-12 21:03:47 +0000167 // 'Dereference' type in case of byval parameter attribute
168 if (AI->hasByValAttr())
169 Ty = cast<PointerType>(Ty)->getElementType();
170 // Size should be aligned to DWORD boundary
171 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
172 }
173
174 raw_svector_ostream(OutName) << '@' << ArgWords;
175}
176
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000177
178/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
179/// and the specified global variable's name. If the global variable doesn't
180/// have a name, this fills in a unique name for the global.
181void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
182 const GlobalValue *GV,
183 bool isImplicitlyPrivate) {
Chris Lattnerff240052010-01-17 18:52:16 +0000184 ManglerPrefixTy PrefixTy = Mangler::Default;
185 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
186 PrefixTy = Mangler::Private;
Bill Wendling32811be2012-08-17 18:33:14 +0000187 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
Chris Lattnerff240052010-01-17 18:52:16 +0000188 PrefixTy = Mangler::LinkerPrivate;
189
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000190 // If this global has a name, handle it simply.
Chris Lattner8a29fa62010-03-12 21:03:47 +0000191 if (GV->hasName()) {
Anton Korobeynikov8caffc12013-04-19 21:20:56 +0000192 StringRef Name = GV->getName();
193 getNameWithPrefix(OutName, Name, PrefixTy);
194 // No need to do anything else if the global has the special "do not mangle"
195 // flag in the name.
196 if (Name[0] == 1)
197 return;
Chris Lattner8a29fa62010-03-12 21:03:47 +0000198 } else {
199 // Get the ID for the global, assigning a new one if we haven't got one
200 // already.
201 unsigned &ID = AnonGlobalIDs[GV];
202 if (ID == 0) ID = NextAnonGlobalID++;
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000203
Chris Lattner8a29fa62010-03-12 21:03:47 +0000204 // Must mangle the global into a unique ID.
205 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
206 }
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000207
Chris Lattner8a29fa62010-03-12 21:03:47 +0000208 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
209 // add it.
Bill Wendling99cb6222013-06-18 07:20:20 +0000210 if (Context.getAsmInfo()->hasMicrosoftFastStdCallMangling()) {
Chris Lattner8a29fa62010-03-12 21:03:47 +0000211 if (const Function *F = dyn_cast<Function>(GV)) {
212 CallingConv::ID CC = F->getCallingConv();
213
214 // fastcall functions need to start with @.
215 // FIXME: This logic seems unlikely to be right.
216 if (CC == CallingConv::X86_FastCall) {
217 if (OutName[0] == '_')
218 OutName[0] = '@';
219 else
220 OutName.insert(OutName.begin(), '@');
221 }
222
223 // fastcall and stdcall functions usually need @42 at the end to specify
224 // the argument info.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000225 FunctionType *FT = F->getFunctionType();
Chris Lattner8a29fa62010-03-12 21:03:47 +0000226 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
227 // "Pure" variadic functions do not receive @0 suffix.
228 (!FT->isVarArg() || FT->getNumParams() == 0 ||
229 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
Bill Wendlingcc5a8822013-05-29 20:37:19 +0000230 AddFastCallStdCallSuffix(OutName, F, *TM->getDataLayout());
Chris Lattner8a29fa62010-03-12 21:03:47 +0000231 }
232 }
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000233}
234
Chris Lattner73ff5642010-03-12 18:55:20 +0000235/// getSymbol - Return the MCSymbol for the specified global value. This
236/// symbol is the main label that is the address of the global.
237MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
238 SmallString<60> NameStr;
239 getNameWithPrefix(NameStr, GV, false);
Chris Lattner9b97a732010-03-30 18:10:53 +0000240 return Context.GetOrCreateSymbol(NameStr.str());
Chris Lattner73ff5642010-03-12 18:55:20 +0000241}
242
243