blob: cec41dfd40071d8a4e1572f9c6fd6e690ad579cd [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,
35 bool hasPrivateLinkage) {
36 assert(!X.empty() && "Cannot mangle empty strings");
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;
Chris Lattnerf34815b2009-07-14 04:50:12 +000041
42 // Figure out the prefix to use.
43 const char *ThePrefix = hasPrivateLinkage ? PrivatePrefix : Prefix;
Chris Lattner721fc382006-09-07 18:20:41 +000044
Chris Lattner4b155fa2005-11-10 19:30:07 +000045 if (!UseQuotes) {
Evan Chengcfdbfcc2009-05-05 22:50:29 +000046 std::string Result;
47
Chris Lattner4b155fa2005-11-10 19:30:07 +000048 // If X does not start with (char)1, add the prefix.
Evan Chengcfdbfcc2009-05-05 22:50:29 +000049 bool NeedPrefix = true;
Chris Lattner4b155fa2005-11-10 19:30:07 +000050 std::string::const_iterator I = X.begin();
Evan Chengcfdbfcc2009-05-05 22:50:29 +000051 if (*I == 1) {
52 NeedPrefix = false;
Chris Lattner4b155fa2005-11-10 19:30:07 +000053 ++I; // Skip over the marker.
Evan Chengcfdbfcc2009-05-05 22:50:29 +000054 }
Chris Lattner4b155fa2005-11-10 19:30:07 +000055
Chris Lattnere1d34bac2005-11-10 21:40:01 +000056 // Mangle the first letter specially, don't allow numbers.
Chris Lattner4b155fa2005-11-10 19:30:07 +000057 if (*I >= '0' && *I <= '9')
58 Result += MangleLetter(*I++);
59
Chris Lattnere1d34bac2005-11-10 21:40:01 +000060 for (std::string::const_iterator E = X.end(); I != E; ++I) {
61 if (!isCharAcceptable(*I))
Chris Lattner4b155fa2005-11-10 19:30:07 +000062 Result += MangleLetter(*I);
63 else
64 Result += *I;
Chris Lattnere1d34bac2005-11-10 21:40:01 +000065 }
Chris Lattnerecc301d2005-11-10 23:24:26 +000066
Chris Lattnerf34815b2009-07-14 04:50:12 +000067 if (NeedPrefix)
68 Result = ThePrefix + Result;
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.
98 return ThePrefix + X;
Evan Chengcfdbfcc2009-05-05 22:50:29 +000099 }
Chris Lattnerf34815b2009-07-14 04:50:12 +0000100
101 Result = X.substr(0, I-X.begin());
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000102
103 // Otherwise, construct the string the expensive way.
104 for (std::string::const_iterator E = X.end(); I != E; ++I) {
105 if (*I == '"')
106 Result += "_QQ_";
107 else if (*I == '\n')
108 Result += "_NL_";
109 else
110 Result += *I;
111 }
112
Chris Lattnerf34815b2009-07-14 04:50:12 +0000113 if (NeedPrefix)
114 Result = ThePrefix + Result;
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000115 Result = '"' + Result + '"';
Chris Lattner9527fea2003-08-24 21:08:38 +0000116 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +0000117}
118
Daniel Dunbar966932c2009-07-14 15:57:55 +0000119std::string Mangler::getValueName(const GlobalValue *GV, const char *Suffix) {
Chris Lattner05f19762009-07-14 00:15:14 +0000120 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
121 "Intrinsic functions cannot be mangled by Mangler");
Chris Lattner105efaf2009-07-14 00:01:06 +0000122
Chris Lattnerf34815b2009-07-14 04:50:12 +0000123 if (GV->hasName())
Daniel Dunbar966932c2009-07-14 15:57:55 +0000124 return makeNameProper(GV->getName() + Suffix, GV->hasPrivateLinkage());
Chris Lattner105efaf2009-07-14 00:01:06 +0000125
126 // Get the ID for the global, assigning a new one if we haven't got one
127 // already.
128 unsigned &ID = AnonGlobalIDs[GV];
129 if (ID == 0) ID = NextAnonGlobalID++;
130
131 // Must mangle the global into a unique ID.
Daniel Dunbar966932c2009-07-14 15:57:55 +0000132 return "__unnamed_" + utostr(ID) + Suffix;
Brian Gaeked4dff192003-07-24 20:20:58 +0000133}
134
Rafael Espindola6de96a12009-01-15 20:18:42 +0000135Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix)
136 : Prefix(prefix), PrivatePrefix (privatePrefix), UseQuotes(false),
Chris Lattner105efaf2009-07-14 00:01:06 +0000137 PreserveAsmNames(false), NextAnonGlobalID(1) {
Owen Andersone2f23a32007-09-07 04:06:50 +0000138 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000139
140 // Letters and numbers are acceptable.
141 for (unsigned char X = 'a'; X <= 'z'; ++X)
142 markCharAcceptable(X);
143 for (unsigned char X = 'A'; X <= 'Z'; ++X)
144 markCharAcceptable(X);
145 for (unsigned char X = '0'; X <= '9'; ++X)
146 markCharAcceptable(X);
147
148 // These chars are acceptable.
149 markCharAcceptable('_');
150 markCharAcceptable('$');
151 markCharAcceptable('.');
Brian Gaeked4dff192003-07-24 20:20:58 +0000152}