Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 1 | //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 9 | // |
| 10 | // Unified name mangler for CWriter and assembly backends. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Brian Gaeke | 6b902dc | 2003-07-25 20:21:20 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Mangler.h" |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/Type.h" |
| 17 | #include "Support/StringExtras.h" |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 18 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 19 | |
Chris Lattner | aa8a847 | 2003-08-24 21:08:38 +0000 | [diff] [blame] | 20 | static char HexDigit(int V) { |
| 21 | return V < 10 ? V+'0' : V+'A'-10; |
| 22 | } |
| 23 | |
| 24 | static std::string MangleLetter(unsigned char C) { |
| 25 | return std::string("_")+HexDigit(C >> 4) + HexDigit(C & 15) + "_"; |
| 26 | } |
| 27 | |
| 28 | /// makeNameProper - We don't want identifier names non-C-identifier characters |
| 29 | /// in them, so mangle them as appropriate. |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 30 | /// |
Chris Lattner | aa8a847 | 2003-08-24 21:08:38 +0000 | [diff] [blame] | 31 | std::string Mangler::makeNameProper(const std::string &X) { |
| 32 | std::string Result; |
| 33 | |
| 34 | // Mangle the first letter specially, don't allow numbers... |
| 35 | if ((X[0] < 'a' || X[0] > 'z') && (X[0] < 'A' || X[0] > 'Z') && X[0] != '_') |
| 36 | Result += MangleLetter(X[0]); |
| 37 | else |
| 38 | Result += X[0]; |
| 39 | |
| 40 | for (std::string::const_iterator I = X.begin()+1, E = X.end(); I != E; ++I) |
| 41 | if ((*I < 'a' || *I > 'z') && (*I < 'A' || *I > 'Z') && |
| 42 | (*I < '0' || *I > '9') && *I != '_') |
| 43 | Result += MangleLetter(*I); |
| 44 | else |
| 45 | Result += *I; |
| 46 | return Result; |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | std::string Mangler::getValueName(const Value *V) { |
| 50 | // Check to see whether we've already named V. |
| 51 | ValueMap::iterator VI = Memo.find(V); |
| 52 | if (VI != Memo.end()) { |
| 53 | return VI->second; // Return the old name for V. |
| 54 | } |
| 55 | |
| 56 | std::string name; |
| 57 | if (V->hasName()) { // Print out the label if it exists... |
| 58 | // Name mangling occurs as follows: |
Chris Lattner | 6e40e1d | 2004-04-05 20:17:53 +0000 | [diff] [blame^] | 59 | // - If V is an intrinsic function, do not change name at all |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 60 | // - If V is not a global, mangling always occurs. |
| 61 | // - Otherwise, mangling occurs when any of the following are true: |
| 62 | // 1) V has internal linkage |
| 63 | // 2) V's name would collide if it is not mangled. |
| 64 | // |
| 65 | const GlobalValue* gv = dyn_cast<GlobalValue>(V); |
Chris Lattner | 6e40e1d | 2004-04-05 20:17:53 +0000 | [diff] [blame^] | 66 | if (gv && isa<Function>(gv) && cast<Function>(gv)->getIntrinsicID()) { |
| 67 | name = gv->getName(); // Is an intrinsic function |
| 68 | } else if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) { |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 69 | name = makeNameProper(gv->getName()); |
Chris Lattner | 2b3860f | 2003-08-11 19:34:29 +0000 | [diff] [blame] | 70 | if (AddUnderscorePrefix) name = "_" + name; |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 71 | } else { |
| 72 | // Non-global, or global with internal linkage / colliding name |
| 73 | // -> mangle. |
| 74 | name = "l" + utostr(V->getType()->getUniqueID()) + "_" + |
| 75 | makeNameProper(V->getName()); |
| 76 | } |
| 77 | } else { |
Brian Gaeke | 4166445 | 2003-07-24 21:37:57 +0000 | [diff] [blame] | 78 | name = "ltmp_" + utostr(Count++) + "_" |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 79 | + utostr(V->getType()->getUniqueID()); |
| 80 | } |
Chris Lattner | 2b3860f | 2003-08-11 19:34:29 +0000 | [diff] [blame] | 81 | |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 82 | Memo[V] = name; |
| 83 | return name; |
| 84 | } |
| 85 | |
Chris Lattner | a6acb4f | 2004-02-14 00:30:23 +0000 | [diff] [blame] | 86 | void Mangler::InsertName(GlobalValue *GV, |
| 87 | std::map<std::string, GlobalValue*> &Names) { |
| 88 | if (!GV->hasName()) { // We must mangle unnamed globals. |
| 89 | MangledGlobals.insert(GV); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // Figure out if this is already used. |
| 94 | GlobalValue *&ExistingValue = Names[GV->getName()]; |
| 95 | if (!ExistingValue) { |
| 96 | ExistingValue = GV; |
| 97 | } else { |
| 98 | // If GV is external but the existing one is static, mangle the existing one |
| 99 | if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) { |
| 100 | MangledGlobals.insert(ExistingValue); |
| 101 | ExistingValue = GV; |
| 102 | } else { |
| 103 | // Otherwise, mangle GV |
| 104 | MangledGlobals.insert(GV); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
Chris Lattner | 2b3860f | 2003-08-11 19:34:29 +0000 | [diff] [blame] | 110 | Mangler::Mangler(Module &m, bool addUnderscorePrefix) |
Chris Lattner | 82a5ff4 | 2004-02-11 03:29:16 +0000 | [diff] [blame] | 111 | : M(m), AddUnderscorePrefix(addUnderscorePrefix), Count(0) { |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 112 | // Calculate which global values have names that will collide when we throw |
| 113 | // away type information. |
Chris Lattner | a6acb4f | 2004-02-14 00:30:23 +0000 | [diff] [blame] | 114 | std::map<std::string, GlobalValue*> Names; |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 115 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | a6acb4f | 2004-02-14 00:30:23 +0000 | [diff] [blame] | 116 | InsertName(I, Names); |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 117 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
Chris Lattner | a6acb4f | 2004-02-14 00:30:23 +0000 | [diff] [blame] | 118 | InsertName(I, Names); |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 119 | } |