make DecorateCygMingName a static method.
[oota-llvm.git] / lib / Target / X86 / X86COFFMachineModuleInfo.cpp
1 //===-- llvm/CodeGen/X86COFFMachineModuleInfo.cpp -------------------------===//
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 // This is an MMI implementation for X86 COFF (windows) targets.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86COFFMachineModuleInfo.h"
15 #include "X86MachineFunctionInfo.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm;
24
25
26 X86COFFMachineModuleInfo::~X86COFFMachineModuleInfo() {
27 }
28
29 /// DecorateCygMingName - Query FunctionInfoMap and use this information for
30 /// various name decorations for Cygwin and MingW.
31 MCSymbol *X86COFFMachineModuleInfo::DecorateCygMingName(MCSymbol *NameSym,
32                                                         MCContext &Ctx,
33                                                         const Function *F,
34                                                         const TargetData &TD) {
35   // We don't want to decorate non-stdcall or non-fastcall functions right now
36   CallingConv::ID CC = F->getCallingConv();
37   if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
38     return NameSym;
39   
40   unsigned ArgWords = 0;
41   
42   // Calculate arguments sizes
43   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
44        AI != AE; ++AI) {
45     const Type *Ty = AI->getType();
46     
47     // 'Dereference' type in case of byval parameter attribute
48     if (AI->hasByValAttr())
49       Ty = cast<PointerType>(Ty)->getElementType();
50     
51     // Size should be aligned to DWORD boundary
52     ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
53   }
54   
55   const FunctionType *FT = F->getFunctionType();
56   
57   SmallString<128> Name(NameSym->getName().begin(), NameSym->getName().end());
58
59   // "Pure" variadic functions do not receive @0 suffix.
60   if (!FT->isVarArg() || FT->getNumParams() == 0 ||
61       (FT->getNumParams() == 1 && F->hasStructRetAttr()))
62     raw_svector_ostream(Name) << '@' << ArgWords;
63   
64   if (CC == CallingConv::X86_FastCall) {
65     if (Name[0] == '_')
66       Name[0] = '@';
67     else
68       Name.insert(Name.begin(), '@');
69   }
70   
71   return Ctx.GetOrCreateSymbol(Name.str());
72 }