1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/StringExtras.h"
20 static char HexDigit(int V
) {
21 return V
< 10 ? V
+'0' : V
+'A'-10;
24 static std::string
MangleLetter(unsigned char C
) {
25 char Result
[] = { '_', HexDigit(C
>> 4), HexDigit(C
& 15), '_', 0 };
29 /// makeNameProper - We don't want identifier names non-C-identifier characters
30 /// in them, so mangle them as appropriate.
32 std::string
Mangler::makeNameProper(const std::string
&X
, const char *Prefix
) {
34 if (X
.empty()) return X
; // Empty names are uniqued by the caller.
37 // If X does not start with (char)1, add the prefix.
38 std::string::const_iterator I
= X
.begin();
42 ++I
; // Skip over the marker.
44 // Mangle the first letter specially, don't allow numbers.
45 if (*I
>= '0' && *I
<= '9')
46 Result
+= MangleLetter(*I
++);
48 for (std::string::const_iterator E
= X
.end(); I
!= E
; ++I
) {
49 if (!isCharAcceptable(*I
))
50 Result
+= MangleLetter(*I
);
55 bool NeedsQuotes
= false;
57 std::string::const_iterator I
= X
.begin();
59 ++I
; // Skip over the marker.
61 // If the first character is a number, we need quotes.
62 if (*I
>= '0' && *I
<= '9')
65 // Do an initial scan of the string, checking to see if we need quotes or
66 // to escape a '"' or not.
68 for (std::string::const_iterator E
= X
.end(); I
!= E
; ++I
)
69 if (!isCharAcceptable(*I
)) {
74 // In the common case, we don't need quotes. Handle this quickly.
82 // Otherwise, construct the string the expensive way.
85 // If X does not start with (char)1, add the prefix.
89 ++I
; // Skip the marker if present.
91 for (std::string::const_iterator E
= X
.end(); I
!= E
; ++I
) {
97 Result
= '"' + Result
+ '"';
102 /// getTypeID - Return a unique ID for the specified LLVM type.
104 unsigned Mangler::getTypeID(const Type
*Ty
) {
105 unsigned &E
= TypeMap
[Ty
];
106 if (E
== 0) E
= ++TypeCounter
;
110 std::string
Mangler::getValueName(const Value
*V
) {
111 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
))
112 return getValueName(GV
);
114 std::string
&Name
= Memo
[V
];
116 return Name
; // Return the already-computed name for V.
118 // Always mangle local names.
119 Name
= "ltmp_" + utostr(Count
++) + "_" + utostr(getTypeID(V
->getType()));
124 std::string
Mangler::getValueName(const GlobalValue
*GV
) {
125 // Check to see whether we've already named V.
126 std::string
&Name
= Memo
[GV
];
128 return Name
; // Return the already-computed name for V.
130 // Name mangling occurs as follows:
131 // - If V is an intrinsic function, do not change name at all
132 // - Otherwise, mangling occurs if global collides with existing name.
133 if (isa
<Function
>(GV
) && cast
<Function
>(GV
)->getIntrinsicID()) {
134 Name
= GV
->getName(); // Is an intrinsic function
135 } else if (!GV
->hasName()) {
136 // Must mangle the global into a unique ID.
137 unsigned TypeUniqueID
= getTypeID(GV
->getType());
138 static unsigned GlobalID
= 0;
139 Name
= "__unnamed_" + utostr(TypeUniqueID
) + "_" + utostr(GlobalID
++);
140 } else if (!MangledGlobals
.count(GV
)) {
141 Name
= makeNameProper(GV
->getName(), Prefix
);
143 unsigned TypeUniqueID
= getTypeID(GV
->getType());
144 Name
= "l" + utostr(TypeUniqueID
) + "_" + makeNameProper(GV
->getName());
150 void Mangler::InsertName(GlobalValue
*GV
,
151 std::map
<std::string
, GlobalValue
*> &Names
) {
152 if (!GV
->hasName()) // We must mangle unnamed globals.
155 // Figure out if this is already used.
156 GlobalValue
*&ExistingValue
= Names
[GV
->getName()];
157 if (!ExistingValue
) {
160 // If GV is external but the existing one is static, mangle the existing one
161 if (GV
->hasExternalLinkage() && !ExistingValue
->hasExternalLinkage()) {
162 MangledGlobals
.insert(ExistingValue
);
164 } else if (GV
->hasExternalLinkage() && ExistingValue
->hasExternalLinkage()&&
165 GV
->isExternal() && ExistingValue
->isExternal()) {
166 // If the two globals both have external inkage, and are both external,
167 // don't mangle either of them, we just have some silly type mismatch.
169 // Otherwise, mangle GV
170 MangledGlobals
.insert(GV
);
176 Mangler::Mangler(Module
&M
, const char *prefix
)
177 : Prefix(prefix
), UseQuotes(false), Count(0), TypeCounter(0) {
178 std::fill(AcceptableChars
,
179 AcceptableChars
+sizeof(AcceptableChars
)/sizeof(AcceptableChars
[0]),
182 // Letters and numbers are acceptable.
183 for (unsigned char X
= 'a'; X
<= 'z'; ++X
)
184 markCharAcceptable(X
);
185 for (unsigned char X
= 'A'; X
<= 'Z'; ++X
)
186 markCharAcceptable(X
);
187 for (unsigned char X
= '0'; X
<= '9'; ++X
)
188 markCharAcceptable(X
);
190 // These chars are acceptable.
191 markCharAcceptable('_');
192 markCharAcceptable('$');
193 markCharAcceptable('.');
195 // Calculate which global values have names that will collide when we throw
196 // away type information.
197 std::map
<std::string
, GlobalValue
*> Names
;
198 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ++I
)
199 InsertName(I
, Names
);
200 for (Module::global_iterator I
= M
.global_begin(), E
= M
.global_end(); I
!= E
; ++I
)
201 InsertName(I
, Names
);
204 // Cause this file to be linked in when Support/Mangler.h is #included
205 DEFINING_FILE_FOR(Mangler
)