Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

[diderot] View of /branches/vis12/src/compiler/mid-il/check-mid-il.sml
 [diderot] / branches / vis12 / src / compiler / mid-il / check-mid-il.sml

# View of /branches/vis12/src/compiler/mid-il/check-mid-il.sml

Sun Jul 8 12:17:56 2012 UTC (9 years, 4 months ago) by jhr
File size: 5763 byte(s)
```  Added checking code for ColonMul operator.
```
```(* check-mid-il.sml
*
* COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
*)

structure CheckOps : OPERATOR_TY = struct

structure Op = MidOps
structure Ty = MidILTypes

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, dim),
Ty.SeqTy(Ty.vecTy dim, dim)
]
in
(tplTy, [Ty.TensorTy[dim, dim]])
end

(* Return the signature of a MidIL operator. *)
fun sigOf rator = (case rator
of Op.Add ty => (ty, [ty, ty])
| Op.Sub ty => (ty, [ty, ty])
| Op.Mul ty => (ty, [ty, ty])
| Op.Div ty => (ty, [ty, ty])
| Op.Neg ty => (ty, [ty])
| Op.Abs ty => (ty, [ty])
| 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.Max => (Ty.realTy, [Ty.realTy, Ty.realTy])
| Op.Min => (Ty.realTy, [Ty.realTy, Ty.realTy])
| Op.Clamp ty => (ty, [ty, ty, ty])
| Op.Lerp ty => (ty, [ty, ty, Ty.realTy])
| Op.Dot d => (Ty.realTy, [Ty.vecTy d, Ty.vecTy d])
| Op.MulVecMat(d1, d2) => (Ty.vecTy d2, [Ty.vecTy d1, Ty.TensorTy[d1, d2]])
| Op.MulMatVec(d1, d2) => (Ty.vecTy d1, [Ty.TensorTy[d1, d2], Ty.vecTy d2])
| Op.MulMatMat(d1, d2, d3) => (Ty.TensorTy[d1, d3], [Ty.TensorTy[d1, d2], Ty.TensorTy[d2, d3]])
| Op.MulVecTen3(d1, d2, d3) => (Ty.TensorTy[d2, d3], [Ty.vecTy d1, Ty.TensorTy[d1, d2, d3]])
| Op.MulTen3Vec(d1, d2, d3) => (Ty.TensorTy[d1, d2], [Ty.TensorTy[d1, d2, d3], Ty.vecTy d3])
| Op.ColonMul(ty1 as Ty.TensorTy dd1, ty2 as Ty.TensorTy(d21::d22::dd2)) => let
fun last2 ([d1, d2], prefix) = (prefix, d1, d2)
| last2 (d::dd, prefix) = last2(dd, d::prefix)
| last2 _ = raise Fail("sigOf: invalid operator " ^ Op.toString rator)
val (prefix, d11, d12) = last2 (dd1, [])
in
if (d11 <> d21) orelse (d12 <> d22)
then raise Fail("sigOf: invalid operator " ^ Op.toString rator)
else (Ty.TensorTy(List.revAppend(prefix, dd2)), [ty1, ty2])
end
| Op.Cross => (vec3Ty, [vec3Ty, vec3Ty])
| Op.Norm(ty as Ty.TensorTy _) => (Ty.realTy, [ty])
| Op.Normalize d => (Ty.vecTy d, [Ty.vecTy d])
| Op.Scale(ty as Ty.TensorTy(_::_)) => (ty, [Ty.realTy, ty])
| Op.PrincipleEvec _ => raise Fail "Op.PrincipleEvec unimplemented"
| Op.EigenVecs2x2 => eigenSig 2
| Op.EigenVecs3x3 => eigenSig 3
| Op.EigenVals2x2 => (Ty.SeqTy(Ty.realTy, 2), [Ty.TensorTy[2,2]])
| Op.EigenVals3x3 => (Ty.SeqTy(Ty.realTy, 3), [Ty.TensorTy[3,3]])
| Op.Identity d => (Ty.TensorTy[d,d], [])
| Op.Zero ty => (ty, [])
| Op.Trace d => (Ty.realTy, [Ty.TensorTy[d, d]])
| 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.Index(ty as Ty.SeqTy(elemTy, _), _) => (elemTy, [ty])
| Op.Subscript(ty as Ty.TensorTy dd) => (Ty.realTy, ty :: List.map (fn _ => Ty.intTy) dd)
| Op.Subscript(ty as Ty.SeqTy(elemTy, d)) => (elemTy, [ty, Ty.intTy])
| Op.MkDynamic(ty, n) => (Ty.DynSeqTy ty, [Ty.SeqTy(ty, n)])
| Op.Prepend ty => (Ty.DynSeqTy ty, [ty, Ty.DynSeqTy ty])
| Op.Append ty => (Ty.DynSeqTy ty, [Ty.DynSeqTy ty, ty])
| Op.Concat ty => (Ty.DynSeqTy ty, [Ty.DynSeqTy ty, Ty.DynSeqTy ty])
| Op.Length ty => (Ty.intTy, [Ty.DynSeqTy ty])
| 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, d), [Ty.TensorTy[d]])
(* FIXME: the type of RealToInt should be
| Op.RealToInt d => (Ty.SeqTy(Ty.IntTy, d), [Ty.SeqTy(Ty.realTy, d)])
*)
val dim = ImageInfo.dim info
in
(Ty.AddrTy info, Ty.ImageTy info :: List.tabulate(dim, fn _ => Ty.intTy))
end
| Op.PosToImgSpace info => let
val dim = ImageInfo.dim info
in
(Ty.vecTy dim, [Ty.ImageTy info, Ty.vecTy dim])
end
| Op.TensorToWorldSpace(info, ty) => (ty, [Ty.ImageTy info, ty])
| Op.EvalKernel(d, _, _) => (Ty.vecTy d, [Ty.vecTy d])
| Op.LoadImage info => (Ty.ImageTy info, [Ty.StringTy])
| Op.Inside(info, _) => (Ty.BoolTy, [Ty.vecTy(ImageInfo.dim info), Ty.ImageTy info])
| Op.Input(ty, _, _) => (ty, [])
| Op.InputWithDefault(ty, _, _) => (ty, [ty])
| Op.Print tys => (Ty.TupleTy[], tys)
| _ => raise Fail("sigOf: invalid operator " ^ Op.toString rator)
(* end case *))

fun typeOfCons (_, []) = false
| typeOfCons (Ty.SeqTy(ty, n), tys) =
List.all (fn ty' => Ty.same(ty, ty')) tys andalso (List.length tys = n)
| typeOfCons (expectedTy, tys as ty1::_) =
if List.all (fn ty => Ty.same(ty1, ty)) tys
then (case (expectedTy, ty1)
of (Ty.SeqTy(_, n), Ty.IntTy) => (n = List.length tys)
| (Ty.TensorTy dd, Ty.TensorTy dd') => (dd = List.length tys :: dd')
| _ => false
(* end case *))
else false

end

structure CheckMidIL = CheckILFn (
structure IL = MidIL
structure OpTy = CheckOps)

structure MidPP = SSAPPFn (MidIL)

```