blob: a58ca330afda7fb2c050cae396275b362f7c2bd4 [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);
Chris Lattner2b3860f2003-08-11 19:34:29 +000045 if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
Brian Gaekeb198ca32003-07-24 20:20:58 +000046 name = makeNameProper(gv->getName());
Chris Lattner2b3860f2003-08-11 19:34:29 +000047 if (AddUnderscorePrefix) name = "_" + name;
Brian Gaekeb198ca32003-07-24 20:20:58 +000048 } else {
49 // Non-global, or global with internal linkage / colliding name
50 // -> mangle.
51 name = "l" + utostr(V->getType()->getUniqueID()) + "_" +
52 makeNameProper(V->getName());
53 }
54 } else {
Brian Gaeke41664452003-07-24 21:37:57 +000055 name = "ltmp_" + utostr(Count++) + "_"
Brian Gaekeb198ca32003-07-24 20:20:58 +000056 + utostr(V->getType()->getUniqueID());
57 }
Chris Lattner2b3860f2003-08-11 19:34:29 +000058
Brian Gaekeb198ca32003-07-24 20:20:58 +000059 Memo[V] = name;
60 return name;
61}
62
Chris Lattner2b3860f2003-08-11 19:34:29 +000063Mangler::Mangler(Module &m, bool addUnderscorePrefix)
64 : M(m), AddUnderscorePrefix(addUnderscorePrefix) {
Brian Gaekeb198ca32003-07-24 20:20:58 +000065 // Calculate which global values have names that will collide when we throw
66 // away type information.
67 std::set<std::string> FoundNames;
68 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
69 if (I->hasName()) // If the global has a name...
70 if (FoundNames.count(I->getName())) // And the name is already used
71 MangledGlobals.insert(I); // Mangle the name
72 else
73 FoundNames.insert(I->getName()); // Otherwise, keep track of name
74
75 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
76 if (I->hasName()) // If the global has a name...
77 if (FoundNames.count(I->getName())) // And the name is already used
78 MangledGlobals.insert(I); // Mangle the name
79 else
80 FoundNames.insert(I->getName()); // Otherwise, keep track of name
81}
82