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