Add support for a marker byte that indicates that we shouldn't add the user
[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, 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 != '_')
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
63 std::string Mangler::getValueName(const Value *V) {
64   // Check to see whether we've already named V.
65   ValueMap::iterator VI = Memo.find(V);
66   if (VI != Memo.end()) {
67     return VI->second; // Return the old name for V.
68   }
69
70   std::string name;
71   if (V->hasName()) { // Print out the label if it exists...
72     // Name mangling occurs as follows:
73     // - If V is an intrinsic function, do not change name at all
74     // - If V is not a global, mangling always occurs.
75     // - Otherwise, mangling occurs when any of the following are true:
76     //   1) V has internal linkage
77     //   2) V's name would collide if it is not mangled.
78     //
79     const GlobalValue* gv = dyn_cast<GlobalValue>(V);
80     if (gv && isa<Function>(gv) && cast<Function>(gv)->getIntrinsicID()) {
81       name = gv->getName(); // Is an intrinsic function
82     } else if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
83       name = makeNameProper(gv->getName(), Prefix);
84     } else {
85       // Non-global, or global with internal linkage / colliding name
86       // -> mangle.
87       unsigned TypeUniqueID = getTypeID(V->getType());
88       name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(V->getName());
89     }
90   } else {
91     name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
92   }
93
94   Memo[V] = name;
95   return name;
96 }
97
98 void Mangler::InsertName(GlobalValue *GV,
99                          std::map<std::string, GlobalValue*> &Names) {
100   if (!GV->hasName()) {   // We must mangle unnamed globals.
101     MangledGlobals.insert(GV);
102     return;
103   }
104
105   // Figure out if this is already used.
106   GlobalValue *&ExistingValue = Names[GV->getName()];
107   if (!ExistingValue) {
108     ExistingValue = GV;
109   } else {
110     // If GV is external but the existing one is static, mangle the existing one
111     if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) {
112       MangledGlobals.insert(ExistingValue);
113       ExistingValue = GV;
114     } else {
115       // Otherwise, mangle GV
116       MangledGlobals.insert(GV);
117     }
118   }
119 }
120
121
122 Mangler::Mangler(Module &m, const char *prefix)
123   : M(m), Prefix(prefix), TypeCounter(0), Count(0) {
124   // Calculate which global values have names that will collide when we throw
125   // away type information.
126   std::map<std::string, GlobalValue*> Names;
127   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
128     InsertName(I, Names);
129   for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
130     InsertName(I, Names);
131 }