LLVM API Documentation
00001 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Unified name mangler for CWriter and assembly backends. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Support/Mangler.h" 00015 #include "llvm/DerivedTypes.h" 00016 #include "llvm/Module.h" 00017 #include "llvm/ADT/STLExtras.h" 00018 #include "llvm/ADT/StringExtras.h" 00019 #include "llvm/ADT/StringMap.h" 00020 using namespace llvm; 00021 00022 static char HexDigit(int V) { 00023 return V < 10 ? V+'0' : V+'A'-10; 00024 } 00025 00026 static std::string MangleLetter(unsigned char C) { 00027 char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 }; 00028 return Result; 00029 } 00030 00031 /// makeNameProper - We don't want identifier names non-C-identifier characters 00032 /// in them, so mangle them as appropriate. 00033 /// 00034 std::string Mangler::makeNameProper(const std::string &X, 00035 ManglerPrefixTy PrefixTy) { 00036 assert(!X.empty() && "Cannot mangle empty strings"); 00037 00038 if (!UseQuotes) { 00039 std::string Result; 00040 00041 // If X does not start with (char)1, add the prefix. 00042 bool NeedPrefix = true; 00043 std::string::const_iterator I = X.begin(); 00044 if (*I == 1) { 00045 NeedPrefix = false; 00046 ++I; // Skip over the marker. 00047 } 00048 00049 // Mangle the first letter specially, don't allow numbers. 00050 if (*I >= '0' && *I <= '9') 00051 Result += MangleLetter(*I++); 00052 00053 for (std::string::const_iterator E = X.end(); I != E; ++I) { 00054 if (!isCharAcceptable(*I)) 00055 Result += MangleLetter(*I); 00056 else 00057 Result += *I; 00058 } 00059 00060 if (NeedPrefix) { 00061 Result = Prefix + Result; 00062 00063 if (PrefixTy == Mangler::Private) 00064 Result = PrivatePrefix + Result; 00065 else if (PrefixTy == Mangler::LinkerPrivate) 00066 Result = LinkerPrivatePrefix + Result; 00067 } 00068 00069 return Result; 00070 } 00071 00072 bool NeedPrefix = true; 00073 bool NeedQuotes = false; 00074 std::string Result; 00075 std::string::const_iterator I = X.begin(); 00076 if (*I == 1) { 00077 NeedPrefix = false; 00078 ++I; // Skip over the marker. 00079 } 00080 00081 // If the first character is a number, we need quotes. 00082 if (*I >= '0' && *I <= '9') 00083 NeedQuotes = true; 00084 00085 // Do an initial scan of the string, checking to see if we need quotes or 00086 // to escape a '"' or not. 00087 if (!NeedQuotes) 00088 for (std::string::const_iterator E = X.end(); I != E; ++I) 00089 if (!isCharAcceptable(*I)) { 00090 NeedQuotes = true; 00091 break; 00092 } 00093 00094 // In the common case, we don't need quotes. Handle this quickly. 00095 if (!NeedQuotes) { 00096 if (!NeedPrefix) 00097 return X.substr(1); // Strip off the \001. 00098 00099 Result = Prefix + X; 00100 00101 if (PrefixTy == Mangler::Private) 00102 Result = PrivatePrefix + Result; 00103 else if (PrefixTy == Mangler::LinkerPrivate) 00104 Result = LinkerPrivatePrefix + Result; 00105 00106 return Result; 00107 } 00108 00109 if (NeedPrefix) 00110 Result = X.substr(0, I-X.begin()); 00111 00112 // Otherwise, construct the string the expensive way. 00113 for (std::string::const_iterator E = X.end(); I != E; ++I) { 00114 if (*I == '"') 00115 Result += "_QQ_"; 00116 else if (*I == '\n') 00117 Result += "_NL_"; 00118 else 00119 Result += *I; 00120 } 00121 00122 if (NeedPrefix) { 00123 Result = Prefix + Result; 00124 00125 if (PrefixTy == Mangler::Private) 00126 Result = PrivatePrefix + Result; 00127 else if (PrefixTy == Mangler::LinkerPrivate) 00128 Result = LinkerPrivatePrefix + Result; 00129 } 00130 00131 Result = '"' + Result + '"'; 00132 return Result; 00133 } 00134 00135 /// getMangledName - Returns the mangled name of V, an LLVM Value, 00136 /// in the current module. If 'Suffix' is specified, the name ends with the 00137 /// specified suffix. If 'ForcePrivate' is specified, the label is specified 00138 /// to have a private label prefix. 00139 /// 00140 std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix, 00141 bool ForcePrivate) { 00142 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) && 00143 "Intrinsic functions cannot be mangled by Mangler"); 00144 00145 ManglerPrefixTy PrefixTy = 00146 (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private : 00147 GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default; 00148 00149 if (GV->hasName()) 00150 return makeNameProper(GV->getNameStr() + Suffix, PrefixTy); 00151 00152 // Get the ID for the global, assigning a new one if we haven't got one 00153 // already. 00154 unsigned &ID = AnonGlobalIDs[GV]; 00155 if (ID == 0) ID = NextAnonGlobalID++; 00156 00157 // Must mangle the global into a unique ID. 00158 return makeNameProper("__unnamed_" + utostr(ID) + Suffix, PrefixTy); 00159 } 00160 00161 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix, 00162 const char *linkerPrivatePrefix) 00163 : Prefix(prefix), PrivatePrefix(privatePrefix), 00164 LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false), 00165 NextAnonGlobalID(1) { 00166 std::fill(AcceptableChars, array_endof(AcceptableChars), 0); 00167 00168 // Letters and numbers are acceptable. 00169 for (unsigned char X = 'a'; X <= 'z'; ++X) 00170 markCharAcceptable(X); 00171 for (unsigned char X = 'A'; X <= 'Z'; ++X) 00172 markCharAcceptable(X); 00173 for (unsigned char X = '0'; X <= '9'; ++X) 00174 markCharAcceptable(X); 00175 00176 // These chars are acceptable. 00177 markCharAcceptable('_'); 00178 markCharAcceptable('$'); 00179 markCharAcceptable('.'); 00180 }