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

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