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

SCM Repository

[diderot] Diff of /branches/charisee/src/compiler/high-to-mid/cleanParam.sml
ViewVC logotype

Diff of /branches/charisee/src/compiler/high-to-mid/cleanParam.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2844, Tue Dec 9 18:05:29 2014 UTC revision 2845, Fri Dec 12 06:46:23 2014 UTC
# Line 1  Line 1 
1  (*  (*
2  cleanParam.sml cleans the parameters in an EIN expression.  *cleanParam.sml cleans the parameters in an EIN expression.
3  Cleaning parameters is simple.  *Cleaning parameters is simple.
4  We keep track of all the paramids used in subexpression(getIdCount()), remap the param ids(mkMap)  *We keep track of all the paramids used in subexpression(getIdCount()),
5  and choosing the mid-il args that are used, and then lastly rewrites the body.  *remap the param ids(mkMap)
6    *and choosing the mid-il args that are used, and then lastly rewrites the body.
7  *)  *)
8  structure cleanParams= struct  structure cleanParams= struct
9    
10      local      local
11    
12      structure E = Ein      structure E = Ein
     structure SrcIL = HighIL  
13      structure DstIL = MidIL      structure DstIL = MidIL
     structure DstV = DstIL.Var  
     structure SrcV = SrcIL.Var  
14      structure P=Printer      structure P=Printer
15        structure DstV = DstIL.Var
16    
17      in      in
18      val testing=0      val testing=1
   
   
19  fun insert (key, value) d =fn s =>  fun insert (key, value) d =fn s =>
20      if s = key then SOME value      if s = key then SOME value
21      else d s      else d s
   
22  fun lookup k d = d k  fun lookup k d = d k
23  val empty =fn key =>NONE  val empty =fn key =>NONE
24  fun err str=raise Fail str  fun err str=raise Fail str
25  fun itos i =Int.toString i      fun iTos i =Int.toString i
26  fun setEin(params,index,body)=Ein.EIN{params=params, index=index, body=body}  fun setEin(params,index,body)=Ein.EIN{params=params, index=index, body=body}
27        fun cnt(DstIL.V{useCnt, ...}) = !useCnt
28        fun incUse (DstIL.V{useCnt, ...}) = (useCnt := !useCnt + 2)
29        fun use x = (incUse x; x)
30        fun getUse e=String.concat["\n\t Varname "^DstIL.Var.name e," -use ",iTos(cnt e)]
31  fun assignEinApp(y,params,index,body,args)= (y,DstIL.EINAPP(setEin(params,index,body),args))  fun assignEinApp(y,params,index,body,args)= (y,DstIL.EINAPP(setEin(params,index,body),args))
32  fun testp n=(case testing  fun testp n=(case testing
33      of 0=> 1      of 0=> 1
34      | _ =>(print(String.concat n);1)      | _ =>(print(String.concat n);1)
35      (*end case*))      (*end case*))
36    
   
37  (*dictionary to lookup mapp*)  (*dictionary to lookup mapp*)
38  fun lkupIndexSingle(e1,mapp,str)=(case (lookup e1 mapp)  fun lkupIndexSingle(e1,mapp,str)=(case (lookup e1 mapp)
39      of SOME l=>l      of SOME l=>l
40      | _=> raise Fail(str^Int.toString(e1))          | _=> raise Fail(str^iTos(e1))
41      (*endcase*))      (*endcase*))
42    
   
43  (*mkMapp:dict*params*var list ->dict*params*var list  (*mkMapp:dict*params*var list ->dict*params*var list
44  countmapp dictionary keeps track of which ids have been used      * countmapp dictionary keeps track of which ids have been used
45  mapp id the dictionary of the new ids      * mapp id the dictionary of the new ids
46  *)  *)
47  fun mkMapp(countmapp,params,args)=let  fun mkMapp(countmapp,params,args)=let
   
48      val n=(length params)      val n=(length params)
49      val ix=List.tabulate(n, (fn e=> e))      val ix=List.tabulate(n, (fn e=> e))
50      fun m([],_,mapp,p, _, a, _)=(mapp,p,a)      fun m([],_,mapp,p, _, a, _)=(mapp,p,a)
51      | m(i::ix,j,mapp,p, p1::params, a, a1::arg)=(case (lookup i countmapp)      | m(i::ix,j,mapp,p, p1::params, a, a1::arg)=(case (lookup i countmapp)
52          of SOME _=>let          of SOME _=>let
53              val _ =testp["\nInserting into dictionary",itos i,"=>",itos j]                  val _ =testp["\nInserting into dictionary",iTos i,"=>",iTos j]
54              val mapp2=insert(i,j) mapp              val mapp2=insert(i,j) mapp
55              in              in
56                   m(ix,j+1,mapp2,p@[p1],params, a@[a1],arg)                   m(ix,j+1,mapp2,p@[p1],params, a@[a1],arg)
57              end              end
58          | _ =>  m(ix,j,mapp,p, params, a, arg)          | _ =>  m(ix,j,mapp,p, params, a, arg)
59          (*end case*))          (*end case*))
60            val _ =testp["ix created up to",iTos(n),"length of params",
61      val _ =testp["ix created up to",itos(n),"length of params",itos(length params),"lengths of args",itos(length args)]                  iTos(length params),"lengths of args",iTos(length args)]
62      val  (mapp,Nparams,Nargs)=m(ix, 0, empty, [],params,[],args)      val  (mapp,Nparams,Nargs)=m(ix, 0, empty, [],params,[],args)
63      in      in
64          (mapp,Nparams,Nargs)          (mapp,Nparams,Nargs)
65      end      end
66    
   
67  (*getIdCount: ein_exp ->dict  (*getIdCount: ein_exp ->dict
68  *rewrite ids in exp using mapp  *rewrite ids in exp using mapp
69  *)  *)
 (*creates mapp that shows which ids were used *)  
70  fun getIdCount e=let  fun getIdCount e=let
71      fun rewriteExp(e,mapp)=let      fun rewriteExp(e,mapp)=let
72          fun iterList([],mapp)=mapp          fun iterList([],mapp)=mapp
# Line 125  Line 118 
118          | E.Delta(i,j)         => e          | E.Delta(i,j)         => e
119          | E.Epsilon(i,j,k)     => e          | E.Epsilon(i,j,k)     => e
120          | E.Eps2(i,j)          => e          | E.Eps2(i,j)          => e
121          | E.Const _            => E.Const 0              | E.Const _            => e
122          | E.Field _            => E.Const 0          | E.Field _            => E.Const 0
123          | E.Partial _          => E.Const 0          | E.Partial _          => E.Const 0
124          | E.Apply _            => E.Const 0          | E.Apply _            => E.Const 0
# Line 138  Line 131 
131          rewriteExp e          rewriteExp e
132      end      end
133    
 (* cleanParams2:ein_exp*params* var list ->params*arg list*ein_exp  
 * make map for param ids and rewrite ein_exp  
 *)  
 fun cleanParams2(e,params,args) =let  
     val _ =testp["Cleaning e ",P.printbody e ]  
     val countmapp=getIdCount e  
     val (mapp,Nparams,Nargs)=mkMapp(countmapp,params,args)  
     val Nbody=rewriteParam(mapp,e)  
     in  
         (Nparams,Nargs,Nbody)  
     end  
   
134  (* cleanParams:var*ein_exp*param*index* var list ->code  (* cleanParams:var*ein_exp*param*index* var list ->code
135  *cleans params  *cleans params
136  *)  *)
137  fun cleanParams(y,body,params,index,args)=let  fun cleanParams(y,body,params,index,args)=let
138      val (Nparams,Nargs,Nbody)=cleanParams2(body,params,args)          (*val _ =print(String.concat["\n Cleaning e ",P.printbody body])*)
139            val countmapp=getIdCount body
140            val (mapp,Nparams,Nargs)=mkMapp(countmapp,params,args)
141            val Nargs =List.map (fn e=> use e) Nargs
142            val Nbody=rewriteParam(mapp,body)
143      val einapp= assignEinApp(y,Nparams,index,Nbody,Nargs)      val einapp= assignEinApp(y,Nparams,index,Nbody,Nargs)
144      in      in
145          einapp          einapp
146      end      end
147    
   
 (* isZero:ein_exp->int  
 *  Checks to see if the entire body is 0. If so returns 1.  
 *)  
 fun isZero e=let  
     fun iterList []= 1  
     | iterList(e1::list)=(case (isZero e1)  
         of 0=>0  
         | _ => iterList list  
         (*end case*))  
     fun iterProdList []= 0  
     | iterProdList(e1::list)=(case (isZero e1)  
         of 1=>1  
         | _ => iterList list  
         (*end case*))  
   
     in (case e  
         of E.Add e             => iterList e  
         | E.Sub(e1,e2)         => iterList [e1,e2]  
         | E.Div(e1,e2)         => isZero e1  
         | E.Sum(_ ,e)          => isZero e  
         | E.Prod e             => iterProdList e  
         | E.Neg e              => isZero e  
         | E.Probe(e,_)         => isZero e  
         | E.Const 0            => 1  
         | _                    => 0  
         (*end case*))  
     end  
   
   
148    end; (* local *)    end; (* local *)
149    
150  end (* local *)  end (* local *)

Legend:
Removed from v.2844  
changed lines
  Added in v.2845

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