Reapply my previous asmprinter changes now with more testing and two
[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/DerivedTypes.h"
16 #include "llvm/Module.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/StringMap.h"
20 using namespace llvm;
21
22 static char HexDigit(int V) {
23   return V < 10 ? V+'0' : V+'A'-10;
24 }
25
26 static std::string MangleLetter(unsigned char C) {
27   char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
28   return Result;
29 }
30
31 /// makeNameProper - We don't want identifier names non-C-identifier characters
32 /// in them, so mangle them as appropriate.
33 ///
34 std::string Mangler::makeNameProper(const std::string &X,
35                                     bool hasPrivateLinkage) {
36   assert(!X.empty() && "Cannot mangle empty strings");
37   
38   // If PreserveAsmNames is set, names with asm identifiers are not modified. 
39   if (PreserveAsmNames && X[0] == 1)
40     return X;
41
42   if (!UseQuotes) {
43     std::string Result;
44
45     // If X does not start with (char)1, add the prefix.
46     bool NeedPrefix = true;
47     std::string::const_iterator I = X.begin();
48     if (*I == 1) {
49       NeedPrefix = false;
50       ++I;  // Skip over the marker.
51     }
52     
53     // Mangle the first letter specially, don't allow numbers.
54     if (*I >= '0' && *I <= '9')
55       Result += MangleLetter(*I++);
56
57     for (std::string::const_iterator E = X.end(); I != E; ++I) {
58       if (!isCharAcceptable(*I))
59         Result += MangleLetter(*I);
60       else
61         Result += *I;
62     }
63
64     if (NeedPrefix) {
65       Result = Prefix + Result;
66       if (hasPrivateLinkage)
67         Result = PrivatePrefix + Result;
68     }
69     return Result;
70   }
71
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) {
96     if (!NeedPrefix)
97       return X.substr(1);   // Strip off the \001.
98     
99     Result = Prefix + X;
100     if (hasPrivateLinkage)
101       Result = PrivatePrefix + Result;
102     return Result;
103   }
104   
105   Result = X.substr(0, I-X.begin());
106     
107   // Otherwise, construct the string the expensive way.
108   for (std::string::const_iterator E = X.end(); I != E; ++I) {
109     if (*I == '"')
110       Result += "_QQ_";
111     else if (*I == '\n')
112       Result += "_NL_";
113     else
114       Result += *I;
115   }
116
117   if (NeedPrefix) {
118     Result = Prefix + Result;
119     if (hasPrivateLinkage)
120       Result = PrivatePrefix + Result;
121   }
122   Result = '"' + Result + '"';
123   return Result;
124 }
125
126 /// getMangledName - Returns the mangled name of V, an LLVM Value,
127 /// in the current module.  If 'Suffix' is specified, the name ends with the
128 /// specified suffix.  If 'ForcePrivate' is specified, the label is specified
129 /// to have a private label prefix.
130 ///
131 std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
132                                     bool ForcePrivate) {
133   assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
134          "Intrinsic functions cannot be mangled by Mangler");
135   
136   if (GV->hasName())
137     return makeNameProper(GV->getName() + Suffix,
138                           GV->hasPrivateLinkage() | ForcePrivate);
139   
140   // Get the ID for the global, assigning a new one if we haven't got one
141   // already.
142   unsigned &ID = AnonGlobalIDs[GV];
143   if (ID == 0) ID = NextAnonGlobalID++;
144   
145   // Must mangle the global into a unique ID.
146   return makeNameProper("__unnamed_" + utostr(ID) + Suffix,
147                         GV->hasPrivateLinkage() | ForcePrivate);
148 }
149
150 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix)
151   : Prefix(prefix), PrivatePrefix (privatePrefix), UseQuotes(false),
152     PreserveAsmNames(false), NextAnonGlobalID(1) {
153   std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
154
155   // Letters and numbers are acceptable.
156   for (unsigned char X = 'a'; X <= 'z'; ++X)
157     markCharAcceptable(X);
158   for (unsigned char X = 'A'; X <= 'Z'; ++X)
159     markCharAcceptable(X);
160   for (unsigned char X = '0'; X <= '9'; ++X)
161     markCharAcceptable(X);
162   
163   // These chars are acceptable.
164   markCharAcceptable('_');
165   markCharAcceptable('$');
166   markCharAcceptable('.');
167 }