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/Support/raw_ostream.h"
23 static void getNameWithPrefixx(raw_ostream &OS, const Twine &GVName,
24 Mangler::ManglerPrefixTy PrefixTy,
25 const DataLayout &DL, bool UseAt) {
26 SmallString<256> TmpData;
27 StringRef Name = GVName.toStringRef(TmpData);
28 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
30 // No need to do anything special if the global has the special "do not
31 // mangle" flag in the name.
32 if (Name[0] == '\1') {
37 if (PrefixTy == Mangler::Private)
38 OS << DL.getPrivateGlobalPrefix();
39 else if (PrefixTy == Mangler::LinkerPrivate)
40 OS << DL.getLinkerPrivateGlobalPrefix();
45 char Prefix = DL.getGlobalPrefix();
50 // If this is a simple string that doesn't need escaping, just append it.
54 void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
55 ManglerPrefixTy PrefixTy) const {
56 return getNameWithPrefixx(OS, GVName, PrefixTy, *DL, false);
59 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
61 ManglerPrefixTy PrefixTy) const {
62 raw_svector_ostream OS(OutName);
63 return getNameWithPrefix(OS, GVName, PrefixTy);
66 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
67 /// a suffix on their name indicating the number of words of arguments they
69 static void AddFastCallStdCallSuffix(raw_ostream &OS, const Function *F,
70 const DataLayout &TD) {
71 // Calculate arguments size total.
72 unsigned ArgWords = 0;
73 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
75 Type *Ty = AI->getType();
76 // 'Dereference' type in case of byval or inalloca parameter attribute.
77 if (AI->hasByValOrInAllocaAttr())
78 Ty = cast<PointerType>(Ty)->getElementType();
79 // Size should be aligned to DWORD boundary
80 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
83 OS << '@' << ArgWords;
86 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
87 bool CannotUsePrivateLabel) const {
88 ManglerPrefixTy PrefixTy = Mangler::Default;
89 if (GV->hasPrivateLinkage()) {
90 if (CannotUsePrivateLabel)
91 PrefixTy = Mangler::LinkerPrivate;
93 PrefixTy = Mangler::Private;
97 // Get the ID for the global, assigning a new one if we haven't got one
99 unsigned &ID = AnonGlobalIDs[GV];
101 ID = NextAnonGlobalID++;
103 // Must mangle the global into a unique ID.
104 getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
108 StringRef Name = GV->getName();
111 const Function *MSFunc = nullptr;
113 if (Name[0] != '\1' && DL->hasMicrosoftFastStdCallMangling()) {
114 if ((MSFunc = dyn_cast<Function>(GV))) {
115 CC = MSFunc->getCallingConv();
116 // fastcall functions need to start with @ instead of _.
117 if (CC == CallingConv::X86_FastCall)
122 getNameWithPrefixx(OS, Name, PrefixTy, *DL, UseAt);
127 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
129 // fastcall and stdcall functions usually need @42 at the end to specify
130 // the argument info.
131 FunctionType *FT = MSFunc->getFunctionType();
132 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
133 // "Pure" variadic functions do not receive @0 suffix.
134 (!FT->isVarArg() || FT->getNumParams() == 0 ||
135 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
136 AddFastCallStdCallSuffix(OS, MSFunc, *DL);
139 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
140 const GlobalValue *GV,
141 bool CannotUsePrivateLabel) const {
142 raw_svector_ostream OS(OutName);
143 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);