Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 1 | //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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" |
Chris Lattner | 9ddf212 | 2005-11-10 18:48:58 +0000 | [diff] [blame] | 15 | #include "llvm/DerivedTypes.h" |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 30 | /// |
Chris Lattner | 5b845c9 | 2005-09-24 08:24:28 +0000 | [diff] [blame] | 31 | std::string Mangler::makeNameProper(const std::string &X, const char *Prefix) { |
Chris Lattner | aa8a847 | 2003-08-24 21:08:38 +0000 | [diff] [blame] | 32 | std::string Result; |
Chris Lattner | 5b845c9 | 2005-09-24 08:24:28 +0000 | [diff] [blame] | 33 | |
Chris Lattner | ac8c834 | 2005-11-10 19:30:07 +0000 | [diff] [blame^] | 34 | if (!UseQuotes) { |
| 35 | // If X does not start with (char)1, add the prefix. |
| 36 | std::string::const_iterator I = X.begin(); |
| 37 | if (*I != 1) |
| 38 | Result = Prefix; |
Chris Lattner | aa8a847 | 2003-08-24 21:08:38 +0000 | [diff] [blame] | 39 | else |
Chris Lattner | ac8c834 | 2005-11-10 19:30:07 +0000 | [diff] [blame^] | 40 | ++I; // Skip over the marker. |
| 41 | |
| 42 | // Mangle the first letter specially, don't allow numbers... |
| 43 | if (*I >= '0' && *I <= '9') |
| 44 | Result += MangleLetter(*I++); |
| 45 | |
| 46 | for (std::string::const_iterator E = X.end(); I != E; ++I) |
| 47 | if ((*I < 'a' || *I > 'z') && (*I < 'A' || *I > 'Z') && |
| 48 | (*I < '0' || *I > '9') && *I != '_' && *I != '$') |
| 49 | Result += MangleLetter(*I); |
| 50 | else |
| 51 | Result += *I; |
| 52 | } else { |
| 53 | bool NeedsQuotes = false; |
| 54 | |
| 55 | // If X does not start with (char)1, add the prefix. |
| 56 | std::string::const_iterator I = X.begin(); |
| 57 | if (*I != 1) |
| 58 | Result = Prefix; |
| 59 | else |
| 60 | ++I; // Skip over the marker. |
| 61 | |
| 62 | // If the first character is a number, we need quotes. |
| 63 | if (*I >= '0' && *I <= '9') |
| 64 | NeedsQuotes = true; |
| 65 | |
| 66 | for (std::string::const_iterator E = X.end(); I != E; ++I) |
| 67 | if (*I == '"') |
| 68 | Result += "_QQ_"; |
| 69 | else { |
| 70 | if ((*I < 'a' || *I > 'z') && (*I < 'A' || *I > 'Z') && |
| 71 | (*I < '0' || *I > '9') && *I != '_' && *I != '$' && *I != '.') |
| 72 | NeedsQuotes = true; |
| 73 | Result += *I; |
| 74 | } |
| 75 | if (NeedsQuotes) |
| 76 | Result = '"' + Result + '"'; |
| 77 | } |
Chris Lattner | aa8a847 | 2003-08-24 21:08:38 +0000 | [diff] [blame] | 78 | return Result; |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Chris Lattner | d6391d7 | 2004-07-08 22:09:34 +0000 | [diff] [blame] | 81 | /// getTypeID - Return a unique ID for the specified LLVM type. |
| 82 | /// |
| 83 | unsigned Mangler::getTypeID(const Type *Ty) { |
| 84 | unsigned &E = TypeMap[Ty]; |
| 85 | if (E == 0) E = ++TypeCounter; |
| 86 | return E; |
| 87 | } |
| 88 | |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 89 | std::string Mangler::getValueName(const Value *V) { |
Chris Lattner | 9ddf212 | 2005-11-10 18:48:58 +0000 | [diff] [blame] | 90 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 91 | return getValueName(GV); |
| 92 | |
| 93 | std::string &Name = Memo[V]; |
| 94 | if (!Name.empty()) |
| 95 | return Name; // Return the already-computed name for V. |
| 96 | |
| 97 | // Always mangle local names. |
| 98 | Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType())); |
| 99 | return Name; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | std::string Mangler::getValueName(const GlobalValue *GV) { |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 104 | // Check to see whether we've already named V. |
Chris Lattner | 9ddf212 | 2005-11-10 18:48:58 +0000 | [diff] [blame] | 105 | std::string &Name = Memo[GV]; |
| 106 | if (!Name.empty()) |
| 107 | return Name; // Return the already-computed name for V. |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 9ddf212 | 2005-11-10 18:48:58 +0000 | [diff] [blame] | 109 | // Name mangling occurs as follows: |
| 110 | // - If V is an intrinsic function, do not change name at all |
| 111 | // - Otherwise, mangling occurs if global collides with existing name. |
| 112 | if (isa<Function>(GV) && cast<Function>(GV)->getIntrinsicID()) { |
| 113 | Name = GV->getName(); // Is an intrinsic function |
| 114 | } else if (!MangledGlobals.count(GV)) { |
| 115 | Name = makeNameProper(GV->getName(), Prefix); |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 116 | } else { |
Chris Lattner | 9ddf212 | 2005-11-10 18:48:58 +0000 | [diff] [blame] | 117 | unsigned TypeUniqueID = getTypeID(GV->getType()); |
| 118 | Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName()); |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 119 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 9ddf212 | 2005-11-10 18:48:58 +0000 | [diff] [blame] | 121 | return Name; |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Chris Lattner | a6acb4f | 2004-02-14 00:30:23 +0000 | [diff] [blame] | 124 | void Mangler::InsertName(GlobalValue *GV, |
| 125 | std::map<std::string, GlobalValue*> &Names) { |
| 126 | if (!GV->hasName()) { // We must mangle unnamed globals. |
| 127 | MangledGlobals.insert(GV); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | // Figure out if this is already used. |
| 132 | GlobalValue *&ExistingValue = Names[GV->getName()]; |
| 133 | if (!ExistingValue) { |
| 134 | ExistingValue = GV; |
| 135 | } else { |
| 136 | // If GV is external but the existing one is static, mangle the existing one |
| 137 | if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) { |
| 138 | MangledGlobals.insert(ExistingValue); |
| 139 | ExistingValue = GV; |
| 140 | } else { |
| 141 | // Otherwise, mangle GV |
| 142 | MangledGlobals.insert(GV); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | |
Chris Lattner | 2f9bc4f | 2005-11-10 19:02:18 +0000 | [diff] [blame] | 148 | Mangler::Mangler(Module &M, const char *prefix) |
Chris Lattner | ac8c834 | 2005-11-10 19:30:07 +0000 | [diff] [blame^] | 149 | : Prefix(prefix), UseQuotes(false), Count(0), TypeCounter(0) { |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 150 | // Calculate which global values have names that will collide when we throw |
| 151 | // away type information. |
Chris Lattner | a6acb4f | 2004-02-14 00:30:23 +0000 | [diff] [blame] | 152 | std::map<std::string, GlobalValue*> Names; |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 153 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | a6acb4f | 2004-02-14 00:30:23 +0000 | [diff] [blame] | 154 | InsertName(I, Names); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 155 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) |
Chris Lattner | a6acb4f | 2004-02-14 00:30:23 +0000 | [diff] [blame] | 156 | InsertName(I, Names); |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 157 | } |