blob: c3e83725d64e18d92aaeedec9358f23f4529d1dd [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"
17#include "llvm/DataLayout.h"
Chris Lattner8a29fa62010-03-12 21:03:47 +000018#include "llvm/DerivedTypes.h"
19#include "llvm/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"
Chris Lattner8a29fa62010-03-12 21:03:47 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Sean Hunt3420e7f2012-04-07 00:37:53 +000025static bool isAcceptableChar(char C, bool AllowPeriod, bool AllowUTF8) {
Chris Lattneracd03ae2010-01-17 19:23:46 +000026 if ((C < 'a' || C > 'z') &&
27 (C < 'A' || C > 'Z') &&
28 (C < '0' || C > '9') &&
Mon P Wangb9a01bc2010-04-29 04:00:56 +000029 C != '_' && C != '$' && C != '@' &&
Sean Hunt3420e7f2012-04-07 00:37:53 +000030 !(AllowPeriod && C == '.') &&
31 !(AllowUTF8 && (C & 0x80)))
Chris Lattneracd03ae2010-01-17 19:23:46 +000032 return false;
33 return true;
34}
35
36static char HexDigit(int V) {
37 return V < 10 ? V+'0' : V+'A'-10;
38}
39
40static void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) {
41 OutName.push_back('_');
42 OutName.push_back(HexDigit(C >> 4));
43 OutName.push_back(HexDigit(C & 15));
44 OutName.push_back('_');
45}
46
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +000047/// NameNeedsEscaping - Return true if the identifier \p Str needs quotes
Chris Lattneracd03ae2010-01-17 19:23:46 +000048/// for this assembler.
49static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
50 assert(!Str.empty() && "Cannot create an empty MCSymbol");
51
52 // If the first character is a number and the target does not allow this, we
53 // need quotes.
54 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
55 return true;
56
57 // If any of the characters in the string is an unacceptable character, force
58 // quotes.
Mon P Wangb9a01bc2010-04-29 04:00:56 +000059 bool AllowPeriod = MAI.doesAllowPeriodsInName();
Sean Hunt3420e7f2012-04-07 00:37:53 +000060 bool AllowUTF8 = MAI.doesAllowUTF8();
Chris Lattneracd03ae2010-01-17 19:23:46 +000061 for (unsigned i = 0, e = Str.size(); i != e; ++i)
Sean Hunt3420e7f2012-04-07 00:37:53 +000062 if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8))
Chris Lattneracd03ae2010-01-17 19:23:46 +000063 return true;
64 return false;
65}
66
67/// appendMangledName - Add the specified string in mangled form if it uses
68/// any unusual characters.
Chris Lattner0bd58b02010-01-17 19:32:29 +000069static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str,
Chris Lattner5ef31a02010-03-12 18:44:54 +000070 const MCAsmInfo &MAI) {
Chris Lattneracd03ae2010-01-17 19:23:46 +000071 // The first character is not allowed to be a number unless the target
72 // explicitly allows it.
Chris Lattner5ef31a02010-03-12 18:44:54 +000073 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
Chris Lattneracd03ae2010-01-17 19:23:46 +000074 MangleLetter(OutName, Str[0]);
75 Str = Str.substr(1);
76 }
Mon P Wangb9a01bc2010-04-29 04:00:56 +000077
78 bool AllowPeriod = MAI.doesAllowPeriodsInName();
Sean Hunt3420e7f2012-04-07 00:37:53 +000079 bool AllowUTF8 = MAI.doesAllowUTF8();
Chris Lattneracd03ae2010-01-17 19:23:46 +000080 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
Sean Hunt3420e7f2012-04-07 00:37:53 +000081 if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8))
Chris Lattneracd03ae2010-01-17 19:23:46 +000082 MangleLetter(OutName, Str[i]);
83 else
84 OutName.push_back(Str[i]);
85 }
86}
87
88
89/// appendMangledQuotedName - On systems that support quoted symbols, we still
90/// have to escape some (obscure) characters like " and \n which would break the
91/// assembler's lexing.
92static void appendMangledQuotedName(SmallVectorImpl<char> &OutName,
93 StringRef Str) {
94 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
95 if (Str[i] == '"' || Str[i] == '\n')
96 MangleLetter(OutName, Str[i]);
97 else
98 OutName.push_back(Str[i]);
99 }
100}
101
102
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000103/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
104/// and the specified name as the global variable name. GVName must not be
105/// empty.
106void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
107 const Twine &GVName, ManglerPrefixTy PrefixTy) {
108 SmallString<256> TmpData;
Benjamin Kramerb357e062010-01-13 12:45:23 +0000109 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000110 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
111
Chris Lattner5ef31a02010-03-12 18:44:54 +0000112 const MCAsmInfo &MAI = Context.getAsmInfo();
113
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000114 // If the global name is not led with \1, add the appropriate prefixes.
Chris Lattneracd03ae2010-01-17 19:23:46 +0000115 if (Name[0] == '\1') {
116 Name = Name.substr(1);
117 } else {
Chris Lattnerc0dba722010-01-17 18:22:35 +0000118 if (PrefixTy == Mangler::Private) {
119 const char *Prefix = MAI.getPrivateGlobalPrefix();
120 OutName.append(Prefix, Prefix+strlen(Prefix));
121 } else if (PrefixTy == Mangler::LinkerPrivate) {
122 const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
123 OutName.append(Prefix, Prefix+strlen(Prefix));
124 }
125
126 const char *Prefix = MAI.getGlobalPrefix();
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000127 if (Prefix[0] == 0)
128 ; // Common noop, no prefix.
129 else if (Prefix[1] == 0)
130 OutName.push_back(Prefix[0]); // Common, one character prefix.
131 else
Chris Lattnerc0dba722010-01-17 18:22:35 +0000132 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000133 }
134
Chris Lattneracd03ae2010-01-17 19:23:46 +0000135 // If this is a simple string that doesn't need escaping, just append it.
136 if (!NameNeedsEscaping(Name, MAI) ||
137 // If quotes are supported, they can be used unless the string contains
138 // a quote or newline.
139 (MAI.doesAllowQuotesInName() &&
140 Name.find_first_of("\n\"") == StringRef::npos)) {
141 OutName.append(Name.begin(), Name.end());
142 return;
143 }
144
145 // On systems that do not allow quoted names, we need to mangle most
146 // strange characters.
147 if (!MAI.doesAllowQuotesInName())
Chris Lattner5ef31a02010-03-12 18:44:54 +0000148 return appendMangledName(OutName, Name, MAI);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000149
150 // Okay, the system allows quoted strings. We can quote most anything, the
151 // only characters that need escaping are " and \n.
152 assert(Name.find_first_of("\n\"") != StringRef::npos);
153 return appendMangledQuotedName(OutName, Name);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000154}
155
Chris Lattner8a29fa62010-03-12 21:03:47 +0000156/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
157/// a suffix on their name indicating the number of words of arguments they
158/// take.
159static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
Micah Villmow3574eca2012-10-08 16:38:25 +0000160 const Function *F, const DataLayout &TD) {
Chris Lattner8a29fa62010-03-12 21:03:47 +0000161 // Calculate arguments size total.
162 unsigned ArgWords = 0;
163 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
164 AI != AE; ++AI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000165 Type *Ty = AI->getType();
Chris Lattner8a29fa62010-03-12 21:03:47 +0000166 // 'Dereference' type in case of byval parameter attribute
167 if (AI->hasByValAttr())
168 Ty = cast<PointerType>(Ty)->getElementType();
169 // Size should be aligned to DWORD boundary
170 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
171 }
172
173 raw_svector_ostream(OutName) << '@' << ArgWords;
174}
175
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000176
177/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
178/// and the specified global variable's name. If the global variable doesn't
179/// have a name, this fills in a unique name for the global.
180void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
181 const GlobalValue *GV,
182 bool isImplicitlyPrivate) {
Chris Lattnerff240052010-01-17 18:52:16 +0000183 ManglerPrefixTy PrefixTy = Mangler::Default;
184 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
185 PrefixTy = Mangler::Private;
Bill Wendling32811be2012-08-17 18:33:14 +0000186 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
Chris Lattnerff240052010-01-17 18:52:16 +0000187 PrefixTy = Mangler::LinkerPrivate;
188
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000189 // If this global has a name, handle it simply.
Chris Lattner8a29fa62010-03-12 21:03:47 +0000190 if (GV->hasName()) {
191 getNameWithPrefix(OutName, GV->getName(), PrefixTy);
192 } else {
193 // Get the ID for the global, assigning a new one if we haven't got one
194 // already.
195 unsigned &ID = AnonGlobalIDs[GV];
196 if (ID == 0) ID = NextAnonGlobalID++;
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000197
Chris Lattner8a29fa62010-03-12 21:03:47 +0000198 // Must mangle the global into a unique ID.
199 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
200 }
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000201
Chris Lattner8a29fa62010-03-12 21:03:47 +0000202 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
203 // add it.
204 if (Context.getAsmInfo().hasMicrosoftFastStdCallMangling()) {
205 if (const Function *F = dyn_cast<Function>(GV)) {
206 CallingConv::ID CC = F->getCallingConv();
207
208 // fastcall functions need to start with @.
209 // FIXME: This logic seems unlikely to be right.
210 if (CC == CallingConv::X86_FastCall) {
211 if (OutName[0] == '_')
212 OutName[0] = '@';
213 else
214 OutName.insert(OutName.begin(), '@');
215 }
216
217 // fastcall and stdcall functions usually need @42 at the end to specify
218 // the argument info.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000219 FunctionType *FT = F->getFunctionType();
Chris Lattner8a29fa62010-03-12 21:03:47 +0000220 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
221 // "Pure" variadic functions do not receive @0 suffix.
222 (!FT->isVarArg() || FT->getNumParams() == 0 ||
223 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
224 AddFastCallStdCallSuffix(OutName, F, TD);
225 }
226 }
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000227}
228
Chris Lattner73ff5642010-03-12 18:55:20 +0000229/// getSymbol - Return the MCSymbol for the specified global value. This
230/// symbol is the main label that is the address of the global.
231MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
232 SmallString<60> NameStr;
233 getNameWithPrefix(NameStr, GV, false);
Chris Lattner9b97a732010-03-30 18:10:53 +0000234 return Context.GetOrCreateSymbol(NameStr.str());
Chris Lattner73ff5642010-03-12 18:55:20 +0000235}
236
237