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 3743 - (download) (annotate)
Mon Apr 11 16:46:49 2016 UTC (3 years, 4 months ago) by cchiw
File size: 7732 byte(s)
added control index operator
(* 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 = SrcIR.GlobalVar
    structure SrcSV = SrcIR.StateVar
    structure SrcTy = MidTypes
    structure VTbl = SrcIR.Var.Tbl
    structure DstIR = LowIR
    structure DstTy = LowTypes
    structure DstOp = LowOps

    fun cvtTy ty = ty

  (* instantiate the translation environment *)
    structure Env = TranslateEnvFn (
      struct
        structure SrcIR = SrcIR
        structure DstIR = DstIR
        val cvtTy = cvtTy
      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))]
          fun copy () = let val [x] = args
                in
                  [(y, DstIR.VAR(Env.rename(env, x)))]
                end
          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.LT ty => assign (DstOp.LT(cvtTy ty))
              | SrcOp.LTE ty => assign (DstOp.LTE(cvtTy ty))
              | SrcOp.EQ ty => assign (DstOp.EQ(cvtTy ty))
              | SrcOp.NEQ ty => assign (DstOp.NEQ(cvtTy ty))
              | SrcOp.GT ty => assign (DstOp.GT(cvtTy ty))
              | SrcOp.GTE ty => assign (DstOp.GTE(cvtTy ty))
              | SrcOp.Not => assign DstOp.Not
              | SrcOp.Abs ty => assign (DstOp.Abs(cvtTy ty))
              | SrcOp.Max ty => assign (DstOp.Max(cvtTy ty))
              | SrcOp.Min ty => assign (DstOp.Min(cvtTy ty))
              | SrcOp.Clamp ty => assign (DstOp.Clamp(cvtTy ty))
              | SrcOp.Lerp ty => assign (DstOp.Lerp(cvtTy ty))
              | SrcOp.EigenVecs2x2 => assign (DstOp.EigenVecs2x2)
              | SrcOp.EigenVecs3x3 => assign (DstOp.EigenVecs3x3)
              | SrcOp.EigenVals2x2 => assign (DstOp.EigenVals2x2)
              | SrcOp.EigenVals3x3 => assign (DstOp.EigenVals3x3)
              | SrcOp.Zero ty => assign (DstOp.Zero(cvtTy ty))
              | SrcOp.Select(ty, i) => assign (DstOp.Select(cvtTy ty, i))
              | SrcOp.Index(ty, i) => assign (DstOp.Index(cvtTy ty, i))
              | SrcOp.Subscript ty => assign (DstOp.Subscript(cvtTy ty))
              | SrcOp.MkDynamic(ty, n) => assign (DstOp.MkDynamic(cvtTy ty, n))
              | SrcOp.Append ty => assign (DstOp.Append(cvtTy ty))
              | SrcOp.Prepend ty => assign (DstOp.Prepend(cvtTy ty))
              | SrcOp.Concat ty => assign (DstOp.Concat(cvtTy ty))
	      | SrcOp.Range => assign DstOp.Range
              | SrcOp.Length ty => assign (DstOp.Length(cvtTy ty))
	      | SrcOp.SphereQuery(ty1, ty2) => assign (DstOp.SphereQuery(cvtTy ty1, cvtTy ty2))
              | SrcOp.Ceiling d => assign (DstOp.Ceiling 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)
(*
R_All
R_Exists
R_Max
R_Min
R_Sum
R_Product
R_Mean
R_Variance
*)
	          | SrcOp.BuildPos s => let
                   val [pos] = args'
                    (* create positions from 1-s.. to s *)
                    val range = 2*s
                    fun f (0, vPs, Ps) = (vPs, Ps)
                    | f(i, vPs, Ps) =  let
                        val e1 = i-s
                        val n = Int.toString e1
                        val vL = DstIR.Var.new ("real"^n, DstTy.realTy)
                        val L = (vL, DstIR.LIT(Literal.Real(RealLit.fromInt(IntInf.fromInt(e1)))))
                        val vP = DstIR.Var.new ("sub"^n, DstTy.realTy)
                        val P = (vP, DstIR.OP(DstOp.RSub, [pos, vL]))
                        in f(i-1, vP::vPs, L::P::Ps) end
                    val (vCs, Cs) = f(range, [], [])
                    in
                        List.rev((y, DstIR.CONS(vCs, DstTy.TensorTy [range]))::Cs)
                    end
              | SrcOp.EvalKernel(d, h, k) => EvalKern.expand(y, d, h, k, args')
              | SrcOp.Transform img => assign (DstOp.Transform img)
              | SrcOp.Translate img => assign (DstOp.Translate img)
              | SrcOp.Kernel _ => dummy()
(*
VoxelAddress
VoxelAddressWithCtl
LoadVoxels
*)
              | SrcOp.Inside info => assign (DstOp.Inside info)
              | SrcOp.ImageDim(info, i) => assign (DstOp.ImageDim(info, i))
              | SrcOp.BorderCtlDefault info => (case args
                   (* of [x, _] => [(y, DstIR.VAR(Env.rename(env, x)))] *)
                    of [x] => [(y, DstIR.VAR(Env.rename(env, x)))]
                  (* end case *))
              | SrcOp.BorderCtlClamp info => copy()
              | SrcOp.BorderCtlMirror info => copy()
              | SrcOp.BorderCtlWrap info => copy()
              | SrcOp.LoadSeq(ty, nrrd) => assign (DstOp.LoadSeq(cvtTy ty, nrrd))
              | SrcOp.LoadImage(ty, nrrd) => assign (DstOp.LoadImage(cvtTy ty, nrrd))
	      | SrcOp.MathFn f => assign (DstOp.MathFn f)
              | rator => raise Fail("bogus operator " ^ SrcOp.toString rator)
            (* end case *)
          end

  (* 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), cvtTy ty))
              | SrcIR.SEQ(args, ty) => assign (DstIR.SEQ(Env.renameList(env, args), cvtTy ty))
              | SrcIR.EINAPP(rator, args) =>
                    EinToLow.expand (y', rator, Env.renameList(env, args))
	handle ex => (print(concat(["error converting  \n",EinPP.toString(rator)] @List.map (fn e => SrcIR.Var.name (e)^",") args)); raise ex)
            (* 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
            LowCensus.init prog;
            prog
          end

  end

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