Use a raw_stream to implement the mangler.
[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/Support/raw_ostream.h"
21 using namespace llvm;
22
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");
29
30   if (PrefixTy == Mangler::Private)
31     OS << DL.getPrivateGlobalPrefix();
32   else if (PrefixTy == Mangler::LinkerPrivate)
33     OS << DL.getLinkerPrivateGlobalPrefix();
34
35   if (UseAt) {
36     OS << '@';
37   } else {
38     char Prefix = DL.getGlobalPrefix();
39     if (Prefix != '\0')
40       OS << Prefix;
41   }
42
43   // If this is a simple string that doesn't need escaping, just append it.
44   OS << Name;
45 }
46
47 void Mangler::getNameWithPrefix(raw_ostream &OS,
48                                 const Twine &GVName, ManglerPrefixTy PrefixTy) {
49   return getNameWithPrefixx(OS, GVName, PrefixTy, *DL, false);
50 }
51
52 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
53                                 const Twine &GVName, ManglerPrefixTy PrefixTy) {
54   raw_svector_ostream OS(OutName);
55   return getNameWithPrefix(OS, GVName, PrefixTy);
56 }
57
58 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
59 /// a suffix on their name indicating the number of words of arguments they
60 /// take.
61 static void AddFastCallStdCallSuffix(raw_ostream &OS, const Function *F,
62                                      const DataLayout &TD) {
63   // Calculate arguments size total.
64   unsigned ArgWords = 0;
65   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
66        AI != AE; ++AI) {
67     Type *Ty = AI->getType();
68     // 'Dereference' type in case of byval parameter attribute
69     if (AI->hasByValAttr())
70       Ty = cast<PointerType>(Ty)->getElementType();
71     // Size should be aligned to DWORD boundary
72     ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
73   }
74
75   OS << '@' << ArgWords;
76 }
77
78 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV) {
79   ManglerPrefixTy PrefixTy = Mangler::Default;
80   if (GV->hasPrivateLinkage())
81     PrefixTy = Mangler::Private;
82   else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
83     PrefixTy = Mangler::LinkerPrivate;
84
85   if (!GV->hasName()) {
86     // Get the ID for the global, assigning a new one if we haven't got one
87     // already.
88     unsigned &ID = AnonGlobalIDs[GV];
89     if (ID == 0)
90       ID = NextAnonGlobalID++;
91
92     // Must mangle the global into a unique ID.
93     getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
94     return;
95   }
96
97   StringRef Name = GV->getName();
98
99   // No need to do anything special if the global has the special "do not
100   // mangle" flag in the name.
101   if (Name[0] == '\1') {
102     OS << Name.substr(1);
103     return;
104   }
105
106   bool UseAt = false;
107   const Function *MSFunc = NULL;
108   CallingConv::ID CC;
109   if (DL->hasMicrosoftFastStdCallMangling()) {
110     if ((MSFunc = dyn_cast<Function>(GV))) {
111       CC = MSFunc->getCallingConv();
112       // fastcall functions need to start with @ instead of _.
113       if (CC == CallingConv::X86_FastCall)
114         UseAt = true;
115     }
116   }
117
118   getNameWithPrefixx(OS, Name, PrefixTy, *DL, UseAt);
119
120   if (!MSFunc)
121     return;
122
123   // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
124   // add it.
125   // fastcall and stdcall functions usually need @42 at the end to specify
126   // the argument info.
127   FunctionType *FT = MSFunc->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 && MSFunc->hasStructRetAttr())))
132     AddFastCallStdCallSuffix(OS, MSFunc, *DL);
133 }
134
135 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
136                                 const GlobalValue *GV) {
137   raw_svector_ostream OS(OutName);
138   getNameWithPrefix(OS, GV);
139 }