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

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