blob: 0bd190ad4edfb9b7fa0af5b72fada397a12bd04a [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///
Evan Chengcfdbfcc2009-05-05 22:50:29 +000034std::string Mangler::makeNameProper(const std::string &X, const char *Prefix,
35 const char *PrivatePrefix) {
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) {
Evan Chengcfdbfcc2009-05-05 22:50:29 +000043 std::string Result;
44
Chris Lattner4b155fa2005-11-10 19:30:07 +000045 // If X does not start with (char)1, add the prefix.
Evan Chengcfdbfcc2009-05-05 22:50:29 +000046 bool NeedPrefix = true;
Chris Lattner4b155fa2005-11-10 19:30:07 +000047 std::string::const_iterator I = X.begin();
Evan Chengcfdbfcc2009-05-05 22:50:29 +000048 if (*I == 1) {
49 NeedPrefix = false;
Chris Lattner4b155fa2005-11-10 19:30:07 +000050 ++I; // Skip over the marker.
Evan Chengcfdbfcc2009-05-05 22:50:29 +000051 }
Chris Lattner4b155fa2005-11-10 19:30:07 +000052
Chris Lattnere1d34bac2005-11-10 21:40:01 +000053 // Mangle the first letter specially, don't allow numbers.
Chris Lattner4b155fa2005-11-10 19:30:07 +000054 if (*I >= '0' && *I <= '9')
55 Result += MangleLetter(*I++);
56
Chris Lattnere1d34bac2005-11-10 21:40:01 +000057 for (std::string::const_iterator E = X.end(); I != E; ++I) {
58 if (!isCharAcceptable(*I))
Chris Lattner4b155fa2005-11-10 19:30:07 +000059 Result += MangleLetter(*I);
60 else
61 Result += *I;
Chris Lattnere1d34bac2005-11-10 21:40:01 +000062 }
Chris Lattnerecc301d2005-11-10 23:24:26 +000063
Evan Chengcfdbfcc2009-05-05 22:50:29 +000064 if (NeedPrefix) {
65 if (Prefix)
66 Result = Prefix + Result;
67 if (PrivatePrefix)
68 Result = PrivatePrefix + Result;
Chris Lattnerecc301d2005-11-10 23:24:26 +000069 }
Evan Chengcfdbfcc2009-05-05 22:50:29 +000070 return Result;
Chris Lattner4b155fa2005-11-10 19:30:07 +000071 }
Evan Chengcfdbfcc2009-05-05 22:50:29 +000072
73 bool NeedPrefix = true;
74 bool NeedQuotes = false;
75 std::string Result;
76 std::string::const_iterator I = X.begin();
77 if (*I == 1) {
78 NeedPrefix = false;
79 ++I; // Skip over the marker.
80 }
81
82 // If the first character is a number, we need quotes.
83 if (*I >= '0' && *I <= '9')
84 NeedQuotes = true;
85
86 // Do an initial scan of the string, checking to see if we need quotes or
87 // to escape a '"' or not.
88 if (!NeedQuotes)
89 for (std::string::const_iterator E = X.end(); I != E; ++I)
90 if (!isCharAcceptable(*I)) {
91 NeedQuotes = true;
92 break;
93 }
94
95 // In the common case, we don't need quotes. Handle this quickly.
96 if (!NeedQuotes) {
97 if (NeedPrefix) {
98 if (Prefix)
99 Result = Prefix + X;
100 else
101 Result = X;
102 if (PrivatePrefix)
103 Result = PrivatePrefix + Result;
104 return Result;
105 } else
106 return X.substr(1);
107 }
108
109 // Otherwise, construct the string the expensive way.
110 for (std::string::const_iterator E = X.end(); I != E; ++I) {
111 if (*I == '"')
112 Result += "_QQ_";
113 else if (*I == '\n')
114 Result += "_NL_";
115 else
116 Result += *I;
117 }
118
119 if (NeedPrefix) {
120 if (Prefix)
121 Result = Prefix + X;
122 else
123 Result = X;
124 if (PrivatePrefix)
125 Result = PrivatePrefix + Result;
126 }
127 Result = '"' + Result + '"';
Chris Lattner9527fea2003-08-24 21:08:38 +0000128 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +0000129}
130
Chris Lattner3a9fd4c2004-07-08 22:09:34 +0000131/// getTypeID - Return a unique ID for the specified LLVM type.
132///
133unsigned Mangler::getTypeID(const Type *Ty) {
134 unsigned &E = TypeMap[Ty];
135 if (E == 0) E = ++TypeCounter;
136 return E;
137}
138
Brian Gaeked4dff192003-07-24 20:20:58 +0000139std::string Mangler::getValueName(const Value *V) {
Chris Lattner12b08312005-11-10 18:48:58 +0000140 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
141 return getValueName(GV);
142
143 std::string &Name = Memo[V];
144 if (!Name.empty())
145 return Name; // Return the already-computed name for V.
146
147 // Always mangle local names.
148 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
149 return Name;
150}
151
152
Bill Wendling5752f692007-09-17 22:39:32 +0000153std::string Mangler::getValueName(const GlobalValue *GV, const char * Suffix) {
Brian Gaeked4dff192003-07-24 20:20:58 +0000154 // Check to see whether we've already named V.
Chris Lattner12b08312005-11-10 18:48:58 +0000155 std::string &Name = Memo[GV];
156 if (!Name.empty())
157 return Name; // Return the already-computed name for V.
Brian Gaeked4dff192003-07-24 20:20:58 +0000158
Chris Lattner12b08312005-11-10 18:48:58 +0000159 // Name mangling occurs as follows:
160 // - If V is an intrinsic function, do not change name at all
161 // - Otherwise, mangling occurs if global collides with existing name.
Duncan Sands38ef3a82007-12-03 20:06:50 +0000162 if (isa<Function>(GV) && cast<Function>(GV)->isIntrinsic()) {
Evan Cheng0e697342008-07-10 00:04:23 +0000163 Name = GV->getNameStart(); // Is an intrinsic function
Chris Lattner7c727b22005-11-15 01:32:03 +0000164 } else if (!GV->hasName()) {
165 // Must mangle the global into a unique ID.
166 unsigned TypeUniqueID = getTypeID(GV->getType());
167 static unsigned GlobalID = 0;
168 Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(GlobalID++);
Brian Gaeked4dff192003-07-24 20:20:58 +0000169 } else {
Rafael Espindola6de96a12009-01-15 20:18:42 +0000170 if (GV->hasPrivateLinkage())
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000171 Name = makeNameProper(GV->getName() + Suffix, Prefix, PrivatePrefix);
Rafael Espindola6de96a12009-01-15 20:18:42 +0000172 else
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000173 Name = makeNameProper(GV->getName() + Suffix, Prefix);
Brian Gaeked4dff192003-07-24 20:20:58 +0000174 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000175
Chris Lattner12b08312005-11-10 18:48:58 +0000176 return Name;
Brian Gaeked4dff192003-07-24 20:20:58 +0000177}
178
Rafael Espindola6de96a12009-01-15 20:18:42 +0000179Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix)
180 : Prefix(prefix), PrivatePrefix (privatePrefix), UseQuotes(false),
181 PreserveAsmNames(false), Count(0), TypeCounter(0) {
Owen Andersone2f23a32007-09-07 04:06:50 +0000182 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000183
184 // Letters and numbers are acceptable.
185 for (unsigned char X = 'a'; X <= 'z'; ++X)
186 markCharAcceptable(X);
187 for (unsigned char X = 'A'; X <= 'Z'; ++X)
188 markCharAcceptable(X);
189 for (unsigned char X = '0'; X <= '9'; ++X)
190 markCharAcceptable(X);
191
192 // These chars are acceptable.
193 markCharAcceptable('_');
194 markCharAcceptable('$');
195 markCharAcceptable('.');
Brian Gaeked4dff192003-07-24 20:20:58 +0000196}