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 1528 - (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 :     val sp = C.GPReg 1
110 :     val spR = T.REG(wordTy, sp)
111 :    
112 : jhr 1521 (* registers used for parameter passing *)
113 :     val argGPRs = List.map C.GPReg [3, 4, 5, 6, 7, 8, 9, 10]
114 :     val argFPRs = List.map C.FPReg [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
115 : jhr 1528 val resRegLoc = Reg(wordTy, C.GPReg 3, 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 1522 (* size and padding for integer types. Note that the padding is based on the
141 :     * parameter-passing description on p. 35 of the documentation.
142 :     *)
143 : jhr 1521 fun sizeOf CTy.I_char = {sz = 1, pad = 3}
144 :     | sizeOf CTy.I_short = {sz = 2, pad = 2}
145 :     | sizeOf CTy.I_int = {sz = 4, pad = 0}
146 :     | sizeOf CTy.I_long = {sz = 4, pad = 0}
147 :     | sizeOf CTy.I_long_long = {sz = 8, pad = 0}
148 :    
149 : jhr 1522 (* sizes of other C types *)
150 :     val sizeOfPtr = {sz = 4, pad = 0}
151 :    
152 : jhr 1525 fun sizeOfStruct tys = let
153 :     (*
154 :     fun sz CTy.C_void = error "unexpected void argument type"
155 :     | sz CTy.C_float = 4
156 :     | sz CTy.C_double = 8
157 :     | sz CTy.C_long_double = 8
158 :     | sz CTy.C_unsigned isz = #sz(sizeOf isz)
159 :     | sz CTy.C_signed isz = #sz(sizeOf isz)
160 :     | sz CTy.C_PTR = 4
161 :     | sz (CTy.C_ARRAY tys) = let
162 :     | sz (CTy.C_STRUCT s) =
163 :     *)
164 :     in
165 :     raise Fail "FIXME"
166 :     end
167 : jhr 1522
168 : jhr 1521 (* compute the layout of a C call's arguments *)
169 :     fun layout {conv, retTy, paramTys} = let
170 : jhr 1527 fun gprRes isz = (case #sz(sizeOf isz)
171 : jhr 1522 of 8 => raise Fail "register pairs not yet supported"
172 : jhr 1528 | _ => SOME resRegLoc
173 : jhr 1521 (* end case *))
174 : jhr 1527 val (resLoc, argGPRs, structRet) = (case retTy
175 :     of CTy.C_void => (NONE, argGPRs, NONE)
176 :     | CTy.C_float => (SOME(FReg(fltTy, resFPR, NONE)), argGPRs, NONE)
177 :     | CTy.C_double => (SOME(FReg(dblTy, resFPR, NONE)), argGPRs, NONE)
178 :     | CTy.C_long_double => (SOME(FReg(dblTy, resFPR, NONE)), argGPRs, NONE)
179 :     | CTy.C_unsigned isz => (gprRes isz, argGPRs, NONE)
180 :     | CTy.C_signed isz => (gprRes isz, argGPRs, NONE)
181 : jhr 1528 | CTy.C_PTR => (SOME resRegLoc, argGPRs, NONE)
182 : jhr 1522 | CTy.C_ARRAY _ => error "array return type"
183 :     | CTy.C_STRUCT s => let
184 :     val sz = sizeOfStruct s
185 :     in
186 :     (* Note that this is a place where the MacOS X and Linux ABIs differ.
187 :     * In Linux, GPR3/GPR4 are used to return composite values of 8 bytes.
188 :     *)
189 :     if (sz > 4)
190 : jhr 1528 then (SOME resRegLoc, List.tl argGPRs, SOME{szb=sz, align=4})
191 :     else (SOME resRegLoc, argGPRs, NONE)
192 : jhr 1522 end
193 :     (* end case *))
194 : jhr 1527 fun assign ([], offset, _, _, layout) = List.rev layout
195 : jhr 1525 | assign (ty::tys, offset, availGPRs, availFPRs, layout) = (
196 :     case ty
197 :     of CTy.C_void => error "unexpected void tyument type"
198 : jhr 1522 | CTy.C_float => (case (availGPRs, availFPRs)
199 : jhr 1527 of (_::gprs, fpr::fprs) =>
200 : jhr 1525 assign (tys, offset+4, gprs, fprs, FReg(fltTy, fpr, SOME offset)::layout)
201 : jhr 1522 | ([], fpr::fprs) =>
202 : jhr 1525 assign (tys, offset+4, [], fprs, FReg(fltTy, fpr, SOME offset)::layout)
203 : jhr 1522 | ([], []) =>
204 : jhr 1525 assign (tys, offset+4, [], [], FStk(fltTy, offset)::layout)
205 : jhr 1522 (* end case *))
206 : jhr 1521 | CTy.C_double =>
207 : jhr 1525 assignFPR (tys, offset, availGPRs, availFPRs, layout)
208 : jhr 1521 | CTy.C_long_double =>
209 : jhr 1525 assignFPR (tys, offset, availGPRs, availFPRs, layout)
210 : jhr 1521 | CTy.C_unsigned isz =>
211 : jhr 1525 assignGPR(sizeOf isz, tys, offset, availGPRs, availFPRs, layout)
212 : jhr 1521 | CTy.C_signed isz =>
213 : jhr 1525 assignGPR(sizeOf isz, tys, offset, availGPRs, availFPRs, layout)
214 : jhr 1521 | CTy.C_PTR =>
215 : jhr 1525 assignGPR(sizeOfPtr, tys, offset, availGPRs, availFPRs, layout)
216 : jhr 1521 | CTy.C_ARRAY _ =>
217 : jhr 1525 assignGPR(sizeOfPtr, tys, offset, availGPRs, availFPRs, layout)
218 :     | CTy.C_STRUCT tys' => raise Fail "struct arguments not supported yet"
219 : jhr 1521 (* end case *))
220 : jhr 1522 (* assign a GP register and memory for an integer/pointer argument. *)
221 :     and assignGPR ({sz, pad}, args, offset, availGPRs, availFPRs, layout) = let
222 :     val (loc, availGPRs) = (case (sz, availGPRs)
223 :     of (8, _) => raise Fail "register pairs not yet supported"
224 :     | (_, []) => (Stk(wordTy, offset), [])
225 :     | (_, r1::rs) => (Reg(wordTy, r1, SOME offset), rs)
226 :     (* end case *))
227 : jhr 1527 val offset = offset + IntInf.fromInt(sz + pad)
228 : jhr 1521 in
229 : jhr 1522 assign (args, offset, availGPRs, availFPRs, loc::layout)
230 : jhr 1521 end
231 : jhr 1522 (* assign a FP register and memory/GPRs for double-precision argument. *)
232 :     and assignFPR (args, offset, availGPRs, availFPRs, layout) = let
233 :     fun continue (availGPRs, availFPRs, loc) =
234 :     assign (args, offset+8, availGPRs, availFPRs, loc::layout)
235 :     fun freg fpr = FReg(dblTy, fpr, SOME offset)
236 :     in
237 :     case (availGPRs, availFPRs)
238 :     of (_::_::gprs, fpr::fprs) => continue (gprs, fprs, freg fpr)
239 :     | (_, fpr::fprs) => continue ([], fprs, freg fpr)
240 :     | ([], []) => continue ([], [], FStk(dblTy, offset))
241 :     (* end case *)
242 :     end
243 : jhr 1523 in {
244 :     argLocs = assign (paramTys, 0, argGPRs, argFPRs, []),
245 : jhr 1527 resLoc = resLoc,
246 : jhr 1525 structRet = structRet
247 : jhr 1523 } end
248 : jhr 1521
249 : jhr 1346 datatype c_arg
250 :     = ARG of T.rexp
251 :     | FARG of T.fexp
252 :     | ARGS of c_arg list
253 :    
254 : jhr 1523 val memRg = T.Region.memory
255 :     val stkRg = T.Region.memory
256 : jhr 1346
257 : jhr 1525 (* SP-based address of parameter at given offset *)
258 : jhr 1527 fun paramAddr off = T.ADD(wordTy, spR, T.LI(off + IntInf.fromInt paramAreaOffset))
259 : jhr 1525
260 : jhr 1445 fun genCall {
261 :     name, proto, paramAlloc, structRet, saveRestoreDedicated,
262 :     callComment, args
263 :     } = let
264 :     val {conv, retTy, paramTys} = proto
265 : jhr 1523 val {argLocs, resLoc, structRet} = layout proto
266 :     (* generate code to assign the arguments to their locations *)
267 :     fun assignArgs ([], [], stms) = stms
268 :     | assignArgs (Reg(ty, r, _) :: locs, ARG exp :: args, stms) =
269 :     assignArgs (locs, args, T.MV(ty, r, exp) :: stms)
270 :     | assignArgs (Stk(ty, off) :: locs, ARG exp :: args, stms) =
271 : jhr 1525 assignArgs (locs, args, T.STORE(ty, paramAddr off, exp, stkRg) :: stms)
272 : jhr 1527 | assignArgs (FReg(ty, r, _) :: locs, FARG fexp :: args, stms) =
273 : jhr 1523 assignArgs (locs, args, T.FMV(ty, r, fexp) :: stms)
274 :     | assignArgs (FStk(ty, off) :: locs, FARG fexp :: args, stms) =
275 : jhr 1525 assignArgs (locs, args, T.FSTORE(ty, paramAddr off, fexp, stkRg) :: stms)
276 : jhr 1523 | assignArgs ((Args locs') :: locs, (ARGS args') :: args, stms) =
277 :     assignArgs (locs, args, assignArgs(locs', args', stms))
278 :     | assignArgs _ = error "argument/formal mismatch"
279 : jhr 1527 val argSetupCode = List.rev(assignArgs(argLocs, args, []))
280 : jhr 1523 (* convert the result location to an MLRISC expression list *)
281 :     val result = (case resLoc
282 :     of NONE => []
283 : jhr 1527 | SOME(Reg(ty, r, _)) => [T.GPR(T.REG(ty, r))]
284 :     | SOME(FReg(ty, r, _)) => [T.FPR(T.FREG(ty, r))]
285 : jhr 1523 | SOME _ => raise Fail "bogus result location"
286 :     (* end case *))
287 :     (* determine the registers used and defined by this call *)
288 :     val (uses, defs) = let
289 :     val locs = (case resLoc
290 :     of NONE => argLocs
291 :     | SOME loc => loc::argLocs
292 :     (* end case *))
293 :     (* get the list of registers used to pass arguments and results *)
294 :     fun addArgReg (Reg(ty, r, _)::locs, argRegs) =
295 :     addArgReg (locs, T.GPR(T.REG(ty, r))::argRegs)
296 :     | addArgReg (FReg(ty, r, _)::locs, argRegs) =
297 :     addArgReg (locs, T.FPR(T.FREG(ty, r))::argRegs)
298 :     | addArgReg ((Args locs')::locs, argRegs) =
299 :     addArgReg (locs, addArgReg(locs', argRegs))
300 :     | addArgReg (_::locs, argRegs) = addArgReg(locs, argRegs)
301 :     val argRegs = addArgReg (locs, [])
302 :     in
303 : jhr 1528 (argRegs, linkReg :: callerSaveRegs)
304 : jhr 1523 end
305 :     (* the actual call instruction *)
306 :     val callStm = T.CALL {
307 :     funct = name, targets = [],
308 :     defs = defs, uses = uses,
309 :     region = memRg, pops = 0
310 :     }
311 :     (* annotate, if necessary *)
312 :     val callStm = (case callComment
313 :     of NONE => callStm
314 :     | SOME c => T.ANNOTATION(callStm, #create MLRiscAnnotations.COMMENT c)
315 :     (* end case *))
316 : jhr 1445 val callseq = List.concat [
317 : jhr 1523 argSetupCode,
318 : jhr 1525 [callStm]
319 : jhr 1445 ]
320 :     in
321 :     (* check calling convention *)
322 :     case conv
323 :     of ("" | "ccall") => ()
324 :     | _ => error (concat [
325 :     "unknown calling convention \"",
326 :     String.toString conv, "\""
327 :     ])
328 :     (* end case *);
329 :     {callseq = callseq, result = result}
330 :     end
331 : jhr 1346
332 : jhr 1445 end

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