Make the MC symbol printer and llvm::Mangler exactly agree on mangling
[oota-llvm.git] / lib / VMCore / Mangler.cpp
1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Unified name mangler for CWriter and assembly backends.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/Mangler.h"
15 #include "llvm/Function.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22
23 static char HexDigit(int V) {
24   return V < 10 ? V+'0' : V+'A'-10;
25 }
26
27 static std::string MangleLetter(unsigned char C) {
28   char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
29   return Result;
30 }
31
32 /// makeNameProper - We don't want identifier names non-C-identifier characters
33 /// in them, so mangle them as appropriate.
34 ///
35 std::string Mangler::makeNameProper(const std::string &X,
36                                     ManglerPrefixTy PrefixTy) {
37   assert(!X.empty() && "Cannot mangle empty strings");
38   
39   if (!UseQuotes) {
40     std::string Result;
41
42     // If X does not start with (char)1, add the prefix.
43     bool NeedPrefix = true;
44     std::string::const_iterator I = X.begin();
45     if (*I == 1) {
46       NeedPrefix = false;
47       ++I;  // Skip over the marker.
48     }
49     
50     // Mangle the first letter specially, don't allow numbers.
51     if (*I >= '0' && *I <= '9')
52       Result += MangleLetter(*I++);
53
54     for (std::string::const_iterator E = X.end(); I != E; ++I) {
55       if (!isCharAcceptable(*I))
56         Result += MangleLetter(*I);
57       else
58         Result += *I;
59     }
60
61     if (NeedPrefix) {
62       Result = Prefix + Result;
63
64       if (PrefixTy == Mangler::Private)
65         Result = PrivatePrefix + Result;
66       else if (PrefixTy == Mangler::LinkerPrivate)
67         Result = LinkerPrivatePrefix + Result;
68     }
69
70     return Result;
71   }
72
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       return X.substr(1);   // Strip off the \001.
99     
100     Result = Prefix + X;
101
102     if (PrefixTy == Mangler::Private)
103       Result = PrivatePrefix + Result;
104     else if (PrefixTy == Mangler::LinkerPrivate)
105       Result = LinkerPrivatePrefix + Result;
106
107     return Result;
108   }
109
110   if (NeedPrefix)
111     Result = X.substr(0, I-X.begin());
112     
113   // Otherwise, construct the string the expensive way.
114   for (std::string::const_iterator E = X.end(); I != E; ++I) {
115     if (*I == '"')
116       Result += "_QQ_";
117     else if (*I == '\n')
118       Result += "_NL_";
119     else
120       Result += *I;
121   }
122
123   if (NeedPrefix) {
124     Result = Prefix + Result;
125
126     if (PrefixTy == Mangler::Private)
127       Result = PrivatePrefix + Result;
128     else if (PrefixTy == Mangler::LinkerPrivate)
129       Result = LinkerPrivatePrefix + Result;
130   }
131
132   Result = '"' + Result + '"';
133   return Result;
134 }
135
136 /// getMangledName - Returns the mangled name of V, an LLVM Value,
137 /// in the current module.  If 'Suffix' is specified, the name ends with the
138 /// specified suffix.  If 'ForcePrivate' is specified, the label is specified
139 /// to have a private label prefix.
140 ///
141 std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
142                                     bool ForcePrivate) {
143   assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
144          "Intrinsic functions cannot be mangled by Mangler");
145
146   ManglerPrefixTy PrefixTy =
147     (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
148       GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
149
150   if (GV->hasName())
151     return makeNameProper(GV->getNameStr() + Suffix, PrefixTy);
152   
153   // Get the ID for the global, assigning a new one if we haven't got one
154   // already.
155   unsigned &ID = AnonGlobalIDs[GV];
156   if (ID == 0) ID = NextAnonGlobalID++;
157   
158   // Must mangle the global into a unique ID.
159   return makeNameProper("__unnamed_" + utostr(ID) + Suffix, PrefixTy);
160 }
161
162
163 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
164 /// and the specified global variable's name.  If the global variable doesn't
165 /// have a name, this fills in a unique name for the global.
166 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
167                                 const GlobalValue *GV,
168                                 bool isImplicitlyPrivate) {
169    
170   // If the global is anonymous or not led with \1, then add the appropriate
171   // prefix.
172   if (!GV->hasName() || GV->getName()[0] != '\1') {
173     if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
174       OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
175     else if (GV->hasLinkerPrivateLinkage())
176       OutName.append(LinkerPrivatePrefix,
177                      LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));;
178     OutName.append(Prefix, Prefix+strlen(Prefix));
179   }
180
181   // If the global has a name, just append it now.
182   if (GV->hasName()) {
183     StringRef Name = GV->getName();
184     
185     // Strip off the prefix marker if present.
186     if (Name[0] != '\1')
187       OutName.append(Name.begin(), Name.end());
188     else
189       OutName.append(Name.begin()+1, Name.end());
190     return;
191   }
192   
193   // If the global variable doesn't have a name, return a unique name for the
194   // global based on a numbering.
195   
196   // Get the ID for the global, assigning a new one if we haven't got one
197   // already.
198   unsigned &ID = AnonGlobalIDs[GV];
199   if (ID == 0) ID = NextAnonGlobalID++;
200   
201   // Must mangle the global into a unique ID.
202   raw_svector_ostream(OutName) << "__unnamed_" << ID;
203 }
204
205
206 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
207                  const char *linkerPrivatePrefix)
208   : Prefix(prefix), PrivatePrefix(privatePrefix),
209     LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
210     NextAnonGlobalID(1) {
211   std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
212
213   // Letters and numbers are acceptable.
214   for (unsigned char X = 'a'; X <= 'z'; ++X)
215     markCharAcceptable(X);
216   for (unsigned char X = 'A'; X <= 'Z'; ++X)
217     markCharAcceptable(X);
218   for (unsigned char X = '0'; X <= '9'; ++X)
219     markCharAcceptable(X);
220   
221   // These chars are acceptable.
222   markCharAcceptable('_');
223   markCharAcceptable('$');
224   markCharAcceptable('.');
225   markCharAcceptable('@');
226 }