blob: 353732f9c1d4a69a64de709a8ae0ca307fe5cf88 [file] [log] [blame]
Brian Gaeked4dff192003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeked4dff192003-07-24 20:20:58 +00009//
10// Unified name mangler for CWriter and assembly backends.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaekeb0078fa2003-07-25 20:21:20 +000014#include "llvm/Support/Mangler.h"
Chris Lattner12b08312005-11-10 18:48:58 +000015#include "llvm/DerivedTypes.h"
Brian Gaeked4dff192003-07-24 20:20:58 +000016#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000017#include "llvm/ADT/StringExtras.h"
Chris Lattnerc9499b62003-12-14 21:35:53 +000018using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000019
Chris Lattner9527fea2003-08-24 21:08:38 +000020static char HexDigit(int V) {
21 return V < 10 ? V+'0' : V+'A'-10;
22}
23
24static std::string MangleLetter(unsigned char C) {
Chris Lattnere1d34bac2005-11-10 21:40:01 +000025 char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
26 return Result;
Chris Lattner9527fea2003-08-24 21:08:38 +000027}
28
29/// makeNameProper - We don't want identifier names non-C-identifier characters
30/// in them, so mangle them as appropriate.
Misha Brukmanb1c93172005-04-21 23:48:37 +000031///
Chris Lattnercc9c0332005-09-24 08:24:28 +000032std::string Mangler::makeNameProper(const std::string &X, const char *Prefix) {
Chris Lattner9527fea2003-08-24 21:08:38 +000033 std::string Result;
Chris Lattnere1d34bac2005-11-10 21:40:01 +000034 if (X.empty()) return X; // Empty names are uniqued by the caller.
Chris Lattnercc9c0332005-09-24 08:24:28 +000035
Chris Lattner721fc382006-09-07 18:20:41 +000036 // If PreserveAsmNames is set, names with asm identifiers are not modified.
37 if (PreserveAsmNames && X[0] == 1)
38 return X;
39
Chris Lattner4b155fa2005-11-10 19:30:07 +000040 if (!UseQuotes) {
41 // If X does not start with (char)1, add the prefix.
42 std::string::const_iterator I = X.begin();
43 if (*I != 1)
44 Result = Prefix;
Chris Lattner9527fea2003-08-24 21:08:38 +000045 else
Chris Lattner4b155fa2005-11-10 19:30:07 +000046 ++I; // Skip over the marker.
47
Chris Lattnere1d34bac2005-11-10 21:40:01 +000048 // Mangle the first letter specially, don't allow numbers.
Chris Lattner4b155fa2005-11-10 19:30:07 +000049 if (*I >= '0' && *I <= '9')
50 Result += MangleLetter(*I++);
51
Chris Lattnere1d34bac2005-11-10 21:40:01 +000052 for (std::string::const_iterator E = X.end(); I != E; ++I) {
53 if (!isCharAcceptable(*I))
Chris Lattner4b155fa2005-11-10 19:30:07 +000054 Result += MangleLetter(*I);
55 else
56 Result += *I;
Chris Lattnere1d34bac2005-11-10 21:40:01 +000057 }
Chris Lattner4b155fa2005-11-10 19:30:07 +000058 } else {
59 bool NeedsQuotes = false;
60
Chris Lattner4b155fa2005-11-10 19:30:07 +000061 std::string::const_iterator I = X.begin();
Chris Lattnerecc301d2005-11-10 23:24:26 +000062 if (*I == 1)
Chris Lattner4b155fa2005-11-10 19:30:07 +000063 ++I; // Skip over the marker.
Chris Lattnerecc301d2005-11-10 23:24:26 +000064
Chris Lattner4b155fa2005-11-10 19:30:07 +000065 // If the first character is a number, we need quotes.
66 if (*I >= '0' && *I <= '9')
67 NeedsQuotes = true;
68
Chris Lattner86164e62005-11-10 21:47:01 +000069 // Do an initial scan of the string, checking to see if we need quotes or
70 // to escape a '"' or not.
71 if (!NeedsQuotes)
72 for (std::string::const_iterator E = X.end(); I != E; ++I)
73 if (!isCharAcceptable(*I)) {
74 NeedsQuotes = true;
75 break;
76 }
77
78 // In the common case, we don't need quotes. Handle this quickly.
Chris Lattnerecc301d2005-11-10 23:24:26 +000079 if (!NeedsQuotes) {
80 if (*X.begin() != 1)
81 return Prefix+X;
82 else
83 return X.substr(1);
84 }
Chris Lattner86164e62005-11-10 21:47:01 +000085
86 // Otherwise, construct the string the expensive way.
87 I = X.begin();
Chris Lattnerecc301d2005-11-10 23:24:26 +000088
89 // If X does not start with (char)1, add the prefix.
90 if (*I != 1)
91 Result = Prefix;
92 else
93 ++I; // Skip the marker if present.
94
Chris Lattner86164e62005-11-10 21:47:01 +000095 for (std::string::const_iterator E = X.end(); I != E; ++I) {
Chris Lattner4b155fa2005-11-10 19:30:07 +000096 if (*I == '"')
97 Result += "_QQ_";
Chris Lattner86164e62005-11-10 21:47:01 +000098 else
Chris Lattner4b155fa2005-11-10 19:30:07 +000099 Result += *I;
Chris Lattner86164e62005-11-10 21:47:01 +0000100 }
101 Result = '"' + Result + '"';
Chris Lattner4b155fa2005-11-10 19:30:07 +0000102 }
Chris Lattner9527fea2003-08-24 21:08:38 +0000103 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +0000104}
105
Chris Lattner3a9fd4c2004-07-08 22:09:34 +0000106/// getTypeID - Return a unique ID for the specified LLVM type.
107///
108unsigned Mangler::getTypeID(const Type *Ty) {
109 unsigned &E = TypeMap[Ty];
110 if (E == 0) E = ++TypeCounter;
111 return E;
112}
113
Brian Gaeked4dff192003-07-24 20:20:58 +0000114std::string Mangler::getValueName(const Value *V) {
Chris Lattner12b08312005-11-10 18:48:58 +0000115 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
116 return getValueName(GV);
117
118 std::string &Name = Memo[V];
119 if (!Name.empty())
120 return Name; // Return the already-computed name for V.
121
122 // Always mangle local names.
123 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
124 return Name;
125}
126
127
128std::string Mangler::getValueName(const GlobalValue *GV) {
Brian Gaeked4dff192003-07-24 20:20:58 +0000129 // Check to see whether we've already named V.
Chris Lattner12b08312005-11-10 18:48:58 +0000130 std::string &Name = Memo[GV];
131 if (!Name.empty())
132 return Name; // Return the already-computed name for V.
Brian Gaeked4dff192003-07-24 20:20:58 +0000133
Chris Lattner12b08312005-11-10 18:48:58 +0000134 // Name mangling occurs as follows:
135 // - If V is an intrinsic function, do not change name at all
136 // - Otherwise, mangling occurs if global collides with existing name.
137 if (isa<Function>(GV) && cast<Function>(GV)->getIntrinsicID()) {
138 Name = GV->getName(); // Is an intrinsic function
Chris Lattner7c727b22005-11-15 01:32:03 +0000139 } else if (!GV->hasName()) {
140 // Must mangle the global into a unique ID.
141 unsigned TypeUniqueID = getTypeID(GV->getType());
142 static unsigned GlobalID = 0;
143 Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(GlobalID++);
Chris Lattner12b08312005-11-10 18:48:58 +0000144 } else if (!MangledGlobals.count(GV)) {
145 Name = makeNameProper(GV->getName(), Prefix);
Brian Gaeked4dff192003-07-24 20:20:58 +0000146 } else {
Chris Lattner12b08312005-11-10 18:48:58 +0000147 unsigned TypeUniqueID = getTypeID(GV->getType());
148 Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
Brian Gaeked4dff192003-07-24 20:20:58 +0000149 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000150
Chris Lattner12b08312005-11-10 18:48:58 +0000151 return Name;
Brian Gaeked4dff192003-07-24 20:20:58 +0000152}
153
Chris Lattner77687a92004-02-14 00:30:23 +0000154void Mangler::InsertName(GlobalValue *GV,
155 std::map<std::string, GlobalValue*> &Names) {
Chris Lattner7c727b22005-11-15 01:32:03 +0000156 if (!GV->hasName()) // We must mangle unnamed globals.
Chris Lattner77687a92004-02-14 00:30:23 +0000157 return;
Chris Lattner77687a92004-02-14 00:30:23 +0000158
159 // Figure out if this is already used.
160 GlobalValue *&ExistingValue = Names[GV->getName()];
161 if (!ExistingValue) {
162 ExistingValue = GV;
163 } else {
164 // If GV is external but the existing one is static, mangle the existing one
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000165 if ((GV->hasExternalLinkage() || GV->hasDLLImportLinkage()) &&
166 !(ExistingValue->hasExternalLinkage() || ExistingValue->hasDLLImportLinkage())) {
Chris Lattner77687a92004-02-14 00:30:23 +0000167 MangledGlobals.insert(ExistingValue);
168 ExistingValue = GV;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000169 } else if ((GV->hasExternalLinkage() ||
170 GV->hasDLLImportLinkage()) &&
171 (ExistingValue->hasExternalLinkage() ||
172 ExistingValue->hasDLLImportLinkage()) &&
Reid Spencer5301e7c2007-01-30 20:08:39 +0000173 GV->isDeclaration() &&
174 ExistingValue->isDeclaration()) {
Chris Lattnereea0f012006-02-13 21:43:26 +0000175 // If the two globals both have external inkage, and are both external,
176 // don't mangle either of them, we just have some silly type mismatch.
Chris Lattner77687a92004-02-14 00:30:23 +0000177 } else {
178 // Otherwise, mangle GV
179 MangledGlobals.insert(GV);
180 }
181 }
182}
183
184
Chris Lattner9fa3bcc2005-11-10 19:02:18 +0000185Mangler::Mangler(Module &M, const char *prefix)
Chris Lattner721fc382006-09-07 18:20:41 +0000186 : Prefix(prefix), UseQuotes(false), PreserveAsmNames(false),
187 Count(0), TypeCounter(0) {
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000188 std::fill(AcceptableChars,
189 AcceptableChars+sizeof(AcceptableChars)/sizeof(AcceptableChars[0]),
190 0);
191
192 // Letters and numbers are acceptable.
193 for (unsigned char X = 'a'; X <= 'z'; ++X)
194 markCharAcceptable(X);
195 for (unsigned char X = 'A'; X <= 'Z'; ++X)
196 markCharAcceptable(X);
197 for (unsigned char X = '0'; X <= '9'; ++X)
198 markCharAcceptable(X);
199
200 // These chars are acceptable.
201 markCharAcceptable('_');
202 markCharAcceptable('$');
203 markCharAcceptable('.');
204
Brian Gaeked4dff192003-07-24 20:20:58 +0000205 // Calculate which global values have names that will collide when we throw
206 // away type information.
Chris Lattner77687a92004-02-14 00:30:23 +0000207 std::map<std::string, GlobalValue*> Names;
Brian Gaeked4dff192003-07-24 20:20:58 +0000208 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner77687a92004-02-14 00:30:23 +0000209 InsertName(I, Names);
Chris Lattner531f9e92005-03-15 04:54:21 +0000210 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattner77687a92004-02-14 00:30:23 +0000211 InsertName(I, Names);
Brian Gaeked4dff192003-07-24 20:20:58 +0000212}
Reid Spencer5113dc52006-06-07 23:03:13 +0000213
214// Cause this file to be linked in when Support/Mangler.h is #included
215DEFINING_FILE_FOR(Mangler)