Changes For Bug 352
[oota-llvm.git] / lib / VMCore / 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 "llvm/ADT/StringExtras.h"
18 using namespace llvm;
19
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.
30 /// 
31 std::string Mangler::makeNameProper(const std::string &X) {
32   std::string Result;
33   
34   // Mangle the first letter specially, don't allow numbers...
35   if ((X[0] < 'a' || X[0] > 'z') && (X[0] < 'A' || X[0] > 'Z') && X[0] != '_')
36     Result += MangleLetter(X[0]);
37   else
38     Result += X[0];
39
40   for (std::string::const_iterator I = X.begin()+1, E = X.end(); I != E; ++I)
41     if ((*I < 'a' || *I > 'z') && (*I < 'A' || *I > 'Z') &&
42         (*I < '0' || *I > '9') && *I != '_')
43       Result += MangleLetter(*I);
44     else
45       Result += *I;
46   return Result;
47 }
48
49 /// getTypeID - Return a unique ID for the specified LLVM type.
50 ///
51 unsigned Mangler::getTypeID(const Type *Ty) {
52   unsigned &E = TypeMap[Ty];
53   if (E == 0) E = ++TypeCounter;
54   return E;
55 }
56
57
58 std::string Mangler::getValueName(const Value *V) {
59   // Check to see whether we've already named V.
60   ValueMap::iterator VI = Memo.find(V);
61   if (VI != Memo.end()) {
62     return VI->second; // Return the old name for V.
63   }
64
65   std::string name;
66   if (V->hasName()) { // Print out the label if it exists...
67     // Name mangling occurs as follows:
68     // - If V is an intrinsic function, do not change name at all
69     // - If V is not a global, mangling always occurs.
70     // - Otherwise, mangling occurs when any of the following are true:
71     //   1) V has internal linkage
72     //   2) V's name would collide if it is not mangled.
73     //
74     const GlobalValue* gv = dyn_cast<GlobalValue>(V);
75     if (gv && isa<Function>(gv) && cast<Function>(gv)->getIntrinsicID()) {
76       name = gv->getName(); // Is an intrinsic function
77     } else if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
78       name = Prefix + makeNameProper(gv->getName());
79     } else {
80       // Non-global, or global with internal linkage / colliding name
81       // -> mangle.
82       unsigned TypeUniqueID = getTypeID(V->getType());
83       name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(V->getName());
84     }
85   } else {
86     name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
87   }
88   
89   Memo[V] = name;
90   return name;
91 }
92
93 void Mangler::InsertName(GlobalValue *GV,
94                          std::map<std::string, GlobalValue*> &Names) {
95   if (!GV->hasName()) {   // We must mangle unnamed globals.
96     MangledGlobals.insert(GV);
97     return;
98   }
99
100   // Figure out if this is already used.
101   GlobalValue *&ExistingValue = Names[GV->getName()];
102   if (!ExistingValue) {
103     ExistingValue = GV;
104   } else {
105     // If GV is external but the existing one is static, mangle the existing one
106     if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) {
107       MangledGlobals.insert(ExistingValue);
108       ExistingValue = GV;
109     } else {
110       // Otherwise, mangle GV
111       MangledGlobals.insert(GV);
112     }
113   }
114 }
115
116
117 Mangler::Mangler(Module &m, const char *prefix)
118   : M(m), Prefix(prefix), TypeCounter(0), Count(0) {
119   // Calculate which global values have names that will collide when we throw
120   // away type information.
121   std::map<std::string, GlobalValue*> Names;
122   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
123     InsertName(I, Names);
124   for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
125     InsertName(I, Names);
126 }