annotate src/cjrize.sml @ 117:94856a3b4752

Serving pages
author Adam Chlipala <adamc@hcoop.net>
date Sun, 13 Jul 2008 15:44:00 -0400
parents c5d7ce9ddd57
children 6230bdd122e7
rev   line source
adamc@29 1 (* Copyright (c) 2008, Adam Chlipala
adamc@29 2 * All rights reserved.
adamc@29 3 *
adamc@29 4 * Redistribution and use in source and binary forms, with or without
adamc@29 5 * modification, are permitted provided that the following conditions are met:
adamc@29 6 *
adamc@29 7 * - Redistributions of source code must retain the above copyright notice,
adamc@29 8 * this list of conditions and the following disclaimer.
adamc@29 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@29 10 * this list of conditions and the following disclaimer in the documentation
adamc@29 11 * and/or other materials provided with the distribution.
adamc@29 12 * - The names of contributors may not be used to endorse or promote products
adamc@29 13 * derived from this software without specific prior written permission.
adamc@29 14 *
adamc@29 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@29 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@29 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@29 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@29 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@29 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@29 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@29 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@29 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@29 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@29 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@29 26 *)
adamc@29 27
adamc@29 28 structure Cjrize :> CJRIZE = struct
adamc@29 29
adamc@109 30 structure L = Mono
adamc@29 31 structure L' = Cjr
adamc@29 32
adamc@29 33 structure Sm :> sig
adamc@29 34 type t
adamc@29 35
adamc@29 36 val empty : t
adamc@29 37 val find : t * (string * L.typ) list * (string * L'.typ) list -> t * int
adamc@29 38
adamc@29 39 val declares : t -> (int * (string * L'.typ) list) list
adamc@29 40 end = struct
adamc@29 41
adamc@29 42 structure FM = BinaryMapFn(struct
adamc@29 43 type ord_key = L.typ
adamc@109 44 val compare = MonoUtil.Typ.compare
adamc@29 45 end)
adamc@29 46
adamc@29 47 type t = int * int FM.map * (int * (string * L'.typ) list) list
adamc@29 48
adamc@102 49 val empty : t = (1, FM.insert (FM.empty, (L.TRecord [], ErrorMsg.dummySpan), 0), [])
adamc@29 50
adamc@29 51 fun find ((n, m, ds), xts, xts') =
adamc@29 52 let
adamc@29 53 val t = (L.TRecord xts, ErrorMsg.dummySpan)
adamc@29 54 in
adamc@29 55 case FM.find (m, t) of
adamc@29 56 NONE => ((n+1, FM.insert (m, t, n), (n, xts') :: ds), n)
adamc@29 57 | SOME i => ((n, m, ds), i)
adamc@29 58 end
adamc@29 59
adamc@29 60 fun declares (_, _, ds) = ds
adamc@29 61
adamc@29 62 end
adamc@29 63
adamc@29 64 fun cifyTyp ((t, loc), sm) =
adamc@29 65 case t of
adamc@109 66 L.TFun (t1, t2) =>
adamc@29 67 let
adamc@29 68 val (t1, sm) = cifyTyp (t1, sm)
adamc@29 69 val (t2, sm) = cifyTyp (t2, sm)
adamc@29 70 in
adamc@109 71 ((L'.TFun (t1, t2), loc), sm)
adamc@29 72 end
adamc@29 73 | L.TRecord xts =>
adamc@29 74 let
adamc@29 75 val old_xts = xts
adamc@29 76 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
adamc@29 77 let
adamc@29 78 val (t, sm) = cifyTyp (t, sm)
adamc@29 79 in
adamc@29 80 ((x, t), sm)
adamc@29 81 end)
adamc@29 82 sm xts
adamc@29 83 val (sm, si) = Sm.find (sm, old_xts, xts)
adamc@29 84 in
adamc@29 85 ((L'.TRecord si, loc), sm)
adamc@29 86 end
adamc@29 87 | L.TNamed n => ((L'.TNamed n, loc), sm)
adamc@53 88 | L.TFfi mx => ((L'.TFfi mx, loc), sm)
adamc@29 89
adamc@109 90 val dummye = (L'.EPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@109 91
adamc@29 92 fun cifyExp ((e, loc), sm) =
adamc@29 93 case e of
adamc@29 94 L.EPrim p => ((L'.EPrim p, loc), sm)
adamc@29 95 | L.ERel n => ((L'.ERel n, loc), sm)
adamc@29 96 | L.ENamed n => ((L'.ENamed n, loc), sm)
adamc@53 97 | L.EFfi mx => ((L'.EFfi mx, loc), sm)
adamc@53 98 | L.EFfiApp (m, x, es) =>
adamc@53 99 let
adamc@53 100 val (es, sm) = ListUtil.foldlMap cifyExp sm es
adamc@53 101 in
adamc@53 102 ((L'.EFfiApp (m, x, es), loc), sm)
adamc@53 103 end
adamc@29 104 | L.EApp (e1, e2) =>
adamc@29 105 let
adamc@29 106 val (e1, sm) = cifyExp (e1, sm)
adamc@29 107 val (e2, sm) = cifyExp (e2, sm)
adamc@29 108 in
adamc@29 109 ((L'.EApp (e1, e2), loc), sm)
adamc@29 110 end
adamc@109 111 | L.EAbs _ => (ErrorMsg.errorAt loc "Anonymous function remains at code generation";
adamc@109 112 (dummye, sm))
adamc@29 113
adamc@29 114 | L.ERecord xes =>
adamc@29 115 let
adamc@29 116 val old_xts = map (fn (x, _, t) => (x, t)) xes
adamc@29 117
adamc@29 118 val (xets, sm) = ListUtil.foldlMap (fn ((x, e, t), sm) =>
adamc@29 119 let
adamc@29 120 val (e, sm) = cifyExp (e, sm)
adamc@29 121 val (t, sm) = cifyTyp (t, sm)
adamc@29 122 in
adamc@29 123 ((x, e, t), sm)
adamc@29 124 end)
adamc@29 125 sm xes
adamc@29 126
adamc@29 127 val (sm, si) = Sm.find (sm, old_xts, map (fn (x, _, t) => (x, t)) xets)
adamc@29 128
adamc@29 129 val xes = map (fn (x, e, _) => (x, e)) xets
adamc@29 130 val xes = ListMergeSort.sort (fn ((x1, _), (x2, _)) => String.compare (x1, x2) = GREATER) xes
adamc@29 131 in
adamc@29 132 ((L'.ERecord (si, xes), loc), sm)
adamc@29 133 end
adamc@29 134 | L.EField (e, x) =>
adamc@29 135 let
adamc@29 136 val (e, sm) = cifyExp (e, sm)
adamc@29 137 in
adamc@29 138 ((L'.EField (e, x), loc), sm)
adamc@29 139 end
adamc@29 140
adamc@102 141 | L.EStrcat _ => raise Fail "Cjrize EStrcat"
adamc@102 142
adamc@102 143 | L.EWrite e =>
adamc@102 144 let
adamc@102 145 val (e, sm) = cifyExp (e, sm)
adamc@102 146 in
adamc@102 147 ((L'.EWrite e, loc), sm)
adamc@102 148 end
adamc@102 149
adamc@106 150 | L.ESeq (e1, e2) =>
adamc@106 151 let
adamc@106 152 val (e1, sm) = cifyExp (e1, sm)
adamc@106 153 val (e2, sm) = cifyExp (e2, sm)
adamc@106 154 in
adamc@106 155 ((L'.ESeq (e1, e2), loc), sm)
adamc@106 156 end
adamc@106 157
adamc@111 158 | L.EClosure _ => (ErrorMsg.errorAt loc "Nested closure remains in code generation";
adamc@111 159 (dummye, sm))
adamc@111 160
adamc@29 161 fun cifyDecl ((d, loc), sm) =
adamc@29 162 case d of
adamc@109 163 L.DVal (x, n, t, e, _) =>
adamc@29 164 let
adamc@29 165 val (t, sm) = cifyTyp (t, sm)
adamc@109 166
adamc@109 167 val (d, sm) = case #1 t of
adamc@109 168 L'.TFun (dom, ran) =>
adamc@109 169 (case #1 e of
adamc@109 170 L.EAbs (ax, _, _, e) =>
adamc@109 171 let
adamc@109 172 val (e, sm) = cifyExp (e, sm)
adamc@109 173 in
adamc@109 174 (L'.DFun (x, n, ax, dom, ran, e), sm)
adamc@109 175 end
adamc@109 176 | _ => (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
adamc@109 177 (L'.DVal ("", 0, t, (L'.EPrim (Prim.Int 0), ErrorMsg.dummySpan)), sm)))
adamc@109 178 | _ =>
adamc@109 179 let
adamc@109 180 val (e, sm) = cifyExp (e, sm)
adamc@109 181 in
adamc@109 182 (L'.DVal (x, n, t, e), sm)
adamc@109 183 end
adamc@29 184 in
adamc@109 185 (SOME (d, loc), NONE, sm)
adamc@29 186 end
adamc@116 187 | L.DExport (s, n) => (NONE, SOME ("/" ^ s, n), sm)
adamc@29 188
adamc@29 189 fun cjrize ds =
adamc@29 190 let
adamc@101 191 val (ds, ps, sm) = foldl (fn (d, (ds, ps, sm)) =>
adamc@101 192 let
adamc@101 193 val (dop, pop, sm) = cifyDecl (d, sm)
adamc@101 194 val ds = case dop of
adamc@101 195 NONE => ds
adamc@101 196 | SOME d => d :: ds
adamc@101 197 val ps = case pop of
adamc@101 198 NONE => ps
adamc@101 199 | SOME p => p :: ps
adamc@101 200 in
adamc@101 201 (ds, ps, sm)
adamc@101 202 end)
adamc@101 203 ([], [], Sm.empty) ds
adamc@29 204 in
adamc@101 205 (List.revAppend (map (fn v => (L'.DStruct v, ErrorMsg.dummySpan)) (Sm.declares sm),
adamc@101 206 rev ds),
adamc@101 207 ps)
adamc@29 208 end
adamc@29 209
adamc@29 210 end