annotate src/cjrize.sml @ 181:31dfab1d4050

Cjrize ECon
author Adam Chlipala <adamc@hcoop.net>
date Sun, 03 Aug 2008 11:17:33 -0400
parents c7a5c8e0a0e0
children d11754ffe252
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@168 87 | L.TDatatype (n, xncs) =>
adamc@168 88 let
adamc@168 89 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
adamc@168 90 case to of
adamc@168 91 NONE => ((x, n, NONE), sm)
adamc@168 92 | SOME t =>
adamc@168 93 let
adamc@168 94 val (t, sm) = cifyTyp (t, sm)
adamc@168 95 in
adamc@168 96 ((x, n, SOME t), sm)
adamc@168 97 end)
adamc@168 98 sm xncs
adamc@168 99 in
adamc@168 100 ((L'.TDatatype (n, xncs), loc), sm)
adamc@168 101 end
adamc@53 102 | L.TFfi mx => ((L'.TFfi mx, loc), sm)
adamc@29 103
adamc@109 104 val dummye = (L'.EPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@109 105
adamc@181 106 fun cifyPatCon pc =
adamc@181 107 case pc of
adamc@181 108 L.PConVar n => L'.PConVar n
adamc@181 109 | L.PConFfi mx => L'.PConFfi mx
adamc@181 110
adamc@181 111 fun cifyPat (p, loc) =
adamc@181 112 case p of
adamc@181 113 L.PWild => (L'.PWild, loc)
adamc@181 114 | L.PVar x => (L'.PVar x, loc)
adamc@181 115 | L.PPrim p => (L'.PPrim p, loc)
adamc@181 116 | L.PCon (pc, po) => (L'.PCon (cifyPatCon pc, Option.map cifyPat po), loc)
adamc@181 117 | L.PRecord xps => (L'.PRecord (map (fn (x, p) => (x, cifyPat p)) xps), loc)
adamc@181 118
adamc@29 119 fun cifyExp ((e, loc), sm) =
adamc@29 120 case e of
adamc@29 121 L.EPrim p => ((L'.EPrim p, loc), sm)
adamc@29 122 | L.ERel n => ((L'.ERel n, loc), sm)
adamc@29 123 | L.ENamed n => ((L'.ENamed n, loc), sm)
adamc@181 124 | L.ECon (n, eo) =>
adamc@181 125 let
adamc@181 126 val (eo, sm) =
adamc@181 127 case eo of
adamc@181 128 NONE => (NONE, sm)
adamc@181 129 | SOME e =>
adamc@181 130 let
adamc@181 131 val (e, sm) = cifyExp (e, sm)
adamc@181 132 in
adamc@181 133 (SOME e, sm)
adamc@181 134 end
adamc@181 135 in
adamc@181 136 ((L'.ECon (n, eo), loc), sm)
adamc@181 137 end
adamc@53 138 | L.EFfi mx => ((L'.EFfi mx, loc), sm)
adamc@53 139 | L.EFfiApp (m, x, es) =>
adamc@53 140 let
adamc@53 141 val (es, sm) = ListUtil.foldlMap cifyExp sm es
adamc@53 142 in
adamc@53 143 ((L'.EFfiApp (m, x, es), loc), sm)
adamc@53 144 end
adamc@29 145 | L.EApp (e1, e2) =>
adamc@29 146 let
adamc@29 147 val (e1, sm) = cifyExp (e1, sm)
adamc@29 148 val (e2, sm) = cifyExp (e2, sm)
adamc@29 149 in
adamc@29 150 ((L'.EApp (e1, e2), loc), sm)
adamc@29 151 end
adamc@109 152 | L.EAbs _ => (ErrorMsg.errorAt loc "Anonymous function remains at code generation";
adamc@109 153 (dummye, sm))
adamc@29 154
adamc@29 155 | L.ERecord xes =>
adamc@29 156 let
adamc@29 157 val old_xts = map (fn (x, _, t) => (x, t)) xes
adamc@29 158
adamc@29 159 val (xets, sm) = ListUtil.foldlMap (fn ((x, e, t), sm) =>
adamc@29 160 let
adamc@29 161 val (e, sm) = cifyExp (e, sm)
adamc@29 162 val (t, sm) = cifyTyp (t, sm)
adamc@29 163 in
adamc@29 164 ((x, e, t), sm)
adamc@29 165 end)
adamc@29 166 sm xes
adamc@29 167
adamc@29 168 val (sm, si) = Sm.find (sm, old_xts, map (fn (x, _, t) => (x, t)) xets)
adamc@29 169
adamc@29 170 val xes = map (fn (x, e, _) => (x, e)) xets
adamc@29 171 val xes = ListMergeSort.sort (fn ((x1, _), (x2, _)) => String.compare (x1, x2) = GREATER) xes
adamc@29 172 in
adamc@29 173 ((L'.ERecord (si, xes), loc), sm)
adamc@29 174 end
adamc@29 175 | L.EField (e, x) =>
adamc@29 176 let
adamc@29 177 val (e, sm) = cifyExp (e, sm)
adamc@29 178 in
adamc@29 179 ((L'.EField (e, x), loc), sm)
adamc@29 180 end
adamc@29 181
adamc@181 182 | L.ECase (e, pes, t) =>
adamc@181 183 let
adamc@181 184 val (e, sm) = cifyExp (e, sm)
adamc@181 185 val (pes, sm) = ListUtil.foldlMap
adamc@181 186 (fn ((p, e), sm) =>
adamc@181 187 let
adamc@181 188 val (e, sm) = cifyExp (e, sm)
adamc@181 189 in
adamc@181 190 ((cifyPat p, e), sm)
adamc@181 191 end) sm pes
adamc@181 192 val (t, sm) = cifyTyp (t, sm)
adamc@181 193 in
adamc@181 194 ((L'.ECase (e, pes, t), loc), sm)
adamc@181 195 end
adamc@178 196
adamc@180 197 | L.EStrcat (e1, e2) =>
adamc@180 198 let
adamc@180 199 val (e1, sm) = cifyExp (e1, sm)
adamc@180 200 val (e2, sm) = cifyExp (e2, sm)
adamc@180 201 in
adamc@180 202 ((L'.EFfiApp ("Basis", "strcat", [e1, e2]), loc), sm)
adamc@180 203 end
adamc@102 204
adamc@102 205 | L.EWrite e =>
adamc@102 206 let
adamc@102 207 val (e, sm) = cifyExp (e, sm)
adamc@102 208 in
adamc@102 209 ((L'.EWrite e, loc), sm)
adamc@102 210 end
adamc@102 211
adamc@106 212 | L.ESeq (e1, e2) =>
adamc@106 213 let
adamc@106 214 val (e1, sm) = cifyExp (e1, sm)
adamc@106 215 val (e2, sm) = cifyExp (e2, sm)
adamc@106 216 in
adamc@106 217 ((L'.ESeq (e1, e2), loc), sm)
adamc@106 218 end
adamc@106 219
adamc@111 220 | L.EClosure _ => (ErrorMsg.errorAt loc "Nested closure remains in code generation";
adamc@111 221 (dummye, sm))
adamc@111 222
adamc@29 223 fun cifyDecl ((d, loc), sm) =
adamc@29 224 case d of
adamc@165 225 L.DDatatype (x, n, xncs) =>
adamc@165 226 let
adamc@165 227 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
adamc@165 228 case to of
adamc@165 229 NONE => ((x, n, NONE), sm)
adamc@165 230 | SOME t =>
adamc@165 231 let
adamc@165 232 val (t, sm) = cifyTyp (t, sm)
adamc@165 233 in
adamc@165 234 ((x, n, SOME t), sm)
adamc@165 235 end) sm xncs
adamc@165 236 in
adamc@165 237 (SOME (L'.DDatatype (x, n, xncs), loc), NONE, sm)
adamc@165 238 end
adamc@164 239
adamc@164 240 | L.DVal (x, n, t, e, _) =>
adamc@29 241 let
adamc@29 242 val (t, sm) = cifyTyp (t, sm)
adamc@109 243
adamc@109 244 val (d, sm) = case #1 t of
adamc@121 245 L'.TFun _ =>
adamc@121 246 let
adamc@121 247 fun unravel (tAll as (t, _), eAll as (e, _)) =
adamc@121 248 case (t, e) of
adamc@121 249 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
adamc@121 250 let
adamc@121 251 val (args, t, e) = unravel (ran, e)
adamc@121 252 in
adamc@121 253 ((ax, dom) :: args, t, e)
adamc@121 254 end
adamc@121 255 | (L'.TFun _, _) =>
adamc@121 256 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
adamc@121 257 ([], tAll, eAll))
adamc@121 258 | _ => ([], tAll, eAll)
adamc@121 259
adamc@121 260 val (args, ran, e) = unravel (t, e)
adamc@121 261 val (e, sm) = cifyExp (e, sm)
adamc@121 262 in
adamc@121 263 (L'.DFun (x, n, args, ran, e), sm)
adamc@121 264 end
adamc@121 265
adamc@109 266 | _ =>
adamc@109 267 let
adamc@109 268 val (e, sm) = cifyExp (e, sm)
adamc@109 269 in
adamc@109 270 (L'.DVal (x, n, t, e), sm)
adamc@109 271 end
adamc@29 272 in
adamc@109 273 (SOME (d, loc), NONE, sm)
adamc@29 274 end
adamc@129 275 | L.DValRec vis =>
adamc@129 276 let
adamc@129 277 val (vis, sm) = ListUtil.foldlMap
adamc@129 278 (fn ((x, n, t, e, _), sm) =>
adamc@129 279 let
adamc@129 280 val (t, sm) = cifyTyp (t, sm)
adamc@129 281
adamc@129 282 fun unravel (tAll as (t, _), eAll as (e, _)) =
adamc@129 283 case (t, e) of
adamc@129 284 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
adamc@129 285 let
adamc@129 286 val (args, t, e) = unravel (ran, e)
adamc@129 287 in
adamc@129 288 ((ax, dom) :: args, t, e)
adamc@129 289 end
adamc@129 290 | (L'.TFun _, _) =>
adamc@129 291 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
adamc@129 292 ([], tAll, eAll))
adamc@129 293 | _ => ([], tAll, eAll)
adamc@129 294
adamc@129 295 val (args, ran, e) = unravel (t, e)
adamc@129 296 val (e, sm) = cifyExp (e, sm)
adamc@129 297 in
adamc@129 298 ((x, n, args, ran, e), sm)
adamc@129 299 end)
adamc@129 300 sm vis
adamc@129 301 in
adamc@129 302 (SOME (L'.DFunRec vis, loc), NONE, sm)
adamc@129 303 end
adamc@129 304
adamc@144 305 | L.DExport (ek, s, n, ts) =>
adamc@120 306 let
adamc@120 307 val (ts, sm) = ListUtil.foldlMap cifyTyp sm ts
adamc@120 308 in
adamc@144 309 (NONE, SOME (ek, "/" ^ s, n, ts), sm)
adamc@120 310 end
adamc@29 311
adamc@29 312 fun cjrize ds =
adamc@29 313 let
adamc@101 314 val (ds, ps, sm) = foldl (fn (d, (ds, ps, sm)) =>
adamc@101 315 let
adamc@101 316 val (dop, pop, sm) = cifyDecl (d, sm)
adamc@101 317 val ds = case dop of
adamc@101 318 NONE => ds
adamc@101 319 | SOME d => d :: ds
adamc@101 320 val ps = case pop of
adamc@101 321 NONE => ps
adamc@101 322 | SOME p => p :: ps
adamc@101 323 in
adamc@101 324 (ds, ps, sm)
adamc@101 325 end)
adamc@101 326 ([], [], Sm.empty) ds
adamc@29 327 in
adamc@101 328 (List.revAppend (map (fn v => (L'.DStruct v, ErrorMsg.dummySpan)) (Sm.declares sm),
adamc@101 329 rev ds),
adamc@101 330 ps)
adamc@29 331 end
adamc@29 332
adamc@29 333 end