| 1 |
(* x86-c-fn.sml |
| 2 |
* |
| 3 |
* |
| 4 |
* C function calls for the X86 using the System V ABI |
| 5 |
* |
| 6 |
* Register conventions: |
| 7 |
* |
| 8 |
* %eax return value (caller save) |
| 9 |
* %ebx global offset for PIC (callee save) |
| 10 |
* %ecx scratch (caller save) |
| 11 |
* %edx extra return/scratch (caller save) |
| 12 |
* %ebp optional frame pointer (callee save) |
| 13 |
* %esp stack pointer (callee save) |
| 14 |
* %esi locals (callee save) |
| 15 |
* %edi locals (callee save) |
| 16 |
* |
| 17 |
* %st(0) top of FP stack; FP return value |
| 18 |
* %st(1..7) FP stack; must be empty on entry and return |
| 19 |
* |
| 20 |
* Calling convention: |
| 21 |
* |
| 22 |
* Return result: |
| 23 |
* + Integer and pointer results are returned in %eax. Small |
| 24 |
* integer results are not promoted. |
| 25 |
* + 64-bit integers (long long) returned in %eax/%edx |
| 26 |
* + Floating point results are returned in %st(0) (all types). |
| 27 |
* + Struct results are returned in space provided by the caller. |
| 28 |
* The address of this space is passed to the callee as an |
| 29 |
* implicit 0th argument, and on return %eax contains this |
| 30 |
* address. The called function is responsible for removing |
| 31 |
* this argument from the stack using a "ret $4" instruction. |
| 32 |
* NOTE: the MacOS X ABI returns small structs in %eax/%edx. |
| 33 |
* |
| 34 |
* Function arguments: |
| 35 |
* + Arguments are pushed on the stack right to left. |
| 36 |
* + Integral and pointer arguments take one word on the stack. |
| 37 |
* + float arguments take one word on the stack. |
| 38 |
* + double arguments take two words on the stack. The i386 ABI does |
| 39 |
* not require double word alignment for these arguments. |
| 40 |
* + long double arguments take three words on the stack. |
| 41 |
* + struct arguments are padded out to word length. |
| 42 |
* |
| 43 |
*) |
| 44 |
|
| 45 |
functor X86CConventionFn ( |
| 46 |
type reg_id |
| 47 |
val eax : reg_id |
| 48 |
val edx : reg_id |
| 49 |
val st0 : reg_id |
| 50 |
structure SA : STAGED_ALLOCATION |
| 51 |
where type reg_id = reg_id |
| 52 |
where type loc_kind = CLocKind.loc_kind |
| 53 |
) = struct |
| 54 |
|
| 55 |
datatype loc_kind = datatype CLocKind.loc_kind |
| 56 |
|
| 57 |
fun gpr r = (32, GPR, r) |
| 58 |
fun gprs rs = List.map gpr rs |
| 59 |
fun fpr r = (80, FPR, r) |
| 60 |
fun fprs rs = List.map fpr rs |
| 61 |
|
| 62 |
(* conventions for calling a C function *) |
| 63 |
val maxAlign = 16 |
| 64 |
val cStack = SA.freshCounter() |
| 65 |
val params = [ |
| 66 |
SA.SEQ[ |
| 67 |
SA.WIDEN (fn ty => Int.max(32, ty)), |
| 68 |
SA.OVERFLOW {counter=cStack, blockDirection=SA.UP, maxAlign=maxAlign} |
| 69 |
] |
| 70 |
] |
| 71 |
|
| 72 |
(* conventions for returning from a C call *) |
| 73 |
val (cInt, retInGpr) = SA.useRegs (gprs [eax, edx]) |
| 74 |
val (cFloat, retInFpr) = SA.useRegs (fprs [st0]) |
| 75 |
val returns = [ |
| 76 |
SA.CHOICE [ |
| 77 |
(* return in general-purpose register *) |
| 78 |
(fn (ty, k, store) => k = GPR, |
| 79 |
SA.SEQ [SA.WIDEN (fn ty => Int.max(ty, 32)), |
| 80 |
retInGpr]), |
| 81 |
(* return in floating-point register *) |
| 82 |
(fn (ty, k, store) => k = FPR, |
| 83 |
SA.SEQ [SA.WIDEN (fn ty => 80), retInFpr]) |
| 84 |
] |
| 85 |
] |
| 86 |
|
| 87 |
(* initial store *) |
| 88 |
val store0 = SA.init [cInt, cFloat, cStack] |
| 89 |
|
| 90 |
end (* X86CConventionsFn *) |