1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Unified name mangler for CWriter and assembly backends.
12 //===----------------------------------------------------------------------===//
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"
22 static char HexDigit(int V
) {
23 return V
< 10 ? V
+'0' : V
+'A'-10;
26 static std::string
MangleLetter(unsigned char C
) {
27 char Result
[] = { '_', HexDigit(C
>> 4), HexDigit(C
& 15), '_', 0 };
31 /// makeNameProper - We don't want identifier names non-C-identifier characters
32 /// in them, so mangle them as appropriate.
34 std::string
Mangler::makeNameProper(const std::string
&X
, const char *Prefix
,
35 const char *PrivatePrefix
) {
36 if (X
.empty()) return X
; // Empty names are uniqued by the caller.
38 // If PreserveAsmNames is set, names with asm identifiers are not modified.
39 if (PreserveAsmNames
&& X
[0] == 1)
45 // If X does not start with (char)1, add the prefix.
46 bool NeedPrefix
= true;
47 std::string::const_iterator I
= X
.begin();
50 ++I
; // Skip over the marker.
53 // Mangle the first letter specially, don't allow numbers.
54 if (*I
>= '0' && *I
<= '9')
55 Result
+= MangleLetter(*I
++);
57 for (std::string::const_iterator E
= X
.end(); I
!= E
; ++I
) {
58 if (!isCharAcceptable(*I
))
59 Result
+= MangleLetter(*I
);
66 Result
= Prefix
+ Result
;
68 Result
= PrivatePrefix
+ Result
;
73 bool NeedPrefix
= true;
74 bool NeedQuotes
= false;
76 std::string::const_iterator I
= X
.begin();
79 ++I
; // Skip over the marker.
82 // If the first character is a number, we need quotes.
83 if (*I
>= '0' && *I
<= '9')
86 // Do an initial scan of the string, checking to see if we need quotes or
87 // to escape a '"' or not.
89 for (std::string::const_iterator E
= X
.end(); I
!= E
; ++I
)
90 if (!isCharAcceptable(*I
)) {
95 // In the common case, we don't need quotes. Handle this quickly.
103 Result
= PrivatePrefix
+ Result
;
109 // Otherwise, construct the string the expensive way.
110 for (std::string::const_iterator E
= X
.end(); I
!= E
; ++I
) {
125 Result
= PrivatePrefix
+ Result
;
127 Result
= '"' + Result
+ '"';
131 /// getTypeID - Return a unique ID for the specified LLVM type.
133 unsigned Mangler::getTypeID(const Type
*Ty
) {
134 unsigned &E
= TypeMap
[Ty
];
135 if (E
== 0) E
= ++TypeCounter
;
139 std::string
Mangler::getValueName(const Value
*V
) {
140 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
))
141 return getValueName(GV
);
143 std::string
&Name
= Memo
[V
];
145 return Name
; // Return the already-computed name for V.
147 // Always mangle local names.
148 Name
= "ltmp_" + utostr(Count
++) + "_" + utostr(getTypeID(V
->getType()));
153 std::string
Mangler::getValueName(const GlobalValue
*GV
, const char * Suffix
) {
154 // Check to see whether we've already named V.
155 std::string
&Name
= Memo
[GV
];
157 return Name
; // Return the already-computed name for V.
159 // Name mangling occurs as follows:
160 // - If V is an intrinsic function, do not change name at all
161 // - Otherwise, mangling occurs if global collides with existing name.
162 if (isa
<Function
>(GV
) && cast
<Function
>(GV
)->isIntrinsic()) {
163 Name
= GV
->getNameStart(); // Is an intrinsic function
164 } else if (!GV
->hasName()) {
165 // Must mangle the global into a unique ID.
166 unsigned TypeUniqueID
= getTypeID(GV
->getType());
167 static unsigned GlobalID
= 0;
168 Name
= "__unnamed_" + utostr(TypeUniqueID
) + "_" + utostr(GlobalID
++);
170 if (GV
->hasPrivateLinkage())
171 Name
= makeNameProper(GV
->getName() + Suffix
, Prefix
, PrivatePrefix
);
173 Name
= makeNameProper(GV
->getName() + Suffix
, Prefix
);
179 Mangler::Mangler(Module
&M
, const char *prefix
, const char *privatePrefix
)
180 : Prefix(prefix
), PrivatePrefix (privatePrefix
), UseQuotes(false),
181 PreserveAsmNames(false), Count(0), TypeCounter(0) {
182 std::fill(AcceptableChars
, array_endof(AcceptableChars
), 0);
184 // Letters and numbers are acceptable.
185 for (unsigned char X
= 'a'; X
<= 'z'; ++X
)
186 markCharAcceptable(X
);
187 for (unsigned char X
= 'A'; X
<= 'Z'; ++X
)
188 markCharAcceptable(X
);
189 for (unsigned char X
= '0'; X
<= '9'; ++X
)
190 markCharAcceptable(X
);
192 // These chars are acceptable.
193 markCharAcceptable('_');
194 markCharAcceptable('$');
195 markCharAcceptable('.');