blob: 50fa3c1885b9c94db263acb98af008fde1eb311d [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//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Owen Andersone2f23a32007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattnerc9499b62003-12-14 21:35:53 +000019using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000020
Chris Lattner9527fea2003-08-24 21:08:38 +000021static char HexDigit(int V) {
22 return V < 10 ? V+'0' : V+'A'-10;
23}
24
25static std::string MangleLetter(unsigned char C) {
Chris Lattnere1d34bac2005-11-10 21:40:01 +000026 char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
27 return Result;
Chris Lattner9527fea2003-08-24 21:08:38 +000028}
29
30/// makeNameProper - We don't want identifier names non-C-identifier characters
31/// in them, so mangle them as appropriate.
Misha Brukmanb1c93172005-04-21 23:48:37 +000032///
Chris Lattnercc9c0332005-09-24 08:24:28 +000033std::string Mangler::makeNameProper(const std::string &X, const char *Prefix) {
Chris Lattner9527fea2003-08-24 21:08:38 +000034 std::string Result;
Chris Lattnere1d34bac2005-11-10 21:40:01 +000035 if (X.empty()) return X; // Empty names are uniqued by the caller.
Chris Lattnercc9c0332005-09-24 08:24:28 +000036
Chris Lattner721fc382006-09-07 18:20:41 +000037 // If PreserveAsmNames is set, names with asm identifiers are not modified.
38 if (PreserveAsmNames && X[0] == 1)
39 return X;
40
Chris Lattner4b155fa2005-11-10 19:30:07 +000041 if (!UseQuotes) {
42 // If X does not start with (char)1, add the prefix.
43 std::string::const_iterator I = X.begin();
44 if (*I != 1)
45 Result = Prefix;
Chris Lattner9527fea2003-08-24 21:08:38 +000046 else
Chris Lattner4b155fa2005-11-10 19:30:07 +000047 ++I; // Skip over the marker.
48
Chris Lattnere1d34bac2005-11-10 21:40:01 +000049 // Mangle the first letter specially, don't allow numbers.
Chris Lattner4b155fa2005-11-10 19:30:07 +000050 if (*I >= '0' && *I <= '9')
51 Result += MangleLetter(*I++);
52
Chris Lattnere1d34bac2005-11-10 21:40:01 +000053 for (std::string::const_iterator E = X.end(); I != E; ++I) {
54 if (!isCharAcceptable(*I))
Chris Lattner4b155fa2005-11-10 19:30:07 +000055 Result += MangleLetter(*I);
56 else
57 Result += *I;
Chris Lattnere1d34bac2005-11-10 21:40:01 +000058 }
Chris Lattner4b155fa2005-11-10 19:30:07 +000059 } else {
60 bool NeedsQuotes = false;
61
Chris Lattner4b155fa2005-11-10 19:30:07 +000062 std::string::const_iterator I = X.begin();
Chris Lattnerecc301d2005-11-10 23:24:26 +000063 if (*I == 1)
Chris Lattner4b155fa2005-11-10 19:30:07 +000064 ++I; // Skip over the marker.
Chris Lattnerecc301d2005-11-10 23:24:26 +000065
Chris Lattner4b155fa2005-11-10 19:30:07 +000066 // If the first character is a number, we need quotes.
67 if (*I >= '0' && *I <= '9')
68 NeedsQuotes = true;
69
Chris Lattner86164e62005-11-10 21:47:01 +000070 // Do an initial scan of the string, checking to see if we need quotes or
71 // to escape a '"' or not.
72 if (!NeedsQuotes)
73 for (std::string::const_iterator E = X.end(); I != E; ++I)
74 if (!isCharAcceptable(*I)) {
75 NeedsQuotes = true;
76 break;
77 }
78
79 // In the common case, we don't need quotes. Handle this quickly.
Chris Lattnerecc301d2005-11-10 23:24:26 +000080 if (!NeedsQuotes) {
81 if (*X.begin() != 1)
82 return Prefix+X;
83 else
84 return X.substr(1);
85 }
Chris Lattner86164e62005-11-10 21:47:01 +000086
87 // Otherwise, construct the string the expensive way.
88 I = X.begin();
Chris Lattnerecc301d2005-11-10 23:24:26 +000089
90 // If X does not start with (char)1, add the prefix.
91 if (*I != 1)
92 Result = Prefix;
93 else
94 ++I; // Skip the marker if present.
95
Chris Lattner86164e62005-11-10 21:47:01 +000096 for (std::string::const_iterator E = X.end(); I != E; ++I) {
Chris Lattner4b155fa2005-11-10 19:30:07 +000097 if (*I == '"')
98 Result += "_QQ_";
Chris Lattner536dded2008-02-15 18:54:56 +000099 else if (*I == '\n')
100 Result += "_NL_";
Chris Lattner86164e62005-11-10 21:47:01 +0000101 else
Chris Lattner4b155fa2005-11-10 19:30:07 +0000102 Result += *I;
Chris Lattner86164e62005-11-10 21:47:01 +0000103 }
104 Result = '"' + Result + '"';
Chris Lattner4b155fa2005-11-10 19:30:07 +0000105 }
Chris Lattner9527fea2003-08-24 21:08:38 +0000106 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +0000107}
108
Chris Lattner3a9fd4c2004-07-08 22:09:34 +0000109/// getTypeID - Return a unique ID for the specified LLVM type.
110///
111unsigned Mangler::getTypeID(const Type *Ty) {
112 unsigned &E = TypeMap[Ty];
113 if (E == 0) E = ++TypeCounter;
114 return E;
115}
116
Brian Gaeked4dff192003-07-24 20:20:58 +0000117std::string Mangler::getValueName(const Value *V) {
Chris Lattner12b08312005-11-10 18:48:58 +0000118 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
119 return getValueName(GV);
120
121 std::string &Name = Memo[V];
122 if (!Name.empty())
123 return Name; // Return the already-computed name for V.
124
125 // Always mangle local names.
126 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
127 return Name;
128}
129
130
Bill Wendling5752f692007-09-17 22:39:32 +0000131std::string Mangler::getValueName(const GlobalValue *GV, const char * Suffix) {
Brian Gaeked4dff192003-07-24 20:20:58 +0000132 // Check to see whether we've already named V.
Chris Lattner12b08312005-11-10 18:48:58 +0000133 std::string &Name = Memo[GV];
134 if (!Name.empty())
135 return Name; // Return the already-computed name for V.
Brian Gaeked4dff192003-07-24 20:20:58 +0000136
Chris Lattner12b08312005-11-10 18:48:58 +0000137 // Name mangling occurs as follows:
138 // - If V is an intrinsic function, do not change name at all
139 // - Otherwise, mangling occurs if global collides with existing name.
Duncan Sands38ef3a82007-12-03 20:06:50 +0000140 if (isa<Function>(GV) && cast<Function>(GV)->isIntrinsic()) {
Chris Lattner12b08312005-11-10 18:48:58 +0000141 Name = GV->getName(); // Is an intrinsic function
Chris Lattner7c727b22005-11-15 01:32:03 +0000142 } else if (!GV->hasName()) {
143 // Must mangle the global into a unique ID.
144 unsigned TypeUniqueID = getTypeID(GV->getType());
145 static unsigned GlobalID = 0;
146 Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(GlobalID++);
Chris Lattner12b08312005-11-10 18:48:58 +0000147 } else if (!MangledGlobals.count(GV)) {
Bill Wendling5752f692007-09-17 22:39:32 +0000148 Name = makeNameProper(GV->getName() + Suffix, Prefix);
Brian Gaeked4dff192003-07-24 20:20:58 +0000149 } else {
Chris Lattner12b08312005-11-10 18:48:58 +0000150 unsigned TypeUniqueID = getTypeID(GV->getType());
151 Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
Brian Gaeked4dff192003-07-24 20:20:58 +0000152 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000153
Chris Lattner12b08312005-11-10 18:48:58 +0000154 return Name;
Brian Gaeked4dff192003-07-24 20:20:58 +0000155}
156
Chris Lattner77687a92004-02-14 00:30:23 +0000157void Mangler::InsertName(GlobalValue *GV,
158 std::map<std::string, GlobalValue*> &Names) {
Chris Lattner7c727b22005-11-15 01:32:03 +0000159 if (!GV->hasName()) // We must mangle unnamed globals.
Chris Lattner77687a92004-02-14 00:30:23 +0000160 return;
Chris Lattner77687a92004-02-14 00:30:23 +0000161
162 // Figure out if this is already used.
163 GlobalValue *&ExistingValue = Names[GV->getName()];
164 if (!ExistingValue) {
165 ExistingValue = GV;
166 } else {
167 // If GV is external but the existing one is static, mangle the existing one
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000168 if ((GV->hasExternalLinkage() || GV->hasDLLImportLinkage()) &&
Gabor Greif697e94c2008-05-15 10:04:30 +0000169 !(ExistingValue->hasExternalLinkage()
170 || ExistingValue->hasDLLImportLinkage())) {
Chris Lattner77687a92004-02-14 00:30:23 +0000171 MangledGlobals.insert(ExistingValue);
172 ExistingValue = GV;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000173 } else if ((GV->hasExternalLinkage() ||
174 GV->hasDLLImportLinkage()) &&
175 (ExistingValue->hasExternalLinkage() ||
176 ExistingValue->hasDLLImportLinkage()) &&
Reid Spencer5301e7c2007-01-30 20:08:39 +0000177 GV->isDeclaration() &&
178 ExistingValue->isDeclaration()) {
Chris Lattnereea0f012006-02-13 21:43:26 +0000179 // If the two globals both have external inkage, and are both external,
180 // don't mangle either of them, we just have some silly type mismatch.
Chris Lattner77687a92004-02-14 00:30:23 +0000181 } else {
182 // Otherwise, mangle GV
183 MangledGlobals.insert(GV);
184 }
185 }
186}
187
188
Chris Lattner9fa3bcc2005-11-10 19:02:18 +0000189Mangler::Mangler(Module &M, const char *prefix)
Chris Lattner721fc382006-09-07 18:20:41 +0000190 : Prefix(prefix), UseQuotes(false), PreserveAsmNames(false),
191 Count(0), TypeCounter(0) {
Owen Andersone2f23a32007-09-07 04:06:50 +0000192 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000193
194 // Letters and numbers are acceptable.
195 for (unsigned char X = 'a'; X <= 'z'; ++X)
196 markCharAcceptable(X);
197 for (unsigned char X = 'A'; X <= 'Z'; ++X)
198 markCharAcceptable(X);
199 for (unsigned char X = '0'; X <= '9'; ++X)
200 markCharAcceptable(X);
201
202 // These chars are acceptable.
203 markCharAcceptable('_');
204 markCharAcceptable('$');
205 markCharAcceptable('.');
206
Brian Gaeked4dff192003-07-24 20:20:58 +0000207 // Calculate which global values have names that will collide when we throw
208 // away type information.
Chris Lattner77687a92004-02-14 00:30:23 +0000209 std::map<std::string, GlobalValue*> Names;
Brian Gaeked4dff192003-07-24 20:20:58 +0000210 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner77687a92004-02-14 00:30:23 +0000211 InsertName(I, Names);
Gabor Greif697e94c2008-05-15 10:04:30 +0000212 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
213 I != E;
214 ++I)
Chris Lattner77687a92004-02-14 00:30:23 +0000215 InsertName(I, Names);
Brian Gaeked4dff192003-07-24 20:20:58 +0000216}