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,
35 bool hasPrivateLinkage) {
36 assert(!X.empty() && "Cannot mangle empty strings");
38 // If PreserveAsmNames is set, names with asm identifiers are not modified.
39 if (PreserveAsmNames && X[0] == 1)
42 // Figure out the prefix to use.
43 const char *ThePrefix = hasPrivateLinkage ? PrivatePrefix : Prefix;
48 // If X does not start with (char)1, add the prefix.
49 bool NeedPrefix = true;
50 std::string::const_iterator I = X.begin();
53 ++I; // Skip over the marker.
56 // Mangle the first letter specially, don't allow numbers.
57 if (*I >= '0' && *I <= '9')
58 Result += MangleLetter(*I++);
60 for (std::string::const_iterator E = X.end(); I != E; ++I) {
61 if (!isCharAcceptable(*I))
62 Result += MangleLetter(*I);
68 Result = ThePrefix + Result;
72 bool NeedPrefix = true;
73 bool NeedQuotes = false;
75 std::string::const_iterator I = X.begin();
78 ++I; // Skip over the marker.
81 // If the first character is a number, we need quotes.
82 if (*I >= '0' && *I <= '9')
85 // Do an initial scan of the string, checking to see if we need quotes or
86 // to escape a '"' or not.
88 for (std::string::const_iterator E = X.end(); I != E; ++I)
89 if (!isCharAcceptable(*I)) {
94 // In the common case, we don't need quotes. Handle this quickly.
97 return X.substr(1); // Strip off the \001.
101 Result = X.substr(0, I-X.begin());
103 // Otherwise, construct the string the expensive way.
104 for (std::string::const_iterator E = X.end(); I != E; ++I) {
114 Result = ThePrefix + Result;
115 Result = '"' + Result + '"';
119 std::string Mangler::getValueName(const GlobalValue *GV, const char *Suffix) {
120 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
121 "Intrinsic functions cannot be mangled by Mangler");
124 return makeNameProper(GV->getName() + Suffix, GV->hasPrivateLinkage());
126 // Get the ID for the global, assigning a new one if we haven't got one
128 unsigned &ID = AnonGlobalIDs[GV];
129 if (ID == 0) ID = NextAnonGlobalID++;
131 // Must mangle the global into a unique ID.
132 return "__unnamed_" + utostr(ID) + Suffix;
135 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix)
136 : Prefix(prefix), PrivatePrefix (privatePrefix), UseQuotes(false),
137 PreserveAsmNames(false), NextAnonGlobalID(1) {
138 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
140 // Letters and numbers are acceptable.
141 for (unsigned char X = 'a'; X <= 'z'; ++X)
142 markCharAcceptable(X);
143 for (unsigned char X = 'A'; X <= 'Z'; ++X)
144 markCharAcceptable(X);
145 for (unsigned char X = '0'; X <= '9'; ++X)
146 markCharAcceptable(X);
148 // These chars are acceptable.
149 markCharAcceptable('_');
150 markCharAcceptable('$');
151 markCharAcceptable('.');