annotate src/core_untangle.sml @ 997:588b31fe6cc9

Put right date stamp in CHANGELOG
author Adam Chlipala <adamc@hcoop.net>
date Fri, 09 Oct 2009 09:50:26 -0400
parents 2a50da66ffd8
children dfe34fad749d
rev   line source
adamc@454 1 (* Copyright (c) 2008, Adam Chlipala
adamc@454 2 * All rights reserved.
adamc@454 3 *
adamc@454 4 * Redistribution and use in source and binary forms, with or without
adamc@454 5 * modification, are permitted provided that the following conditions are met:
adamc@454 6 *
adamc@454 7 * - Redistributions of source code must retain the above copyright notice,
adamc@454 8 * this list of conditions and the following disclaimer.
adamc@454 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@454 10 * this list of conditions and the following disclaimer in the documentation
adamc@454 11 * and/or other materials provided with the distribution.
adamc@454 12 * - The names of contributors may not be used to endorse or promote products
adamc@454 13 * derived from this software without specific prior written permission.
adamc@454 14 *
adamc@454 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@454 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@454 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@454 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@454 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@454 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@454 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@454 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@454 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@454 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@454 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@454 26 *)
adamc@454 27
adamc@454 28 structure CoreUntangle :> CORE_UNTANGLE = struct
adamc@454 29
adamc@454 30 open Core
adamc@454 31
adamc@454 32 structure U = CoreUtil
adamc@454 33 structure E = CoreEnv
adamc@454 34
adamc@454 35 structure IS = IntBinarySet
adamc@454 36 structure IM = IntBinaryMap
adamc@454 37
adamc@454 38 fun default (k, s) = s
adamc@454 39
adamc@519 40 fun exp thisGroup (e, s) =
adamc@954 41 let
adamc@954 42 fun try n =
adamc@954 43 if IS.member (thisGroup, n) then
adamc@954 44 IS.add (s, n)
adamc@954 45 else
adamc@954 46 s
adamc@954 47 in
adamc@954 48 case e of
adamc@954 49 ENamed n => try n
adamc@954 50 | EClosure (n, _) => try n
adamc@954 51 | EServerCall (n, _, _, _, _) => try n
adamc@954 52 | ETailCall (n, _, _, _, _) => try n
adamc@954 53 | _ => s
adamc@954 54 end
adamc@454 55
adamc@454 56 fun untangle file =
adamc@454 57 let
adamc@519 58 fun expUsed thisGroup = U.Exp.fold {con = default,
adamc@519 59 kind = default,
adamc@519 60 exp = exp thisGroup} IS.empty
adamc@455 61
adamc@454 62 fun decl (dAll as (d, loc)) =
adamc@454 63 case d of
adamc@454 64 DValRec vis =>
adamc@454 65 let
adamc@454 66 val thisGroup = foldl (fn ((_, n, _, _, _), thisGroup) =>
adamc@454 67 IS.add (thisGroup, n)) IS.empty vis
adamc@454 68
adamc@519 69 val edefs = foldl (fn ((_, n, _, e, _), edefs) =>
adamc@519 70 IM.insert (edefs, n, expUsed thisGroup e))
adamc@519 71 IM.empty vis
adamc@455 72
adamc@519 73 val used = edefs
adamc@454 74
adamc@455 75 fun expand used =
adamc@455 76 IS.foldl (fn (n, used) =>
adamc@455 77 case IM.find (edefs, n) of
adamc@455 78 NONE => used
adamc@519 79 | SOME usedHere =>
adamc@519 80 if IS.isEmpty (IS.difference (usedHere, used)) then
adamc@519 81 used
adamc@519 82 else
adamc@519 83 expand (IS.union (usedHere, used)))
adamc@455 84 used used
adamc@455 85
adamc@454 86 fun p_graph reachable =
adamc@454 87 IM.appi (fn (n, reachableHere) =>
adamc@454 88 (print (Int.toString n);
adamc@454 89 print ":";
adamc@454 90 IS.app (fn n' => (print " ";
adamc@454 91 print (Int.toString n'))) reachableHere;
adamc@454 92 print "\n")) reachable
adamc@454 93
adamc@454 94 (*val () = print "used:\n"
adamc@454 95 val () = p_graph used*)
adamc@454 96
adamc@454 97 fun expand reachable =
adamc@454 98 let
adamc@454 99 val changed = ref false
adamc@454 100
adamc@454 101 val reachable =
adamc@454 102 IM.mapi (fn (n, reachableHere) =>
adamc@454 103 IS.foldl (fn (n', reachableHere) =>
adamc@454 104 let
adamc@454 105 val more = valOf (IM.find (reachable, n'))
adamc@454 106 in
adamc@454 107 if IS.isEmpty (IS.difference (more, reachableHere)) then
adamc@454 108 reachableHere
adamc@454 109 else
adamc@454 110 (changed := true;
adamc@454 111 IS.union (more, reachableHere))
adamc@454 112 end)
adamc@454 113 reachableHere reachableHere) reachable
adamc@454 114 in
adamc@454 115 (reachable, !changed)
adamc@454 116 end
adamc@454 117
adamc@454 118 fun iterate reachable =
adamc@454 119 let
adamc@454 120 val (reachable, changed) = expand reachable
adamc@454 121 in
adamc@454 122 if changed then
adamc@454 123 iterate reachable
adamc@454 124 else
adamc@454 125 reachable
adamc@454 126 end
adamc@454 127
adamc@454 128 val reachable = iterate used
adamc@454 129
adamc@454 130 (*val () = print "reachable:\n"
adamc@454 131 val () = p_graph reachable*)
adamc@454 132
adamc@454 133 fun sccs (nodes, acc) =
adamc@454 134 case IS.find (fn _ => true) nodes of
adamc@454 135 NONE => acc
adamc@454 136 | SOME rep =>
adamc@454 137 let
adamc@454 138 val reachableHere = valOf (IM.find (reachable, rep))
adamc@454 139
adamc@454 140 val (nodes, scc) = IS.foldl (fn (node, (nodes, scc)) =>
adamc@454 141 if node = rep then
adamc@454 142 (nodes, scc)
adamc@454 143 else
adamc@454 144 let
adamc@454 145 val reachableThere =
adamc@454 146 valOf (IM.find (reachable, node))
adamc@454 147 in
adamc@454 148 if IS.member (reachableThere, rep) then
adamc@454 149 (IS.delete (nodes, node),
adamc@454 150 IS.add (scc, node))
adamc@454 151 else
adamc@454 152 (nodes, scc)
adamc@454 153 end)
adamc@454 154 (IS.delete (nodes, rep), IS.singleton rep) reachableHere
adamc@454 155 in
adamc@454 156 sccs (nodes, scc :: acc)
adamc@454 157 end
adamc@454 158
adamc@454 159 val sccs = sccs (thisGroup, [])
adamc@519 160
adamc@454 161 (*val () = app (fn nodes => (print "SCC:";
adamc@454 162 IS.app (fn i => (print " ";
adamc@454 163 print (Int.toString i))) nodes;
adamc@454 164 print "\n")) sccs*)
adamc@454 165
adamc@454 166 fun depends nodes1 nodes2 =
adamc@454 167 let
adamc@454 168 val node1 = valOf (IS.find (fn _ => true) nodes1)
adamc@454 169 val node2 = valOf (IS.find (fn _ => true) nodes2)
adamc@454 170 val reachable1 = valOf (IM.find (reachable, node1))
adamc@454 171 in
adamc@454 172 IS.member (reachable1, node2)
adamc@454 173 end
adamc@454 174
adamc@454 175 fun findReady (sccs, passed) =
adamc@454 176 case sccs of
adamc@454 177 [] => raise Fail "Untangle: Unable to topologically sort 'val rec'"
adamc@454 178 | nodes :: sccs =>
adamc@454 179 if List.exists (depends nodes) passed
adamc@454 180 orelse List.exists (depends nodes) sccs then
adamc@454 181 findReady (sccs, nodes :: passed)
adamc@454 182 else
adamc@454 183 (nodes, List.revAppend (passed, sccs))
adamc@454 184
adamc@454 185 fun topo (sccs, acc) =
adamc@454 186 case sccs of
adamc@454 187 [] => rev acc
adamc@454 188 | _ =>
adamc@454 189 let
adamc@454 190 val (node, sccs) = findReady (sccs, [])
adamc@454 191 in
adamc@454 192 topo (sccs, node :: acc)
adamc@454 193 end
adamc@454 194
adamc@454 195 val sccs = topo (sccs, [])
adamc@519 196
adamc@454 197 (*val () = app (fn nodes => (print "SCC':";
adamc@454 198 IS.app (fn i => (print " ";
adamc@454 199 print (Int.toString i))) nodes;
adamc@454 200 print "\n")) sccs*)
adamc@454 201
adamc@454 202 fun isNonrec nodes =
adamc@454 203 case IS.find (fn _ => true) nodes of
adamc@454 204 NONE => NONE
adamc@454 205 | SOME node =>
adamc@454 206 let
adamc@454 207 val nodes = IS.delete (nodes, node)
adamc@454 208 val reachableHere = valOf (IM.find (reachable, node))
adamc@454 209 in
adamc@454 210 if IS.isEmpty nodes then
adamc@454 211 if IS.member (reachableHere, node) then
adamc@454 212 NONE
adamc@454 213 else
adamc@454 214 SOME node
adamc@454 215 else
adamc@454 216 NONE
adamc@454 217 end
adamc@454 218
adamc@454 219 val ds = map (fn nodes =>
adamc@454 220 case isNonrec nodes of
adamc@454 221 SOME node =>
adamc@454 222 let
adamc@454 223 val vi = valOf (List.find (fn (_, n, _, _, _) => n = node) vis)
adamc@454 224 in
adamc@454 225 (DVal vi, loc)
adamc@454 226 end
adamc@454 227 | NONE =>
adamc@454 228 (DValRec (List.filter (fn (_, n, _, _, _) => IS.member (nodes, n)) vis), loc))
adamc@454 229 sccs
adamc@454 230 in
adamc@454 231 ds
adamc@454 232 end
adamc@454 233 | _ => [dAll]
adamc@454 234 in
adamc@454 235 ListUtil.mapConcat decl file
adamc@454 236 end
adamc@454 237
adamc@454 238 end