blob: fb937060749925857f77eccf9f7ff5d789c6eea1 [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
Chris Lattneracd03ae2010-01-17 19:23:46 +000025static bool isAcceptableChar(char C) {
26 if ((C < 'a' || C > 'z') &&
27 (C < 'A' || C > 'Z') &&
28 (C < '0' || C > '9') &&
29 C != '_' && C != '$' && C != '.' && C != '@')
30 return false;
31 return true;
32}
33
34static char HexDigit(int V) {
35 return V < 10 ? V+'0' : V+'A'-10;
36}
37
38static void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) {
39 OutName.push_back('_');
40 OutName.push_back(HexDigit(C >> 4));
41 OutName.push_back(HexDigit(C & 15));
42 OutName.push_back('_');
43}
44
45/// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes
46/// for this assembler.
47static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
48 assert(!Str.empty() && "Cannot create an empty MCSymbol");
49
50 // If the first character is a number and the target does not allow this, we
51 // need quotes.
52 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
53 return true;
54
55 // If any of the characters in the string is an unacceptable character, force
56 // quotes.
57 for (unsigned i = 0, e = Str.size(); i != e; ++i)
58 if (!isAcceptableChar(Str[i]))
59 return true;
60 return false;
61}
62
63/// appendMangledName - Add the specified string in mangled form if it uses
64/// any unusual characters.
Chris Lattner0bd58b02010-01-17 19:32:29 +000065static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str,
Chris Lattner5ef31a02010-03-12 18:44:54 +000066 const MCAsmInfo &MAI) {
Chris Lattneracd03ae2010-01-17 19:23:46 +000067 // The first character is not allowed to be a number unless the target
68 // explicitly allows it.
Chris Lattner5ef31a02010-03-12 18:44:54 +000069 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
Chris Lattneracd03ae2010-01-17 19:23:46 +000070 MangleLetter(OutName, Str[0]);
71 Str = Str.substr(1);
72 }
73
74 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
75 if (!isAcceptableChar(Str[i]))
76 MangleLetter(OutName, Str[i]);
77 else
78 OutName.push_back(Str[i]);
79 }
80}
81
82
83/// appendMangledQuotedName - On systems that support quoted symbols, we still
84/// have to escape some (obscure) characters like " and \n which would break the
85/// assembler's lexing.
86static void appendMangledQuotedName(SmallVectorImpl<char> &OutName,
87 StringRef Str) {
88 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
89 if (Str[i] == '"' || Str[i] == '\n')
90 MangleLetter(OutName, Str[i]);
91 else
92 OutName.push_back(Str[i]);
93 }
94}
95
96
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000097/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
98/// and the specified name as the global variable name. GVName must not be
99/// empty.
100void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
101 const Twine &GVName, ManglerPrefixTy PrefixTy) {
102 SmallString<256> TmpData;
Benjamin Kramerb357e062010-01-13 12:45:23 +0000103 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000104 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
105
Chris Lattner5ef31a02010-03-12 18:44:54 +0000106 const MCAsmInfo &MAI = Context.getAsmInfo();
107
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000108 // If the global name is not led with \1, add the appropriate prefixes.
Chris Lattneracd03ae2010-01-17 19:23:46 +0000109 if (Name[0] == '\1') {
110 Name = Name.substr(1);
111 } else {
Chris Lattnerc0dba722010-01-17 18:22:35 +0000112 if (PrefixTy == Mangler::Private) {
113 const char *Prefix = MAI.getPrivateGlobalPrefix();
114 OutName.append(Prefix, Prefix+strlen(Prefix));
115 } else if (PrefixTy == Mangler::LinkerPrivate) {
116 const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
117 OutName.append(Prefix, Prefix+strlen(Prefix));
118 }
119
120 const char *Prefix = MAI.getGlobalPrefix();
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000121 if (Prefix[0] == 0)
122 ; // Common noop, no prefix.
123 else if (Prefix[1] == 0)
124 OutName.push_back(Prefix[0]); // Common, one character prefix.
125 else
Chris Lattnerc0dba722010-01-17 18:22:35 +0000126 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000127 }
128
Chris Lattneracd03ae2010-01-17 19:23:46 +0000129 // If this is a simple string that doesn't need escaping, just append it.
130 if (!NameNeedsEscaping(Name, MAI) ||
131 // If quotes are supported, they can be used unless the string contains
132 // a quote or newline.
133 (MAI.doesAllowQuotesInName() &&
134 Name.find_first_of("\n\"") == StringRef::npos)) {
135 OutName.append(Name.begin(), Name.end());
136 return;
137 }
138
139 // On systems that do not allow quoted names, we need to mangle most
140 // strange characters.
141 if (!MAI.doesAllowQuotesInName())
Chris Lattner5ef31a02010-03-12 18:44:54 +0000142 return appendMangledName(OutName, Name, MAI);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000143
144 // Okay, the system allows quoted strings. We can quote most anything, the
145 // only characters that need escaping are " and \n.
146 assert(Name.find_first_of("\n\"") != StringRef::npos);
147 return appendMangledQuotedName(OutName, Name);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000148}
149
Chris Lattner8a29fa62010-03-12 21:03:47 +0000150/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
151/// a suffix on their name indicating the number of words of arguments they
152/// take.
153static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
154 const Function *F, const TargetData &TD) {
155 // Calculate arguments size total.
156 unsigned ArgWords = 0;
157 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
158 AI != AE; ++AI) {
159 const Type *Ty = AI->getType();
160 // 'Dereference' type in case of byval parameter attribute
161 if (AI->hasByValAttr())
162 Ty = cast<PointerType>(Ty)->getElementType();
163 // Size should be aligned to DWORD boundary
164 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
165 }
166
167 raw_svector_ostream(OutName) << '@' << ArgWords;
168}
169
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000170
171/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
172/// and the specified global variable's name. If the global variable doesn't
173/// have a name, this fills in a unique name for the global.
174void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
175 const GlobalValue *GV,
176 bool isImplicitlyPrivate) {
Chris Lattnerff240052010-01-17 18:52:16 +0000177 ManglerPrefixTy PrefixTy = Mangler::Default;
178 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
179 PrefixTy = Mangler::Private;
180 else if (GV->hasLinkerPrivateLinkage())
181 PrefixTy = Mangler::LinkerPrivate;
182
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000183 // If this global has a name, handle it simply.
Chris Lattner8a29fa62010-03-12 21:03:47 +0000184 if (GV->hasName()) {
185 getNameWithPrefix(OutName, GV->getName(), PrefixTy);
186 } else {
187 // Get the ID for the global, assigning a new one if we haven't got one
188 // already.
189 unsigned &ID = AnonGlobalIDs[GV];
190 if (ID == 0) ID = NextAnonGlobalID++;
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000191
Chris Lattner8a29fa62010-03-12 21:03:47 +0000192 // Must mangle the global into a unique ID.
193 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
194 }
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000195
Chris Lattner8a29fa62010-03-12 21:03:47 +0000196 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
197 // add it.
198 if (Context.getAsmInfo().hasMicrosoftFastStdCallMangling()) {
199 if (const Function *F = dyn_cast<Function>(GV)) {
200 CallingConv::ID CC = F->getCallingConv();
201
202 // fastcall functions need to start with @.
203 // FIXME: This logic seems unlikely to be right.
204 if (CC == CallingConv::X86_FastCall) {
205 if (OutName[0] == '_')
206 OutName[0] = '@';
207 else
208 OutName.insert(OutName.begin(), '@');
209 }
210
211 // fastcall and stdcall functions usually need @42 at the end to specify
212 // the argument info.
213 const FunctionType *FT = F->getFunctionType();
214 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
215 // "Pure" variadic functions do not receive @0 suffix.
216 (!FT->isVarArg() || FT->getNumParams() == 0 ||
217 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
218 AddFastCallStdCallSuffix(OutName, F, TD);
219 }
220 }
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000221}
222
Chris Lattner61f160a2010-01-16 18:06:34 +0000223/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
224/// and the specified global variable's name. If the global variable doesn't
225/// have a name, this fills in a unique name for the global.
226std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
227 bool isImplicitlyPrivate) {
228 SmallString<64> Buf;
229 getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
230 return std::string(Buf.begin(), Buf.end());
231}
Chris Lattner73ff5642010-03-12 18:55:20 +0000232
233/// getSymbol - Return the MCSymbol for the specified global value. This
234/// symbol is the main label that is the address of the global.
235MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
236 SmallString<60> NameStr;
237 getNameWithPrefix(NameStr, GV, false);
238 if (!GV->hasPrivateLinkage())
239 return Context.GetOrCreateSymbol(NameStr.str());
240
241 return Context.GetOrCreateTemporarySymbol(NameStr.str());
242}
243
244