Chris Lattner | 67c6b6e | 2009-09-20 06:45:52 +0000 | [diff] [blame] | 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" |
Chris Lattner | 4fb69f4 | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCContext.h" |
| 19 | #include "llvm/MC/MCSymbol.h" |
Chris Lattner | 67c6b6e | 2009-09-20 06:45:52 +0000 | [diff] [blame] | 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() { |
Chris Lattner | 67c6b6e | 2009-09-20 06:45:52 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void X86COFFMachineModuleInfo::AddFunctionInfo(const Function *F, |
| 31 | const X86MachineFunctionInfo &Val) { |
| 32 | FunctionInfoMap[F] = Val; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | |
| 37 | static 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. |
| 74 | void 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 Lattner | 5957c84 | 2010-01-18 00:59:24 +0000 | [diff] [blame^] | 118 | void X86COFFMachineModuleInfo::DecorateCygMingName(MCSymbol *&Name, |
Chris Lattner | 4fb69f4 | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 119 | MCContext &Ctx, |
Chris Lattner | 67c6b6e | 2009-09-20 06:45:52 +0000 | [diff] [blame] | 120 | const GlobalValue *GV, |
| 121 | const TargetData &TD) { |
Chris Lattner | 4fb69f4 | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 122 | SmallString<128> NameStr(Name->getName().begin(), Name->getName().end()); |
Chris Lattner | 67c6b6e | 2009-09-20 06:45:52 +0000 | [diff] [blame] | 123 | DecorateCygMingName(NameStr, GV, TD); |
Chris Lattner | 4fb69f4 | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 124 | |
| 125 | Name = Ctx.GetOrCreateSymbol(NameStr.str()); |
Chris Lattner | 67c6b6e | 2009-09-20 06:45:52 +0000 | [diff] [blame] | 126 | } |