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/Target/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/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Support/raw_ostream.h"
26 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
27 /// and the specified name as the global variable name. GVName must not be
29 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
30 const Twine &GVName, ManglerPrefixTy PrefixTy,
31 bool UseGlobalPrefix) {
32 SmallString<256> TmpData;
33 StringRef Name = GVName.toStringRef(TmpData);
34 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
36 const MCAsmInfo *MAI = TM->getMCAsmInfo();
38 // If the global name is not led with \1, add the appropriate prefixes.
39 if (Name[0] == '\1') {
40 Name = Name.substr(1);
42 if (PrefixTy == Mangler::Private) {
43 const char *Prefix = MAI->getPrivateGlobalPrefix();
44 OutName.append(Prefix, Prefix+strlen(Prefix));
45 } else if (PrefixTy == Mangler::LinkerPrivate) {
46 const char *Prefix = MAI->getLinkerPrivateGlobalPrefix();
47 OutName.append(Prefix, Prefix+strlen(Prefix));
50 if (UseGlobalPrefix) {
51 const char *Prefix = MAI->getGlobalPrefix();
53 ; // Common noop, no prefix.
54 else if (Prefix[1] == 0)
55 OutName.push_back(Prefix[0]); // Common, one character prefix.
57 // Arbitrary length prefix.
58 OutName.append(Prefix, Prefix+strlen(Prefix));
62 // If this is a simple string that doesn't need escaping, just append it.
63 OutName.append(Name.begin(), Name.end());
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(SmallVectorImpl<char> &OutName,
70 const Function *F, 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 parameter attribute
77 if (AI->hasByValAttr())
78 Ty = cast<PointerType>(Ty)->getElementType();
79 // Size should be aligned to DWORD boundary
80 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
83 raw_svector_ostream(OutName) << '@' << ArgWords;
87 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
88 /// and the specified global variable's name. If the global variable doesn't
89 /// have a name, this fills in a unique name for the global.
90 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
91 const GlobalValue *GV, bool isImplicitlyPrivate,
92 bool UseGlobalPrefix) {
93 ManglerPrefixTy PrefixTy = Mangler::Default;
94 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
95 PrefixTy = Mangler::Private;
96 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
97 PrefixTy = Mangler::LinkerPrivate;
99 // If this global has a name, handle it simply.
101 StringRef Name = GV->getName();
102 getNameWithPrefix(OutName, Name, PrefixTy, UseGlobalPrefix);
103 // No need to do anything else if the global has the special "do not mangle"
108 // Get the ID for the global, assigning a new one if we haven't got one
110 unsigned &ID = AnonGlobalIDs[GV];
111 if (ID == 0) ID = NextAnonGlobalID++;
113 // Must mangle the global into a unique ID.
114 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy,
118 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
120 if (TM->getMCAsmInfo()->hasMicrosoftFastStdCallMangling()) {
121 if (const Function *F = dyn_cast<Function>(GV)) {
122 CallingConv::ID CC = F->getCallingConv();
124 // fastcall functions need to start with @.
125 // FIXME: This logic seems unlikely to be right.
126 if (CC == CallingConv::X86_FastCall) {
127 if (OutName[0] == '_')
130 OutName.insert(OutName.begin(), '@');
133 // fastcall and stdcall functions usually need @42 at the end to specify
134 // the argument info.
135 FunctionType *FT = F->getFunctionType();
136 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
137 // "Pure" variadic functions do not receive @0 suffix.
138 (!FT->isVarArg() || FT->getNumParams() == 0 ||
139 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
140 AddFastCallStdCallSuffix(OutName, F, *TM->getDataLayout());