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/DerivedTypes.h"
16 #include "llvm/Function.h"
17 #include "llvm/Target/TargetData.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/Twine.h"
25 static 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 != '@')
34 static char HexDigit(int V) {
35 return V < 10 ? V+'0' : V+'A'-10;
38 static 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('_');
45 /// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes
46 /// for this assembler.
47 static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
48 assert(!Str.empty() && "Cannot create an empty MCSymbol");
50 // If the first character is a number and the target does not allow this, we
52 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
55 // If any of the characters in the string is an unacceptable character, force
57 for (unsigned i = 0, e = Str.size(); i != e; ++i)
58 if (!isAcceptableChar(Str[i]))
63 /// appendMangledName - Add the specified string in mangled form if it uses
64 /// any unusual characters.
65 static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str,
66 const MCAsmInfo &MAI) {
67 // The first character is not allowed to be a number unless the target
68 // explicitly allows it.
69 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
70 MangleLetter(OutName, Str[0]);
74 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
75 if (!isAcceptableChar(Str[i]))
76 MangleLetter(OutName, Str[i]);
78 OutName.push_back(Str[i]);
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.
86 static void appendMangledQuotedName(SmallVectorImpl<char> &OutName,
88 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
89 if (Str[i] == '"' || Str[i] == '\n')
90 MangleLetter(OutName, Str[i]);
92 OutName.push_back(Str[i]);
97 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
98 /// and the specified name as the global variable name. GVName must not be
100 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
101 const Twine &GVName, ManglerPrefixTy PrefixTy) {
102 SmallString<256> TmpData;
103 StringRef Name = GVName.toStringRef(TmpData);
104 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
106 const MCAsmInfo &MAI = Context.getAsmInfo();
108 // If the global name is not led with \1, add the appropriate prefixes.
109 if (Name[0] == '\1') {
110 Name = Name.substr(1);
112 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));
120 const char *Prefix = MAI.getGlobalPrefix();
122 ; // Common noop, no prefix.
123 else if (Prefix[1] == 0)
124 OutName.push_back(Prefix[0]); // Common, one character prefix.
126 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
129 // 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());
139 // On systems that do not allow quoted names, we need to mangle most
140 // strange characters.
141 if (!MAI.doesAllowQuotesInName())
142 return appendMangledName(OutName, Name, MAI);
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);
150 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
151 /// a suffix on their name indicating the number of words of arguments they
153 static 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();
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;
167 raw_svector_ostream(OutName) << '@' << ArgWords;
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.
174 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
175 const GlobalValue *GV,
176 bool isImplicitlyPrivate) {
177 ManglerPrefixTy PrefixTy = Mangler::Default;
178 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
179 PrefixTy = Mangler::Private;
180 else if (GV->hasLinkerPrivateLinkage())
181 PrefixTy = Mangler::LinkerPrivate;
183 // If this global has a name, handle it simply.
185 getNameWithPrefix(OutName, GV->getName(), PrefixTy);
187 // Get the ID for the global, assigning a new one if we haven't got one
189 unsigned &ID = AnonGlobalIDs[GV];
190 if (ID == 0) ID = NextAnonGlobalID++;
192 // Must mangle the global into a unique ID.
193 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
196 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
198 if (Context.getAsmInfo().hasMicrosoftFastStdCallMangling()) {
199 if (const Function *F = dyn_cast<Function>(GV)) {
200 CallingConv::ID CC = F->getCallingConv();
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] == '_')
208 OutName.insert(OutName.begin(), '@');
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);
223 /// 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.
226 std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
227 bool isImplicitlyPrivate) {
229 getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
230 return std::string(Buf.begin(), Buf.end());
233 /// getSymbol - Return the MCSymbol for the specified global value. This
234 /// symbol is the main label that is the address of the global.
235 MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
236 SmallString<60> NameStr;
237 getNameWithPrefix(NameStr, GV, false);
238 return Context.GetOrCreateSymbol(NameStr.str());