annotate src/especialize.sml @ 1793:446e73902b09

Fix JavaScript implementation of strcspn()
author Adam Chlipala <adam@chlipala.net>
date Tue, 24 Jul 2012 19:49:50 -0400
parents 92cfc69419bd
children 62c18ecbfec4
rev   line source
adam@1677 1 (* Copyright (c) 2008-2012, Adam Chlipala
adamc@443 2 * All rights reserved.
adamc@443 3 *
adamc@443 4 * Redistribution and use in source and binary forms, with or without
adamc@443 5 * modification, are permitted provided that the following conditions are met:
adamc@443 6 *
adamc@443 7 * - Redistributions of source code must retain the above copyright notice,
adamc@443 8 * this list of conditions and the following disclaimer.
adamc@443 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@443 10 * this list of conditions and the following disclaimer in the documentation
adamc@443 11 * and/or other materials provided with the distribution.
adamc@443 12 * - The names of contributors may not be used to endorse or promote products
adamc@443 13 * derived from this software without specific prior written permission.
adamc@443 14 *
adamc@443 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@443 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@443 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@443 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@443 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@443 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@443 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@443 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@443 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@443 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@443 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@443 26 *)
adamc@443 27
adamc@443 28 structure ESpecialize :> ESPECIALIZE = struct
adamc@443 29
adamc@443 30 open Core
adamc@443 31
adamc@443 32 structure E = CoreEnv
adamc@443 33 structure U = CoreUtil
adamc@443 34
adamc@479 35 type skey = exp
adamc@453 36
adamc@453 37 structure K = struct
adam@1314 38 type ord_key = con list * exp list
adam@1314 39 fun compare ((cs1, es1), (cs2, es2)) = Order.join (Order.joinL U.Con.compare (cs1, cs2),
adam@1314 40 fn () => Order.joinL U.Exp.compare (es1, es2))
adamc@443 41 end
adamc@443 42
adamc@453 43 structure KM = BinaryMapFn(K)
adamc@443 44 structure IM = IntBinaryMap
adamc@482 45 structure IS = IntBinarySet
adamc@443 46
adamc@626 47 val freeVars = U.Exp.foldB {kind = fn (_, _, xs) => xs,
adamc@488 48 con = fn (_, _, xs) => xs,
adamc@488 49 exp = fn (bound, e, xs) =>
adamc@488 50 case e of
adamc@488 51 ERel x =>
adamc@488 52 if x >= bound then
adamc@488 53 IS.add (xs, x - bound)
adamc@488 54 else
adamc@488 55 xs
adamc@488 56 | _ => xs,
adamc@488 57 bind = fn (bound, b) =>
adamc@488 58 case b of
adamc@488 59 U.Exp.RelE _ => bound + 1
adamc@488 60 | _ => bound}
adamc@488 61 0 IS.empty
adamc@479 62
adamc@1120 63 fun isPolyT (t, _) =
adamc@1120 64 case t of
adamc@1120 65 TFun (_, ran) => isPolyT ran
adamc@1120 66 | TCFun _ => true
adamc@1120 67 | TKFun _ => true
adamc@1120 68 | _ => false
adamc@1120 69
adamc@1120 70 fun isPoly (d, _) =
adamc@1120 71 case d of
adamc@1120 72 DVal (_, _, t, _, _) => isPolyT t
adamc@1120 73 | DValRec vis => List.exists (isPolyT o #3) vis
adamc@1120 74 | _ => false
adamc@522 75
adamc@488 76 fun positionOf (v : int, ls) =
adamc@488 77 let
adamc@488 78 fun pof (pos, ls) =
adamc@488 79 case ls of
adamc@488 80 [] => raise Fail "Defunc.positionOf"
adamc@488 81 | v' :: ls' =>
adamc@488 82 if v = v' then
adamc@488 83 pos
adamc@488 84 else
adamc@488 85 pof (pos + 1, ls')
adamc@488 86 in
adamc@488 87 pof (0, ls)
adamc@488 88 end
adamc@488 89
adamc@1079 90 fun squish fvs =
adamc@626 91 U.Exp.mapB {kind = fn _ => fn k => k,
adamc@488 92 con = fn _ => fn c => c,
adamc@488 93 exp = fn bound => fn e =>
adamc@479 94 case e of
adamc@488 95 ERel x =>
adamc@488 96 if x >= bound then
adamc@1079 97 ERel (positionOf (x - bound, fvs) + bound)
adamc@488 98 else
adamc@488 99 e
adamc@488 100 | _ => e,
adamc@488 101 bind = fn (bound, b) =>
adamc@488 102 case b of
adamc@488 103 U.Exp.RelE _ => bound + 1
adamc@488 104 | _ => bound}
adamc@488 105 0
adamc@453 106
adamc@443 107 type func = {
adamc@443 108 name : string,
adamc@453 109 args : int KM.map,
adamc@443 110 body : exp,
adamc@443 111 typ : con,
adam@1675 112 tag : string,
adam@1675 113 constArgs : int (* What length prefix of the arguments never vary across recursive calls? *)
adamc@443 114 }
adamc@443 115
adamc@443 116 type state = {
adamc@443 117 maxName : int,
adamc@443 118 funcs : func IM.map,
adamc@1079 119 decls : (string * int * con * exp * string) list,
adamc@1080 120 specialized : IS.set
adamc@443 121 }
adamc@443 122
adamc@488 123 fun default (_, x, st) = (x, st)
adamc@443 124
adam@1289 125 val functionInside = U.Con.exists {kind = fn _ => false,
adam@1289 126 con = fn TFun _ => true
adam@1289 127 | CFfi ("Basis", "transaction") => true
adam@1289 128 | CFfi ("Basis", "eq") => true
adam@1289 129 | CFfi ("Basis", "num") => true
adam@1289 130 | CFfi ("Basis", "ord") => true
adam@1289 131 | CFfi ("Basis", "show") => true
adam@1289 132 | CFfi ("Basis", "read") => true
adam@1289 133 | CFfi ("Basis", "sql_injectable_prim") => true
adam@1289 134 | CFfi ("Basis", "sql_injectable") => true
adam@1289 135 | _ => false}
adam@1289 136
adam@1675 137 fun getApp (e, _) =
adam@1675 138 case e of
adam@1675 139 ENamed f => SOME (f, [])
adam@1675 140 | EApp (e1, e2) =>
adam@1675 141 (case getApp e1 of
adam@1675 142 NONE => NONE
adam@1675 143 | SOME (f, xs) => SOME (f, xs @ [e2]))
adam@1675 144 | _ => NONE
adam@1675 145
adam@1675 146 val getApp = fn e => case getApp e of
adam@1675 147 v as SOME (_, _ :: _) => v
adam@1675 148 | _ => NONE
adam@1675 149
adam@1675 150 val maxInt = Option.getOpt (Int.maxInt, 9999)
adam@1675 151
adam@1766 152 fun calcConstArgs enclosingFunctions e =
adam@1675 153 let
adam@1675 154 fun ca depth e =
adam@1675 155 case #1 e of
adam@1675 156 EPrim _ => maxInt
adam@1675 157 | ERel _ => maxInt
adam@1766 158 | ENamed n => if IS.member (enclosingFunctions, n) then 0 else maxInt
adam@1675 159 | ECon (_, _, _, NONE) => maxInt
adam@1675 160 | ECon (_, _, _, SOME e) => ca depth e
adam@1675 161 | EFfi _ => maxInt
adam@1675 162 | EFfiApp (_, _, ecs) => foldl (fn ((e, _), d) => Int.min (ca depth e, d)) maxInt ecs
adam@1675 163 | EApp (e1, e2) =>
adam@1675 164 let
adam@1675 165 fun default () = Int.min (ca depth e1, ca depth e2)
adam@1675 166 in
adam@1675 167 case getApp e of
adam@1675 168 NONE => default ()
adam@1675 169 | SOME (f, args) =>
adam@1766 170 if not (IS.member (enclosingFunctions, f)) then
adam@1675 171 default ()
adam@1675 172 else
adam@1675 173 let
adam@1675 174 fun visitArgs (count, args) =
adam@1675 175 case args of
adam@1675 176 [] => count
adam@1675 177 | arg :: args' =>
adam@1675 178 let
adam@1675 179 fun default () = foldl (fn (e, d) => Int.min (ca depth e, d)) count args
adam@1675 180 in
adam@1675 181 case #1 arg of
adam@1675 182 ERel n =>
adam@1676 183 if n = depth - 1 - count then
adam@1675 184 visitArgs (count + 1, args')
adam@1675 185 else
adam@1675 186 default ()
adam@1675 187 | _ => default ()
adam@1675 188 end
adam@1675 189 in
adam@1675 190 visitArgs (0, args)
adam@1675 191 end
adam@1675 192 end
adam@1675 193 | EAbs (_, _, _, e1) => ca (depth + 1) e1
adam@1675 194 | ECApp (e1, _) => ca depth e1
adam@1675 195 | ECAbs (_, _, e1) => ca depth e1
adam@1675 196 | EKAbs (_, e1) => ca depth e1
adam@1675 197 | EKApp (e1, _) => ca depth e1
adam@1675 198 | ERecord xets => foldl (fn ((_, e, _), d) => Int.min (ca depth e, d)) maxInt xets
adam@1675 199 | EField (e1, _, _) => ca depth e1
adam@1675 200 | EConcat (e1, _, e2, _) => Int.min (ca depth e1, ca depth e2)
adam@1675 201 | ECut (e1, _, _) => ca depth e1
adam@1675 202 | ECutMulti (e1, _, _) => ca depth e1
adam@1675 203 | ECase (e1, pes, _) => foldl (fn ((p, e), d) => Int.min (ca (depth + E.patBindsN p) e, d)) (ca depth e1) pes
adam@1675 204 | EWrite e1 => ca depth e1
adam@1675 205 | EClosure (_, es) => foldl (fn (e, d) => Int.min (ca depth e, d)) maxInt es
adam@1675 206 | ELet (_, _, e1, e2) => Int.min (ca depth e1, ca (depth + 1) e2)
adam@1675 207 | EServerCall (_, es, _) => foldl (fn (e, d) => Int.min (ca depth e, d)) maxInt es
adam@1675 208
adam@1675 209 fun enterAbs depth e =
adam@1675 210 case #1 e of
adam@1675 211 EAbs (_, _, _, e1) => enterAbs (depth + 1) e1
adam@1675 212 | _ => ca depth e
adam@1675 213 in
adam@1677 214 enterAbs 0 e
adam@1675 215 end
adam@1675 216
adam@1675 217
adamc@1080 218 fun specialize' (funcs, specialized) file =
adamc@443 219 let
adamc@488 220 fun bind (env, b) =
adamc@488 221 case b of
adamc@521 222 U.Decl.RelE xt => xt :: env
adamc@521 223 | _ => env
adamc@488 224
adamc@1080 225 fun exp (env, e as (_, loc), st : state) =
adamc@482 226 let
adamc@721 227 (*val () = Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty
adamc@721 228 (e, ErrorMsg.dummySpan))]*)
adamc@721 229
adamc@1080 230 fun default () =
adamc@1080 231 case #1 e of
adamc@1080 232 EPrim _ => (e, st)
adamc@1080 233 | ERel _ => (e, st)
adamc@1080 234 | ENamed _ => (e, st)
adamc@1080 235 | ECon (_, _, _, NONE) => (e, st)
adamc@1080 236 | ECon (dk, pc, cs, SOME e) =>
adamc@1080 237 let
adamc@1080 238 val (e, st) = exp (env, e, st)
adamc@1080 239 in
adamc@1080 240 ((ECon (dk, pc, cs, SOME e), loc), st)
adamc@1080 241 end
adamc@1080 242 | EFfi _ => (e, st)
adamc@1080 243 | EFfiApp (m, x, es) =>
adamc@1080 244 let
adam@1663 245 val (es, st) = ListUtil.foldlMap (fn ((e, t), st) =>
adam@1663 246 let
adam@1663 247 val (e, st) = exp (env, e, st)
adam@1663 248 in
adam@1663 249 ((e, t), st)
adam@1663 250 end) st es
adamc@1080 251 in
adamc@1080 252 ((EFfiApp (m, x, es), loc), st)
adamc@1080 253 end
adamc@1080 254 | EApp (e1, e2) =>
adamc@1080 255 let
adamc@1080 256 val (e1, st) = exp (env, e1, st)
adamc@1080 257 val (e2, st) = exp (env, e2, st)
adamc@1080 258 in
adamc@1080 259 ((EApp (e1, e2), loc), st)
adamc@1080 260 end
adamc@1080 261 | EAbs (x, d, r, e) =>
adamc@1080 262 let
adamc@1080 263 val (e, st) = exp ((x, d) :: env, e, st)
adamc@1080 264 in
adamc@1080 265 ((EAbs (x, d, r, e), loc), st)
adamc@1080 266 end
adamc@1080 267 | ECApp (e, c) =>
adamc@1080 268 let
adamc@1080 269 val (e, st) = exp (env, e, st)
adamc@1080 270 in
adamc@1080 271 ((ECApp (e, c), loc), st)
adamc@1080 272 end
adamc@1185 273 | ECAbs _ => (e, st)
adamc@1120 274 | EKAbs _ => (e, st)
adamc@1080 275 | EKApp (e, k) =>
adamc@1080 276 let
adamc@1080 277 val (e, st) = exp (env, e, st)
adamc@1080 278 in
adamc@1080 279 ((EKApp (e, k), loc), st)
adamc@1080 280 end
adamc@1080 281 | ERecord fs =>
adamc@1080 282 let
adamc@1080 283 val (fs, st) = ListUtil.foldlMap (fn ((c1, e, c2), st) =>
adamc@1080 284 let
adamc@1080 285 val (e, st) = exp (env, e, st)
adamc@1080 286 in
adamc@1080 287 ((c1, e, c2), st)
adamc@1080 288 end) st fs
adamc@1080 289 in
adamc@1080 290 ((ERecord fs, loc), st)
adamc@1080 291 end
adamc@1080 292 | EField (e, c, cs) =>
adamc@1080 293 let
adamc@1080 294 val (e, st) = exp (env, e, st)
adamc@1080 295 in
adamc@1080 296 ((EField (e, c, cs), loc), st)
adamc@1080 297 end
adamc@1080 298 | EConcat (e1, c1, e2, c2) =>
adamc@1080 299 let
adamc@1080 300 val (e1, st) = exp (env, e1, st)
adamc@1080 301 val (e2, st) = exp (env, e2, st)
adamc@1080 302 in
adamc@1080 303 ((EConcat (e1, c1, e2, c2), loc), st)
adamc@1080 304 end
adamc@1080 305 | ECut (e, c, cs) =>
adamc@1080 306 let
adamc@1080 307 val (e, st) = exp (env, e, st)
adamc@1080 308 in
adamc@1080 309 ((ECut (e, c, cs), loc), st)
adamc@1080 310 end
adamc@1080 311 | ECutMulti (e, c, cs) =>
adamc@1080 312 let
adamc@1080 313 val (e, st) = exp (env, e, st)
adamc@1080 314 in
adamc@1080 315 ((ECutMulti (e, c, cs), loc), st)
adamc@1080 316 end
adamc@1080 317
adamc@1080 318 | ECase (e, pes, cs) =>
adamc@1080 319 let
adamc@1080 320 val (e, st) = exp (env, e, st)
adamc@1080 321 val (pes, st) = ListUtil.foldlMap (fn ((p, e), st) =>
adamc@1080 322 let
adamc@1080 323 val (e, st) = exp (E.patBindsL p @ env, e, st)
adamc@1080 324 in
adamc@1080 325 ((p, e), st)
adamc@1080 326 end) st pes
adamc@1080 327 in
adamc@1080 328 ((ECase (e, pes, cs), loc), st)
adamc@1080 329 end
adamc@1080 330
adamc@1080 331 | EWrite e =>
adamc@1080 332 let
adamc@1080 333 val (e, st) = exp (env, e, st)
adamc@1080 334 in
adamc@1080 335 ((EWrite e, loc), st)
adamc@1080 336 end
adamc@1080 337 | EClosure (n, es) =>
adamc@1080 338 let
adamc@1080 339 val (es, st) = ListUtil.foldlMap (fn (e, st) => exp (env, e, st)) st es
adamc@1080 340 in
adamc@1080 341 ((EClosure (n, es), loc), st)
adamc@1080 342 end
adamc@1080 343 | ELet (x, t, e1, e2) =>
adamc@1080 344 let
adamc@1080 345 val (e1, st) = exp (env, e1, st)
adamc@1080 346 val (e2, st) = exp ((x, t) :: env, e2, st)
adamc@1080 347 in
adamc@1080 348 ((ELet (x, t, e1, e2), loc), st)
adamc@1080 349 end
adamc@1080 350 | EServerCall (n, es, t) =>
adamc@1080 351 let
adamc@1080 352 val (es, st) = ListUtil.foldlMap (fn (e, st) => exp (env, e, st)) st es
adamc@1080 353 in
adamc@1080 354 ((EServerCall (n, es, t), loc), st)
adamc@1080 355 end
adamc@482 356 in
adamc@482 357 case getApp e of
adamc@1080 358 NONE => default ()
adamc@488 359 | SOME (f, xs) =>
adamc@485 360 case IM.find (#funcs st, f) of
adamc@1272 361 NONE => ((*print ("No find: " ^ Int.toString f ^ "\n");*) default ())
adam@1675 362 | SOME {name, args, body, typ, tag, constArgs} =>
adamc@488 363 let
adamc@1080 364 val (xs, st) = ListUtil.foldlMap (fn (e, st) => exp (env, e, st)) st xs
adamc@1080 365
adamc@721 366 (*val () = Print.prefaces "Consider" [("e", CorePrint.p_exp CoreEnv.empty
adamc@721 367 (e, ErrorMsg.dummySpan))]*)
adamc@721 368
adamc@488 369 val loc = ErrorMsg.dummySpan
adamc@488 370
adam@1677 371 val oldXs = xs
adam@1677 372
adam@1675 373 fun findSplit av (constArgs, xs, typ, fxs, fvs) =
adamc@488 374 case (#1 typ, xs) of
adamc@488 375 (TFun (dom, ran), e :: xs') =>
adam@1675 376 if constArgs > 0 then
adam@1677 377 if functionInside dom then
adam@1677 378 (rev (e :: fxs), xs', IS.union (fvs, freeVars e))
adam@1677 379 else
adam@1677 380 findSplit av (constArgs - 1,
adam@1677 381 xs',
adam@1677 382 ran,
adam@1677 383 e :: fxs,
adam@1677 384 IS.union (fvs, freeVars e))
adam@1675 385 else
adam@1677 386 ([], oldXs, IS.empty)
adam@1677 387 | _ => ([], oldXs, IS.empty)
adamc@488 388
adam@1675 389 val (fxs, xs, fvs) = findSplit true (constArgs, xs, typ, [], IS.empty)
adam@1355 390
adam@1314 391 val vts = map (fn n => #2 (List.nth (env, n))) (IS.listItems fvs)
adamc@1079 392 val fxs' = map (squish (IS.listItems fvs)) fxs
adam@1362 393
adam@1362 394 val p_bool = Print.PD.string o Bool.toString
adamc@488 395 in
adam@1355 396 (*Print.prefaces "Func" [("name", Print.PD.string name),
adam@1355 397 ("e", CorePrint.p_exp CoreEnv.empty e),
adam@1355 398 ("fxs'", Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')];*)
adam@1675 399 if List.all (fn (ERel _, _) => true
adam@1675 400 | _ => false) fxs' then
adam@1675 401 default ()
adamc@488 402 else
adam@1667 403 case KM.find (args, (vts, fxs')) of
adam@1667 404 SOME f' =>
adamc@485 405 let
adamc@488 406 val e = (ENamed f', loc)
adamc@488 407 val e = IS.foldr (fn (arg, e) => (EApp (e, (ERel arg, loc)), loc))
adamc@488 408 e fvs
adamc@1079 409 val e = foldl (fn (arg, e) => (EApp (e, arg), loc))
adamc@488 410 e xs
adamc@488 411 in
adamc@488 412 (*Print.prefaces "Brand new (reuse)"
adamc@721 413 [("e'", CorePrint.p_exp CoreEnv.empty e)];*)
adamc@1080 414 (e, st)
adamc@488 415 end
adam@1667 416 | NONE =>
adamc@488 417 let
adamc@800 418 (*val () = Print.prefaces "New one"
adam@1667 419 [("name", Print.PD.string name),
adam@1667 420 ("f", Print.PD.string (Int.toString f)),
adam@1667 421 ("|fvs|", Print.PD.string (Int.toString (IS.numItems fvs))),
adam@1667 422 ("|fxs|", Print.PD.string (Int.toString (length fxs))),
adam@1766 423 ("fxs'", Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs'),
adam@1667 424 ("spec", Print.PD.string (Bool.toString (IS.member (#specialized st, f))))]*)
adamc@800 425
adamc@818 426 (*val () = Print.prefaces ("Yes(" ^ name ^ ")")
adamc@818 427 [("fxs'",
adamc@818 428 Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')]*)
adamc@818 429
adam@1675 430 (*val () = Print.prefaces name
adam@1675 431 [("Available", Print.PD.string (Int.toString constArgs)),
adam@1675 432 ("Used", Print.PD.string (Int.toString (length fxs'))),
adam@1675 433 ("fxs'",
adam@1675 434 Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')]*)
adam@1675 435
adamc@1079 436 fun subBody (body, typ, fxs') =
adamc@1079 437 case (#1 body, #1 typ, fxs') of
adamc@488 438 (_, _, []) => SOME (body, typ)
adamc@1079 439 | (EAbs (_, _, _, body'), TFun (_, typ'), x :: fxs'') =>
adamc@488 440 let
adamc@1079 441 val body'' = E.subExpInExp (0, x) body'
adamc@488 442 in
adamc@488 443 subBody (body'',
adamc@488 444 typ',
adamc@1079 445 fxs'')
adamc@488 446 end
adamc@488 447 | _ => NONE
adamc@488 448 in
adamc@1079 449 case subBody (body, typ, fxs') of
adamc@1080 450 NONE => default ()
adamc@488 451 | SOME (body', typ') =>
adamc@488 452 let
adamc@488 453 val f' = #maxName st
adam@1314 454 val args = KM.insert (args, (vts, fxs'), f')
adamc@488 455 val funcs = IM.insert (#funcs st, f, {name = name,
adamc@488 456 args = args,
adamc@488 457 body = body,
adamc@488 458 typ = typ,
adam@1675 459 tag = tag,
adam@1766 460 constArgs = calcConstArgs (IS.singleton f) body})
adamc@1079 461
adamc@488 462 val st = {
adamc@488 463 maxName = f' + 1,
adamc@488 464 funcs = funcs,
adamc@1079 465 decls = #decls st,
adamc@1080 466 specialized = IS.add (#specialized st, f')
adamc@488 467 }
adamc@487 468
adamc@488 469 (*val () = Print.prefaces "specExp"
adamc@488 470 [("f", CorePrint.p_exp env (ENamed f, loc)),
adamc@488 471 ("f'", CorePrint.p_exp env (ENamed f', loc)),
adamc@488 472 ("xs", Print.p_list (CorePrint.p_exp env) xs),
adamc@488 473 ("fxs'", Print.p_list
adamc@488 474 (CorePrint.p_exp E.empty) fxs'),
adamc@488 475 ("e", CorePrint.p_exp env (e, loc))]*)
adamc@488 476 val (body', typ') = IS.foldl (fn (n, (body', typ')) =>
adamc@488 477 let
adamc@521 478 val (x, xt) = List.nth (env, n)
adamc@488 479 in
adamc@488 480 ((EAbs (x, xt, typ', body'),
adamc@488 481 loc),
adamc@488 482 (TFun (xt, typ'), loc))
adamc@488 483 end)
adamc@488 484 (body', typ') fvs
adamc@1272 485 (*val () = print ("NEW: " ^ name ^ "__" ^ Int.toString f' ^ "\n");*)
adamc@1272 486 val body' = ReduceLocal.reduceExp body'
adamc@1080 487 (*val () = Print.preface ("PRE", CorePrint.p_exp CoreEnv.empty body')*)
adamc@1080 488 val (body', st) = exp (env, body', st)
adamc@482 489
adamc@488 490 val e' = (ENamed f', loc)
adamc@488 491 val e' = IS.foldr (fn (arg, e) => (EApp (e, (ERel arg, loc)), loc))
adamc@488 492 e' fvs
adamc@1079 493 val e' = foldl (fn (arg, e) => (EApp (e, arg), loc))
adamc@488 494 e' xs
adam@1362 495
adamc@488 496 (*val () = Print.prefaces "Brand new"
adamc@721 497 [("e'", CorePrint.p_exp CoreEnv.empty e'),
adamc@1080 498 ("e", CorePrint.p_exp CoreEnv.empty e),
adamc@721 499 ("body'", CorePrint.p_exp CoreEnv.empty body')]*)
adamc@488 500 in
adamc@1080 501 (e',
adamc@488 502 {maxName = #maxName st,
adamc@488 503 funcs = #funcs st,
adamc@1079 504 decls = (name, f', typ', body', tag) :: #decls st,
adamc@1079 505 specialized = #specialized st})
adamc@488 506 end
adamc@485 507 end
adamc@488 508 end
adamc@485 509 end
adamc@482 510
adamc@521 511 fun doDecl (d, (st : state, changed)) =
adamc@488 512 let
adamc@521 513 (*val befor = Time.now ()*)
adamc@482 514
adamc@453 515 val funcs = #funcs st
adamc@453 516 val funcs =
adamc@453 517 case #1 d of
adamc@453 518 DValRec vis =>
adam@1766 519 let
adam@1766 520 val fs = foldl (fn ((_, n, _, _, _), fs) => IS.add (fs, n)) IS.empty vis
adam@1766 521 val constArgs = foldl (fn ((_, _, _, e, _), constArgs) =>
adam@1766 522 Int.min (constArgs, calcConstArgs fs e))
adam@1766 523 maxInt vis
adam@1766 524 in
adam@1766 525 foldl (fn ((x, n, c, e, tag), funcs) =>
adam@1766 526 IM.insert (funcs, n, {name = x,
adam@1766 527 args = KM.empty,
adam@1766 528 body = e,
adam@1766 529 typ = c,
adam@1766 530 tag = tag,
adam@1766 531 constArgs = constArgs}))
adam@1766 532 funcs vis
adam@1766 533 end
adamc@453 534 | _ => funcs
adamc@453 535
adamc@453 536 val st = {maxName = #maxName st,
adamc@453 537 funcs = funcs,
adamc@1079 538 decls = [],
adamc@1079 539 specialized = #specialized st}
adamc@453 540
adamc@482 541 (*val () = Print.prefaces "decl" [("d", CorePrint.p_decl CoreEnv.empty d)]*)
adamc@521 542
adamc@522 543 val (d', st) =
adamc@522 544 if isPoly d then
adamc@522 545 (d, st)
adamc@522 546 else
adamc@1080 547 case #1 d of
adamc@1080 548 DVal (x, n, t, e, s) =>
adamc@1080 549 let
adam@1362 550 (*val () = Print.preface ("Visiting", Print.box [Print.PD.string (x ^ "__" ^ Int.toString n),
adam@1362 551 Print.space,
adam@1362 552 Print.PD.string ":",
adam@1362 553 Print.space,
adam@1362 554 CorePrint.p_con CoreEnv.empty t])*)
adam@1362 555
adamc@1080 556 val (e, st) = exp ([], e, st)
adamc@1080 557 in
adamc@1080 558 ((DVal (x, n, t, e, s), #2 d), st)
adamc@1080 559 end
adamc@1080 560 | DValRec vis =>
adamc@1080 561 let
adamc@1120 562 (*val () = Print.preface ("Visiting", Print.p_list (fn vi =>
adam@1362 563 Print.box [Print.PD.string (#1 vi ^ "__"
adam@1362 564 ^ Int.toString
adam@1362 565 (#2 vi)),
adam@1362 566 Print.space,
adam@1362 567 Print.PD.string ":",
adam@1362 568 Print.space,
adam@1362 569 CorePrint.p_con CoreEnv.empty (#3 vi)])
adamc@1120 570 vis)*)
adamc@1120 571
adamc@1080 572 val (vis, st) = ListUtil.foldlMap (fn ((x, n, t, e, s), st) =>
adamc@1080 573 let
adamc@1080 574 val (e, st) = exp ([], e, st)
adamc@1080 575 in
adamc@1080 576 ((x, n, t, e, s), st)
adamc@1080 577 end) st vis
adamc@1080 578 in
adamc@1080 579 ((DValRec vis, #2 d), st)
adamc@1080 580 end
adamc@1080 581 | DTable (s, n, t, s1, e1, t1, e2, t2) =>
adamc@1080 582 let
adamc@1080 583 val (e1, st) = exp ([], e1, st)
adamc@1080 584 val (e2, st) = exp ([], e2, st)
adamc@1080 585 in
adamc@1080 586 ((DTable (s, n, t, s1, e1, t2, e2, t2), #2 d), st)
adamc@1080 587 end
adamc@1080 588 | DView (x, n, s, e, t) =>
adamc@1080 589 let
adamc@1080 590 val (e, st) = exp ([], e, st)
adamc@1080 591 in
adamc@1080 592 ((DView (x, n, s, e, t), #2 d), st)
adamc@1080 593 end
adamc@1080 594 | DTask (e1, e2) =>
adamc@1080 595 let
adamc@1080 596 val (e1, st) = exp ([], e1, st)
adamc@1080 597 val (e2, st) = exp ([], e2, st)
adamc@1080 598 in
adamc@1080 599 ((DTask (e1, e2), #2 d), st)
adamc@1080 600 end
adamc@1080 601 | _ => (d, st)
adamc@1080 602
adamc@482 603 (*val () = print "/decl\n"*)
adamc@443 604
adamc@443 605 val funcs = #funcs st
adamc@443 606 val funcs =
adamc@443 607 case #1 d of
adamc@443 608 DVal (x, n, c, e as (EAbs _, _), tag) =>
adamc@443 609 IM.insert (funcs, n, {name = x,
adamc@453 610 args = KM.empty,
adamc@443 611 body = e,
adamc@443 612 typ = c,
adam@1675 613 tag = tag,
adam@1766 614 constArgs = calcConstArgs (IS.singleton n) e})
adamc@469 615 | DVal (_, n, _, (ENamed n', _), _) =>
adamc@469 616 (case IM.find (funcs, n') of
adamc@469 617 NONE => funcs
adamc@469 618 | SOME v => IM.insert (funcs, n, v))
adamc@443 619 | _ => funcs
adamc@443 620
adamc@453 621 val (changed, ds) =
adamc@443 622 case #decls st of
adamc@453 623 [] => (changed, [d'])
adamc@453 624 | vis =>
adamc@453 625 (true, case d' of
adamc@453 626 (DValRec vis', _) => [(DValRec (vis @ vis'), ErrorMsg.dummySpan)]
adamc@453 627 | _ => [(DValRec vis, ErrorMsg.dummySpan), d'])
adamc@443 628 in
adamc@802 629 (*Print.prefaces "doDecl" [("d", CorePrint.p_decl E.empty d),
adamc@802 630 ("d'", CorePrint.p_decl E.empty d')];*)
adamc@521 631 (ds, ({maxName = #maxName st,
adamc@453 632 funcs = funcs,
adamc@1079 633 decls = [],
adamc@1079 634 specialized = #specialized st}, changed))
adamc@443 635 end
adamc@443 636
adamc@1120 637 (*val () = Print.preface ("RESET", CorePrint.p_file CoreEnv.empty file)*)
adamc@1079 638 val (ds, (st, changed)) = ListUtil.foldlMapConcat doDecl
adamc@521 639 ({maxName = U.File.maxName file + 1,
adamc@1080 640 funcs = funcs,
adamc@1079 641 decls = [],
adamc@1079 642 specialized = specialized},
adamc@488 643 false)
adamc@488 644 file
adamc@443 645 in
adamc@1120 646 (*print ("Changed = " ^ Bool.toString changed ^ "\n");*)
adamc@1080 647 (changed, ds, #funcs st, #specialized st)
adamc@443 648 end
adamc@443 649
adamc@1080 650 fun specializeL (funcs, specialized) file =
adamc@453 651 let
adamc@721 652 val file = ReduceLocal.reduce file
adamc@520 653 (*val file = ReduceLocal.reduce file*)
adamc@1080 654 val (changed, file, funcs, specialized) = specialize' (funcs, specialized) file
adamc@520 655 (*val file = ReduceLocal.reduce file
adamc@520 656 val file = CoreUntangle.untangle file
adamc@488 657 val file = Shake.shake file*)
adamc@453 658 in
adamc@488 659 (*print "Round over\n";*)
adamc@453 660 if changed then
adamc@520 661 let
adamc@721 662 (*val file = ReduceLocal.reduce file*)
adamc@802 663 (*val () = Print.prefaces "Pre-untangle" [("file", CorePrint.p_file CoreEnv.empty file)]*)
adamc@520 664 val file = CoreUntangle.untangle file
adamc@802 665 (*val () = Print.prefaces "Post-untangle" [("file", CorePrint.p_file CoreEnv.empty file)]*)
adamc@520 666 val file = Shake.shake file
adamc@520 667 in
adamc@520 668 (*print "Again!\n";*)
adamc@1080 669 (*Print.prefaces "Again" [("file", CorePrint.p_file CoreEnv.empty file)];*)
adamc@1080 670 specializeL (funcs, specialized) file
adamc@520 671 end
adamc@453 672 else
adamc@453 673 file
adamc@453 674 end
adamc@453 675
adamc@1080 676 val specialize = specializeL (IM.empty, IS.empty)
adamc@1079 677
adamc@443 678 end