1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Unified name mangler for assembly backends.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/Mangler.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/Support/raw_ostream.h"
25 static bool isAcceptableChar(char C, bool AllowPeriod, bool AllowUTF8) {
26 if ((C < 'a' || C > 'z') &&
27 (C < 'A' || C > 'Z') &&
28 (C < '0' || C > '9') &&
29 C != '_' && C != '$' && C != '@' &&
30 !(AllowPeriod && C == '.') &&
31 !(AllowUTF8 && (C & 0x80)))
36 static char HexDigit(int V) {
37 return V < 10 ? V+'0' : V+'A'-10;
40 static 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('_');
47 /// NameNeedsEscaping - Return true if the identifier \p Str needs quotes
48 /// for this assembler.
49 static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
50 assert(!Str.empty() && "Cannot create an empty MCSymbol");
52 // If the first character is a number and the target does not allow this, we
54 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
57 // If any of the characters in the string is an unacceptable character, force
59 bool AllowPeriod = MAI.doesAllowPeriodsInName();
60 bool AllowUTF8 = MAI.doesAllowUTF8();
61 for (unsigned i = 0, e = Str.size(); i != e; ++i)
62 if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8))
67 /// appendMangledName - Add the specified string in mangled form if it uses
68 /// any unusual characters.
69 static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str,
70 const MCAsmInfo &MAI) {
71 // The first character is not allowed to be a number unless the target
72 // explicitly allows it.
73 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
74 MangleLetter(OutName, Str[0]);
78 bool AllowPeriod = MAI.doesAllowPeriodsInName();
79 bool AllowUTF8 = MAI.doesAllowUTF8();
80 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
81 if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8))
82 MangleLetter(OutName, Str[i]);
84 OutName.push_back(Str[i]);
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.
92 static void appendMangledQuotedName(SmallVectorImpl<char> &OutName,
94 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
95 if (Str[i] == '"' || Str[i] == '\n')
96 MangleLetter(OutName, Str[i]);
98 OutName.push_back(Str[i]);
103 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
104 /// and the specified name as the global variable name. GVName must not be
106 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
107 const Twine &GVName, ManglerPrefixTy PrefixTy) {
108 SmallString<256> TmpData;
109 StringRef Name = GVName.toStringRef(TmpData);
110 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
112 const MCAsmInfo &MAI = Context.getAsmInfo();
114 // If the global name is not led with \1, add the appropriate prefixes.
115 if (Name[0] == '\1') {
116 Name = Name.substr(1);
118 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));
126 const char *Prefix = MAI.getGlobalPrefix();
128 ; // Common noop, no prefix.
129 else if (Prefix[1] == 0)
130 OutName.push_back(Prefix[0]); // Common, one character prefix.
132 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
135 // 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());
145 // On systems that do not allow quoted names, we need to mangle most
146 // strange characters.
147 if (!MAI.doesAllowQuotesInName())
148 return appendMangledName(OutName, Name, MAI);
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);
156 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
157 /// a suffix on their name indicating the number of words of arguments they
159 static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
160 const Function *F, const DataLayout &TD) {
161 // Calculate arguments size total.
162 unsigned ArgWords = 0;
163 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
165 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;
173 raw_svector_ostream(OutName) << '@' << ArgWords;
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.
180 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
181 const GlobalValue *GV,
182 bool isImplicitlyPrivate) {
183 ManglerPrefixTy PrefixTy = Mangler::Default;
184 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
185 PrefixTy = Mangler::Private;
186 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
187 PrefixTy = Mangler::LinkerPrivate;
189 // If this global has a name, handle it simply.
191 getNameWithPrefix(OutName, GV->getName(), PrefixTy);
193 // Get the ID for the global, assigning a new one if we haven't got one
195 unsigned &ID = AnonGlobalIDs[GV];
196 if (ID == 0) ID = NextAnonGlobalID++;
198 // Must mangle the global into a unique ID.
199 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
202 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
204 if (Context.getAsmInfo().hasMicrosoftFastStdCallMangling()) {
205 if (const Function *F = dyn_cast<Function>(GV)) {
206 CallingConv::ID CC = F->getCallingConv();
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] == '_')
214 OutName.insert(OutName.begin(), '@');
217 // fastcall and stdcall functions usually need @42 at the end to specify
218 // the argument info.
219 FunctionType *FT = F->getFunctionType();
220 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);
229 /// getSymbol - Return the MCSymbol for the specified global value. This
230 /// symbol is the main label that is the address of the global.
231 MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
232 SmallString<60> NameStr;
233 getNameWithPrefix(NameStr, GV, false);
234 return Context.GetOrCreateSymbol(NameStr.str());