blob: 14830cb8f820bd39c636947250af99eabc45769b [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"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/ADT/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
Chris Lattnerd6391d72004-07-08 22:09:34 +000049/// getTypeID - Return a unique ID for the specified LLVM type.
50///
51unsigned Mangler::getTypeID(const Type *Ty) {
52 unsigned &E = TypeMap[Ty];
53 if (E == 0) E = ++TypeCounter;
54 return E;
55}
56
57
Brian Gaekeb198ca32003-07-24 20:20:58 +000058std::string Mangler::getValueName(const Value *V) {
59 // Check to see whether we've already named V.
60 ValueMap::iterator VI = Memo.find(V);
61 if (VI != Memo.end()) {
62 return VI->second; // Return the old name for V.
63 }
64
65 std::string name;
66 if (V->hasName()) { // Print out the label if it exists...
67 // Name mangling occurs as follows:
Chris Lattner6e40e1d2004-04-05 20:17:53 +000068 // - If V is an intrinsic function, do not change name at all
Brian Gaekeb198ca32003-07-24 20:20:58 +000069 // - If V is not a global, mangling always occurs.
70 // - Otherwise, mangling occurs when any of the following are true:
71 // 1) V has internal linkage
72 // 2) V's name would collide if it is not mangled.
73 //
74 const GlobalValue* gv = dyn_cast<GlobalValue>(V);
Chris Lattner6e40e1d2004-04-05 20:17:53 +000075 if (gv && isa<Function>(gv) && cast<Function>(gv)->getIntrinsicID()) {
76 name = gv->getName(); // Is an intrinsic function
77 } else if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
Chris Lattner97f3e372004-08-17 06:06:54 +000078 name = Prefix + makeNameProper(gv->getName());
Brian Gaekeb198ca32003-07-24 20:20:58 +000079 } else {
80 // Non-global, or global with internal linkage / colliding name
81 // -> mangle.
Chris Lattnerd6391d72004-07-08 22:09:34 +000082 unsigned TypeUniqueID = getTypeID(V->getType());
Brian Gaekea4c7c412004-07-06 20:29:05 +000083 name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(V->getName());
Brian Gaekeb198ca32003-07-24 20:20:58 +000084 }
85 } else {
Chris Lattnerd6391d72004-07-08 22:09:34 +000086 name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
Brian Gaekeb198ca32003-07-24 20:20:58 +000087 }
Chris Lattner2b3860f2003-08-11 19:34:29 +000088
Brian Gaekeb198ca32003-07-24 20:20:58 +000089 Memo[V] = name;
90 return name;
91}
92
Chris Lattnera6acb4f2004-02-14 00:30:23 +000093void Mangler::InsertName(GlobalValue *GV,
94 std::map<std::string, GlobalValue*> &Names) {
95 if (!GV->hasName()) { // We must mangle unnamed globals.
96 MangledGlobals.insert(GV);
97 return;
98 }
99
100 // Figure out if this is already used.
101 GlobalValue *&ExistingValue = Names[GV->getName()];
102 if (!ExistingValue) {
103 ExistingValue = GV;
104 } else {
105 // If GV is external but the existing one is static, mangle the existing one
106 if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) {
107 MangledGlobals.insert(ExistingValue);
108 ExistingValue = GV;
109 } else {
110 // Otherwise, mangle GV
111 MangledGlobals.insert(GV);
112 }
113 }
114}
115
116
Chris Lattner97f3e372004-08-17 06:06:54 +0000117Mangler::Mangler(Module &m, const char *prefix)
118 : M(m), Prefix(prefix), TypeCounter(0), Count(0) {
Brian Gaekeb198ca32003-07-24 20:20:58 +0000119 // Calculate which global values have names that will collide when we throw
120 // away type information.
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000121 std::map<std::string, GlobalValue*> Names;
Brian Gaekeb198ca32003-07-24 20:20:58 +0000122 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000123 InsertName(I, Names);
Chris Lattnere4d5c442005-03-15 04:54:21 +0000124 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattnera6acb4f2004-02-14 00:30:23 +0000125 InsertName(I, Names);
Brian Gaekeb198ca32003-07-24 20:20:58 +0000126}