| 1 |
(* sparc-c-fn.sml |
| 2 |
* |
| 3 |
* Calling convention for the Sparc: |
| 4 |
* |
| 5 |
* Return result: |
| 6 |
* + Integer and pointer results are returned in %o0 |
| 7 |
* + 64-bit integers (long long) returned in %o1/%o1 |
| 8 |
* + float results are returned in %f0; double in %f0/%f1 |
| 9 |
* + Struct results are returned in space provided by the caller. |
| 10 |
* The address of this space is passed to the callee as a hidden |
| 11 |
* implicit argument on the stack (in the caller's frame). It |
| 12 |
* gets stored at [%sp+64] (from the caller's point of view). |
| 13 |
* An UNIMP instruction must be placed after the call instruction, |
| 14 |
* indicating how much space has been reserved for the return value. |
| 15 |
* + long double results are returned like structs |
| 16 |
* |
| 17 |
* Function arguments: |
| 18 |
* + Arguments that are smaller than a word are promoted to word-size. |
| 19 |
* + Up to six argument words (words 0-5) are passed in registers |
| 20 |
* %o0...%o5. This includes doubles and long longs. Alignment for |
| 21 |
* those types is NOT maintained, i.e., it is possible for an 8-byte |
| 22 |
* quantity to end up in an odd-even register pair. |
| 23 |
* * Arguments beyond 6 words are passed on the stack in the caller's |
| 24 |
* frame. For this, the caller must reserve space in its frame |
| 25 |
* prior to the call. Argument word 6 appears at [%sp+92], word 7 |
| 26 |
* at [%sp+96], ... |
| 27 |
* + struct arguments are passed as pointers to a copy of the struct. |
| 28 |
* The copy itself is allocated by the caller in its stack frame. |
| 29 |
* + long double arguments are passed like structs (i.e., via pointer |
| 30 |
* to temp copy) |
| 31 |
* + Space for argument words 0-5 is already allocated in the |
| 32 |
* caller's frame. This space might be used by the callee to |
| 33 |
* save those arguments that must be addressable. %o0 corresponds |
| 34 |
* to [%sp+68], %o1 to [%sp+72], ... |
| 35 |
*) |
| 36 |
|
| 37 |
functor SparcCConventionFn ( |
| 38 |
type reg_id |
| 39 |
|
| 40 |
(* parameter GPRs*) |
| 41 |
val r8 : reg_id |
| 42 |
val r9 : reg_id |
| 43 |
val r10 : reg_id |
| 44 |
val r11 : reg_id |
| 45 |
val r12 : reg_id |
| 46 |
val r13 : reg_id |
| 47 |
(* parameter FPRs *) |
| 48 |
val f0 : reg_id |
| 49 |
val f1 : reg_id |
| 50 |
|
| 51 |
structure SA : STAGED_ALLOCATION |
| 52 |
where type reg_id = reg_id |
| 53 |
where type loc_kind = CLocKind.loc_kind |
| 54 |
|
| 55 |
) = struct |
| 56 |
|
| 57 |
datatype loc_kind = datatype CLocKind.loc_kind |
| 58 |
|
| 59 |
fun gpr r = (32, GPR, r) |
| 60 |
fun gprs rs = List.map gpr rs |
| 61 |
fun fpr r = (64, FPR, r) |
| 62 |
fun fprs rs = List.map fpr rs |
| 63 |
|
| 64 |
(* convention for calling a C function *) |
| 65 |
val (cParam, paramRegs) = SA.useRegs (List.map gpr [r8, r9, r10, r12, r13]) |
| 66 |
val cStack = SA.freshCounter() |
| 67 |
val params = [ |
| 68 |
SA.WIDEN (fn w => Int.max(32, w)), |
| 69 |
paramRegs, |
| 70 |
SA.OVERFLOW{counter=cStack, blockDirection=SA.UP, maxAlign=8} |
| 71 |
] |
| 72 |
|
| 73 |
(* convention for returning values *) |
| 74 |
val (cFRet, retFlt) = SA.useRegs (List.map fpr [f0, f1]) |
| 75 |
val (cRet, retGpr) = SA.useRegs (List.map gpr [r8]) |
| 76 |
val return = [ |
| 77 |
SA.WIDEN (fn w => Int.max(32, w)), |
| 78 |
SA.CHOICE [ |
| 79 |
(fn (w, k, store) => k = FPR, retFlt), |
| 80 |
(fn (w, k, store) => true, retGpr) |
| 81 |
] |
| 82 |
] |
| 83 |
|
| 84 |
(* initial store *) |
| 85 |
val store0 = SA.init[cStack, cParam, cFRet, cRet] |
| 86 |
|
| 87 |
end |