| 1 |
(* vararg-call-fn.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2008 Michael Rainey (http://cs.uchicago.edu/~mrainey) |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Glues together the ML side of the variadic call. The call setup is |
| 7 |
* as follows: |
| 8 |
* - convert the arguments to requests |
| 9 |
* - convernt the requests to locations |
| 10 |
* - marshal the locations for the interpreter |
| 11 |
* - transfer control to the interpreter by using the compiler's |
| 12 |
* standard C calling facility |
| 13 |
*) |
| 14 |
|
| 15 |
functor VarargCallFn ( |
| 16 |
|
| 17 |
(* machine-specific data for staged allocation *) |
| 18 |
structure SA : STAGED_ALLOCATION |
| 19 |
where type reg_id = int |
| 20 |
where type loc_kind = CLocKind.loc_kind |
| 21 |
(* parameter convention *) |
| 22 |
val params : SA.stage list |
| 23 |
(* return convention *) |
| 24 |
val returns : SA.stage list |
| 25 |
(* initial store *) |
| 26 |
val store0 : SA.store |
| 27 |
|
| 28 |
val bitWidthOfPointer : int |
| 29 |
(* byte alignment of a pointer *) |
| 30 |
val alignBOfPointer : int |
| 31 |
(* byte alignment of an integer *) |
| 32 |
val alignBOfInt : int |
| 33 |
(* byte alignment of a double-precision float *) |
| 34 |
val alignBOfDouble : int |
| 35 |
(* register kind of ints *) |
| 36 |
val kindOfInt : CLocKind.loc_kind |
| 37 |
(* register kind of pointers *) |
| 38 |
val kindOfPointer : CLocKind.loc_kind |
| 39 |
(* register kind of doubles *) |
| 40 |
val kindOfDouble : CLocKind.loc_kind |
| 41 |
|
| 42 |
) = struct |
| 43 |
|
| 44 |
structure LocatedArgs = LocatedArgFn (structure SA = SA) |
| 45 |
structure V = Vararg |
| 46 |
|
| 47 |
fun widthOfArg (V.SINT_ARG i) = 32 |
| 48 |
| widthOfArg (V.DOUBLE_ARG r) = 64 |
| 49 |
| widthOfArg (V.PTR_ARG s) = bitWidthOfPointer |
| 50 |
| widthOfArg (V.STRING_ARG _) = bitWidthOfPointer |
| 51 |
|
| 52 |
fun kindOfArg (V.SINT_ARG i) = kindOfInt |
| 53 |
| kindOfArg (V.DOUBLE_ARG r) = kindOfDouble |
| 54 |
| kindOfArg (V.PTR_ARG s) = kindOfPointer |
| 55 |
| kindOfArg (V.STRING_ARG _) = kindOfPointer |
| 56 |
|
| 57 |
fun alignOfArg (V.SINT_ARG i) = alignBOfInt |
| 58 |
| alignOfArg (V.DOUBLE_ARG r) = alignBOfDouble |
| 59 |
| alignOfArg (V.PTR_ARG s) = alignBOfPointer |
| 60 |
| alignOfArg (V.STRING_ARG _) = alignBOfPointer |
| 61 |
|
| 62 |
fun argToReq a = (widthOfArg a, kindOfArg a, alignOfArg a) |
| 63 |
|
| 64 |
(* apply the variadic C function to args *) |
| 65 |
fun dispatchLowlevelCall (cFun, args) = let |
| 66 |
val reqs = List.map argToReq args |
| 67 |
val (locs, store) = SA.allocateSeq params (reqs, store0) |
| 68 |
val locdArgs = LocatedArgs.mkLocatedArgs (args, locs) |
| 69 |
(*val _ = print ((String.concatWith " " (List.map LocatedArgs.toString locdArgs))^"\n")*) |
| 70 |
val nLocdArgs = List.length locdArgs |
| 71 |
val {startLocdArgs, endLocdArgs} = Marshal.marshalLocdArgs locdArgs |
| 72 |
in |
| 73 |
(* call the interpreter *) |
| 74 |
SMLNJPrimCCall.applyInterp( |
| 75 |
cFun, |
| 76 |
startLocdArgs, |
| 77 |
endLocdArgs) |
| 78 |
end |
| 79 |
|
| 80 |
end |