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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3833 - (download) (annotate)
Fri May 6 15:35:03 2016 UTC (3 years, 5 months ago) by jhr
File size: 15188 byte(s)
working on merge
(* check-tree.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.
 *
 * TODO: check global and state variable consistency
 *)

(* FIXME: this module needs to be parameterized over the vector layout of the target *)

structure CheckTree : sig

    val check : string * TreeIR.program -> bool

  end = struct

    structure IR = TreeIR
    structure Op = TreeOps
    structure GVar = TreeGlobalVar
    structure SVar = TreeStateVar
    structure Var = TreeVar
    structure VSet = Var.Set
    structure Ty = IR.Ty

    datatype token
      = NL | S of string | A of Atom.atom | V of IR.var
      | TY of Ty.t | TYS of Ty.t list

    fun error errBuf toks = let
          fun tok2str NL = "\n  ** "
            | tok2str (S s) = s
            | tok2str (A s) = Atom.toString s
            | tok2str (V x) = Var.toString x
            | tok2str (TY ty) = Ty.toString ty
            | tok2str (TYS []) = "()"
            | tok2str (TYS[ty]) = Ty.toString ty
            | tok2str (TYS tys) = String.concat[
                  "(", String.concatWith " * " (List.map Ty.toString tys), ")"
                ]
          in
            errBuf := concat ("**** Error: " :: List.map tok2str toks)
              :: !errBuf
          end

  (* 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
(* FIXME: what about pieces? *)
            (tplTy, [Ty.TensorTy[dim, dim]])
          end

  (* Return the signature of a TreeIR 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.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.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.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])
(*
TensorIndex
*)
            | Op.EigenVecs2x2 => eigenSig 2
            | Op.EigenVecs3x3 => eigenSig 3
(* FIXME: what about pieces? *)
            | 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.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.Sqrt => (Ty.realTy, [Ty.realTy])
	    | Op.Cos => (Ty.realTy, [Ty.realTy])
	    | Op.ArcCos => (Ty.realTy, [Ty.realTy])
	    | Op.Sine => (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.vecTy 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
*)
(* FIXME: these should probably be compiled down to lower-level operartions at this point!
	    | Op.Transform info => let
                val dim = ImageInfo.dim info
                in
		  if (dim = 1)
		    then (Ty.realTy, [Ty.ImageTy info])
		    else (Ty.matrixTy(dim, dim), [Ty.ImageTy info])
                end
	    | Op.Translate info => let
                val dim = ImageInfo.dim info
                in
		  if (dim = 1)
		    then (Ty.realTy, [Ty.ImageTy info])
		    else (Ty.matrixTy(dim, dim), [Ty.ImageTy info])
                end
*)
	    | Op.ControlIndex(info, _, _) => (Ty.IntTy, [Ty.ImageTy info, Ty.IntTy])
            | Op.Inside(info, _) => (Ty.BoolTy, [Ty.vecTy(ImageInfo.dim info), Ty.ImageTy info])
            | 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 *))

    fun check (phase, prog) = let
	  val IR.Program{
		  props, consts, inputs, constInit, globals, globalInit,
		  strand, create, update
		} = prog
          val errBuf = ref []
          val errFn = error errBuf
          fun final () = (case !errBuf
                 of [] => false
                  | errs => (
                      Log.msg ["********** IR Errors detected after ", phase, " **********\n"];
                      List.app (fn msg => Log.msg [msg, "\n"]) (List.rev errs);
                      true)
                (* end case *))
        (* check a variable use *)
          fun checkVar (bvs, x) = if VSet.member(bvs, x)
                then ()
                else errFn [S "variable ", V x, S " is not bound"]
	  fun chkBlock (bvs, IR.Block{locals, body}) = let
		fun chkExp (bvs, e) = let
		      fun chk e = (case e
			     of IR.E_Global gv => GVar.ty gv
			      | IR.E_State sv => SVar.ty sv
			      | IR.E_Var x => Var.ty x
			      | IR.E_Lit(Literal.Int _) => Ty.IntTy
			      | IR.E_Lit(Literal.Real _) => Ty.realTy
			      | IR.E_Lit(Literal.String _) => Ty.StringTy
			      | IR.E_Lit(Literal.Bool _) => Ty.BoolTy
			      | IR.E_Op(rator, args) => let
				  val (resTy, paramTys) = sigOf rator
				  val argTys = List.map chk args
				  in
				    if ListPair.allEq Ty.same (paramTys, argTys)
				      then ()
				      else errFn [
					  S "argument type mismatch in application of ",
					  S(Op.toString rator),
					  NL, S "expected: ", TYS paramTys,
					  NL, S "found:    ", TYS argTys
					];
				    resTy
				  end
			      | IR.E_Cons([], ty) => (
				  errFn [S "empty cons"];
				  ty)
			      | IR.E_Cons(es, consTy as Ty.TensorTy dd) => let
				  val nelems = List.foldl Int.* 1 dd
				  in
				    if (length es <> nelems)
				      then errFn [
					  S "cons has incorrect number of elements",
					  NL, S "  expected: ", S(Int.toString nelems),
					  NL, S "  found:    ", S(Int.toString(length es))
					]
				      else ();
				    chkElems ("cons", Ty.RealTy, es);
				    consTy
				  end
			      | IR.E_Cons(es, ty) => (
				  errFn [S "unexpected type for cons: ", TY ty];
				  ty)
			      | IR.E_Seq([], ty as Ty.SeqTy(_, SOME 0)) => ty
			      | IR.E_Seq([], ty as Ty.SeqTy(_, SOME n)) => (
				  errFn [S "empty sequence, but expected ", TY ty];
				  ty)
			      | IR.E_Seq(es, seqTy as Ty.SeqTy(ty, NONE)) => (
				  chkElems ("sequence", ty, es);
				  seqTy)
			      | IR.E_Seq(es, seqTy as Ty.SeqTy(ty, SOME n)) => (
				  if (length es <> n)
				    then errFn [
				        S "sequence has incorrect number of elements",
					NL, S "  expected: ", S(Int.toString n),
					NL, S "  found:    ", S(Int.toString(length es))
				      ]
				    else ();
				  chkElems ("sequence", ty, es);
				  seqTy)
			      | IR.E_Seq(es, ty) => (
				  errFn [S "unexpected type for sequence: ", TY ty];
				  ty)
			      | IR.E_Pack es => raise Fail "FIXME"
			      | IR.E_Select(e, i) => raise Fail "FIXME"
			    (* end case *))
		      and chkElems (cxt, ty, []) = ()
			| chkElems (cxt, ty, e::es) = let
			    val ty' = chk e
			    in
			      if Ty.same(ty, ty')
				then ()
				else errFn [
				    S "element of ", S cxt, S " has incorrect type",
				    NL, S "expected: ", TY ty,
				    NL, S "found:    ", TY ty'
				  ];
			      chkElems (cxt, ty, es)
			    end
		      in
			chk e
		      end
		fun chkStm (stm, bvs) = (case stm
		       of IR.S_Comment _ => bvs
			| IR.S_Unpack(xs, e) => let
			    fun chkVar (x, ty) = if Ty.same(Var.ty x, ty)
				  then ()
				  else errFn[
				      S "type mismatch in assignment to ", S(Var.name x),
				      NL, S "lhs: ", TY(Var.ty x),
				      NL, S "rhs: ", TY ty
				    ]
			    in
			      case (xs, chkExp (bvs, e))
			       of (_, Ty.VecTy{pieces, ...}) => (
				    if (List.length xs <> List.length pieces)
				      then errFn [
					  S "arity mismatch in assigning composite vector",
					  NL, S"  lhs arity: ", S(Int.toString(List.length xs)),
					  NL, S"  rhs arity: ", S(Int.toString(List.length pieces))
					]
				      else ();
				    ListPair.app (fn (x, d) => chkVar(x, Ty.vecTy d)) (xs, pieces))
				| ([x], ty) => chkVar (x, ty)
				| (_::_, ty) => errFn [
				      S "assignment of non-composite value to (",
				      S(String.concatWithMap "," Var.name xs), S ")"
				    ]
				| _ => errFn [S "empty lhs for Unpack"]
			      (* end case *);
			      bvs
			    end
			| IR.S_Assign(x, e) => let
			    val ty = chkExp (bvs, e)
			    in
			      if Ty.same(Var.ty x, ty)
				then ()
				else errFn[
				    S "type mismatch in assignment to local ", S(Var.name x),
				    NL, S "lhs: ", TY(Var.ty x),
				    NL, S "rhs: ", TY ty
				  ];
			      bvs
			    end
			| IR.S_GAssign(gv, e) => let
			    val ty = chkExp (bvs, e)
			    in
			      if Ty.same(GVar.ty gv, ty)
				then ()
				else errFn[
				    S "type mismatch in assignment to global ", S(GVar.name gv),
				    NL, S "lhs: ", TY(GVar.ty gv),
				    NL, S "rhs: ", TY ty
				  ];
			      bvs
			    end
			| IR.S_IfThen(e, b) => let
			    val ty = chkExp (bvs, e)
			    in
			      if Ty.same(ty, Ty.BoolTy)
				then ()
				else errFn[
				    S "expected bool for if-then, but found ", TY ty
				  ];
			      chkBlock (bvs, b);
			      bvs
			    end
			| IR.S_IfThenElse(e, b1, b2) => let
			    val ty = chkExp (bvs, e)
			    in
			      if Ty.same(ty, Ty.BoolTy)
				then ()
				else errFn[
				    S "expected bool for if-then-else, but found ", TY ty
				  ];
			      chkBlock (bvs, b1);
			      chkBlock (bvs, b2);
			      bvs
			    end
			| IR.S_Foreach(x, e, b) => (
			    case chkExp (bvs, e)
			     of Ty.SeqTy(ty, _) =>
				  if Ty.same(ty, Var.ty x)
				    then ()
				    else errFn [
					S "type mismatch in foreach ", V x,
					NL, S "variable type: ", TY(Var.ty x),
				        NL, S "domain type:   ", TY ty
				      ]
			      | ty => errFn [
				    S "domain of foreach is not sequence type; found ", TY ty
				  ]
			    (* end case *);
			    ignore (chkBlock (VSet.add(bvs, x), b));
			    bvs)
			| IR.S_LoadNrrd(x, name) => bvs (* FIXME: check type if x *)
			| IR.S_Input(gv, _, _, NONE) => bvs
			| IR.S_Input(gv, _, _, SOME e) => let
			    val ty = chkExp (bvs, e)
			    in
			      if Ty.same(GVar.ty gv, ty)
				then ()
				else errFn[
				    S "type mismatch in default for input ", S(GVar.name gv),
				    NL, S "expected: ", TY(GVar.ty gv),
				    NL, S "found:    ", TY ty
				  ];
			      bvs
			    end
			| IR.S_InputNrrd(gv, _, _, _) => (
			    case GVar.ty gv
			     of Ty.SeqTy(_, NONE) => ()
			      | Ty.ImageTy _ => ()
			      | ty => errFn [
				    S "input variable ", S(GVar.name gv), S " has bogus type ",
				    TY ty, S " for lhs for InputNrrd"
				  ]
			    (* end case *);
			    bvs)
			| IR.S_New(_, es) => (
			    List.app (fn e => ignore (chkExp(bvs, e))) es;
			    bvs)
			| IR.S_Save(sv, e) => let
			    val ty = chkExp (bvs, e)
			    in
			      if Ty.same(SVar.ty sv, ty)
				then ()
				else errFn[
				    S "type mismatch in assignment to state variable ",
				    S(SVar.name sv),
				    NL, S "lhs: ", TY(SVar.ty sv),
				    NL, S "rhs: ", TY ty
				  ];
			      bvs
			    end
			| IR.S_Exit es => (
			    List.app (fn e => ignore (chkExp(bvs, e))) es;
			    bvs)
			| IR.S_Print(tys, es) => (
			    if (length tys <> length es)
			      then errFn [
				]
			      else ();
			    ListPair.appi
			      (fn (i, ty, e) => let val ty' = chkExp(bvs, e)
				in
				  if Ty.same(ty, ty')
				    then ()
				    else errFn[
					S "type mismatch in argument ", S(Int.toString i),
					S " of print",
					NL, S "expected:  ", TY ty,
					NL, S "but found: ", TY ty'
				      ]
				end)
				(tys, es);
			    bvs)
			| IR.S_Active => bvs
			| IR.S_Stabilize => bvs
			| IR.S_Die => bvs
		      (* end case *))
		val bvs = List.foldl VSet.add' bvs locals
		in
		  ignore (List.foldl chkStm bvs body)
		end
	  fun chkOptBlock (_, NONE) = ()
	    | chkOptBlock (bvs, SOME blk) = ignore (chkBlock (bvs, blk))
	  fun chkStrand (IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM}) = (
		ignore (chkBlock (VSet.fromList params, stateInit));
		chkOptBlock (VSet.empty, initM);
		ignore (chkBlock (VSet.empty, updateM));
		chkOptBlock (VSet.empty, stabilizeM))
	  in
	    ignore (chkBlock (VSet.empty, constInit));
	    ignore (chkBlock (VSet.empty, globalInit));
	    chkStrand strand;
	    case create of IR.Create{code, ...} => ignore (chkBlock (VSet.empty, code));
	    chkOptBlock (VSet.empty, update);
	    final ()
	  end

  end

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