• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Unified name mangler for assembly backends.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Target/Mangler.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Function.h"
17 #include "llvm/Target/TargetData.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/Twine.h"
23 using namespace llvm;
24 
isAcceptableChar(char C,bool AllowPeriod,bool AllowUTF8)25 static bool isAcceptableChar(char C, bool AllowPeriod, bool AllowUTF8) {
26   if ((C < 'a' || C > 'z') &&
27       (C < 'A' || C > 'Z') &&
28       (C < '0' || C > '9') &&
29       C != '_' && C != '$' && C != '@' &&
30       !(AllowPeriod && C == '.') &&
31       !(AllowUTF8 && (C & 0x80)))
32     return false;
33   return true;
34 }
35 
HexDigit(int V)36 static char HexDigit(int V) {
37   return V < 10 ? V+'0' : V+'A'-10;
38 }
39 
MangleLetter(SmallVectorImpl<char> & OutName,unsigned char C)40 static void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) {
41   OutName.push_back('_');
42   OutName.push_back(HexDigit(C >> 4));
43   OutName.push_back(HexDigit(C & 15));
44   OutName.push_back('_');
45 }
46 
47 /// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes
48 /// for this assembler.
NameNeedsEscaping(StringRef Str,const MCAsmInfo & MAI)49 static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
50   assert(!Str.empty() && "Cannot create an empty MCSymbol");
51 
52   // If the first character is a number and the target does not allow this, we
53   // need quotes.
54   if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
55     return true;
56 
57   // If any of the characters in the string is an unacceptable character, force
58   // quotes.
59   bool AllowPeriod = MAI.doesAllowPeriodsInName();
60   bool AllowUTF8 = MAI.doesAllowUTF8();
61   for (unsigned i = 0, e = Str.size(); i != e; ++i)
62     if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8))
63       return true;
64   return false;
65 }
66 
67 /// appendMangledName - Add the specified string in mangled form if it uses
68 /// any unusual characters.
appendMangledName(SmallVectorImpl<char> & OutName,StringRef Str,const MCAsmInfo & MAI)69 static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str,
70                               const MCAsmInfo &MAI) {
71   // The first character is not allowed to be a number unless the target
72   // explicitly allows it.
73   if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
74     MangleLetter(OutName, Str[0]);
75     Str = Str.substr(1);
76   }
77 
78   bool AllowPeriod = MAI.doesAllowPeriodsInName();
79   bool AllowUTF8 = MAI.doesAllowUTF8();
80   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
81     if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8))
82       MangleLetter(OutName, Str[i]);
83     else
84       OutName.push_back(Str[i]);
85   }
86 }
87 
88 
89 /// appendMangledQuotedName - On systems that support quoted symbols, we still
90 /// have to escape some (obscure) characters like " and \n which would break the
91 /// assembler's lexing.
appendMangledQuotedName(SmallVectorImpl<char> & OutName,StringRef Str)92 static void appendMangledQuotedName(SmallVectorImpl<char> &OutName,
93                                    StringRef Str) {
94   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
95     if (Str[i] == '"' || Str[i] == '\n')
96       MangleLetter(OutName, Str[i]);
97     else
98       OutName.push_back(Str[i]);
99   }
100 }
101 
102 
103 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
104 /// and the specified name as the global variable name.  GVName must not be
105 /// empty.
getNameWithPrefix(SmallVectorImpl<char> & OutName,const Twine & GVName,ManglerPrefixTy PrefixTy)106 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
107                                 const Twine &GVName, ManglerPrefixTy PrefixTy) {
108   SmallString<256> TmpData;
109   StringRef Name = GVName.toStringRef(TmpData);
110   assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
111 
112   const MCAsmInfo &MAI = Context.getAsmInfo();
113 
114   // If the global name is not led with \1, add the appropriate prefixes.
115   if (Name[0] == '\1') {
116     Name = Name.substr(1);
117   } else {
118     if (PrefixTy == Mangler::Private) {
119       const char *Prefix = MAI.getPrivateGlobalPrefix();
120       OutName.append(Prefix, Prefix+strlen(Prefix));
121     } else if (PrefixTy == Mangler::LinkerPrivate) {
122       const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
123       OutName.append(Prefix, Prefix+strlen(Prefix));
124     }
125 
126     const char *Prefix = MAI.getGlobalPrefix();
127     if (Prefix[0] == 0)
128       ; // Common noop, no prefix.
129     else if (Prefix[1] == 0)
130       OutName.push_back(Prefix[0]);  // Common, one character prefix.
131     else
132       OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
133   }
134 
135   // If this is a simple string that doesn't need escaping, just append it.
136   if (!NameNeedsEscaping(Name, MAI) ||
137       // If quotes are supported, they can be used unless the string contains
138       // a quote or newline.
139       (MAI.doesAllowQuotesInName() &&
140        Name.find_first_of("\n\"") == StringRef::npos)) {
141     OutName.append(Name.begin(), Name.end());
142     return;
143   }
144 
145   // On systems that do not allow quoted names, we need to mangle most
146   // strange characters.
147   if (!MAI.doesAllowQuotesInName())
148     return appendMangledName(OutName, Name, MAI);
149 
150   // Okay, the system allows quoted strings.  We can quote most anything, the
151   // only characters that need escaping are " and \n.
152   assert(Name.find_first_of("\n\"") != StringRef::npos);
153   return appendMangledQuotedName(OutName, Name);
154 }
155 
156 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
157 /// a suffix on their name indicating the number of words of arguments they
158 /// take.
AddFastCallStdCallSuffix(SmallVectorImpl<char> & OutName,const Function * F,const TargetData & TD)159 static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
160                                      const Function *F, const TargetData &TD) {
161   // Calculate arguments size total.
162   unsigned ArgWords = 0;
163   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
164        AI != AE; ++AI) {
165     Type *Ty = AI->getType();
166     // 'Dereference' type in case of byval parameter attribute
167     if (AI->hasByValAttr())
168       Ty = cast<PointerType>(Ty)->getElementType();
169     // Size should be aligned to DWORD boundary
170     ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
171   }
172 
173   raw_svector_ostream(OutName) << '@' << ArgWords;
174 }
175 
176 
177 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
178 /// and the specified global variable's name.  If the global variable doesn't
179 /// have a name, this fills in a unique name for the global.
getNameWithPrefix(SmallVectorImpl<char> & OutName,const GlobalValue * GV,bool isImplicitlyPrivate)180 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
181                                 const GlobalValue *GV,
182                                 bool isImplicitlyPrivate) {
183   ManglerPrefixTy PrefixTy = Mangler::Default;
184   if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
185     PrefixTy = Mangler::Private;
186   else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
187     PrefixTy = Mangler::LinkerPrivate;
188 
189   // If this global has a name, handle it simply.
190   if (GV->hasName()) {
191     getNameWithPrefix(OutName, GV->getName(), PrefixTy);
192   } else {
193     // Get the ID for the global, assigning a new one if we haven't got one
194     // already.
195     unsigned &ID = AnonGlobalIDs[GV];
196     if (ID == 0) ID = NextAnonGlobalID++;
197 
198     // Must mangle the global into a unique ID.
199     getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
200   }
201 
202   // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
203   // add it.
204   if (Context.getAsmInfo().hasMicrosoftFastStdCallMangling()) {
205     if (const Function *F = dyn_cast<Function>(GV)) {
206       CallingConv::ID CC = F->getCallingConv();
207 
208       // fastcall functions need to start with @.
209       // FIXME: This logic seems unlikely to be right.
210       if (CC == CallingConv::X86_FastCall) {
211         if (OutName[0] == '_')
212           OutName[0] = '@';
213         else
214           OutName.insert(OutName.begin(), '@');
215       }
216 
217       // fastcall and stdcall functions usually need @42 at the end to specify
218       // the argument info.
219       FunctionType *FT = F->getFunctionType();
220       if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
221           // "Pure" variadic functions do not receive @0 suffix.
222           (!FT->isVarArg() || FT->getNumParams() == 0 ||
223            (FT->getNumParams() == 1 && F->hasStructRetAttr())))
224         AddFastCallStdCallSuffix(OutName, F, TD);
225     }
226   }
227 }
228 
229 /// getSymbol - Return the MCSymbol for the specified global value.  This
230 /// symbol is the main label that is the address of the global.
getSymbol(const GlobalValue * GV)231 MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
232   SmallString<60> NameStr;
233   getNameWithPrefix(NameStr, GV, false);
234   return Context.GetOrCreateSymbol(NameStr.str());
235 }
236 
237 
238