Debian Codegraph @

lib/mlrisc-lib/MLRISC/c-call/varargs/call/located-arg-fn.sml

raw
1 (* located-arg-fn.sml
2 *
3 * Create located arguments.
4 *)
5
6 functor LocatedArgFn (
7 structure SA : STAGED_ALLOCATION
8 where type reg_id = int
9 where type loc_kind = CLocKind.loc_kind
10 ) :> sig
11
12 type located_arg = {
13 k : CLocKind.loc_kind,
14 width : int, (* bit width of the argument *)
15 narrowing : int option, (* bit width to convert the argument to *)
16 loc : int, (* register id or stack offset *)
17 offset : int, (* offset if the argument is split between two locations *)
18 arg : Vararg.arg (* the argument *)
19 }
20
21 (* converts a sequence of arguments and staged allocation locations to located arguments *)
22 val mkLocatedArgs : (Vararg.arg list * SA.loc list) -> located_arg list
23
24 val toString : located_arg -> string
25
26 end = struct
27
28 structure V = Vararg
29 structure CK = CLocKind
30 structure SA = SA
31
32 type located_arg = {
33 k : CK.loc_kind,
34 width : int, (* bit width of the argument *)
35 narrowing : int option, (* bit width to convert the argument to *)
36 loc : int, (* register id or stack offset *)
37 offset : int, (* offset if the argument is split between two locations *)
38 arg : Vararg.arg (* the argument *)
39 }
40
41 fun narrowLocdArg narrowing {arg, k, width, loc, offset, narrowing=n} =
42 {arg=arg, k=k, width=width, loc=loc, offset=offset, narrowing=SOME narrowing}
43
44 fun narrowLocdArgs (narrowing, locdArgs) =
45 List.map (narrowLocdArg narrowing) locdArgs
46
47 fun combineLocdArg ({arg, k, width, loc, offset, narrowing} : located_arg, (i, locdArgs)) = let
48 val offset = offset + i
49 val locdArg = {arg=arg, k=k, width=width, loc=loc, offset=offset, narrowing=narrowing}
50 in
51 (i + 1, locdArg :: locdArgs)
52 end
53
54 val combineLocdArgs = List.rev o #2 o List.foldl combineLocdArg (0, [])
55
56 (* take an argument and a location and return a located argument *)
57 fun mkLocatedArg (arg:V.arg, SA.REG (w, k, r)) =
58 [{arg=arg, k=k, width=w, narrowing=NONE, loc=r, offset=0}]
59 | mkLocatedArg (arg, SA.BLOCK_OFFSET (w, (CK.GPR | CK.STK), off)) =
60 [{arg=arg, k=CK.STK, width=w, narrowing=NONE, loc=off, offset=0}]
61 | mkLocatedArg (arg, SA.BLOCK_OFFSET (w, (CK.FPR | CK.FSTK), off)) =
62 [{arg=arg, k=CK.FSTK, width=w, narrowing=NONE, loc=off, offset=0}]
63 | mkLocatedArg (arg, SA.NARROW (loc, narrowing, k)) =
64 narrowLocdArgs (narrowing, mkLocatedArg (arg, loc))
65 | mkLocatedArg (arg, SA.COMBINE (l1, l2)) = let
66 val locdArgs1 = mkLocatedArg (arg, l1)
67 val locdArgs2 = mkLocatedArg (arg, l2)
68 in
69 combineLocdArgs (locdArgs1 @ locdArgs2)
70 end
71
72 (* converts a sequence of arguments and staged allocation locations to located arguments *)
73 val mkLocatedArgs = List.concat o ListPair.mapEq mkLocatedArg
74
75 fun k2s CK.GPR = "GPR"
76 | k2s CK.FPR = "FPR"
77 | k2s CK.STK = "STK"
78 | k2s CK.FSTK = "FSTK"
79 val i2s = Int.toString
80
81 fun toString {k, width, narrowing, offset, arg, loc} =
82 "{k="^k2s k^",width="^i2s width^",narrowing="^i2s (Option.getOpt(narrowing, width))^",offset="^i2s offset^",loc="^i2s loc^"}"
83
84 end