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/IR/Mangler.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm;
23
24 namespace {
25 enum ManglerPrefixTy {
26 Default, ///< Emit default string before each symbol.
27 Private, ///< Emit "private" prefix before each symbol.
28 LinkerPrivate ///< Emit "linker private" prefix before each symbol.
29 };
30 }
31
getNameWithPrefixImpl(raw_ostream & OS,const Twine & GVName,ManglerPrefixTy PrefixTy,const DataLayout & DL,char Prefix)32 static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
33 ManglerPrefixTy PrefixTy,
34 const DataLayout &DL, char Prefix) {
35 SmallString<256> TmpData;
36 StringRef Name = GVName.toStringRef(TmpData);
37 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
38
39 // No need to do anything special if the global has the special "do not
40 // mangle" flag in the name.
41 if (Name[0] == '\1') {
42 OS << Name.substr(1);
43 return;
44 }
45
46 if (PrefixTy == Private)
47 OS << DL.getPrivateGlobalPrefix();
48 else if (PrefixTy == LinkerPrivate)
49 OS << DL.getLinkerPrivateGlobalPrefix();
50
51 if (Prefix != '\0')
52 OS << Prefix;
53
54 // If this is a simple string that doesn't need escaping, just append it.
55 OS << Name;
56 }
57
getNameWithPrefixImpl(raw_ostream & OS,const Twine & GVName,const DataLayout & DL,ManglerPrefixTy PrefixTy)58 static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
59 const DataLayout &DL,
60 ManglerPrefixTy PrefixTy) {
61 char Prefix = DL.getGlobalPrefix();
62 return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
63 }
64
getNameWithPrefix(raw_ostream & OS,const Twine & GVName,const DataLayout & DL)65 void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
66 const DataLayout &DL) {
67 return getNameWithPrefixImpl(OS, GVName, DL, Default);
68 }
69
getNameWithPrefix(SmallVectorImpl<char> & OutName,const Twine & GVName,const DataLayout & DL)70 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
71 const Twine &GVName, const DataLayout &DL) {
72 raw_svector_ostream OS(OutName);
73 char Prefix = DL.getGlobalPrefix();
74 return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
75 }
76
hasByteCountSuffix(CallingConv::ID CC)77 static bool hasByteCountSuffix(CallingConv::ID CC) {
78 switch (CC) {
79 case CallingConv::X86_FastCall:
80 case CallingConv::X86_StdCall:
81 case CallingConv::X86_VectorCall:
82 return true;
83 default:
84 return false;
85 }
86 }
87
88 /// Microsoft fastcall and stdcall functions require a suffix on their name
89 /// indicating the number of words of arguments they take.
addByteCountSuffix(raw_ostream & OS,const Function * F,const DataLayout & DL)90 static void addByteCountSuffix(raw_ostream &OS, const Function *F,
91 const DataLayout &DL) {
92 // Calculate arguments size total.
93 unsigned ArgWords = 0;
94 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
95 AI != AE; ++AI) {
96 Type *Ty = AI->getType();
97 // 'Dereference' type in case of byval or inalloca parameter attribute.
98 if (AI->hasByValOrInAllocaAttr())
99 Ty = cast<PointerType>(Ty)->getElementType();
100 // Size should be aligned to pointer size.
101 unsigned PtrSize = DL.getPointerSize();
102 ArgWords += RoundUpToAlignment(DL.getTypeAllocSize(Ty), PtrSize);
103 }
104
105 OS << '@' << ArgWords;
106 }
107
getNameWithPrefix(raw_ostream & OS,const GlobalValue * GV,bool CannotUsePrivateLabel) const108 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
109 bool CannotUsePrivateLabel) const {
110 ManglerPrefixTy PrefixTy = Default;
111 if (GV->hasPrivateLinkage()) {
112 if (CannotUsePrivateLabel)
113 PrefixTy = LinkerPrivate;
114 else
115 PrefixTy = Private;
116 }
117
118 const DataLayout &DL = GV->getParent()->getDataLayout();
119 if (!GV->hasName()) {
120 // Get the ID for the global, assigning a new one if we haven't got one
121 // already.
122 unsigned &ID = AnonGlobalIDs[GV];
123 if (ID == 0)
124 ID = NextAnonGlobalID++;
125
126 // Must mangle the global into a unique ID.
127 getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
128 return;
129 }
130
131 StringRef Name = GV->getName();
132 char Prefix = DL.getGlobalPrefix();
133
134 // Mangle functions with Microsoft calling conventions specially. Only do
135 // this mangling for x86_64 vectorcall and 32-bit x86.
136 const Function *MSFunc = dyn_cast<Function>(GV);
137 if (Name.startswith("\01"))
138 MSFunc = nullptr; // Don't mangle when \01 is present.
139 CallingConv::ID CC =
140 MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
141 if (!DL.hasMicrosoftFastStdCallMangling() &&
142 CC != CallingConv::X86_VectorCall)
143 MSFunc = nullptr;
144 if (MSFunc) {
145 if (CC == CallingConv::X86_FastCall)
146 Prefix = '@'; // fastcall functions have an @ prefix instead of _.
147 else if (CC == CallingConv::X86_VectorCall)
148 Prefix = '\0'; // vectorcall functions have no prefix.
149 }
150
151 getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
152
153 if (!MSFunc)
154 return;
155
156 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
157 // or vectorcall, add it. These functions have a suffix of @N where N is the
158 // cumulative byte size of all of the parameters to the function in decimal.
159 if (CC == CallingConv::X86_VectorCall)
160 OS << '@'; // vectorcall functions use a double @ suffix.
161 FunctionType *FT = MSFunc->getFunctionType();
162 if (hasByteCountSuffix(CC) &&
163 // "Pure" variadic functions do not receive @0 suffix.
164 (!FT->isVarArg() || FT->getNumParams() == 0 ||
165 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
166 addByteCountSuffix(OS, MSFunc, DL);
167 }
168
getNameWithPrefix(SmallVectorImpl<char> & OutName,const GlobalValue * GV,bool CannotUsePrivateLabel) const169 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
170 const GlobalValue *GV,
171 bool CannotUsePrivateLabel) const {
172 raw_svector_ostream OS(OutName);
173 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
174 }
175