Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/high-opt/normalize.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/high-opt/normalize.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3513 - (view) (download)

1 : jhr 3513 (* normalize.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     structure Normalize : sig
10 :    
11 :     val transform : HighIR.program -> HighIR.program
12 :    
13 :     end = struct
14 :    
15 :     structure IR = HighIR
16 :     structure Op = HighOps
17 :     structure V = IR.Var
18 :    
19 :     structure UnusedElim = UnusedElimFn (
20 :     structure IR = IR
21 :     val cntUnused = cntUnused)
22 :    
23 :     fun useCount (IR.V{useCnt, ...}) = !useCnt
24 :    
25 :     (* adjust a variable's use count *)
26 :     fun incUse (IR.V{useCnt, ...}) = (useCnt := !useCnt + 1)
27 :     fun decUse (IR.V{useCnt, ...}) = (useCnt := !useCnt - 1)
28 :     fun use x = (incUse x; x)
29 :    
30 :     fun isEin x = (case V.binding x
31 :     of IR.VB_RHS(rhs as IR.EINAPP(e, arg)) => SOME rhs
32 :     | IR.VB_RHS(IR.VAR x') => isEin x'
33 :     | _ => NONE
34 :     (* end case *))
35 :    
36 :     (* doNormalize : EIN->EIN
37 :     * Orders EIN, normalizes it, then cleans the summation
38 :     *)
39 :     fun doNormalize (e, args) = let
40 :     val ordered = OrderEin.orderfn e
41 :     val (n, change) = NormalizeEin.normalize(ordered, args)
42 :     in
43 :     S.cleanSummation n
44 :     end
45 :    
46 :     fun nameCnt e = String.concat[V.toString e, "(", Int.toString(useCount e), ")"]
47 :     fun nameCnts e = String.concatWithMap "" nameCnt e
48 :    
49 :     (* rewriteEin : EIN.Params*int*int*EIN*HighIR Vars* rhs list* EIN *HighIR Var
50 :     * -> int*EIN*int*rhs list
51 :     * Orders EIN, normalizes it, then cleans the summation orig-original EIN
52 :     *)
53 :     fun rewriteEin (paramCount, place, changed, newE, newArgs, done, newEinApp, orig, lhs)= (
54 :     case List.nth(paramCount, place)
55 :     of Ein.TEN(0, _)=> (changed, orig, place+1, done@[newEinApp])
56 :     | _ => let
57 :     val rtnArgs = done @ newArgs
58 :     val (c, subst) = App.app(orig, place, newE, newArgs, done)
59 :     in
60 :     if (c = 0)
61 :     then (1, subst, place + length newArgs, rtnArgs)
62 :     else (
63 :     use lhs; List.app use newArgs; decUse newEinApp;
64 :     (true, subst, place + length newArgs, rtnArgs))
65 :     end
66 :     (* end case *))
67 :    
68 :     (* doRHS: HighIR.var * rhs -> (var * rhs) list option
69 :     * Looks at each argument to the original EINAPP.
70 :     * If it is another EIN APP calls foundEIN to do application
71 :     * "place"-The Param-id for the EIN operator.
72 :     * Keeps track of the place of the argument in substitution.
73 :     *)
74 :     fun doRHS (lhs, IR.EINAPP(ein, args)) = let
75 :     fun rewrite (false, _, _, [], _) = NONE
76 :     | rewrite (true, orig, _, [], args') =
77 :     SOME[(lhs, IR.EINAPP(doNormalize(orig, args'), args'))]
78 :     | rewrite (changed, orig, place, e::es, args') = (case isEin e
79 :     of NONE => rewrite(changed, orig, place+1, es, args'@[e])
80 :     | SOME(IR.EINAPP(newE, newA)) => let
81 :     val Ein.EIN{params, index, body} = orig
82 :     val (changed, e', place', done') =
83 :     rewriteEin (params, place, changed, newE, newA, args', e, orig, lhs)
84 :     in
85 :     rewrite(changed, e', place', es, done')
86 :     end
87 :     | _ => raise Fail"isEin did not work"
88 :     (* end case *))
89 :     in
90 :     rewrite (false, ein, 0, args, [])
91 :     end
92 :     | doRHS _ = NONE
93 :    
94 :     structure Rewrite = RewriteFn (
95 :     struct
96 :     structure IR = IR
97 :     val doAssign = doRHS
98 :    
99 :     fun doMAssign _ = NONE
100 :     val elimUnusedVars = UnusedElim.reduce
101 :     end)
102 :    
103 :     val transform = Rewrite.transform
104 :    
105 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0