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

SCM Repository

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

View of /branches/vis15/src/compiler/low-ir/check-low.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4480 - (download) (annotate)
Thu Sep 1 14:39:27 2016 UTC (2 years, 10 months ago) by jhr
File size: 8316 byte(s)
  Working on merge: adding support for global stabilize
(* check-low.sml
 *
 * 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 CheckOps : OPERATOR_TY = struct

    structure Op = LowOps
    structure Ty = LowTypes

    type rator = Op.rator
    type ty = Ty.ty

    val vec3Ty = Ty.vecTy 3

    fun chkIndex (idx, bnd) = ((0 <= idx) andalso (idx < bnd))

  (* Return the signature of a LowIR operator. *)
    fun sigOf rator = (case rator
           of Op.IAdd => (Ty.IntTy, [Ty.IntTy, Ty.IntTy])
            | Op.ISub => (Ty.IntTy, [Ty.IntTy, Ty.IntTy])
            | Op.IMul => (Ty.IntTy, [Ty.IntTy, Ty.IntTy])
            | Op.IDiv => (Ty.IntTy, [Ty.IntTy, Ty.IntTy])
            | Op.IMod => (Ty.IntTy, [Ty.IntTy, Ty.IntTy])
            | Op.INeg => (Ty.IntTy, [Ty.IntTy])
            | Op.RAdd => (Ty.realTy, [Ty.realTy, Ty.realTy])
            | Op.RSub => (Ty.realTy, [Ty.realTy, Ty.realTy])
            | Op.RMul => (Ty.realTy, [Ty.realTy, Ty.realTy])
            | Op.RDiv => (Ty.realTy, [Ty.realTy, Ty.realTy])
            | Op.RNeg => (Ty.realTy, [Ty.realTy])
            | Op.RClamp => (Ty.realTy, [Ty.realTy, Ty.realTy, Ty.realTy])
            | Op.RLerp => (Ty.realTy, [Ty.realTy, Ty.realTy, Ty.realTy])
            | Op.VAdd d => (Ty.vecTy d, [Ty.vecTy d, Ty.vecTy d])
            | Op.VSub d => (Ty.vecTy d, [Ty.vecTy d, Ty.vecTy d])
            | Op.VScale d => (Ty.vecTy d, [Ty.realTy, Ty.vecTy d])
            | Op.VMul d => (Ty.vecTy d, [Ty.vecTy d, Ty.vecTy d])
            | Op.VNeg d => (Ty.vecTy d, [Ty.vecTy d])
            | Op.VSum d => (Ty.realTy, [Ty.vecTy d])
            | Op.VDot d => (Ty.realTy, [Ty.vecTy d, Ty.vecTy d])
            | Op.VIndex(d, idx) =>
                if chkIndex (idx, d)
                  then (Ty.realTy, [Ty.vecTy d])
                  else raise Fail("sigOf: invalid index in operator " ^ Op.toString rator)
            | Op.VClamp d => (Ty.vecTy d, [Ty.vecTy d, Ty.realTy, Ty.realTy])
            | Op.VMapClamp d => (Ty.vecTy d, [Ty.vecTy d, Ty.vecTy d, Ty.vecTy d])
            | Op.VLerp d => (Ty.vecTy d, [Ty.vecTy d, Ty.vecTy d, Ty.realTy])
            | Op.TensorIndex(ty as Ty.TensorTy shp, idxs) =>
                if ListPair.allEq chkIndex (idxs, shp)
                  then (Ty.realTy, [ty])
                  else raise Fail("sigOf: invalid index in operator " ^ Op.toString rator)
            | Op.ProjectLast(ty as Ty.TensorTy(shp as _::_::_), idxs) => let
                fun chk ([], [_]) = true
                  | chk (idx::idxs, d::dd) = chkIndex (idx, d) andalso chk (idxs, dd)
                  | chk _ = false
                in
                  if chk (idxs, shp)
                    then (Ty.TensorTy[List.last shp], [ty])
                    else raise Fail("sigOf: invalid index in operator " ^ Op.toString rator)
                end
            | Op.LT ty => (Ty.BoolTy, [ty, ty])
            | Op.LTE ty => (Ty.BoolTy, [ty, ty])
            | Op.EQ ty => (Ty.BoolTy, [ty, ty])
            | Op.NEQ ty => (Ty.BoolTy, [ty, ty])
            | Op.GT ty => (Ty.BoolTy, [ty, ty])
            | Op.GTE ty => (Ty.BoolTy, [ty, ty])
            | Op.BAnd => (Ty.BoolTy, [Ty.BoolTy, Ty.BoolTy])
            | Op.BOr => (Ty.BoolTy, [Ty.BoolTy, Ty.BoolTy])
            | Op.BNot => (Ty.BoolTy, [Ty.BoolTy])
            | Op.Abs ty => (ty, [ty])
            | Op.Max ty => (ty, [ty, ty])
            | Op.Min ty => (ty, [ty, ty])
            | Op.EigenVals2x2 => (Ty.SeqTy(Ty.realTy, SOME 2), [Ty.TensorTy[2,2]])
            | Op.EigenVals3x3 => (Ty.SeqTy(Ty.realTy, SOME 3), [Ty.TensorTy[3,3]])
            | Op.Zero ty => (ty, [])
            | Op.Select(ty as Ty.TupleTy tys, i) => (List.nth(tys, i-1), [ty])
            | Op.Subscript(ty as Ty.SeqTy(elemTy, _)) => (elemTy, [ty, Ty.intTy])
            | Op.MkDynamic(ty, n) => (Ty.SeqTy(ty, NONE), [Ty.SeqTy(ty, SOME n)])
            | Op.Append ty => (Ty.SeqTy(ty, NONE), [Ty.SeqTy(ty, NONE), ty])
            | Op.Prepend ty => (Ty.SeqTy(ty, NONE), [ty, Ty.SeqTy(ty, NONE)])
            | Op.Concat ty => (Ty.SeqTy(ty, NONE), [Ty.SeqTy(ty, NONE), Ty.SeqTy(ty, NONE)])
            | Op.Range => (Ty.SeqTy(Ty.intTy, NONE), [Ty.IntTy, Ty.IntTy])
            | Op.Length ty => (Ty.intTy, [Ty.SeqTy(ty, NONE)])
            | Op.SphereQuery(1, strandTy) =>
                (Ty.SeqTy(strandTy, NONE), [Ty.realTy, Ty.realTy])
            | Op.SphereQuery(dim, strandTy) =>
                (Ty.SeqTy(strandTy, NONE), [Ty.TensorTy[dim], Ty.realTy])
            | Op.Sqrt => (Ty.realTy, [Ty.realTy])
            | Op.Cos => (Ty.realTy, [Ty.realTy])
            | Op.ArcCos => (Ty.realTy, [Ty.realTy])
            | Op.Sin => (Ty.realTy, [Ty.realTy])
            | Op.ArcSin => (Ty.realTy, [Ty.realTy])
            | Op.Tan => (Ty.realTy, [Ty.realTy])
            | Op.ArcTan => (Ty.realTy, [Ty.realTy])
            | Op.Exp => (Ty.realTy, [Ty.realTy])
            | Op.Ceiling d => (Ty.vecTy d, [Ty.vecTy d])
            | Op.Floor d => (Ty.vecTy d, [Ty.vecTy d])
            | Op.Round d => (Ty.vecTy d, [Ty.vecTy d])
            | Op.Trunc d => (Ty.vecTy d, [Ty.vecTy d])
            | Op.IntToReal => (Ty.realTy, [Ty.intTy])
            | Op.RealToInt 1 => (Ty.IntTy, [Ty.realTy])
            | Op.RealToInt d => (Ty.SeqTy(Ty.IntTy, SOME d), [Ty.TensorTy[d]])
            | Op.NumStrands _ => (Ty.IntTy, [])
            | Op.Strands(strandTy, _) => (Ty.SeqTy(strandTy, NONE), [])
            | Op.Transform info => let
                val dim = ImageInfo.dim info
                in
                  if (dim = 1)
                    then (Ty.TensorTy[], [Ty.ImageTy info])
                    else (Ty.TensorTy[dim, dim], [Ty.ImageTy info])
                end
            | Op.Translate info => let
                val dim = ImageInfo.dim info
                in
                  if (dim = 1)
                    then (Ty.TensorTy[], [Ty.ImageTy info])
                    else (Ty.TensorTy[dim], [Ty.ImageTy info])
                end
            | Op.ControlIndex(info, _, _) => (Ty.intTy, [Ty.ImageTy info, Ty.IntTy])
            | Op.LoadVoxel info => (Ty.realTy, [Ty.ImageTy info, Ty.IntTy])
            | Op.Inside(info, _) =>
                (Ty.BoolTy, [Ty.vecTy(ImageInfo.dim info), Ty.ImageTy info])
            | Op.IndexInside(info, _) => let
                val idxTy = (case ImageInfo.dim info
                       of 1 => Ty.IntTy
                        | d => Ty.SeqTy(Ty.IntTy, SOME d)
                      (* end case *))
                in
                  (Ty.BoolTy, [idxTy, Ty.ImageTy info])
                end
            | Op.ImageDim(info, _) => (Ty.IntTy, [Ty.ImageTy info])
            | Op.LoadSeq(ty, _) => (ty, [])
            | Op.LoadImage(ty, _) => (ty, [])
            | Op.MathFn f => MathFns.sigOf (Ty.realTy, f)
            | _ => raise Fail("sigOf: invalid operator " ^ Op.toString rator)
          (* end case *))

  (* utility function for synthesizing eigenvector/eigenvalue signature *)
    fun eigenSig dim = let
          val resTy = [
                  Ty.SeqTy(Ty.realTy, SOME dim),
                  Ty.SeqTy(Ty.vecTy dim, SOME dim)
                ]
          in
            (resTy, [Ty.TensorTy[dim, dim]])
          end

    fun msigOf rator = (case rator
           of Op.EigenVecs2x2 => eigenSig 2
            | Op.EigenVecs3x3 => eigenSig 3
            | Op.StabilizeAll => ([], [])
            | Op.Print tys => ([], tys)
            | _ => raise Fail("msigOf: invalid operator " ^ Op.toString rator)
          (* end case *))

    fun typeOfCons (Ty.TensorTy dd', (ty1 as Ty.TensorTy dd)::r) =
          if List.all (fn ty => Ty.same(ty1, ty)) r
            then (dd' = (List.length r + 1)::dd)
            else false
      | typeOfCons _ = false

    fun typeOfSeq (Ty.SeqTy(ty, NONE), tys) = List.all (fn ty' => Ty.same(ty, ty')) tys
      | typeOfSeq (Ty.SeqTy(ty, SOME n), tys) =
          List.all (fn ty' => Ty.same(ty, ty')) tys andalso (List.length tys = n)
      | typeOfSeq _ = false

    fun isStrandTy (Ty.StrandTy _) = true
      | isStrandTy _ = false

    fun isBoolTy Ty.BoolTy = true
      | isBoolTy _ = false

  end

structure CheckLow = CheckIRFn (
    structure IR = LowIR
    structure OpTy = CheckOps)

structure LowPP = SSAPPFn (LowIR)

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