03111803186157d15338d72714fe130b9f42b19a
[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/DerivedTypes.h"
16 #include "llvm/Module.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, const char *Prefix) {
32   std::string Result;
33
34   // If X does not start with (char)1, add the prefix.
35   std::string::const_iterator I = X.begin();
36   if (*I != 1)
37     Result = Prefix;
38   else
39     ++I;  // Skip over the marker.
40   
41   // Mangle the first letter specially, don't allow numbers...
42   if (*I >= '0' && *I <= '9')
43     Result += MangleLetter(*I++);
44
45   for (std::string::const_iterator E = X.end(); I != E; ++I)
46     if ((*I < 'a' || *I > 'z') && (*I < 'A' || *I > 'Z') &&
47         (*I < '0' || *I > '9') && *I != '_' && *I != '$')
48       Result += MangleLetter(*I);
49     else
50       Result += *I;
51   return Result;
52 }
53
54 /// getTypeID - Return a unique ID for the specified LLVM type.
55 ///
56 unsigned Mangler::getTypeID(const Type *Ty) {
57   unsigned &E = TypeMap[Ty];
58   if (E == 0) E = ++TypeCounter;
59   return E;
60 }
61
62 std::string Mangler::getValueName(const Value *V) {
63   if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
64     return getValueName(GV);
65   
66   std::string &Name = Memo[V];
67   if (!Name.empty())
68     return Name;       // Return the already-computed name for V.
69   
70   // Always mangle local names.
71   Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
72   return Name;
73 }
74
75
76 std::string Mangler::getValueName(const GlobalValue *GV) {
77   // Check to see whether we've already named V.
78   std::string &Name = Memo[GV];
79   if (!Name.empty())
80     return Name;       // Return the already-computed name for V.
81
82   // Name mangling occurs as follows:
83   // - If V is an intrinsic function, do not change name at all
84   // - Otherwise, mangling occurs if global collides with existing name.
85   if (isa<Function>(GV) && cast<Function>(GV)->getIntrinsicID()) {
86     Name = GV->getName(); // Is an intrinsic function
87   } else if (!MangledGlobals.count(GV)) {
88     Name = makeNameProper(GV->getName(), Prefix);
89   } else {
90     unsigned TypeUniqueID = getTypeID(GV->getType());
91     Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
92   }
93
94   return Name;
95 }
96
97 void Mangler::InsertName(GlobalValue *GV,
98                          std::map<std::string, GlobalValue*> &Names) {
99   if (!GV->hasName()) {   // We must mangle unnamed globals.
100     MangledGlobals.insert(GV);
101     return;
102   }
103
104   // Figure out if this is already used.
105   GlobalValue *&ExistingValue = Names[GV->getName()];
106   if (!ExistingValue) {
107     ExistingValue = GV;
108   } else {
109     // If GV is external but the existing one is static, mangle the existing one
110     if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) {
111       MangledGlobals.insert(ExistingValue);
112       ExistingValue = GV;
113     } else {
114       // Otherwise, mangle GV
115       MangledGlobals.insert(GV);
116     }
117   }
118 }
119
120
121 Mangler::Mangler(Module &M, const char *prefix)
122   : Prefix(prefix), Count(0), TypeCounter(0) {
123   // Calculate which global values have names that will collide when we throw
124   // away type information.
125   std::map<std::string, GlobalValue*> Names;
126   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
127     InsertName(I, Names);
128   for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
129     InsertName(I, Names);
130 }