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/ppc/c-calls/ppc-macosx.sml
ViewVC logotype

Annotation of /sml/trunk/src/MLRISC/ppc/c-calls/ppc-macosx.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1533 - (view) (download)

1 : jhr 1346 (* ppc-macosx.sml
2 :     *
3 :     * COPYRIGHT (c) 2003 John Reppy (http://www.cs.uchicago.edu/~jhr)
4 :     * All rights reserved.
5 :     *
6 :     * C function calls for the PowerPC using the MacOS X ABI.
7 :     *
8 :     * Register conventions:
9 :     *
10 :     * Register Callee-save Purpose
11 :     * -------- ----------- -------
12 :     * GPR0 no Zero
13 :     * 1 no Stack pointer
14 :     * 2 no scratch (TOC on AIX)
15 :     * 3 no arg0 and return result
16 :     * 4-10 no arg1-arg7
17 :     * 11 no scratch
18 :     * 12 no holds taget of indirect call
19 :     * 13-31 yes callee-save registers
20 :     *
21 :     * FPR0 no scratch
22 :     * 1-13 no floating-point arguments
23 :     * 14-31 yes floating-point callee-save registers
24 :     *
25 :     * V0-V1 no scratch vector registers
26 :     * 2-13 no vector argument registers
27 :     * 14-19 no scratch vector registers
28 :     * 20-31 yes callee-save vector registers
29 :     *
30 :     * LR no link register holds return address
31 :     *
32 :     * CR0-CR1 no scratch condition registers
33 :     * 2-4 yes callee-save condition registers
34 :     * 5-7 no scratch condition registers
35 :     *
36 :     * Calling convention:
37 :     *
38 :     * Return result:
39 :     * + Integer and pointer results are returned in GPR3
40 :     * + 64-bit integers (long long) returned in GPR3/GPR4
41 :     * + float/double results are returned in FPR1
42 :     * + Struct results are returned in space provided by the caller.
43 :     * The address of this space is passed to the callee as an
44 :     * implicit first argument in GPR3 and the first real argument is
45 :     * passed in GPR4.
46 :     *
47 :     * Function arguments:
48 :     * * arguments (except for floating-point values) are passed in
49 :     * registers GPR3-GPR10
50 : jhr 1438 *
51 :     * Note also that stack frames are supposed to be 16-byte aligned.
52 : jhr 1346 *)
53 :    
54 :     (* we extend the interface to support generating the stubs needed for
55 :     * dynamic linking (see "Inside MacOS X: Mach-O Runtime Architecture"
56 :     * for details.
57 :     *)
58 :     signature PPC_MACOSX_C_CALLS =
59 :     sig
60 :     include C_CALLS
61 : jhr 1438
62 :     (*
63 :     val genStub : {
64 :     name : T.rexp,
65 :     proto : CTypes.c_proto,
66 :     paramAlloc : {szb : int, align : int} -> bool,
67 :     structRet : {szb : int, align : int} -> T.rexp,
68 :     saveRestoreDedicated :
69 :     T.mlrisc list -> {save: T.stm list, restore: T.stm list},
70 :     callComment : string option,
71 :     args : c_arg list
72 :     } -> {
73 :     callseq : T.stm list,
74 :     result: T.mlrisc list
75 :     }
76 :     *)
77 :    
78 : jhr 1346 end;
79 :    
80 :     functor PPCMacOSX_CCalls (
81 :    
82 :     structure T : MLTREE
83 :    
84 : jhr 1527 ): C_CALLS = struct
85 :    
86 : jhr 1346 structure T = T
87 : jhr 1521 structure CTy = CTypes
88 : jhr 1346 structure C = PPCCells
89 :    
90 :     fun error msg = MLRiscErrorMsg.error ("PPCCompCCalls", msg)
91 :    
92 : jhr 1521 (* the location of arguments/parameters; offsets are given with respect to the
93 :     * low end of the parameter area.
94 :     *)
95 :     datatype arg_location
96 : jhr 1522 = Reg of T.ty * T.reg * T.I.machine_int option
97 :     (* integer/pointer argument in register *)
98 : jhr 1524 | FReg of T.fty * T.reg * T.I.machine_int option
99 : jhr 1522 (* floating-point argument in register *)
100 : jhr 1521 | Stk of T.ty * T.I.machine_int (* integer/pointer argument in parameter area *)
101 :     | FStk of T.fty * T.I.machine_int (* floating-point argument in parameter area *)
102 :     | Args of arg_location list
103 :    
104 : jhr 1522 val wordTy = 32
105 :     val fltTy = 32 (* MLRISC type of float *)
106 :     val dblTy = 64 (* MLRISC type of double *)
107 : jhr 1346
108 : jhr 1523 (* stack pointer *)
109 : jhr 1530 val spReg = T.REG(wordTy, C.GPReg 1)
110 : jhr 1523
111 : jhr 1521 (* registers used for parameter passing *)
112 :     val argGPRs = List.map C.GPReg [3, 4, 5, 6, 7, 8, 9, 10]
113 :     val argFPRs = List.map C.FPReg [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
114 : jhr 1530 val resGPR = C.GPReg 3
115 :     val resRegLoc = Reg(wordTy, resGPR, NONE)
116 : jhr 1522 val resFPR = C.FPReg 1
117 : jhr 1521
118 :     (* C callee-save registers *)
119 :     val calleeSaveRegs = List.map C.GPReg [
120 :     13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
121 :     23, 24, 25, 26, 27, 28, 29, 30, 31
122 :     ]
123 :     val calleeSaveFRegs = List.map C.FPReg [
124 :     14, 15, 16, 17, 18, 19, 20, 21, 22,
125 :     23, 24, 25, 26, 27, 28, 29, 30, 31
126 :     ]
127 :    
128 : jhr 1523 (* C caller-save registers (including argument registers) *)
129 :     val callerSaveRegs =
130 : jhr 1528 T.FPR(T.FREG(dblTy, C.FPReg 0)) ::
131 :     (List.map (fn r => T.GPR(T.REG(wordTy, C.GPReg r))) [2, 11, 12])
132 : jhr 1523
133 : jhr 1528 val linkReg = T.GPR(T.REG(wordTy, C.lr))
134 :    
135 : jhr 1525 (* the parameter area lies just above the linkage area in the caller's frame.
136 :     * The linkage area is 24 bytes, so the first parameter is at 24(sp).
137 :     *)
138 :     val paramAreaOffset = 24
139 :    
140 : jhr 1531 (* size, padding, and natural alignment for integer types. Note that the
141 :     * padding is based on the parameter-passing description on p. 35 of the
142 :     * documentation and the alignment is from p. 31.
143 : jhr 1522 *)
144 : jhr 1531 fun sizeOf CTy.I_char = {sz = 1, pad = 3, align = 1}
145 :     | sizeOf CTy.I_short = {sz = 2, pad = 2, align = 2}
146 :     | sizeOf CTy.I_int = {sz = 4, pad = 0, align = 4}
147 :     | sizeOf CTy.I_long = {sz = 4, pad = 0, align = 4}
148 :     | sizeOf CTy.I_long_long = {sz = 8, pad = 0, align = 8}
149 : jhr 1521
150 : jhr 1522 (* sizes of other C types *)
151 : jhr 1531 val sizeOfPtr = {sz = 4, pad = 0, align = 4}
152 : jhr 1522
153 : jhr 1531 (* compute the size and alignment information for a struct; tys is the list
154 :     * of member types. The alignment is what Apple calls the "embedding" alignment.
155 :     *)
156 : jhr 1525 fun sizeOfStruct tys = let
157 : jhr 1531 (* align the address to the given alignment, which must be a power of 2 *)
158 :     fun alignAddr (addr, align) = let
159 :     val mask = Word.fromInt(align-1)
160 :     in
161 :     Word.toIntX(Word.andb(Word.fromInt addr + mask, Word.notb mask))
162 :     end
163 : jhr 1525 fun sz CTy.C_void = error "unexpected void argument type"
164 : jhr 1531 | sz CTy.C_float = {sz = 4, align = 4}
165 :     | sz CTy.C_double = {sz = 8, align = 8}
166 :     | sz CTy.C_long_double = {sz = 8, align = 8}
167 :     | sz (CTy.C_unsigned isz) = let
168 :     val {sz, align, ...} = sizeOf isz
169 :     in
170 :     {sz = sz, align = align}
171 :     end
172 :     | sz (CTy.C_signed isz) = let
173 :     val {sz, align, ...} = sizeOf isz
174 :     in
175 :     {sz = sz, align = align}
176 :     end
177 :     | sz CTy.C_PTR = {sz = 4, align = 4}
178 :     | sz (CTy.C_ARRAY(ty, n)) = let
179 :     val {sz, align} = sz ty
180 :     in
181 :     {sz = n*sz, align = align}
182 :     end
183 :     | sz (CTy.C_STRUCT tys) = ssz tys
184 :     and ssz [] = {sz = 0, align = 4}
185 :     | ssz (first::rest) = let
186 :     fun f ([], maxAlign, offset) =
187 :     {sz = alignAddr(offset, maxAlign), align = maxAlign}
188 :     | f (ty::tys, maxAlign, offset) = let
189 :     val {sz, align} = sz ty
190 :     val align = Int.min(align, 4)
191 :     val offset = alignAddr(offset, align)
192 :     in
193 :     f (tys, Int.max(maxAlign, align), offset+sz)
194 :     end
195 :     val {sz, align} = sz first
196 :     in
197 :     f (rest, align, sz)
198 :     end
199 : jhr 1525 in
200 : jhr 1531 #sz(ssz tys)
201 : jhr 1525 end
202 : jhr 1522
203 : jhr 1521 (* compute the layout of a C call's arguments *)
204 :     fun layout {conv, retTy, paramTys} = let
205 : jhr 1527 fun gprRes isz = (case #sz(sizeOf isz)
206 : jhr 1522 of 8 => raise Fail "register pairs not yet supported"
207 : jhr 1528 | _ => SOME resRegLoc
208 : jhr 1521 (* end case *))
209 : jhr 1527 val (resLoc, argGPRs, structRet) = (case retTy
210 :     of CTy.C_void => (NONE, argGPRs, NONE)
211 :     | CTy.C_float => (SOME(FReg(fltTy, resFPR, NONE)), argGPRs, NONE)
212 :     | CTy.C_double => (SOME(FReg(dblTy, resFPR, NONE)), argGPRs, NONE)
213 :     | CTy.C_long_double => (SOME(FReg(dblTy, resFPR, NONE)), argGPRs, NONE)
214 :     | CTy.C_unsigned isz => (gprRes isz, argGPRs, NONE)
215 :     | CTy.C_signed isz => (gprRes isz, argGPRs, NONE)
216 : jhr 1528 | CTy.C_PTR => (SOME resRegLoc, argGPRs, NONE)
217 : jhr 1522 | CTy.C_ARRAY _ => error "array return type"
218 :     | CTy.C_STRUCT s => let
219 :     val sz = sizeOfStruct s
220 :     in
221 :     (* Note that this is a place where the MacOS X and Linux ABIs differ.
222 :     * In Linux, GPR3/GPR4 are used to return composite values of 8 bytes.
223 :     *)
224 :     if (sz > 4)
225 : jhr 1528 then (SOME resRegLoc, List.tl argGPRs, SOME{szb=sz, align=4})
226 :     else (SOME resRegLoc, argGPRs, NONE)
227 : jhr 1522 end
228 :     (* end case *))
229 : jhr 1527 fun assign ([], offset, _, _, layout) = List.rev layout
230 : jhr 1525 | assign (ty::tys, offset, availGPRs, availFPRs, layout) = (
231 :     case ty
232 : jhr 1533 of CTy.C_void => error "unexpected void argument type"
233 : jhr 1522 | CTy.C_float => (case (availGPRs, availFPRs)
234 : jhr 1527 of (_::gprs, fpr::fprs) =>
235 : jhr 1525 assign (tys, offset+4, gprs, fprs, FReg(fltTy, fpr, SOME offset)::layout)
236 : jhr 1522 | ([], fpr::fprs) =>
237 : jhr 1525 assign (tys, offset+4, [], fprs, FReg(fltTy, fpr, SOME offset)::layout)
238 : jhr 1522 | ([], []) =>
239 : jhr 1525 assign (tys, offset+4, [], [], FStk(fltTy, offset)::layout)
240 : jhr 1522 (* end case *))
241 : jhr 1521 | CTy.C_double =>
242 : jhr 1525 assignFPR (tys, offset, availGPRs, availFPRs, layout)
243 : jhr 1521 | CTy.C_long_double =>
244 : jhr 1525 assignFPR (tys, offset, availGPRs, availFPRs, layout)
245 : jhr 1521 | CTy.C_unsigned isz =>
246 : jhr 1525 assignGPR(sizeOf isz, tys, offset, availGPRs, availFPRs, layout)
247 : jhr 1521 | CTy.C_signed isz =>
248 : jhr 1525 assignGPR(sizeOf isz, tys, offset, availGPRs, availFPRs, layout)
249 : jhr 1521 | CTy.C_PTR =>
250 : jhr 1525 assignGPR(sizeOfPtr, tys, offset, availGPRs, availFPRs, layout)
251 : jhr 1521 | CTy.C_ARRAY _ =>
252 : jhr 1525 assignGPR(sizeOfPtr, tys, offset, availGPRs, availFPRs, layout)
253 :     | CTy.C_STRUCT tys' => raise Fail "struct arguments not supported yet"
254 : jhr 1521 (* end case *))
255 : jhr 1522 (* assign a GP register and memory for an integer/pointer argument. *)
256 : jhr 1531 and assignGPR ({sz, pad, ...}, args, offset, availGPRs, availFPRs, layout) = let
257 : jhr 1522 val (loc, availGPRs) = (case (sz, availGPRs)
258 :     of (8, _) => raise Fail "register pairs not yet supported"
259 :     | (_, []) => (Stk(wordTy, offset), [])
260 :     | (_, r1::rs) => (Reg(wordTy, r1, SOME offset), rs)
261 :     (* end case *))
262 : jhr 1527 val offset = offset + IntInf.fromInt(sz + pad)
263 : jhr 1521 in
264 : jhr 1522 assign (args, offset, availGPRs, availFPRs, loc::layout)
265 : jhr 1521 end
266 : jhr 1522 (* assign a FP register and memory/GPRs for double-precision argument. *)
267 :     and assignFPR (args, offset, availGPRs, availFPRs, layout) = let
268 :     fun continue (availGPRs, availFPRs, loc) =
269 :     assign (args, offset+8, availGPRs, availFPRs, loc::layout)
270 :     fun freg fpr = FReg(dblTy, fpr, SOME offset)
271 :     in
272 :     case (availGPRs, availFPRs)
273 :     of (_::_::gprs, fpr::fprs) => continue (gprs, fprs, freg fpr)
274 :     | (_, fpr::fprs) => continue ([], fprs, freg fpr)
275 :     | ([], []) => continue ([], [], FStk(dblTy, offset))
276 :     (* end case *)
277 :     end
278 : jhr 1523 in {
279 :     argLocs = assign (paramTys, 0, argGPRs, argFPRs, []),
280 : jhr 1527 resLoc = resLoc,
281 : jhr 1530 structRetLoc = structRet
282 : jhr 1523 } end
283 : jhr 1521
284 : jhr 1346 datatype c_arg
285 :     = ARG of T.rexp
286 :     | FARG of T.fexp
287 :     | ARGS of c_arg list
288 :    
289 : jhr 1523 val memRg = T.Region.memory
290 :     val stkRg = T.Region.memory
291 : jhr 1346
292 : jhr 1525 (* SP-based address of parameter at given offset *)
293 : jhr 1530 fun paramAddr off =
294 :     T.ADD(wordTy, spReg, T.LI(off + IntInf.fromInt paramAreaOffset))
295 : jhr 1525
296 : jhr 1445 fun genCall {
297 :     name, proto, paramAlloc, structRet, saveRestoreDedicated,
298 :     callComment, args
299 :     } = let
300 :     val {conv, retTy, paramTys} = proto
301 : jhr 1530 val {argLocs, resLoc, structRetLoc} = layout proto
302 : jhr 1523 (* generate code to assign the arguments to their locations *)
303 :     fun assignArgs ([], [], stms) = stms
304 :     | assignArgs (Reg(ty, r, _) :: locs, ARG exp :: args, stms) =
305 :     assignArgs (locs, args, T.MV(ty, r, exp) :: stms)
306 :     | assignArgs (Stk(ty, off) :: locs, ARG exp :: args, stms) =
307 : jhr 1525 assignArgs (locs, args, T.STORE(ty, paramAddr off, exp, stkRg) :: stms)
308 : jhr 1527 | assignArgs (FReg(ty, r, _) :: locs, FARG fexp :: args, stms) =
309 : jhr 1523 assignArgs (locs, args, T.FMV(ty, r, fexp) :: stms)
310 :     | assignArgs (FStk(ty, off) :: locs, FARG fexp :: args, stms) =
311 : jhr 1525 assignArgs (locs, args, T.FSTORE(ty, paramAddr off, fexp, stkRg) :: stms)
312 : jhr 1523 | assignArgs ((Args locs') :: locs, (ARGS args') :: args, stms) =
313 :     assignArgs (locs, args, assignArgs(locs', args', stms))
314 :     | assignArgs _ = error "argument/formal mismatch"
315 : jhr 1527 val argSetupCode = List.rev(assignArgs(argLocs, args, []))
316 : jhr 1523 (* convert the result location to an MLRISC expression list *)
317 :     val result = (case resLoc
318 :     of NONE => []
319 : jhr 1527 | SOME(Reg(ty, r, _)) => [T.GPR(T.REG(ty, r))]
320 :     | SOME(FReg(ty, r, _)) => [T.FPR(T.FREG(ty, r))]
321 : jhr 1523 | SOME _ => raise Fail "bogus result location"
322 :     (* end case *))
323 : jhr 1530 (* make struct return-area setup (if necessary) *)
324 :     val setupStructRet = (case structRetLoc
325 :     of NONE => []
326 :     | SOME loc => let
327 :     val structAddr = structRet loc
328 :     in
329 :     [T.MV(wordTy, resGPR, structAddr)]
330 :     end
331 :     (* end case *))
332 : jhr 1523 (* determine the registers used and defined by this call *)
333 :     val (uses, defs) = let
334 :     val locs = (case resLoc
335 :     of NONE => argLocs
336 :     | SOME loc => loc::argLocs
337 :     (* end case *))
338 :     (* get the list of registers used to pass arguments and results *)
339 :     fun addArgReg (Reg(ty, r, _)::locs, argRegs) =
340 :     addArgReg (locs, T.GPR(T.REG(ty, r))::argRegs)
341 :     | addArgReg (FReg(ty, r, _)::locs, argRegs) =
342 :     addArgReg (locs, T.FPR(T.FREG(ty, r))::argRegs)
343 :     | addArgReg ((Args locs')::locs, argRegs) =
344 :     addArgReg (locs, addArgReg(locs', argRegs))
345 :     | addArgReg (_::locs, argRegs) = addArgReg(locs, argRegs)
346 : jhr 1533 | addArgReg ([], argRegs) = argRegs
347 : jhr 1523 val argRegs = addArgReg (locs, [])
348 :     in
349 : jhr 1528 (argRegs, linkReg :: callerSaveRegs)
350 : jhr 1523 end
351 :     (* the actual call instruction *)
352 :     val callStm = T.CALL {
353 :     funct = name, targets = [],
354 :     defs = defs, uses = uses,
355 :     region = memRg, pops = 0
356 :     }
357 :     (* annotate, if necessary *)
358 :     val callStm = (case callComment
359 :     of NONE => callStm
360 :     | SOME c => T.ANNOTATION(callStm, #create MLRiscAnnotations.COMMENT c)
361 :     (* end case *))
362 : jhr 1530 (* take care of dedicated client registers *)
363 :     val {save, restore} = saveRestoreDedicated defs
364 : jhr 1445 val callseq = List.concat [
365 : jhr 1530 setupStructRet,
366 : jhr 1523 argSetupCode,
367 : jhr 1530 save,
368 :     [callStm],
369 :     restore
370 : jhr 1445 ]
371 :     in
372 :     (* check calling convention *)
373 :     case conv
374 :     of ("" | "ccall") => ()
375 :     | _ => error (concat [
376 :     "unknown calling convention \"",
377 :     String.toString conv, "\""
378 :     ])
379 :     (* end case *);
380 :     {callseq = callseq, result = result}
381 :     end
382 : jhr 1346
383 : jhr 1445 end

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