Debian Codegraph @

lib/mlrisc-lib/MLRISC/c-call/archs/x86-64-svid-fn.sml

raw
1 (* x86-64-svid-fn.sml
2 *
3 * C calling-sequence generator for x86-64.
4 *
5 * Mike Rainey ([email protected])
6 *)
7
8 functor X86_64SVIDFn (
9 structure T : MLTREE
10 ) = struct
11
12 structure T = T
13 structure C = AMD64Cells
14 structure CB = CellsBasis
15 structure CTy = CType
16
17 val wordTy = 64
18 val mem = T.Region.memory
19 val stack = T.Region.stack
20
21 fun lit i = T.LI (T.I.fromInt (wordTy, i))
22 fun gpr r = T.GPR (T.REG (wordTy, r))
23 fun fpr (ty, f) = T.FPR (T.FREG (ty, f))
24 fun sum ls = List.foldl (op +) 0 ls
25 fun szBOfCTy cTy = #sz (CSizes.sizeOfTy cTy)
26 fun alignBOfCTy cTy = #align (CSizes.sizeOfTy cTy)
27 val spReg = T.REG (wordTy, C.rsp)
28 fun offSp 0 = spReg
29 | offSp offset = T.ADD (wordTy, spReg, T.LI offset)
30
31 datatype loc_kind = datatype CLocKind.loc_kind
32
33 structure SA = StagedAllocationFn (
34 type reg_id = T.reg
35 datatype loc_kind = datatype loc_kind
36 val memSize = 8 (* bytes *))
37
38 structure Gen = CCallGenFn (
39 structure T = T
40 structure C = C
41 val wordTy = wordTy
42 val offSp = offSp
43 structure SA = SA
44 fun lobits {e, nBits, width} = e
45 fun sx {fromWidth, toWidth, e} = T.SX(toWidth, fromWidth, e)
46 fun f2f {fromWidth, toWidth, e} = e
47 )
48
49 datatype c_arg = datatype Gen.c_arg
50
51 structure CCs = X86_64CConventionFn (
52 structure SA = SA
53 type reg_id = T.reg
54 val rax = C.rax
55 val rdi = C.rdi
56 val rsi = C.rsi
57 val rdx = C.rdx
58 val rcx = C.rcx
59 val r8 = C.r8
60 val r9 = C.r9
61 val xmm0 = C.xmm0
62 val xmm1 = C.xmm1
63 val xmm2 = C.xmm2
64 val xmm3 = C.xmm3
65 val xmm4 = C.xmm4
66 val xmm5 = C.xmm5
67 val xmm6 = C.xmm6
68 val xmm7 = C.xmm7)
69
70 fun toGpr r = (wordTy, r)
71 fun toGprs gprs = List.map toGpr gprs
72 fun toFpr r = (64, r)
73 fun toFprs fprs = List.map toFpr fprs
74
75 val calleeSaveRegs = [C.rbx, C.r12, C.r13, C.r14, C.r15]
76 val callerSaveRegs = [C.rax, C.rcx, C.rdx, C.rsi, C.rdi, C.r8, C.r9, C.r10, C.r11]
77 val callerSaveFRegs = (C.Regs CB.FP {from=0, to=15, step=1})
78 val calleeSaveFRegs : T.reg list = []
79
80 val frameAlignB = 16
81
82 val calleeSaveRegs' = toGprs calleeSaveRegs
83 val callerSaveRegs' = toGprs callerSaveRegs
84 val calleeSaveFRegs' = toFprs calleeSaveFRegs
85 val callerSaveFRegs' = toFprs callerSaveFRegs
86
87 (* convert a list of C types to a list of eight bytes *)
88 fun eightBytesOfCTys ([], [], ebs) = List.rev (List.map List.rev ebs)
89 | eightBytesOfCTys ([], eb, ebs) = List.rev (List.map List.rev (eb :: ebs))
90 | eightBytesOfCTys (cTy :: cTys, eb, ebs) = let
91 val szTy = szBOfCTy cTy
92 val szEb = sum(List.map szBOfCTy eb)
93 in
94 if szTy + szEb = 8
95 then eightBytesOfCTys(cTys, [], (cTy :: eb) :: ebs)
96 else if szTy + szEb < 8
97 then eightBytesOfCTys(cTys, cTy :: eb, ebs)
98 else eightBytesOfCTys(cTys, [cTy], eb :: ebs)
99 end
100
101 (* convert a C type into its eight bytes *)
102 fun eightBytesOfCTy cTy = eightBytesOfCTys (CType.flattenCTy cTy, [], [])
103
104 (* classify a C type into its location kind (assuming that aggregates cannot be passed in registers) *)
105 fun kindOfCTy (CTy.C_float | CTy.C_double | CTy.C_long_double) = FPR
106 | kindOfCTy (CTy.C_ARRAY _ | CTy.C_STRUCT _ | CTy.C_UNION _) = raise Fail "impossible"
107 | kindOfCTy (CTy.C_unsigned _ | CTy.C_signed _ | CTy.C_PTR) = GPR
108
109 fun combineKinds (k1, k2) = if (k1 = k2)
110 then k1
111 else (case (k1, k2)
112 of (STK, _) => STK
113 | (_, STK) => STK
114 | (GPR, _) => GPR
115 | (_, GPR) => GPR
116 | _ => FPR
117 (* end case*))
118
119 (* this part of the ABI is tricky. if the eightbyte contains all floats, we use fprs, but
120 * otherwise we use gprs. *)
121 fun kindOfEightByte [] = raise Fail "impossible"
122 | kindOfEightByte [cTy] = kindOfCTy cTy
123 | kindOfEightByte (cTy1 :: cTy2 :: cTys) = let
124 val k1 = combineKinds (kindOfCTy cTy1, kindOfCTy cTy2)
125 val k2 = kindOfEightByte(cTy2 :: cTys)
126 in
127 combineKinds(k1, k2)
128 end
129
130 fun containsUnalignedFields cTy = (case cTy
131 of (CTy.C_STRUCT cTys | CTy.C_UNION cTys) => List.exists containsUnalignedFields cTys
132 | cTy => Int.max(8, szBOfCTy cTy) mod 8 <> 0
133 (* end case *))
134
135 fun reqsOfCTy (cTy as (CTy.C_STRUCT _ | CTy.C_UNION _ | CTy.C_ARRAY _)) =
136 if (szBOfCTy cTy > 2*8 orelse containsUnalignedFields cTy)
137 then List.tabulate (szBOfCTy cTy div 8, fn _ => (8*8, STK, 8))
138 else List.map (fn eb => (8*8, kindOfEightByte eb, 8)) (eightBytesOfCTy cTy)
139 | reqsOfCTy cTy = [(8*szBOfCTy cTy, kindOfCTy cTy, alignBOfCTy cTy)]
140
141 fun reqOfCTy cTy = (case reqsOfCTy cTy
142 of [req] => req
143 | _ => raise Fail "malformed C type"
144 (* end case *))
145
146 (* given a return type, return the locations for the return values *)
147 fun layoutReturn retTy = (case retTy
148 of CTy.C_void => ([], NONE, CCs.store0)
149 | retTy as CTy.C_STRUCT _ => raise Fail "todo"
150 | retTy => let
151 val (locs, store) = SA.allocateSeq CCs.returns (reqsOfCTy retTy, CCs.store0)
152 in
153 (locs, NONE, store)
154 end
155 (* end case *))
156
157 (* given a store and some parameters, return the C locations for those parameters *)
158 fun layoutCall (store, paramTys) = let
159 val paramReqs = List.map reqsOfCTy paramTys
160 in
161 SA.allocateSeqs CCs.params (paramReqs, store)
162 end
163
164 fun layout {conv, retTy, paramTys} = let
165 val (resLocs, structRetLoc, store) = layoutReturn retTy
166 val (paramLocss, store) = layoutCall(store, paramTys)
167 (* number of bytes allocated for the call *)
168 val frameSzB = SA.find(store, CCs.cCallStk)
169 val argMem = {szb=CSizes.alignAddr(frameSzB, frameAlignB), align=frameAlignB}
170 in
171 {argLocs=paramLocss, argMem=argMem, structRetLoc=structRetLoc, resLocs=resLocs}
172 end
173
174 fun genCall {name, proto, paramAlloc, structRet, saveRestoreDedicated, callComment, args} = let
175 val {argLocs, argMem, resLocs, structRetLoc} = layout proto
176 val argAlloc = if (#szb argMem = 0 orelse paramAlloc argMem)
177 then []
178 else [T.MV (wordTy, C.rsp, T.SUB (wordTy, spReg,
179 T.LI (T.I.fromInt (wordTy, #szb argMem))))]
180 val (copyArgs, gprUses, fprUses) = Gen.writeLocs(args, argLocs)
181 (* the defined registers of the call depend on the calling convention *)
182 val defs = (case #conv proto
183 of "ccall" => List.map (gpr o #2) callerSaveRegs' @ List.map fpr callerSaveFRegs'
184 | "ccall-bare" => []
185 | conv => raise Fail (concat [
186 "unknown calling convention \"", String.toString conv, "\""
187 ])
188 (* end case *))
189 val uses = List.map gpr gprUses @ List.map fpr fprUses
190 val callStm = T.CALL {funct=name, targets=[], defs=defs, uses=uses, region=mem, pops=0}
191 val (resultRegs, copyResult) = Gen.readLocs resLocs
192 val callSeq = argAlloc @ copyArgs @ [callStm] @ copyResult
193 in
194 {callseq=callSeq, result=resultRegs}
195 end
196
197 end (* X86_64SVIDFn *)