blob: d86d9c0da6c95ffd93c121d1823047d98107a194 [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"
Evan Cheng0e697342008-07-10 00:04:23 +000019#include "llvm/ADT/StringMap.h"
Chris Lattnerc9499b62003-12-14 21:35:53 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Chris Lattner9527fea2003-08-24 21:08:38 +000022static char HexDigit(int V) {
23 return V < 10 ? V+'0' : V+'A'-10;
24}
25
26static std::string MangleLetter(unsigned char C) {
Chris Lattnere1d34bac2005-11-10 21:40:01 +000027 char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
28 return Result;
Chris Lattner9527fea2003-08-24 21:08:38 +000029}
30
31/// makeNameProper - We don't want identifier names non-C-identifier characters
32/// in them, so mangle them as appropriate.
Misha Brukmanb1c93172005-04-21 23:48:37 +000033///
Chris Lattnercc9c0332005-09-24 08:24:28 +000034std::string Mangler::makeNameProper(const std::string &X, const char *Prefix) {
Chris Lattner9527fea2003-08-24 21:08:38 +000035 std::string Result;
Chris Lattnere1d34bac2005-11-10 21:40:01 +000036 if (X.empty()) return X; // Empty names are uniqued by the caller.
Chris Lattnercc9c0332005-09-24 08:24:28 +000037
Chris Lattner721fc382006-09-07 18:20:41 +000038 // If PreserveAsmNames is set, names with asm identifiers are not modified.
39 if (PreserveAsmNames && X[0] == 1)
40 return X;
41
Chris Lattner4b155fa2005-11-10 19:30:07 +000042 if (!UseQuotes) {
43 // If X does not start with (char)1, add the prefix.
44 std::string::const_iterator I = X.begin();
45 if (*I != 1)
46 Result = Prefix;
Chris Lattner9527fea2003-08-24 21:08:38 +000047 else
Chris Lattner4b155fa2005-11-10 19:30:07 +000048 ++I; // Skip over the marker.
49
Chris Lattnere1d34bac2005-11-10 21:40:01 +000050 // Mangle the first letter specially, don't allow numbers.
Chris Lattner4b155fa2005-11-10 19:30:07 +000051 if (*I >= '0' && *I <= '9')
52 Result += MangleLetter(*I++);
53
Chris Lattnere1d34bac2005-11-10 21:40:01 +000054 for (std::string::const_iterator E = X.end(); I != E; ++I) {
55 if (!isCharAcceptable(*I))
Chris Lattner4b155fa2005-11-10 19:30:07 +000056 Result += MangleLetter(*I);
57 else
58 Result += *I;
Chris Lattnere1d34bac2005-11-10 21:40:01 +000059 }
Chris Lattner4b155fa2005-11-10 19:30:07 +000060 } else {
61 bool NeedsQuotes = false;
62
Chris Lattner4b155fa2005-11-10 19:30:07 +000063 std::string::const_iterator I = X.begin();
Chris Lattnerecc301d2005-11-10 23:24:26 +000064 if (*I == 1)
Chris Lattner4b155fa2005-11-10 19:30:07 +000065 ++I; // Skip over the marker.
Chris Lattnerecc301d2005-11-10 23:24:26 +000066
Chris Lattner4b155fa2005-11-10 19:30:07 +000067 // If the first character is a number, we need quotes.
68 if (*I >= '0' && *I <= '9')
69 NeedsQuotes = true;
70
Chris Lattner86164e62005-11-10 21:47:01 +000071 // Do an initial scan of the string, checking to see if we need quotes or
72 // to escape a '"' or not.
73 if (!NeedsQuotes)
74 for (std::string::const_iterator E = X.end(); I != E; ++I)
75 if (!isCharAcceptable(*I)) {
76 NeedsQuotes = true;
77 break;
78 }
79
80 // In the common case, we don't need quotes. Handle this quickly.
Chris Lattnerecc301d2005-11-10 23:24:26 +000081 if (!NeedsQuotes) {
82 if (*X.begin() != 1)
83 return Prefix+X;
84 else
85 return X.substr(1);
86 }
Chris Lattner86164e62005-11-10 21:47:01 +000087
88 // Otherwise, construct the string the expensive way.
89 I = X.begin();
Chris Lattnerecc301d2005-11-10 23:24:26 +000090
91 // If X does not start with (char)1, add the prefix.
92 if (*I != 1)
93 Result = Prefix;
94 else
95 ++I; // Skip the marker if present.
96
Chris Lattner86164e62005-11-10 21:47:01 +000097 for (std::string::const_iterator E = X.end(); I != E; ++I) {
Chris Lattner4b155fa2005-11-10 19:30:07 +000098 if (*I == '"')
99 Result += "_QQ_";
Chris Lattner536dded2008-02-15 18:54:56 +0000100 else if (*I == '\n')
101 Result += "_NL_";
Chris Lattner86164e62005-11-10 21:47:01 +0000102 else
Chris Lattner4b155fa2005-11-10 19:30:07 +0000103 Result += *I;
Chris Lattner86164e62005-11-10 21:47:01 +0000104 }
105 Result = '"' + Result + '"';
Chris Lattner4b155fa2005-11-10 19:30:07 +0000106 }
Chris Lattner9527fea2003-08-24 21:08:38 +0000107 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +0000108}
109
Chris Lattner3a9fd4c2004-07-08 22:09:34 +0000110/// getTypeID - Return a unique ID for the specified LLVM type.
111///
112unsigned Mangler::getTypeID(const Type *Ty) {
113 unsigned &E = TypeMap[Ty];
114 if (E == 0) E = ++TypeCounter;
115 return E;
116}
117
Brian Gaeked4dff192003-07-24 20:20:58 +0000118std::string Mangler::getValueName(const Value *V) {
Chris Lattner12b08312005-11-10 18:48:58 +0000119 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
120 return getValueName(GV);
121
122 std::string &Name = Memo[V];
123 if (!Name.empty())
124 return Name; // Return the already-computed name for V.
125
126 // Always mangle local names.
127 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
128 return Name;
129}
130
131
Bill Wendling5752f692007-09-17 22:39:32 +0000132std::string Mangler::getValueName(const GlobalValue *GV, const char * Suffix) {
Brian Gaeked4dff192003-07-24 20:20:58 +0000133 // Check to see whether we've already named V.
Chris Lattner12b08312005-11-10 18:48:58 +0000134 std::string &Name = Memo[GV];
135 if (!Name.empty())
136 return Name; // Return the already-computed name for V.
Brian Gaeked4dff192003-07-24 20:20:58 +0000137
Chris Lattner12b08312005-11-10 18:48:58 +0000138 // Name mangling occurs as follows:
139 // - If V is an intrinsic function, do not change name at all
140 // - Otherwise, mangling occurs if global collides with existing name.
Duncan Sands38ef3a82007-12-03 20:06:50 +0000141 if (isa<Function>(GV) && cast<Function>(GV)->isIntrinsic()) {
Evan Cheng0e697342008-07-10 00:04:23 +0000142 Name = GV->getNameStart(); // Is an intrinsic function
Chris Lattner7c727b22005-11-15 01:32:03 +0000143 } else if (!GV->hasName()) {
144 // Must mangle the global into a unique ID.
145 unsigned TypeUniqueID = getTypeID(GV->getType());
146 static unsigned GlobalID = 0;
147 Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(GlobalID++);
Chris Lattner12b08312005-11-10 18:48:58 +0000148 } else if (!MangledGlobals.count(GV)) {
Bill Wendling5752f692007-09-17 22:39:32 +0000149 Name = makeNameProper(GV->getName() + Suffix, Prefix);
Brian Gaeked4dff192003-07-24 20:20:58 +0000150 } else {
Chris Lattner12b08312005-11-10 18:48:58 +0000151 unsigned TypeUniqueID = getTypeID(GV->getType());
152 Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
Brian Gaeked4dff192003-07-24 20:20:58 +0000153 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000154
Chris Lattner12b08312005-11-10 18:48:58 +0000155 return Name;
Brian Gaeked4dff192003-07-24 20:20:58 +0000156}
157
Evan Cheng0e697342008-07-10 00:04:23 +0000158static void InsertName(GlobalValue *GV, StringMap<GlobalValue*> &Names,
159 SmallPtrSet<const GlobalValue*, 16> &MangledGlobals) {
Chris Lattner7c727b22005-11-15 01:32:03 +0000160 if (!GV->hasName()) // We must mangle unnamed globals.
Chris Lattner77687a92004-02-14 00:30:23 +0000161 return;
Chris Lattner77687a92004-02-14 00:30:23 +0000162
163 // Figure out if this is already used.
Evan Cheng0e697342008-07-10 00:04:23 +0000164 GlobalValue *&ExistingValue = Names[GV->getNameStart()];
Chris Lattner77687a92004-02-14 00:30:23 +0000165 if (!ExistingValue) {
166 ExistingValue = GV;
Evan Cheng0e697342008-07-10 00:04:23 +0000167 return;
168 }
169
170 // If GV is external but the existing one is static, mangle the existing one
171 if ((GV->hasExternalLinkage() || GV->hasDLLImportLinkage()) &&
172 !(ExistingValue->hasExternalLinkage()
173 || ExistingValue->hasDLLImportLinkage())) {
174 MangledGlobals.insert(ExistingValue);
175 ExistingValue = GV;
176 } else if ((GV->hasExternalLinkage() ||
177 GV->hasDLLImportLinkage()) &&
178 (ExistingValue->hasExternalLinkage() ||
179 ExistingValue->hasDLLImportLinkage()) &&
180 GV->isDeclaration() &&
181 ExistingValue->isDeclaration()) {
182 // If the two globals both have external inkage, and are both external,
183 // don't mangle either of them, we just have some silly type mismatch.
Chris Lattner77687a92004-02-14 00:30:23 +0000184 } else {
Evan Cheng0e697342008-07-10 00:04:23 +0000185 // Otherwise, mangle GV
186 MangledGlobals.insert(GV);
Chris Lattner77687a92004-02-14 00:30:23 +0000187 }
188}
189
190
Chris Lattner9fa3bcc2005-11-10 19:02:18 +0000191Mangler::Mangler(Module &M, const char *prefix)
Chris Lattner721fc382006-09-07 18:20:41 +0000192 : Prefix(prefix), UseQuotes(false), PreserveAsmNames(false),
193 Count(0), TypeCounter(0) {
Owen Andersone2f23a32007-09-07 04:06:50 +0000194 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000195
196 // Letters and numbers are acceptable.
197 for (unsigned char X = 'a'; X <= 'z'; ++X)
198 markCharAcceptable(X);
199 for (unsigned char X = 'A'; X <= 'Z'; ++X)
200 markCharAcceptable(X);
201 for (unsigned char X = '0'; X <= '9'; ++X)
202 markCharAcceptable(X);
203
204 // These chars are acceptable.
205 markCharAcceptable('_');
206 markCharAcceptable('$');
207 markCharAcceptable('.');
208
Brian Gaeked4dff192003-07-24 20:20:58 +0000209 // Calculate which global values have names that will collide when we throw
210 // away type information.
Evan Cheng0e697342008-07-10 00:04:23 +0000211 StringMap<GlobalValue*> Names;
Brian Gaeked4dff192003-07-24 20:20:58 +0000212 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Evan Cheng0e697342008-07-10 00:04:23 +0000213 InsertName(I, Names, MangledGlobals);
Gabor Greif697e94c2008-05-15 10:04:30 +0000214 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Evan Cheng0e697342008-07-10 00:04:23 +0000215 I != E; ++I)
216 InsertName(I, Names, MangledGlobals);
Brian Gaeked4dff192003-07-24 20:20:58 +0000217}