| 1 |
(* x86-64-c-fn.sml |
| 2 |
* |
| 3 |
* C calling convention for the X86-64 using the System V ABI |
| 4 |
* |
| 5 |
* Register conventions: |
| 6 |
* %rax return value (caller save) |
| 7 |
* %rbx optional base pointer (callee save) |
| 8 |
* %rbp optional frame pointer (callee save) |
| 9 |
* %rdi arg 1 (caller save) |
| 10 |
* %rsi arg 2 (caller save) |
| 11 |
* %rdx arg 3 (caller save) |
| 12 |
* %rcx arg 4 (caller save) |
| 13 |
* %r8 arg 5 (caller save) |
| 14 |
* %r9 arg 6 (caller save) |
| 15 |
* %r10 chain pointer (caller save) |
| 16 |
* %r11 scratch (caller save) |
| 17 |
* %r12-r15 general purpose (callee save) |
| 18 |
* %xmm0-xmm1 pass/return fp (caller save) |
| 19 |
* %xmm2-xmm7 pass fp (caller save) |
| 20 |
* %xmm8-xmm15 scratch (caller save) |
| 21 |
* |
| 22 |
* Calling conventions: |
| 23 |
* |
| 24 |
* Return result: |
| 25 |
* + Integer and pointer results are returned in %rax. |
| 26 |
* + 128-bit integers returned in %rax/%rdx. |
| 27 |
* + Floating-point results returned in %xmm0/%xmm1. |
| 28 |
* + Small structs returned in %rax/%rdx. Otherwise, returned |
| 29 |
* in space provided by the caller. |
| 30 |
* |
| 31 |
* Function arguments: |
| 32 |
* + The first 6 integer arguments go in the argument registers. |
| 33 |
* + The first 8 floating-point registers go in %xmm0-xmm8. |
| 34 |
* + Otherwise, arguments are pushed on the stack right to left. |
| 35 |
* + The stack is 16-byte aligned. |
| 36 |
* + Struct arguments are padded out to word length. |
| 37 |
*) |
| 38 |
|
| 39 |
|
| 40 |
functor X86_64CConventionFn ( |
| 41 |
type reg_id |
| 42 |
|
| 43 |
(* relevant GPRs *) |
| 44 |
val rax : reg_id |
| 45 |
val rdi : reg_id |
| 46 |
val rsi : reg_id |
| 47 |
val rdx : reg_id |
| 48 |
val rcx : reg_id |
| 49 |
val r8 : reg_id |
| 50 |
val r9 : reg_id |
| 51 |
(* relevant FPRs*) |
| 52 |
val xmm0 : reg_id |
| 53 |
val xmm1 : reg_id |
| 54 |
val xmm2 : reg_id |
| 55 |
val xmm3 : reg_id |
| 56 |
val xmm4 : reg_id |
| 57 |
val xmm5 : reg_id |
| 58 |
val xmm6 : reg_id |
| 59 |
val xmm7 : reg_id |
| 60 |
|
| 61 |
structure SA : STAGED_ALLOCATION |
| 62 |
where type reg_id = reg_id |
| 63 |
where type loc_kind = CLocKind.loc_kind |
| 64 |
|
| 65 |
) = struct |
| 66 |
|
| 67 |
datatype loc_kind = datatype CLocKind.loc_kind |
| 68 |
|
| 69 |
fun gpr r = (64, GPR, r) |
| 70 |
fun gprs rs = List.map gpr rs |
| 71 |
fun fpr r = (64, FPR, r) |
| 72 |
fun fprs rs = List.map fpr rs |
| 73 |
|
| 74 |
(* registers *) |
| 75 |
val gprRetRegs = gprs [rax, rdx] |
| 76 |
val fprRetRegs = fprs [xmm0, xmm1] |
| 77 |
|
| 78 |
val gprParamRegs = gprs [rdi, rsi, rdx, rcx, r8, r9] |
| 79 |
val fprParamRegs = fprs [xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7] |
| 80 |
|
| 81 |
(* staged-allocation counters *) |
| 82 |
val cCallStk = SA.freshCounter () |
| 83 |
val cCallGpr = SA.freshCounter () |
| 84 |
val cCallFpr = SA.freshCounter () |
| 85 |
val cRetGpr = SA.freshCounter () |
| 86 |
val cRetFpr = SA.freshCounter () |
| 87 |
|
| 88 |
(* max frame alignment in bytes *) |
| 89 |
val maxAlign = 16 |
| 90 |
|
| 91 |
(* initial store *) |
| 92 |
val store0 = SA.init [cCallStk, cCallGpr, cCallFpr, cRetFpr, cRetGpr] |
| 93 |
|
| 94 |
(* rules for passing arguments *) |
| 95 |
val params = [ |
| 96 |
SA.WIDEN (fn w => Int.max (64, w)), |
| 97 |
SA.CHOICE [ |
| 98 |
(fn (w, k, store) => (k = GPR), SA.SEQ [ |
| 99 |
SA.BITCOUNTER cCallGpr, |
| 100 |
SA.REGS_BY_BITS (cCallGpr, gprParamRegs) |
| 101 |
]), |
| 102 |
(fn (w, k, store) => (k = FPR), SA.SEQ [ |
| 103 |
SA.BITCOUNTER cCallFpr, |
| 104 |
SA.REGS_BY_BITS (cCallFpr, fprParamRegs) |
| 105 |
]), |
| 106 |
(fn (w, k, store) => (k = STK orelse k = FSTK), |
| 107 |
SA.OVERFLOW {counter=cCallStk, |
| 108 |
blockDirection=SA.UP, |
| 109 |
maxAlign=maxAlign}) |
| 110 |
], |
| 111 |
SA.OVERFLOW {counter=cCallStk, |
| 112 |
blockDirection=SA.UP, |
| 113 |
maxAlign=maxAlign} |
| 114 |
] |
| 115 |
|
| 116 |
(* rules for returning values *) |
| 117 |
val returns = [ |
| 118 |
SA.WIDEN (fn w => Int.max (64, w)), |
| 119 |
SA.CHOICE [ |
| 120 |
(fn (w, k, store) => (k = GPR), |
| 121 |
SA.SEQ [ |
| 122 |
SA.BITCOUNTER cRetGpr, |
| 123 |
SA.REGS_BY_BITS (cRetGpr, gprRetRegs)]), |
| 124 |
(fn (w, k, store) => (k = FPR), |
| 125 |
SA.SEQ [ |
| 126 |
SA.BITCOUNTER cRetFpr, |
| 127 |
SA.REGS_BY_BITS (cRetGpr, fprRetRegs)])] |
| 128 |
] |
| 129 |
end |