blob: d9186a9c938f9777ce3070089cc24ecd6a348a58 [file] [log] [blame]
Brian Gaekeb198ca32003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2//
3// Unified name mangler for CWriter and assembly backends.
4//
5//===----------------------------------------------------------------------===//
6
Brian Gaeke6b902dc2003-07-25 20:21:20 +00007#include "llvm/Support/Mangler.h"
Brian Gaekeb198ca32003-07-24 20:20:58 +00008#include "llvm/Module.h"
9#include "llvm/Type.h"
10#include "Support/StringExtras.h"
Brian Gaekeb198ca32003-07-24 20:20:58 +000011
Chris Lattneraa8a8472003-08-24 21:08:38 +000012static char HexDigit(int V) {
13 return V < 10 ? V+'0' : V+'A'-10;
14}
15
16static std::string MangleLetter(unsigned char C) {
17 return std::string("_")+HexDigit(C >> 4) + HexDigit(C & 15) + "_";
18}
19
20/// makeNameProper - We don't want identifier names non-C-identifier characters
21/// in them, so mangle them as appropriate.
Brian Gaekeb198ca32003-07-24 20:20:58 +000022///
Chris Lattneraa8a8472003-08-24 21:08:38 +000023std::string Mangler::makeNameProper(const std::string &X) {
24 std::string Result;
25
26 // Mangle the first letter specially, don't allow numbers...
27 if ((X[0] < 'a' || X[0] > 'z') && (X[0] < 'A' || X[0] > 'Z') && X[0] != '_')
28 Result += MangleLetter(X[0]);
29 else
30 Result += X[0];
31
32 for (std::string::const_iterator I = X.begin()+1, E = X.end(); I != E; ++I)
33 if ((*I < 'a' || *I > 'z') && (*I < 'A' || *I > 'Z') &&
34 (*I < '0' || *I > '9') && *I != '_')
35 Result += MangleLetter(*I);
36 else
37 Result += *I;
38 return Result;
Brian Gaekeb198ca32003-07-24 20:20:58 +000039}
40
41std::string Mangler::getValueName(const Value *V) {
42 // Check to see whether we've already named V.
43 ValueMap::iterator VI = Memo.find(V);
44 if (VI != Memo.end()) {
45 return VI->second; // Return the old name for V.
46 }
47
48 std::string name;
49 if (V->hasName()) { // Print out the label if it exists...
50 // Name mangling occurs as follows:
51 // - If V is not a global, mangling always occurs.
52 // - Otherwise, mangling occurs when any of the following are true:
53 // 1) V has internal linkage
54 // 2) V's name would collide if it is not mangled.
55 //
56 const GlobalValue* gv = dyn_cast<GlobalValue>(V);
Chris Lattner2b3860f2003-08-11 19:34:29 +000057 if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
Brian Gaekeb198ca32003-07-24 20:20:58 +000058 name = makeNameProper(gv->getName());
Chris Lattner2b3860f2003-08-11 19:34:29 +000059 if (AddUnderscorePrefix) name = "_" + name;
Brian Gaekeb198ca32003-07-24 20:20:58 +000060 } else {
61 // Non-global, or global with internal linkage / colliding name
62 // -> mangle.
63 name = "l" + utostr(V->getType()->getUniqueID()) + "_" +
64 makeNameProper(V->getName());
65 }
66 } else {
Brian Gaeke41664452003-07-24 21:37:57 +000067 name = "ltmp_" + utostr(Count++) + "_"
Brian Gaekeb198ca32003-07-24 20:20:58 +000068 + utostr(V->getType()->getUniqueID());
69 }
Chris Lattner2b3860f2003-08-11 19:34:29 +000070
Brian Gaekeb198ca32003-07-24 20:20:58 +000071 Memo[V] = name;
72 return name;
73}
74
Chris Lattner2b3860f2003-08-11 19:34:29 +000075Mangler::Mangler(Module &m, bool addUnderscorePrefix)
76 : M(m), AddUnderscorePrefix(addUnderscorePrefix) {
Brian Gaekeb198ca32003-07-24 20:20:58 +000077 // Calculate which global values have names that will collide when we throw
78 // away type information.
79 std::set<std::string> FoundNames;
80 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
81 if (I->hasName()) // If the global has a name...
82 if (FoundNames.count(I->getName())) // And the name is already used
83 MangledGlobals.insert(I); // Mangle the name
84 else
85 FoundNames.insert(I->getName()); // Otherwise, keep track of name
86
87 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
88 if (I->hasName()) // If the global has a name...
89 if (FoundNames.count(I->getName())) // And the name is already used
90 MangledGlobals.insert(I); // Mangle the name
91 else
92 FoundNames.insert(I->getName()); // Otherwise, keep track of name
93}
94