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"
21 static char HexDigit(int V) {
22 return V < 10 ? V+'0' : V+'A'-10;
25 static std::string MangleLetter(unsigned char C) {
26 char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
30 /// makeNameProper - We don't want identifier names non-C-identifier characters
31 /// in them, so mangle them as appropriate.
33 std::string Mangler::makeNameProper(const std::string &X, const char *Prefix) {
35 if (X.empty()) return X; // Empty names are uniqued by the caller.
37 // If PreserveAsmNames is set, names with asm identifiers are not modified.
38 if (PreserveAsmNames && X[0] == 1)
42 // If X does not start with (char)1, add the prefix.
43 std::string::const_iterator I = X.begin();
47 ++I; // Skip over the marker.
49 // Mangle the first letter specially, don't allow numbers.
50 if (*I >= '0' && *I <= '9')
51 Result += MangleLetter(*I++);
53 for (std::string::const_iterator E = X.end(); I != E; ++I) {
54 if (!isCharAcceptable(*I))
55 Result += MangleLetter(*I);
60 bool NeedsQuotes = false;
62 std::string::const_iterator I = X.begin();
64 ++I; // Skip over the marker.
66 // If the first character is a number, we need quotes.
67 if (*I >= '0' && *I <= '9')
70 // Do an initial scan of the string, checking to see if we need quotes or
71 // to escape a '"' or not.
73 for (std::string::const_iterator E = X.end(); I != E; ++I)
74 if (!isCharAcceptable(*I)) {
79 // In the common case, we don't need quotes. Handle this quickly.
87 // Otherwise, construct the string the expensive way.
90 // If X does not start with (char)1, add the prefix.
94 ++I; // Skip the marker if present.
96 for (std::string::const_iterator E = X.end(); I != E; ++I) {
104 Result = '"' + Result + '"';
109 /// getTypeID - Return a unique ID for the specified LLVM type.
111 unsigned Mangler::getTypeID(const Type *Ty) {
112 unsigned &E = TypeMap[Ty];
113 if (E == 0) E = ++TypeCounter;
117 std::string Mangler::getValueName(const Value *V) {
118 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
119 return getValueName(GV);
121 std::string &Name = Memo[V];
123 return Name; // Return the already-computed name for V.
125 // Always mangle local names.
126 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
131 std::string Mangler::getValueName(const GlobalValue *GV, const char * Suffix) {
132 // Check to see whether we've already named V.
133 std::string &Name = Memo[GV];
135 return Name; // Return the already-computed name for V.
137 // Name mangling occurs as follows:
138 // - If V is an intrinsic function, do not change name at all
139 // - Otherwise, mangling occurs if global collides with existing name.
140 if (isa<Function>(GV) && cast<Function>(GV)->isIntrinsic()) {
141 Name = GV->getName(); // Is an intrinsic function
142 } else if (!GV->hasName()) {
143 // Must mangle the global into a unique ID.
144 unsigned TypeUniqueID = getTypeID(GV->getType());
145 static unsigned GlobalID = 0;
146 Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(GlobalID++);
147 } else if (!MangledGlobals.count(GV)) {
148 Name = makeNameProper(GV->getName() + Suffix, Prefix);
150 unsigned TypeUniqueID = getTypeID(GV->getType());
151 Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
157 void Mangler::InsertName(GlobalValue *GV,
158 std::map<std::string, GlobalValue*> &Names) {
159 if (!GV->hasName()) // We must mangle unnamed globals.
162 // Figure out if this is already used.
163 GlobalValue *&ExistingValue = Names[GV->getName()];
164 if (!ExistingValue) {
167 // If GV is external but the existing one is static, mangle the existing one
168 if ((GV->hasExternalLinkage() || GV->hasDLLImportLinkage()) &&
169 !(ExistingValue->hasExternalLinkage() || ExistingValue->hasDLLImportLinkage())) {
170 MangledGlobals.insert(ExistingValue);
172 } else if ((GV->hasExternalLinkage() ||
173 GV->hasDLLImportLinkage()) &&
174 (ExistingValue->hasExternalLinkage() ||
175 ExistingValue->hasDLLImportLinkage()) &&
176 GV->isDeclaration() &&
177 ExistingValue->isDeclaration()) {
178 // If the two globals both have external inkage, and are both external,
179 // don't mangle either of them, we just have some silly type mismatch.
181 // Otherwise, mangle GV
182 MangledGlobals.insert(GV);
188 Mangler::Mangler(Module &M, const char *prefix)
189 : Prefix(prefix), UseQuotes(false), PreserveAsmNames(false),
190 Count(0), TypeCounter(0) {
191 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
193 // Letters and numbers are acceptable.
194 for (unsigned char X = 'a'; X <= 'z'; ++X)
195 markCharAcceptable(X);
196 for (unsigned char X = 'A'; X <= 'Z'; ++X)
197 markCharAcceptable(X);
198 for (unsigned char X = '0'; X <= '9'; ++X)
199 markCharAcceptable(X);
201 // These chars are acceptable.
202 markCharAcceptable('_');
203 markCharAcceptable('$');
204 markCharAcceptable('.');
206 // Calculate which global values have names that will collide when we throw
207 // away type information.
208 std::map<std::string, GlobalValue*> Names;
209 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
210 InsertName(I, Names);
211 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
212 InsertName(I, Names);