comparison src/mono_shake.sml @ 134:18299126a343

MonoShake
author Adam Chlipala <adamc@hcoop.net>
date Thu, 17 Jul 2008 13:12:16 -0400
parents
children f0d3402184d1
comparison
equal deleted inserted replaced
133:55d8cfa4d024 134:18299126a343
1 (* Copyright (c) 2008, Adam Chlipala
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - The names of contributors may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 *)
27
28 (* Remove unused definitions from a file *)
29
30 structure MonoShake :> MONO_SHAKE = struct
31
32 open Mono
33
34 structure U = MonoUtil
35
36 structure IS = IntBinarySet
37 structure IM = IntBinaryMap
38
39 type free = {
40 con : IS.set,
41 exp : IS.set
42 }
43
44 fun shake file =
45 let
46 val page_es = List.foldl
47 (fn ((DExport (_, n, _), _), page_es) => n :: page_es
48 | (_, page_es) => page_es) [] file
49
50 val (cdef, edef) = foldl (fn ((DVal (_, n, t, e, _), _), (cdef, edef)) => (cdef, IM.insert (edef, n, (t, e)))
51 | ((DValRec vis, _), (cdef, edef)) =>
52 (cdef, foldl (fn ((_, n, t, e, _), edef) => IM.insert (edef, n, (t, e))) edef vis)
53 | ((DExport _, _), acc) => acc)
54 (IM.empty, IM.empty) file
55
56 fun typ (_, s) = s
57
58 fun exp (e, s) =
59 case e of
60 ENamed n =>
61 if IS.member (#exp s, n) then
62 s
63 else
64 let
65 val s' = {exp = IS.add (#exp s, n),
66 con = #con s}
67 in
68 case IM.find (edef, n) of
69 NONE => s'
70 | SOME (t, e) => shakeExp s' e
71 end
72 | _ => s
73
74 and shakeExp s = U.Exp.fold {typ = typ, exp = exp} s
75
76 val s = {con = IS.empty, exp = IS.addList (IS.empty, page_es)}
77
78 val s = foldl (fn (n, s) =>
79 case IM.find (edef, n) of
80 NONE => raise Fail "Shake: Couldn't find 'val'"
81 | SOME (t, e) => shakeExp s e) s page_es
82 in
83 List.filter (fn (DVal (_, n, _, _, _), _) => IS.member (#exp s, n)
84 | (DValRec vis, _) => List.exists (fn (_, n, _, _, _) => IS.member (#exp s, n)) vis
85 | (DExport _, _) => true) file
86 end
87
88 end