blob: fc2f78d5b620137a6aac5ae089795c8f2c46cc44 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner52145182010-01-16 21:08:46 +000010// Unified name mangler for assembly backends.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner31a54742010-01-16 21:57:06 +000014#include "llvm/Target/Mangler.h"
Chris Lattner0130c1d2010-03-12 21:03:47 +000015#include "llvm/DerivedTypes.h"
16#include "llvm/Function.h"
17#include "llvm/Target/TargetData.h"
Chris Lattner63ab9bb2010-01-17 18:22:35 +000018#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4aeebec2010-03-12 18:44:54 +000019#include "llvm/MC/MCContext.h"
Chris Lattner0130c1d2010-03-12 21:03:47 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner83411fe2010-01-13 05:02:57 +000021#include "llvm/ADT/SmallString.h"
Chris Lattner52145182010-01-16 21:08:46 +000022#include "llvm/ADT/Twine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023using namespace llvm;
24
Mon P Wang84cd1e72010-04-29 04:00:56 +000025static bool isAcceptableChar(char C, bool AllowPeriod) {
Chris Lattnere295e552010-01-17 19:23:46 +000026 if ((C < 'a' || C > 'z') &&
27 (C < 'A' || C > 'Z') &&
28 (C < '0' || C > '9') &&
Mon P Wang84cd1e72010-04-29 04:00:56 +000029 C != '_' && C != '$' && C != '@' &&
30 !(AllowPeriod && C == '.'))
Chris Lattnere295e552010-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 Wang84cd1e72010-04-29 04:00:56 +000058 bool AllowPeriod = MAI.doesAllowPeriodsInName();
Chris Lattnere295e552010-01-17 19:23:46 +000059 for (unsigned i = 0, e = Str.size(); i != e; ++i)
Mon P Wang84cd1e72010-04-29 04:00:56 +000060 if (!isAcceptableChar(Str[i], AllowPeriod))
Chris Lattnere295e552010-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 Lattner306ee862010-01-17 19:32:29 +000067static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str,
Chris Lattner4aeebec2010-03-12 18:44:54 +000068 const MCAsmInfo &MAI) {
Chris Lattnere295e552010-01-17 19:23:46 +000069 // The first character is not allowed to be a number unless the target
70 // explicitly allows it.
Chris Lattner4aeebec2010-03-12 18:44:54 +000071 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
Chris Lattnere295e552010-01-17 19:23:46 +000072 MangleLetter(OutName, Str[0]);
73 Str = Str.substr(1);
74 }
Mon P Wang84cd1e72010-04-29 04:00:56 +000075
76 bool AllowPeriod = MAI.doesAllowPeriodsInName();
Chris Lattnere295e552010-01-17 19:23:46 +000077 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
Mon P Wang84cd1e72010-04-29 04:00:56 +000078 if (!isAcceptableChar(Str[i], AllowPeriod))
Chris Lattnere295e552010-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 Lattnercc3404e2010-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 Kramere05acd12010-01-13 12:45:23 +0000106 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattnercc3404e2010-01-13 07:01:09 +0000107 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
108
Chris Lattner4aeebec2010-03-12 18:44:54 +0000109 const MCAsmInfo &MAI = Context.getAsmInfo();
110
Chris Lattnercc3404e2010-01-13 07:01:09 +0000111 // If the global name is not led with \1, add the appropriate prefixes.
Chris Lattnere295e552010-01-17 19:23:46 +0000112 if (Name[0] == '\1') {
113 Name = Name.substr(1);
114 } else {
Chris Lattner63ab9bb2010-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));
Bill Wendlingf8b61372010-06-29 21:24:00 +0000121 } else if (PrefixTy == Mangler::LinkerWeak) {
122 const char *Prefix = MAI.getLinkerWeakGlobalPrefix();
123 OutName.append(Prefix, Prefix+strlen(Prefix));
Chris Lattner63ab9bb2010-01-17 18:22:35 +0000124 }
125
126 const char *Prefix = MAI.getGlobalPrefix();
Chris Lattnercc3404e2010-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 Lattner63ab9bb2010-01-17 18:22:35 +0000132 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
Chris Lattnercc3404e2010-01-13 07:01:09 +0000133 }
134
Chris Lattnere295e552010-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 Lattner4aeebec2010-03-12 18:44:54 +0000148 return appendMangledName(OutName, Name, MAI);
Chris Lattnere295e552010-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 Lattnercc3404e2010-01-13 07:01:09 +0000154}
155
Chris Lattner0130c1d2010-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,
160 const Function *F, const TargetData &TD) {
161 // 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) {
165 const Type *Ty = AI->getType();
166 // '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 Lattnerbd7e1102009-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 Lattner4f6f7842010-01-17 18:52:16 +0000183 ManglerPrefixTy PrefixTy = Mangler::Default;
184 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
185 PrefixTy = Mangler::Private;
186 else if (GV->hasLinkerPrivateLinkage())
187 PrefixTy = Mangler::LinkerPrivate;
Bill Wendlingf8b61372010-06-29 21:24:00 +0000188 else if (GV->hasLinkerWeakLinkage())
189 PrefixTy = Mangler::LinkerWeak;
Chris Lattner4f6f7842010-01-17 18:52:16 +0000190
Chris Lattnercc3404e2010-01-13 07:01:09 +0000191 // If this global has a name, handle it simply.
Chris Lattner0130c1d2010-03-12 21:03:47 +0000192 if (GV->hasName()) {
193 getNameWithPrefix(OutName, GV->getName(), PrefixTy);
194 } else {
195 // Get the ID for the global, assigning a new one if we haven't got one
196 // already.
197 unsigned &ID = AnonGlobalIDs[GV];
198 if (ID == 0) ID = NextAnonGlobalID++;
Chris Lattnercc3404e2010-01-13 07:01:09 +0000199
Chris Lattner0130c1d2010-03-12 21:03:47 +0000200 // Must mangle the global into a unique ID.
201 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
202 }
Chris Lattnerbd7e1102009-09-11 05:40:42 +0000203
Chris Lattner0130c1d2010-03-12 21:03:47 +0000204 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
205 // add it.
206 if (Context.getAsmInfo().hasMicrosoftFastStdCallMangling()) {
207 if (const Function *F = dyn_cast<Function>(GV)) {
208 CallingConv::ID CC = F->getCallingConv();
209
210 // fastcall functions need to start with @.
211 // FIXME: This logic seems unlikely to be right.
212 if (CC == CallingConv::X86_FastCall) {
213 if (OutName[0] == '_')
214 OutName[0] = '@';
215 else
216 OutName.insert(OutName.begin(), '@');
217 }
218
219 // fastcall and stdcall functions usually need @42 at the end to specify
220 // the argument info.
221 const FunctionType *FT = F->getFunctionType();
222 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
223 // "Pure" variadic functions do not receive @0 suffix.
224 (!FT->isVarArg() || FT->getNumParams() == 0 ||
225 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
226 AddFastCallStdCallSuffix(OutName, F, TD);
227 }
228 }
Chris Lattnerbd7e1102009-09-11 05:40:42 +0000229}
230
Chris Lattnerbfd97272010-01-16 18:06:34 +0000231/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
232/// and the specified global variable's name. If the global variable doesn't
233/// have a name, this fills in a unique name for the global.
234std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
235 bool isImplicitlyPrivate) {
236 SmallString<64> Buf;
237 getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
238 return std::string(Buf.begin(), Buf.end());
239}
Chris Lattner81199d52010-03-12 18:55:20 +0000240
241/// getSymbol - Return the MCSymbol for the specified global value. This
242/// symbol is the main label that is the address of the global.
243MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
244 SmallString<60> NameStr;
245 getNameWithPrefix(NameStr, GV, false);
Chris Lattner3b197832010-03-30 18:10:53 +0000246 return Context.GetOrCreateSymbol(NameStr.str());
Chris Lattner81199d52010-03-12 18:55:20 +0000247}
248
249