blob: c630062bdfac03a4a64db719e7ab2e2cc6185d3f [file] [log] [blame]
Brian Gaekeb198ca32003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaekeb198ca32003-07-24 20:20:58 +00009//
Chris Lattnerc94c8252010-01-16 21:08:46 +000010// Unified name mangler for assembly backends.
Brian Gaekeb198ca32003-07-24 20:20:58 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner45111d12010-01-16 21:57:06 +000014#include "llvm/Target/Mangler.h"
Chris Lattnerc94c8252010-01-16 21:08:46 +000015#include "llvm/GlobalValue.h"
Chris Lattnerc0dba722010-01-17 18:22:35 +000016#include "llvm/MC/MCAsmInfo.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000017#include "llvm/MC/MCContext.h"
Chris Lattner36e69ae2010-01-13 05:02:57 +000018#include "llvm/ADT/SmallString.h"
Chris Lattnerc94c8252010-01-16 21:08:46 +000019#include "llvm/ADT/Twine.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattneracd03ae2010-01-17 19:23:46 +000022static bool isAcceptableChar(char C) {
23 if ((C < 'a' || C > 'z') &&
24 (C < 'A' || C > 'Z') &&
25 (C < '0' || C > '9') &&
26 C != '_' && C != '$' && C != '.' && C != '@')
27 return false;
28 return true;
29}
30
31static char HexDigit(int V) {
32 return V < 10 ? V+'0' : V+'A'-10;
33}
34
35static void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) {
36 OutName.push_back('_');
37 OutName.push_back(HexDigit(C >> 4));
38 OutName.push_back(HexDigit(C & 15));
39 OutName.push_back('_');
40}
41
42/// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes
43/// for this assembler.
44static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
45 assert(!Str.empty() && "Cannot create an empty MCSymbol");
46
47 // If the first character is a number and the target does not allow this, we
48 // need quotes.
49 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
50 return true;
51
52 // If any of the characters in the string is an unacceptable character, force
53 // quotes.
54 for (unsigned i = 0, e = Str.size(); i != e; ++i)
55 if (!isAcceptableChar(Str[i]))
56 return true;
57 return false;
58}
59
60/// appendMangledName - Add the specified string in mangled form if it uses
61/// any unusual characters.
Chris Lattner0bd58b02010-01-17 19:32:29 +000062static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str,
Chris Lattner5ef31a02010-03-12 18:44:54 +000063 const MCAsmInfo &MAI) {
Chris Lattneracd03ae2010-01-17 19:23:46 +000064 // The first character is not allowed to be a number unless the target
65 // explicitly allows it.
Chris Lattner5ef31a02010-03-12 18:44:54 +000066 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
Chris Lattneracd03ae2010-01-17 19:23:46 +000067 MangleLetter(OutName, Str[0]);
68 Str = Str.substr(1);
69 }
70
71 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
72 if (!isAcceptableChar(Str[i]))
73 MangleLetter(OutName, Str[i]);
74 else
75 OutName.push_back(Str[i]);
76 }
77}
78
79
80/// appendMangledQuotedName - On systems that support quoted symbols, we still
81/// have to escape some (obscure) characters like " and \n which would break the
82/// assembler's lexing.
83static void appendMangledQuotedName(SmallVectorImpl<char> &OutName,
84 StringRef Str) {
85 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
86 if (Str[i] == '"' || Str[i] == '\n')
87 MangleLetter(OutName, Str[i]);
88 else
89 OutName.push_back(Str[i]);
90 }
91}
92
93
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000094/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
95/// and the specified name as the global variable name. GVName must not be
96/// empty.
97void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
98 const Twine &GVName, ManglerPrefixTy PrefixTy) {
99 SmallString<256> TmpData;
Benjamin Kramerb357e062010-01-13 12:45:23 +0000100 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000101 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
102
Chris Lattner5ef31a02010-03-12 18:44:54 +0000103 const MCAsmInfo &MAI = Context.getAsmInfo();
104
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000105 // If the global name is not led with \1, add the appropriate prefixes.
Chris Lattneracd03ae2010-01-17 19:23:46 +0000106 if (Name[0] == '\1') {
107 Name = Name.substr(1);
108 } else {
Chris Lattnerc0dba722010-01-17 18:22:35 +0000109 if (PrefixTy == Mangler::Private) {
110 const char *Prefix = MAI.getPrivateGlobalPrefix();
111 OutName.append(Prefix, Prefix+strlen(Prefix));
112 } else if (PrefixTy == Mangler::LinkerPrivate) {
113 const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
114 OutName.append(Prefix, Prefix+strlen(Prefix));
115 }
116
117 const char *Prefix = MAI.getGlobalPrefix();
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000118 if (Prefix[0] == 0)
119 ; // Common noop, no prefix.
120 else if (Prefix[1] == 0)
121 OutName.push_back(Prefix[0]); // Common, one character prefix.
122 else
Chris Lattnerc0dba722010-01-17 18:22:35 +0000123 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000124 }
125
Chris Lattneracd03ae2010-01-17 19:23:46 +0000126 // If this is a simple string that doesn't need escaping, just append it.
127 if (!NameNeedsEscaping(Name, MAI) ||
128 // If quotes are supported, they can be used unless the string contains
129 // a quote or newline.
130 (MAI.doesAllowQuotesInName() &&
131 Name.find_first_of("\n\"") == StringRef::npos)) {
132 OutName.append(Name.begin(), Name.end());
133 return;
134 }
135
136 // On systems that do not allow quoted names, we need to mangle most
137 // strange characters.
138 if (!MAI.doesAllowQuotesInName())
Chris Lattner5ef31a02010-03-12 18:44:54 +0000139 return appendMangledName(OutName, Name, MAI);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000140
141 // Okay, the system allows quoted strings. We can quote most anything, the
142 // only characters that need escaping are " and \n.
143 assert(Name.find_first_of("\n\"") != StringRef::npos);
144 return appendMangledQuotedName(OutName, Name);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000145}
146
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000147
148/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
149/// and the specified global variable's name. If the global variable doesn't
150/// have a name, this fills in a unique name for the global.
151void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
152 const GlobalValue *GV,
153 bool isImplicitlyPrivate) {
Chris Lattnerff240052010-01-17 18:52:16 +0000154 ManglerPrefixTy PrefixTy = Mangler::Default;
155 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
156 PrefixTy = Mangler::Private;
157 else if (GV->hasLinkerPrivateLinkage())
158 PrefixTy = Mangler::LinkerPrivate;
159
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000160 // If this global has a name, handle it simply.
Chris Lattnerff240052010-01-17 18:52:16 +0000161 if (GV->hasName())
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000162 return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +0000163
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000164 // Get the ID for the global, assigning a new one if we haven't got one
165 // already.
166 unsigned &ID = AnonGlobalIDs[GV];
167 if (ID == 0) ID = NextAnonGlobalID++;
168
169 // Must mangle the global into a unique ID.
Chris Lattnerff240052010-01-17 18:52:16 +0000170 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000171}
172
Chris Lattner61f160a2010-01-16 18:06:34 +0000173/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
174/// and the specified global variable's name. If the global variable doesn't
175/// have a name, this fills in a unique name for the global.
176std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
177 bool isImplicitlyPrivate) {
178 SmallString<64> Buf;
179 getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
180 return std::string(Buf.begin(), Buf.end());
181}
Chris Lattner73ff5642010-03-12 18:55:20 +0000182
183/// getSymbol - Return the MCSymbol for the specified global value. This
184/// symbol is the main label that is the address of the global.
185MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
186 SmallString<60> NameStr;
187 getNameWithPrefix(NameStr, GV, false);
188 if (!GV->hasPrivateLinkage())
189 return Context.GetOrCreateSymbol(NameStr.str());
190
191 return Context.GetOrCreateTemporarySymbol(NameStr.str());
192}
193
194