1 (* Copyright (C) 2005-2007 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
8 functor PrePasses2 (S: PREPASSES2_STRUCTS): PREPASSES2 =
13 structure DeadBlocks =
16 fun eliminateFunction f =
18 val {args, blocks, mayInline, name, raises, returns, start} =
20 val {get = isLive, set = setLive, rem} =
21 Property.getSetOnce (Label.plist, Property.initConst false)
22 val _ = Function.dfs (f, fn Block.T {label, ...} =>
23 (setLive (label, true)
26 if Vector.forall (blocks, isLive o Block.label)
32 (blocks, isLive o Block.label)
34 Function.new {args = args,
36 mayInline = mayInline,
42 val _ = Vector.foreach (blocks, rem o Block.label)
47 fun eliminate (Program.T {datatypes, globals, functions, main}) =
48 Program.T {datatypes = datatypes,
50 functions = List.revMap (functions, eliminateFunction),
54 val eliminateDeadBlocksFunction = DeadBlocks.eliminateFunction
55 (* quell unused warning *)
56 val _ = eliminateDeadBlocksFunction
57 val eliminateDeadBlocks = DeadBlocks.eliminate
63 fun orderFunctions (p as Program.T {globals, datatypes, main, ...}) =
65 val functions = ref []
70 val {args, mayInline, name, raises, returns, start, ...} =
76 (List.push (blocks, b)
78 val f = Function.new {args = args,
79 blocks = Vector.fromListRev (!blocks),
80 mayInline = mayInline,
86 List.push (functions, f)
90 Program.T {datatypes = datatypes,
92 functions = List.rev (!functions),
98 val orderFunctions = Order.orderFunctions
104 fun reverseFunctions (Program.T {globals, datatypes, functions, main}) =
105 Program.T {datatypes = datatypes,
107 functions = List.rev functions,
111 val reverseFunctions = Reverse.reverseFunctions