Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / VMCore / Mangler.cpp
blob53719aff2cc1405a5461269adc461cfbf1a4f7b5
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.
12 //===----------------------------------------------------------------------===//
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;
20 static char HexDigit(int V) {
21 return V < 10 ? V+'0' : V+'A'-10;
24 static std::string MangleLetter(unsigned char C) {
25 char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
26 return Result;
29 /// makeNameProper - We don't want identifier names non-C-identifier characters
30 /// in them, so mangle them as appropriate.
31 ///
32 std::string Mangler::makeNameProper(const std::string &X, const char *Prefix) {
33 std::string Result;
34 if (X.empty()) return X; // Empty names are uniqued by the caller.
36 if (!UseQuotes) {
37 // If X does not start with (char)1, add the prefix.
38 std::string::const_iterator I = X.begin();
39 if (*I != 1)
40 Result = Prefix;
41 else
42 ++I; // Skip over the marker.
44 // Mangle the first letter specially, don't allow numbers.
45 if (*I >= '0' && *I <= '9')
46 Result += MangleLetter(*I++);
48 for (std::string::const_iterator E = X.end(); I != E; ++I) {
49 if (!isCharAcceptable(*I))
50 Result += MangleLetter(*I);
51 else
52 Result += *I;
54 } else {
55 bool NeedsQuotes = false;
57 std::string::const_iterator I = X.begin();
58 if (*I == 1)
59 ++I; // Skip over the marker.
61 // If the first character is a number, we need quotes.
62 if (*I >= '0' && *I <= '9')
63 NeedsQuotes = true;
65 // Do an initial scan of the string, checking to see if we need quotes or
66 // to escape a '"' or not.
67 if (!NeedsQuotes)
68 for (std::string::const_iterator E = X.end(); I != E; ++I)
69 if (!isCharAcceptable(*I)) {
70 NeedsQuotes = true;
71 break;
74 // In the common case, we don't need quotes. Handle this quickly.
75 if (!NeedsQuotes) {
76 if (*X.begin() != 1)
77 return Prefix+X;
78 else
79 return X.substr(1);
82 // Otherwise, construct the string the expensive way.
83 I = X.begin();
85 // If X does not start with (char)1, add the prefix.
86 if (*I != 1)
87 Result = Prefix;
88 else
89 ++I; // Skip the marker if present.
91 for (std::string::const_iterator E = X.end(); I != E; ++I) {
92 if (*I == '"')
93 Result += "_QQ_";
94 else
95 Result += *I;
97 Result = '"' + Result + '"';
99 return Result;
102 /// getTypeID - Return a unique ID for the specified LLVM type.
104 unsigned Mangler::getTypeID(const Type *Ty) {
105 unsigned &E = TypeMap[Ty];
106 if (E == 0) E = ++TypeCounter;
107 return E;
110 std::string Mangler::getValueName(const Value *V) {
111 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
112 return getValueName(GV);
114 std::string &Name = Memo[V];
115 if (!Name.empty())
116 return Name; // Return the already-computed name for V.
118 // Always mangle local names.
119 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
120 return Name;
124 std::string Mangler::getValueName(const GlobalValue *GV) {
125 // Check to see whether we've already named V.
126 std::string &Name = Memo[GV];
127 if (!Name.empty())
128 return Name; // Return the already-computed name for V.
130 // Name mangling occurs as follows:
131 // - If V is an intrinsic function, do not change name at all
132 // - Otherwise, mangling occurs if global collides with existing name.
133 if (isa<Function>(GV) && cast<Function>(GV)->getIntrinsicID()) {
134 Name = GV->getName(); // Is an intrinsic function
135 } else if (!GV->hasName()) {
136 // Must mangle the global into a unique ID.
137 unsigned TypeUniqueID = getTypeID(GV->getType());
138 static unsigned GlobalID = 0;
139 Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(GlobalID++);
140 } else if (!MangledGlobals.count(GV)) {
141 Name = makeNameProper(GV->getName(), Prefix);
142 } else {
143 unsigned TypeUniqueID = getTypeID(GV->getType());
144 Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
147 return Name;
150 void Mangler::InsertName(GlobalValue *GV,
151 std::map<std::string, GlobalValue*> &Names) {
152 if (!GV->hasName()) // We must mangle unnamed globals.
153 return;
155 // Figure out if this is already used.
156 GlobalValue *&ExistingValue = Names[GV->getName()];
157 if (!ExistingValue) {
158 ExistingValue = GV;
159 } else {
160 // If GV is external but the existing one is static, mangle the existing one
161 if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) {
162 MangledGlobals.insert(ExistingValue);
163 ExistingValue = GV;
164 } else if (GV->hasExternalLinkage() && ExistingValue->hasExternalLinkage()&&
165 GV->isExternal() && ExistingValue->isExternal()) {
166 // If the two globals both have external inkage, and are both external,
167 // don't mangle either of them, we just have some silly type mismatch.
168 } else {
169 // Otherwise, mangle GV
170 MangledGlobals.insert(GV);
176 Mangler::Mangler(Module &M, const char *prefix)
177 : Prefix(prefix), UseQuotes(false), Count(0), TypeCounter(0) {
178 std::fill(AcceptableChars,
179 AcceptableChars+sizeof(AcceptableChars)/sizeof(AcceptableChars[0]),
182 // Letters and numbers are acceptable.
183 for (unsigned char X = 'a'; X <= 'z'; ++X)
184 markCharAcceptable(X);
185 for (unsigned char X = 'A'; X <= 'Z'; ++X)
186 markCharAcceptable(X);
187 for (unsigned char X = '0'; X <= '9'; ++X)
188 markCharAcceptable(X);
190 // These chars are acceptable.
191 markCharAcceptable('_');
192 markCharAcceptable('$');
193 markCharAcceptable('.');
195 // Calculate which global values have names that will collide when we throw
196 // away type information.
197 std::map<std::string, GlobalValue*> Names;
198 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
199 InsertName(I, Names);
200 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
201 InsertName(I, Names);
204 // Cause this file to be linked in when Support/Mangler.h is #included
205 DEFINING_FILE_FOR(Mangler)