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/System/Atomic.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringMap.h"
23 static char HexDigit(int V) {
24 return V < 10 ? V+'0' : V+'A'-10;
27 static std::string MangleLetter(unsigned char C) {
28 char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
32 /// makeNameProper - We don't want identifier names non-C-identifier characters
33 /// in them, so mangle them as appropriate.
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.
39 // If PreserveAsmNames is set, names with asm identifiers are not modified.
40 if (PreserveAsmNames && X[0] == 1)
46 // If X does not start with (char)1, add the prefix.
47 bool NeedPrefix = true;
48 std::string::const_iterator I = X.begin();
51 ++I; // Skip over the marker.
54 // Mangle the first letter specially, don't allow numbers.
55 if (*I >= '0' && *I <= '9')
56 Result += MangleLetter(*I++);
58 for (std::string::const_iterator E = X.end(); I != E; ++I) {
59 if (!isCharAcceptable(*I))
60 Result += MangleLetter(*I);
67 Result = Prefix + Result;
69 Result = PrivatePrefix + Result;
74 bool NeedPrefix = true;
75 bool NeedQuotes = false;
77 std::string::const_iterator I = X.begin();
80 ++I; // Skip over the marker.
83 // If the first character is a number, we need quotes.
84 if (*I >= '0' && *I <= '9')
87 // Do an initial scan of the string, checking to see if we need quotes or
88 // to escape a '"' or not.
90 for (std::string::const_iterator E = X.end(); I != E; ++I)
91 if (!isCharAcceptable(*I)) {
96 // In the common case, we don't need quotes. Handle this quickly.
104 Result = PrivatePrefix + Result;
110 // Otherwise, construct the string the expensive way.
111 for (std::string::const_iterator E = X.end(); I != E; ++I) {
126 Result = PrivatePrefix + Result;
128 Result = '"' + Result + '"';
132 /// getTypeID - Return a unique ID for the specified LLVM type.
134 unsigned Mangler::getTypeID(const Type *Ty) {
135 unsigned &E = TypeMap[Ty];
136 if (E == 0) E = ++TypeCounter;
140 std::string Mangler::getValueName(const Value *V) {
141 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
142 return getValueName(GV);
144 std::string &Name = Memo[V];
146 return Name; // Return the already-computed name for V.
148 // Always mangle local names.
149 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
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];
158 return Name; // Return the already-computed name for V.
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;
170 unsigned OldID = GlobalID;
171 sys::AtomicIncrement(&GlobalID);
173 Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(OldID);
175 if (GV->hasPrivateLinkage())
176 Name = makeNameProper(GV->getName() + Suffix, Prefix, PrivatePrefix);
178 Name = makeNameProper(GV->getName() + Suffix, Prefix);
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);
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);
197 // These chars are acceptable.
198 markCharAcceptable('_');
199 markCharAcceptable('$');
200 markCharAcceptable('.');