1234353Sdim//===-- SparcCallingConv.td - Calling Conventions Sparc ----*- tablegen -*-===//
2234353Sdim//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7234353Sdim//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This describes the calling conventions for the Sparc architectures.
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14193323Sed//===----------------------------------------------------------------------===//
15251662Sdim// SPARC v8 32-bit.
16193323Sed//===----------------------------------------------------------------------===//
17193323Sed
18193323Seddef CC_Sparc32 : CallingConv<[
19263508Sdim  // Custom assign SRet to [sp+64].
20218893Sdim  CCIfSRet<CCCustom<"CC_Sparc_Assign_SRet">>,
21218893Sdim  // i32 f32 arguments get passed in integer registers if there is space.
22218893Sdim  CCIfType<[i32, f32], CCAssignToReg<[I0, I1, I2, I3, I4, I5]>>,
23218893Sdim  // f64 arguments are split and passed through registers or through stack.
24218893Sdim  CCIfType<[f64], CCCustom<"CC_Sparc_Assign_f64">>,
25218893Sdim
26193323Sed  // Alternatively, they are assigned to the stack in 4-byte aligned units.
27193323Sed  CCAssignToStack<4, 4>
28193323Sed]>;
29249423Sdim
30251662Sdimdef RetCC_Sparc32 : CallingConv<[
31251662Sdim  CCIfType<[i32], CCAssignToReg<[I0, I1, I2, I3, I4, I5]>>,
32251662Sdim  CCIfType<[f32], CCAssignToReg<[F0, F1, F2, F3]>>,
33251662Sdim  CCIfType<[f64], CCAssignToReg<[D0, D1]>>
34251662Sdim]>;
35251662Sdim
36251662Sdim
37251662Sdim//===----------------------------------------------------------------------===//
38251662Sdim// SPARC v9 64-bit.
39251662Sdim//===----------------------------------------------------------------------===//
40251662Sdim//
41251662Sdim// The 64-bit ABI conceptually assigns all function arguments to a parameter
42251662Sdim// array starting at [%fp+BIAS+128] in the callee's stack frame. All arguments
43251662Sdim// occupy a multiple of 8 bytes in the array. Integer arguments are extended to
44251662Sdim// 64 bits by the caller. Floats are right-aligned in their 8-byte slot, the
45251662Sdim// first 4 bytes in the slot are undefined.
46251662Sdim//
47251662Sdim// The integer registers %i0 to %i5 shadow the first 48 bytes of the parameter
48251662Sdim// array at fixed offsets. Integer arguments are promoted to registers when
49251662Sdim// possible.
50251662Sdim//
51251662Sdim// The floating point registers %f0 to %f31 shadow the first 128 bytes of the
52251662Sdim// parameter array at fixed offsets. Float and double parameters are promoted
53251662Sdim// to these registers when possible.
54251662Sdim//
55251662Sdim// Structs up to 16 bytes in size are passed by value. They are right-aligned
56251662Sdim// in one or two 8-byte slots in the parameter array. Struct members are
57251662Sdim// promoted to both floating point and integer registers when possible. A
58251662Sdim// struct containing two floats would thus be passed in %f0 and %f1, while two
59251662Sdim// float function arguments would occupy 8 bytes each, and be passed in %f1 and
60251662Sdim// %f3.
61251662Sdim//
62251662Sdim// When a struct { int, float } is passed by value, the int goes in the high
63251662Sdim// bits of an integer register while the float goes in a floating point
64251662Sdim// register.
65251662Sdim//
66251662Sdim// The difference is encoded in LLVM IR using the inreg atttribute on function
67251662Sdim// arguments:
68251662Sdim//
69251662Sdim//   C:   void f(float, float);
70251662Sdim//   IR:  declare void f(float %f1, float %f3)
71251662Sdim//
72251662Sdim//   C:   void f(struct { float f0, f1; });
73251662Sdim//   IR:  declare void f(float inreg %f0, float inreg %f1)
74251662Sdim//
75251662Sdim//   C:   void f(int, float);
76251662Sdim//   IR:  declare void f(int signext %i0, float %f3)
77251662Sdim//
78251662Sdim//   C:   void f(struct { int i0high; float f1; });
79251662Sdim//   IR:  declare void f(i32 inreg %i0high, float inreg %f1)
80251662Sdim//
81251662Sdim// Two ints in a struct are simply coerced to i64:
82251662Sdim//
83251662Sdim//   C:   void f(struct { int i0high, i0low; });
84251662Sdim//   IR:  declare void f(i64 %i0.coerced)
85251662Sdim//
86251662Sdim// The frontend and backend divide the task of producing ABI compliant code for
87251662Sdim// C functions. The C frontend will:
88251662Sdim//
89251662Sdim//  - Annotate integer arguments with zeroext or signext attributes.
90251662Sdim//
91251662Sdim//  - Split structs into one or two 64-bit sized chunks, or 32-bit chunks with
92251662Sdim//    inreg attributes.
93251662Sdim//
94251662Sdim//  - Pass structs larger than 16 bytes indirectly with an explicit pointer
95251662Sdim//    argument. The byval attribute is not used.
96251662Sdim//
97251662Sdim// The backend will:
98251662Sdim//
99251662Sdim//  - Assign all arguments to 64-bit aligned stack slots, 32-bits for inreg.
100251662Sdim//
101251662Sdim//  - Promote to integer or floating point registers depending on type.
102251662Sdim//
103251662Sdim// Function return values are passed exactly like function arguments, except a
104251662Sdim// struct up to 32 bytes in size can be returned in registers.
105251662Sdim
106263763Sdim// Function arguments AND most return values.
107249423Sdimdef CC_Sparc64 : CallingConv<[
108251662Sdim  // The frontend uses the inreg flag to indicate i32 and float arguments from
109251662Sdim  // structs. These arguments are not promoted to 64 bits, but they can still
110251662Sdim  // be assigned to integer and float registers.
111251662Sdim  CCIfInReg<CCIfType<[i32, f32], CCCustom<"CC_Sparc64_Half">>>,
112251662Sdim
113249423Sdim  // All integers are promoted to i64 by the caller.
114249423Sdim  CCIfType<[i32], CCPromoteToType<i64>>,
115249423Sdim
116251662Sdim  // Custom assignment is required because stack space is reserved for all
117251662Sdim  // arguments whether they are passed in registers or not.
118251662Sdim  CCCustom<"CC_Sparc64_Full">
119249423Sdim]>;
120263508Sdim
121263763Sdimdef RetCC_Sparc64 : CallingConv<[
122263763Sdim  // A single f32 return value always goes in %f0. The ABI doesn't specify what
123263763Sdim  // happens to multiple f32 return values outside a struct.
124263763Sdim  CCIfType<[f32], CCCustom<"CC_Sparc64_Half">>,
125263763Sdim
126263763Sdim  // Otherwise, return values are passed exactly like arguments.
127263763Sdim  CCDelegateTo<CC_Sparc64>
128263763Sdim]>;
129263763Sdim
130263508Sdim// Callee-saved registers are handled by the register window mechanism.
131263508Sdimdef CSR : CalleeSavedRegs<(add)> {
132263508Sdim  let OtherPreserved = (add (sequence "I%u", 0, 7),
133263508Sdim                            (sequence "L%u", 0, 7));
134263508Sdim}
135263508Sdim
136263508Sdim// Callee-saved registers for calls with ReturnsTwice attribute.
137263508Sdimdef RTCSR : CalleeSavedRegs<(add)> {
138263508Sdim  let OtherPreserved = (add I6, I7);
139263508Sdim}
140