blob: 5257ca13a0c08a5a0f4c6fe32185edf2fcb23a46 [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 Lattner4b155fa2005-11-10 19:30:07 +000036 if (!UseQuotes) {
37 // If X does not start with (char)1, add the prefix.
38 std::string::const_iterator I = X.begin();
39 if (*I != 1)
40 Result = Prefix;
Chris Lattner9527fea2003-08-24 21:08:38 +000041 else
Chris Lattner4b155fa2005-11-10 19:30:07 +000042 ++I; // Skip over the marker.
43
Chris Lattnere1d34bac2005-11-10 21:40:01 +000044 // Mangle the first letter specially, don't allow numbers.
Chris Lattner4b155fa2005-11-10 19:30:07 +000045 if (*I >= '0' && *I <= '9')
46 Result += MangleLetter(*I++);
47
Chris Lattnere1d34bac2005-11-10 21:40:01 +000048 for (std::string::const_iterator E = X.end(); I != E; ++I) {
49 if (!isCharAcceptable(*I))
Chris Lattner4b155fa2005-11-10 19:30:07 +000050 Result += MangleLetter(*I);
51 else
52 Result += *I;
Chris Lattnere1d34bac2005-11-10 21:40:01 +000053 }
Chris Lattner4b155fa2005-11-10 19:30:07 +000054 } else {
55 bool NeedsQuotes = false;
56
Chris Lattner4b155fa2005-11-10 19:30:07 +000057 std::string::const_iterator I = X.begin();
Chris Lattnerecc301d2005-11-10 23:24:26 +000058 if (*I == 1)
Chris Lattner4b155fa2005-11-10 19:30:07 +000059 ++I; // Skip over the marker.
Chris Lattnerecc301d2005-11-10 23:24:26 +000060
Chris Lattner4b155fa2005-11-10 19:30:07 +000061 // If the first character is a number, we need quotes.
62 if (*I >= '0' && *I <= '9')
63 NeedsQuotes = true;
64
Chris Lattner86164e62005-11-10 21:47:01 +000065 // Do an initial scan of the string, checking to see if we need quotes or
66 // to escape a '"' or not.
67 if (!NeedsQuotes)
68 for (std::string::const_iterator E = X.end(); I != E; ++I)
69 if (!isCharAcceptable(*I)) {
70 NeedsQuotes = true;
71 break;
72 }
73
74 // In the common case, we don't need quotes. Handle this quickly.
Chris Lattnerecc301d2005-11-10 23:24:26 +000075 if (!NeedsQuotes) {
76 if (*X.begin() != 1)
77 return Prefix+X;
78 else
79 return X.substr(1);
80 }
Chris Lattner86164e62005-11-10 21:47:01 +000081
82 // Otherwise, construct the string the expensive way.
83 I = X.begin();
Chris Lattnerecc301d2005-11-10 23:24:26 +000084
85 // If X does not start with (char)1, add the prefix.
86 if (*I != 1)
87 Result = Prefix;
88 else
89 ++I; // Skip the marker if present.
90
Chris Lattner86164e62005-11-10 21:47:01 +000091 for (std::string::const_iterator E = X.end(); I != E; ++I) {
Chris Lattner4b155fa2005-11-10 19:30:07 +000092 if (*I == '"')
93 Result += "_QQ_";
Chris Lattner86164e62005-11-10 21:47:01 +000094 else
Chris Lattner4b155fa2005-11-10 19:30:07 +000095 Result += *I;
Chris Lattner86164e62005-11-10 21:47:01 +000096 }
97 Result = '"' + Result + '"';
Chris Lattner4b155fa2005-11-10 19:30:07 +000098 }
Chris Lattner9527fea2003-08-24 21:08:38 +000099 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +0000100}
101
Chris Lattner3a9fd4c2004-07-08 22:09:34 +0000102/// getTypeID - Return a unique ID for the specified LLVM type.
103///
104unsigned Mangler::getTypeID(const Type *Ty) {
105 unsigned &E = TypeMap[Ty];
106 if (E == 0) E = ++TypeCounter;
107 return E;
108}
109
Brian Gaeked4dff192003-07-24 20:20:58 +0000110std::string Mangler::getValueName(const Value *V) {
Chris Lattner12b08312005-11-10 18:48:58 +0000111 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
112 return getValueName(GV);
113
114 std::string &Name = Memo[V];
115 if (!Name.empty())
116 return Name; // Return the already-computed name for V.
117
118 // Always mangle local names.
119 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
120 return Name;
121}
122
123
124std::string Mangler::getValueName(const GlobalValue *GV) {
Brian Gaeked4dff192003-07-24 20:20:58 +0000125 // Check to see whether we've already named V.
Chris Lattner12b08312005-11-10 18:48:58 +0000126 std::string &Name = Memo[GV];
127 if (!Name.empty())
128 return Name; // Return the already-computed name for V.
Brian Gaeked4dff192003-07-24 20:20:58 +0000129
Chris Lattner12b08312005-11-10 18:48:58 +0000130 // Name mangling occurs as follows:
131 // - If V is an intrinsic function, do not change name at all
132 // - Otherwise, mangling occurs if global collides with existing name.
133 if (isa<Function>(GV) && cast<Function>(GV)->getIntrinsicID()) {
134 Name = GV->getName(); // Is an intrinsic function
135 } else if (!MangledGlobals.count(GV)) {
136 Name = makeNameProper(GV->getName(), Prefix);
Brian Gaeked4dff192003-07-24 20:20:58 +0000137 } else {
Chris Lattner12b08312005-11-10 18:48:58 +0000138 unsigned TypeUniqueID = getTypeID(GV->getType());
139 Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
Brian Gaeked4dff192003-07-24 20:20:58 +0000140 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000141
Chris Lattner12b08312005-11-10 18:48:58 +0000142 return Name;
Brian Gaeked4dff192003-07-24 20:20:58 +0000143}
144
Chris Lattner77687a92004-02-14 00:30:23 +0000145void Mangler::InsertName(GlobalValue *GV,
146 std::map<std::string, GlobalValue*> &Names) {
147 if (!GV->hasName()) { // We must mangle unnamed globals.
148 MangledGlobals.insert(GV);
149 return;
150 }
151
152 // Figure out if this is already used.
153 GlobalValue *&ExistingValue = Names[GV->getName()];
154 if (!ExistingValue) {
155 ExistingValue = GV;
156 } else {
157 // If GV is external but the existing one is static, mangle the existing one
158 if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) {
159 MangledGlobals.insert(ExistingValue);
160 ExistingValue = GV;
161 } else {
162 // Otherwise, mangle GV
163 MangledGlobals.insert(GV);
164 }
165 }
166}
167
168
Chris Lattner9fa3bcc2005-11-10 19:02:18 +0000169Mangler::Mangler(Module &M, const char *prefix)
Chris Lattner4b155fa2005-11-10 19:30:07 +0000170 : Prefix(prefix), UseQuotes(false), Count(0), TypeCounter(0) {
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000171 std::fill(AcceptableChars,
172 AcceptableChars+sizeof(AcceptableChars)/sizeof(AcceptableChars[0]),
173 0);
174
175 // Letters and numbers are acceptable.
176 for (unsigned char X = 'a'; X <= 'z'; ++X)
177 markCharAcceptable(X);
178 for (unsigned char X = 'A'; X <= 'Z'; ++X)
179 markCharAcceptable(X);
180 for (unsigned char X = '0'; X <= '9'; ++X)
181 markCharAcceptable(X);
182
183 // These chars are acceptable.
184 markCharAcceptable('_');
185 markCharAcceptable('$');
186 markCharAcceptable('.');
187
Brian Gaeked4dff192003-07-24 20:20:58 +0000188 // Calculate which global values have names that will collide when we throw
189 // away type information.
Chris Lattner77687a92004-02-14 00:30:23 +0000190 std::map<std::string, GlobalValue*> Names;
Brian Gaeked4dff192003-07-24 20:20:58 +0000191 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner77687a92004-02-14 00:30:23 +0000192 InsertName(I, Names);
Chris Lattner531f9e92005-03-15 04:54:21 +0000193 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattner77687a92004-02-14 00:30:23 +0000194 InsertName(I, Names);
Brian Gaeked4dff192003-07-24 20:20:58 +0000195}