blob: 44cf77826629546e4c35d25d9c92824ed575ad56 [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///
Chris Lattnerf34815b2009-07-14 04:50:12 +000034std::string Mangler::makeNameProper(const std::string &X,
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000035 ManglerPrefixTy PrefixTy) {
Chris Lattnerf34815b2009-07-14 04:50:12 +000036 assert(!X.empty() && "Cannot mangle empty strings");
Chris Lattnercc9c0332005-09-24 08:24:28 +000037
Chris Lattner4b155fa2005-11-10 19:30:07 +000038 if (!UseQuotes) {
Evan Chengcfdbfcc2009-05-05 22:50:29 +000039 std::string Result;
40
Chris Lattner4b155fa2005-11-10 19:30:07 +000041 // If X does not start with (char)1, add the prefix.
Evan Chengcfdbfcc2009-05-05 22:50:29 +000042 bool NeedPrefix = true;
Chris Lattner4b155fa2005-11-10 19:30:07 +000043 std::string::const_iterator I = X.begin();
Evan Chengcfdbfcc2009-05-05 22:50:29 +000044 if (*I == 1) {
45 NeedPrefix = false;
Chris Lattner4b155fa2005-11-10 19:30:07 +000046 ++I; // Skip over the marker.
Evan Chengcfdbfcc2009-05-05 22:50:29 +000047 }
Chris Lattner4b155fa2005-11-10 19:30:07 +000048
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 Lattnerecc301d2005-11-10 23:24:26 +000059
Chris Lattner8c9a96b2009-07-14 18:17:16 +000060 if (NeedPrefix) {
61 Result = Prefix + Result;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000062
Bill Wendling1bcfbff2009-07-20 19:41:27 +000063 if (PrefixTy == Mangler::Private)
Chris Lattner8c9a96b2009-07-14 18:17:16 +000064 Result = PrivatePrefix + Result;
Bill Wendling1bcfbff2009-07-20 19:41:27 +000065 else if (PrefixTy == Mangler::LinkerPrivate)
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000066 Result = LinkerPrivatePrefix + Result;
Chris Lattner8c9a96b2009-07-14 18:17:16 +000067 }
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000068
Evan Chengcfdbfcc2009-05-05 22:50:29 +000069 return Result;
Chris Lattner4b155fa2005-11-10 19:30:07 +000070 }
Evan Chengcfdbfcc2009-05-05 22:50:29 +000071
72 bool NeedPrefix = true;
73 bool NeedQuotes = false;
74 std::string Result;
75 std::string::const_iterator I = X.begin();
76 if (*I == 1) {
77 NeedPrefix = false;
78 ++I; // Skip over the marker.
79 }
80
81 // If the first character is a number, we need quotes.
82 if (*I >= '0' && *I <= '9')
83 NeedQuotes = true;
84
85 // Do an initial scan of the string, checking to see if we need quotes or
86 // to escape a '"' or not.
87 if (!NeedQuotes)
88 for (std::string::const_iterator E = X.end(); I != E; ++I)
89 if (!isCharAcceptable(*I)) {
90 NeedQuotes = true;
91 break;
92 }
93
94 // In the common case, we don't need quotes. Handle this quickly.
95 if (!NeedQuotes) {
Chris Lattnerf34815b2009-07-14 04:50:12 +000096 if (!NeedPrefix)
97 return X.substr(1); // Strip off the \001.
Chris Lattner8c9a96b2009-07-14 18:17:16 +000098
99 Result = Prefix + X;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000100
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000101 if (PrefixTy == Mangler::Private)
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000102 Result = PrivatePrefix + Result;
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000103 else if (PrefixTy == Mangler::LinkerPrivate)
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000104 Result = LinkerPrivatePrefix + Result;
105
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000106 return Result;
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000107 }
Chris Lattnerf34815b2009-07-14 04:50:12 +0000108
109 Result = X.substr(0, I-X.begin());
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000110
111 // Otherwise, construct the string the expensive way.
112 for (std::string::const_iterator E = X.end(); I != E; ++I) {
113 if (*I == '"')
114 Result += "_QQ_";
115 else if (*I == '\n')
116 Result += "_NL_";
117 else
118 Result += *I;
119 }
120
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000121 if (NeedPrefix) {
122 Result = Prefix + Result;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000123
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000124 if (PrefixTy == Mangler::Private)
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000125 Result = PrivatePrefix + Result;
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000126 else if (PrefixTy == Mangler::LinkerPrivate)
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000127 Result = LinkerPrivatePrefix + Result;
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000128 }
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000129
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000130 Result = '"' + Result + '"';
Chris Lattner9527fea2003-08-24 21:08:38 +0000131 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +0000132}
133
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000134/// getMangledName - Returns the mangled name of V, an LLVM Value,
135/// in the current module. If 'Suffix' is specified, the name ends with the
136/// specified suffix. If 'ForcePrivate' is specified, the label is specified
137/// to have a private label prefix.
138///
139std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
140 bool ForcePrivate) {
Chris Lattner05f19762009-07-14 00:15:14 +0000141 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
142 "Intrinsic functions cannot be mangled by Mangler");
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000143
144 ManglerPrefixTy PrefixTy =
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000145 (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
146 GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000147
Chris Lattnerf34815b2009-07-14 04:50:12 +0000148 if (GV->hasName())
Daniel Dunbar5899dda2009-07-22 21:33:09 +0000149 return makeNameProper(GV->getNameStr() + Suffix, PrefixTy);
Chris Lattner105efaf2009-07-14 00:01:06 +0000150
151 // Get the ID for the global, assigning a new one if we haven't got one
152 // already.
153 unsigned &ID = AnonGlobalIDs[GV];
154 if (ID == 0) ID = NextAnonGlobalID++;
155
156 // Must mangle the global into a unique ID.
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000157 return makeNameProper("__unnamed_" + utostr(ID) + Suffix, PrefixTy);
Brian Gaeked4dff192003-07-24 20:20:58 +0000158}
159
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000160Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
161 const char *linkerPrivatePrefix)
162 : Prefix(prefix), PrivatePrefix(privatePrefix),
163 LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
Chris Lattnerc35b5ba2009-07-15 04:50:47 +0000164 NextAnonGlobalID(1) {
Owen Andersone2f23a32007-09-07 04:06:50 +0000165 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000166
167 // Letters and numbers are acceptable.
168 for (unsigned char X = 'a'; X <= 'z'; ++X)
169 markCharAcceptable(X);
170 for (unsigned char X = 'A'; X <= 'Z'; ++X)
171 markCharAcceptable(X);
172 for (unsigned char X = '0'; X <= '9'; ++X)
173 markCharAcceptable(X);
174
175 // These chars are acceptable.
176 markCharAcceptable('_');
177 markCharAcceptable('$');
178 markCharAcceptable('.');
Brian Gaeked4dff192003-07-24 20:20:58 +0000179}