Remove the InlineHint attribute. There are no current or planned
[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/SmallString.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 void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) {
28   OutName.push_back('_');
29   OutName.push_back(HexDigit(C >> 4));
30   OutName.push_back(HexDigit(C & 15));
31   OutName.push_back('_');
32 }
33
34 /// makeNameProper - We don't want identifier names non-C-identifier characters
35 /// in them, so mangle them as appropriate.
36 ///
37 /// FIXME: This is deprecated, new code should use getNameWithPrefix and use
38 /// MCSymbol printing to handle quotes or not etc.
39 ///
40 void Mangler::makeNameProper(SmallVectorImpl<char> &OutName,
41                              const Twine &TheName,
42                              ManglerPrefixTy PrefixTy) {
43   SmallString<256> TmpData;
44   StringRef X = TheName.toStringRef(TmpData);
45   assert(!X.empty() && "Cannot mangle empty strings");
46   
47   if (!UseQuotes) {
48     // If X does not start with (char)1, add the prefix.
49     StringRef::iterator I = X.begin();
50     if (*I == 1) {
51       ++I;  // Skip over the no-prefix marker.
52     } else {
53       if (PrefixTy == Mangler::Private)
54         OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
55       else if (PrefixTy == Mangler::LinkerPrivate)
56         OutName.append(LinkerPrivatePrefix,
57                        LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
58       OutName.append(Prefix, Prefix+strlen(Prefix));
59     }
60     
61     // Mangle the first letter specially, don't allow numbers unless the target
62     // explicitly allows them.
63     if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9')
64       MangleLetter(OutName, *I++);
65
66     for (StringRef::iterator E = X.end(); I != E; ++I) {
67       if (!isCharAcceptable(*I))
68         MangleLetter(OutName, *I);
69       else
70         OutName.push_back(*I);
71     }
72     return;
73   }
74
75   bool NeedPrefix = true;
76   bool NeedQuotes = false;
77   StringRef::iterator I = X.begin();
78   if (*I == 1) {
79     NeedPrefix = false;
80     ++I;  // Skip over the marker.
81   }
82
83   // If the first character is a number, we need quotes.
84   if (*I >= '0' && *I <= '9')
85     NeedQuotes = true;
86     
87   // Do an initial scan of the string, checking to see if we need quotes or
88   // to escape a '"' or not.
89   if (!NeedQuotes)
90     for (StringRef::iterator E = X.end(); I != E; ++I)
91       if (!isCharAcceptable(*I)) {
92         NeedQuotes = true;
93         break;
94       }
95     
96   // In the common case, we don't need quotes.  Handle this quickly.
97   if (!NeedQuotes) {
98     if (!NeedPrefix) {
99       OutName.append(X.begin()+1, X.end());   // Strip off the \001.
100       return;
101     }
102
103     if (PrefixTy == Mangler::Private)
104       OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
105     else if (PrefixTy == Mangler::LinkerPrivate)
106       OutName.append(LinkerPrivatePrefix,
107                      LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
108     
109     if (Prefix[0] == 0)
110       ; // Common noop, no prefix.
111     else if (Prefix[1] == 0)
112       OutName.push_back(Prefix[0]);  // Common, one character prefix.
113     else
114       OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary prefix.
115     OutName.append(X.begin(), X.end());
116     return;
117   }
118
119   // Add leading quote.
120   OutName.push_back('"');
121   
122   // Add prefixes unless disabled.
123   if (NeedPrefix) {
124     if (PrefixTy == Mangler::Private)
125       OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
126     else if (PrefixTy == Mangler::LinkerPrivate)
127       OutName.append(LinkerPrivatePrefix,
128                      LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
129     OutName.append(Prefix, Prefix+strlen(Prefix));
130   }
131   
132   // Add the piece that we already scanned through.
133   OutName.append(X.begin()+!NeedPrefix, I);
134   
135   // Otherwise, construct the string the expensive way.
136   for (StringRef::iterator E = X.end(); I != E; ++I) {
137     if (*I == '"') {
138       const char *Quote = "_QQ_";
139       OutName.append(Quote, Quote+4);
140     } else if (*I == '\n') {
141       const char *Newline = "_NL_";
142       OutName.append(Newline, Newline+4);
143     } else
144       OutName.push_back(*I);
145   }
146
147   // Add trailing quote.
148   OutName.push_back('"');
149 }
150
151 /// getMangledName - Returns the mangled name of V, an LLVM Value,
152 /// in the current module.  If 'Suffix' is specified, the name ends with the
153 /// specified suffix.  If 'ForcePrivate' is specified, the label is specified
154 /// to have a private label prefix.
155 ///
156 /// FIXME: This is deprecated, new code should use getNameWithPrefix and use
157 /// MCSymbol printing to handle quotes or not etc.
158 ///
159 std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
160                                     bool ForcePrivate) {
161   assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
162          "Intrinsic functions cannot be mangled by Mangler");
163
164   ManglerPrefixTy PrefixTy =
165     (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
166       GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
167
168   SmallString<128> Result;
169   if (GV->hasName()) {
170     makeNameProper(Result, GV->getNameStr() + Suffix, PrefixTy);
171     return Result.str().str();
172   }
173   
174   // Get the ID for the global, assigning a new one if we haven't got one
175   // already.
176   unsigned &ID = AnonGlobalIDs[GV];
177   if (ID == 0) ID = NextAnonGlobalID++;
178   
179   // Must mangle the global into a unique ID.
180   makeNameProper(Result, "__unnamed_" + utostr(ID) + Suffix, PrefixTy);
181   return Result.str().str();
182 }
183
184 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
185 /// and the specified name as the global variable name.  GVName must not be
186 /// empty.
187 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
188                                 const Twine &GVName, ManglerPrefixTy PrefixTy) {
189   SmallString<256> TmpData;
190   StringRef Name = GVName.toStringRef(TmpData);
191   assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
192   
193   // If the global name is not led with \1, add the appropriate prefixes.
194   if (Name[0] != '\1') {
195     if (PrefixTy == Mangler::Private)
196       OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
197     else if (PrefixTy == Mangler::LinkerPrivate)
198       OutName.append(LinkerPrivatePrefix,
199                      LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
200     
201     if (Prefix[0] == 0)
202       ; // Common noop, no prefix.
203     else if (Prefix[1] == 0)
204       OutName.push_back(Prefix[0]);  // Common, one character prefix.
205     else
206       OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary prefix.
207   } else {
208     Name = Name.substr(1);
209   }
210   
211   OutName.append(Name.begin(), Name.end());
212 }
213
214
215 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
216 /// and the specified global variable's name.  If the global variable doesn't
217 /// have a name, this fills in a unique name for the global.
218 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
219                                 const GlobalValue *GV,
220                                 bool isImplicitlyPrivate) {
221   // If this global has a name, handle it simply.
222   if (GV->hasName()) {
223     ManglerPrefixTy PrefixTy = Mangler::Default;
224     if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
225       PrefixTy = Mangler::Private;
226     else if (GV->hasLinkerPrivateLinkage())
227       PrefixTy = Mangler::LinkerPrivate;
228     
229     return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
230   }
231   
232   // If the global variable doesn't have a name, return a unique name for the
233   // global based on a numbering.
234   
235   // Anonymous names always get prefixes.
236   if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
237     OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
238   else if (GV->hasLinkerPrivateLinkage())
239     OutName.append(LinkerPrivatePrefix,
240                    LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));;
241   OutName.append(Prefix, Prefix+strlen(Prefix));
242   
243   // Get the ID for the global, assigning a new one if we haven't got one
244   // already.
245   unsigned &ID = AnonGlobalIDs[GV];
246   if (ID == 0) ID = NextAnonGlobalID++;
247   
248   // Must mangle the global into a unique ID.
249   raw_svector_ostream(OutName) << "__unnamed_" << ID;
250 }
251
252
253 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
254                  const char *linkerPrivatePrefix)
255   : Prefix(prefix), PrivatePrefix(privatePrefix),
256     LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
257     SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) {
258   std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
259
260   // Letters and numbers are acceptable.
261   for (unsigned char X = 'a'; X <= 'z'; ++X)
262     markCharAcceptable(X);
263   for (unsigned char X = 'A'; X <= 'Z'; ++X)
264     markCharAcceptable(X);
265   for (unsigned char X = '0'; X <= '9'; ++X)
266     markCharAcceptable(X);
267   
268   // These chars are acceptable.
269   markCharAcceptable('_');
270   markCharAcceptable('$');
271   markCharAcceptable('.');
272   markCharAcceptable('@');
273 }