Mangler.cpp revision 206083
1//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// Unified name mangler for assembly backends. 11// 12//===----------------------------------------------------------------------===// 13 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" 23using namespace llvm; 24 25static 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. 65static 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]); 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 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 99/// empty. 100void 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"); 105 106 const MCAsmInfo &MAI = Context.getAsmInfo(); 107 108 // If the global name is not led with \1, add the appropriate prefixes. 109 if (Name[0] == '\1') { 110 Name = Name.substr(1); 111 } else { 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)); 118 } 119 120 const char *Prefix = MAI.getGlobalPrefix(); 121 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 126 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix. 127 } 128 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()); 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()) 142 return appendMangledName(OutName, Name, MAI); 143 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); 148} 149 150/// 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 170 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) { 177 ManglerPrefixTy PrefixTy = Mangler::Default; 178 if (GV->hasPrivateLinkage() || isImplicitlyPrivate) 179 PrefixTy = Mangler::Private; 180 else if (GV->hasLinkerPrivateLinkage()) 181 PrefixTy = Mangler::LinkerPrivate; 182 183 // If this global has a name, handle it simply. 184 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++; 191 192 // Must mangle the global into a unique ID. 193 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy); 194 } 195 196 // 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 } 221} 222 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. 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} 232 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 return Context.GetOrCreateSymbol(NameStr.str()); 239} 240 241 242