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/sparc/c-calls/sparc-c-calls.sml
ViewVC logotype

Annotation of /sml/trunk/src/MLRISC/sparc/c-calls/sparc-c-calls.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1523 - (view) (download)

1 : blume 840 (* sparc-c-calls.sml
2 :     *
3 :     * COPYRIGHT (c) 2001 Bell Labs, Lucent Technologies
4 :     *
5 :     * author: Matthias Blume (blume@reseach.bell-labs.com)
6 :     *
7 :     * Comment: This is a first cut. It might be quite sub-optimal for some cases.
8 :     * (For example, I make no attempt at using ldd/ldx for
9 :     * copying stuff around because this would require keeping
10 :     * more track of alignment issues.)
11 :     *
12 :     * C function calls for the Sparc
13 :     *
14 :     * Register conventions:
15 :     *
16 :     * ?
17 :     *
18 :     * Calling convention:
19 :     *
20 :     * Return result:
21 :     * + Integer and pointer results are returned in %o0
22 :     * + 64-bit integers (long long) returned in %o1/%o1
23 :     * + float results are returned in %f0; double in %f0/%f1
24 :     * + Struct results are returned in space provided by the caller.
25 :     * The address of this space is passed to the callee as a hidden
26 :     * implicit argument on the stack (in the caller's frame). It
27 :     * gets stored at [%sp+64] (from the caller's point of view).
28 :     * An UNIMP instruction must be placed after the call instruction,
29 :     * indicating how much space has been reserved for the return value.
30 :     * + long double results are returned like structs
31 :     *
32 :     * Function arguments:
33 :     * + Arguments that are smaller than a word are promoted to word-size.
34 :     * + Up to six argument words (words 0-5) are passed in registers
35 :     * %o0...%o5. This includes doubles and long longs. Alignment for
36 :     * those types is NOT maintained, i.e., it is possible for an 8-byte
37 :     * quantity to end up in an odd-even register pair.
38 :     * * Arguments beyond 6 words are passed on the stack in the caller's
39 :     * frame. For this, the caller must reserve space in its frame
40 :     * prior to the call. Argument word 6 appears at [%sp+92], word 7
41 :     * at [%sp+96], ...
42 :     * + struct arguments are passed as pointers to a copy of the struct.
43 :     * The copy itself is allocated by the caller in its stack frame.
44 :     * + long double arguments are passed like structs (i.e., via pointer
45 :     * to temp copy)
46 :     * + Space for argument words 0-5 is already allocated in the
47 :     * caller's frame. This space might be used by the callee to
48 :     * save those arguments that must be addressable. %o0 corresponds
49 :     * to [%sp+68], %o1 to [%sp+72], ...
50 :     *)
51 :     functor Sparc_CCalls
52 :     (structure T : MLTREE
53 :     val ix : (T.stm, T.rexp, T.fexp, T.ccexp) SparcInstrExt.sext
54 :     -> T.sext): C_CALLS =
55 :     struct
56 :     structure T = T
57 :     structure Ty = CTypes
58 :     structure C = SparcCells
59 :     structure IX = SparcInstrExt
60 :    
61 :     fun error msg = MLRiscErrorMsg.error ("SparcCompCCalls", msg)
62 :    
63 :     datatype c_arg =
64 :     ARG of T.rexp
65 :     | FARG of T.fexp
66 :     | ARGS of c_arg list
67 :    
68 :     val mem = T.Region.memory
69 :     val stack = T.Region.memory
70 :    
71 : blume 1045 val maxRegArgs = 6
72 : jhr 1043 val paramAreaOffset = 68
73 :    
74 : blume 840 fun LI i = T.LI (T.I.fromInt (32, i))
75 :    
76 :     val GP = C.GPReg
77 :     val FP = C.FPReg
78 :    
79 :     fun greg r = GP r
80 :     fun oreg r = GP (r + 8)
81 : jhr 1521 fun ireg r = GP (r + 24)
82 : blume 840 fun freg r = FP r
83 :    
84 :     fun reg32 r = T.REG (32, r)
85 :     fun freg64 r = T.FREG (64, r)
86 :    
87 :     val sp = oreg 6
88 :     val spreg = reg32 sp
89 :    
90 :     fun addli (x, 0) = x
91 :     | addli (x, d) = let
92 :     val d' = T.I.fromInt (32, d)
93 :     in
94 :     case x of
95 :     T.ADD (_, r, T.LI d) =>
96 :     T.ADD (32, r, T.LI (T.I.ADD (32, d, d')))
97 :     | _ => T.ADD (32, x, T.LI d')
98 :     end
99 :    
100 : jhr 1043 fun argaddr n = addli (spreg, paramAreaOffset + 4*n)
101 : blume 841
102 : blume 840 (* temp location for transfers through memory *)
103 : blume 841 val tmpaddr = argaddr 1
104 : blume 840
105 :     fun roundup (i, a) = a * ((i + a - 1) div a)
106 :    
107 :     (* calculate size and alignment for a C type *)
108 :     fun szal (Ty.C_void | Ty.C_float | Ty.C_PTR |
109 :     Ty.C_signed (Ty.I_int | Ty.I_long) |
110 :     Ty.C_unsigned (Ty.I_int | Ty.I_long)) = (4, 4)
111 :     | szal (Ty.C_double |
112 :     Ty.C_signed Ty.I_long_long |
113 :     Ty.C_unsigned Ty.I_long_long) = (8, 8)
114 :     | szal (Ty.C_long_double) = (16, 8)
115 :     | szal (Ty.C_signed Ty.I_char | Ty.C_unsigned Ty.I_char) = (1, 1)
116 :     | szal (Ty.C_signed Ty.I_short | Ty.C_unsigned Ty.I_short) = (2, 2)
117 :     | szal (Ty.C_ARRAY (t, n)) = let val (s, a) = szal t in (n * s, a) end
118 :     | szal (Ty.C_STRUCT l) =
119 :     let (* i: next free memory address (relative to struct start);
120 :     * a: current total alignment,
121 :     * l: list of struct member types *)
122 :     fun pack (i, a, []) =
123 :     (* when we are done with all elements, the total size
124 :     * of the struct must be padded out to its own alignment *)
125 :     (roundup (i, a), a)
126 :     | pack (i, a, t :: tl) = let
127 :     val (ts, ta) = szal t (* size and alignment for member *)
128 :     in
129 :     (* member must be aligned according to its own
130 :     * alignment requirement; the next free position
131 :     * is then at "aligned member-address plus member-size";
132 :     * new total alignment is max of current alignment
133 :     * and member alignment (assuming all alignments are
134 :     * powers of 2) *)
135 :     pack (roundup (i, ta) + ts, Int.max (a, ta), tl)
136 :     end
137 :     in
138 :     pack (0, 1, l)
139 :     end
140 :    
141 : jhr 1521 (**** START NEW CODE ****)
142 :    
143 :     (* the location of arguments/parameters; offsets are given with respect to the
144 :     * low end of the parameter area (see paramAreaOffset above).
145 :     *)
146 :     datatype arg_location
147 : jhr 1523 = Reg of T.ty * T.reg * T.I.machine_int option
148 :     (* integer/pointer argument in register *)
149 :     | FReg of T.fty * T.reg * T.I.machine_int option
150 :     (* floating-point argument in register *)
151 : jhr 1521 | Stk of T.ty * T.I.machine_int (* integer/pointer argument in parameter area *)
152 :     | FStk of T.fty * T.I.machine_int (* floating-point argument in parameter area *)
153 :     | Args of arg_location list
154 :    
155 :     fun layout {conv, retTy, paramTys} = let
156 :     in
157 :     raise Fail "layout not implemented yet"
158 :     end
159 :    
160 :     (* C callee-save registers *)
161 :     val calleeSaveRegs = (* %l0-%l7 and %i0-%i7 *)
162 :     List.tabulate (16, fn r => GP(r+16))
163 :     val calleeSaveFRegs = []
164 :    
165 :     (**** END NEW CODE ****)
166 :    
167 : jhr 1043 fun genCall { name, proto, paramAlloc, structRet, saveRestoreDedicated,
168 : blume 840 callComment, args } = let
169 :     val { conv, retTy, paramTys } = proto
170 :     val _ = case conv of
171 :     ("" | "ccall") => ()
172 :     | _ => error (concat ["unknown calling convention \"",
173 :     String.toString conv, "\""])
174 :     val res_szal =
175 :     case retTy of
176 :     (Ty.C_long_double | Ty.C_STRUCT _) => SOME (szal retTy)
177 :     | _ => NONE
178 :    
179 :     val nargwords = let
180 :     fun loop ([], n) = n
181 :     | loop (t :: tl, n) =
182 :     loop (tl, (case t of
183 :     (Ty.C_double | Ty.C_signed Ty.I_long_long |
184 :     Ty.C_unsigned Ty.I_long_long) => 2
185 :     | _ => 1) + n)
186 :     in
187 :     loop (paramTys, 0)
188 :     end
189 :    
190 : blume 1045 val regargwords = Int.min (nargwords, maxRegArgs)
191 :     val stackargwords = Int.max (nargwords, maxRegArgs) - maxRegArgs
192 : blume 840
193 : blume 1049 val stackargsstart = paramAreaOffset + 4 * maxRegArgs
194 : blume 840
195 : blume 1049 val scratchstart = stackargsstart + 4 * stackargwords
196 :    
197 : blume 840 (* Copy struct or part thereof to designated area on the stack.
198 :     * An already properly aligned address (relative to %sp) is
199 :     * in to_off. *)
200 :     fun struct_copy (sz, al, ARG a, t, to_off, cpc) =
201 :     (* Two main cases here:
202 :     * 1. t is C_STRUCT _: in this case "a" computes the address
203 :     * of the struct to be copied.
204 :     * 2. t is some other non-floating type; "a" computes the
205 :     * the corresponding value (i.e., not its address).
206 :     *)
207 :     let fun ldst ty =
208 :     T.STORE (ty, addli (spreg, to_off), a, stack) :: cpc
209 :     in
210 :     case t of
211 :     (Ty.C_void | Ty.C_PTR |
212 :     Ty.C_signed (Ty.I_int | Ty.I_long) |
213 :     Ty.C_unsigned (Ty.I_int | Ty.I_long)) => ldst 32
214 :     | (Ty.C_signed Ty.I_char | Ty.C_unsigned Ty.I_char) => ldst 8
215 :     | (Ty.C_signed Ty.I_short | Ty.C_unsigned Ty.I_short) =>
216 :     ldst 16
217 :     | (Ty.C_signed Ty.I_long_long |
218 :     Ty.C_unsigned Ty.I_long_long) => ldst 64
219 :     | (Ty.C_ARRAY _) =>
220 :     error "ARRAY within gather/scatter struct"
221 :     | (Ty.C_STRUCT _) =>
222 :     (* Here we have to do the equivalent of a "memcpy". *)
223 :     let val from = a (* argument is address of struct *)
224 :     fun cp (ty, incr) = let
225 :     fun load_from from_off =
226 :     T.LOAD (32, addli (from, from_off), mem)
227 :     (* from_off is relative to from,
228 :     * to_off is relative to %sp *)
229 :     fun loop (i, from_off, to_off, cpc) =
230 :     if i <= 0 then cpc
231 :     else loop (i - incr,
232 :     from_off + incr, to_off + incr,
233 :     T.STORE (ty, addli (spreg, to_off),
234 :     load_from from_off,
235 :     stack)
236 :     :: cpc)
237 :     in
238 :     loop (sz, 0, to_off, cpc)
239 :     end
240 :     in
241 :     case al of
242 :     1 => cp (8, 1)
243 :     | 2 => cp (16, 2)
244 :     | _ => (* 4 or more *) cp (32, 4)
245 :     end
246 :     | (Ty.C_float | Ty.C_double | Ty.C_long_double) =>
247 :     error "floating point type does not match ARG"
248 :     end
249 :     | struct_copy (_, _, ARGS args, Ty.C_STRUCT tl, to_off, cpc) =
250 :     (* gather/scatter case *)
251 :     let fun loop ([], [], _, cpc) = cpc
252 :     | loop (t :: tl, a :: al, to_off, cpc) = let
253 :     val (tsz, tal) = szal t
254 :     val to_off' = roundup (to_off, tal)
255 :     val cpc' = struct_copy (tsz, tal, a, t, to_off', cpc)
256 :     in
257 :     loop (tl, al, to_off' + tsz, cpc')
258 :     end
259 :     | loop _ =
260 :     error "number of types does not match number of arguments"
261 :     in
262 :     loop (tl, args, to_off, cpc)
263 :     end
264 :     | struct_copy (_, _, ARGS _, _, _, _) =
265 :     error "gather/scatter for non-struct"
266 :     | struct_copy (sz, al, FARG a, t, to_off, cpc) =
267 :     let fun fldst ty =
268 :     T.FSTORE (ty, addli (spreg, to_off), a, stack) :: cpc
269 :     in
270 :     case t of
271 :     Ty.C_float => fldst 32
272 :     | Ty.C_double => fldst 64
273 :     | Ty.C_long_double => fldst 128
274 :     | _ => error "non-floating-point type does not match FARG"
275 :     end
276 :    
277 :     val (stackdelta, argsetupcode, copycode) = let
278 : blume 1049 fun loop ([], [], _, ss, asc, cpc) =
279 :     (roundup (Int.max (0, ss - stackargsstart), 8), asc, cpc)
280 : blume 840 | loop (t :: tl, a :: al, n, ss, asc, cpc) = let
281 :     fun wordassign a =
282 :     if n < 6 then T.MV (32, oreg n, a)
283 : blume 841 else T.STORE (32, argaddr n, a, stack)
284 : blume 840 fun wordarg (a, cpc, ss) =
285 :     loop (tl, al, n + 1, ss, wordassign a :: asc, cpc)
286 :    
287 :     fun dwordmemarg (addr, region, tmpstore) = let
288 :     fun toreg (n, addr) =
289 :     T.MV (32, oreg n, T.LOAD (32, addr, region))
290 :     fun tomem (n, addr) =
291 :     T.STORE (32,
292 : blume 841 argaddr n,
293 : blume 840 T.LOAD (32, addr, region),
294 :     stack)
295 :     fun toany (n, addr) =
296 :     if n < 6 then toreg (n, addr) else tomem (n, addr)
297 :     in
298 : blume 841 (* if n < 6 andalso n div 2 = 0 then
299 :     * use ldd here once MLRISC gets its usage right
300 :     * else
301 :     * ... *)
302 : blume 840 loop (tl, al, n+2, ss,
303 :     tmpstore @
304 :     toany (n, addr)
305 :     :: toany (n+1, addli (addr, 4))
306 :     :: asc,
307 :     cpc)
308 :     end
309 : blume 841 fun dwordarg mkstore =
310 :     if n > 6 andalso n div 2 = 1 then
311 :     (* 8-byte aligned memory *)
312 :     loop (tl, al, n+2, ss,
313 :     mkstore (argaddr n) :: asc,
314 :     cpc)
315 :     else dwordmemarg (tmpaddr, stack, [mkstore tmpaddr])
316 : blume 840 in
317 :     case (t, a) of
318 :     ((Ty.C_void | Ty.C_PTR | Ty.C_ARRAY _ |
319 :     Ty.C_unsigned (Ty.I_int | Ty.I_long) |
320 :     Ty.C_signed (Ty.I_int | Ty.I_long)),
321 :     ARG a) => wordarg (a, cpc, ss)
322 :     | (Ty.C_signed Ty.I_char, ARG a) =>
323 :     wordarg (T.SX (32, 8, a), cpc, ss)
324 :     | (Ty.C_unsigned Ty.I_char, ARG a) =>
325 :     wordarg (T.ZX (32, 8, a), cpc, ss)
326 :     | (Ty.C_signed Ty.I_short, ARG a) =>
327 :     wordarg (T.SX (32, 16, a), cpc, ss)
328 :     | (Ty.C_unsigned Ty.I_short, ARG a) =>
329 :     wordarg (T.ZX (32, 16, a), cpc, ss)
330 :     | ((Ty.C_signed Ty.I_long_long |
331 :     Ty.C_unsigned Ty.I_long_long), ARG a) =>
332 :     (case a of
333 :     T.LOAD (_, addr, region) =>
334 :     dwordmemarg (addr, region, [])
335 : blume 841 | _ => dwordarg (fn addr =>
336 :     T.STORE (64, addr, a, stack)))
337 : blume 840 | (Ty.C_float, FARG a) =>
338 :     (* we use the stack region reserved for storing
339 :     * %o0-%o5 as temporary storage for transferring
340 :     * floating point values *)
341 :     (case a of
342 :     T.FLOAD (_, addr, region) =>
343 :     wordarg (T.LOAD (32, addr, region), cpc, ss)
344 :     | _ =>
345 :     if n < 6 then let
346 :     val ld = T.MV (32, oreg n,
347 :     T.LOAD (32, tmpaddr, stack))
348 :     val cp = T.FSTORE (32, tmpaddr, a, stack)
349 :     in
350 :     loop (tl, al, n + 1, ss, cp :: ld :: asc, cpc)
351 :     end
352 :     else loop (tl, al, n + 1, ss,
353 : blume 841 T.FSTORE (32, argaddr n, a, stack)
354 :     :: asc,
355 : blume 840 cpc))
356 :     | (Ty.C_double, FARG a) =>
357 : blume 841 (case a of
358 :     T.FLOAD (_, addr, region) =>
359 :     dwordmemarg (addr, region, [])
360 :     | _ => dwordarg (fn addr =>
361 :     T.FSTORE (64, addr, a, stack)))
362 : blume 840 | (Ty.C_long_double, FARG a) => let
363 :     (* Copy 128-bit floating point value (16 bytes)
364 :     * into scratch space (aligned at 8-byte boundary).
365 :     * The address of the scratch copy is then
366 :     * passed as a regular 32-bit argument. *)
367 :     val ss' = roundup (ss, 8)
368 :     val ssaddr = addli (spreg, ss')
369 :     in
370 :     wordarg (ssaddr,
371 :     T.FSTORE (128, ssaddr, a, stack) :: cpc,
372 :     ss' + 16)
373 :     end
374 :     | (t as Ty.C_STRUCT _, a) => let
375 :     (* copy entire struct into scratch space
376 :     * (aligned according to struct's alignment
377 :     * requirements). The address of the scratch
378 :     * copy is then passed as a regular 32-bit
379 :     * argument. *)
380 :     val (sz, al) = szal t
381 :     val ss' = roundup (ss, al)
382 :     val ssaddr = addli (spreg, ss')
383 :     val cpc' = struct_copy (sz, al, a, t, ss', cpc)
384 :     in
385 :     wordarg (ssaddr, cpc', ss' + sz)
386 :     end
387 :     | _ => error "argument/type mismatch"
388 :     end
389 :     | loop _ = error "wrong number of arguments"
390 :     in
391 :     loop (paramTys, args, 0, scratchstart, [], [])
392 :     end
393 :    
394 :     val (defs, uses) = let
395 :     val gp = T.GPR o reg32
396 :     val fp = T.FPR o freg64
397 :     val g_regs = map (gp o greg) [1, 2, 3, 4, 5, 6, 7]
398 :     val a_regs = map (gp o oreg) [0, 1, 2, 3, 4, 5]
399 :     val l_reg = gp (oreg 7)
400 :     val f_regs = map (fp o freg)
401 :     [0, 2, 4, 6, 8, 10, 12, 14,
402 :     16, 18, 20, 22, 24, 26, 28, 30]
403 :     (* a call instruction defines all caller-save registers:
404 :     * - %g1 - %g7
405 :     * - %o0 - %o5 (argument registers)
406 :     * - %o7 (link register)
407 :     * - all fp registers *)
408 :    
409 :     val defs = g_regs @ a_regs @ l_reg :: f_regs
410 :     (* A call instruction "uses" just the argument registers. *)
411 : blume 1045 val uses = List.take (a_regs, regargwords)
412 : blume 840 in
413 :     (defs, uses)
414 :     end
415 :    
416 :     val result =
417 :     case retTy of
418 :     Ty.C_float => [T.FPR (T.FREG (32, FP 0))]
419 :     | Ty.C_double => [T.FPR (T.FREG (64, FP 0))] (* %f0/%f1 *)
420 :     | Ty.C_long_double => []
421 :     | Ty.C_STRUCT _ => []
422 :     | Ty.C_ARRAY _ => error "array return type"
423 :     | (Ty.C_PTR | Ty.C_void |
424 :     Ty.C_signed (Ty.I_int | Ty.I_long) |
425 :     Ty.C_unsigned (Ty.I_int | Ty.I_long)) =>
426 :     [T.GPR (T.REG (32, oreg 0))]
427 :     | (Ty.C_signed Ty.I_char | Ty.C_unsigned Ty.I_char) =>
428 :     [T.GPR (T.REG (8, oreg 0))]
429 :     | (Ty.C_signed Ty.I_short | Ty.C_unsigned Ty.I_short) =>
430 :     [T.GPR (T.REG (16, oreg 0))]
431 :     | (Ty.C_signed Ty.I_long_long | Ty.C_unsigned Ty.I_long_long) =>
432 :     [T.GPR (T.REG (64, oreg 0))]
433 :    
434 :     val { save, restore } = saveRestoreDedicated defs
435 :    
436 :     val (sretsetup, srethandshake) =
437 :     case res_szal of
438 :     NONE => ([], [])
439 :     | SOME (sz, al) => let
440 :     val addr = structRet { szb = sz, align = al }
441 :     in
442 :     ([T.STORE (32, addli (spreg, 64), addr, stack)],
443 :     [T.EXT (ix (IX.UNIMP sz))])
444 :     end
445 :    
446 :     val call = T.CALL { funct = name, targets = [],
447 :     defs = defs, uses = uses,
448 :     region = mem, pops = 0 }
449 :    
450 :     val call =
451 :     case callComment of
452 :     NONE => call
453 :     | SOME c =>
454 :     T.ANNOTATION (call, #create MLRiscAnnotations.COMMENT c)
455 :    
456 : blume 1045 val (sp_sub, sp_add) =
457 :     if stackdelta = 0 then ([], []) else
458 :     if paramAlloc { szb = stackdelta, align = 4 } then ([], [])
459 :     else ([T.MV (32, sp, T.SUB (32, spreg, LI stackdelta))],
460 :     [T.MV (32, sp, addli (spreg, stackdelta))])
461 :    
462 : blume 840 val callseq =
463 : blume 1045 List.concat [sp_sub,
464 : blume 840 copycode,
465 :     argsetupcode,
466 :     sretsetup,
467 :     save,
468 :     [call],
469 :     srethandshake,
470 :     restore,
471 : blume 1045 sp_add]
472 : blume 840
473 :     in
474 :     { callseq = callseq, result = result }
475 :     end
476 :     end

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