Move the llvm mangler to lib/IR.
[oota-llvm.git] / lib / IR / 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/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/MC/MCContext.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm;
23
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
26 /// empty.
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");
32
33   // If the global name is not led with \1, add the appropriate prefixes.
34   if (Name[0] == '\1') {
35     Name = Name.substr(1);
36   } else {
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));
43     }
44
45     char Prefix = DL->getGlobalPrefix();
46     if (Prefix != '\0')
47       OutName.push_back(Prefix);
48   }
49
50   // If this is a simple string that doesn't need escaping, just append it.
51   OutName.append(Name.begin(), Name.end());
52 }
53
54 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
55 /// a suffix on their name indicating the number of words of arguments they
56 /// take.
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();
62        AI != AE; ++AI) {
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;
69   }
70   
71   raw_svector_ostream(OutName) << '@' << ArgWords;
72 }
73
74
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;
85   
86   // If this global has a name, handle it simply.
87   if (GV->hasName()) {
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"
91     // flag in the name.
92     if (Name[0] == 1)
93       return;
94   } else {
95     // Get the ID for the global, assigning a new one if we haven't got one
96     // already.
97     unsigned &ID = AnonGlobalIDs[GV];
98     if (ID == 0) ID = NextAnonGlobalID++;
99   
100     // Must mangle the global into a unique ID.
101     getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
102   }
103
104   // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
105   // add it.
106   if (DL->hasMicrosoftFastStdCallMangling()) {
107     if (const Function *F = dyn_cast<Function>(GV)) {
108       CallingConv::ID CC = F->getCallingConv();
109     
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] == '_')
114           OutName[0] = '@';
115         else
116           OutName.insert(OutName.begin(), '@');
117       }
118     
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);
127     }
128   }
129 }