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/Module.h" 00016 #include "llvm/Type.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 return std::string("_")+HexDigit(C >> 4) + HexDigit(C & 15) + "_"; 00026 } 00027 00028 /// makeNameProper - We don't want identifier names non-C-identifier characters 00029 /// in them, so mangle them as appropriate. 00030 /// 00031 std::string Mangler::makeNameProper(const std::string &X) { 00032 std::string Result; 00033 00034 // Mangle the first letter specially, don't allow numbers... 00035 if ((X[0] < 'a' || X[0] > 'z') && (X[0] < 'A' || X[0] > 'Z') && X[0] != '_') 00036 Result += MangleLetter(X[0]); 00037 else 00038 Result += X[0]; 00039 00040 for (std::string::const_iterator I = X.begin()+1, E = X.end(); I != E; ++I) 00041 if ((*I < 'a' || *I > 'z') && (*I < 'A' || *I > 'Z') && 00042 (*I < '0' || *I > '9') && *I != '_') 00043 Result += MangleLetter(*I); 00044 else 00045 Result += *I; 00046 return Result; 00047 } 00048 00049 /// getTypeID - Return a unique ID for the specified LLVM type. 00050 /// 00051 unsigned Mangler::getTypeID(const Type *Ty) { 00052 unsigned &E = TypeMap[Ty]; 00053 if (E == 0) E = ++TypeCounter; 00054 return E; 00055 } 00056 00057 00058 std::string Mangler::getValueName(const Value *V) { 00059 // Check to see whether we've already named V. 00060 ValueMap::iterator VI = Memo.find(V); 00061 if (VI != Memo.end()) { 00062 return VI->second; // Return the old name for V. 00063 } 00064 00065 std::string name; 00066 if (V->hasName()) { // Print out the label if it exists... 00067 // Name mangling occurs as follows: 00068 // - If V is an intrinsic function, do not change name at all 00069 // - If V is not a global, mangling always occurs. 00070 // - Otherwise, mangling occurs when any of the following are true: 00071 // 1) V has internal linkage 00072 // 2) V's name would collide if it is not mangled. 00073 // 00074 const GlobalValue* gv = dyn_cast<GlobalValue>(V); 00075 if (gv && isa<Function>(gv) && cast<Function>(gv)->getIntrinsicID()) { 00076 name = gv->getName(); // Is an intrinsic function 00077 } else if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) { 00078 name = Prefix + makeNameProper(gv->getName()); 00079 } else { 00080 // Non-global, or global with internal linkage / colliding name 00081 // -> mangle. 00082 unsigned TypeUniqueID = getTypeID(V->getType()); 00083 name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(V->getName()); 00084 } 00085 } else { 00086 name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType())); 00087 } 00088 00089 Memo[V] = name; 00090 return name; 00091 } 00092 00093 void Mangler::InsertName(GlobalValue *GV, 00094 std::map<std::string, GlobalValue*> &Names) { 00095 if (!GV->hasName()) { // We must mangle unnamed globals. 00096 MangledGlobals.insert(GV); 00097 return; 00098 } 00099 00100 // Figure out if this is already used. 00101 GlobalValue *&ExistingValue = Names[GV->getName()]; 00102 if (!ExistingValue) { 00103 ExistingValue = GV; 00104 } else { 00105 // If GV is external but the existing one is static, mangle the existing one 00106 if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) { 00107 MangledGlobals.insert(ExistingValue); 00108 ExistingValue = GV; 00109 } else { 00110 // Otherwise, mangle GV 00111 MangledGlobals.insert(GV); 00112 } 00113 } 00114 } 00115 00116 00117 Mangler::Mangler(Module &m, const char *prefix) 00118 : M(m), Prefix(prefix), TypeCounter(0), Count(0) { 00119 // Calculate which global values have names that will collide when we throw 00120 // away type information. 00121 std::map<std::string, GlobalValue*> Names; 00122 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 00123 InsertName(I, Names); 00124 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) 00125 InsertName(I, Names); 00126 }