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/Module.h"
16 #include "llvm/Type.h"
17 #include "Support/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 return std::string("_")+HexDigit(C >> 4) + HexDigit(C & 15) + "_";
28 /// makeNameProper - We don't want identifier names non-C-identifier characters
29 /// in them, so mangle them as appropriate.
31 std::string Mangler::makeNameProper(const std::string &X) {
34 // Mangle the first letter specially, don't allow numbers...
35 if ((X[0] < 'a' || X[0] > 'z') && (X[0] < 'A' || X[0] > 'Z') && X[0] != '_')
36 Result += MangleLetter(X[0]);
40 for (std::string::const_iterator I = X.begin()+1, E = X.end(); I != E; ++I)
41 if ((*I < 'a' || *I > 'z') && (*I < 'A' || *I > 'Z') &&
42 (*I < '0' || *I > '9') && *I != '_')
43 Result += MangleLetter(*I);
49 std::string Mangler::getValueName(const Value *V) {
50 // Check to see whether we've already named V.
51 ValueMap::iterator VI = Memo.find(V);
52 if (VI != Memo.end()) {
53 return VI->second; // Return the old name for V.
57 if (V->hasName()) { // Print out the label if it exists...
58 // Name mangling occurs as follows:
59 // - If V is not a global, mangling always occurs.
60 // - Otherwise, mangling occurs when any of the following are true:
61 // 1) V has internal linkage
62 // 2) V's name would collide if it is not mangled.
64 const GlobalValue* gv = dyn_cast<GlobalValue>(V);
65 if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
66 name = makeNameProper(gv->getName());
67 if (AddUnderscorePrefix) name = "_" + name;
69 // Non-global, or global with internal linkage / colliding name
71 name = "l" + utostr(V->getType()->getUniqueID()) + "_" +
72 makeNameProper(V->getName());
75 name = "ltmp_" + utostr(Count++) + "_"
76 + utostr(V->getType()->getUniqueID());
83 void Mangler::InsertName(GlobalValue *GV,
84 std::map<std::string, GlobalValue*> &Names) {
85 if (!GV->hasName()) { // We must mangle unnamed globals.
86 MangledGlobals.insert(GV);
90 // Figure out if this is already used.
91 GlobalValue *&ExistingValue = Names[GV->getName()];
95 // If GV is external but the existing one is static, mangle the existing one
96 if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) {
97 MangledGlobals.insert(ExistingValue);
100 // Otherwise, mangle GV
101 MangledGlobals.insert(GV);
107 Mangler::Mangler(Module &m, bool addUnderscorePrefix)
108 : M(m), AddUnderscorePrefix(addUnderscorePrefix), Count(0) {
109 // Calculate which global values have names that will collide when we throw
110 // away type information.
111 std::map<std::string, GlobalValue*> Names;
112 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
113 InsertName(I, Names);
114 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
115 InsertName(I, Names);