Debian Codegraph @

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

raw
1 (* x86-svid-fn.sml
2 *
3 * C calling-sequence generator for x86.
4 *
5 * Mike Rainey ([email protected])
6 *)
7
8
9 functor X86SVIDFn (
10 structure T : MLTREE
11 val abi : string
12 val ix : (T.stm,T.rexp,T.fexp,T.ccexp) X86InstrExt.sext -> T.sext
13 (* Note that the fast_loating_point flag must match the one passed
14 * to the code generator module.
15 *)
16 val fast_floating_point : bool ref
17 ) : C_CALL =
18 struct
19
20 structure T = T
21 structure C = X86Cells
22 structure CB = CellsBasis
23 structure CTy = CType
24 structure IX = X86InstrExt
25
26 val wordTy = 32
27 fun gpr r = T.GPR(T.REG(32, r))
28 fun fpr f = T.FPR(T.FREG(80, f))
29 val spReg = T.REG (32, C.esp)
30
31 (* the C calling convention requires that the FP stack be empty on function
32 * entry. We add the fpStk list to the defs when the fast_floating_point flag
33 * is set.
34 *)
35 val st0 = C.ST 0
36
37 datatype loc_kind = datatype CLocKind.loc_kind
38
39 structure SA = StagedAllocationFn (
40 type reg_id = T.reg
41 datatype loc_kind = datatype loc_kind
42 val memSize = 4)
43
44 structure Gen = CCallGenFn(
45 structure T = T
46 structure C = C
47 val wordTy = wordTy
48 fun offSp 0 = spReg
49 | offSp offset = T.ADD (32, spReg, T.LI offset)
50 fun lobits {e, nBits, width} = e
51 fun sx {fromWidth, toWidth, e} = T.SX(toWidth, fromWidth, e)
52 fun f2f {fromWidth, toWidth, e} = e
53 structure SA = SA)
54
55 datatype c_arg = datatype Gen.c_arg
56
57 structure CCs = X86CConventionFn (
58 type reg_id = T.reg
59 val eax = C.eax
60 val edx = C.edx
61 val st0 = st0
62 structure SA = SA
63 )
64
65 val calleeSaveRegs = [C.ebx, C.esi, C.edi]
66 val callerSaveRegs = [C.eax, C.ecx, C.edx]
67 val calleeSaveFRegs = []
68 val callerSaveFRegs = []
69
70 (* assign a C type to a kind of machine location *)
71 fun kindOfCTy (CTy.C_float | CTy.C_double | CTy.C_long_double) = FPR
72 | kindOfCTy (CTy.C_unsigned _ | CTy.C_signed _ | CTy.C_PTR | CTy.C_ARRAY _) = GPR
73
74 (* convert a C type to reqs for staged allocation *)
75 fun cTyToReqs cTy = let
76 val {sz, align} = X86CSizes.sizeOfTy cTy
77 (* compute argument reqs for the flattened C type *)
78 val reqs = List.map (fn cTy => (sz * 8, kindOfCTy cTy, align))
79 (CTy.flattenCTy cTy)
80 in
81 case (cTy, abi)
82 of (CTy.C_STRUCT _, "Darwin") =>
83 (* for Darwin, structs <= 8 bytes are returned in GPRs *)
84 if (sz <= 4)
85 then [(8, GPR, align)]
86 else if (sz <= 8)
87 then [(8, GPR, align), (8, GPR, align)]
88 else reqs
89 | ( (CTy.C_unsigned CTy.I_long_long |
90 CTy.C_signed CTy.I_long_long ),
91 _ ) =>
92 (* 64-bit integers are returned in GPRs *)
93 [(8, GPR, align), (8, GPR, align)]
94 | _ => reqs
95 end
96
97 (* compute the parameter passing and return for a given C call *)
98 fun layout {conv, retTy, paramTys} = let
99 (* lay out the return parameters *)
100 val (resLocs, structRetLoc, store) = (case retTy
101 of CTy.C_void => ([], NONE, CCs.store0)
102 | retTy as CTy.C_STRUCT _ => raise Fail ""
103 | retTy => let
104 val {sz, align} = X86CSizes.sizeOfTy retTy
105 val (locs, store) = SA.allocateSeq CCs.returns (cTyToReqs retTy, CCs.store0)
106 in
107 (locs, NONE, store)
108 end
109 (* end case *))
110
111 (* lay out the parameters *)
112 val paramReqss = List.map cTyToReqs paramTys
113 val (paramLocss, store) = SA.allocateSeqs CCs.params (paramReqss, store)
114
115 (* number of bytes allocated for the call *)
116 val cStkSzB = let
117 val n = SA.find(store, CCs.cStack)
118 in
119 if (abi = "Mac OS X")
120 then X86CSizes.alignAddr(n, 16)
121 else n
122 end
123 in
124 {argLocs=paramLocss, argMem={szb=cStkSzB, align=4}, structRetLoc=structRetLoc, resLocs=resLocs}
125 end (* layout *)
126
127 val callerSaveRegs' = List.map gpr calleeSaveRegs
128 val calleeSaveRegs' = List.map gpr calleeSaveRegs
129 val calleeSaveFRegs' = []
130 val callerSaveFRegs' = []
131
132 (* the C calling convention requires that the FP stack be empty on function
133 * entry. We add the fpStk list to the defs when the fast_floating_point flag
134 * is set.
135 *)
136 val fpStk = List.tabulate(8, fn i => fpr (C.ST i))
137
138
139 (* List of registers defined by a C Call with the given return type; this list
140 * is the result registers plus the caller-save registers.
141 *)
142 fun definedRegs resTy = if !fast_floating_point
143 then let
144 val defs = callerSaveRegs' @ fpStk
145 in
146 case resTy
147 of (CTy.C_unsigned(CTy.I_long_long)) => gpr C.edx :: defs
148 | (CTy.C_signed(CTy.I_long_long)) => gpr C.edx :: defs
149 | _ => defs
150 (* end case *)
151 end
152 else (case resTy
153 of (CTy.C_float) => fpr st0 :: callerSaveRegs'
154 | (CTy.C_double) => fpr st0 :: callerSaveRegs'
155 | (CTy.C_long_double) => fpr st0 :: callerSaveRegs'
156 | (CTy.C_unsigned(CTy.I_long_long)) => gpr C.edx :: callerSaveRegs'
157 | (CTy.C_signed(CTy.I_long_long)) => gpr C.edx :: callerSaveRegs'
158 | _ => callerSaveRegs'
159 (* end case *))
160
161 fun fstp (32, f) = T.EXT(ix(IX.FSTPS(f)))
162 | fstp (64, f) = T.EXT(ix(IX.FSTPL(f)))
163 | fstp (80, f) = T.EXT(ix(IX.FSTPT(f)))
164 | fstp (sz, f) = raise Fail ("fstp(" ^ Int.toString sz ^ ",_)")
165
166 (* This annotation is used to indicate that a call returns a fp value
167 * in %st(0)
168 *)
169 val fpReturnValueInST0 = #create MLRiscAnnotations.RETURN_ARG C.ST0
170
171 fun genCall {
172 name, proto, paramAlloc, structRet, saveRestoreDedicated, callComment, args
173 } = let
174 val {argLocs, argMem, structRetLoc, resLocs} = layout proto
175
176 (* for functions that return a struct/union, pass the location as an
177 * implicit first argument. Because the callee removes this implicit
178 * argument from the stack, we must also keep track of the size of the
179 * explicit arguments.
180 *)
181 val (args, argLocs, explicitArgSzB) = (case structRetLoc
182 of SOME pos =>
183 (ARG(structRet pos)::args, [SA.BLOCK_OFFSET(wordTy, GPR, 0)]::argLocs, #szb argMem)
184 | NONE => (args, argLocs, #szb argMem)
185 (* end case *))
186
187 (* instruction to allocate space for arguments *)
188 val argAlloc = if (#szb argMem = 0 orelse paramAlloc argMem)
189 then []
190 else if abi = "Darwin"
191 then let
192 (* align the frame on a 16-byte boundary *)
193 val szb = X86CSizes.alignAddr(#szb argMem + 2*4, 16)-2*4
194 in
195 [T.MV(wordTy, C.esp, T.SUB(wordTy, spReg, T.LI(IntInf.fromInt szb)))]
196 end
197 else [T.MV(wordTy, C.esp, T.SUB(wordTy, spReg, T.LI(IntInf.fromInt(#szb argMem))))]
198 val (copyArgs, gprUses, fprUses) = Gen.writeLocs(args, argLocs)
199
200 (* the SVID specifies that the caller pops arguments, but the callee
201 * pops the arguments in a stdcall on Windows. I'm not sure what other
202 * differences there might be between the SVID and Windows ABIs. (JHR)
203 *)
204 val calleePops = (case #conv proto
205 of (""|"ccall") => false
206 | "stdcall" => true
207 | conv => raise Fail (concat [
208 "unknown calling convention \"", String.toString conv, "\""
209 ])
210 (* end case *))
211
212 (* code to pop the arguments from the stack *)
213 val popArgs = if calleePops orelse (explicitArgSzB = 0)
214 then []
215 else [T.MV(wordTy, C.esp, T.ADD(wordTy, spReg, T.LI(IntInf.fromInt explicitArgSzB)))]
216
217 (* FIXME: support fast floating point *)
218 (* read return values *)
219 val (resultRegs, copyResult) = Gen.readLocs resLocs
220
221 val defs = definedRegs(#retTy proto)
222 val { save, restore } = saveRestoreDedicated defs
223
224 val callStm = T.CALL{
225 funct=name, targets=[], defs=defs, uses=[],
226 region = T.Region.memory,
227 pops = if calleePops
228 then Int32.fromInt(#szb argMem)
229 else Int32.fromInt(#szb argMem - explicitArgSzB)
230 }
231 val callStm = (case callComment
232 of NONE => callStm
233 | SOME c => T.ANNOTATION (callStm, #create MLRiscAnnotations.COMMENT c)
234 (* end case *))
235
236 val callStm = if !fast_floating_point
237 andalso ((#retTy proto = CTy.C_float)
238 orelse (#retTy proto = CTy.C_double)
239 orelse (#retTy proto = CTy.C_long_double))
240 then T.ANNOTATION(callStm, fpReturnValueInST0)
241 else callStm
242
243 (* assemble the call sequence *)
244 val callSeq = argAlloc @ copyArgs @ save @ [callStm] @ restore @ popArgs @ copyResult
245
246 in
247 {callseq=callSeq, result=resultRegs}
248 end
249
250 end (* X86SVIDFn *)
251