Debian Codegraph @

lib/mlrisc-lib/MLRISC/ppc/c-calls/ppc-macosx.sml

raw
1 (* ppc-macosx.sml
2 *
3 * COPYRIGHT (c) 2003 John Reppy (http://www.cs.uchicago.edu/~jhr)
4 * All rights reserved.
5 *
6 * C function calls for the PowerPC using the MacOS X ABI.
7 *
8 * Register conventions:
9 *
10 * Register Callee-save Purpose
11 * -------- ----------- -------
12 * GPR0 no Zero
13 * 1 no Stack pointer
14 * 2 no scratch (TOC on AIX)
15 * 3 no arg0 and return result
16 * 4-10 no arg1-arg7
17 * 11 no scratch
18 * 12 no holds taget of indirect call
19 * 13-31 yes callee-save registers
20 *
21 * FPR0 no scratch
22 * 1-13 no floating-point arguments
23 * 14-31 yes floating-point callee-save registers
24 *
25 * V0-V1 no scratch vector registers
26 * 2-13 no vector argument registers
27 * 14-19 no scratch vector registers
28 * 20-31 yes callee-save vector registers
29 *
30 * LR no link register holds return address
31 *
32 * CR0-CR1 no scratch condition registers
33 * 2-4 yes callee-save condition registers
34 * 5-7 no scratch condition registers
35 *
36 * Calling convention:
37 *
38 * Return result:
39 * + Integer and pointer results are returned in GPR3
40 * + 64-bit integers (long long) returned in GPR3/GPR4
41 * + float/double results are returned in FPR1
42 * + Struct results are returned in space provided by the caller.
43 * The address of this space is passed to the callee as an
44 * implicit first argument in GPR3 and the first real argument is
45 * passed in GPR4.
46 *
47 * Function arguments:
48 * * arguments (except for floating-point values) are passed in
49 * registers GPR3-GPR10
50 *
51 * Note also that stack frames are supposed to be 16-byte aligned.
52 *)
53
54 (* we extend the interface to support generating the stubs needed for
55 * dynamic linking (see "Inside MacOS X: Mach-O Runtime Architecture"
56 * for details.
57 *)
58 signature PPC_MACOSX_C_CALLS =
59 sig
60 include C_CALLS
61
62 (*
63 val genStub : {
64 name : T.rexp,
65 proto : CTypes.c_proto,
66 paramAlloc : {szb : int, align : int} -> bool,
67 structRet : {szb : int, align : int} -> T.rexp,
68 saveRestoreDedicated :
69 T.mlrisc list -> {save: T.stm list, restore: T.stm list},
70 callComment : string option,
71 args : c_arg list
72 } -> {
73 callseq : T.stm list,
74 result: T.mlrisc list
75 }
76 *)
77
78 end;
79
80 functor PPCMacOSX_CCalls (
81
82 structure T : MLTREE
83
84 ): C_CALLS = struct
85
86 structure T = T
87 structure CTy = CTypes
88 structure C = PPCCells
89
90 fun error msg = MLRiscErrorMsg.error ("PPCCompCCalls", msg)
91
92 (* the location of arguments/parameters; offsets are given with respect to the
93 * low end of the parameter area.
94 *)
95 datatype arg_location
96 = Reg of T.ty * T.reg * T.I.machine_int option
97 (* integer/pointer argument in register *)
98 | FReg of T.fty * T.reg * T.I.machine_int option
99 (* floating-point argument in register *)
100 | Stk of T.ty * T.I.machine_int (* integer/pointer argument in parameter area *)
101 | FStk of T.fty * T.I.machine_int (* floating-point argument in parameter area *)
102 | Args of arg_location list
103
104 val wordTy = 32
105 val fltTy = 32 (* MLRISC type of float *)
106 val dblTy = 64 (* MLRISC type of double *)
107
108 (* shorts and chars are promoted to 32-bits *)
109 val naturalIntSz = wordTy
110
111 (* stack pointer *)
112 val spReg = T.REG(wordTy, C.GPReg 1)
113
114 (* registers used for parameter passing *)
115 val argGPRs = List.map C.GPReg [3, 4, 5, 6, 7, 8, 9, 10]
116 val argFPRs = List.map C.FPReg [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
117 val resGPR = C.GPReg 3
118 val resGPR2 = C.GPReg 4
119 val resRegLoc = Reg(wordTy, resGPR, NONE)
120 val resRegLoc2 = Reg(wordTy, resGPR2, NONE)
121 val resRegLocPair = Args[resRegLoc, resRegLoc2]
122 val resFPR = C.FPReg 1
123
124 (* C callee-save registers *)
125 val calleeSaveRegs = List.map C.GPReg [
126 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
127 23, 24, 25, 26, 27, 28, 29, 30, 31
128 ]
129 val calleeSaveFRegs = List.map C.FPReg [
130 14, 15, 16, 17, 18, 19, 20, 21, 22,
131 23, 24, 25, 26, 27, 28, 29, 30, 31
132 ]
133
134 (* C caller-save registers (including argument registers) *)
135 val callerSaveRegs =
136 T.FPR(T.FREG(dblTy, C.FPReg 0)) ::
137 (List.map (fn r => T.GPR(T.REG(wordTy, C.GPReg r))) [2, 11, 12])
138
139 val linkReg = T.GPR(T.REG(wordTy, C.lr))
140
141 (* the parameter area lies just above the linkage area in the caller's frame.
142 * The linkage area is 24 bytes, so the first parameter is at 24(sp).
143 *)
144 val paramAreaOffset = 24
145
146 (* size, padding, and natural alignment for integer types. Note that the
147 * padding is based on the parameter-passing description on p. 35 of the
148 * documentation and the alignment is from p. 31.
149 *)
150 fun sizeOfInt CTy.I_char = {sz = 1, pad = 3, align = 1}
151 | sizeOfInt CTy.I_short = {sz = 2, pad = 2, align = 2}
152 | sizeOfInt CTy.I_int = {sz = 4, pad = 0, align = 4}
153 | sizeOfInt CTy.I_long = {sz = 4, pad = 0, align = 4}
154 | sizeOfInt CTy.I_long_long = {sz = 8, pad = 0, align = 8}
155
156 (* sizes of other C types *)
157 val sizeOfPtr = {sz = 4, pad = 0, align = 4}
158
159 (* align the address to the given alignment, which must be a power of 2 *)
160 fun alignAddr (addr, align) = let
161 val mask = Word.fromInt(align-1)
162 in
163 Word.toIntX(Word.andb(Word.fromInt addr + mask, Word.notb mask))
164 end
165
166 (* compute the size and alignment information for a struct; tys is the list
167 * of member types. The alignment is what Apple calls the "embedding" alignment.
168 * The total size is padded to agree with the struct's alignment.
169 *)
170 fun sizeOfStruct tys = let
171 fun ssz [] = {sz = 0, align = 1}
172 | ssz (first::rest) = let
173 fun f ([], maxAlign, offset) =
174 {sz = alignAddr(offset, maxAlign), align = maxAlign}
175 | f (ty::tys, maxAlign, offset) = let
176 val {sz, align} = sizeOfTy ty
177 val align = Int.min(align, 4)
178 val offset = alignAddr(offset, align)
179 in
180 f (tys, Int.max(maxAlign, align), offset+sz)
181 end
182 val {sz, align} = sizeOfTy first
183 in
184 f (rest, align, sz)
185 end
186 in
187 ssz tys
188 end
189
190 (* the size alignment of a union type is the maximum of the sizes and alignments of the
191 * members. The final size is padded to agree with the alignment.
192 *)
193 and sizeOfUnion tys = let
194 fun usz [] = {sz = 0, align = 1}
195 | usz (first::rest) = let
196 fun f ([], maxAlign, maxSz) =
197 {sz = alignAddr(maxSz, maxAlign), align = maxAlign}
198 | f (ty::tys, maxAlign, maxSz) = let
199 val {sz, align} = sizeOfTy ty
200 in
201 f (tys, Int.max(maxAlign, align), Int.max(align, maxAlign))
202 end
203 val {sz, align} = sizeOfTy first
204 in
205 f (rest, align, sz)
206 end
207 in
208 usz tys
209 end
210
211 and sizeOfTy CTy.C_void = error "unexpected void argument type"
212 | sizeOfTy CTy.C_float = {sz = 4, align = 4}
213 | sizeOfTy CTy.C_double = {sz = 8, align = 8}
214 | sizeOfTy CTy.C_long_double = {sz = 8, align = 8}
215 | sizeOfTy (CTy.C_unsigned isz) = let
216 val {sz, align, ...} = sizeOfInt isz
217 in
218 {sz = sz, align = align}
219 end
220 | sizeOfTy (CTy.C_signed isz) = let
221 val {sz, align, ...} = sizeOfInt isz
222 in
223 {sz = sz, align = align}
224 end
225 | sizeOfTy CTy.C_PTR = {sz = 4, align = 4}
226 | sizeOfTy (CTy.C_ARRAY(ty, n)) = let
227 val {sz, align} = sizeOfTy ty
228 in
229 {sz = n*sz, align = align}
230 end
231 | sizeOfTy (CTy.C_STRUCT tys) = sizeOfStruct tys
232 | sizeOfTy (CTy.C_UNION tys) = sizeOfUnion tys
233
234 (* compute the layout of a C call's arguments *)
235 fun layout {conv, retTy, paramTys} = let
236 fun gprRes isz = (case #sz(sizeOfInt isz)
237 of 8 => SOME resRegLocPair
238 | _ => SOME resRegLoc
239 (* end case *))
240 val (resLoc, argGPRs, structRet) = (case retTy
241 of CTy.C_void => (NONE, argGPRs, NONE)
242 | CTy.C_float => (SOME(FReg(fltTy, resFPR, NONE)), argGPRs, NONE)
243 | CTy.C_double => (SOME(FReg(dblTy, resFPR, NONE)), argGPRs, NONE)
244 | CTy.C_long_double => (SOME(FReg(dblTy, resFPR, NONE)), argGPRs, NONE)
245 | CTy.C_unsigned isz => (gprRes isz, argGPRs, NONE)
246 | CTy.C_signed isz => (gprRes isz, argGPRs, NONE)
247 | CTy.C_PTR => (SOME resRegLoc, argGPRs, NONE)
248 | CTy.C_ARRAY _ => error "array return type"
249 | CTy.C_STRUCT s => let
250 val sz = #sz(sizeOfStruct s)
251 in
252 (* Note that this is a place where the MacOS X and Linux ABIs differ.
253 * In Linux, GPR3/GPR4 are used to return composite values of 8 bytes.
254 *)
255 (SOME resRegLoc, List.tl argGPRs, SOME{szb=sz, align=4})
256 end
257 | CTy.C_UNION u => let
258 val sz = #sz(sizeOfUnion u)
259 in
260 (SOME resRegLoc, List.tl argGPRs, SOME{szb=sz, align=4})
261 end
262 (* end case *))
263 fun assign ([], offset, _, _, layout) = (offset, List.rev layout)
264 | assign (ty::tys, offset, availGPRs, availFPRs, layout) = (
265 case ty
266 of CTy.C_void => error "unexpected void argument type"
267 | CTy.C_float => (case (availGPRs, availFPRs)
268 of (_::gprs, fpr::fprs) =>
269 assign (tys, offset+4, gprs, fprs, FReg(fltTy, fpr, SOME offset)::layout)
270 | ([], fpr::fprs) =>
271 assign (tys, offset+4, [], fprs, FReg(fltTy, fpr, SOME offset)::layout)
272 | ([], []) =>
273 assign (tys, offset+4, [], [], FStk(fltTy, offset)::layout)
274 | _ => error "FPRs exhausted before GPRs"
275 (* end case *))
276 | CTy.C_double =>
277 assignFPR (tys, offset, availGPRs, availFPRs, layout)
278 | CTy.C_long_double =>
279 assignFPR (tys, offset, availGPRs, availFPRs, layout)
280 | (CTy.C_unsigned isz | CTy.C_signed isz) =>
281 assignGPR([sizeOfInt isz], tys, offset, availGPRs, availFPRs, layout)
282 | CTy.C_PTR =>
283 assignGPR([sizeOfPtr], tys, offset, availGPRs, availFPRs, layout)
284 | CTy.C_ARRAY _ =>
285 assignGPR([sizeOfPtr], tys, offset, availGPRs, availFPRs, layout)
286 | CTy.C_STRUCT tys' =>
287 assignMem(sizeOfStruct tys', tys, offset, availGPRs, availFPRs, layout)
288 | CTy.C_UNION tys' =>
289 assignMem(sizeOfUnion tys', tys, offset, availGPRs, availFPRs, layout)
290 (* end case *))
291 (* assign a GP register and memory for an integer/pointer argument. *)
292 and assignGPR ([], args, offset, availGPRs, availFPRs, layout) =
293 assign (args, offset, availGPRs, availFPRs, layout)
294 | assignGPR ({ sz = 8, ... } :: szs,
295 args, offset, availGPRs, availFPRs, layout) =
296 (* The C compiler seems to treat "long long" arguments
297 * as two individual 4-byte arguments. There seems to be
298 * no 8-byte alignment requirement, as far as I can tell.
299 * - Matthias *)
300 assignGPR ({ sz = 4, pad = 0, align = 4 } ::
301 { sz = 4, pad = 0, align = 4 } :: szs,
302 args, offset, availGPRs, availFPRs, layout)
303 | assignGPR ({ sz, pad, ... } :: szs,
304 args, offset, availGPRs, availFPRs, layout) =
305 let val (loc, availGPRs) =
306 case availGPRs
307 of [] => (Stk(wordTy, offset), [])
308 | r1 :: rs => (Reg(wordTy, r1, SOME offset), rs)
309 val offset = offset + IntInf.fromInt (sz + pad)
310 in assignGPR (szs, args, offset, availGPRs, availFPRs, loc :: layout)
311 end
312
313 (* assign a FP register and memory/GPRs for double-precision argument. *)
314 and assignFPR (args, offset, availGPRs, availFPRs, layout) = let
315 fun continue (availGPRs, availFPRs, loc) =
316 assign (args, offset+8, availGPRs, availFPRs, loc::layout)
317 fun freg fpr = FReg(dblTy, fpr, SOME offset)
318 in
319 case (availGPRs, availFPRs)
320 of (_::_::gprs, fpr::fprs) => continue (gprs, fprs, freg fpr)
321 | (_, fpr::fprs) => continue ([], fprs, freg fpr)
322 | ([], []) => continue ([], [], FStk(dblTy, offset))
323 | _ => error "FPRs exhausted before GPRs"
324 (* end case *)
325 end
326 (* assign a argument locations to pass a composite argument (struct or union) *)
327 and assignMem ({sz, ...}, args, offset, availGPRs, availFPRs, layout) = let
328 val sz = IntInf.fromInt sz
329 fun assignMem (relOffset, availGPRs, fields) =
330 if (relOffset < sz)
331 then let
332 val (loc, availGPRs) = (case availGPRs
333 of [] => (Stk(wordTy, offset+relOffset), [])
334 | r1::rs => (Reg(wordTy, r1, SOME(offset+relOffset)), rs)
335 (* end case *))
336 in
337 assignMem (relOffset+4, availGPRs, loc::fields)
338 end
339 else assign (args, offset+relOffset, availGPRs, availFPRs,
340 Args(List.rev fields) :: layout)
341 in
342 assignMem (0, availGPRs, [])
343 end
344 val (sz, argLocs) = assign (paramTys, 0, argGPRs, argFPRs, [])
345 in {
346 argLocs = argLocs,
347 argMem = {szb = IntInf.toInt sz, align = 4},
348 resLoc = resLoc,
349 structRetLoc = structRet
350 } end
351
352 datatype c_arg
353 = ARG of T.rexp
354 | FARG of T.fexp
355 | ARGS of c_arg list
356
357 val memRg = T.Region.memory
358 val stkRg = T.Region.memory
359
360 (* SP-based address of parameter at given offset *)
361 fun paramAddr off =
362 T.ADD(wordTy, spReg, T.LI(off + IntInf.fromInt paramAreaOffset))
363
364 fun genCall {
365 name, proto, paramAlloc, structRet, saveRestoreDedicated,
366 callComment, args
367 } = let
368 val {conv, retTy, paramTys} = proto
369 val {argLocs, argMem, resLoc, structRetLoc} = layout proto
370 (* inform the client of the size of the parameter area *)
371 val _ = if not(paramAlloc argMem)
372 then raise Fail "parameter memory allocation not implemented yet"
373 else ()
374 (* generate code to assign the arguments to their locations *)
375 fun assignArgs ([], [], stms) = stms
376 | assignArgs (Reg(ty, r, _) :: locs, ARG exp :: args, stms) =
377 assignArgs (locs, args, T.MV(ty, r, exp) :: stms)
378 | assignArgs (Stk(ty, off) :: locs, ARG exp :: args, stms) =
379 assignArgs (locs, args, T.STORE(ty, paramAddr off, exp, stkRg) :: stms)
380 | assignArgs (FReg(ty, r, _) :: locs, FARG fexp :: args, stms) =
381 assignArgs (locs, args, T.FMV(ty, r, fexp) :: stms)
382 | assignArgs (FStk(ty, off) :: locs, FARG fexp :: args, stms) =
383 assignArgs (locs, args, T.FSTORE(ty, paramAddr off, fexp, stkRg) :: stms)
384 | assignArgs ((Args locs') :: locs, (ARGS args') :: args, stms) =
385 raise Fail "ARGS constructor is obsolete"
386 | assignArgs ((Args locs') :: locs, ARG exp :: args, stms) = let
387 (* MLRISC expression for address inside the source struct *)
388 fun addr 0 = T.LOAD(wordTy, exp, memRg)
389 | addr offset = T.LOAD(wordTy, T.ADD(wordTy, exp, T.LI offset), memRg)
390 fun copy ([], _, stms) = assignArgs(locs, args, stms)
391 | copy (Reg(ty, r, _) :: locs, offset, stms) =
392 copy (locs, offset+4, T.MV(ty, r, addr offset)::stms)
393 | copy (Stk(ty, off) :: locs, offset, stms) = let
394 val r = C.newReg()
395 in
396 copy (locs, offset+4,
397 T.STORE(ty, paramAddr off, T.REG(wordTy, r), stkRg)
398 :: T.MV(ty, r, addr offset) :: stms)
399 end
400 | copy _ = raise Fail "unexpected FReg/FStk/Args in location list"
401 in
402 (* copy data from memory specified by exp to locs' *)
403 copy (locs', 0, stms)
404 end
405 | assignArgs _ = error "argument/formal mismatch"
406 val argSetupCode = List.rev(assignArgs(argLocs, args, []))
407 (* convert the result location to an MLRISC expression list *)
408 val result = (case resLoc
409 of NONE => []
410 | SOME(Reg(ty, r, _)) => [T.GPR(T.REG(ty, r))]
411 | SOME(FReg(ty, r, _)) => [T.FPR(T.FREG(ty, r))]
412 | SOME(Args[Reg(ty1,r1,_),Reg(ty2,r2,_)]) =>
413 [T.GPR(T.REG(ty1,r1)), T.GPR(T.REG(ty2,r2))]
414 | SOME _ => raise Fail "bogus result location"
415 (* end case *))
416 (* make struct return-area setup (if necessary) *)
417 val setupStructRet = (case structRetLoc
418 of NONE => []
419 | SOME loc => let
420 val structAddr = structRet loc
421 in
422 [T.MV(wordTy, resGPR, structAddr)]
423 end
424 (* end case *))
425 (* determine the registers used and defined by this call *)
426 val (uses, defs) = let
427 val locs = (case resLoc
428 of NONE => argLocs
429 | SOME loc => loc::argLocs
430 (* end case *))
431 (* get the list of registers used to pass arguments and results *)
432 fun addArgReg (Reg(ty, r, _)::locs, argRegs) =
433 addArgReg (locs, T.GPR(T.REG(ty, r))::argRegs)
434 | addArgReg (FReg(ty, r, _)::locs, argRegs) =
435 addArgReg (locs, T.FPR(T.FREG(ty, r))::argRegs)
436 | addArgReg ((Args locs')::locs, argRegs) =
437 addArgReg (locs, addArgReg(locs', argRegs))
438 | addArgReg (_::locs, argRegs) = addArgReg(locs, argRegs)
439 | addArgReg ([], argRegs) = argRegs
440 val argRegs = addArgReg (locs, [])
441 in
442 (argRegs, linkReg :: callerSaveRegs)
443 end
444 (* the actual call instruction *)
445 val callStm = T.CALL {
446 funct = name, targets = [],
447 defs = defs, uses = uses,
448 region = memRg, pops = 0
449 }
450 (* annotate, if necessary *)
451 val callStm = (case callComment
452 of NONE => callStm
453 | SOME c => T.ANNOTATION(callStm, #create MLRiscAnnotations.COMMENT c)
454 (* end case *))
455 (* take care of dedicated client registers *)
456 val {save, restore} = saveRestoreDedicated defs
457 val callseq = List.concat [
458 setupStructRet,
459 argSetupCode,
460 save,
461 [callStm],
462 restore
463 ]
464 in
465 (* check calling convention *)
466 case conv
467 of ("" | "ccall") => ()
468 | _ => error (concat [
469 "unknown calling convention \"",
470 String.toString conv, "\""
471 ])
472 (* end case *);
473 {callseq = callseq, result = result}
474 end
475
476 end