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 (!MangledGlobals.count(GV)) {
136 Name = makeNameProper(GV->getName(), Prefix);
138 unsigned TypeUniqueID = getTypeID(GV->getType());
139 Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
145 void Mangler::InsertName(GlobalValue *GV,
146 std::map<std::string, GlobalValue*> &Names) {
147 if (!GV->hasName()) { // We must mangle unnamed globals.
148 MangledGlobals.insert(GV);
152 // Figure out if this is already used.
153 GlobalValue *&ExistingValue = Names[GV->getName()];
154 if (!ExistingValue) {
157 // If GV is external but the existing one is static, mangle the existing one
158 if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) {
159 MangledGlobals.insert(ExistingValue);
162 // Otherwise, mangle GV
163 MangledGlobals.insert(GV);
169 Mangler::Mangler(Module &M, const char *prefix)
170 : Prefix(prefix), UseQuotes(false), Count(0), TypeCounter(0) {
171 std::fill(AcceptableChars,
172 AcceptableChars+sizeof(AcceptableChars)/sizeof(AcceptableChars[0]),
175 // Letters and numbers are acceptable.
176 for (unsigned char X = 'a'; X <= 'z'; ++X)
177 markCharAcceptable(X);
178 for (unsigned char X = 'A'; X <= 'Z'; ++X)
179 markCharAcceptable(X);
180 for (unsigned char X = '0'; X <= '9'; ++X)
181 markCharAcceptable(X);
183 // These chars are acceptable.
184 markCharAcceptable('_');
185 markCharAcceptable('$');
186 markCharAcceptable('.');
188 // Calculate which global values have names that will collide when we throw
189 // away type information.
190 std::map<std::string, GlobalValue*> Names;
191 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
192 InsertName(I, Names);
193 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
194 InsertName(I, Names);