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

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