blob: 03111803186157d15338d72714fe130b9f42b19a [file] [log] [blame]
Brian Gaeked4dff192003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeked4dff192003-07-24 20:20:58 +00009//
10// Unified name mangler for CWriter and assembly backends.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaekeb0078fa2003-07-25 20:21:20 +000014#include "llvm/Support/Mangler.h"
Chris Lattner12b08312005-11-10 18:48:58 +000015#include "llvm/DerivedTypes.h"
Brian Gaeked4dff192003-07-24 20:20:58 +000016#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000017#include "llvm/ADT/StringExtras.h"
Chris Lattnerc9499b62003-12-14 21:35:53 +000018using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000019
Chris Lattner9527fea2003-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.
Misha Brukmanb1c93172005-04-21 23:48:37 +000030///
Chris Lattnercc9c0332005-09-24 08:24:28 +000031std::string Mangler::makeNameProper(const std::string &X, const char *Prefix) {
Chris Lattner9527fea2003-08-24 21:08:38 +000032 std::string Result;
Misha Brukmanb1c93172005-04-21 23:48:37 +000033
Chris Lattnercc9c0332005-09-24 08:24:28 +000034 // If X does not start with (char)1, add the prefix.
35 std::string::const_iterator I = X.begin();
36 if (*I != 1)
37 Result = Prefix;
Chris Lattner9527fea2003-08-24 21:08:38 +000038 else
Chris Lattnercc9c0332005-09-24 08:24:28 +000039 ++I; // Skip over the marker.
40
41 // Mangle the first letter specially, don't allow numbers...
42 if (*I >= '0' && *I <= '9')
43 Result += MangleLetter(*I++);
Chris Lattner9527fea2003-08-24 21:08:38 +000044
Chris Lattnercc9c0332005-09-24 08:24:28 +000045 for (std::string::const_iterator E = X.end(); I != E; ++I)
Chris Lattner9527fea2003-08-24 21:08:38 +000046 if ((*I < 'a' || *I > 'z') && (*I < 'A' || *I > 'Z') &&
Chris Lattnere3870fb2005-10-14 01:28:34 +000047 (*I < '0' || *I > '9') && *I != '_' && *I != '$')
Chris Lattner9527fea2003-08-24 21:08:38 +000048 Result += MangleLetter(*I);
49 else
50 Result += *I;
51 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +000052}
53
Chris Lattner3a9fd4c2004-07-08 22:09:34 +000054/// getTypeID - Return a unique ID for the specified LLVM type.
55///
56unsigned Mangler::getTypeID(const Type *Ty) {
57 unsigned &E = TypeMap[Ty];
58 if (E == 0) E = ++TypeCounter;
59 return E;
60}
61
Brian Gaeked4dff192003-07-24 20:20:58 +000062std::string Mangler::getValueName(const Value *V) {
Chris Lattner12b08312005-11-10 18:48:58 +000063 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
64 return getValueName(GV);
65
66 std::string &Name = Memo[V];
67 if (!Name.empty())
68 return Name; // Return the already-computed name for V.
69
70 // Always mangle local names.
71 Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
72 return Name;
73}
74
75
76std::string Mangler::getValueName(const GlobalValue *GV) {
Brian Gaeked4dff192003-07-24 20:20:58 +000077 // Check to see whether we've already named V.
Chris Lattner12b08312005-11-10 18:48:58 +000078 std::string &Name = Memo[GV];
79 if (!Name.empty())
80 return Name; // Return the already-computed name for V.
Brian Gaeked4dff192003-07-24 20:20:58 +000081
Chris Lattner12b08312005-11-10 18:48:58 +000082 // Name mangling occurs as follows:
83 // - If V is an intrinsic function, do not change name at all
84 // - Otherwise, mangling occurs if global collides with existing name.
85 if (isa<Function>(GV) && cast<Function>(GV)->getIntrinsicID()) {
86 Name = GV->getName(); // Is an intrinsic function
87 } else if (!MangledGlobals.count(GV)) {
88 Name = makeNameProper(GV->getName(), Prefix);
Brian Gaeked4dff192003-07-24 20:20:58 +000089 } else {
Chris Lattner12b08312005-11-10 18:48:58 +000090 unsigned TypeUniqueID = getTypeID(GV->getType());
91 Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
Brian Gaeked4dff192003-07-24 20:20:58 +000092 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000093
Chris Lattner12b08312005-11-10 18:48:58 +000094 return Name;
Brian Gaeked4dff192003-07-24 20:20:58 +000095}
96
Chris Lattner77687a92004-02-14 00:30:23 +000097void Mangler::InsertName(GlobalValue *GV,
98 std::map<std::string, GlobalValue*> &Names) {
99 if (!GV->hasName()) { // We must mangle unnamed globals.
100 MangledGlobals.insert(GV);
101 return;
102 }
103
104 // Figure out if this is already used.
105 GlobalValue *&ExistingValue = Names[GV->getName()];
106 if (!ExistingValue) {
107 ExistingValue = GV;
108 } else {
109 // If GV is external but the existing one is static, mangle the existing one
110 if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) {
111 MangledGlobals.insert(ExistingValue);
112 ExistingValue = GV;
113 } else {
114 // Otherwise, mangle GV
115 MangledGlobals.insert(GV);
116 }
117 }
118}
119
120
Chris Lattner9fa3bcc2005-11-10 19:02:18 +0000121Mangler::Mangler(Module &M, const char *prefix)
122 : Prefix(prefix), Count(0), TypeCounter(0) {
Brian Gaeked4dff192003-07-24 20:20:58 +0000123 // Calculate which global values have names that will collide when we throw
124 // away type information.
Chris Lattner77687a92004-02-14 00:30:23 +0000125 std::map<std::string, GlobalValue*> Names;
Brian Gaeked4dff192003-07-24 20:20:58 +0000126 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner77687a92004-02-14 00:30:23 +0000127 InsertName(I, Names);
Chris Lattner531f9e92005-03-15 04:54:21 +0000128 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattner77687a92004-02-14 00:30:23 +0000129 InsertName(I, Names);
Brian Gaeked4dff192003-07-24 20:20:58 +0000130}