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

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