blob: 8b8ba598ef8131d04063c8e2b36c71c027fbe5f4 [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"
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 Lattner86164e62005-11-10 21:47:01 +000099 else
Chris Lattner4b155fa2005-11-10 19:30:07 +0000100 Result += *I;
Chris Lattner86164e62005-11-10 21:47:01 +0000101 }
102 Result = '"' + Result + '"';
Chris Lattner4b155fa2005-11-10 19:30:07 +0000103 }
Chris Lattner9527fea2003-08-24 21:08:38 +0000104 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +0000105}
106
Chris Lattner3a9fd4c2004-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 Gaeked4dff192003-07-24 20:20:58 +0000115std::string Mangler::getValueName(const Value *V) {
Chris Lattner12b08312005-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 Wendling5752f692007-09-17 22:39:32 +0000129std::string Mangler::getValueName(const GlobalValue *GV, const char * Suffix) {
Brian Gaeked4dff192003-07-24 20:20:58 +0000130 // Check to see whether we've already named V.
Chris Lattner12b08312005-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 Gaeked4dff192003-07-24 20:20:58 +0000134
Chris Lattner12b08312005-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.
138 if (isa<Function>(GV) && cast<Function>(GV)->getIntrinsicID()) {
139 Name = GV->getName(); // Is an intrinsic function
Chris Lattner7c727b22005-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 Lattner12b08312005-11-10 18:48:58 +0000145 } else if (!MangledGlobals.count(GV)) {
Bill Wendling5752f692007-09-17 22:39:32 +0000146 Name = makeNameProper(GV->getName() + Suffix, Prefix);
Brian Gaeked4dff192003-07-24 20:20:58 +0000147 } else {
Chris Lattner12b08312005-11-10 18:48:58 +0000148 unsigned TypeUniqueID = getTypeID(GV->getType());
149 Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
Brian Gaeked4dff192003-07-24 20:20:58 +0000150 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000151
Chris Lattner12b08312005-11-10 18:48:58 +0000152 return Name;
Brian Gaeked4dff192003-07-24 20:20:58 +0000153}
154
Chris Lattner77687a92004-02-14 00:30:23 +0000155void Mangler::InsertName(GlobalValue *GV,
156 std::map<std::string, GlobalValue*> &Names) {
Chris Lattner7c727b22005-11-15 01:32:03 +0000157 if (!GV->hasName()) // We must mangle unnamed globals.
Chris Lattner77687a92004-02-14 00:30:23 +0000158 return;
Chris Lattner77687a92004-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 Korobeynikovd61d39e2006-09-14 18:23:27 +0000166 if ((GV->hasExternalLinkage() || GV->hasDLLImportLinkage()) &&
167 !(ExistingValue->hasExternalLinkage() || ExistingValue->hasDLLImportLinkage())) {
Chris Lattner77687a92004-02-14 00:30:23 +0000168 MangledGlobals.insert(ExistingValue);
169 ExistingValue = GV;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000170 } else if ((GV->hasExternalLinkage() ||
171 GV->hasDLLImportLinkage()) &&
172 (ExistingValue->hasExternalLinkage() ||
173 ExistingValue->hasDLLImportLinkage()) &&
Reid Spencer5301e7c2007-01-30 20:08:39 +0000174 GV->isDeclaration() &&
175 ExistingValue->isDeclaration()) {
Chris Lattnereea0f012006-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 Lattner77687a92004-02-14 00:30:23 +0000178 } else {
179 // Otherwise, mangle GV
180 MangledGlobals.insert(GV);
181 }
182 }
183}
184
185
Chris Lattner9fa3bcc2005-11-10 19:02:18 +0000186Mangler::Mangler(Module &M, const char *prefix)
Chris Lattner721fc382006-09-07 18:20:41 +0000187 : Prefix(prefix), UseQuotes(false), PreserveAsmNames(false),
188 Count(0), TypeCounter(0) {
Owen Andersone2f23a32007-09-07 04:06:50 +0000189 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
Chris Lattnere1d34bac2005-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 Gaeked4dff192003-07-24 20:20:58 +0000204 // Calculate which global values have names that will collide when we throw
205 // away type information.
Chris Lattner77687a92004-02-14 00:30:23 +0000206 std::map<std::string, GlobalValue*> Names;
Brian Gaeked4dff192003-07-24 20:20:58 +0000207 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner77687a92004-02-14 00:30:23 +0000208 InsertName(I, Names);
Chris Lattner531f9e92005-03-15 04:54:21 +0000209 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattner77687a92004-02-14 00:30:23 +0000210 InsertName(I, Names);
Brian Gaeked4dff192003-07-24 20:20:58 +0000211}
Reid Spencer5113dc52006-06-07 23:03:13 +0000212
213// Cause this file to be linked in when Support/Mangler.h is #included
214DEFINING_FILE_FOR(Mangler)