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, const char *Prefix) { 00035 std::string Result; 00036 if (X.empty()) return X; // Empty names are uniqued by the caller. 00037 00038 // If PreserveAsmNames is set, names with asm identifiers are not modified. 00039 if (PreserveAsmNames && X[0] == 1) 00040 return X; 00041 00042 if (!UseQuotes) { 00043 // If X does not start with (char)1, add the prefix. 00044 std::string::const_iterator I = X.begin(); 00045 if (*I != 1) 00046 Result = Prefix; 00047 else 00048 ++I; // Skip over the marker. 00049 00050 // Mangle the first letter specially, don't allow numbers. 00051 if (*I >= '0' && *I <= '9') 00052 Result += MangleLetter(*I++); 00053 00054 for (std::string::const_iterator E = X.end(); I != E; ++I) { 00055 if (!isCharAcceptable(*I)) 00056 Result += MangleLetter(*I); 00057 else 00058 Result += *I; 00059 } 00060 } else { 00061 bool NeedsQuotes = false; 00062 00063 std::string::const_iterator I = X.begin(); 00064 if (*I == 1) 00065 ++I; // Skip over the marker. 00066 00067 // If the first character is a number, we need quotes. 00068 if (*I >= '0' && *I <= '9') 00069 NeedsQuotes = true; 00070 00071 // Do an initial scan of the string, checking to see if we need quotes or 00072 // to escape a '"' or not. 00073 if (!NeedsQuotes) 00074 for (std::string::const_iterator E = X.end(); I != E; ++I) 00075 if (!isCharAcceptable(*I)) { 00076 NeedsQuotes = true; 00077 break; 00078 } 00079 00080 // In the common case, we don't need quotes. Handle this quickly. 00081 if (!NeedsQuotes) { 00082 if (*X.begin() != 1) 00083 return Prefix+X; 00084 else 00085 return X.substr(1); 00086 } 00087 00088 // Otherwise, construct the string the expensive way. 00089 I = X.begin(); 00090 00091 // If X does not start with (char)1, add the prefix. 00092 if (*I != 1) 00093 Result = Prefix; 00094 else 00095 ++I; // Skip the marker if present. 00096 00097 for (std::string::const_iterator E = X.end(); I != E; ++I) { 00098 if (*I == '"') 00099 Result += "_QQ_"; 00100 else if (*I == '\n') 00101 Result += "_NL_"; 00102 else 00103 Result += *I; 00104 } 00105 Result = '"' + Result + '"'; 00106 } 00107 return Result; 00108 } 00109 00110 /// getTypeID - Return a unique ID for the specified LLVM type. 00111 /// 00112 unsigned Mangler::getTypeID(const Type *Ty) { 00113 unsigned &E = TypeMap[Ty]; 00114 if (E == 0) E = ++TypeCounter; 00115 return E; 00116 } 00117 00118 std::string Mangler::getValueName(const Value *V) { 00119 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) 00120 return getValueName(GV); 00121 00122 std::string &Name = Memo[V]; 00123 if (!Name.empty()) 00124 return Name; // Return the already-computed name for V. 00125 00126 // Always mangle local names. 00127 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType())); 00128 return Name; 00129 } 00130 00131 00132 std::string Mangler::getValueName(const GlobalValue *GV, const char * Suffix) { 00133 // Check to see whether we've already named V. 00134 std::string &Name = Memo[GV]; 00135 if (!Name.empty()) 00136 return Name; // Return the already-computed name for V. 00137 00138 // Name mangling occurs as follows: 00139 // - If V is an intrinsic function, do not change name at all 00140 // - Otherwise, mangling occurs if global collides with existing name. 00141 if (isa<Function>(GV) && cast<Function>(GV)->isIntrinsic()) { 00142 Name = GV->getNameStart(); // Is an intrinsic function 00143 } else if (!GV->hasName()) { 00144 // Must mangle the global into a unique ID. 00145 unsigned TypeUniqueID = getTypeID(GV->getType()); 00146 static unsigned GlobalID = 0; 00147 Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(GlobalID++); 00148 } else { 00149 Name = makeNameProper(GV->getName() + Suffix, Prefix); 00150 std::string prefix; 00151 if (GV->hasPrivateLinkage()) 00152 prefix = PrivatePrefix; 00153 else 00154 prefix = ""; 00155 Name = prefix + Name; 00156 } 00157 00158 return Name; 00159 } 00160 00161 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix) 00162 : Prefix(prefix), PrivatePrefix (privatePrefix), UseQuotes(false), 00163 PreserveAsmNames(false), Count(0), TypeCounter(0) { 00164 std::fill(AcceptableChars, array_endof(AcceptableChars), 0); 00165 00166 // Letters and numbers are acceptable. 00167 for (unsigned char X = 'a'; X <= 'z'; ++X) 00168 markCharAcceptable(X); 00169 for (unsigned char X = 'A'; X <= 'Z'; ++X) 00170 markCharAcceptable(X); 00171 for (unsigned char X = '0'; X <= '9'; ++X) 00172 markCharAcceptable(X); 00173 00174 // These chars are acceptable. 00175 markCharAcceptable('_'); 00176 markCharAcceptable('$'); 00177 markCharAcceptable('.'); 00178 }