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/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"
24 static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
25 Mangler::ManglerPrefixTy PrefixTy,
26 const DataLayout &DL, char Prefix) {
27 SmallString<256> TmpData;
28 StringRef Name = GVName.toStringRef(TmpData);
29 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
31 // No need to do anything special if the global has the special "do not
32 // mangle" flag in the name.
33 if (Name[0] == '\1') {
38 if (PrefixTy == Mangler::Private)
39 OS << DL.getPrivateGlobalPrefix();
40 else if (PrefixTy == Mangler::LinkerPrivate)
41 OS << DL.getLinkerPrivateGlobalPrefix();
46 // If this is a simple string that doesn't need escaping, just append it.
50 void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
52 ManglerPrefixTy PrefixTy) {
53 char Prefix = DL.getGlobalPrefix();
54 return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
57 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
58 const Twine &GVName, const DataLayout &DL,
59 ManglerPrefixTy PrefixTy) {
60 raw_svector_ostream OS(OutName);
61 char Prefix = DL.getGlobalPrefix();
62 return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
65 static bool hasByteCountSuffix(CallingConv::ID CC) {
67 case CallingConv::X86_FastCall:
68 case CallingConv::X86_StdCall:
69 case CallingConv::X86_VectorCall:
76 /// Microsoft fastcall and stdcall functions require a suffix on their name
77 /// indicating the number of words of arguments they take.
78 static void addByteCountSuffix(raw_ostream &OS, const Function *F,
79 const DataLayout &DL) {
80 // Calculate arguments size total.
81 unsigned ArgWords = 0;
82 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
84 Type *Ty = AI->getType();
85 // 'Dereference' type in case of byval or inalloca parameter attribute.
86 if (AI->hasByValOrInAllocaAttr())
87 Ty = cast<PointerType>(Ty)->getElementType();
88 // Size should be aligned to pointer size.
89 unsigned PtrSize = DL.getPointerSize();
90 ArgWords += RoundUpToAlignment(DL.getTypeAllocSize(Ty), PtrSize);
93 OS << '@' << ArgWords;
96 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
97 bool CannotUsePrivateLabel) const {
98 ManglerPrefixTy PrefixTy = Mangler::Default;
99 if (GV->hasPrivateLinkage()) {
100 if (CannotUsePrivateLabel)
101 PrefixTy = Mangler::LinkerPrivate;
103 PrefixTy = Mangler::Private;
106 const DataLayout &DL = GV->getParent()->getDataLayout();
107 if (!GV->hasName()) {
108 // Get the ID for the global, assigning a new one if we haven't got one
110 unsigned &ID = AnonGlobalIDs[GV];
112 ID = NextAnonGlobalID++;
114 // Must mangle the global into a unique ID.
115 getNameWithPrefix(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
119 StringRef Name = GV->getName();
120 char Prefix = DL.getGlobalPrefix();
122 // Mangle functions with Microsoft calling conventions specially. Only do
123 // this mangling for x86_64 vectorcall and 32-bit x86.
124 const Function *MSFunc = dyn_cast<Function>(GV);
125 if (Name.startswith("\01"))
126 MSFunc = nullptr; // Don't mangle when \01 is present.
128 MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
129 if (!DL.hasMicrosoftFastStdCallMangling() &&
130 CC != CallingConv::X86_VectorCall)
133 if (CC == CallingConv::X86_FastCall)
134 Prefix = '@'; // fastcall functions have an @ prefix instead of _.
135 else if (CC == CallingConv::X86_VectorCall)
136 Prefix = '\0'; // vectorcall functions have no prefix.
139 getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
144 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
145 // or vectorcall, add it. These functions have a suffix of @N where N is the
146 // cumulative byte size of all of the parameters to the function in decimal.
147 if (CC == CallingConv::X86_VectorCall)
148 OS << '@'; // vectorcall functions use a double @ suffix.
149 FunctionType *FT = MSFunc->getFunctionType();
150 if (hasByteCountSuffix(CC) &&
151 // "Pure" variadic functions do not receive @0 suffix.
152 (!FT->isVarArg() || FT->getNumParams() == 0 ||
153 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
154 addByteCountSuffix(OS, MSFunc, DL);
157 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
158 const GlobalValue *GV,
159 bool CannotUsePrivateLabel) const {
160 raw_svector_ostream OS(OutName);
161 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);