Disable JITTest.FunctionIsRecompiledAndRelinked on ARM where it's not
[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 unless the target
51     // explicitly allows them.
52     if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9')
53       Result += MangleLetter(*I++);
54
55     for (std::string::const_iterator E = X.end(); I != E; ++I) {
56       if (!isCharAcceptable(*I))
57         Result += MangleLetter(*I);
58       else
59         Result += *I;
60     }
61
62     if (NeedPrefix) {
63       Result = Prefix + Result;
64
65       if (PrefixTy == Mangler::Private)
66         Result = PrivatePrefix + Result;
67       else if (PrefixTy == Mangler::LinkerPrivate)
68         Result = LinkerPrivatePrefix + Result;
69     }
70
71     return Result;
72   }
73
74   bool NeedPrefix = true;
75   bool NeedQuotes = false;
76   std::string Result;    
77   std::string::const_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 (std::string::const_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       return X.substr(1);   // Strip off the \001.
100     
101     Result = Prefix + X;
102
103     if (PrefixTy == Mangler::Private)
104       Result = PrivatePrefix + Result;
105     else if (PrefixTy == Mangler::LinkerPrivate)
106       Result = LinkerPrivatePrefix + Result;
107
108     return Result;
109   }
110
111   if (NeedPrefix)
112     Result = X.substr(0, I-X.begin());
113     
114   // Otherwise, construct the string the expensive way.
115   for (std::string::const_iterator E = X.end(); I != E; ++I) {
116     if (*I == '"')
117       Result += "_QQ_";
118     else if (*I == '\n')
119       Result += "_NL_";
120     else
121       Result += *I;
122   }
123
124   if (NeedPrefix) {
125     Result = Prefix + Result;
126
127     if (PrefixTy == Mangler::Private)
128       Result = PrivatePrefix + Result;
129     else if (PrefixTy == Mangler::LinkerPrivate)
130       Result = LinkerPrivatePrefix + Result;
131   }
132
133   Result = '"' + Result + '"';
134   return Result;
135 }
136
137 /// getMangledName - Returns the mangled name of V, an LLVM Value,
138 /// in the current module.  If 'Suffix' is specified, the name ends with the
139 /// specified suffix.  If 'ForcePrivate' is specified, the label is specified
140 /// to have a private label prefix.
141 ///
142 std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
143                                     bool ForcePrivate) {
144   assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
145          "Intrinsic functions cannot be mangled by Mangler");
146
147   ManglerPrefixTy PrefixTy =
148     (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
149       GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
150
151   if (GV->hasName())
152     return makeNameProper(GV->getNameStr() + Suffix, PrefixTy);
153   
154   // Get the ID for the global, assigning a new one if we haven't got one
155   // already.
156   unsigned &ID = AnonGlobalIDs[GV];
157   if (ID == 0) ID = NextAnonGlobalID++;
158   
159   // Must mangle the global into a unique ID.
160   return makeNameProper("__unnamed_" + utostr(ID) + Suffix, PrefixTy);
161 }
162
163
164 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
165 /// and the specified global variable's name.  If the global variable doesn't
166 /// have a name, this fills in a unique name for the global.
167 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
168                                 const GlobalValue *GV,
169                                 bool isImplicitlyPrivate) {
170    
171   // If the global is anonymous or not led with \1, then add the appropriate
172   // prefix.
173   if (!GV->hasName() || GV->getName()[0] != '\1') {
174     if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
175       OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
176     else if (GV->hasLinkerPrivateLinkage())
177       OutName.append(LinkerPrivatePrefix,
178                      LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));;
179     OutName.append(Prefix, Prefix+strlen(Prefix));
180   }
181
182   // If the global has a name, just append it now.
183   if (GV->hasName()) {
184     StringRef Name = GV->getName();
185     
186     // Strip off the prefix marker if present.
187     if (Name[0] != '\1')
188       OutName.append(Name.begin(), Name.end());
189     else
190       OutName.append(Name.begin()+1, Name.end());
191     return;
192   }
193   
194   // If the global variable doesn't have a name, return a unique name for the
195   // global based on a numbering.
196   
197   // Get the ID for the global, assigning a new one if we haven't got one
198   // already.
199   unsigned &ID = AnonGlobalIDs[GV];
200   if (ID == 0) ID = NextAnonGlobalID++;
201   
202   // Must mangle the global into a unique ID.
203   raw_svector_ostream(OutName) << "__unnamed_" << ID;
204 }
205
206
207 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
208                  const char *linkerPrivatePrefix)
209   : Prefix(prefix), PrivatePrefix(privatePrefix),
210     LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
211     SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) {
212   std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
213
214   // Letters and numbers are acceptable.
215   for (unsigned char X = 'a'; X <= 'z'; ++X)
216     markCharAcceptable(X);
217   for (unsigned char X = 'A'; X <= 'Z'; ++X)
218     markCharAcceptable(X);
219   for (unsigned char X = '0'; X <= '9'; ++X)
220     markCharAcceptable(X);
221   
222   // These chars are acceptable.
223   markCharAcceptable('_');
224   markCharAcceptable('$');
225   markCharAcceptable('.');
226   markCharAcceptable('@');
227 }