Mangler.cpp revision 243830
11541Srgrimes//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===// 21541Srgrimes// 31541Srgrimes// The LLVM Compiler Infrastructure 41541Srgrimes// 51541Srgrimes// This file is distributed under the University of Illinois Open Source 61541Srgrimes// License. See LICENSE.TXT for details. 71541Srgrimes// 845773Sdcs//===----------------------------------------------------------------------===// 945773Sdcs// 101541Srgrimes// Unified name mangler for assembly backends. 111541Srgrimes// 121541Srgrimes//===----------------------------------------------------------------------===// 131541Srgrimes 141541Srgrimes#include "llvm/Target/Mangler.h" 151541Srgrimes#include "llvm/DerivedTypes.h" 161541Srgrimes#include "llvm/Function.h" 171541Srgrimes#include "llvm/DataLayout.h" 181541Srgrimes#include "llvm/MC/MCAsmInfo.h" 191541Srgrimes#include "llvm/MC/MCContext.h" 201541Srgrimes#include "llvm/Support/raw_ostream.h" 211541Srgrimes#include "llvm/ADT/SmallString.h" 221541Srgrimes#include "llvm/ADT/Twine.h" 231541Srgrimesusing namespace llvm; 241541Srgrimes 251541Srgrimesstatic bool isAcceptableChar(char C, bool AllowPeriod, bool AllowUTF8) { 261541Srgrimes if ((C < 'a' || C > 'z') && 271541Srgrimes (C < 'A' || C > 'Z') && 281541Srgrimes (C < '0' || C > '9') && 291541Srgrimes C != '_' && C != '$' && C != '@' && 301541Srgrimes !(AllowPeriod && C == '.') && 311541Srgrimes !(AllowUTF8 && (C & 0x80))) 321541Srgrimes return false; 331541Srgrimes return true; 341541Srgrimes} 351541Srgrimes 361541Srgrimesstatic char HexDigit(int V) { 371541Srgrimes return V < 10 ? V+'0' : V+'A'-10; 381541Srgrimes} 3922521Sdyson 4045773Sdcsstatic void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) { 411541Srgrimes OutName.push_back('_'); 421541Srgrimes OutName.push_back(HexDigit(C >> 4)); 431541Srgrimes OutName.push_back(HexDigit(C & 15)); 441541Srgrimes OutName.push_back('_'); 451541Srgrimes} 461541Srgrimes 471541Srgrimes/// NameNeedsEscaping - Return true if the identifier \p Str needs quotes 481541Srgrimes/// for this assembler. 491541Srgrimesstatic bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) { 5045773Sdcs assert(!Str.empty() && "Cannot create an empty MCSymbol"); 5145773Sdcs 5245773Sdcs // If the first character is a number and the target does not allow this, we 5345773Sdcs // need quotes. 5445773Sdcs if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') 5545773Sdcs return true; 5645773Sdcs 5745773Sdcs // If any of the characters in the string is an unacceptable character, force 5845773Sdcs // quotes. 5945773Sdcs bool AllowPeriod = MAI.doesAllowPeriodsInName(); 6045773Sdcs bool AllowUTF8 = MAI.doesAllowUTF8(); 6145773Sdcs for (unsigned i = 0, e = Str.size(); i != e; ++i) 6245773Sdcs if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8)) 6345773Sdcs return true; 6445773Sdcs return false; 6545773Sdcs} 6645773Sdcs 6745773Sdcs/// appendMangledName - Add the specified string in mangled form if it uses 6845773Sdcs/// any unusual characters. 6945773Sdcsstatic void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str, 7045773Sdcs const MCAsmInfo &MAI) { 7145773Sdcs // The first character is not allowed to be a number unless the target 7245773Sdcs // explicitly allows it. 7345773Sdcs if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') { 7445773Sdcs MangleLetter(OutName, Str[0]); 7545773Sdcs Str = Str.substr(1); 7645773Sdcs } 7745773Sdcs 7845773Sdcs bool AllowPeriod = MAI.doesAllowPeriodsInName(); 791541Srgrimes bool AllowUTF8 = MAI.doesAllowUTF8(); 8045773Sdcs for (unsigned i = 0, e = Str.size(); i != e; ++i) { 811541Srgrimes if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8)) 821541Srgrimes MangleLetter(OutName, Str[i]); 831541Srgrimes else 8445773Sdcs OutName.push_back(Str[i]); 8522593Sbde } 8622593Sbde} 8722593Sbde 8822593Sbde 8945773Sdcs/// appendMangledQuotedName - On systems that support quoted symbols, we still 901541Srgrimes/// have to escape some (obscure) characters like " and \n which would break the 911541Srgrimes/// assembler's lexing. 9245773Sdcsstatic void appendMangledQuotedName(SmallVectorImpl<char> &OutName, 938876Srgrimes StringRef Str) { 9445773Sdcs for (unsigned i = 0, e = Str.size(); i != e; ++i) { 9545773Sdcs if (Str[i] == '"' || Str[i] == '\n') 961541Srgrimes MangleLetter(OutName, Str[i]); 9745773Sdcs else 9845773Sdcs OutName.push_back(Str[i]); 9945773Sdcs } 10045773Sdcs} 10145773Sdcs 1021541Srgrimes 1031541Srgrimes/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix 1041541Srgrimes/// and the specified name as the global variable name. GVName must not be 1051541Srgrimes/// empty. 10645773Sdcsvoid Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, 10745773Sdcs const Twine &GVName, ManglerPrefixTy PrefixTy) { 10845773Sdcs SmallString<256> TmpData; 1091541Srgrimes StringRef Name = GVName.toStringRef(TmpData); 1101541Srgrimes assert(!Name.empty() && "getNameWithPrefix requires non-empty name"); 1111541Srgrimes 1121541Srgrimes const MCAsmInfo &MAI = Context.getAsmInfo(); 1131541Srgrimes 1141541Srgrimes // If the global name is not led with \1, add the appropriate prefixes. 1151541Srgrimes if (Name[0] == '\1') { 1161541Srgrimes Name = Name.substr(1); 1171541Srgrimes } else { 1181541Srgrimes if (PrefixTy == Mangler::Private) { 1191541Srgrimes const char *Prefix = MAI.getPrivateGlobalPrefix(); 1201541Srgrimes OutName.append(Prefix, Prefix+strlen(Prefix)); 1211541Srgrimes } else if (PrefixTy == Mangler::LinkerPrivate) { 1221541Srgrimes const char *Prefix = MAI.getLinkerPrivateGlobalPrefix(); 12345773Sdcs OutName.append(Prefix, Prefix+strlen(Prefix)); 12445773Sdcs } 12545773Sdcs 1261541Srgrimes const char *Prefix = MAI.getGlobalPrefix(); 12745773Sdcs if (Prefix[0] == 0) 12845773Sdcs ; // Common noop, no prefix. 12945773Sdcs else if (Prefix[1] == 0) 13045773Sdcs OutName.push_back(Prefix[0]); // Common, one character prefix. 13145773Sdcs else 13245773Sdcs OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix. 13345773Sdcs } 1341541Srgrimes 1351541Srgrimes // If this is a simple string that doesn't need escaping, just append it. 1361541Srgrimes if (!NameNeedsEscaping(Name, MAI) || 1371541Srgrimes // If quotes are supported, they can be used unless the string contains 1381541Srgrimes // a quote or newline. 1391541Srgrimes (MAI.doesAllowQuotesInName() && 1401541Srgrimes Name.find_first_of("\n\"") == StringRef::npos)) { 1411541Srgrimes OutName.append(Name.begin(), Name.end()); 1421541Srgrimes return; 14345773Sdcs } 1441541Srgrimes 1451541Srgrimes // On systems that do not allow quoted names, we need to mangle most 14645773Sdcs // strange characters. 14722593Sbde if (!MAI.doesAllowQuotesInName()) 14822521Sdyson return appendMangledName(OutName, Name, MAI); 14922593Sbde 15022521Sdyson // Okay, the system allows quoted strings. We can quote most anything, the 15122521Sdyson // only characters that need escaping are " and \n. 15222521Sdyson assert(Name.find_first_of("\n\"") != StringRef::npos); 15345773Sdcs return appendMangledQuotedName(OutName, Name); 1541541Srgrimes} 1551541Srgrimes 15645773Sdcs/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require 1578876Srgrimes/// a suffix on their name indicating the number of words of arguments they 1581541Srgrimes/// take. 1591541Srgrimesstatic void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName, 1601541Srgrimes const Function *F, const DataLayout &TD) { 1611541Srgrimes // Calculate arguments size total. 16245773Sdcs unsigned ArgWords = 0; 16345773Sdcs for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); 1648876Srgrimes AI != AE; ++AI) { 1651541Srgrimes Type *Ty = AI->getType(); 1661541Srgrimes // 'Dereference' type in case of byval parameter attribute 16745773Sdcs if (AI->hasByValAttr()) 16845773Sdcs Ty = cast<PointerType>(Ty)->getElementType(); 1691541Srgrimes // Size should be aligned to DWORD boundary 17045773Sdcs ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4; 1711541Srgrimes } 17245773Sdcs 1731541Srgrimes raw_svector_ostream(OutName) << '@' << ArgWords; 1741541Srgrimes} 1751541Srgrimes 176 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) { 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; 188 189 // If this global has a name, handle it simply. 190 if (GV->hasName()) { 191 getNameWithPrefix(OutName, GV->getName(), PrefixTy); 192 } else { 193 // Get the ID for the global, assigning a new one if we haven't got one 194 // already. 195 unsigned &ID = AnonGlobalIDs[GV]; 196 if (ID == 0) ID = NextAnonGlobalID++; 197 198 // Must mangle the global into a unique ID. 199 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy); 200 } 201 202 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall, 203 // add it. 204 if (Context.getAsmInfo().hasMicrosoftFastStdCallMangling()) { 205 if (const Function *F = dyn_cast<Function>(GV)) { 206 CallingConv::ID CC = F->getCallingConv(); 207 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] == '_') 212 OutName[0] = '@'; 213 else 214 OutName.insert(OutName.begin(), '@'); 215 } 216 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); 225 } 226 } 227} 228 229/// getSymbol - Return the MCSymbol for the specified global value. This 230/// symbol is the main label that is the address of the global. 231MCSymbol *Mangler::getSymbol(const GlobalValue *GV) { 232 SmallString<60> NameStr; 233 getNameWithPrefix(NameStr, GV, false); 234 return Context.GetOrCreateSymbol(NameStr.str()); 235} 236 237 238