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/MCContext.h"
21 #include "llvm/Support/raw_ostream.h"
24 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
25 /// and the specified name as the global variable name. GVName must not be
27 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
28 const Twine &GVName, ManglerPrefixTy PrefixTy) {
29 SmallString<256> TmpData;
30 StringRef Name = GVName.toStringRef(TmpData);
31 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
33 // If the global name is not led with \1, add the appropriate prefixes.
34 if (Name[0] == '\1') {
35 Name = Name.substr(1);
37 if (PrefixTy == Mangler::Private) {
38 const char *Prefix = DL->getPrivateGlobalPrefix();
39 OutName.append(Prefix, Prefix+strlen(Prefix));
40 } else if (PrefixTy == Mangler::LinkerPrivate) {
41 const char *Prefix = DL->getLinkerPrivateGlobalPrefix();
42 OutName.append(Prefix, Prefix+strlen(Prefix));
45 char Prefix = DL->getGlobalPrefix();
47 OutName.push_back(Prefix);
50 // If this is a simple string that doesn't need escaping, just append it.
51 OutName.append(Name.begin(), Name.end());
54 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
55 /// a suffix on their name indicating the number of words of arguments they
57 static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
58 const Function *F, const DataLayout &TD) {
59 // Calculate arguments size total.
60 unsigned ArgWords = 0;
61 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
63 Type *Ty = AI->getType();
64 // 'Dereference' type in case of byval parameter attribute
65 if (AI->hasByValAttr())
66 Ty = cast<PointerType>(Ty)->getElementType();
67 // Size should be aligned to DWORD boundary
68 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
71 raw_svector_ostream(OutName) << '@' << ArgWords;
75 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
76 /// and the specified global variable's name. If the global variable doesn't
77 /// have a name, this fills in a unique name for the global.
78 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
79 const GlobalValue *GV) {
80 ManglerPrefixTy PrefixTy = Mangler::Default;
81 if (GV->hasPrivateLinkage())
82 PrefixTy = Mangler::Private;
83 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
84 PrefixTy = Mangler::LinkerPrivate;
86 // If this global has a name, handle it simply.
88 StringRef Name = GV->getName();
89 getNameWithPrefix(OutName, Name, PrefixTy);
90 // No need to do anything else if the global has the special "do not mangle"
95 // Get the ID for the global, assigning a new one if we haven't got one
97 unsigned &ID = AnonGlobalIDs[GV];
98 if (ID == 0) ID = NextAnonGlobalID++;
100 // Must mangle the global into a unique ID.
101 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
104 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
106 if (DL->hasMicrosoftFastStdCallMangling()) {
107 if (const Function *F = dyn_cast<Function>(GV)) {
108 CallingConv::ID CC = F->getCallingConv();
110 // fastcall functions need to start with @.
111 // FIXME: This logic seems unlikely to be right.
112 if (CC == CallingConv::X86_FastCall) {
113 if (OutName[0] == '_')
116 OutName.insert(OutName.begin(), '@');
119 // fastcall and stdcall functions usually need @42 at the end to specify
120 // the argument info.
121 FunctionType *FT = F->getFunctionType();
122 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
123 // "Pure" variadic functions do not receive @0 suffix.
124 (!FT->isVarArg() || FT->getNumParams() == 0 ||
125 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
126 AddFastCallStdCallSuffix(OutName, F, *DL);