annotate src/especialize.sml @ 1861:52043ad66ce7

Extend Especialize rule: find maximal argument prefixes that end in 1 or more arguments with functional types
author Adam Chlipala <adam@chlipala.net>
date Fri, 09 Aug 2013 16:04:16 -0400
parents e15234fbb163
children 32784d27b5bc
rev   line source
adam@1848 1 (* Copyright (c) 2008-2013, 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@1817 127 | TCFun _ => true
adam@1289 128 | CFfi ("Basis", "transaction") => true
adam@1289 129 | CFfi ("Basis", "eq") => true
adam@1289 130 | CFfi ("Basis", "num") => true
adam@1289 131 | CFfi ("Basis", "ord") => true
adam@1289 132 | CFfi ("Basis", "show") => true
adam@1289 133 | CFfi ("Basis", "read") => true
adam@1289 134 | CFfi ("Basis", "sql_injectable_prim") => true
adam@1289 135 | CFfi ("Basis", "sql_injectable") => true
adam@1289 136 | _ => false}
adam@1289 137
adam@1675 138 fun getApp (e, _) =
adam@1675 139 case e of
adam@1675 140 ENamed f => SOME (f, [])
adam@1675 141 | EApp (e1, e2) =>
adam@1675 142 (case getApp e1 of
adam@1675 143 NONE => NONE
adam@1675 144 | SOME (f, xs) => SOME (f, xs @ [e2]))
adam@1675 145 | _ => NONE
adam@1675 146
adam@1675 147 val getApp = fn e => case getApp e of
adam@1675 148 v as SOME (_, _ :: _) => v
adam@1675 149 | _ => NONE
adam@1675 150
adam@1675 151 val maxInt = Option.getOpt (Int.maxInt, 9999)
adam@1675 152
adam@1766 153 fun calcConstArgs enclosingFunctions e =
adam@1675 154 let
adam@1675 155 fun ca depth e =
adam@1675 156 case #1 e of
adam@1675 157 EPrim _ => maxInt
adam@1675 158 | ERel _ => maxInt
adam@1766 159 | ENamed n => if IS.member (enclosingFunctions, n) then 0 else maxInt
adam@1675 160 | ECon (_, _, _, NONE) => maxInt
adam@1675 161 | ECon (_, _, _, SOME e) => ca depth e
adam@1675 162 | EFfi _ => maxInt
adam@1675 163 | EFfiApp (_, _, ecs) => foldl (fn ((e, _), d) => Int.min (ca depth e, d)) maxInt ecs
adam@1675 164 | EApp (e1, e2) =>
adam@1675 165 let
adam@1675 166 fun default () = Int.min (ca depth e1, ca depth e2)
adam@1675 167 in
adam@1675 168 case getApp e of
adam@1675 169 NONE => default ()
adam@1675 170 | SOME (f, args) =>
adam@1766 171 if not (IS.member (enclosingFunctions, f)) then
adam@1675 172 default ()
adam@1675 173 else
adam@1675 174 let
adam@1675 175 fun visitArgs (count, args) =
adam@1675 176 case args of
adam@1675 177 [] => count
adam@1675 178 | arg :: args' =>
adam@1675 179 let
adam@1675 180 fun default () = foldl (fn (e, d) => Int.min (ca depth e, d)) count args
adam@1675 181 in
adam@1675 182 case #1 arg of
adam@1675 183 ERel n =>
adam@1676 184 if n = depth - 1 - count then
adam@1675 185 visitArgs (count + 1, args')
adam@1675 186 else
adam@1675 187 default ()
adam@1675 188 | _ => default ()
adam@1675 189 end
adam@1675 190 in
adam@1675 191 visitArgs (0, args)
adam@1675 192 end
adam@1675 193 end
adam@1675 194 | EAbs (_, _, _, e1) => ca (depth + 1) e1
adam@1675 195 | ECApp (e1, _) => ca depth e1
adam@1675 196 | ECAbs (_, _, e1) => ca depth e1
adam@1675 197 | EKAbs (_, e1) => ca depth e1
adam@1675 198 | EKApp (e1, _) => ca depth e1
adam@1675 199 | ERecord xets => foldl (fn ((_, e, _), d) => Int.min (ca depth e, d)) maxInt xets
adam@1675 200 | EField (e1, _, _) => ca depth e1
adam@1675 201 | EConcat (e1, _, e2, _) => Int.min (ca depth e1, ca depth e2)
adam@1675 202 | ECut (e1, _, _) => ca depth e1
adam@1675 203 | ECutMulti (e1, _, _) => ca depth e1
adam@1675 204 | ECase (e1, pes, _) => foldl (fn ((p, e), d) => Int.min (ca (depth + E.patBindsN p) e, d)) (ca depth e1) pes
adam@1675 205 | EWrite e1 => ca depth e1
adam@1675 206 | EClosure (_, es) => foldl (fn (e, d) => Int.min (ca depth e, d)) maxInt es
adam@1675 207 | ELet (_, _, e1, e2) => Int.min (ca depth e1, ca (depth + 1) e2)
adam@1848 208 | EServerCall (_, es, _, _) => foldl (fn (e, d) => Int.min (ca depth e, d)) maxInt es
adam@1675 209
adam@1675 210 fun enterAbs depth e =
adam@1675 211 case #1 e of
adam@1675 212 EAbs (_, _, _, e1) => enterAbs (depth + 1) e1
adam@1675 213 | _ => ca depth e
adam@1675 214 in
adam@1677 215 enterAbs 0 e
adam@1675 216 end
adam@1675 217
adam@1675 218
adamc@1080 219 fun specialize' (funcs, specialized) file =
adamc@443 220 let
adamc@488 221 fun bind (env, b) =
adamc@488 222 case b of
adamc@521 223 U.Decl.RelE xt => xt :: env
adamc@521 224 | _ => env
adamc@488 225
adamc@1080 226 fun exp (env, e as (_, loc), st : state) =
adamc@482 227 let
adamc@721 228 (*val () = Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty
adamc@721 229 (e, ErrorMsg.dummySpan))]*)
adamc@721 230
adamc@1080 231 fun default () =
adamc@1080 232 case #1 e of
adamc@1080 233 EPrim _ => (e, st)
adamc@1080 234 | ERel _ => (e, st)
adamc@1080 235 | ENamed _ => (e, st)
adamc@1080 236 | ECon (_, _, _, NONE) => (e, st)
adamc@1080 237 | ECon (dk, pc, cs, SOME e) =>
adamc@1080 238 let
adamc@1080 239 val (e, st) = exp (env, e, st)
adamc@1080 240 in
adamc@1080 241 ((ECon (dk, pc, cs, SOME e), loc), st)
adamc@1080 242 end
adamc@1080 243 | EFfi _ => (e, st)
adamc@1080 244 | EFfiApp (m, x, es) =>
adamc@1080 245 let
adam@1663 246 val (es, st) = ListUtil.foldlMap (fn ((e, t), st) =>
adam@1663 247 let
adam@1663 248 val (e, st) = exp (env, e, st)
adam@1663 249 in
adam@1663 250 ((e, t), st)
adam@1663 251 end) st es
adamc@1080 252 in
adamc@1080 253 ((EFfiApp (m, x, es), loc), st)
adamc@1080 254 end
adamc@1080 255 | EApp (e1, e2) =>
adamc@1080 256 let
adamc@1080 257 val (e1, st) = exp (env, e1, st)
adamc@1080 258 val (e2, st) = exp (env, e2, st)
adamc@1080 259 in
adamc@1080 260 ((EApp (e1, e2), loc), st)
adamc@1080 261 end
adamc@1080 262 | EAbs (x, d, r, e) =>
adamc@1080 263 let
adamc@1080 264 val (e, st) = exp ((x, d) :: env, e, st)
adamc@1080 265 in
adamc@1080 266 ((EAbs (x, d, r, e), loc), st)
adamc@1080 267 end
adamc@1080 268 | ECApp (e, c) =>
adamc@1080 269 let
adamc@1080 270 val (e, st) = exp (env, e, st)
adamc@1080 271 in
adamc@1080 272 ((ECApp (e, c), loc), st)
adamc@1080 273 end
adamc@1185 274 | ECAbs _ => (e, st)
adamc@1120 275 | EKAbs _ => (e, st)
adamc@1080 276 | EKApp (e, k) =>
adamc@1080 277 let
adamc@1080 278 val (e, st) = exp (env, e, st)
adamc@1080 279 in
adamc@1080 280 ((EKApp (e, k), loc), st)
adamc@1080 281 end
adamc@1080 282 | ERecord fs =>
adamc@1080 283 let
adamc@1080 284 val (fs, st) = ListUtil.foldlMap (fn ((c1, e, c2), st) =>
adamc@1080 285 let
adamc@1080 286 val (e, st) = exp (env, e, st)
adamc@1080 287 in
adamc@1080 288 ((c1, e, c2), st)
adamc@1080 289 end) st fs
adamc@1080 290 in
adamc@1080 291 ((ERecord fs, loc), st)
adamc@1080 292 end
adamc@1080 293 | EField (e, c, cs) =>
adamc@1080 294 let
adamc@1080 295 val (e, st) = exp (env, e, st)
adamc@1080 296 in
adamc@1080 297 ((EField (e, c, cs), loc), st)
adamc@1080 298 end
adamc@1080 299 | EConcat (e1, c1, e2, c2) =>
adamc@1080 300 let
adamc@1080 301 val (e1, st) = exp (env, e1, st)
adamc@1080 302 val (e2, st) = exp (env, e2, st)
adamc@1080 303 in
adamc@1080 304 ((EConcat (e1, c1, e2, c2), loc), st)
adamc@1080 305 end
adamc@1080 306 | ECut (e, c, cs) =>
adamc@1080 307 let
adamc@1080 308 val (e, st) = exp (env, e, st)
adamc@1080 309 in
adamc@1080 310 ((ECut (e, c, cs), loc), st)
adamc@1080 311 end
adamc@1080 312 | ECutMulti (e, c, cs) =>
adamc@1080 313 let
adamc@1080 314 val (e, st) = exp (env, e, st)
adamc@1080 315 in
adamc@1080 316 ((ECutMulti (e, c, cs), loc), st)
adamc@1080 317 end
adamc@1080 318
adamc@1080 319 | ECase (e, pes, cs) =>
adamc@1080 320 let
adamc@1080 321 val (e, st) = exp (env, e, st)
adamc@1080 322 val (pes, st) = ListUtil.foldlMap (fn ((p, e), st) =>
adamc@1080 323 let
adamc@1080 324 val (e, st) = exp (E.patBindsL p @ env, e, st)
adamc@1080 325 in
adamc@1080 326 ((p, e), st)
adamc@1080 327 end) st pes
adamc@1080 328 in
adamc@1080 329 ((ECase (e, pes, cs), loc), st)
adamc@1080 330 end
adamc@1080 331
adamc@1080 332 | EWrite e =>
adamc@1080 333 let
adamc@1080 334 val (e, st) = exp (env, e, st)
adamc@1080 335 in
adamc@1080 336 ((EWrite e, loc), st)
adamc@1080 337 end
adamc@1080 338 | EClosure (n, es) =>
adamc@1080 339 let
adamc@1080 340 val (es, st) = ListUtil.foldlMap (fn (e, st) => exp (env, e, st)) st es
adamc@1080 341 in
adamc@1080 342 ((EClosure (n, es), loc), st)
adamc@1080 343 end
adamc@1080 344 | ELet (x, t, e1, e2) =>
adamc@1080 345 let
adamc@1080 346 val (e1, st) = exp (env, e1, st)
adamc@1080 347 val (e2, st) = exp ((x, t) :: env, e2, st)
adamc@1080 348 in
adamc@1080 349 ((ELet (x, t, e1, e2), loc), st)
adamc@1080 350 end
adam@1848 351 | EServerCall (n, es, t, fm) =>
adamc@1080 352 let
adamc@1080 353 val (es, st) = ListUtil.foldlMap (fn (e, st) => exp (env, e, st)) st es
adamc@1080 354 in
adam@1848 355 ((EServerCall (n, es, t, fm), loc), st)
adamc@1080 356 end
adamc@482 357 in
adamc@482 358 case getApp e of
adamc@1080 359 NONE => default ()
adamc@488 360 | SOME (f, xs) =>
adamc@485 361 case IM.find (#funcs st, f) of
adamc@1272 362 NONE => ((*print ("No find: " ^ Int.toString f ^ "\n");*) default ())
adam@1675 363 | SOME {name, args, body, typ, tag, constArgs} =>
adamc@488 364 let
adamc@1080 365 val (xs, st) = ListUtil.foldlMap (fn (e, st) => exp (env, e, st)) st xs
adamc@1080 366
adam@1861 367 (*val () = Print.prefaces "Consider" [("e", CorePrint.p_exp CoreEnv.empty e)]*)
adamc@721 368
adamc@488 369 val loc = ErrorMsg.dummySpan
adamc@488 370
adam@1677 371 val oldXs = xs
adam@1677 372
adam@1861 373 fun findSplit av (initialPart, constArgs, xs, typ, fxs, fvs) =
adam@1861 374 let
adam@1861 375 fun default () =
adam@1861 376 if initialPart then
adam@1861 377 ([], oldXs, IS.empty)
adam@1677 378 else
adam@1861 379 (rev fxs, xs, fvs)
adam@1861 380 in
adam@1861 381 case (#1 typ, xs) of
adam@1861 382 (TFun (dom, ran), e :: xs') =>
adam@1861 383 if constArgs > 0 then
adam@1861 384 let
adam@1861 385 val fi = functionInside dom
adam@1861 386 in
adam@1861 387 if initialPart orelse fi then
adam@1861 388 findSplit av (not fi andalso initialPart,
adam@1861 389 constArgs - 1,
adam@1861 390 xs',
adam@1861 391 ran,
adam@1861 392 e :: fxs,
adam@1861 393 IS.union (fvs, freeVars e))
adam@1861 394 else
adam@1861 395 default ()
adam@1861 396 end
adam@1861 397 else
adam@1861 398 default ()
adam@1861 399 | _ => default ()
adam@1861 400 end
adamc@488 401
adam@1861 402 val (fxs, xs, fvs) = findSplit true (true, constArgs, xs, typ, [], IS.empty)
adam@1355 403
adam@1314 404 val vts = map (fn n => #2 (List.nth (env, n))) (IS.listItems fvs)
adamc@1079 405 val fxs' = map (squish (IS.listItems fvs)) fxs
adam@1362 406
adam@1362 407 val p_bool = Print.PD.string o Bool.toString
adamc@488 408 in
adam@1355 409 (*Print.prefaces "Func" [("name", Print.PD.string name),
adam@1355 410 ("e", CorePrint.p_exp CoreEnv.empty e),
adam@1355 411 ("fxs'", Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')];*)
adam@1675 412 if List.all (fn (ERel _, _) => true
adam@1675 413 | _ => false) fxs' then
adam@1675 414 default ()
adamc@488 415 else
adam@1667 416 case KM.find (args, (vts, fxs')) of
adam@1667 417 SOME f' =>
adamc@485 418 let
adamc@488 419 val e = (ENamed f', loc)
adamc@488 420 val e = IS.foldr (fn (arg, e) => (EApp (e, (ERel arg, loc)), loc))
adamc@488 421 e fvs
adamc@1079 422 val e = foldl (fn (arg, e) => (EApp (e, arg), loc))
adamc@488 423 e xs
adamc@488 424 in
adamc@488 425 (*Print.prefaces "Brand new (reuse)"
adamc@721 426 [("e'", CorePrint.p_exp CoreEnv.empty e)];*)
adamc@1080 427 (e, st)
adamc@488 428 end
adam@1667 429 | NONE =>
adamc@488 430 let
adamc@800 431 (*val () = Print.prefaces "New one"
adam@1667 432 [("name", Print.PD.string name),
adam@1667 433 ("f", Print.PD.string (Int.toString f)),
adam@1667 434 ("|fvs|", Print.PD.string (Int.toString (IS.numItems fvs))),
adam@1667 435 ("|fxs|", Print.PD.string (Int.toString (length fxs))),
adam@1766 436 ("fxs'", Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs'),
adam@1667 437 ("spec", Print.PD.string (Bool.toString (IS.member (#specialized st, f))))]*)
adamc@800 438
adamc@818 439 (*val () = Print.prefaces ("Yes(" ^ name ^ ")")
adamc@818 440 [("fxs'",
adamc@818 441 Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')]*)
adamc@818 442
adam@1675 443 (*val () = Print.prefaces name
adam@1675 444 [("Available", Print.PD.string (Int.toString constArgs)),
adam@1675 445 ("Used", Print.PD.string (Int.toString (length fxs'))),
adam@1675 446 ("fxs'",
adam@1675 447 Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')]*)
adam@1675 448
adamc@1079 449 fun subBody (body, typ, fxs') =
adamc@1079 450 case (#1 body, #1 typ, fxs') of
adamc@488 451 (_, _, []) => SOME (body, typ)
adamc@1079 452 | (EAbs (_, _, _, body'), TFun (_, typ'), x :: fxs'') =>
adamc@488 453 let
adamc@1079 454 val body'' = E.subExpInExp (0, x) body'
adamc@488 455 in
adamc@488 456 subBody (body'',
adamc@488 457 typ',
adamc@1079 458 fxs'')
adamc@488 459 end
adamc@488 460 | _ => NONE
adamc@488 461 in
adamc@1079 462 case subBody (body, typ, fxs') of
adamc@1080 463 NONE => default ()
adamc@488 464 | SOME (body', typ') =>
adamc@488 465 let
adamc@488 466 val f' = #maxName st
adam@1314 467 val args = KM.insert (args, (vts, fxs'), f')
adamc@488 468 val funcs = IM.insert (#funcs st, f, {name = name,
adamc@488 469 args = args,
adamc@488 470 body = body,
adamc@488 471 typ = typ,
adam@1675 472 tag = tag,
adam@1766 473 constArgs = calcConstArgs (IS.singleton f) body})
adamc@1079 474
adamc@488 475 val st = {
adamc@488 476 maxName = f' + 1,
adamc@488 477 funcs = funcs,
adamc@1079 478 decls = #decls st,
adamc@1080 479 specialized = IS.add (#specialized st, f')
adamc@488 480 }
adamc@487 481
adamc@488 482 (*val () = Print.prefaces "specExp"
adamc@488 483 [("f", CorePrint.p_exp env (ENamed f, loc)),
adamc@488 484 ("f'", CorePrint.p_exp env (ENamed f', loc)),
adamc@488 485 ("xs", Print.p_list (CorePrint.p_exp env) xs),
adamc@488 486 ("fxs'", Print.p_list
adamc@488 487 (CorePrint.p_exp E.empty) fxs'),
adamc@488 488 ("e", CorePrint.p_exp env (e, loc))]*)
adamc@488 489 val (body', typ') = IS.foldl (fn (n, (body', typ')) =>
adamc@488 490 let
adamc@521 491 val (x, xt) = List.nth (env, n)
adamc@488 492 in
adamc@488 493 ((EAbs (x, xt, typ', body'),
adamc@488 494 loc),
adamc@488 495 (TFun (xt, typ'), loc))
adamc@488 496 end)
adamc@488 497 (body', typ') fvs
adam@1861 498 (*val () = print ("NEW: " ^ name ^ "__" ^ Int.toString f' ^ "\n")*)
adamc@1272 499 val body' = ReduceLocal.reduceExp body'
adamc@1080 500 (*val () = Print.preface ("PRE", CorePrint.p_exp CoreEnv.empty body')*)
adamc@1080 501 val (body', st) = exp (env, body', st)
adamc@482 502
adamc@488 503 val e' = (ENamed f', loc)
adamc@488 504 val e' = IS.foldr (fn (arg, e) => (EApp (e, (ERel arg, loc)), loc))
adamc@488 505 e' fvs
adamc@1079 506 val e' = foldl (fn (arg, e) => (EApp (e, arg), loc))
adamc@488 507 e' xs
adam@1362 508
adamc@488 509 (*val () = Print.prefaces "Brand new"
adamc@721 510 [("e'", CorePrint.p_exp CoreEnv.empty e'),
adamc@1080 511 ("e", CorePrint.p_exp CoreEnv.empty e),
adamc@721 512 ("body'", CorePrint.p_exp CoreEnv.empty body')]*)
adamc@488 513 in
adamc@1080 514 (e',
adamc@488 515 {maxName = #maxName st,
adamc@488 516 funcs = #funcs st,
adamc@1079 517 decls = (name, f', typ', body', tag) :: #decls st,
adamc@1079 518 specialized = #specialized st})
adamc@488 519 end
adamc@485 520 end
adamc@488 521 end
adamc@485 522 end
adamc@482 523
adamc@521 524 fun doDecl (d, (st : state, changed)) =
adamc@488 525 let
adamc@521 526 (*val befor = Time.now ()*)
adamc@482 527
adamc@453 528 val funcs = #funcs st
adamc@453 529 val funcs =
adamc@453 530 case #1 d of
adamc@453 531 DValRec vis =>
adam@1766 532 let
adam@1766 533 val fs = foldl (fn ((_, n, _, _, _), fs) => IS.add (fs, n)) IS.empty vis
adam@1766 534 val constArgs = foldl (fn ((_, _, _, e, _), constArgs) =>
adam@1766 535 Int.min (constArgs, calcConstArgs fs e))
adam@1766 536 maxInt vis
adam@1766 537 in
adam@1861 538 (*Print.prefaces "ConstArgs" [("d", CorePrint.p_decl CoreEnv.empty d),
adam@1861 539 ("ca", Print.PD.string (Int.toString constArgs))];*)
adam@1766 540 foldl (fn ((x, n, c, e, tag), funcs) =>
adam@1766 541 IM.insert (funcs, n, {name = x,
adam@1766 542 args = KM.empty,
adam@1766 543 body = e,
adam@1766 544 typ = c,
adam@1766 545 tag = tag,
adam@1766 546 constArgs = constArgs}))
adam@1766 547 funcs vis
adam@1766 548 end
adamc@453 549 | _ => funcs
adamc@453 550
adamc@453 551 val st = {maxName = #maxName st,
adamc@453 552 funcs = funcs,
adamc@1079 553 decls = [],
adamc@1079 554 specialized = #specialized st}
adamc@453 555
adamc@482 556 (*val () = Print.prefaces "decl" [("d", CorePrint.p_decl CoreEnv.empty d)]*)
adamc@521 557
adamc@522 558 val (d', st) =
adamc@522 559 if isPoly d then
adamc@522 560 (d, st)
adamc@522 561 else
adamc@1080 562 case #1 d of
adamc@1080 563 DVal (x, n, t, e, s) =>
adamc@1080 564 let
adam@1362 565 (*val () = Print.preface ("Visiting", Print.box [Print.PD.string (x ^ "__" ^ Int.toString n),
adam@1362 566 Print.space,
adam@1362 567 Print.PD.string ":",
adam@1362 568 Print.space,
adam@1362 569 CorePrint.p_con CoreEnv.empty t])*)
adam@1362 570
adamc@1080 571 val (e, st) = exp ([], e, st)
adamc@1080 572 in
adamc@1080 573 ((DVal (x, n, t, e, s), #2 d), st)
adamc@1080 574 end
adamc@1080 575 | DValRec vis =>
adamc@1080 576 let
adamc@1120 577 (*val () = Print.preface ("Visiting", Print.p_list (fn vi =>
adam@1362 578 Print.box [Print.PD.string (#1 vi ^ "__"
adam@1362 579 ^ Int.toString
adam@1362 580 (#2 vi)),
adam@1362 581 Print.space,
adam@1362 582 Print.PD.string ":",
adam@1362 583 Print.space,
adam@1362 584 CorePrint.p_con CoreEnv.empty (#3 vi)])
adamc@1120 585 vis)*)
adamc@1120 586
adamc@1080 587 val (vis, st) = ListUtil.foldlMap (fn ((x, n, t, e, s), st) =>
adamc@1080 588 let
adamc@1080 589 val (e, st) = exp ([], e, st)
adamc@1080 590 in
adamc@1080 591 ((x, n, t, e, s), st)
adamc@1080 592 end) st vis
adamc@1080 593 in
adamc@1080 594 ((DValRec vis, #2 d), st)
adamc@1080 595 end
adamc@1080 596 | DTable (s, n, t, s1, e1, t1, e2, t2) =>
adamc@1080 597 let
adamc@1080 598 val (e1, st) = exp ([], e1, st)
adamc@1080 599 val (e2, st) = exp ([], e2, st)
adamc@1080 600 in
adamc@1080 601 ((DTable (s, n, t, s1, e1, t2, e2, t2), #2 d), st)
adamc@1080 602 end
adamc@1080 603 | DView (x, n, s, e, t) =>
adamc@1080 604 let
adamc@1080 605 val (e, st) = exp ([], e, st)
adamc@1080 606 in
adamc@1080 607 ((DView (x, n, s, e, t), #2 d), st)
adamc@1080 608 end
adamc@1080 609 | DTask (e1, e2) =>
adamc@1080 610 let
adamc@1080 611 val (e1, st) = exp ([], e1, st)
adamc@1080 612 val (e2, st) = exp ([], e2, st)
adamc@1080 613 in
adamc@1080 614 ((DTask (e1, e2), #2 d), st)
adamc@1080 615 end
adamc@1080 616 | _ => (d, st)
adamc@1080 617
adamc@482 618 (*val () = print "/decl\n"*)
adamc@443 619
adamc@443 620 val funcs = #funcs st
adamc@443 621 val funcs =
adamc@443 622 case #1 d of
adamc@443 623 DVal (x, n, c, e as (EAbs _, _), tag) =>
adam@1861 624 ((*Print.prefaces "ConstArgs[2]" [("d", CorePrint.p_decl CoreEnv.empty d),
adam@1861 625 ("ca", Print.PD.string (Int.toString (calcConstArgs (IS.singleton n) e)))];*)
adamc@443 626 IM.insert (funcs, n, {name = x,
adamc@453 627 args = KM.empty,
adamc@443 628 body = e,
adamc@443 629 typ = c,
adam@1675 630 tag = tag,
adam@1861 631 constArgs = calcConstArgs (IS.singleton n) e}))
adamc@469 632 | DVal (_, n, _, (ENamed n', _), _) =>
adamc@469 633 (case IM.find (funcs, n') of
adamc@469 634 NONE => funcs
adamc@469 635 | SOME v => IM.insert (funcs, n, v))
adamc@443 636 | _ => funcs
adamc@443 637
adamc@453 638 val (changed, ds) =
adamc@443 639 case #decls st of
adamc@453 640 [] => (changed, [d'])
adamc@453 641 | vis =>
adamc@453 642 (true, case d' of
adamc@453 643 (DValRec vis', _) => [(DValRec (vis @ vis'), ErrorMsg.dummySpan)]
adamc@453 644 | _ => [(DValRec vis, ErrorMsg.dummySpan), d'])
adamc@443 645 in
adamc@802 646 (*Print.prefaces "doDecl" [("d", CorePrint.p_decl E.empty d),
adamc@802 647 ("d'", CorePrint.p_decl E.empty d')];*)
adamc@521 648 (ds, ({maxName = #maxName st,
adamc@453 649 funcs = funcs,
adamc@1079 650 decls = [],
adamc@1079 651 specialized = #specialized st}, changed))
adamc@443 652 end
adamc@443 653
adamc@1120 654 (*val () = Print.preface ("RESET", CorePrint.p_file CoreEnv.empty file)*)
adamc@1079 655 val (ds, (st, changed)) = ListUtil.foldlMapConcat doDecl
adamc@521 656 ({maxName = U.File.maxName file + 1,
adamc@1080 657 funcs = funcs,
adamc@1079 658 decls = [],
adamc@1079 659 specialized = specialized},
adamc@488 660 false)
adamc@488 661 file
adamc@443 662 in
adamc@1120 663 (*print ("Changed = " ^ Bool.toString changed ^ "\n");*)
adamc@1080 664 (changed, ds, #funcs st, #specialized st)
adamc@443 665 end
adamc@443 666
adamc@1080 667 fun specializeL (funcs, specialized) file =
adamc@453 668 let
adamc@721 669 val file = ReduceLocal.reduce file
adamc@520 670 (*val file = ReduceLocal.reduce file*)
adamc@1080 671 val (changed, file, funcs, specialized) = specialize' (funcs, specialized) file
adamc@520 672 (*val file = ReduceLocal.reduce file
adamc@520 673 val file = CoreUntangle.untangle file
adamc@488 674 val file = Shake.shake file*)
adamc@453 675 in
adamc@488 676 (*print "Round over\n";*)
adamc@453 677 if changed then
adamc@520 678 let
adamc@721 679 (*val file = ReduceLocal.reduce file*)
adamc@802 680 (*val () = Print.prefaces "Pre-untangle" [("file", CorePrint.p_file CoreEnv.empty file)]*)
adamc@520 681 val file = CoreUntangle.untangle file
adamc@802 682 (*val () = Print.prefaces "Post-untangle" [("file", CorePrint.p_file CoreEnv.empty file)]*)
adamc@520 683 val file = Shake.shake file
adamc@520 684 in
adamc@520 685 (*print "Again!\n";*)
adamc@1080 686 (*Print.prefaces "Again" [("file", CorePrint.p_file CoreEnv.empty file)];*)
adamc@1080 687 specializeL (funcs, specialized) file
adamc@520 688 end
adamc@453 689 else
adamc@453 690 file
adamc@453 691 end
adamc@453 692
adamc@1080 693 val specialize = specializeL (IM.empty, IS.empty)
adamc@1079 694
adamc@443 695 end