blob: 665cb7388c117b4b78bc936870bef4ffde468740 [file] [log] [blame]
Brian Gaekeb198ca32003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2//
3// Unified name mangler for CWriter and assembly backends.
4//
5//===----------------------------------------------------------------------===//
6
Brian Gaeke6b902dc2003-07-25 20:21:20 +00007#include "llvm/Support/Mangler.h"
Brian Gaekeb198ca32003-07-24 20:20:58 +00008#include "llvm/Module.h"
9#include "llvm/Type.h"
10#include "Support/StringExtras.h"
Brian Gaekeb198ca32003-07-24 20:20:58 +000011
12/// makeNameProper - We don't want identifier names with ., space, or
13/// - in them, so we mangle these characters into the strings "d_",
14/// "s_", and "D_", respectively.
15///
Brian Gaeke41664452003-07-24 21:37:57 +000016std::string Mangler::makeNameProper(const std::string &x) {
Brian Gaekeb198ca32003-07-24 20:20:58 +000017 std::string tmp;
Brian Gaeke41664452003-07-24 21:37:57 +000018 for (std::string::const_iterator sI = x.begin(), sEnd = x.end();
19 sI != sEnd; sI++)
Brian Gaekeb198ca32003-07-24 20:20:58 +000020 switch (*sI) {
21 case '.': tmp += "d_"; break;
22 case ' ': tmp += "s_"; break;
23 case '-': tmp += "D_"; break;
24 default: tmp += *sI;
25 }
26 return tmp;
27}
28
29std::string Mangler::getValueName(const Value *V) {
30 // Check to see whether we've already named V.
31 ValueMap::iterator VI = Memo.find(V);
32 if (VI != Memo.end()) {
33 return VI->second; // Return the old name for V.
34 }
35
36 std::string name;
37 if (V->hasName()) { // Print out the label if it exists...
38 // Name mangling occurs as follows:
39 // - If V is not a global, mangling always occurs.
40 // - Otherwise, mangling occurs when any of the following are true:
41 // 1) V has internal linkage
42 // 2) V's name would collide if it is not mangled.
43 //
44 const GlobalValue* gv = dyn_cast<GlobalValue>(V);
45 if(gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
46 name = makeNameProper(gv->getName());
47 } else {
48 // Non-global, or global with internal linkage / colliding name
49 // -> mangle.
50 name = "l" + utostr(V->getType()->getUniqueID()) + "_" +
51 makeNameProper(V->getName());
52 }
53 } else {
Brian Gaeke41664452003-07-24 21:37:57 +000054 name = "ltmp_" + utostr(Count++) + "_"
Brian Gaekeb198ca32003-07-24 20:20:58 +000055 + utostr(V->getType()->getUniqueID());
56 }
57 Memo[V] = name;
58 return name;
59}
60
Brian Gaeke41664452003-07-24 21:37:57 +000061Mangler::Mangler(Module &M_) : M(M_)
Brian Gaekeb198ca32003-07-24 20:20:58 +000062{
63 // Calculate which global values have names that will collide when we throw
64 // away type information.
65 std::set<std::string> FoundNames;
66 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
67 if (I->hasName()) // If the global has a name...
68 if (FoundNames.count(I->getName())) // And the name is already used
69 MangledGlobals.insert(I); // Mangle the name
70 else
71 FoundNames.insert(I->getName()); // Otherwise, keep track of name
72
73 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
74 if (I->hasName()) // If the global has a name...
75 if (FoundNames.count(I->getName())) // And the name is already used
76 MangledGlobals.insert(I); // Mangle the name
77 else
78 FoundNames.insert(I->getName()); // Otherwise, keep track of name
79}
80