1 //===-- llvm/CodeGen/X86COFFMachineModuleInfo.cpp -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is an MMI implementation for X86 COFF (windows) targets.
12 //===----------------------------------------------------------------------===//
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"
25 X86COFFMachineModuleInfo::X86COFFMachineModuleInfo(const MachineModuleInfo &) {
27 X86COFFMachineModuleInfo::~X86COFFMachineModuleInfo() {
30 void X86COFFMachineModuleInfo::AddFunctionInfo(const Function *F,
31 const X86MachineFunctionInfo &Val) {
32 FunctionInfoMap[F] = Val;
37 static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
38 const TargetData &TD) {
39 X86MachineFunctionInfo Info;
42 switch (F->getCallingConv()) {
43 case CallingConv::X86_StdCall:
44 Info.setDecorationStyle(StdCall);
46 case CallingConv::X86_FastCall:
47 Info.setDecorationStyle(FastCall);
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();
58 // 'Dereference' type in case of byval parameter attribute
59 if (F->paramHasAttr(argNum, Attribute::ByVal))
60 Ty = cast<PointerType>(Ty)->getElementType();
62 // Size should be aligned to DWORD boundary
63 Size += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
66 // We're not supporting tooooo huge arguments :)
67 Info.setBytesToPopOnReturn((unsigned int)Size);
72 /// DecorateCygMingName - Query FunctionInfoMap and use this information for
73 /// various name decorations for Cygwin and MingW.
74 void X86COFFMachineModuleInfo::DecorateCygMingName(SmallVectorImpl<char> &Name,
75 const GlobalValue *GV,
76 const TargetData &TD) {
77 const Function *F = dyn_cast<Function>(GV);
80 // Save function name for later type emission.
81 if (F->isDeclaration())
82 CygMingStubs.insert(StringRef(Name.data(), Name.size()));
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)
89 const X86MachineFunctionInfo *Info;
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];
97 Info = &info_item->second;
100 if (Info->getDecorationStyle() == None) return;
101 const FunctionType *FT = F->getFunctionType();
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();
108 if (Info->getDecorationStyle() == FastCall) {
112 Name.insert(Name.begin(), '@');
116 /// DecorateCygMingName - Query FunctionInfoMap and use this information for
117 /// various name decorations for Cygwin and MingW.
118 void X86COFFMachineModuleInfo::DecorateCygMingName(const MCSymbol *&Name,
120 const GlobalValue *GV,
121 const TargetData &TD) {
122 SmallString<128> NameStr(Name->getName().begin(), Name->getName().end());
123 DecorateCygMingName(NameStr, GV, TD);
125 Name = Ctx.GetOrCreateSymbol(NameStr.str());