blob: 0982efd8cd9e4a196cda586df5fe982ea0cf5e37 [file] [log] [blame]
Brian Gaekeb198ca32003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Brian Gaekeb198ca32003-07-24 20:20:58 +00009//
10// Unified name mangler for CWriter and assembly backends.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaeke6b902dc2003-07-25 20:21:20 +000014#include "llvm/Support/Mangler.h"
Brian Gaekeb198ca32003-07-24 20:20:58 +000015#include "llvm/Module.h"
16#include "llvm/Type.h"
17#include "Support/StringExtras.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000018using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000019
Chris Lattneraa8a8472003-08-24 21:08:38 +000020static char HexDigit(int V) {
21 return V < 10 ? V+'0' : V+'A'-10;
22}
23
24static std::string MangleLetter(unsigned char C) {
25 return std::string("_")+HexDigit(C >> 4) + HexDigit(C & 15) + "_";
26}
27
28/// makeNameProper - We don't want identifier names non-C-identifier characters
29/// in them, so mangle them as appropriate.
Brian Gaekeb198ca32003-07-24 20:20:58 +000030///
Chris Lattneraa8a8472003-08-24 21:08:38 +000031std::string Mangler::makeNameProper(const std::string &X) {
32 std::string Result;
33
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]);
37 else
38 Result += X[0];
39
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);
44 else
45 Result += *I;
46 return Result;
Brian Gaekeb198ca32003-07-24 20:20:58 +000047}
48
49std::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.
54 }
55
56 std::string name;
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.
63 //
64 const GlobalValue* gv = dyn_cast<GlobalValue>(V);
Chris Lattner2b3860f2003-08-11 19:34:29 +000065 if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
Brian Gaekeb198ca32003-07-24 20:20:58 +000066 name = makeNameProper(gv->getName());
Chris Lattner2b3860f2003-08-11 19:34:29 +000067 if (AddUnderscorePrefix) name = "_" + name;
Brian Gaekeb198ca32003-07-24 20:20:58 +000068 } else {
69 // Non-global, or global with internal linkage / colliding name
70 // -> mangle.
71 name = "l" + utostr(V->getType()->getUniqueID()) + "_" +
72 makeNameProper(V->getName());
73 }
74 } else {
Brian Gaeke41664452003-07-24 21:37:57 +000075 name = "ltmp_" + utostr(Count++) + "_"
Brian Gaekeb198ca32003-07-24 20:20:58 +000076 + utostr(V->getType()->getUniqueID());
77 }
Chris Lattner2b3860f2003-08-11 19:34:29 +000078
Brian Gaekeb198ca32003-07-24 20:20:58 +000079 Memo[V] = name;
80 return name;
81}
82
Chris Lattnera6acb4f2004-02-14 00:30:23 +000083void Mangler::InsertName(GlobalValue *GV,
84 std::map<std::string, GlobalValue*> &Names) {
85 if (!GV->hasName()) { // We must mangle unnamed globals.
86 MangledGlobals.insert(GV);
87 return;
88 }
89
90 // Figure out if this is already used.
91 GlobalValue *&ExistingValue = Names[GV->getName()];
92 if (!ExistingValue) {
93 ExistingValue = GV;
94 } else {
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);
98 ExistingValue = GV;
99 } else {
100 // Otherwise, mangle GV
101 MangledGlobals.insert(GV);
102 }
103 }
104}
105
106
Chris Lattner2b3860f2003-08-11 19:34:29 +0000107Mangler::Mangler(Module &m, bool addUnderscorePrefix)
Chris Lattner82a5ff42004-02-11 03:29:16 +0000108 : M(m), AddUnderscorePrefix(addUnderscorePrefix), Count(0) {
Brian Gaekeb198ca32003-07-24 20:20:58 +0000109 // Calculate which global values have names that will collide when we throw
110 // away type information.
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000111 std::map<std::string, GlobalValue*> Names;
Brian Gaekeb198ca32003-07-24 20:20:58 +0000112 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000113 InsertName(I, Names);
Brian Gaekeb198ca32003-07-24 20:20:58 +0000114 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000115 InsertName(I, Names);
Brian Gaekeb198ca32003-07-24 20:20:58 +0000116}