blob: 07a1b38169886fc6de1214d9b6d882c2d7f189ae [file] [log] [blame]
Chris Lattner67c6b6e2009-09-20 06:45:52 +00001//===-- 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"
Chris Lattner4fb69f42010-01-16 00:51:39 +000018#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCSymbol.h"
Chris Lattner67c6b6e2009-09-20 06:45:52 +000020#include "llvm/Target/TargetData.h"
21#include "llvm/ADT/SmallString.h"
22#include "llvm/Support/raw_ostream.h"
23using namespace llvm;
24
25X86COFFMachineModuleInfo::X86COFFMachineModuleInfo(const MachineModuleInfo &) {
26}
27X86COFFMachineModuleInfo::~X86COFFMachineModuleInfo() {
Chris Lattner67c6b6e2009-09-20 06:45:52 +000028}
29
30void X86COFFMachineModuleInfo::AddFunctionInfo(const Function *F,
31 const X86MachineFunctionInfo &Val) {
32 FunctionInfoMap[F] = Val;
33}
34
35
36
37static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
38 const TargetData &TD) {
39 X86MachineFunctionInfo Info;
40 uint64_t Size = 0;
41
42 switch (F->getCallingConv()) {
43 case CallingConv::X86_StdCall:
44 Info.setDecorationStyle(StdCall);
45 break;
46 case CallingConv::X86_FastCall:
47 Info.setDecorationStyle(FastCall);
48 break;
49 default:
50 return Info;
51 }
52
53 unsigned argNum = 1;
54 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
55 AI != AE; ++AI, ++argNum) {
56 const Type* Ty = AI->getType();
57
58 // 'Dereference' type in case of byval parameter attribute
59 if (F->paramHasAttr(argNum, Attribute::ByVal))
60 Ty = cast<PointerType>(Ty)->getElementType();
61
62 // Size should be aligned to DWORD boundary
63 Size += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
64 }
65
66 // We're not supporting tooooo huge arguments :)
67 Info.setBytesToPopOnReturn((unsigned int)Size);
68 return Info;
69}
70
71
72/// DecorateCygMingName - Query FunctionInfoMap and use this information for
73/// various name decorations for Cygwin and MingW.
74void X86COFFMachineModuleInfo::DecorateCygMingName(SmallVectorImpl<char> &Name,
75 const GlobalValue *GV,
76 const TargetData &TD) {
77 const Function *F = dyn_cast<Function>(GV);
78 if (!F) return;
79
80 // Save function name for later type emission.
81 if (F->isDeclaration())
82 CygMingStubs.insert(StringRef(Name.data(), Name.size()));
83
84 // We don't want to decorate non-stdcall or non-fastcall functions right now
85 CallingConv::ID CC = F->getCallingConv();
86 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
87 return;
88
89 const X86MachineFunctionInfo *Info;
90
91 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
92 if (info_item == FunctionInfoMap.end()) {
93 // Calculate apropriate function info and populate map
94 FunctionInfoMap[F] = calculateFunctionInfo(F, TD);
95 Info = &FunctionInfoMap[F];
96 } else {
97 Info = &info_item->second;
98 }
99
100 if (Info->getDecorationStyle() == None) return;
101 const FunctionType *FT = F->getFunctionType();
102
103 // "Pure" variadic functions do not receive @0 suffix.
104 if (!FT->isVarArg() || FT->getNumParams() == 0 ||
105 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
106 raw_svector_ostream(Name) << '@' << Info->getBytesToPopOnReturn();
107
108 if (Info->getDecorationStyle() == FastCall) {
109 if (Name[0] == '_')
110 Name[0] = '@';
111 else
112 Name.insert(Name.begin(), '@');
113 }
114}
115
116/// DecorateCygMingName - Query FunctionInfoMap and use this information for
117/// various name decorations for Cygwin and MingW.
Chris Lattner4fb69f42010-01-16 00:51:39 +0000118void X86COFFMachineModuleInfo::DecorateCygMingName(const MCSymbol *&Name,
119 MCContext &Ctx,
Chris Lattner67c6b6e2009-09-20 06:45:52 +0000120 const GlobalValue *GV,
121 const TargetData &TD) {
Chris Lattner4fb69f42010-01-16 00:51:39 +0000122 SmallString<128> NameStr(Name->getName().begin(), Name->getName().end());
Chris Lattner67c6b6e2009-09-20 06:45:52 +0000123 DecorateCygMingName(NameStr, GV, TD);
Chris Lattner4fb69f42010-01-16 00:51:39 +0000124
125 Name = Ctx.GetOrCreateSymbol(NameStr.str());
Chris Lattner67c6b6e2009-09-20 06:45:52 +0000126}