c41a0f3591bd9eef365f1a854fb444f2f697522f
[oota-llvm.git] / lib / Target / Mangler.cpp
1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Unified name mangler for assembly backends.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
24 using namespace llvm;
25
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
28 /// empty.
29 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
30                                 const Twine &GVName, ManglerPrefixTy PrefixTy) {
31   SmallString<256> TmpData;
32   StringRef Name = GVName.toStringRef(TmpData);
33   assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
34   
35   const MCAsmInfo *MAI = TM->getMCAsmInfo();
36   
37   // If the global name is not led with \1, add the appropriate prefixes.
38   if (Name[0] == '\1') {
39     Name = Name.substr(1);
40   } else {
41     if (PrefixTy == Mangler::Private) {
42       const char *Prefix = MAI->getPrivateGlobalPrefix();
43       OutName.append(Prefix, Prefix+strlen(Prefix));
44     } else if (PrefixTy == Mangler::LinkerPrivate) {
45       const char *Prefix = MAI->getLinkerPrivateGlobalPrefix();
46       OutName.append(Prefix, Prefix+strlen(Prefix));
47     }
48
49
50     char Prefix = MAI->getGlobalPrefix();
51     if (Prefix != '\0')
52       OutName.push_back(Prefix);
53   }
54
55   // If this is a simple string that doesn't need escaping, just append it.
56   OutName.append(Name.begin(), Name.end());
57 }
58
59 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
60 /// a suffix on their name indicating the number of words of arguments they
61 /// take.
62 static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
63                                      const Function *F, 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();
67        AI != AE; ++AI) {
68     Type *Ty = AI->getType();
69     // 'Dereference' type in case of byval parameter attribute
70     if (AI->hasByValAttr())
71       Ty = cast<PointerType>(Ty)->getElementType();
72     // Size should be aligned to DWORD boundary
73     ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
74   }
75   
76   raw_svector_ostream(OutName) << '@' << ArgWords;
77 }
78
79
80 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
81 /// and the specified global variable's name.  If the global variable doesn't
82 /// have a name, this fills in a unique name for the global.
83 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
84                                 const GlobalValue *GV,
85                                 bool isImplicitlyPrivate) {
86   ManglerPrefixTy PrefixTy = Mangler::Default;
87   if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
88     PrefixTy = Mangler::Private;
89   else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
90     PrefixTy = Mangler::LinkerPrivate;
91   
92   // If this global has a name, handle it simply.
93   if (GV->hasName()) {
94     StringRef Name = GV->getName();
95     getNameWithPrefix(OutName, Name, PrefixTy);
96     // No need to do anything else if the global has the special "do not mangle"
97     // flag in the name.
98     if (Name[0] == 1)
99       return;
100   } else {
101     // Get the ID for the global, assigning a new one if we haven't got one
102     // already.
103     unsigned &ID = AnonGlobalIDs[GV];
104     if (ID == 0) ID = NextAnonGlobalID++;
105   
106     // Must mangle the global into a unique ID.
107     getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
108   }
109   
110   // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
111   // add it.
112   if (TM->getMCAsmInfo()->hasMicrosoftFastStdCallMangling()) {
113     if (const Function *F = dyn_cast<Function>(GV)) {
114       CallingConv::ID CC = F->getCallingConv();
115     
116       // fastcall functions need to start with @.
117       // FIXME: This logic seems unlikely to be right.
118       if (CC == CallingConv::X86_FastCall) {
119         if (OutName[0] == '_')
120           OutName[0] = '@';
121         else
122           OutName.insert(OutName.begin(), '@');
123       }
124     
125       // fastcall and stdcall functions usually need @42 at the end to specify
126       // the argument info.
127       FunctionType *FT = F->getFunctionType();
128       if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
129           // "Pure" variadic functions do not receive @0 suffix.
130           (!FT->isVarArg() || FT->getNumParams() == 0 ||
131            (FT->getNumParams() == 1 && F->hasStructRetAttr())))
132         AddFastCallStdCallSuffix(OutName, F, *TM->getDataLayout());
133     }
134   }
135 }