d9186a9c938f9777ce3070089cc24ecd6a348a58
[oota-llvm.git] / lib / VMCore / Mangler.cpp
1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2 //
3 // Unified name mangler for CWriter and assembly backends.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Support/Mangler.h"
8 #include "llvm/Module.h"
9 #include "llvm/Type.h"
10 #include "Support/StringExtras.h"
11
12 static char HexDigit(int V) {
13   return V < 10 ? V+'0' : V+'A'-10;
14 }
15
16 static 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.
22 /// 
23 std::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;
39 }
40
41 std::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);
57     if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
58       name = makeNameProper(gv->getName());
59       if (AddUnderscorePrefix) name = "_" + name;
60     } 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 {
67     name = "ltmp_" + utostr(Count++) + "_"
68       + utostr(V->getType()->getUniqueID());
69   }
70   
71   Memo[V] = name;
72   return name;
73 }
74
75 Mangler::Mangler(Module &m, bool addUnderscorePrefix)
76   : M(m), AddUnderscorePrefix(addUnderscorePrefix) {
77   // 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