blob: 264b84c6fe34bff1d8c7a9bbafc735354de3149c [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
57 // If X does not start with (char)1, add the prefix.
58 std::string::const_iterator I = X.begin();
59 if (*I != 1)
60 Result = Prefix;
61 else
62 ++I; // Skip over the marker.
63
64 // If the first character is a number, we need quotes.
65 if (*I >= '0' && *I <= '9')
66 NeedsQuotes = true;
67
Chris Lattner86164e62005-11-10 21:47:01 +000068 // Do an initial scan of the string, checking to see if we need quotes or
69 // to escape a '"' or not.
70 if (!NeedsQuotes)
71 for (std::string::const_iterator E = X.end(); I != E; ++I)
72 if (!isCharAcceptable(*I)) {
73 NeedsQuotes = true;
74 break;
75 }
76
77 // In the common case, we don't need quotes. Handle this quickly.
78 if (!NeedsQuotes)
79 return Result + X;
80
81 // Otherwise, construct the string the expensive way.
82 I = X.begin();
83 if (*I == 1) ++I; // Skip the marker if present.
84 for (std::string::const_iterator E = X.end(); I != E; ++I) {
Chris Lattner4b155fa2005-11-10 19:30:07 +000085 if (*I == '"')
86 Result += "_QQ_";
Chris Lattner86164e62005-11-10 21:47:01 +000087 else
Chris Lattner4b155fa2005-11-10 19:30:07 +000088 Result += *I;
Chris Lattner86164e62005-11-10 21:47:01 +000089 }
90 Result = '"' + Result + '"';
Chris Lattner4b155fa2005-11-10 19:30:07 +000091 }
Chris Lattner9527fea2003-08-24 21:08:38 +000092 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +000093}
94
Chris Lattner3a9fd4c2004-07-08 22:09:34 +000095/// getTypeID - Return a unique ID for the specified LLVM type.
96///
97unsigned Mangler::getTypeID(const Type *Ty) {
98 unsigned &E = TypeMap[Ty];
99 if (E == 0) E = ++TypeCounter;
100 return E;
101}
102
Brian Gaeked4dff192003-07-24 20:20:58 +0000103std::string Mangler::getValueName(const Value *V) {
Chris Lattner12b08312005-11-10 18:48:58 +0000104 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
105 return getValueName(GV);
106
107 std::string &Name = Memo[V];
108 if (!Name.empty())
109 return Name; // Return the already-computed name for V.
110
111 // Always mangle local names.
112 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
113 return Name;
114}
115
116
117std::string Mangler::getValueName(const GlobalValue *GV) {
Brian Gaeked4dff192003-07-24 20:20:58 +0000118 // Check to see whether we've already named V.
Chris Lattner12b08312005-11-10 18:48:58 +0000119 std::string &Name = Memo[GV];
120 if (!Name.empty())
121 return Name; // Return the already-computed name for V.
Brian Gaeked4dff192003-07-24 20:20:58 +0000122
Chris Lattner12b08312005-11-10 18:48:58 +0000123 // Name mangling occurs as follows:
124 // - If V is an intrinsic function, do not change name at all
125 // - Otherwise, mangling occurs if global collides with existing name.
126 if (isa<Function>(GV) && cast<Function>(GV)->getIntrinsicID()) {
127 Name = GV->getName(); // Is an intrinsic function
128 } else if (!MangledGlobals.count(GV)) {
129 Name = makeNameProper(GV->getName(), Prefix);
Brian Gaeked4dff192003-07-24 20:20:58 +0000130 } else {
Chris Lattner12b08312005-11-10 18:48:58 +0000131 unsigned TypeUniqueID = getTypeID(GV->getType());
132 Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
Brian Gaeked4dff192003-07-24 20:20:58 +0000133 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000134
Chris Lattner12b08312005-11-10 18:48:58 +0000135 return Name;
Brian Gaeked4dff192003-07-24 20:20:58 +0000136}
137
Chris Lattner77687a92004-02-14 00:30:23 +0000138void Mangler::InsertName(GlobalValue *GV,
139 std::map<std::string, GlobalValue*> &Names) {
140 if (!GV->hasName()) { // We must mangle unnamed globals.
141 MangledGlobals.insert(GV);
142 return;
143 }
144
145 // Figure out if this is already used.
146 GlobalValue *&ExistingValue = Names[GV->getName()];
147 if (!ExistingValue) {
148 ExistingValue = GV;
149 } else {
150 // If GV is external but the existing one is static, mangle the existing one
151 if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) {
152 MangledGlobals.insert(ExistingValue);
153 ExistingValue = GV;
154 } else {
155 // Otherwise, mangle GV
156 MangledGlobals.insert(GV);
157 }
158 }
159}
160
161
Chris Lattner9fa3bcc2005-11-10 19:02:18 +0000162Mangler::Mangler(Module &M, const char *prefix)
Chris Lattner4b155fa2005-11-10 19:30:07 +0000163 : Prefix(prefix), UseQuotes(false), Count(0), TypeCounter(0) {
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000164 std::fill(AcceptableChars,
165 AcceptableChars+sizeof(AcceptableChars)/sizeof(AcceptableChars[0]),
166 0);
167
168 // Letters and numbers are acceptable.
169 for (unsigned char X = 'a'; X <= 'z'; ++X)
170 markCharAcceptable(X);
171 for (unsigned char X = 'A'; X <= 'Z'; ++X)
172 markCharAcceptable(X);
173 for (unsigned char X = '0'; X <= '9'; ++X)
174 markCharAcceptable(X);
175
176 // These chars are acceptable.
177 markCharAcceptable('_');
178 markCharAcceptable('$');
179 markCharAcceptable('.');
180
Brian Gaeked4dff192003-07-24 20:20:58 +0000181 // Calculate which global values have names that will collide when we throw
182 // away type information.
Chris Lattner77687a92004-02-14 00:30:23 +0000183 std::map<std::string, GlobalValue*> Names;
Brian Gaeked4dff192003-07-24 20:20:58 +0000184 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner77687a92004-02-14 00:30:23 +0000185 InsertName(I, Names);
Chris Lattner531f9e92005-03-15 04:54:21 +0000186 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattner77687a92004-02-14 00:30:23 +0000187 InsertName(I, Names);
Brian Gaeked4dff192003-07-24 20:20:58 +0000188}