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

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