LLVM API Documentation
00001 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source 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/StringExtras.h" 00018 using namespace llvm; 00019 00020 static char HexDigit(int V) { 00021 return V < 10 ? V+'0' : V+'A'-10; 00022 } 00023 00024 static std::string MangleLetter(unsigned char C) { 00025 char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 }; 00026 return Result; 00027 } 00028 00029 /// makeNameProper - We don't want identifier names non-C-identifier characters 00030 /// in them, so mangle them as appropriate. 00031 /// 00032 std::string Mangler::makeNameProper(const std::string &X, const char *Prefix) { 00033 std::string Result; 00034 if (X.empty()) return X; // Empty names are uniqued by the caller. 00035 00036 if (!UseQuotes) { 00037 // If X does not start with (char)1, add the prefix. 00038 std::string::const_iterator I = X.begin(); 00039 if (*I != 1) 00040 Result = Prefix; 00041 else 00042 ++I; // Skip over the marker. 00043 00044 // Mangle the first letter specially, don't allow numbers. 00045 if (*I >= '0' && *I <= '9') 00046 Result += MangleLetter(*I++); 00047 00048 for (std::string::const_iterator E = X.end(); I != E; ++I) { 00049 if (!isCharAcceptable(*I)) 00050 Result += MangleLetter(*I); 00051 else 00052 Result += *I; 00053 } 00054 } else { 00055 bool NeedsQuotes = false; 00056 00057 std::string::const_iterator I = X.begin(); 00058 if (*I == 1) 00059 ++I; // Skip over the marker. 00060 00061 // If the first character is a number, we need quotes. 00062 if (*I >= '0' && *I <= '9') 00063 NeedsQuotes = true; 00064 00065 // Do an initial scan of the string, checking to see if we need quotes or 00066 // to escape a '"' or not. 00067 if (!NeedsQuotes) 00068 for (std::string::const_iterator E = X.end(); I != E; ++I) 00069 if (!isCharAcceptable(*I)) { 00070 NeedsQuotes = true; 00071 break; 00072 } 00073 00074 // In the common case, we don't need quotes. Handle this quickly. 00075 if (!NeedsQuotes) { 00076 if (*X.begin() != 1) 00077 return Prefix+X; 00078 else 00079 return X.substr(1); 00080 } 00081 00082 // Otherwise, construct the string the expensive way. 00083 I = X.begin(); 00084 00085 // If X does not start with (char)1, add the prefix. 00086 if (*I != 1) 00087 Result = Prefix; 00088 else 00089 ++I; // Skip the marker if present. 00090 00091 for (std::string::const_iterator E = X.end(); I != E; ++I) { 00092 if (*I == '"') 00093 Result += "_QQ_"; 00094 else 00095 Result += *I; 00096 } 00097 Result = '"' + Result + '"'; 00098 } 00099 return Result; 00100 } 00101 00102 /// getTypeID - Return a unique ID for the specified LLVM type. 00103 /// 00104 unsigned Mangler::getTypeID(const Type *Ty) { 00105 unsigned &E = TypeMap[Ty]; 00106 if (E == 0) E = ++TypeCounter; 00107 return E; 00108 } 00109 00110 std::string Mangler::getValueName(const Value *V) { 00111 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) 00112 return getValueName(GV); 00113 00114 std::string &Name = Memo[V]; 00115 if (!Name.empty()) 00116 return Name; // Return the already-computed name for V. 00117 00118 // Always mangle local names. 00119 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType())); 00120 return Name; 00121 } 00122 00123 00124 std::string Mangler::getValueName(const GlobalValue *GV) { 00125 // Check to see whether we've already named V. 00126 std::string &Name = Memo[GV]; 00127 if (!Name.empty()) 00128 return Name; // Return the already-computed name for V. 00129 00130 // Name mangling occurs as follows: 00131 // - If V is an intrinsic function, do not change name at all 00132 // - Otherwise, mangling occurs if global collides with existing name. 00133 if (isa<Function>(GV) && cast<Function>(GV)->getIntrinsicID()) { 00134 Name = GV->getName(); // Is an intrinsic function 00135 } else if (!GV->hasName()) { 00136 // Must mangle the global into a unique ID. 00137 unsigned TypeUniqueID = getTypeID(GV->getType()); 00138 static unsigned GlobalID = 0; 00139 Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(GlobalID++); 00140 } else if (!MangledGlobals.count(GV)) { 00141 Name = makeNameProper(GV->getName(), Prefix); 00142 } else { 00143 unsigned TypeUniqueID = getTypeID(GV->getType()); 00144 Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName()); 00145 } 00146 00147 return Name; 00148 } 00149 00150 void Mangler::InsertName(GlobalValue *GV, 00151 std::map<std::string, GlobalValue*> &Names) { 00152 if (!GV->hasName()) // We must mangle unnamed globals. 00153 return; 00154 00155 // Figure out if this is already used. 00156 GlobalValue *&ExistingValue = Names[GV->getName()]; 00157 if (!ExistingValue) { 00158 ExistingValue = GV; 00159 } else { 00160 // If GV is external but the existing one is static, mangle the existing one 00161 if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) { 00162 MangledGlobals.insert(ExistingValue); 00163 ExistingValue = GV; 00164 } else if (GV->hasExternalLinkage() && ExistingValue->hasExternalLinkage()&& 00165 GV->isExternal() && ExistingValue->isExternal()) { 00166 // If the two globals both have external inkage, and are both external, 00167 // don't mangle either of them, we just have some silly type mismatch. 00168 } else { 00169 // Otherwise, mangle GV 00170 MangledGlobals.insert(GV); 00171 } 00172 } 00173 } 00174 00175 00176 Mangler::Mangler(Module &M, const char *prefix) 00177 : Prefix(prefix), UseQuotes(false), Count(0), TypeCounter(0) { 00178 std::fill(AcceptableChars, 00179 AcceptableChars+sizeof(AcceptableChars)/sizeof(AcceptableChars[0]), 00180 0); 00181 00182 // Letters and numbers are acceptable. 00183 for (unsigned char X = 'a'; X <= 'z'; ++X) 00184 markCharAcceptable(X); 00185 for (unsigned char X = 'A'; X <= 'Z'; ++X) 00186 markCharAcceptable(X); 00187 for (unsigned char X = '0'; X <= '9'; ++X) 00188 markCharAcceptable(X); 00189 00190 // These chars are acceptable. 00191 markCharAcceptable('_'); 00192 markCharAcceptable('$'); 00193 markCharAcceptable('.'); 00194 00195 // Calculate which global values have names that will collide when we throw 00196 // away type information. 00197 std::map<std::string, GlobalValue*> Names; 00198 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 00199 InsertName(I, Names); 00200 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) 00201 InsertName(I, Names); 00202 } 00203 00204 // Cause this file to be linked in when Support/Mangler.h is #included 00205 DEFINING_FILE_FOR(Mangler)