blob: fcdeba1b1cc598210e9c621f924eda87c03efbdd [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
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000030void X86COFFMachineModuleInfo::addExternalFunction(const StringRef& Name) {
31 CygMingStubs.insert(Name);
Chris Lattner67c6b6e2009-09-20 06:45:52 +000032}
33
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000034/// DecorateCygMingName - Apply various name decorations if the function uses
35/// stdcall or fastcall calling convention.
Chris Lattner67c6b6e2009-09-20 06:45:52 +000036void 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;
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000041
Chris Lattner67c6b6e2009-09-20 06:45:52 +000042 // 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;
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000046
47 unsigned ArgWords = 0;
Chris Lattner355670b2010-03-12 19:31:03 +000048
49 // Calculate arguments sizes
50 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
51 AI != AE; ++AI) {
52 const Type *Ty = AI->getType();
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000053
Chris Lattner355670b2010-03-12 19:31:03 +000054 // 'Dereference' type in case of byval parameter attribute
55 if (AI->hasByValAttr())
56 Ty = cast<PointerType>(Ty)->getElementType();
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000057
Chris Lattner355670b2010-03-12 19:31:03 +000058 // Size should be aligned to DWORD boundary
59 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
60 }
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000061
Chris Lattner67c6b6e2009-09-20 06:45:52 +000062 const FunctionType *FT = F->getFunctionType();
Chris Lattner67c6b6e2009-09-20 06:45:52 +000063 // "Pure" variadic functions do not receive @0 suffix.
64 if (!FT->isVarArg() || FT->getNumParams() == 0 ||
65 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000066 raw_svector_ostream(Name) << '@' << ArgWords;
67
68 if (CC == CallingConv::X86_FastCall) {
Chris Lattner67c6b6e2009-09-20 06:45:52 +000069 if (Name[0] == '_')
70 Name[0] = '@';
71 else
72 Name.insert(Name.begin(), '@');
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000073 }
Chris Lattner67c6b6e2009-09-20 06:45:52 +000074}
75
76/// DecorateCygMingName - Query FunctionInfoMap and use this information for
77/// various name decorations for Cygwin and MingW.
Chris Lattner5957c842010-01-18 00:59:24 +000078void X86COFFMachineModuleInfo::DecorateCygMingName(MCSymbol *&Name,
Chris Lattner4fb69f42010-01-16 00:51:39 +000079 MCContext &Ctx,
Chris Lattner67c6b6e2009-09-20 06:45:52 +000080 const GlobalValue *GV,
81 const TargetData &TD) {
Chris Lattner4fb69f42010-01-16 00:51:39 +000082 SmallString<128> NameStr(Name->getName().begin(), Name->getName().end());
Chris Lattner67c6b6e2009-09-20 06:45:52 +000083 DecorateCygMingName(NameStr, GV, TD);
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000084
Chris Lattner4fb69f42010-01-16 00:51:39 +000085 Name = Ctx.GetOrCreateSymbol(NameStr.str());
Chris Lattner67c6b6e2009-09-20 06:45:52 +000086}