665cb7388c117b4b78bc936870bef4ffde468740
[oota-llvm.git] / lib / Support / 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 /// makeNameProper - We don't want identifier names with ., space, or
13 /// - in them, so we mangle these characters into the strings "d_",
14 /// "s_", and "D_", respectively.
15 /// 
16 std::string Mangler::makeNameProper(const std::string &x) {
17   std::string tmp;
18   for (std::string::const_iterator sI = x.begin(), sEnd = x.end();
19        sI != sEnd; sI++)
20     switch (*sI) {
21     case '.': tmp += "d_"; break;
22     case ' ': tmp += "s_"; break;
23     case '-': tmp += "D_"; break;
24     default:  tmp += *sI;
25     }
26   return tmp;
27 }
28
29 std::string Mangler::getValueName(const Value *V) {
30   // Check to see whether we've already named V.
31   ValueMap::iterator VI = Memo.find(V);
32   if (VI != Memo.end()) {
33     return VI->second; // Return the old name for V.
34   }
35
36   std::string name;
37   if (V->hasName()) { // Print out the label if it exists...
38     // Name mangling occurs as follows:
39     // - If V is not a global, mangling always occurs.
40     // - Otherwise, mangling occurs when any of the following are true:
41     //   1) V has internal linkage
42     //   2) V's name would collide if it is not mangled.
43     //
44     const GlobalValue* gv = dyn_cast<GlobalValue>(V);
45     if(gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
46       name = makeNameProper(gv->getName());
47     } else {
48       // Non-global, or global with internal linkage / colliding name
49       // -> mangle.
50       name = "l" + utostr(V->getType()->getUniqueID()) + "_" +
51         makeNameProper(V->getName());      
52     }
53   } else {
54     name = "ltmp_" + utostr(Count++) + "_"
55       + utostr(V->getType()->getUniqueID());
56   }
57   Memo[V] = name;
58   return name;
59 }
60
61 Mangler::Mangler(Module &M_) : M(M_)
62 {
63   // Calculate which global values have names that will collide when we throw
64   // away type information.
65   std::set<std::string> FoundNames;
66   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
67     if (I->hasName())                      // If the global has a name...
68       if (FoundNames.count(I->getName()))  // And the name is already used
69         MangledGlobals.insert(I);          // Mangle the name
70       else
71         FoundNames.insert(I->getName());   // Otherwise, keep track of name
72
73   for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
74     if (I->hasName())                      // If the global has a name...
75       if (FoundNames.count(I->getName()))  // And the name is already used
76         MangledGlobals.insert(I);          // Mangle the name
77       else
78         FoundNames.insert(I->getName());   // Otherwise, keep track of name
79 }
80