blob: 777e5b51eebb6ade1c614d7e26080389e0e6625e [file] [log] [blame]
Brian Gaekeb198ca32003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaekeb198ca32003-07-24 20:20:58 +00009//
10// Unified name mangler for CWriter and assembly backends.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaeke6b902dc2003-07-25 20:21:20 +000014#include "llvm/Support/Mangler.h"
Chris Lattner9ddf2122005-11-10 18:48:58 +000015#include "llvm/DerivedTypes.h"
Brian Gaekeb198ca32003-07-24 20:20:58 +000016#include "llvm/Module.h"
Owen Anderson718cb662007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattneraa8a8472003-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 Lattner91150e52005-11-10 21:40:01 +000026 char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
27 return Result;
Chris Lattneraa8a8472003-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 Brukmanfd939082005-04-21 23:48:37 +000032///
Chris Lattner5b845c92005-09-24 08:24:28 +000033std::string Mangler::makeNameProper(const std::string &X, const char *Prefix) {
Chris Lattneraa8a8472003-08-24 21:08:38 +000034 std::string Result;
Chris Lattner91150e52005-11-10 21:40:01 +000035 if (X.empty()) return X; // Empty names are uniqued by the caller.
Chris Lattner5b845c92005-09-24 08:24:28 +000036
Chris Lattner833eb682006-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 Lattnerac8c8342005-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 Lattneraa8a8472003-08-24 21:08:38 +000046 else
Chris Lattnerac8c8342005-11-10 19:30:07 +000047 ++I; // Skip over the marker.
48
Chris Lattner91150e52005-11-10 21:40:01 +000049 // Mangle the first letter specially, don't allow numbers.
Chris Lattnerac8c8342005-11-10 19:30:07 +000050 if (*I >= '0' && *I <= '9')
51 Result += MangleLetter(*I++);
52
Chris Lattner91150e52005-11-10 21:40:01 +000053 for (std::string::const_iterator E = X.end(); I != E; ++I) {
54 if (!isCharAcceptable(*I))
Chris Lattnerac8c8342005-11-10 19:30:07 +000055 Result += MangleLetter(*I);
56 else
57 Result += *I;
Chris Lattner91150e52005-11-10 21:40:01 +000058 }
Chris Lattnerac8c8342005-11-10 19:30:07 +000059 } else {
60 bool NeedsQuotes = false;
61
Chris Lattnerac8c8342005-11-10 19:30:07 +000062 std::string::const_iterator I = X.begin();
Chris Lattner92c4bb92005-11-10 23:24:26 +000063 if (*I == 1)
Chris Lattnerac8c8342005-11-10 19:30:07 +000064 ++I; // Skip over the marker.
Chris Lattner92c4bb92005-11-10 23:24:26 +000065
Chris Lattnerac8c8342005-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 Lattnerbc076662005-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 Lattner92c4bb92005-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 Lattnerbc076662005-11-10 21:47:01 +000086
87 // Otherwise, construct the string the expensive way.
88 I = X.begin();
Chris Lattner92c4bb92005-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 Lattnerbc076662005-11-10 21:47:01 +000096 for (std::string::const_iterator E = X.end(); I != E; ++I) {
Chris Lattnerac8c8342005-11-10 19:30:07 +000097 if (*I == '"')
98 Result += "_QQ_";
Chris Lattnerbc076662005-11-10 21:47:01 +000099 else
Chris Lattnerac8c8342005-11-10 19:30:07 +0000100 Result += *I;
Chris Lattnerbc076662005-11-10 21:47:01 +0000101 }
102 Result = '"' + Result + '"';
Chris Lattnerac8c8342005-11-10 19:30:07 +0000103 }
Chris Lattneraa8a8472003-08-24 21:08:38 +0000104 return Result;
Brian Gaekeb198ca32003-07-24 20:20:58 +0000105}
106
Chris Lattnerd6391d72004-07-08 22:09:34 +0000107/// getTypeID - Return a unique ID for the specified LLVM type.
108///
109unsigned Mangler::getTypeID(const Type *Ty) {
110 unsigned &E = TypeMap[Ty];
111 if (E == 0) E = ++TypeCounter;
112 return E;
113}
114
Brian Gaekeb198ca32003-07-24 20:20:58 +0000115std::string Mangler::getValueName(const Value *V) {
Chris Lattner9ddf2122005-11-10 18:48:58 +0000116 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
117 return getValueName(GV);
118
119 std::string &Name = Memo[V];
120 if (!Name.empty())
121 return Name; // Return the already-computed name for V.
122
123 // Always mangle local names.
124 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
125 return Name;
126}
127
128
Bill Wendlingecd91372007-09-17 22:39:32 +0000129std::string Mangler::getValueName(const GlobalValue *GV, const char * Suffix) {
Brian Gaekeb198ca32003-07-24 20:20:58 +0000130 // Check to see whether we've already named V.
Chris Lattner9ddf2122005-11-10 18:48:58 +0000131 std::string &Name = Memo[GV];
132 if (!Name.empty())
133 return Name; // Return the already-computed name for V.
Brian Gaekeb198ca32003-07-24 20:20:58 +0000134
Chris Lattner9ddf2122005-11-10 18:48:58 +0000135 // Name mangling occurs as follows:
136 // - If V is an intrinsic function, do not change name at all
137 // - Otherwise, mangling occurs if global collides with existing name.
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000138 if (isa<Function>(GV) && cast<Function>(GV)->isIntrinsic()) {
Chris Lattner9ddf2122005-11-10 18:48:58 +0000139 Name = GV->getName(); // Is an intrinsic function
Chris Lattnerd358cfc2005-11-15 01:32:03 +0000140 } else if (!GV->hasName()) {
141 // Must mangle the global into a unique ID.
142 unsigned TypeUniqueID = getTypeID(GV->getType());
143 static unsigned GlobalID = 0;
144 Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(GlobalID++);
Chris Lattner9ddf2122005-11-10 18:48:58 +0000145 } else if (!MangledGlobals.count(GV)) {
Bill Wendlingecd91372007-09-17 22:39:32 +0000146 Name = makeNameProper(GV->getName() + Suffix, Prefix);
Brian Gaekeb198ca32003-07-24 20:20:58 +0000147 } else {
Chris Lattner9ddf2122005-11-10 18:48:58 +0000148 unsigned TypeUniqueID = getTypeID(GV->getType());
149 Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
Brian Gaekeb198ca32003-07-24 20:20:58 +0000150 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000151
Chris Lattner9ddf2122005-11-10 18:48:58 +0000152 return Name;
Brian Gaekeb198ca32003-07-24 20:20:58 +0000153}
154
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000155void Mangler::InsertName(GlobalValue *GV,
156 std::map<std::string, GlobalValue*> &Names) {
Chris Lattnerd358cfc2005-11-15 01:32:03 +0000157 if (!GV->hasName()) // We must mangle unnamed globals.
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000158 return;
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000159
160 // Figure out if this is already used.
161 GlobalValue *&ExistingValue = Names[GV->getName()];
162 if (!ExistingValue) {
163 ExistingValue = GV;
164 } else {
165 // If GV is external but the existing one is static, mangle the existing one
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000166 if ((GV->hasExternalLinkage() || GV->hasDLLImportLinkage()) &&
167 !(ExistingValue->hasExternalLinkage() || ExistingValue->hasDLLImportLinkage())) {
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000168 MangledGlobals.insert(ExistingValue);
169 ExistingValue = GV;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000170 } else if ((GV->hasExternalLinkage() ||
171 GV->hasDLLImportLinkage()) &&
172 (ExistingValue->hasExternalLinkage() ||
173 ExistingValue->hasDLLImportLinkage()) &&
Reid Spencer5cbf9852007-01-30 20:08:39 +0000174 GV->isDeclaration() &&
175 ExistingValue->isDeclaration()) {
Chris Lattnered1e1e22006-02-13 21:43:26 +0000176 // If the two globals both have external inkage, and are both external,
177 // don't mangle either of them, we just have some silly type mismatch.
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000178 } else {
179 // Otherwise, mangle GV
180 MangledGlobals.insert(GV);
181 }
182 }
183}
184
185
Chris Lattner2f9bc4f2005-11-10 19:02:18 +0000186Mangler::Mangler(Module &M, const char *prefix)
Chris Lattner833eb682006-09-07 18:20:41 +0000187 : Prefix(prefix), UseQuotes(false), PreserveAsmNames(false),
188 Count(0), TypeCounter(0) {
Owen Anderson718cb662007-09-07 04:06:50 +0000189 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
Chris Lattner91150e52005-11-10 21:40:01 +0000190
191 // Letters and numbers are acceptable.
192 for (unsigned char X = 'a'; X <= 'z'; ++X)
193 markCharAcceptable(X);
194 for (unsigned char X = 'A'; X <= 'Z'; ++X)
195 markCharAcceptable(X);
196 for (unsigned char X = '0'; X <= '9'; ++X)
197 markCharAcceptable(X);
198
199 // These chars are acceptable.
200 markCharAcceptable('_');
201 markCharAcceptable('$');
202 markCharAcceptable('.');
203
Brian Gaekeb198ca32003-07-24 20:20:58 +0000204 // Calculate which global values have names that will collide when we throw
205 // away type information.
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000206 std::map<std::string, GlobalValue*> Names;
Brian Gaekeb198ca32003-07-24 20:20:58 +0000207 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000208 InsertName(I, Names);
Chris Lattnere4d5c442005-03-15 04:54:21 +0000209 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000210 InsertName(I, Names);
Brian Gaekeb198ca32003-07-24 20:20:58 +0000211}
Reid Spenceraf303d52006-06-07 23:03:13 +0000212
213// Cause this file to be linked in when Support/Mangler.h is #included
214DEFINING_FILE_FOR(Mangler)