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

SCM Repository

[diderot] View of /branches/charisee/src/compiler/high-il/normalize.sml
ViewVC logotype

View of /branches/charisee/src/compiler/high-il/normalize.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2870 - (download) (annotate)
Wed Feb 25 21:47:43 2015 UTC (4 years, 5 months ago) by cchiw
File size: 5790 byte(s)
added sqrt,pow, and examples
(* normalize.sml
 *
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *)

structure Normalize : sig

    val transform : HighIL.program -> HighIL.program

  end = struct


    structure IL = HighIL
    structure Op = HighOps
    structure V = IL.Var
    structure Ty = HighILTypes
    structure ST = Stats
    structure NE = NormalizeEin
    structure P=Printer
    structure Order=OrderEin
    structure mk=mkOperators
    structure App=App
    structure S=SummationEin


(********** Counters for statistics **********)
    val cntInsideScale          = ST.newCounter "high-opt:inside-scale"
    val cntInsideOffset         = ST.newCounter "high-opt:inside-offset"
    val cntInsideNeg            = ST.newCounter "high-opt:inside-meg"
    val cntInsideCurl           = ST.newCounter "high-opt:inside-curl"
    val cntInsideDiff           = ST.newCounter "high-opt:inside-diff"
    val cntProbeAdd             = ST.newCounter "high-opt:probe-add"
    val cntProbeSub             = ST.newCounter "high-opt:probe-sub"
    val cntProbeScale           = ST.newCounter "high-opt:probe-scale"
    val cntProbeOffset          = ST.newCounter "high-opt:probe-offset"
    val cntProbeNeg             = ST.newCounter "high-opt:probe-neg"
    val cntProbeCurl            = ST.newCounter "high-opt:probe-curl"
    val cntDiffField            = ST.newCounter "high-opt:diff-field"
    val cntDiffAdd              = ST.newCounter "high-opt:diff-add"
    val cntDiffScale            = ST.newCounter "high-opt:diff-scale"
    val cntDiffOffset           = ST.newCounter "high-opt:diff-offset"
    val cntDiffNeg              = ST.newCounter "high-opt:diff-neg"
    val cntCurlScale            = ST.newCounter "high-opt:curl-scale"
    val cntCurlNeg              = ST.newCounter "high-opt:curl-neg"
    val cntUnused               = ST.newCounter "high-opt:unused"
    val firstCounter            = cntInsideScale
    val lastCounter             = cntUnused

    structure UnusedElim = UnusedElimFn (
        structure IL = IL
        val cntUnused = cntUnused)

    fun useCount (IL.V{useCnt, ...}) = !useCnt
   (* adjust a variable's use count *)
    fun incUse (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
    fun decUse (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)
    fun use x = (incUse x; x)
    fun isEin x = (case V.binding x
        of IL.VB_RHS(IL.EINAPP (e,arg))=>SOME(IL.EINAPP (e,arg))
        | IL.VB_RHS(IL.VAR x') => isEin x'
        |_=>NONE
    (* end case *))

    val testing=1
    fun testp e=(case testing
        of 1 => (print(String.concat e);1)
        | _ => 1
    (*end case*))

    (*doNormalize:EIN->EIN
    * Orders EIN, normalizes it, then cleans the summation
    *)
    fun doNormalize(e,args)=let
        val _ = testp ["\n in do Normalize"]
        val ordered=Order.orderfn e
        val (n,change)=NE.normalize(ordered,args)
        val e'=S.cleanSummation n
        val _  =testp ["original \n =>\n",P.printerE(e),"\n Ordered:\n",P.printerE(ordered),"\n normalized\n=>",P.printerE(n),"\n Move Sums \n =>", P.printerE(e')]
        in
            e'
        end

    (*foundEIN:EIN.Params*int*int*EIN*HighIL Vars* rhs list* EIN  *HighIL Var
        ->int*EIN*int*rhs list
    * Orders EIN, normalizes it, then cleans the summation
    *orig-original EIN 
    *)
    fun foundEin(paramCount,place,change,newE,newArgs,done,newEinApp,orig,lhs)= (case (List.nth(paramCount, place))
        of Ein.TEN(0,_)=> (change,orig, place+1,  done@[newEinApp])
        | _ => let
            val rtnArgs=done@newArgs
            val (c,subst)=App.app(orig,place,newE,newArgs,done)
            val a=String.concatWith " , " (List.map V.toString newArgs)
            val b=String.concatWith " , " (List.map V.toString done)
            val r=String.concatWith " , " (List.map V.toString rtnArgs)

            val _ = testp["\n Apply at ", Int.toString(place), ":--",P.printerE(newE),
                "\nArgs:",b,"\n newArgs:",a,"\nrtnArgs:",r,
                    "\n","Subst Result:\t", P.printerE(subst), "\n"]
            in (case c
                of 0 => (1,subst, place+length(newArgs), rtnArgs)
                |_ => (use lhs;(1,subst, place+length(newArgs), rtnArgs))
                (*end case*))
            end
        (*end case*))

    (*
    *doRHS: HighIL.var * rhs -> option rhs
    *Looks at each argument to the original EINAPP.
    *If it is another EIN APP calls foundEIN to do application
    * "place"-The Param-id for the EIN operator.
    * Keeps track of the place of the argument in substitution.
    *)
    fun doRHS (lhs, IL.EINAPP (ein, args))=let
        val _ = testp ["\n\n**************************\n\n\n",V.toString  lhs,"=="," ---Current: ", P.printerE(ein), (String.concatWith "," (List.map V.toString args))]
        fun rewrite(0,_,_, [], _)= NONE
            | rewrite(_,orig,_, [], args')= SOME[(lhs,IL.EINAPP(doNormalize(orig,args'), args'))]
            | rewrite(change,orig, place, e::es,args')=(case (isEin e)
                of NONE => rewrite(change,orig, place+1, es, args'@[e])
                | SOME(IL.EINAPP (newE,newA))=> let

                    val Ein.EIN{params, index, body}=orig
                    val (change',e',place',done')=foundEin(params,place,change,newE,newA,args',e,orig,lhs)
                    in rewrite(change',e',place',es,done')
                    end
                | _ => raise Fail"isEin did not work"
                (* end case *))
        in rewrite(0,ein, 0,args,[])
        end
    | doRHS _ = NONE

    structure Rewrite = RewriteFn (
      struct
        structure IL = IL
        val doAssign = doRHS
        fun doMAssign _ = NONE
        val elimUnusedVars = UnusedElim.reduce
      end)

    val transform = Rewrite.transform

  end

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