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 if (PrefixTy == Mangler::Private)
31 OS << DL.getPrivateGlobalPrefix();
32 else if (PrefixTy == Mangler::LinkerPrivate)
33 OS << DL.getLinkerPrivateGlobalPrefix();
38 char Prefix = DL.getGlobalPrefix();
43 // If this is a simple string that doesn't need escaping, just append it.
47 void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
48 ManglerPrefixTy PrefixTy) const {
49 return getNameWithPrefixx(OS, GVName, PrefixTy, *DL, false);
52 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
54 ManglerPrefixTy PrefixTy) const {
55 raw_svector_ostream OS(OutName);
56 return getNameWithPrefix(OS, GVName, PrefixTy);
59 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
60 /// a suffix on their name indicating the number of words of arguments they
62 static void AddFastCallStdCallSuffix(raw_ostream &OS, const Function *F,
63 const DataLayout &TD) {
64 // Calculate arguments size total.
65 unsigned ArgWords = 0;
66 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
68 Type *Ty = AI->getType();
69 // 'Dereference' type in case of byval or inalloca parameter attribute.
70 if (AI->hasByValOrInAllocaAttr())
71 Ty = cast<PointerType>(Ty)->getElementType();
72 // Size should be aligned to DWORD boundary
73 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
76 OS << '@' << ArgWords;
79 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
80 bool CannotUsePrivateLabel) const {
81 ManglerPrefixTy PrefixTy = Mangler::Default;
82 if (GV->hasPrivateLinkage()) {
83 if (CannotUsePrivateLabel)
84 PrefixTy = Mangler::LinkerPrivate;
86 PrefixTy = Mangler::Private;
87 } else if (GV->hasLinkerPrivateLinkage() ||
88 GV->hasLinkerPrivateWeakLinkage()) {
89 PrefixTy = Mangler::LinkerPrivate;
93 // Get the ID for the global, assigning a new one if we haven't got one
95 unsigned &ID = AnonGlobalIDs[GV];
97 ID = NextAnonGlobalID++;
99 // Must mangle the global into a unique ID.
100 getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
104 StringRef Name = GV->getName();
106 // No need to do anything special if the global has the special "do not
107 // mangle" flag in the name.
108 if (Name[0] == '\1') {
109 OS << Name.substr(1);
114 const Function *MSFunc = NULL;
116 if (DL->hasMicrosoftFastStdCallMangling()) {
117 if ((MSFunc = dyn_cast<Function>(GV))) {
118 CC = MSFunc->getCallingConv();
119 // fastcall functions need to start with @ instead of _.
120 if (CC == CallingConv::X86_FastCall)
125 getNameWithPrefixx(OS, Name, PrefixTy, *DL, UseAt);
130 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
132 // fastcall and stdcall functions usually need @42 at the end to specify
133 // the argument info.
134 FunctionType *FT = MSFunc->getFunctionType();
135 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
136 // "Pure" variadic functions do not receive @0 suffix.
137 (!FT->isVarArg() || FT->getNumParams() == 0 ||
138 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
139 AddFastCallStdCallSuffix(OS, MSFunc, *DL);
142 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
143 const GlobalValue *GV,
144 bool CannotUsePrivateLabel) const {
145 raw_svector_ostream OS(OutName);
146 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);