1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Unified name mangler for assembly backends.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/Mangler.h"
15 #include "llvm/GlobalValue.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/Twine.h"
22 static 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 != '@')
31 static char HexDigit(int V) {
32 return V < 10 ? V+'0' : V+'A'-10;
35 static 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('_');
42 /// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes
43 /// for this assembler.
44 static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
45 assert(!Str.empty() && "Cannot create an empty MCSymbol");
47 // If the first character is a number and the target does not allow this, we
49 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
52 // If any of the characters in the string is an unacceptable character, force
54 for (unsigned i = 0, e = Str.size(); i != e; ++i)
55 if (!isAcceptableChar(Str[i]))
60 /// appendMangledName - Add the specified string in mangled form if it uses
61 /// any unusual characters.
62 static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str,
63 const MCAsmInfo &MAI) {
64 // The first character is not allowed to be a number unless the target
65 // explicitly allows it.
66 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
67 MangleLetter(OutName, Str[0]);
71 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
72 if (!isAcceptableChar(Str[i]))
73 MangleLetter(OutName, Str[i]);
75 OutName.push_back(Str[i]);
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.
83 static void appendMangledQuotedName(SmallVectorImpl<char> &OutName,
85 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
86 if (Str[i] == '"' || Str[i] == '\n')
87 MangleLetter(OutName, Str[i]);
89 OutName.push_back(Str[i]);
94 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
95 /// and the specified name as the global variable name. GVName must not be
97 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
98 const Twine &GVName, ManglerPrefixTy PrefixTy) {
99 SmallString<256> TmpData;
100 StringRef Name = GVName.toStringRef(TmpData);
101 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
103 const MCAsmInfo &MAI = Context.getAsmInfo();
105 // If the global name is not led with \1, add the appropriate prefixes.
106 if (Name[0] == '\1') {
107 Name = Name.substr(1);
109 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));
117 const char *Prefix = MAI.getGlobalPrefix();
119 ; // Common noop, no prefix.
120 else if (Prefix[1] == 0)
121 OutName.push_back(Prefix[0]); // Common, one character prefix.
123 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
126 // 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());
136 // On systems that do not allow quoted names, we need to mangle most
137 // strange characters.
138 if (!MAI.doesAllowQuotesInName())
139 return appendMangledName(OutName, Name, MAI);
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);
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.
151 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
152 const GlobalValue *GV,
153 bool isImplicitlyPrivate) {
154 ManglerPrefixTy PrefixTy = Mangler::Default;
155 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
156 PrefixTy = Mangler::Private;
157 else if (GV->hasLinkerPrivateLinkage())
158 PrefixTy = Mangler::LinkerPrivate;
160 // If this global has a name, handle it simply.
162 return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
164 // Get the ID for the global, assigning a new one if we haven't got one
166 unsigned &ID = AnonGlobalIDs[GV];
167 if (ID == 0) ID = NextAnonGlobalID++;
169 // Must mangle the global into a unique ID.
170 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
173 /// 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.
176 std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
177 bool isImplicitlyPrivate) {
179 getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
180 return std::string(Buf.begin(), Buf.end());
183 /// getSymbol - Return the MCSymbol for the specified global value. This
184 /// symbol is the main label that is the address of the global.
185 MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
186 SmallString<60> NameStr;
187 getNameWithPrefix(NameStr, GV, false);
188 if (!GV->hasPrivateLinkage())
189 return Context.GetOrCreateSymbol(NameStr.str());
191 return Context.GetOrCreateTemporarySymbol(NameStr.str());