llvm.org GIT mirror llvm / f824868
Adding codegeneration for StdCall & FastCall calling conventions git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30549 91177308-0d34-0410-b5e6-96231b3b80d8 Anton Korobeynikov 14 years ago
8 changed file(s) with 780 addition(s) and 71 deletion(s). Raw diff Collapse all Expand all
1414
1515 #include "X86ATTAsmPrinter.h"
1616 #include "X86.h"
17 #include "X86MachineFunctionInfo.h"
1718 #include "X86TargetMachine.h"
1819 #include "X86TargetAsmInfo.h"
20 #include "llvm/CallingConv.h"
1921 #include "llvm/Module.h"
2022 #include "llvm/Support/Mangler.h"
2123 #include "llvm/Target/TargetAsmInfo.h"
4143
4244 // Print out labels for the function.
4345 const Function *F = MF.getFunction();
46 unsigned CC = F->getCallingConv();
47
48 // Populate function information map. Actually, We don't want to populate
49 // non-stdcall or non-fastcall functions' information right now.
50 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall) {
51 FunctionInfoMap[F] = *(MF.getInfo());
52 }
53
54 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
55
4456 switch (F->getLinkage()) {
4557 default: assert(0 && "Unknown linkage type!");
4658 case Function::InternalLinkage: // Symbols default to internal.
181193 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
182194 if (!isMemOp && !isCallOp) O << '$';
183195
184 GlobalValue *GV = MO.getGlobal();
196 GlobalValue *GV = MO.getGlobal();
185197 std::string Name = Mang->getValueName(GV);
186198
187199 bool isExt = (GV->isExternal() || GV->hasWeakLinkage() ||
188200 GV->hasLinkOnceLinkage());
201
202 X86SharedAsmPrinter::decorateName(Name, (Function*)GV);
203
189204 if (X86PICStyle == PICStyle::Stub &&
190205 TM.getRelocationModel() != Reloc::Static) {
191206 // Link-once, External, or Weakly-linked global variables need
201216 }
202217 } else {
203218 if (GV->hasDLLImportLinkage()) {
204 // FIXME: This should be fixed with full support of stdcall & fastcall
205 // CC's
206219 O << "__imp_";
207220 }
208221 O << Name;
212225 O << "-\"L" << getFunctionNumber() << "$pb\"";
213226 } else {
214227 if (GV->hasDLLImportLinkage()) {
215 // FIXME: This should be fixed with full support of stdcall & fastcall
216 // CC's
217228 O << "__imp_";
218229 }
219230 O << Name;
220
221231 }
222232
223233 int Offset = MO.getOffset();
1616 #include "X86AsmPrinter.h"
1717 #include "X86ATTAsmPrinter.h"
1818 #include "X86IntelAsmPrinter.h"
19 #include "X86MachineFunctionInfo.h"
1920 #include "X86Subtarget.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/CallingConv.h"
2023 #include "llvm/Constants.h"
2124 #include "llvm/Module.h"
2225 #include "llvm/Type.h"
2326 #include "llvm/Assembly/Writer.h"
2427 #include "llvm/Support/Mangler.h"
2528 #include "llvm/Target/TargetAsmInfo.h"
29
2630 using namespace llvm;
2731
2832 Statistic<> llvm::EmittedInsts("asm-printer",
2933 "Number of machine instrs printed");
34
35 static X86FunctionInfo calculateFunctionInfo(const Function* F,
36 const TargetData* TD)
37 {
38 X86FunctionInfo Info;
39 uint64_t size = 0;
40
41 switch (F->getCallingConv()) {
42 case CallingConv::X86_StdCall:
43 Info.setDecorationStyle(StdCall);
44 break;
45 case CallingConv::X86_FastCall:
46 Info.setDecorationStyle(FastCall);
47 break;
48 default:
49 return Info;
50 }
51
52 for (Function::const_arg_iterator AI = F->arg_begin(),
53 AE = F->arg_end();
54 AI != AE;
55 ++AI) {
56 size += TD->getTypeSize(AI->getType());
57 }
58
59 // We're not supporting tooooo huge arguments :)
60 Info.setBytesToPopOnReturn((unsigned int)size);
61
62 return Info;
63 }
64
65
66 // Query FunctionInfoMap and use this information for various name decoration
67 void X86SharedAsmPrinter::decorateName(std::string& Name, const GlobalValue* GV)
68 {
69 const X86FunctionInfo* Info;
70 const Function* F;
71
72 if ((F = dyn_cast(GV)) == NULL) {
73 return;
74 }
75
76 unsigned CC = F->getCallingConv();
77
78 // We don't want to decorate non-stdcall or non-fastcall functions right now
79 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall) {
80 return;
81 }
82
83 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
84
85 if (info_item == FunctionInfoMap.end()) {
86 // Calculate apropriate function info and populate map
87 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
88 Info = &FunctionInfoMap[F];
89 } else {
90 Info = &(info_item->second);
91 }
92
93 switch (Info->getDecorationStyle()) {
94 case None:
95 break;
96 case StdCall:
97 if (!F->isVarArg()) {
98 // Variadic functions do not receive @0 suffix
99 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
100 }
101 break;
102 case FastCall:
103 if (!F->isVarArg()) {
104 // Variadic functions do not receive @0 suffix
105 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
106 }
107 if (Name[0] == '_') {
108 Name[0] = '@';
109 } else {
110 Name = '@' + Name;
111 }
112 break;
113 default:
114 assert(0 && "Unsupported DecorationStyle");
115 }
116
117 }
30118
31119 /// doInitialization
32120 bool X86SharedAsmPrinter::doInitialization(Module &M) {
1616 #define X86ASMPRINTER_H
1717
1818 #include "X86.h"
19 #include "X86MachineFunctionInfo.h"
1920 #include "X86TargetMachine.h"
2021 #include "llvm/CodeGen/AsmPrinter.h"
2122 #include "llvm/CodeGen/DwarfWriter.h"
4344 : AsmPrinter(O, TM, T), DW(O, this, T), X86PICStyle(PICStyle::GOT) {
4445 Subtarget = &TM.getSubtarget();
4546 }
47
48 typedef std::map FMFInfoMap ;
49
50 // We have to propagate some information about MachineFunction to
51 // AsmPrinter. It's ok, when we're printing the function, since we have
52 // access to MachineFunction and can get the appropriate MachineFunctionInfo.
53 // Unfortunately, this is not possible when we're printing reference to
54 // Function (e.g. calling it and so on). Even more, there is no way to get the
55 // corresponding MachineFunctions: it can even be not created at all. That's
56 // why we should use additional structure, when we're collecting all necessary
57 // information.
58
59 // This structure is using e.g. for name decoration for stdcall & fastcall'ed
60 // function, since we have to use arguments' size for decoration.
61 FMFInfoMap FunctionInfoMap;
62
63 void decorateName(std::string& Name, const GlobalValue* GV);
4664
4765 bool doInitialization(Module &M);
4866 bool doFinalization(Module &M);
15221522 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
15231523 }
15241524
1525 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG){
1525 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op,
1526 SelectionDAG &DAG,
1527 bool isFastCall){
15261528 SDOperand Chain = Op.getOperand(0);
15271529 unsigned CallingConv= cast(Op.getOperand(1))->getValue();
15281530 bool isVarArg = cast(Op.getOperand(2))->getValue() != 0;
15451547 { X86::AX, X86::DX },
15461548 { X86::EAX, X86::EDX }
15471549 };
1550 static const unsigned FastCallGPRArgRegs[][2] = {
1551 { X86::CL, X86::DL },
1552 { X86::CX, X86::DX },
1553 { X86::ECX, X86::EDX }
1554 };
15481555 static const unsigned XMMArgRegs[] = {
15491556 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
15501557 };
15571564 case MVT::i8:
15581565 case MVT::i16:
15591566 case MVT::i32:
1560 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1561 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
1562 ++NumIntRegs;
1563 break;
1564 }
1565 #endif
1567 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS);
1568 if (NumIntRegs < MaxNumIntRegs) {
1569 ++NumIntRegs;
1570 break;
1571 }
15661572 // Fall through
15671573 case MVT::f32:
15681574 NumBytes += 4;
15761582 case MVT::v2i64:
15771583 case MVT::v4f32:
15781584 case MVT::v2f64:
1579 if (NumXMMRegs < 4)
1580 NumXMMRegs++;
1581 else {
1582 // XMM arguments have to be aligned on 16-byte boundary.
1583 NumBytes = ((NumBytes + 15) / 16) * 16;
1584 NumBytes += 16;
1585 }
1586 break;
1585 if (isFastCall) {
1586 assert(0 && "Unknown value type!");
1587 } else {
1588 if (NumXMMRegs < 4)
1589 NumXMMRegs++;
1590 else {
1591 // XMM arguments have to be aligned on 16-byte boundary.
1592 NumBytes = ((NumBytes + 15) / 16) * 16;
1593 NumBytes += 16;
1594 }
1595 }
1596 break;
15871597 }
15881598 }
15891599
16081618 case MVT::i8:
16091619 case MVT::i16:
16101620 case MVT::i32:
1611 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1612 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
1613 RegsToPass.push_back(
1614 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1615 Arg));
1616 ++NumIntRegs;
1617 break;
1618 }
1619 #endif
1621 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS);
1622 if (NumIntRegs < MaxNumIntRegs) {
1623 RegsToPass.push_back(
1624 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1625 Arg));
1626 ++NumIntRegs;
1627 break;
1628 }
16201629 // Fall through
16211630 case MVT::f32: {
16221631 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
16401649 case MVT::v2i64:
16411650 case MVT::v4f32:
16421651 case MVT::v2f64:
1643 if (NumXMMRegs < 4) {
1644 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1645 NumXMMRegs++;
1646 } else {
1647 // XMM arguments have to be aligned on 16-byte boundary.
1648 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1649 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1650 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1651 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1652 Arg, PtrOff, DAG.getSrcValue(NULL)));
1653 ArgOffset += 16;
1654 }
1652 if (isFastCall) {
1653 assert(0 && "Unexpected ValueType for argument!");
1654 } else {
1655 if (NumXMMRegs < 4) {
1656 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1657 NumXMMRegs++;
1658 } else {
1659 // XMM arguments have to be aligned on 16-byte boundary.
1660 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1661 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1662 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1663 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1664 Arg, PtrOff, DAG.getSrcValue(NULL)));
1665 ArgOffset += 16;
1666 }
1667 }
1668 break;
16551669 }
16561670 }
16571671
17441758 case MVT::v2i64:
17451759 case MVT::v4f32:
17461760 case MVT::v2f64:
1747 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1748 ResultVals.push_back(Chain.getValue(0));
1749 NodeTys.push_back(RetVT);
1750 break;
1761 if (isFastCall) {
1762 assert(0 && "Unknown value type to return!");
1763 } else {
1764 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1765 ResultVals.push_back(Chain.getValue(0));
1766 NodeTys.push_back(RetVT);
1767 }
1768 break;
17511769 case MVT::f32:
17521770 case MVT::f64: {
17531771 std::vector Tys;
18051823 return Res.getValue(Op.ResNo);
18061824 }
18071825
1826 //===----------------------------------------------------------------------===//
1827 // StdCall Calling Convention implementation
1828 //===----------------------------------------------------------------------===//
1829 // StdCall calling convention seems to be standard for many Windows' API
1830 // routines and around. It differs from C calling convention just a little:
1831 // callee should clean up the stack, not caller. Symbols should be also
1832 // decorated in some fancy way :) It doesn't support any vector arguments.
1833
1834 /// HowToPassStdCallCCArgument - Returns how an formal argument of the specified
1835 /// type should be passed. Returns the size of the stack slot
1836 static void
1837 HowToPassStdCallCCArgument(MVT::ValueType ObjectVT, unsigned &ObjSize) {
1838 switch (ObjectVT) {
1839 default: assert(0 && "Unhandled argument type!");
1840 case MVT::i8: ObjSize = 1; break;
1841 case MVT::i16: ObjSize = 2; break;
1842 case MVT::i32: ObjSize = 4; break;
1843 case MVT::i64: ObjSize = 8; break;
1844 case MVT::f32: ObjSize = 4; break;
1845 case MVT::f64: ObjSize = 8; break;
1846 }
1847 }
1848
1849 SDOperand X86TargetLowering::LowerStdCallCCArguments(SDOperand Op,
1850 SelectionDAG &DAG) {
1851 unsigned NumArgs = Op.Val->getNumValues() - 1;
1852 MachineFunction &MF = DAG.getMachineFunction();
1853 MachineFrameInfo *MFI = MF.getFrameInfo();
1854 SDOperand Root = Op.getOperand(0);
1855 std::vector ArgValues;
1856
1857 // Add DAG nodes to load the arguments... On entry to a function on the X86,
1858 // the stack frame looks like this:
1859 //
1860 // [ESP] -- return address
1861 // [ESP + 4] -- first argument (leftmost lexically)
1862 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
1863 // ...
1864 //
1865 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1866 for (unsigned i = 0; i < NumArgs; ++i) {
1867 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1868 unsigned ArgIncrement = 4;
1869 unsigned ObjSize = 0;
1870 HowToPassStdCallCCArgument(ObjectVT, ObjSize);
1871 if (ObjSize > 4)
1872 ArgIncrement = ObjSize;
1873
1874 SDOperand ArgValue;
1875 // Create the frame index object for this incoming parameter...
1876 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1877 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1878 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
1879 DAG.getSrcValue(NULL));
1880 ArgValues.push_back(ArgValue);
1881 ArgOffset += ArgIncrement; // Move on to the next argument...
1882 }
1883
1884 ArgValues.push_back(Root);
1885
1886 // If the function takes variable number of arguments, make a frame index for
1887 // the start of the first vararg value... for expansion of llvm.va_start.
1888 bool isVarArg = cast(Op.getOperand(2))->getValue() != 0;
1889 if (isVarArg) {
1890 BytesToPopOnReturn = 0; // Callee pops nothing.
1891 BytesCallerReserves = ArgOffset;
1892 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
1893 } else {
1894 BytesToPopOnReturn = ArgOffset; // Callee pops everything..
1895 BytesCallerReserves = 0;
1896 }
1897 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1898 ReturnAddrIndex = 0; // No return address slot generated yet.
1899
1900 MF.getInfo()->setBytesToPopOnReturn(BytesToPopOnReturn);
1901
1902 // Return the new list of results.
1903 std::vector RetVTs(Op.Val->value_begin(),
1904 Op.Val->value_end());
1905 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1906 }
1907
1908
1909 SDOperand X86TargetLowering::LowerStdCallCCCallTo(SDOperand Op,
1910 SelectionDAG &DAG) {
1911 SDOperand Chain = Op.getOperand(0);
1912 unsigned CallingConv= cast(Op.getOperand(1))->getValue();
1913 bool isVarArg = cast(Op.getOperand(2))->getValue() != 0;
1914 bool isTailCall = cast(Op.getOperand(3))->getValue() != 0;
1915 SDOperand Callee = Op.getOperand(4);
1916 MVT::ValueType RetVT= Op.Val->getValueType(0);
1917 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1918
1919 // Count how many bytes are to be pushed on the stack.
1920 unsigned NumBytes = 0;
1921 for (unsigned i = 0; i != NumOps; ++i) {
1922 SDOperand Arg = Op.getOperand(5+2*i);
1923
1924 switch (Arg.getValueType()) {
1925 default: assert(0 && "Unexpected ValueType for argument!");
1926 case MVT::i8:
1927 case MVT::i16:
1928 case MVT::i32:
1929 case MVT::f32:
1930 NumBytes += 4;
1931 break;
1932 case MVT::i64:
1933 case MVT::f64:
1934 NumBytes += 8;
1935 break;
1936 }
1937 }
1938
1939 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1940
1941 // Arguments go on the stack in reverse order, as specified by the ABI.
1942 unsigned ArgOffset = 0;
1943 std::vector MemOpChains;
1944 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1945 for (unsigned i = 0; i != NumOps; ++i) {
1946 SDOperand Arg = Op.getOperand(5+2*i);
1947
1948 switch (Arg.getValueType()) {
1949 default: assert(0 && "Unexpected ValueType for argument!");
1950 case MVT::i8:
1951 case MVT::i16: {
1952 // Promote the integer to 32 bits. If the input type is signed use a
1953 // sign extend, otherwise use a zero extend.
1954 unsigned ExtOp =
1955 dyn_cast(Op.getOperand(5+2*i+1))->getValue() ?
1956 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
1957 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
1958 }
1959 // Fallthrough
1960
1961 case MVT::i32:
1962 case MVT::f32: {
1963 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1964 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1965 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1966 Arg, PtrOff, DAG.getSrcValue(NULL)));
1967 ArgOffset += 4;
1968 break;
1969 }
1970 case MVT::i64:
1971 case MVT::f64: {
1972 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1973 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1974 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1975 Arg, PtrOff, DAG.getSrcValue(NULL)));
1976 ArgOffset += 8;
1977 break;
1978 }
1979 }
1980 }
1981
1982 if (!MemOpChains.empty())
1983 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1984 &MemOpChains[0], MemOpChains.size());
1985
1986 // If the callee is a GlobalAddress node (quite common, every direct call is)
1987 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1988 if (GlobalAddressSDNode *G = dyn_cast(Callee))
1989 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1990 else if (ExternalSymbolSDNode *S = dyn_cast(Callee))
1991 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1992
1993 std::vector NodeTys;
1994 NodeTys.push_back(MVT::Other); // Returns a chain
1995 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1996 std::vector Ops;
1997 Ops.push_back(Chain);
1998 Ops.push_back(Callee);
1999
2000 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
2001 NodeTys, &Ops[0], Ops.size());
2002 SDOperand InFlag = Chain.getValue(1);
2003
2004 // Create the CALLSEQ_END node.
2005 unsigned NumBytesForCalleeToPush;
2006
2007 if (isVarArg) {
2008 NumBytesForCalleeToPush = 0;
2009 } else {
2010 NumBytesForCalleeToPush = NumBytes;
2011 }
2012
2013 NodeTys.clear();
2014 NodeTys.push_back(MVT::Other); // Returns a chain
2015 if (RetVT != MVT::Other)
2016 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
2017 Ops.clear();
2018 Ops.push_back(Chain);
2019 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
2020 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
2021 Ops.push_back(InFlag);
2022 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
2023 if (RetVT != MVT::Other)
2024 InFlag = Chain.getValue(1);
2025
2026 std::vector ResultVals;
2027 NodeTys.clear();
2028 switch (RetVT) {
2029 default: assert(0 && "Unknown value type to return!");
2030 case MVT::Other: break;
2031 case MVT::i8:
2032 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
2033 ResultVals.push_back(Chain.getValue(0));
2034 NodeTys.push_back(MVT::i8);
2035 break;
2036 case MVT::i16:
2037 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
2038 ResultVals.push_back(Chain.getValue(0));
2039 NodeTys.push_back(MVT::i16);
2040 break;
2041 case MVT::i32:
2042 if (Op.Val->getValueType(1) == MVT::i32) {
2043 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
2044 ResultVals.push_back(Chain.getValue(0));
2045 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
2046 Chain.getValue(2)).getValue(1);
2047 ResultVals.push_back(Chain.getValue(0));
2048 NodeTys.push_back(MVT::i32);
2049 } else {
2050 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
2051 ResultVals.push_back(Chain.getValue(0));
2052 }
2053 NodeTys.push_back(MVT::i32);
2054 break;
2055 case MVT::f32:
2056 case MVT::f64: {
2057 std::vector Tys;
2058 Tys.push_back(MVT::f64);
2059 Tys.push_back(MVT::Other);
2060 Tys.push_back(MVT::Flag);
2061 std::vector Ops;
2062 Ops.push_back(Chain);
2063 Ops.push_back(InFlag);
2064 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
2065 &Ops[0], Ops.size());
2066 Chain = RetVal.getValue(1);
2067 InFlag = RetVal.getValue(2);
2068 if (X86ScalarSSE) {
2069 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
2070 // shouldn't be necessary except that RFP cannot be live across
2071 // multiple blocks. When stackifier is fixed, they can be uncoupled.
2072 MachineFunction &MF = DAG.getMachineFunction();
2073 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
2074 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2075 Tys.clear();
2076 Tys.push_back(MVT::Other);
2077 Ops.clear();
2078 Ops.push_back(Chain);
2079 Ops.push_back(RetVal);
2080 Ops.push_back(StackSlot);
2081 Ops.push_back(DAG.getValueType(RetVT));
2082 Ops.push_back(InFlag);
2083 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
2084 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
2085 DAG.getSrcValue(NULL));
2086 Chain = RetVal.getValue(1);
2087 }
2088
2089 if (RetVT == MVT::f32 && !X86ScalarSSE)
2090 // FIXME: we would really like to remember that this FP_ROUND
2091 // operation is okay to eliminate if we allow excess FP precision.
2092 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
2093 ResultVals.push_back(RetVal);
2094 NodeTys.push_back(RetVT);
2095 break;
2096 }
2097 }
2098
2099 // If the function returns void, just return the chain.
2100 if (ResultVals.empty())
2101 return Chain;
2102
2103 // Otherwise, merge everything together with a MERGE_VALUES node.
2104 NodeTys.push_back(MVT::Other);
2105 ResultVals.push_back(Chain);
2106 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
2107 &ResultVals[0], ResultVals.size());
2108 return Res.getValue(Op.ResNo);
2109 }
2110
2111 //===----------------------------------------------------------------------===//
2112 // FastCall Calling Convention implementation
2113 //===----------------------------------------------------------------------===//
2114 //
2115 // The X86 'fastcall' calling convention passes up to two integer arguments in
2116 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
2117 // and requires that the callee pop its arguments off the stack (allowing proper
2118 // tail calls), and has the same return value conventions as C calling convs.
2119 //
2120 // This calling convention always arranges for the callee pop value to be 8n+4
2121 // bytes, which is needed for tail recursion elimination and stack alignment
2122 // reasons.
2123 //
2124
2125 /// HowToPassFastCallCCArgument - Returns how an formal argument of the
2126 /// specified type should be passed. If it is through stack, returns the size of
2127 /// the stack slot; if it is through integer register, returns the number of
2128 /// integer registers are needed.
2129 static void
2130 HowToPassFastCallCCArgument(MVT::ValueType ObjectVT,
2131 unsigned NumIntRegs,
2132 unsigned &ObjSize,
2133 unsigned &ObjIntRegs)
2134 {
2135 ObjSize = 0;
2136 ObjIntRegs = 0;
2137
2138 switch (ObjectVT) {
2139 default: assert(0 && "Unhandled argument type!");
2140 case MVT::i8:
2141 if (NumIntRegs < 2)
2142 ObjIntRegs = 1;
2143 else
2144 ObjSize = 1;
2145 break;
2146 case MVT::i16:
2147 if (NumIntRegs < 2)
2148 ObjIntRegs = 1;
2149 else
2150 ObjSize = 2;
2151 break;
2152 case MVT::i32:
2153 if (NumIntRegs < 2)
2154 ObjIntRegs = 1;
2155 else
2156 ObjSize = 4;
2157 break;
2158 case MVT::i64:
2159 if (NumIntRegs+2 <= 2) {
2160 ObjIntRegs = 2;
2161 } else if (NumIntRegs+1 <= 2) {
2162 ObjIntRegs = 1;
2163 ObjSize = 4;
2164 } else
2165 ObjSize = 8;
2166 case MVT::f32:
2167 ObjSize = 4;
2168 break;
2169 case MVT::f64:
2170 ObjSize = 8;
2171 break;
2172 }
2173 }
2174
2175 SDOperand
2176 X86TargetLowering::LowerFastCallCCArguments(SDOperand Op, SelectionDAG &DAG) {
2177 unsigned NumArgs = Op.Val->getNumValues()-1;
2178 MachineFunction &MF = DAG.getMachineFunction();
2179 MachineFrameInfo *MFI = MF.getFrameInfo();
2180 SDOperand Root = Op.getOperand(0);
2181 std::vector ArgValues;
2182
2183 // Add DAG nodes to load the arguments... On entry to a function the stack
2184 // frame looks like this:
2185 //
2186 // [ESP] -- return address
2187 // [ESP + 4] -- first nonreg argument (leftmost lexically)
2188 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
2189 // ...
2190 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
2191
2192 // Keep track of the number of integer regs passed so far. This can be either
2193 // 0 (neither ECX or EDX used), 1 (ECX is used) or 2 (ECX and EDX are both
2194 // used).
2195 unsigned NumIntRegs = 0;
2196
2197 for (unsigned i = 0; i < NumArgs; ++i) {
2198 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
2199 unsigned ArgIncrement = 4;
2200 unsigned ObjSize = 0;
2201 unsigned ObjIntRegs = 0;
2202
2203 HowToPassFastCallCCArgument(ObjectVT, NumIntRegs, ObjSize, ObjIntRegs);
2204 if (ObjSize > 4)
2205 ArgIncrement = ObjSize;
2206
2207 unsigned Reg = 0;
2208 SDOperand ArgValue;
2209 if (ObjIntRegs) {
2210 switch (ObjectVT) {
2211 default: assert(0 && "Unhandled argument type!");
2212 case MVT::i8:
2213 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::CL,
2214 X86::GR8RegisterClass);
2215 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
2216 break;
2217 case MVT::i16:
2218 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::CX,
2219 X86::GR16RegisterClass);
2220 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
2221 break;
2222 case MVT::i32:
2223 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::ECX,
2224 X86::GR32RegisterClass);
2225 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
2226 break;
2227 case MVT::i64:
2228 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::ECX,
2229 X86::GR32RegisterClass);
2230 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
2231 if (ObjIntRegs == 2) {
2232 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
2233 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
2234 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
2235 }
2236 break;
2237 }
2238
2239 NumIntRegs += ObjIntRegs;
2240 }
2241
2242 if (ObjSize) {
2243 // Create the SelectionDAG nodes corresponding to a load from this
2244 // parameter.
2245 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
2246 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
2247 if (ObjectVT == MVT::i64 && ObjIntRegs) {
2248 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
2249 DAG.getSrcValue(NULL));
2250 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
2251 } else
2252 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
2253 DAG.getSrcValue(NULL));
2254 ArgOffset += ArgIncrement; // Move on to the next argument.
2255 }
2256
2257 ArgValues.push_back(ArgValue);
2258 }
2259
2260 ArgValues.push_back(Root);
2261
2262 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
2263 // arguments and the arguments after the retaddr has been pushed are aligned.
2264 if ((ArgOffset & 7) == 0)
2265 ArgOffset += 4;
2266
2267 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
2268 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
2269 ReturnAddrIndex = 0; // No return address slot generated yet.
2270 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
2271 BytesCallerReserves = 0;
2272
2273 MF.getInfo()->setBytesToPopOnReturn(BytesToPopOnReturn);
2274
2275 // Finally, inform the code generator which regs we return values in.
2276 switch (getValueType(MF.getFunction()->getReturnType())) {
2277 default: assert(0 && "Unknown type!");
2278 case MVT::isVoid: break;
2279 case MVT::i8:
2280 case MVT::i16:
2281 case MVT::i32:
2282 MF.addLiveOut(X86::ECX);
2283 break;
2284 case MVT::i64:
2285 MF.addLiveOut(X86::ECX);
2286 MF.addLiveOut(X86::EDX);
2287 break;
2288 case MVT::f32:
2289 case MVT::f64:
2290 MF.addLiveOut(X86::ST0);
2291 break;
2292 }
2293
2294 // Return the new list of results.
2295 std::vector RetVTs(Op.Val->value_begin(),
2296 Op.Val->value_end());
2297 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
2298 }
2299
18082300 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
18092301 if (ReturnAddrIndex == 0) {
18102302 // Set up a frame object for the return address.
19642456 }
19652457
19662458 /// WindowsGVRequiresExtraLoad - true if accessing the GV requires an extra
1967 /// load. For Windows, dllimported variables (not functions!) are indirect,
1968 /// loading the value at address GV rather then the value of GV itself. This
1969 /// means that the GlobalAddress must be in the base or index register of the
1970 /// address, not the GV offset field.
2459 /// load. For Windows, dllimported symbols are indirect, loading the value at
2460 /// address GV rather then the value of GV itself. This means that the
2461 /// GlobalAddress must be in the base or index register of the address, not the
2462 /// GV offset field.
19712463 static bool WindowsGVRequiresExtraLoad(GlobalValue *GV) {
19722464 return (GV->hasDLLImportLinkage());
19732465 }
37894281
37904282 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
37914283 unsigned CallingConv= cast(Op.getOperand(1))->getValue();
4284
37924285 if (Subtarget->is64Bit())
37934286 return LowerX86_64CCCCallTo(Op, DAG);
3794 else if (CallingConv == CallingConv::Fast && EnableFastCC)
3795 return LowerFastCCCallTo(Op, DAG);
37964287 else
3797 return LowerCCCCallTo(Op, DAG);
4288 switch (CallingConv) {
4289 case CallingConv::Fast:
4290 if (EnableFastCC) {
4291 return LowerFastCCCallTo(Op, DAG, false);
4292 }
4293 // Falls through
4294 case CallingConv::C:
4295 case CallingConv::CSRet:
4296 return LowerCCCCallTo(Op, DAG);
4297 case CallingConv::X86_StdCall:
4298 return LowerStdCallCCCallTo(Op, DAG);
4299 case CallingConv::X86_FastCall:
4300 return LowerFastCCCallTo(Op, DAG, true);
4301 default:
4302 assert(0 && "Unsupported calling convention");
4303 }
37984304 }
37994305
38004306 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
39134419 unsigned CC = cast(Op.getOperand(1))->getValue();
39144420 if (Subtarget->is64Bit())
39154421 return LowerX86_64CCCArguments(Op, DAG);
3916 else if (CC == CallingConv::Fast && EnableFastCC)
3917 return LowerFastCCArguments(Op, DAG);
39184422 else
3919 return LowerCCCArguments(Op, DAG);
4423 switch(CC) {
4424 case CallingConv::Fast:
4425 if (EnableFastCC) {
4426 return LowerFastCCArguments(Op, DAG);
4427 }
4428 // Falls through
4429 case CallingConv::C:
4430 case CallingConv::CSRet:
4431 return LowerCCCArguments(Op, DAG);
4432 case CallingConv::X86_StdCall:
4433 MF.getInfo()->setDecorationStyle(StdCall);
4434 return LowerStdCallCCArguments(Op, DAG);
4435 case CallingConv::X86_FastCall:
4436 MF.getInfo()->setDecorationStyle(FastCall);
4437 return LowerFastCallCCArguments(Op, DAG);
4438 default:
4439 assert(0 && "Unsupported calling convention");
4440 }
39204441 }
39214442
39224443 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
365365
366366 // Fast Calling Convention implementation.
367367 SDOperand LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG);
368 SDOperand LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG);
368 SDOperand LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
369 bool isFastCall);
370
371 // StdCall Calling Convention implementation.
372 SDOperand LowerStdCallCCArguments(SDOperand Op, SelectionDAG &DAG);
373 SDOperand LowerStdCallCCCallTo(SDOperand Op, SelectionDAG &DAG);
374
375 // FastCall Calling Convention implementation.
376 SDOperand LowerFastCallCCArguments(SDOperand Op, SelectionDAG &DAG);
369377
370378 SDOperand LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG);
371379 SDOperand LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG);
1515 #include "X86IntelAsmPrinter.h"
1616 #include "X86TargetAsmInfo.h"
1717 #include "X86.h"
18 #include "llvm/CallingConv.h"
1819 #include "llvm/Constants.h"
1920 #include "llvm/Module.h"
2021 #include "llvm/Assembly/Writer.h"
3536
3637 // Print out labels for the function.
3738 const Function* F = MF.getFunction();
39 unsigned CC = F->getCallingConv();
40
41 // Populate function information map. Actually, We don't want to populate
42 // non-stdcall or non-fastcall functions' information right now.
43 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall) {
44 FunctionInfoMap[F] = *(MF.getInfo());
45 }
46
47 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
48
3849 switch (F->getLinkage()) {
3950 default: assert(0 && "Unsupported linkage type!");
4051 case Function::InternalLinkage:
131142 bool isCallOp = Modifier && !strcmp(Modifier, "call");
132143 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
133144 GlobalValue *GV = MO.getGlobal();
145 std::string Name = Mang->getValueName(GV);
146
147 X86SharedAsmPrinter::decorateName(Name, GV);
134148
135149 if (!isMemOp && !isCallOp) O << "OFFSET ";
136150 if (GV->hasDLLImportLinkage()) {
138152 // CC's
139153 O << "__imp_";
140154 }
141 O << Mang->getValueName(GV);
155 O << Name;
142156 int Offset = MO.getOffset();
143157 if (Offset > 0)
144158 O << " + " << Offset;
321335
322336 // Emit declarations for external functions.
323337 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
324 if (I->isExternal())
325 O << "\textern " << Mang->getValueName(I) << ":near\n";
326
338 if (I->isExternal()) {
339 std::string Name = Mang->getValueName(I);
340 X86SharedAsmPrinter::decorateName(Name, I);
341
342 O << "\textern " ;
343 if (I->hasDLLImportLinkage()) {
344 O << "__imp_";
345 }
346 O << Name << ":near\n";
347 }
348
327349 // Emit declarations for external globals. Note that VC++ always declares
328350 // external globals to have type byte, and if that's good enough for VC++...
329351 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
330352 I != E; ++I) {
331 if (I->isExternal())
332 O << "\textern " << Mang->getValueName(I) << ":byte\n";
353 if (I->isExternal()) {
354 std::string Name = Mang->getValueName(I);
355
356 O << "\textern " ;
357 if (I->hasDLLImportLinkage()) {
358 O << "__imp_";
359 }
360 O << Name << ":byte\n";
361 }
333362 }
334363
335364 return false;
1717
1818 namespace llvm {
1919
20 enum NameDecorationStyle {
21 None,
22 StdCall,
23 FastCall
24 };
25
2026 /// X86FunctionInfo - This class is derived from MachineFunction private
2127 /// X86 target-specific information for each MachineFunction.
2228 class X86FunctionInfo : public MachineFunctionInfo {
23 // ForceFramePointer - True if the function is required to use of frame
24 // pointer for reasons other than it containing dynamic allocation or
25 // that FP eliminatation is turned off. For example, Cygwin main function
26 // contains stack pointer re-alignment code which requires FP.
29 /// ForceFramePointer - True if the function is required to use of frame
30 /// pointer for reasons other than it containing dynamic allocation or
31 /// that FP eliminatation is turned off. For example, Cygwin main function
32 /// contains stack pointer re-alignment code which requires FP.
2733 bool ForceFramePointer;
34
35 /// BytesToPopOnReturn - amount of bytes function pops on return.
36 /// Used on windows platform for stdcall & fastcall name decoration
37 unsigned BytesToPopOnReturn;
38
39 /// If the function requires additional name decoration, DecorationStyle holds
40 /// the right way to do so.
41 NameDecorationStyle DecorationStyle;
42
2843 public:
29 X86FunctionInfo(MachineFunction& MF) : ForceFramePointer(false) {}
44 X86FunctionInfo() : ForceFramePointer(false),
45 BytesToPopOnReturn(0),
46 DecorationStyle(None) {}
47
48 X86FunctionInfo(MachineFunction& MF) : ForceFramePointer(false),
49 BytesToPopOnReturn(0),
50 DecorationStyle(None) {}
51
3052 bool getForceFramePointer() const { return ForceFramePointer;}
3153 void setForceFramePointer(bool forceFP) { ForceFramePointer = forceFP; }
54
55 unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; }
56 void setBytesToPopOnReturn (unsigned bytes) { BytesToPopOnReturn = bytes;}
57
58 NameDecorationStyle getDecorationStyle() const { return DecorationStyle; }
59 void setDecorationStyle(NameDecorationStyle style) { DecorationStyle = style;}
60
3261 };
3362 } // End llvm namespace
3463
966966 // Print the calling convention.
967967 switch (F->getCallingConv()) {
968968 case CallingConv::C: break; // default
969 case CallingConv::CSRet: Out << "csretcc "; break;
970 case CallingConv::Fast: Out << "fastcc "; break;
971 case CallingConv::Cold: Out << "coldcc "; break;
969 case CallingConv::CSRet: Out << "csretcc "; break;
970 case CallingConv::Fast: Out << "fastcc "; break;
971 case CallingConv::Cold: Out << "coldcc "; break;
972 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
973 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
972974 default: Out << "cc" << F->getCallingConv() << " "; break;
973975 }
974976
11581160 case CallingConv::CSRet: Out << " csretcc"; break;
11591161 case CallingConv::Fast: Out << " fastcc"; break;
11601162 case CallingConv::Cold: Out << " coldcc"; break;
1163 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1164 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
11611165 default: Out << " cc" << CI->getCallingConv(); break;
11621166 }
11631167
11961200 case CallingConv::CSRet: Out << " csretcc"; break;
11971201 case CallingConv::Fast: Out << " fastcc"; break;
11981202 case CallingConv::Cold: Out << " coldcc"; break;
1203 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1204 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
11991205 default: Out << " cc" << II->getCallingConv(); break;
12001206 }
12011207