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

SCM Repository

[diderot] View of /branches/charisee_dev/src/compiler/high-to-mid/einVarSet.sml
ViewVC logotype

View of /branches/charisee_dev/src/compiler/high-to-mid/einVarSet.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3540 - (download) (annotate)
Mon Jan 4 18:03:22 2016 UTC (4 years, 8 months ago) by cchiw
File size: 3351 byte(s)
all-lift(no split),postpone ein-to-low,krn subterms in ein
(* Expands probe ein
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * All rights reserved.
 *)

structure einVarSet = struct

    local
   
    structure E = Ein
    structure DstIL = MidIL
    structure DstOp = MidOps
    structure P=Printer
    structure MidToS=MidToString
    structure DstV  = DstIL.Var
    in


    fun decUse(DstIL.V{useCnt, ...}) = (useCnt := !useCnt - 1)
    fun getUse(DstIL.V{useCnt, ...})= Int.toString(!useCnt)
    datatype keyset
            = RHS of MidIL.var*MidIL.rhs
            | VAR of MidIL.var*MidIL.var


   fun cmp0(RHS(_,MidIL.EINAPP(e1,_)))=   HashEin.hashfn e1
     | cmp0(VAR(v0,v1))=  (DstIL.Var.hash v0)+(DstIL.Var.hash v1)
    | cmp0 (RHS(_,DstIL.OP(op1,_)))=DstOp.hash op1
    | cmp0(RHS(_,DstIL.LIT m))=Literal.hash m
    | cmp0(RHS(_,DstIL.CONS _ ))=0w17
    | cmp0(RHS(_,DstIL.VAR v ))=DstIL.Var.hash v

    fun cmp(e1,e2)=Word.compare(cmp0 e1,cmp0 e2)


   structure OprKey =
        struct
        type ord_key = keyset
        val compare = cmp
    end;
    structure  EinVarSet= RedBlackSetFn(OprKey);
    structure  VarSet= RedBlackSetFn(OprKey);


    fun allEq([], []) = true
      | allEq(x::xs, y::ys) = DstIL.Var.same(x,y) andalso allEq (xs,ys)
      | allEq _ = false

    fun setFindE (tbl,_,MidIL.EINAPP(ein0,arg0))= (
        EinVarSet.find(
            (fn (RHS(_,MidIL.EINAPP(ein1,arg1)))=>let
                    val t=EqualEin.isEinEqual(ein0,ein1)
                    val k= allEq(arg0,arg1)
                    val _= if t then
                        if k then ""
                            else (String.concat["*** \n\n ein1: ",P.printerE(ein0),"\n=\n ein2:",P.printerE(ein1)])
                        else  ""

                    in t andalso k end | _ =>false
        ))) tbl
    | setFindE (tbl,_,MidIL.OP(ein0,arg0))= (
        EinVarSet.find(
            (fn (RHS(_,MidIL.OP(ein1,arg1)))=>( MidOps.same(ein0,ein1) andalso allEq(arg0,arg1)) | _ =>false
            ))) tbl
    | setFindE (tbl,_,MidIL.VAR ein0)= (
        EinVarSet.find(
            (fn (RHS(_,MidIL.VAR ein1))=> DstIL.Var.same(ein0,ein1) | _ =>false
            ))) tbl
      | setFindE  _ = NONE

    fun varToStr v=String.concat[DstV.toString v, "(",getUse v,")"]
    fun pst(x,v)= (String.concat["\ncompare:",varToStr x ,"--tmp",varToStr v])
    fun setFindV (tbl,v)= (VarSet.find((fn (VAR(_,x))=> (pst(x,v);DstIL.Var.same(x,v)) | _ =>false))) tbl

    fun replaceArg(tbl,arg)= (case setFindV(tbl,arg)
        of NONE             =>  ((String.concat["\n NONE ARG ", varToStr arg]);arg)
        | SOME(VAR(x0,x1))  =>  (decUse(x1);(String.concat["\n found ARG ",varToStr x0,"=",varToStr x1]); x0)
        (*end case*))

    fun rtnVarN (tbl,(v,e))=(case setFindE(tbl,v,e)
        of NONE            => ((String.concat["\n RtnVar NONE ",varToStr v]);(EinVarSet.add(tbl,RHS(v,e)), NONE))
        | SOME(RHS(v1,_))  => ((String.concat["\n RtnVar SOME orig- ",varToStr v1," tmp-",varToStr v]);(EinVarSet.add(tbl,VAR(v1,v)),SOME v1))
    (*end case*))



    fun EinVarSetToString(str,RHS(v,MidIL.EINAPP(e,args)))=(String.concat["\n",str,DstV.toString v,"=",
        P.printerE e,"(",String.concatWith","(List.map DstV.toString args),")\n"])
        |EinVarSetToString _ =""


  end; (* local *)

end (* local *)

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