Added LLVM project notice to the top of every C++ source file.
[oota-llvm.git] / lib / Support / Mangler.cpp
1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
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 //===----------------------------------------------------------------------===//
9 //
10 // Unified name mangler for CWriter and assembly backends.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/Mangler.h"
15 #include "llvm/Module.h"
16 #include "llvm/Type.h"
17 #include "Support/StringExtras.h"
18
19 static char HexDigit(int V) {
20   return V < 10 ? V+'0' : V+'A'-10;
21 }
22
23 static std::string MangleLetter(unsigned char C) {
24   return std::string("_")+HexDigit(C >> 4) + HexDigit(C & 15) + "_";
25 }
26
27 /// makeNameProper - We don't want identifier names non-C-identifier characters
28 /// in them, so mangle them as appropriate.
29 /// 
30 std::string Mangler::makeNameProper(const std::string &X) {
31   std::string Result;
32   
33   // Mangle the first letter specially, don't allow numbers...
34   if ((X[0] < 'a' || X[0] > 'z') && (X[0] < 'A' || X[0] > 'Z') && X[0] != '_')
35     Result += MangleLetter(X[0]);
36   else
37     Result += X[0];
38
39   for (std::string::const_iterator I = X.begin()+1, E = X.end(); I != E; ++I)
40     if ((*I < 'a' || *I > 'z') && (*I < 'A' || *I > 'Z') &&
41         (*I < '0' || *I > '9') && *I != '_')
42       Result += MangleLetter(*I);
43     else
44       Result += *I;
45   return Result;
46 }
47
48 std::string Mangler::getValueName(const Value *V) {
49   // Check to see whether we've already named V.
50   ValueMap::iterator VI = Memo.find(V);
51   if (VI != Memo.end()) {
52     return VI->second; // Return the old name for V.
53   }
54
55   std::string name;
56   if (V->hasName()) { // Print out the label if it exists...
57     // Name mangling occurs as follows:
58     // - If V is not a global, mangling always occurs.
59     // - Otherwise, mangling occurs when any of the following are true:
60     //   1) V has internal linkage
61     //   2) V's name would collide if it is not mangled.
62     //
63     const GlobalValue* gv = dyn_cast<GlobalValue>(V);
64     if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
65       name = makeNameProper(gv->getName());
66       if (AddUnderscorePrefix) name = "_" + name;
67     } else {
68       // Non-global, or global with internal linkage / colliding name
69       // -> mangle.
70       name = "l" + utostr(V->getType()->getUniqueID()) + "_" +
71         makeNameProper(V->getName());      
72     }
73   } else {
74     name = "ltmp_" + utostr(Count++) + "_"
75       + utostr(V->getType()->getUniqueID());
76   }
77   
78   Memo[V] = name;
79   return name;
80 }
81
82 Mangler::Mangler(Module &m, bool addUnderscorePrefix)
83   : M(m), AddUnderscorePrefix(addUnderscorePrefix) {
84   // Calculate which global values have names that will collide when we throw
85   // away type information.
86   std::set<std::string> FoundNames;
87   for (Module::iterator I = M.begin(), E = M.end(); 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   for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
95     if (I->hasName())                      // If the global has a name...
96       if (FoundNames.count(I->getName()))  // And the name is already used
97         MangledGlobals.insert(I);          // Mangle the name
98       else
99         FoundNames.insert(I->getName());   // Otherwise, keep track of name
100 }
101