Fix TextAlignFillValue in a few places
[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 X86COFFMachineModuleInfo::X86COFFMachineModuleInfo(const MachineModuleInfo &) {
26 }
27 X86COFFMachineModuleInfo::~X86COFFMachineModuleInfo() {
28 }
29
30 void X86COFFMachineModuleInfo::addExternalFunction(const StringRef& Name) {
31   CygMingStubs.insert(Name);
32 }
33
34 /// DecorateCygMingName - Apply various name decorations if the function uses
35 /// stdcall or fastcall calling convention.
36 void X86COFFMachineModuleInfo::DecorateCygMingName(SmallVectorImpl<char> &Name,
37                                                    const GlobalValue *GV,
38                                                    const TargetData &TD) {
39   const Function *F = dyn_cast<Function>(GV);
40   if (!F) return;
41
42   // We don't want to decorate non-stdcall or non-fastcall functions right now
43   CallingConv::ID CC = F->getCallingConv();
44   if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
45     return;
46
47   unsigned ArgWords = 0;
48   DenseMap<const Function*, unsigned>::const_iterator item = FnArgWords.find(F);
49   if (item == FnArgWords.end()) {
50     // Calculate arguments sizes
51     for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
52          AI != AE; ++AI) {
53       const Type* Ty = AI->getType();
54
55       // 'Dereference' type in case of byval parameter attribute
56       if (AI->hasByValAttr())
57         Ty = cast<PointerType>(Ty)->getElementType();
58
59       // Size should be aligned to DWORD boundary
60       ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
61     }
62
63     FnArgWords[F] = ArgWords;
64   } else
65     ArgWords = item->second;
66
67   const FunctionType *FT = F->getFunctionType();
68   // "Pure" variadic functions do not receive @0 suffix.
69   if (!FT->isVarArg() || FT->getNumParams() == 0 ||
70       (FT->getNumParams() == 1 && F->hasStructRetAttr()))
71     raw_svector_ostream(Name) << '@' << ArgWords;
72
73   if (CC == CallingConv::X86_FastCall) {
74     if (Name[0] == '_')
75       Name[0] = '@';
76     else
77       Name.insert(Name.begin(), '@');
78   }
79 }
80
81 /// DecorateCygMingName - Query FunctionInfoMap and use this information for
82 /// various name decorations for Cygwin and MingW.
83 void X86COFFMachineModuleInfo::DecorateCygMingName(MCSymbol *&Name,
84                                                    MCContext &Ctx,
85                                                    const GlobalValue *GV,
86                                                    const TargetData &TD) {
87   SmallString<128> NameStr(Name->getName().begin(), Name->getName().end());
88   DecorateCygMingName(NameStr, GV, TD);
89
90   Name = Ctx.GetOrCreateSymbol(NameStr.str());
91 }