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