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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3757 - (download) (annotate)
Fri Apr 15 14:30:25 2016 UTC (3 years, 6 months ago) by jhr
File size: 6029 byte(s)
working on merge
(* check-mid.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 = MidOps
    structure Ty = MidTypes

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

    val vec3Ty = Ty.vecTy 3

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

  (* Return the signature of a MidIR 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.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.Not => (Ty.BoolTy, [Ty.BoolTy])
            | Op.Abs ty => (ty, [ty])
            | Op.Max ty => (ty, [ty, ty])
            | Op.Min ty => (ty, [ty, ty])
            | Op.Clamp ty => (ty, [ty, ty, ty])
            | Op.Lerp ty => (ty, [ty, ty, Ty.realTy])
            | Op.EigenVecs2x2 => eigenSig 2
            | Op.EigenVecs3x3 => eigenSig 3
            | 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.Index(ty as Ty.TensorTy[d], _) => (Ty.realTy, [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.Prepend ty => (Ty.SeqTy(ty, NONE), [ty, Ty.SeqTy(ty, NONE)])
            | Op.Append ty => (Ty.SeqTy(ty, NONE), [Ty.SeqTy(ty, NONE), ty])
            | 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(ptTy, strandTy) => (Ty.SeqTy(strandTy, NONE), [ptTy, 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]])
(* FIXME: the type of RealToInt should be
            | Op.RealToInt d => (Ty.SeqTy(Ty.IntTy, d), [Ty.SeqTy(Ty.realTy, d)])
*)
(* not sure if we will need these
      | R_All of ty
      | R_Exists of ty
      | R_Max of ty
      | R_Min of ty
      | R_Sum of ty
      | R_Product of ty
      | R_Mean of ty
      | R_Variance of ty
*)
	    | Op.BuildPos s => (Ty.TensorTy[2*s], [Ty.realTy])
	    | Op.EvalKernel(d, _, _) => (Ty.TensorTy[d], [Ty.TensorTy[d]])
	    | Op.Kernel _ => (Ty.KernelTy, [])
	    | 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.VoxelAddress(info, offset) => let
                val dim = ImageInfo.dim info
                in
                  (Ty.AddrTy info, Ty.ImageTy info :: List.tabulate(dim, fn _ => Ty.intTy))
                end
	    | Op.VoxelAddressWithCtl(info, offset, ctl) => let
                val dim = ImageInfo.dim info
                in
                  (Ty.AddrTy info, Ty.ImageTy info :: List.tabulate(dim, fn _ => Ty.intTy))
                end
            | Op.LoadVoxels(info, n) => (Ty.vecTy n, [Ty.AddrTy info])
            | Op.Inside(info, _) => (Ty.BoolTy, [Ty.vecTy(ImageInfo.dim info), Ty.ImageTy info])
            | Op.ImageDim(info, _) => (Ty.IntTy, [Ty.ImageTy info])
	    | Op.BorderCtlDefault info =>
		(Ty.ImageTy info, [Ty.ImageTy info, Ty.TensorTy(ImageInfo.voxelShape info)])
	    | Op.BorderCtlClamp info => (Ty.ImageTy info, [Ty.ImageTy info])
	    | Op.BorderCtlMirror info => (Ty.ImageTy info, [Ty.ImageTy info])
	    | Op.BorderCtlWrap info => (Ty.ImageTy info, [Ty.ImageTy info])
            | Op.LoadSeq(ty, _) => (ty, [])
            | Op.LoadImage(ty, _) => (ty, [])
            | Op.Print tys => (Ty.TupleTy[], tys)
	    | Op.MathFn f => MathFns.sigOf (Ty.realTy, f)
            | _ => raise Fail("sigOf: 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

  end

structure CheckMid = CheckIRFn (
    structure IR = MidIR
    structure OpTy = CheckOps)

structure MidPP = SSAPPFn (MidIR)

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