Protect the GC table in Function.cpp
[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/System/Atomic.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringMap.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, const char *Prefix,
36                                     const char *PrivatePrefix) {
37   if (X.empty()) return X;  // Empty names are uniqued by the caller.
38   
39   // If PreserveAsmNames is set, names with asm identifiers are not modified. 
40   if (PreserveAsmNames && X[0] == 1)
41     return X;
42   
43   if (!UseQuotes) {
44     std::string Result;
45
46     // If X does not start with (char)1, add the prefix.
47     bool NeedPrefix = true;
48     std::string::const_iterator I = X.begin();
49     if (*I == 1) {
50       NeedPrefix = false;
51       ++I;  // Skip over the marker.
52     }
53     
54     // Mangle the first letter specially, don't allow numbers.
55     if (*I >= '0' && *I <= '9')
56       Result += MangleLetter(*I++);
57
58     for (std::string::const_iterator E = X.end(); I != E; ++I) {
59       if (!isCharAcceptable(*I))
60         Result += MangleLetter(*I);
61       else
62         Result += *I;
63     }
64
65     if (NeedPrefix) {
66       if (Prefix)
67         Result = Prefix + Result;
68       if (PrivatePrefix)
69         Result = PrivatePrefix + Result;
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       if (Prefix)
100         Result = Prefix + X;
101       else
102         Result = X;
103       if (PrivatePrefix)
104         Result = PrivatePrefix + Result;
105       return Result;
106     } else
107       return X.substr(1);
108   }
109     
110   // Otherwise, construct the string the expensive way.
111   for (std::string::const_iterator E = X.end(); I != E; ++I) {
112     if (*I == '"')
113       Result += "_QQ_";
114     else if (*I == '\n')
115       Result += "_NL_";
116     else
117       Result += *I;
118   }
119
120   if (NeedPrefix) {
121     if (Prefix)
122       Result = Prefix + X;
123     else
124       Result = X;
125     if (PrivatePrefix)
126       Result = PrivatePrefix + Result;
127   }
128   Result = '"' + Result + '"';
129   return Result;
130 }
131
132 /// getTypeID - Return a unique ID for the specified LLVM type.
133 ///
134 unsigned Mangler::getTypeID(const Type *Ty) {
135   unsigned &E = TypeMap[Ty];
136   if (E == 0) E = ++TypeCounter;
137   return E;
138 }
139
140 std::string Mangler::getValueName(const Value *V) {
141   if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
142     return getValueName(GV);
143   
144   std::string &Name = Memo[V];
145   if (!Name.empty())
146     return Name;       // Return the already-computed name for V.
147   
148   // Always mangle local names.
149   Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
150   return Name;
151 }
152
153
154 std::string Mangler::getValueName(const GlobalValue *GV, const char * Suffix) {
155   // Check to see whether we've already named V.
156   std::string &Name = Memo[GV];
157   if (!Name.empty())
158     return Name;       // Return the already-computed name for V.
159
160   // Name mangling occurs as follows:
161   // - If V is an intrinsic function, do not change name at all
162   // - Otherwise, mangling occurs if global collides with existing name.
163   if (isa<Function>(GV) && cast<Function>(GV)->isIntrinsic()) {
164     Name = GV->getNameStart(); // Is an intrinsic function
165   } else if (!GV->hasName()) {
166     // Must mangle the global into a unique ID.
167     unsigned TypeUniqueID = getTypeID(GV->getType());
168     static uint32_t GlobalID = 0;
169     
170     unsigned OldID = GlobalID;
171     sys::AtomicIncrement(&GlobalID);
172     
173     Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(OldID);
174   } else {
175     if (GV->hasPrivateLinkage())
176       Name = makeNameProper(GV->getName() + Suffix, Prefix, PrivatePrefix);
177     else
178       Name = makeNameProper(GV->getName() + Suffix, Prefix);
179   }
180
181   return Name;
182 }
183
184 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix)
185   : Prefix(prefix), PrivatePrefix (privatePrefix), UseQuotes(false),
186     PreserveAsmNames(false), Count(0), TypeCounter(0) {
187   std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
188
189   // Letters and numbers are acceptable.
190   for (unsigned char X = 'a'; X <= 'z'; ++X)
191     markCharAcceptable(X);
192   for (unsigned char X = 'A'; X <= 'Z'; ++X)
193     markCharAcceptable(X);
194   for (unsigned char X = '0'; X <= '9'; ++X)
195     markCharAcceptable(X);
196   
197   // These chars are acceptable.
198   markCharAcceptable('_');
199   markCharAcceptable('$');
200   markCharAcceptable('.');
201 }