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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/mid-to-low/mid-to-low.sml
ViewVC logotype

View of /branches/vis15/src/compiler/mid-to-low/mid-to-low.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3625 - (download) (annotate)
Fri Jan 29 18:00:00 2016 UTC (3 years, 6 months ago) by jhr
File size: 5698 byte(s)
starting to work on mid-to-low phase
(* mid-to-low.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Translation from MidIR to LowIR representations.
 *)

structure MidToLow : sig

    val translate : MidIR.program -> LowIR.program

  end = struct

    structure SrcIR = MidIR
    structure SrcOp = MidOps
    structure SrcGV = SrcIL.GlobalVar
    structure SrcSV = SrcIR.StateVar
    structure SrcTy = MidTypes
    structure VTbl = SrcIR.Var.Tbl
    structure DstIR = LowIR
    structure DstTy = LowTypes
    structure DstOp = LowOps

  (* instantiate the translation environment *)
    structure Env = TranslateEnvFn (
      struct
        structure SrcIL = SrcIL
        structure DstIL = DstIL
        fun cvtTy ty = ty
      end)

    structure Env = TranslateEnvFn (
      struct
        structure SrcIR = SrcIR
        structure DstIR = DstIR
      
        type var_env = var_env
        type state_var_env = state_var_env
        val rename = rename
        val renameSV = renameSV
      end)

    fun expandOp (env, y, rator, args) = let
          val args' = Env.renameList (env, args)
          fun assign rator' = [(y, DstIR.OP(rator', args'))]
          fun dummy () = [(y, DstIR.LIT(Literal.Int 0))]
          in
            case rator
             of SrcOp.IAdd => assign DstOp.IAdd
              | SrcOp.ISub => assign DstOp.ISub
              | SrcOp.IMul => assign DstOp.IMul
              | SrcOp.IDiv => assign DstOp.IDiv
              | SrcOp.IMod => assign DstOp.IMod
              | SrcOp.INeg => assign DstOp.INeg
              | SrcOp.Abs ty => assign (DstOp.Abs ty)
              | SrcOp.LT ty => assign (DstOp.LT ty)
              | SrcOp.LTE ty => assign (DstOp.LTE ty)
              | SrcOp.EQ ty => assign (DstOp.EQ ty)
              | SrcOp.NEQ ty => assign (DstOp.NEQ ty)
              | SrcOp.GT ty => assign (DstOp.GT ty)
              | SrcOp.GTE ty => assign (DstOp.GTE ty)
              | SrcOp.Not => assign DstOp.Not
              | SrcOp.Max => assign DstOp.Max
              | SrcOp.Min => assign DstOp.Min
              | SrcOp.Clamp ty => assign (DstOp.Clamp ty)
              | SrcOp.Lerp ty => assign (DstOp.Lerp  ty)
              | SrcOp.Sqrt=>assign DstOp.Sqrt
              | SrcOp.Zero ty => assign (DstOp.Zero ty)
              | SrcOp.PrincipleEvec ty => assign (DstOp.PrincipleEvec  ty)
              | SrcOp.EigenVals2x2 => assign (DstOp.EigenVals2x2)
              | SrcOp.EigenVals3x3 => assign (DstOp.EigenVals3x3)
              | SrcOp.Select(ty as SrcTy.TupleTy tys, i) => assign (DstOp.Select( ty, i))
              | SrcOp.Index(ty, i) => assign (DstOp.Index( ty, i))
              | SrcOp.Subscript ty => assign (DstOp.Subscript  ty)
              | SrcOp.CeIRing d => assign (DstOp.CeIRing d)
              | SrcOp.Floor d => assign (DstOp.Floor d)
              | SrcOp.Round d => assign (DstOp.Round d)
              | SrcOp.Trunc d => assign (DstOp.Trunc d)
              | SrcOp.IntToReal => assign (DstOp.IntToReal)
              | SrcOp.RealToInt d => assign (DstOp.RealToInt d)
              | SrcOp.Kernel h => assign(DstOp.Kernel h)
              | SrcOp.Inside info => assign (DstOp.Inside info)
              | SrcOp.LoadImage(ty, nrrd, info) => assign (DstOp.LoadImage( ty, nrrd, info))
              | SrcOp.Input inp => assign (DstOp.Input inp)
              | SrcOp.Normalize d => assign (DstOp.Normalize d)
              | SrcOp.Transform V=> assign (DstOp.Transform V)
              | SrcOp.Translate V=> assign(DstOp.Translate V)
              | rator => raise Fail("bogus operator " ^ SrcOp.toString rator)
            (* end case *)
          end

    fun expandEinOp(env, y, e, args) = EinToLow.scan(y, e, Env.renameList(env, args))
	  handle ex => (
	    print ("error converting " ^ EinPP.expToString e);
	    raise ex)

  (* expand a SrcIR assignment to a DstIR CFG *)
    fun expand (env, (y, rhs)) = let
          val y' = Env.rename (env, y)
          fun assign rhs = [DstIR.ASSGN(y', rhs)]
          in 
            case rhs
             of SrcIR.GLOBAL x => assign (DstIR.GLOBAL(Env.renameGV(env, x)))
              | SrcIR.STATE x => assign (DstIR.STATE(Env.renameSV(env, x)))
              | SrcIR.VAR x => assign (DstIR.VAR(Env.rename(env, x)))
              | SrcIR.LIT lit => assign (DstIR.LIT lit)
              | SrcIR.OP(rator, args) => List.map DstIR.ASSGN (expandOp (env, y', rator, args))
              | SrcIR.CONS(args, ty) => assign (DstIR.CONS(Env.renameList(env, args), ty))
              | SrcIR.SEQ(args, ty) => assign (DstIR.SEQ(Env.renameList(env, args), ty))
              | SrcIR.EINAPP(rator, args) => expandEinOp (env, Env.rename (env, y), rator, args)
            (* end case *)
          end

  (* expand a SrcIR multi-assignment to a DstIR CFG *)
    fun mexpand (env, (ys, rator, xs)) = let
          val ys' = Env.renameList(env, ys)
          val rator' = (case rator
                 of SrcOp.EigenVecs2x2 => DstOp.EigenVecs2x2
                  | SrcOp.EigenVecs3x3 => DstOp.EigenVecs3x3
                  | SrcOp.Print tys => DstOp.Print tys
                  | _ => raise Fail("bogus operator " ^ SrcOp.toString rator)
                (* end case *))
          val xs' = Env.renameList(env, xs)
          val nd = DstIR.Node.mkMASSIGN(ys', rator', xs')
          in
            DstIR.CFG{entry=nd, exit=nd}
          end

    structure Trans =  TranslateFn (
      struct
        open Env
        val expand = DstIR.CFG.mkBlock o expand
        val mexpand = mexpand
      end)

    fun translate prog = let
          val prog = Trans.translate prog
          in
            LowIRCensus.init prog;
            prog
          end

  end

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