Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/MLRISC/ppc/c-calls/ppc-macosx.sml
ViewVC logotype

Annotation of /sml/trunk/src/MLRISC/ppc/c-calls/ppc-macosx.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1445 - (view) (download)

1 : jhr 1346 (* 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 : jhr 1438 *
51 :     * Note also that stack frames are supposed to be 16-byte aligned.
52 : jhr 1346 *)
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 : jhr 1438
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 : jhr 1346 end;
79 :    
80 :     functor PPCMacOSX_CCalls (
81 :    
82 :     structure T : MLTREE
83 :     val ix : (T.stm, T.rexp, T.fexp, T.ccexp) PPCInstrExt.sext -> T.sext
84 :    
85 :     ): PPC_MACOSX_C_CALLS = struct
86 :     structure T = T
87 :     structure Ty = CTypes
88 :     structure C = PPCCells
89 :     structure IX = PPCInstrExt
90 :    
91 :     fun error msg = MLRiscErrorMsg.error ("PPCCompCCalls", msg)
92 :    
93 :     datatype arg_loc
94 :     = GPR of C.cell
95 :     | FPR of C.cell
96 :     | STK
97 :    
98 :     type arg_pos = {
99 :     offset : int, (* stack offset of memory for argument *)
100 :     loc : arg_loc (* location where argument is passed *)
101 :     }
102 :    
103 :     datatype c_arg
104 :     = ARG of T.rexp
105 :     | FARG of T.fexp
106 :     | ARGS of c_arg list
107 :    
108 :     val mem = T.Region.memory
109 :     val stack = T.Region.memory
110 :    
111 :     val maxRegArgs = 6
112 :     val paramAreaOffset = 68
113 :    
114 : jhr 1443 fun LI i = T.LI(T.I.fromInt (32, i))
115 : jhr 1346
116 : jhr 1443 fun reg r = C.GPReg r
117 :     fun freg r = C.FPReg r
118 : jhr 1346
119 : jhr 1443 fun reg32 r = T.REG(32, r)
120 :     fun freg64 r = T.FREG(64, r)
121 : jhr 1346
122 : jhr 1443 (* stack pointer *)
123 :     val sp = reg1
124 :     val spR = reg32 sp
125 : jhr 1346
126 :     fun addli (x, 0) = x
127 :     | addli (x, d) = let
128 : jhr 1438 val d' = T.I.fromInt (32, d)
129 :     in
130 :     case x
131 :     of T.ADD (_, r, T.LI d) =>
132 :     T.ADD (32, r, T.LI (T.I.ADD (32, d, d')))
133 : jhr 1346 | _ => T.ADD (32, x, T.LI d')
134 : jhr 1443 (* end case *)
135 : jhr 1438 end
136 : jhr 1346
137 :     fun argaddr n = addli (spreg, paramAreaOffset + 4*n)
138 :    
139 : jhr 1443 (* layout information for C types; note that stack and struct alignment
140 :     * are different for some types
141 :     *)
142 :     type layout_info = {
143 :     sz : int,
144 :     stkAlign : int,
145 :     structAlign : int
146 :     }
147 : jhr 1346
148 :     fun roundup (i, a) = a * ((i + a - 1) div a)
149 :    
150 : jhr 1443 (* layout information for integer types *)
151 :     local
152 :     fun layout n = {sz = n, stkAlign = n, structAlign = n}
153 : jhr 1438
154 : jhr 1443 fun intSizeAndAlign Ty.I_char = layout 1
155 :     | intSizeAndAlign Ty.I_short = layout 2
156 :     | intSizeAndAlign Ty.I_int = layout 4
157 :     | intSizeAndAlign Ty.I_long = layout 4
158 :     | intSizeAndAlign Ty.I_long_long = {sz = 8, stkAlign = 8, structAlign = 4}
159 :    
160 :     in
161 :    
162 : jhr 1438 (* calculate size and alignment for a C type *)
163 :     fun szal (T.C_unsigned ty) = intSizeAndAlign ty
164 :     | szal (T.C_signed ty) = intSizeAndAlign ty
165 :     | szal Ty.C_void = raise Fail "unexpected void type"
166 : jhr 1443 | szal Ty.C_float = layout 4
167 :     | szal Ty.C_PTR = layout 4
168 :     | szal Ty.C_double = {sz = 8, stkAlign = 8, structAlign = 4}
169 :     | szal (Ty.C_long_double) = {sz = 8, stkAlign = 8, structAlign = 4}
170 :     | szal (Ty.C_ARRAY(t, n)) = let
171 :     val a = szal t
172 :     in
173 :     {sz = n * #sz a, stkAlign = ?, structAlign = #structAlign a}
174 :     end
175 : jhr 1438 | szal (Ty.C_STRUCT l) = let
176 :     (* FIXME: the rules for structs are more complicated (and they also depend
177 :     * on the alignment mode). In Power alignment, 8-byte quantites like
178 :     * long long and double are 4-byte aligned in structs.
179 :     *)
180 :     (* i: next free memory address (relative to struct start);
181 :     * a: current total alignment,
182 :     * l: list of struct member types
183 :     *)
184 :     fun pack (i, a, []) =
185 :     (* when we are done with all elements, the total size
186 :     * of the struct must be padded out to its own alignment
187 :     *)
188 : jhr 1346 (roundup (i, a), a)
189 : jhr 1438 | pack (i, a, t :: tl) = let
190 :     val (ts, ta) = szal t (* size and alignment for member *)
191 : jhr 1346 in
192 : jhr 1438 (* member must be aligned according to its own
193 :     * alignment requirement; the next free position
194 :     * is then at "aligned member-address plus member-size";
195 :     * new total alignment is max of current alignment
196 :     * and member alignment (assuming all alignments are
197 :     * powers of 2) *)
198 :     pack (roundup (i, ta) + ts, Int.max (a, ta), tl)
199 : jhr 1346 end
200 : jhr 1438 in
201 : jhr 1346 pack (0, 1, l)
202 : jhr 1438 end
203 : jhr 1443 end
204 : jhr 1346
205 : jhr 1445 fun assignIntLoc (ty, gprs, offset) = let
206 :     val {sz, alignStk, alignStruct} = szal ty
207 :     val offset = align(offset, alignStk)
208 : jhr 1443 in
209 : jhr 1445 case (sz, gprs)
210 :     of (_, []) => ({offset = offset, loc = ARG(??)}, offset+sz, [])
211 :     | (8, [r]) =>
212 :     | (8, r1::r2::rs) =>
213 :     | (_, r::rs) =>({offset = offset, loc = GPR r}, offset+sz, rs)
214 :     (* end case *)
215 : jhr 1443 end
216 :    
217 : jhr 1445 fun genCall {
218 :     name, proto, paramAlloc, structRet, saveRestoreDedicated,
219 :     callComment, args
220 :     } = let
221 :     val {conv, retTy, paramTys} = proto
222 :     val callseq = List.concat [
223 :     sp_sub,
224 :     copycode,
225 :     argsetupcode,
226 :     sretsetup,
227 :     save,
228 :     [call],
229 :     srethandshake,
230 :     restore,
231 :     sp_add
232 :     ]
233 :     in
234 :     (* check calling convention *)
235 :     case conv
236 :     of ("" | "ccall") => ()
237 :     | _ => error (concat [
238 :     "unknown calling convention \"",
239 :     String.toString conv, "\""
240 :     ])
241 :     (* end case *);
242 :     {callseq = callseq, result = result}
243 :     end
244 : jhr 1346
245 : jhr 1445 (******
246 :     val res_szal = (case retTy
247 :     of (Ty.C_long_double | Ty.C_STRUCT _) => SOME(szal retTy)
248 :     | _ => NONE
249 :    
250 : jhr 1346 val nargwords = let
251 :     fun loop ([], n) = n
252 :     | loop (t :: tl, n) =
253 :     loop (tl, (case t of
254 :     (Ty.C_double | Ty.C_signed Ty.I_long_long |
255 :     Ty.C_unsigned Ty.I_long_long) => 2
256 :     | _ => 1) + n)
257 :     in
258 :     loop (paramTys, 0)
259 :     end
260 :    
261 :     val regargwords = Int.min (nargwords, maxRegArgs)
262 :     val stackargwords = Int.max (nargwords, maxRegArgs) - maxRegArgs
263 :    
264 :     val stackargsstart = paramAreaOffset + 4 * maxRegArgs
265 :    
266 :     val scratchstart = stackargsstart + 4 * stackargwords
267 :    
268 :     (* Copy struct or part thereof to designated area on the stack.
269 :     * An already properly aligned address (relative to %sp) is
270 :     * in to_off. *)
271 :     fun struct_copy (sz, al, ARG a, t, to_off, cpc) =
272 :     (* Two main cases here:
273 :     * 1. t is C_STRUCT _: in this case "a" computes the address
274 :     * of the struct to be copied.
275 :     * 2. t is some other non-floating type; "a" computes the
276 :     * the corresponding value (i.e., not its address).
277 :     *)
278 :     let fun ldst ty =
279 :     T.STORE (ty, addli (spreg, to_off), a, stack) :: cpc
280 :     in
281 :     case t of
282 :     (Ty.C_void | Ty.C_PTR |
283 :     Ty.C_signed (Ty.I_int | Ty.I_long) |
284 :     Ty.C_unsigned (Ty.I_int | Ty.I_long)) => ldst 32
285 :     | (Ty.C_signed Ty.I_char | Ty.C_unsigned Ty.I_char) => ldst 8
286 :     | (Ty.C_signed Ty.I_short | Ty.C_unsigned Ty.I_short) =>
287 :     ldst 16
288 :     | (Ty.C_signed Ty.I_long_long |
289 :     Ty.C_unsigned Ty.I_long_long) => ldst 64
290 :     | (Ty.C_ARRAY _) =>
291 :     error "ARRAY within gather/scatter struct"
292 :     | (Ty.C_STRUCT _) =>
293 :     (* Here we have to do the equivalent of a "memcpy". *)
294 :     let val from = a (* argument is address of struct *)
295 :     fun cp (ty, incr) = let
296 :     fun load_from from_off =
297 :     T.LOAD (32, addli (from, from_off), mem)
298 :     (* from_off is relative to from,
299 :     * to_off is relative to %sp *)
300 :     fun loop (i, from_off, to_off, cpc) =
301 :     if i <= 0 then cpc
302 :     else loop (i - incr,
303 :     from_off + incr, to_off + incr,
304 :     T.STORE (ty, addli (spreg, to_off),
305 :     load_from from_off,
306 :     stack)
307 :     :: cpc)
308 :     in
309 :     loop (sz, 0, to_off, cpc)
310 :     end
311 :     in
312 :     case al of
313 :     1 => cp (8, 1)
314 :     | 2 => cp (16, 2)
315 :     | _ => (* 4 or more *) cp (32, 4)
316 :     end
317 :     | (Ty.C_float | Ty.C_double | Ty.C_long_double) =>
318 :     error "floating point type does not match ARG"
319 :     end
320 :     | struct_copy (_, _, ARGS args, Ty.C_STRUCT tl, to_off, cpc) =
321 :     (* gather/scatter case *)
322 :     let fun loop ([], [], _, cpc) = cpc
323 :     | loop (t :: tl, a :: al, to_off, cpc) = let
324 :     val (tsz, tal) = szal t
325 :     val to_off' = roundup (to_off, tal)
326 :     val cpc' = struct_copy (tsz, tal, a, t, to_off', cpc)
327 :     in
328 :     loop (tl, al, to_off' + tsz, cpc')
329 :     end
330 :     | loop _ =
331 :     error "number of types does not match number of arguments"
332 :     in
333 :     loop (tl, args, to_off, cpc)
334 :     end
335 :     | struct_copy (_, _, ARGS _, _, _, _) =
336 :     error "gather/scatter for non-struct"
337 :     | struct_copy (sz, al, FARG a, t, to_off, cpc) =
338 :     let fun fldst ty =
339 :     T.FSTORE (ty, addli (spreg, to_off), a, stack) :: cpc
340 :     in
341 :     case t of
342 :     Ty.C_float => fldst 32
343 :     | Ty.C_double => fldst 64
344 :     | Ty.C_long_double => fldst 128
345 :     | _ => error "non-floating-point type does not match FARG"
346 :     end
347 :    
348 :     val (stackdelta, argsetupcode, copycode) = let
349 :     fun loop ([], [], _, ss, asc, cpc) =
350 :     (roundup (Int.max (0, ss - stackargsstart), 8), asc, cpc)
351 :     | loop (t :: tl, a :: al, n, ss, asc, cpc) = let
352 :     fun wordassign a =
353 :     if n < 6 then T.MV (32, oreg n, a)
354 :     else T.STORE (32, argaddr n, a, stack)
355 :     fun wordarg (a, cpc, ss) =
356 :     loop (tl, al, n + 1, ss, wordassign a :: asc, cpc)
357 :    
358 :     fun dwordmemarg (addr, region, tmpstore) = let
359 :     fun toreg (n, addr) =
360 :     T.MV (32, oreg n, T.LOAD (32, addr, region))
361 :     fun tomem (n, addr) =
362 :     T.STORE (32,
363 :     argaddr n,
364 :     T.LOAD (32, addr, region),
365 :     stack)
366 :     fun toany (n, addr) =
367 :     if n < 6 then toreg (n, addr) else tomem (n, addr)
368 :     in
369 :     (* if n < 6 andalso n div 2 = 0 then
370 :     * use ldd here once MLRISC gets its usage right
371 :     * else
372 :     * ... *)
373 :     loop (tl, al, n+2, ss,
374 :     tmpstore @
375 :     toany (n, addr)
376 :     :: toany (n+1, addli (addr, 4))
377 :     :: asc,
378 :     cpc)
379 :     end
380 :     fun dwordarg mkstore =
381 :     if n > 6 andalso n div 2 = 1 then
382 :     (* 8-byte aligned memory *)
383 :     loop (tl, al, n+2, ss,
384 :     mkstore (argaddr n) :: asc,
385 :     cpc)
386 :     else dwordmemarg (tmpaddr, stack, [mkstore tmpaddr])
387 :     in
388 :     case (t, a) of
389 :     ((Ty.C_void | Ty.C_PTR | Ty.C_ARRAY _ |
390 :     Ty.C_unsigned (Ty.I_int | Ty.I_long) |
391 :     Ty.C_signed (Ty.I_int | Ty.I_long)),
392 :     ARG a) => wordarg (a, cpc, ss)
393 :     | (Ty.C_signed Ty.I_char, ARG a) =>
394 :     wordarg (T.SX (32, 8, a), cpc, ss)
395 :     | (Ty.C_unsigned Ty.I_char, ARG a) =>
396 :     wordarg (T.ZX (32, 8, a), cpc, ss)
397 :     | (Ty.C_signed Ty.I_short, ARG a) =>
398 :     wordarg (T.SX (32, 16, a), cpc, ss)
399 :     | (Ty.C_unsigned Ty.I_short, ARG a) =>
400 :     wordarg (T.ZX (32, 16, a), cpc, ss)
401 :     | ((Ty.C_signed Ty.I_long_long |
402 :     Ty.C_unsigned Ty.I_long_long), ARG a) =>
403 :     (case a of
404 :     T.LOAD (_, addr, region) =>
405 :     dwordmemarg (addr, region, [])
406 :     | _ => dwordarg (fn addr =>
407 :     T.STORE (64, addr, a, stack)))
408 :     | (Ty.C_float, FARG a) =>
409 :     (* we use the stack region reserved for storing
410 :     * %o0-%o5 as temporary storage for transferring
411 :     * floating point values *)
412 :     (case a of
413 :     T.FLOAD (_, addr, region) =>
414 :     wordarg (T.LOAD (32, addr, region), cpc, ss)
415 :     | _ =>
416 :     if n < 6 then let
417 :     val ld = T.MV (32, oreg n,
418 :     T.LOAD (32, tmpaddr, stack))
419 :     val cp = T.FSTORE (32, tmpaddr, a, stack)
420 :     in
421 :     loop (tl, al, n + 1, ss, cp :: ld :: asc, cpc)
422 :     end
423 :     else loop (tl, al, n + 1, ss,
424 :     T.FSTORE (32, argaddr n, a, stack)
425 :     :: asc,
426 :     cpc))
427 :     | (Ty.C_double, FARG a) =>
428 :     (case a of
429 :     T.FLOAD (_, addr, region) =>
430 :     dwordmemarg (addr, region, [])
431 :     | _ => dwordarg (fn addr =>
432 :     T.FSTORE (64, addr, a, stack)))
433 :     | (Ty.C_long_double, FARG a) => let
434 :     (* Copy 128-bit floating point value (16 bytes)
435 :     * into scratch space (aligned at 8-byte boundary).
436 :     * The address of the scratch copy is then
437 :     * passed as a regular 32-bit argument. *)
438 :     val ss' = roundup (ss, 8)
439 :     val ssaddr = addli (spreg, ss')
440 :     in
441 :     wordarg (ssaddr,
442 :     T.FSTORE (128, ssaddr, a, stack) :: cpc,
443 :     ss' + 16)
444 :     end
445 :     | (t as Ty.C_STRUCT _, a) => let
446 :     (* copy entire struct into scratch space
447 :     * (aligned according to struct's alignment
448 :     * requirements). The address of the scratch
449 :     * copy is then passed as a regular 32-bit
450 :     * argument. *)
451 :     val (sz, al) = szal t
452 :     val ss' = roundup (ss, al)
453 :     val ssaddr = addli (spreg, ss')
454 :     val cpc' = struct_copy (sz, al, a, t, ss', cpc)
455 :     in
456 :     wordarg (ssaddr, cpc', ss' + sz)
457 :     end
458 :     | _ => error "argument/type mismatch"
459 :     end
460 :     | loop _ = error "wrong number of arguments"
461 :     in
462 :     loop (paramTys, args, 0, scratchstart, [], [])
463 :     end
464 :    
465 :     val (defs, uses) = let
466 :     val gp = T.GPR o reg32
467 :     val fp = T.FPR o freg64
468 :     val g_regs = map (gp o greg) [1, 2, 3, 4, 5, 6, 7]
469 :     val a_regs = map (gp o oreg) [0, 1, 2, 3, 4, 5]
470 :     val l_reg = gp (oreg 7)
471 :     val f_regs = map (fp o freg)
472 :     [0, 2, 4, 6, 8, 10, 12, 14,
473 :     16, 18, 20, 22, 24, 26, 28, 30]
474 :     (* a call instruction defines all caller-save registers:
475 :     * - %g1 - %g7
476 :     * - %o0 - %o5 (argument registers)
477 :     * - %o7 (link register)
478 :     * - all fp registers *)
479 :    
480 :     val defs = g_regs @ a_regs @ l_reg :: f_regs
481 :     (* A call instruction "uses" just the argument registers. *)
482 :     val uses = List.take (a_regs, regargwords)
483 :     in
484 :     (defs, uses)
485 :     end
486 :    
487 :     val result =
488 :     case retTy of
489 :     Ty.C_float => [T.FPR (T.FREG (32, FP 0))]
490 :     | Ty.C_double => [T.FPR (T.FREG (64, FP 0))] (* %f0/%f1 *)
491 :     | Ty.C_long_double => []
492 :     | Ty.C_STRUCT _ => []
493 :     | Ty.C_ARRAY _ => error "array return type"
494 :     | (Ty.C_PTR | Ty.C_void |
495 :     Ty.C_signed (Ty.I_int | Ty.I_long) |
496 :     Ty.C_unsigned (Ty.I_int | Ty.I_long)) =>
497 :     [T.GPR (T.REG (32, oreg 0))]
498 :     | (Ty.C_signed Ty.I_char | Ty.C_unsigned Ty.I_char) =>
499 :     [T.GPR (T.REG (8, oreg 0))]
500 :     | (Ty.C_signed Ty.I_short | Ty.C_unsigned Ty.I_short) =>
501 :     [T.GPR (T.REG (16, oreg 0))]
502 :     | (Ty.C_signed Ty.I_long_long | Ty.C_unsigned Ty.I_long_long) =>
503 :     [T.GPR (T.REG (64, oreg 0))]
504 :    
505 :     val { save, restore } = saveRestoreDedicated defs
506 :    
507 :     val (sretsetup, srethandshake) =
508 :     case res_szal of
509 :     NONE => ([], [])
510 :     | SOME (sz, al) => let
511 :     val addr = structRet { szb = sz, align = al }
512 :     in
513 :     ([T.STORE (32, addli (spreg, 64), addr, stack)],
514 :     [T.EXT (ix (IX.UNIMP sz))])
515 :     end
516 :    
517 :     val call = T.CALL { funct = name, targets = [],
518 :     defs = defs, uses = uses,
519 :     region = mem, pops = 0 }
520 :    
521 :     val call =
522 :     case callComment of
523 :     NONE => call
524 :     | SOME c =>
525 :     T.ANNOTATION (call, #create MLRiscAnnotations.COMMENT c)
526 :    
527 :     val (sp_sub, sp_add) =
528 :     if stackdelta = 0 then ([], []) else
529 :     if paramAlloc { szb = stackdelta, align = 4 } then ([], [])
530 :     else ([T.MV (32, sp, T.SUB (32, spreg, LI stackdelta))],
531 :     [T.MV (32, sp, addli (spreg, stackdelta))])
532 :    
533 :     val callseq =
534 :     List.concat [sp_sub,
535 :     copycode,
536 :     argsetupcode,
537 :     sretsetup,
538 :     save,
539 :     [call],
540 :     srethandshake,
541 :     restore,
542 :     sp_add]
543 :    
544 :     in
545 :     { callseq = callseq, result = result }
546 :     end
547 : jhr 1445 *****)
548 :    
549 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0