blob: 49efe75d79d8f02e51df9aad34c7af69042d3cb0 [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"
Chris Lattner8a29fa62010-03-12 21:03:47 +000015#include "llvm/DerivedTypes.h"
16#include "llvm/Function.h"
17#include "llvm/Target/TargetData.h"
Chris Lattnerc0dba722010-01-17 18:22:35 +000018#include "llvm/MC/MCAsmInfo.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000019#include "llvm/MC/MCContext.h"
Chris Lattner8a29fa62010-03-12 21:03:47 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner36e69ae2010-01-13 05:02:57 +000021#include "llvm/ADT/SmallString.h"
Chris Lattnerc94c8252010-01-16 21:08:46 +000022#include "llvm/ADT/Twine.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Mon P Wangb9a01bc2010-04-29 04:00:56 +000025static bool isAcceptableChar(char C, bool AllowPeriod) {
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 != '@' &&
30 !(AllowPeriod && C == '.'))
Chris Lattneracd03ae2010-01-17 19:23:46 +000031 return false;
32 return true;
33}
34
35static char HexDigit(int V) {
36 return V < 10 ? V+'0' : V+'A'-10;
37}
38
39static void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) {
40 OutName.push_back('_');
41 OutName.push_back(HexDigit(C >> 4));
42 OutName.push_back(HexDigit(C & 15));
43 OutName.push_back('_');
44}
45
46/// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes
47/// for this assembler.
48static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
49 assert(!Str.empty() && "Cannot create an empty MCSymbol");
50
51 // If the first character is a number and the target does not allow this, we
52 // need quotes.
53 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
54 return true;
55
56 // If any of the characters in the string is an unacceptable character, force
57 // quotes.
Mon P Wangb9a01bc2010-04-29 04:00:56 +000058 bool AllowPeriod = MAI.doesAllowPeriodsInName();
Chris Lattneracd03ae2010-01-17 19:23:46 +000059 for (unsigned i = 0, e = Str.size(); i != e; ++i)
Mon P Wangb9a01bc2010-04-29 04:00:56 +000060 if (!isAcceptableChar(Str[i], AllowPeriod))
Chris Lattneracd03ae2010-01-17 19:23:46 +000061 return true;
62 return false;
63}
64
65/// appendMangledName - Add the specified string in mangled form if it uses
66/// any unusual characters.
Chris Lattner0bd58b02010-01-17 19:32:29 +000067static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str,
Chris Lattner5ef31a02010-03-12 18:44:54 +000068 const MCAsmInfo &MAI) {
Chris Lattneracd03ae2010-01-17 19:23:46 +000069 // The first character is not allowed to be a number unless the target
70 // explicitly allows it.
Chris Lattner5ef31a02010-03-12 18:44:54 +000071 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
Chris Lattneracd03ae2010-01-17 19:23:46 +000072 MangleLetter(OutName, Str[0]);
73 Str = Str.substr(1);
74 }
Mon P Wangb9a01bc2010-04-29 04:00:56 +000075
76 bool AllowPeriod = MAI.doesAllowPeriodsInName();
Chris Lattneracd03ae2010-01-17 19:23:46 +000077 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
Mon P Wangb9a01bc2010-04-29 04:00:56 +000078 if (!isAcceptableChar(Str[i], AllowPeriod))
Chris Lattneracd03ae2010-01-17 19:23:46 +000079 MangleLetter(OutName, Str[i]);
80 else
81 OutName.push_back(Str[i]);
82 }
83}
84
85
86/// appendMangledQuotedName - On systems that support quoted symbols, we still
87/// have to escape some (obscure) characters like " and \n which would break the
88/// assembler's lexing.
89static void appendMangledQuotedName(SmallVectorImpl<char> &OutName,
90 StringRef Str) {
91 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
92 if (Str[i] == '"' || Str[i] == '\n')
93 MangleLetter(OutName, Str[i]);
94 else
95 OutName.push_back(Str[i]);
96 }
97}
98
99
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000100/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
101/// and the specified name as the global variable name. GVName must not be
102/// empty.
103void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
104 const Twine &GVName, ManglerPrefixTy PrefixTy) {
105 SmallString<256> TmpData;
Benjamin Kramerb357e062010-01-13 12:45:23 +0000106 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000107 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
108
Chris Lattner5ef31a02010-03-12 18:44:54 +0000109 const MCAsmInfo &MAI = Context.getAsmInfo();
110
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000111 // If the global name is not led with \1, add the appropriate prefixes.
Chris Lattneracd03ae2010-01-17 19:23:46 +0000112 if (Name[0] == '\1') {
113 Name = Name.substr(1);
114 } else {
Chris Lattnerc0dba722010-01-17 18:22:35 +0000115 if (PrefixTy == Mangler::Private) {
116 const char *Prefix = MAI.getPrivateGlobalPrefix();
117 OutName.append(Prefix, Prefix+strlen(Prefix));
118 } else if (PrefixTy == Mangler::LinkerPrivate) {
119 const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
120 OutName.append(Prefix, Prefix+strlen(Prefix));
121 }
122
123 const char *Prefix = MAI.getGlobalPrefix();
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000124 if (Prefix[0] == 0)
125 ; // Common noop, no prefix.
126 else if (Prefix[1] == 0)
127 OutName.push_back(Prefix[0]); // Common, one character prefix.
128 else
Chris Lattnerc0dba722010-01-17 18:22:35 +0000129 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000130 }
131
Chris Lattneracd03ae2010-01-17 19:23:46 +0000132 // If this is a simple string that doesn't need escaping, just append it.
133 if (!NameNeedsEscaping(Name, MAI) ||
134 // If quotes are supported, they can be used unless the string contains
135 // a quote or newline.
136 (MAI.doesAllowQuotesInName() &&
137 Name.find_first_of("\n\"") == StringRef::npos)) {
138 OutName.append(Name.begin(), Name.end());
139 return;
140 }
141
142 // On systems that do not allow quoted names, we need to mangle most
143 // strange characters.
144 if (!MAI.doesAllowQuotesInName())
Chris Lattner5ef31a02010-03-12 18:44:54 +0000145 return appendMangledName(OutName, Name, MAI);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000146
147 // Okay, the system allows quoted strings. We can quote most anything, the
148 // only characters that need escaping are " and \n.
149 assert(Name.find_first_of("\n\"") != StringRef::npos);
150 return appendMangledQuotedName(OutName, Name);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000151}
152
Chris Lattner8a29fa62010-03-12 21:03:47 +0000153/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
154/// a suffix on their name indicating the number of words of arguments they
155/// take.
156static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
157 const Function *F, const TargetData &TD) {
158 // Calculate arguments size total.
159 unsigned ArgWords = 0;
160 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
161 AI != AE; ++AI) {
162 const Type *Ty = AI->getType();
163 // 'Dereference' type in case of byval parameter attribute
164 if (AI->hasByValAttr())
165 Ty = cast<PointerType>(Ty)->getElementType();
166 // Size should be aligned to DWORD boundary
167 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
168 }
169
170 raw_svector_ostream(OutName) << '@' << ArgWords;
171}
172
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000173
174/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
175/// and the specified global variable's name. If the global variable doesn't
176/// have a name, this fills in a unique name for the global.
177void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
178 const GlobalValue *GV,
179 bool isImplicitlyPrivate) {
Chris Lattnerff240052010-01-17 18:52:16 +0000180 ManglerPrefixTy PrefixTy = Mangler::Default;
181 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
182 PrefixTy = Mangler::Private;
Bill Wendling55ae5152010-08-20 22:05:50 +0000183 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage() ||
184 GV->hasLinkerPrivateWeakDefAutoLinkage())
Chris Lattnerff240052010-01-17 18:52:16 +0000185 PrefixTy = Mangler::LinkerPrivate;
186
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000187 // If this global has a name, handle it simply.
Chris Lattner8a29fa62010-03-12 21:03:47 +0000188 if (GV->hasName()) {
189 getNameWithPrefix(OutName, GV->getName(), PrefixTy);
190 } else {
191 // Get the ID for the global, assigning a new one if we haven't got one
192 // already.
193 unsigned &ID = AnonGlobalIDs[GV];
194 if (ID == 0) ID = NextAnonGlobalID++;
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000195
Chris Lattner8a29fa62010-03-12 21:03:47 +0000196 // Must mangle the global into a unique ID.
197 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
198 }
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000199
Chris Lattner8a29fa62010-03-12 21:03:47 +0000200 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
201 // add it.
202 if (Context.getAsmInfo().hasMicrosoftFastStdCallMangling()) {
203 if (const Function *F = dyn_cast<Function>(GV)) {
204 CallingConv::ID CC = F->getCallingConv();
205
206 // fastcall functions need to start with @.
207 // FIXME: This logic seems unlikely to be right.
208 if (CC == CallingConv::X86_FastCall) {
209 if (OutName[0] == '_')
210 OutName[0] = '@';
211 else
212 OutName.insert(OutName.begin(), '@');
213 }
214
215 // fastcall and stdcall functions usually need @42 at the end to specify
216 // the argument info.
217 const FunctionType *FT = F->getFunctionType();
218 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
219 // "Pure" variadic functions do not receive @0 suffix.
220 (!FT->isVarArg() || FT->getNumParams() == 0 ||
221 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
222 AddFastCallStdCallSuffix(OutName, F, TD);
223 }
224 }
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000225}
226
Chris Lattner61f160a2010-01-16 18:06:34 +0000227/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
228/// and the specified global variable's name. If the global variable doesn't
229/// have a name, this fills in a unique name for the global.
230std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
231 bool isImplicitlyPrivate) {
232 SmallString<64> Buf;
233 getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
234 return std::string(Buf.begin(), Buf.end());
235}
Chris Lattner73ff5642010-03-12 18:55:20 +0000236
237/// getSymbol - Return the MCSymbol for the specified global value. This
238/// symbol is the main label that is the address of the global.
239MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
240 SmallString<60> NameStr;
241 getNameWithPrefix(NameStr, GV, false);
Chris Lattner9b97a732010-03-30 18:10:53 +0000242 return Context.GetOrCreateSymbol(NameStr.str());
Chris Lattner73ff5642010-03-12 18:55:20 +0000243}
244
245