Debian Codegraph @

lib/mlrisc-lib/MLRISC/sparc/c-calls/sparc-c-calls.sml

raw
1 (* sparc-c-calls.sml
2 *
3 * COPYRIGHT (c) 2001 Bell Labs, Lucent Technologies
4 *
5 * author: Matthias Blume ([email protected])
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 val maxRegArgs = 6
72 val paramAreaOffset = 68
73
74 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 ireg r = GP (r + 24)
82 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 fun argaddr n = addli (spreg, paramAreaOffset + 4*n)
101
102 (* temp location for transfers through memory *)
103 val tmpaddr = argaddr 1
104
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 | szal (Ty.C_UNION l) =
141 let (* m: current max size
142 * a: current total alignment *)
143 fun overlay (m, a, []) = (roundup (m, a), a)
144 | overlay (m, a, t :: tl) =
145 let val (ts, ta) = szal t
146 in
147 overlay (Int.max (m, ts), Int.max (a, ta), tl)
148 end
149 in
150 overlay (0, 1, l)
151 end
152
153 (**** START NEW CODE ****)
154
155 (* shorts and chars are promoted to 32-bits *)
156 val naturalIntSz = 32
157
158 (* the location of arguments/parameters; offsets are given with respect to the
159 * low end of the parameter area (see paramAreaOffset above).
160 *)
161 datatype arg_location
162 = Reg of T.ty * T.reg * T.I.machine_int option
163 (* integer/pointer argument in register *)
164 | FReg of T.fty * T.reg * T.I.machine_int option
165 (* floating-point argument in register *)
166 | Stk of T.ty * T.I.machine_int (* integer/pointer argument in parameter area *)
167 | FStk of T.fty * T.I.machine_int (* floating-point argument in parameter area *)
168 | Args of arg_location list
169
170 fun layout {conv, retTy, paramTys} = let
171 in
172 raise Fail "layout not implemented yet"
173 end
174
175 (* C callee-save registers *)
176 val calleeSaveRegs = (* %l0-%l7 and %i0-%i7 *)
177 List.tabulate (16, fn r => GP(r+16))
178 val calleeSaveFRegs = []
179
180 (**** END NEW CODE ****)
181
182 fun genCall { name, proto, paramAlloc, structRet, saveRestoreDedicated,
183 callComment, args } = let
184 val { conv, retTy, paramTys } = proto
185 val _ = case conv of
186 ("" | "ccall") => ()
187 | _ => error (concat ["unknown calling convention \"",
188 String.toString conv, "\""])
189 val res_szal =
190 case retTy of
191 (Ty.C_long_double | Ty.C_STRUCT _ | Ty.C_UNION _) =>
192 SOME (szal retTy)
193 | _ => NONE
194
195 val nargwords = let
196 fun loop ([], n) = n
197 | loop (t :: tl, n) =
198 loop (tl, (case t of
199 (Ty.C_double | Ty.C_signed Ty.I_long_long |
200 Ty.C_unsigned Ty.I_long_long) => 2
201 | _ => 1) + n)
202 in
203 loop (paramTys, 0)
204 end
205
206 val regargwords = Int.min (nargwords, maxRegArgs)
207 val stackargwords = Int.max (nargwords, maxRegArgs) - maxRegArgs
208
209 val stackargsstart = paramAreaOffset + 4 * maxRegArgs
210
211 val scratchstart = stackargsstart + 4 * stackargwords
212
213 (* Copy struct or part thereof to designated area on the stack.
214 * An already properly aligned address (relative to %sp) is
215 * in to_off. *)
216 fun struct_copy (sz, al, ARG a, t, to_off, cpc) =
217 (* Two main cases here:
218 * 1. t is C_STRUCT _ or C_UNION _;
219 * in this case "a" computes the address
220 * of the struct to be copied.
221 * 2. t is some other non-floating type; "a" computes the
222 * the corresponding value (i.e., not its address).
223 *)
224 let fun ldst ty =
225 T.STORE (ty, addli (spreg, to_off), a, stack) :: cpc
226 in
227 case t of
228 (Ty.C_void | Ty.C_PTR |
229 Ty.C_signed (Ty.I_int | Ty.I_long) |
230 Ty.C_unsigned (Ty.I_int | Ty.I_long)) => ldst 32
231 | (Ty.C_signed Ty.I_char | Ty.C_unsigned Ty.I_char) => ldst 8
232 | (Ty.C_signed Ty.I_short | Ty.C_unsigned Ty.I_short) =>
233 ldst 16
234 | (Ty.C_signed Ty.I_long_long |
235 Ty.C_unsigned Ty.I_long_long) => ldst 64
236 | (Ty.C_ARRAY _) =>
237 error "ARRAY within gather/scatter struct"
238 | (Ty.C_STRUCT _ | Ty.C_UNION _) =>
239 (* Here we have to do the equivalent of a "memcpy". *)
240 let val from = a (* argument is address of struct *)
241 fun cp (ty, incr) = let
242 fun load_from from_off =
243 T.LOAD (32, addli (from, from_off), mem)
244 (* from_off is relative to from,
245 * to_off is relative to %sp *)
246 fun loop (i, from_off, to_off, cpc) =
247 if i <= 0 then cpc
248 else loop (i - incr,
249 from_off + incr, to_off + incr,
250 T.STORE (ty, addli (spreg, to_off),
251 load_from from_off,
252 stack)
253 :: cpc)
254 in
255 loop (sz, 0, to_off, cpc)
256 end
257 in
258 case al of
259 1 => cp (8, 1)
260 | 2 => cp (16, 2)
261 | _ => (* 4 or more *) cp (32, 4)
262 end
263 | (Ty.C_float | Ty.C_double | Ty.C_long_double) =>
264 error "floating point type does not match ARG"
265 end
266 (*
267 | struct_copy (_, _, ARGS args, Ty.C_STRUCT tl, to_off, cpc) =
268 (* gather/scatter case *)
269 let fun loop ([], [], _, cpc) = cpc
270 | loop (t :: tl, a :: al, to_off, cpc) = let
271 val (tsz, tal) = szal t
272 val to_off' = roundup (to_off, tal)
273 val cpc' = struct_copy (tsz, tal, a, t, to_off', cpc)
274 in
275 loop (tl, al, to_off' + tsz, cpc')
276 end
277 | loop _ =
278 error "number of types does not match number of arguments"
279 in
280 loop (tl, args, to_off, cpc)
281 end
282 *)
283 | struct_copy (_, _, ARGS _, _, _, _) =
284 error "gather/scatter (ARGS) not supported (obsolete)"
285 | struct_copy (sz, al, FARG a, t, to_off, cpc) =
286 let fun fldst ty =
287 T.FSTORE (ty, addli (spreg, to_off), a, stack) :: cpc
288 in
289 case t of
290 Ty.C_float => fldst 32
291 | Ty.C_double => fldst 64
292 | Ty.C_long_double => fldst 128
293 | _ => error "non-floating-point type does not match FARG"
294 end
295
296 val (stackdelta, argsetupcode, copycode) = let
297 fun loop ([], [], _, ss, asc, cpc) =
298 (roundup (Int.max (0, ss - stackargsstart), 8), asc, cpc)
299 | loop (t :: tl, a :: al, n, ss, asc, cpc) = let
300 fun wordassign a =
301 if n < 6 then T.MV (32, oreg n, a)
302 else T.STORE (32, argaddr n, a, stack)
303 fun wordarg (a, cpc, ss) =
304 loop (tl, al, n + 1, ss, wordassign a :: asc, cpc)
305
306 fun dwordmemarg (addr, region, tmpstore) = let
307 fun toreg (n, addr) =
308 T.MV (32, oreg n, T.LOAD (32, addr, region))
309 fun tomem (n, addr) =
310 T.STORE (32,
311 argaddr n,
312 T.LOAD (32, addr, region),
313 stack)
314 fun toany (n, addr) =
315 if n < 6 then toreg (n, addr) else tomem (n, addr)
316 in
317 (* if n < 6 andalso n div 2 = 0 then
318 * use ldd here once MLRISC gets its usage right
319 * else
320 * ... *)
321 loop (tl, al, n+2, ss,
322 tmpstore @
323 toany (n, addr)
324 :: toany (n+1, addli (addr, 4))
325 :: asc,
326 cpc)
327 end
328 fun dwordarg mkstore =
329 if n > 6 andalso n div 2 = 1 then
330 (* 8-byte aligned memory *)
331 loop (tl, al, n+2, ss,
332 mkstore (argaddr n) :: asc,
333 cpc)
334 else dwordmemarg (tmpaddr, stack, [mkstore tmpaddr])
335 in
336 case (t, a) of
337 ((Ty.C_void | Ty.C_PTR | Ty.C_ARRAY _ |
338 Ty.C_unsigned (Ty.I_int | Ty.I_long) |
339 Ty.C_signed (Ty.I_int | Ty.I_long)),
340 ARG a) => wordarg (a, cpc, ss)
341 | (Ty.C_signed Ty.I_char, ARG a) =>
342 wordarg (T.SX (32, 8, a), cpc, ss)
343 | (Ty.C_unsigned Ty.I_char, ARG a) =>
344 wordarg (T.ZX (32, 8, a), cpc, ss)
345 | (Ty.C_signed Ty.I_short, ARG a) =>
346 wordarg (T.SX (32, 16, a), cpc, ss)
347 | (Ty.C_unsigned Ty.I_short, ARG a) =>
348 wordarg (T.ZX (32, 16, a), cpc, ss)
349 | ((Ty.C_signed Ty.I_long_long |
350 Ty.C_unsigned Ty.I_long_long), ARG a) =>
351 (case a of
352 T.LOAD (_, addr, region) =>
353 dwordmemarg (addr, region, [])
354 | _ => dwordarg (fn addr =>
355 T.STORE (64, addr, a, stack)))
356 | (Ty.C_float, FARG a) =>
357 (* we use the stack region reserved for storing
358 * %o0-%o5 as temporary storage for transferring
359 * floating point values *)
360 (case a of
361 T.FLOAD (_, addr, region) =>
362 wordarg (T.LOAD (32, addr, region), cpc, ss)
363 | _ =>
364 if n < 6 then let
365 val ld = T.MV (32, oreg n,
366 T.LOAD (32, tmpaddr, stack))
367 val cp = T.FSTORE (32, tmpaddr, a, stack)
368 in
369 loop (tl, al, n + 1, ss, cp :: ld :: asc, cpc)
370 end
371 else loop (tl, al, n + 1, ss,
372 T.FSTORE (32, argaddr n, a, stack)
373 :: asc,
374 cpc))
375 | (Ty.C_double, FARG a) =>
376 (case a of
377 T.FLOAD (_, addr, region) =>
378 dwordmemarg (addr, region, [])
379 | _ => dwordarg (fn addr =>
380 T.FSTORE (64, addr, a, stack)))
381 | (Ty.C_long_double, FARG a) => let
382 (* Copy 128-bit floating point value (16 bytes)
383 * into scratch space (aligned at 8-byte boundary).
384 * The address of the scratch copy is then
385 * passed as a regular 32-bit argument. *)
386 val ss' = roundup (ss, 8)
387 val ssaddr = addli (spreg, ss')
388 in
389 wordarg (ssaddr,
390 T.FSTORE (128, ssaddr, a, stack) :: cpc,
391 ss' + 16)
392 end
393 | (t as (Ty.C_STRUCT _ | Ty.C_UNION _), a) => let
394 (* copy entire struct into scratch space
395 * (aligned according to struct's alignment
396 * requirements). The address of the scratch
397 * copy is then passed as a regular 32-bit
398 * argument. *)
399 val (sz, al) = szal t
400 val ss' = roundup (ss, al)
401 val ssaddr = addli (spreg, ss')
402 val cpc' = struct_copy (sz, al, a, t, ss', cpc)
403 in
404 wordarg (ssaddr, cpc', ss' + sz)
405 end
406 | _ => error "argument/type mismatch"
407 end
408 | loop _ = error "wrong number of arguments"
409 in
410 loop (paramTys, args, 0, scratchstart, [], [])
411 end
412
413 val (defs, uses) = let
414 val gp = T.GPR o reg32
415 val fp = T.FPR o freg64
416 val g_regs = map (gp o greg) [1, 2, 3, 4, 5, 6, 7]
417 val a_regs = map (gp o oreg) [0, 1, 2, 3, 4, 5]
418 val l_reg = gp (oreg 7)
419 val f_regs = map (fp o freg)
420 [0, 2, 4, 6, 8, 10, 12, 14,
421 16, 18, 20, 22, 24, 26, 28, 30]
422 (* a call instruction defines all caller-save registers:
423 * - %g1 - %g7
424 * - %o0 - %o5 (argument registers)
425 * - %o7 (link register)
426 * - all fp registers *)
427
428 val defs = g_regs @ a_regs @ l_reg :: f_regs
429 (* A call instruction "uses" just the argument registers. *)
430 val uses = List.take (a_regs, regargwords)
431 in
432 (defs, uses)
433 end
434
435 val result =
436 case retTy of
437 Ty.C_float => [T.FPR (T.FREG (32, FP 0))]
438 | Ty.C_double => [T.FPR (T.FREG (64, FP 0))] (* %f0/%f1 *)
439 | Ty.C_long_double => []
440 | (Ty.C_STRUCT _ | Ty.C_UNION _) => []
441 | Ty.C_ARRAY _ => error "array return type"
442 | (Ty.C_PTR | Ty.C_void |
443 Ty.C_signed (Ty.I_int | Ty.I_long) |
444 Ty.C_unsigned (Ty.I_int | Ty.I_long)) =>
445 [T.GPR (T.REG (32, oreg 0))]
446 | (Ty.C_signed Ty.I_char | Ty.C_unsigned Ty.I_char) =>
447 [T.GPR (T.REG (8, oreg 0))]
448 | (Ty.C_signed Ty.I_short | Ty.C_unsigned Ty.I_short) =>
449 [T.GPR (T.REG (16, oreg 0))]
450 | (Ty.C_signed Ty.I_long_long | Ty.C_unsigned Ty.I_long_long) =>
451 [T.GPR (T.REG (64, oreg 0))]
452
453 val { save, restore } = saveRestoreDedicated defs
454
455 val (sretsetup, srethandshake) =
456 case res_szal of
457 NONE => ([], [])
458 | SOME (sz, al) => let
459 val addr = structRet { szb = sz, align = al }
460 in
461 ([T.STORE (32, addli (spreg, 64), addr, stack)],
462 [T.EXT (ix (IX.UNIMP sz))])
463 end
464
465 val call = T.CALL { funct = name, targets = [],
466 defs = defs, uses = uses,
467 region = mem, pops = 0 }
468
469 val call =
470 case callComment of
471 NONE => call
472 | SOME c =>
473 T.ANNOTATION (call, #create MLRiscAnnotations.COMMENT c)
474
475 val (sp_sub, sp_add) =
476 if stackdelta = 0 then ([], []) else
477 if paramAlloc { szb = stackdelta, align = 4 } then ([], [])
478 else ([T.MV (32, sp, T.SUB (32, spreg, LI stackdelta))],
479 [T.MV (32, sp, addli (spreg, stackdelta))])
480
481 val callseq =
482 List.concat [sp_sub,
483 copycode,
484 argsetupcode,
485 sretsetup,
486 save,
487 [call],
488 srethandshake,
489 restore,
490 sp_add]
491
492 in
493 { callseq = callseq, result = result }
494 end
495 end