Unbreak Win64 CC. Step one: honour register save area, fix some alignment and provide...
[oota-llvm.git] / lib / Target / X86 / X86CallingConv.td
1 //===- X86CallingConv.td - Calling Conventions X86 32/64 ---*- tablegen -*-===//
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 describes the calling conventions for the X86-32 and X86-64
11 // architectures.
12 //
13 //===----------------------------------------------------------------------===//
14
15 /// CCIfSubtarget - Match if the current subtarget has a feature F.
16 class CCIfSubtarget<string F, CCAction A>
17  : CCIf<!strconcat("State.getTarget().getSubtarget<X86Subtarget>().", F), A>;
18
19 //===----------------------------------------------------------------------===//
20 // Return Value Calling Conventions
21 //===----------------------------------------------------------------------===//
22
23 // Return-value conventions common to all X86 CC's.
24 def RetCC_X86Common : CallingConv<[
25   // Scalar values are returned in AX first, then DX.  For i8, the ABI
26   // requires the values to be in AL and AH, however this code uses AL and DL
27   // instead. This is because using AH for the second register conflicts with
28   // the way LLVM does multiple return values -- a return of {i16,i8} would end
29   // up in AX and AH, which overlap. Front-ends wishing to conform to the ABI
30   // for functions that return two i8 values are currently expected to pack the
31   // values into an i16 (which uses AX, and thus AL:AH).
32   CCIfType<[i8] , CCAssignToReg<[AL, DL]>>,
33   CCIfType<[i16], CCAssignToReg<[AX, DX]>>,
34   CCIfType<[i32], CCAssignToReg<[EAX, EDX]>>,
35   CCIfType<[i64], CCAssignToReg<[RAX, RDX]>>,
36   
37   // Vector types are returned in XMM0 and XMM1, when they fit.  XMMM2 and XMM3
38   // can only be used by ABI non-compliant code. If the target doesn't have XMM
39   // registers, it won't have vector types.
40   CCIfType<[v16i8, v8i16, v4i32, v2i64, v4f32, v2f64],
41             CCAssignToReg<[XMM0,XMM1,XMM2,XMM3]>>,
42
43   // MMX vector types are always returned in MM0. If the target doesn't have
44   // MM0, it doesn't support these vector types.
45   CCIfType<[v8i8, v4i16, v2i32, v1i64, v2f32], CCAssignToReg<[MM0]>>,
46
47   // Long double types are always returned in ST0 (even with SSE).
48   CCIfType<[f80], CCAssignToReg<[ST0, ST1]>>
49 ]>;
50
51 // X86-32 C return-value convention.
52 def RetCC_X86_32_C : CallingConv<[
53   // The X86-32 calling convention returns FP values in ST0, unless marked
54   // with "inreg" (used here to distinguish one kind of reg from another,
55   // weirdly; this is really the sse-regparm calling convention) in which
56   // case they use XMM0, otherwise it is the same as the common X86 calling
57   // conv.
58   CCIfInReg<CCIfSubtarget<"hasSSE2()",
59     CCIfType<[f32, f64], CCAssignToReg<[XMM0,XMM1,XMM2]>>>>,
60   CCIfType<[f32,f64], CCAssignToReg<[ST0, ST1]>>,
61   CCDelegateTo<RetCC_X86Common>
62 ]>;
63
64 // X86-32 FastCC return-value convention.
65 def RetCC_X86_32_Fast : CallingConv<[
66   // The X86-32 fastcc returns 1, 2, or 3 FP values in XMM0-2 if the target has
67   // SSE2, otherwise it is the the C calling conventions.
68   // This can happen when a float, 2 x float, or 3 x float vector is split by
69   // target lowering, and is returned in 1-3 sse regs.
70   CCIfType<[f32], CCIfSubtarget<"hasSSE2()", CCAssignToReg<[XMM0,XMM1,XMM2]>>>,
71   CCIfType<[f64], CCIfSubtarget<"hasSSE2()", CCAssignToReg<[XMM0,XMM1,XMM2]>>>,
72   CCDelegateTo<RetCC_X86Common>
73 ]>;
74
75 // X86-64 C return-value convention.
76 def RetCC_X86_64_C : CallingConv<[
77   // The X86-64 calling convention always returns FP values in XMM0.
78   CCIfType<[f32], CCAssignToReg<[XMM0, XMM1]>>,
79   CCIfType<[f64], CCAssignToReg<[XMM0, XMM1]>>,
80
81   // MMX vector types are always returned in XMM0 except for v1i64 which is
82   // returned in RAX. This disagrees with ABI documentation but is bug
83   // compatible with gcc.
84   CCIfType<[v1i64], CCAssignToReg<[RAX]>>,
85   CCIfType<[v8i8, v4i16, v2i32, v2f32], CCAssignToReg<[XMM0, XMM1]>>,
86   CCDelegateTo<RetCC_X86Common>
87 ]>;
88
89 // X86-Win64 C return-value convention.
90 def RetCC_X86_Win64_C : CallingConv<[
91   // The X86-Win64 calling convention always returns __m64 values in RAX.
92   CCIfType<[v8i8, v4i16, v2i32, v1i64], CCAssignToReg<[RAX]>>,
93
94   // And FP in XMM0 only.
95   CCIfType<[f32], CCAssignToReg<[XMM0]>>,
96   CCIfType<[f64], CCAssignToReg<[XMM0]>>,
97
98   // Otherwise, everything is the same as 'normal' X86-64 C CC.
99   CCDelegateTo<RetCC_X86_64_C>
100 ]>;
101
102
103 // This is the root return-value convention for the X86-32 backend.
104 def RetCC_X86_32 : CallingConv<[
105   // If FastCC, use RetCC_X86_32_Fast.
106   CCIfCC<"CallingConv::Fast", CCDelegateTo<RetCC_X86_32_Fast>>,
107   // Otherwise, use RetCC_X86_32_C.
108   CCDelegateTo<RetCC_X86_32_C>
109 ]>;
110
111 // This is the root return-value convention for the X86-64 backend.
112 def RetCC_X86_64 : CallingConv<[
113   // Mingw64 and native Win64 use Win64 CC
114   CCIfSubtarget<"isTargetWin64()", CCDelegateTo<RetCC_X86_Win64_C>>,
115
116   // Otherwise, drop to normal X86-64 CC
117   CCDelegateTo<RetCC_X86_64_C>
118 ]>;
119
120 // This is the return-value convention used for the entire X86 backend.
121 def RetCC_X86 : CallingConv<[
122   CCIfSubtarget<"is64Bit()", CCDelegateTo<RetCC_X86_64>>,
123   CCDelegateTo<RetCC_X86_32>
124 ]>;
125
126 //===----------------------------------------------------------------------===//
127 // X86-64 Argument Calling Conventions
128 //===----------------------------------------------------------------------===//
129
130 def CC_X86_64_C : CallingConv<[
131   // Handles byval parameters.
132   CCIfByVal<CCPassByVal<8, 8>>,
133
134   // Promote i8/i16 arguments to i32.
135   CCIfType<[i8, i16], CCPromoteToType<i32>>,
136
137   // The 'nest' parameter, if any, is passed in R10.
138   CCIfNest<CCAssignToReg<[R10]>>,
139
140   // The first 6 integer arguments are passed in integer registers.
141   CCIfType<[i32], CCAssignToReg<[EDI, ESI, EDX, ECX, R8D, R9D]>>,
142   CCIfType<[i64], CCAssignToReg<[RDI, RSI, RDX, RCX, R8 , R9 ]>>,
143   
144   // The first 8 FP/Vector arguments are passed in XMM registers.
145   CCIfType<[f32, f64, v16i8, v8i16, v4i32, v2i64, v4f32, v2f64],
146             CCIfSubtarget<"hasSSE1()",
147             CCAssignToReg<[XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7]>>>,
148
149   // The first 8 MMX (except for v1i64) vector arguments are passed in XMM
150   // registers on Darwin.
151   CCIfType<[v8i8, v4i16, v2i32, v2f32],
152             CCIfSubtarget<"isTargetDarwin()",
153             CCIfSubtarget<"hasSSE2()",
154             CCAssignToReg<[XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7]>>>>,
155
156   // The first 8 v1i64 vector arguments are passed in GPRs on Darwin.
157   CCIfType<[v1i64],
158             CCIfSubtarget<"isTargetDarwin()",
159             CCAssignToReg<[RDI, RSI, RDX, RCX, R8]>>>,
160  
161   // Integer/FP values get stored in stack slots that are 8 bytes in size and
162   // 8-byte aligned if there are no more registers to hold them.
163   CCIfType<[i32, i64, f32, f64], CCAssignToStack<8, 8>>,
164   
165   // Long doubles get stack slots whose size and alignment depends on the
166   // subtarget.
167   CCIfType<[f80], CCAssignToStack<0, 0>>,
168
169   // Vectors get 16-byte stack slots that are 16-byte aligned.
170   CCIfType<[v16i8, v8i16, v4i32, v2i64, v4f32, v2f64], CCAssignToStack<16, 16>>,
171
172   // __m64 vectors get 8-byte stack slots that are 8-byte aligned.
173   CCIfType<[v8i8, v4i16, v2i32, v1i64, v2f32], CCAssignToStack<8, 8>>
174 ]>;
175
176 // Calling convention used on Win64
177 def CC_X86_Win64_C : CallingConv<[
178   // FIXME: Handle byval stuff.
179   // FIXME: Handle varargs.
180
181   // Promote i8/i16 arguments to i32.
182   CCIfType<[i8, i16], CCPromoteToType<i32>>,
183
184   // The 'nest' parameter, if any, is passed in R10.
185   CCIfNest<CCAssignToReg<[R10]>>,
186
187   // The first 4 integer arguments are passed in integer registers.
188   CCIfType<[i32], CCAssignToRegWithShadow<[ECX , EDX , R8D , R9D ],
189                                           [XMM0, XMM1, XMM2, XMM3]>>,
190   CCIfType<[i64], CCAssignToRegWithShadow<[RCX , RDX , R8  , R9  ],
191                                           [XMM0, XMM1, XMM2, XMM3]>>,
192
193   // The first 4 FP/Vector arguments are passed in XMM registers.
194   CCIfType<[f32, f64, v16i8, v8i16, v4i32, v2i64, v4f32, v2f64],
195            CCAssignToRegWithShadow<[XMM0, XMM1, XMM2, XMM3],
196                                    [RCX , RDX , R8  , R9  ]>>,
197
198   // The first 4 MMX vector arguments are passed in GPRs.
199   CCIfType<[v8i8, v4i16, v2i32, v1i64, v2f32],
200            CCAssignToRegWithShadow<[RCX , RDX , R8  , R9  ],
201                                    [XMM0, XMM1, XMM2, XMM3]>>,
202
203   // Integer/FP values get stored in stack slots that are 8 bytes in size and
204   // 8-byte aligned if there are no more registers to hold them.
205   CCIfType<[i32, i64, f32, f64], CCAssignToStack<8, 8>>,
206
207   // Long doubles get stack slots whose size and alignment depends on the
208   // subtarget.
209   CCIfType<[f80], CCAssignToStack<0, 0>>,
210
211   // Vectors get 16-byte stack slots that are 16-byte aligned.
212   CCIfType<[v16i8, v8i16, v4i32, v2i64, v4f32, v2f64], CCAssignToStack<16, 16>>,
213
214   // __m64 vectors get 8-byte stack slots that are 8-byte aligned.
215   CCIfType<[v8i8, v4i16, v2i32, v1i64], CCAssignToStack<8, 8>>
216 ]>;
217
218 //===----------------------------------------------------------------------===//
219 // X86 C Calling Convention
220 //===----------------------------------------------------------------------===//
221
222 /// CC_X86_32_Common - In all X86-32 calling conventions, extra integers and FP
223 /// values are spilled on the stack, and the first 4 vector values go in XMM
224 /// regs.
225 def CC_X86_32_Common : CallingConv<[
226   // Handles byval parameters.
227   CCIfByVal<CCPassByVal<4, 4>>,
228
229   // The first 3 float or double arguments, if marked 'inreg' and if the call
230   // is not a vararg call and if SSE2 is available, are passed in SSE registers.
231   CCIfNotVarArg<CCIfInReg<CCIfType<[f32,f64],
232                 CCIfSubtarget<"hasSSE2()",
233                 CCAssignToReg<[XMM0,XMM1,XMM2]>>>>>,
234
235   // The first 3 __m64 (except for v1i64) vector arguments are passed in mmx
236   // registers if the call is not a vararg call.
237   CCIfNotVarArg<CCIfType<[v8i8, v4i16, v2i32, v2f32],
238                 CCAssignToReg<[MM0, MM1, MM2]>>>,
239
240   // Integer/Float values get stored in stack slots that are 4 bytes in
241   // size and 4-byte aligned.
242   CCIfType<[i32, f32], CCAssignToStack<4, 4>>,
243   
244   // Doubles get 8-byte slots that are 4-byte aligned.
245   CCIfType<[f64], CCAssignToStack<8, 4>>,
246
247   // Long doubles get slots whose size depends on the subtarget.
248   CCIfType<[f80], CCAssignToStack<0, 4>>,
249
250   // The first 4 SSE vector arguments are passed in XMM registers.
251   CCIfNotVarArg<CCIfType<[v16i8, v8i16, v4i32, v2i64, v4f32, v2f64],
252                 CCAssignToReg<[XMM0, XMM1, XMM2, XMM3]>>>,
253
254   // Other SSE vectors get 16-byte stack slots that are 16-byte aligned.
255   CCIfType<[v16i8, v8i16, v4i32, v2i64, v4f32, v2f64], CCAssignToStack<16, 16>>,
256
257   // __m64 vectors get 8-byte stack slots that are 4-byte aligned. They are
258   // passed in the parameter area.
259   CCIfType<[v8i8, v4i16, v2i32, v1i64], CCAssignToStack<8, 4>>]>;
260
261 def CC_X86_32_C : CallingConv<[
262   // Promote i8/i16 arguments to i32.
263   CCIfType<[i8, i16], CCPromoteToType<i32>>,
264
265   // The 'nest' parameter, if any, is passed in ECX.
266   CCIfNest<CCAssignToReg<[ECX]>>,
267
268   // The first 3 integer arguments, if marked 'inreg' and if the call is not
269   // a vararg call, are passed in integer registers.
270   CCIfNotVarArg<CCIfInReg<CCIfType<[i32], CCAssignToReg<[EAX, EDX, ECX]>>>>,
271
272   // Otherwise, same as everything else.
273   CCDelegateTo<CC_X86_32_Common>
274 ]>;
275
276 def CC_X86_32_FastCall : CallingConv<[
277   // Promote i8/i16 arguments to i32.
278   CCIfType<[i8, i16], CCPromoteToType<i32>>,
279
280   // The 'nest' parameter, if any, is passed in EAX.
281   CCIfNest<CCAssignToReg<[EAX]>>,
282
283   // The first 2 integer arguments are passed in ECX/EDX
284   CCIfType<[i32], CCAssignToReg<[ECX, EDX]>>,
285
286   // Otherwise, same as everything else.
287   CCDelegateTo<CC_X86_32_Common>
288 ]>;
289
290 def CC_X86_32_FastCC : CallingConv<[
291   // Handles byval parameters.  Note that we can't rely on the delegation
292   // to CC_X86_32_Common for this because that happens after code that
293   // puts arguments in registers.
294   CCIfByVal<CCPassByVal<4, 4>>,
295
296   // Promote i8/i16 arguments to i32.
297   CCIfType<[i8, i16], CCPromoteToType<i32>>,
298
299   // The 'nest' parameter, if any, is passed in EAX.
300   CCIfNest<CCAssignToReg<[EAX]>>,
301
302   // The first 2 integer arguments are passed in ECX/EDX
303   CCIfType<[i32], CCAssignToReg<[ECX, EDX]>>,
304
305   // The first 3 float or double arguments, if the call is not a vararg
306   // call and if SSE2 is available, are passed in SSE registers.
307   CCIfNotVarArg<CCIfType<[f32,f64],
308                 CCIfSubtarget<"hasSSE2()",
309                 CCAssignToReg<[XMM0,XMM1,XMM2]>>>>,
310
311   // Doubles get 8-byte slots that are 8-byte aligned.
312   CCIfType<[f64], CCAssignToStack<8, 8>>,
313
314   // Otherwise, same as everything else.
315   CCDelegateTo<CC_X86_32_Common>
316 ]>;