| 1 |
(* marshal.sml |
| 2 |
* |
| 3 |
* Marshaling for located arguments. |
| 4 |
*) |
| 5 |
|
| 6 |
structure Marshal = |
| 7 |
struct |
| 8 |
|
| 9 |
structure DL = DynLinkage |
| 10 |
structure Consts = VarargConstants |
| 11 |
structure V = Vararg |
| 12 |
structure W = VarargConstants.W |
| 13 |
|
| 14 |
datatype argument = I of int | R of real | B of bool | S of string |
| 15 |
|
| 16 |
fun main's s = DL.lib_symbol (DL.main_lib, s) |
| 17 |
val malloc_h = main's "malloc" |
| 18 |
val free_h = main's "free" |
| 19 |
|
| 20 |
exception OutOfMemory |
| 21 |
|
| 22 |
fun sys_malloc (n : W.word) = |
| 23 |
let val w_p = RawMemInlineT.rawccall : |
| 24 |
W.word * W.word * (unit * word -> string) list |
| 25 |
-> W.word |
| 26 |
val a = w_p (DL.addr malloc_h, n, []) |
| 27 |
in if a = 0w0 then raise OutOfMemory else a |
| 28 |
end |
| 29 |
|
| 30 |
fun sys_free (a : W.word) = |
| 31 |
let val p_u = RawMemInlineT.rawccall : |
| 32 |
W.word * W.word * (unit * string -> unit) list |
| 33 |
-> unit |
| 34 |
in p_u (DL.addr free_h, a, []) |
| 35 |
end |
| 36 |
|
| 37 |
fun alloc bytes = sys_malloc (W.toLargeWord bytes) |
| 38 |
fun free a = sys_free a |
| 39 |
|
| 40 |
type addr = W.word |
| 41 |
infix ++ |
| 42 |
fun (a: addr) ++ i = a + W.fromInt i |
| 43 |
|
| 44 |
fun set' (p, w) = RawMemInlineT.w32s (p, w) |
| 45 |
fun nxt' p = p ++ 1 |
| 46 |
|
| 47 |
fun cpML' { from, to } = let |
| 48 |
val n = String.size from |
| 49 |
fun loop (i, p) = |
| 50 |
if i >= n then set' (p, 0w0) |
| 51 |
else (set' (p, W.fromInt (Char.ord |
| 52 |
(String.sub (from, i)))); |
| 53 |
loop (i+1, nxt' p)) |
| 54 |
in |
| 55 |
loop (0, to) |
| 56 |
end |
| 57 |
|
| 58 |
fun dupML' s = let |
| 59 |
val z = alloc (W.fromInt (String.size s + 1)) |
| 60 |
in |
| 61 |
cpML' { from = s, to = z }; |
| 62 |
z |
| 63 |
end |
| 64 |
|
| 65 |
fun set (p, off, v) = set'(p + off, v) |
| 66 |
|
| 67 |
(* track strings allocated for the call *) |
| 68 |
local |
| 69 |
val allocatedStrs = ref ([] : W.word list) |
| 70 |
in |
| 71 |
fun freeStrs () = ( |
| 72 |
List.app free (!allocatedStrs); |
| 73 |
allocatedStrs := []) |
| 74 |
fun addStr s = allocatedStrs := s :: !allocatedStrs |
| 75 |
end |
| 76 |
|
| 77 |
(* marshal the argument field *) |
| 78 |
fun marshalArg (locdArgsArr, V.SINT_ARG i) = set(locdArgsArr, Consts.argOffB, W.fromInt i) |
| 79 |
| marshalArg (locdArgsArr, V.STRING_ARG s) = let |
| 80 |
val strPtr = dupML' s |
| 81 |
in |
| 82 |
addStr strPtr; |
| 83 |
set(locdArgsArr, Consts.argOffB, strPtr) |
| 84 |
end |
| 85 |
| marshalArg (locdArgsArr, V.PTR_ARG p) = set(locdArgsArr, Consts.argOffB, p) |
| 86 |
| marshalArg (locdArgsArr, V.DOUBLE_ARG r) = RawMemInlineT.f64s (locdArgsArr + Consts.argOffB, r) |
| 87 |
|
| 88 |
(* marshal a located argument *) |
| 89 |
fun marshalLocdArg ({arg, k, width, narrowing, loc, offset}, locdArgsArr) = ( |
| 90 |
set(locdArgsArr, Consts.kindOffB, Consts.kind k); |
| 91 |
set(locdArgsArr, Consts.widthOffB, W.fromInt width); |
| 92 |
set(locdArgsArr, Consts.narrowingOffB, W.fromInt (Option.getOpt(narrowing, width))); |
| 93 |
set(locdArgsArr, Consts.locOffB, W.fromInt loc); |
| 94 |
set(locdArgsArr, Consts.offsetOffB, W.fromInt offset); |
| 95 |
marshalArg(locdArgsArr, arg); |
| 96 |
(* advance the pointer by one located argument *) |
| 97 |
locdArgsArr + Consts.locdArgSzB |
| 98 |
) |
| 99 |
|
| 100 |
(* marshal an array of located arguments *) |
| 101 |
fun marshalLocdArgs locdArgs = let |
| 102 |
val argsSzB = W.fromInt (List.length locdArgs) * Consts.locdArgSzB |
| 103 |
val locdArgsArr = alloc argsSzB |
| 104 |
in |
| 105 |
List.foldl marshalLocdArg locdArgsArr locdArgs; |
| 106 |
{startLocdArgs=locdArgsArr, endLocdArgs=argsSzB + locdArgsArr} |
| 107 |
end |
| 108 |
|
| 109 |
end |