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

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/low-il/check-low-il.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/low-il/check-low-il.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3291 - (view) (download)

1 : jhr 458 (* check-low-il.sml
2 :     *
3 : jhr 3291 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 458 * All rights reserved.
7 :     *)
8 :    
9 :     structure CheckOps : OPERATOR_TY = struct
10 :    
11 :     structure Op = LowOps
12 :     structure Ty = LowILTypes
13 :    
14 :     type rator = Op.rator
15 :     type ty = Ty.ty
16 :    
17 : jhr 1116 val vec3Ty = Ty.vecTy 3
18 : jhr 458
19 : jhr 1640 (* utility function for synthesizing eigenvector/eigenvalue signature *)
20 :     fun eigenSig dim = let
21 :     val tplTy = Ty.TupleTy[
22 :     Ty.SeqTy(Ty.realTy, dim),
23 :     Ty.SeqTy(Ty.vecTy dim, dim)
24 :     ]
25 :     in
26 :     (tplTy, [Ty.TensorTy[dim, dim]])
27 :     end
28 :    
29 : jhr 458 (* Return the signature of a LowIL operator. *)
30 :     fun sigOf rator = (case rator
31 : jhr 2345 of Op.Add(Ty.AddrTy rTy) => (Ty.AddrTy rTy, [Ty.AddrTy rTy, Ty.intTy])
32 :     | Op.Add ty => (ty, [ty, ty])
33 :     | Op.Sub(Ty.AddrTy rTy) => (Ty.AddrTy rTy, [Ty.AddrTy rTy, Ty.intTy])
34 :     | Op.Sub ty => (ty, [ty, ty])
35 :     | Op.Mul ty => (ty, [ty, ty])
36 :     | Op.Div ty => (ty, [ty, ty])
37 :     | Op.Neg ty => (ty, [ty])
38 :     | Op.Abs ty => (ty, [ty])
39 :     | Op.LT ty => (Ty.BoolTy, [ty, ty])
40 :     | Op.LTE ty => (Ty.BoolTy, [ty, ty])
41 :     | Op.EQ ty => (Ty.BoolTy, [ty, ty])
42 :     | Op.NEQ ty => (Ty.BoolTy, [ty, ty])
43 :     | Op.GT ty => (Ty.BoolTy, [ty, ty])
44 :     | Op.GTE ty => (Ty.BoolTy, [ty, ty])
45 :     | Op.Not => (Ty.BoolTy, [Ty.BoolTy])
46 :     | Op.Max => (Ty.realTy, [Ty.realTy, Ty.realTy])
47 :     | Op.Min => (Ty.realTy, [Ty.realTy, Ty.realTy])
48 :     | Op.Clamp ty => (ty, [ty, ty, ty])
49 :     | Op.Lerp ty => (ty, [ty, ty, Ty.realTy])
50 :     | Op.Dot d => (Ty.realTy, [Ty.vecTy d, Ty.vecTy d])
51 :     | Op.MulVecMat(d1, d2) => (Ty.vecTy d2, [Ty.vecTy d1, Ty.TensorTy[d1, d2]])
52 :     | Op.MulMatVec(d1, d2) => (Ty.vecTy d1, [Ty.TensorTy[d1, d2], Ty.vecTy d2])
53 :     | Op.MulMatMat(d1, d2, d3) => (Ty.TensorTy[d1, d3], [Ty.TensorTy[d1, d2], Ty.TensorTy[d2, d3]])
54 :     | Op.MulVecTen3(d1, d2, d3) => (Ty.TensorTy[d2, d3], [Ty.vecTy d1, Ty.TensorTy[d1, d2, d3]])
55 :     | Op.MulTen3Vec(d1, d2, d3) => (Ty.TensorTy[d1, d2], [Ty.TensorTy[d1, d2, d3], Ty.vecTy d3])
56 :     | Op.ColonMul(ty1 as Ty.TensorTy dd1, ty2 as Ty.TensorTy(d21::d22::dd2)) => let
57 :     fun last2 ([d1, d2], prefix) = (prefix, d1, d2)
58 :     | last2 (d::dd, prefix) = last2(dd, d::prefix)
59 :     | last2 _ = raise Fail("sigOf: invalid operator " ^ Op.toString rator)
60 :     val (prefix, d11, d12) = last2 (dd1, [])
61 :     in
62 :     if (d11 <> d21) orelse (d12 <> d22)
63 :     then raise Fail("sigOf: invalid operator " ^ Op.toString rator)
64 :     else (Ty.TensorTy(List.revAppend(prefix, dd2)), [ty1, ty2])
65 :     end
66 :     | Op.Cross => (vec3Ty, [vec3Ty, vec3Ty])
67 :     | Op.Norm(ty as Ty.TensorTy _) => (Ty.realTy, [ty])
68 :     | Op.Normalize d => (Ty.vecTy d, [Ty.vecTy d])
69 :     | Op.Scale(ty as Ty.TensorTy(_::_)) => (ty, [Ty.realTy, ty])
70 :     | Op.PrincipleEvec _ => raise Fail "Op.PrincipleEvec unimplemented"
71 : jhr 1640 | Op.EigenVecs2x2 => eigenSig 2
72 :     | Op.EigenVecs3x3 => eigenSig 3
73 :     | Op.EigenVals2x2 => (Ty.SeqTy(Ty.realTy, 2), [Ty.TensorTy[2,2]])
74 :     | Op.EigenVals3x3 => (Ty.SeqTy(Ty.realTy, 3), [Ty.TensorTy[3,3]])
75 : jhr 2345 | Op.Identity d => (Ty.TensorTy[d,d], [])
76 :     | Op.Zero ty => (ty, [])
77 :     | Op.Transpose(d1, d2) => (Ty.TensorTy[d2, d1], [Ty.TensorTy[d1, d2]])
78 : jhr 2822 | Op.Slice(ty as Ty.TensorTy dd, m) => let
79 :     val (resDims, idxDims) = let
80 :     fun f (d, false, (rds, ids)) = (d::rds, ids)
81 :     | f (_, true, (rds, ids)) = (rds, Ty.IntTy::ids)
82 :     in
83 :     ListPair.foldr f ([], []) (dd, m)
84 :     end
85 :     in
86 :     (Ty.TensorTy resDims, ty::idxDims)
87 :     end
88 : jhr 1640 | Op.Select(ty as Ty.TupleTy tys, i) => (List.nth(tys, i-1), [ty])
89 : jhr 2345 | Op.Index(ty as Ty.TensorTy[d], _) => (Ty.realTy, [ty])
90 :     | Op.Index(ty as Ty.SeqTy(elemTy, _), _) => (elemTy, [ty])
91 :     | Op.Subscript(ty as Ty.TensorTy dd) => (Ty.realTy, ty :: List.map (fn _ => Ty.intTy) dd)
92 :     | Op.Subscript(ty as Ty.SeqTy(elemTy, d)) => (elemTy, [ty, Ty.intTy])
93 :     | Op.Subscript(ty as Ty.DynSeqTy(elemTy)) => (elemTy, [ty, Ty.intTy])
94 : jhr 1689 | Op.MkDynamic(ty, n) => (Ty.DynSeqTy ty, [Ty.SeqTy(ty, n)])
95 :     | Op.Prepend ty => (Ty.DynSeqTy ty, [ty, Ty.DynSeqTy ty])
96 :     | Op.Append ty => (Ty.DynSeqTy ty, [Ty.DynSeqTy ty, ty])
97 :     | Op.Concat ty => (Ty.DynSeqTy ty, [Ty.DynSeqTy ty, Ty.DynSeqTy ty])
98 : jhr 1925 | Op.Length ty => (Ty.intTy, [Ty.DynSeqTy ty])
99 : jhr 2345 | Op.Ceiling d => (Ty.vecTy d, [Ty.vecTy d])
100 :     | Op.Floor d => (Ty.vecTy d, [Ty.vecTy d])
101 :     | Op.Round d => (Ty.vecTy d, [Ty.vecTy d])
102 :     | Op.Trunc d => (Ty.vecTy d, [Ty.vecTy d])
103 :     | Op.IntToReal => (Ty.realTy, [Ty.intTy])
104 :     | Op.RealToInt 1 => (Ty.IntTy, [Ty.realTy])
105 :     | Op.RealToInt d => (Ty.SeqTy(Ty.IntTy, d), [Ty.TensorTy[d]])
106 : jhr 1640 (* FIXME: the type of RealToInt should be
107 : jhr 2345 | Op.RealToInt d => (Ty.SeqTy(Ty.IntTy, d), [Ty.SeqTy(Ty.realTy, d)])
108 : jhr 1640 *)
109 : jhr 2345 | Op.ImageAddress info => (Ty.AddrTy info, [Ty.ImageTy info])
110 : jhr 3073 | Op.ControlIndex(info, ictl, d) => (Ty.IntTy, [Ty.ImageTy info, Ty.IntTy])
111 : jhr 2345 | Op.LoadVoxels(info, n) => (Ty.vecTy n, [Ty.AddrTy info])
112 :     | Op.PosToImgSpace info => let
113 :     val dim = ImageInfo.dim info
114 :     in
115 :     (Ty.vecTy dim, [Ty.ImageTy info, Ty.vecTy dim])
116 :     end
117 :     | Op.TensorToWorldSpace(info, ty) => (ty, [Ty.ImageTy info, ty])
118 :     | Op.Inside(info, _) => (Ty.BoolTy, [Ty.vecTy(ImageInfo.dim info), Ty.ImageTy info])
119 : jhr 2998 | Op.ImageDim(info, _) => (Ty.IntTy, [Ty.ImageTy info])
120 : jhr 2345 | Op.Input(Inputs.INP{ty, ...}) => (ty, [])
121 : jhr 2813 | Op.InputWithDefault(Inputs.INP{ty, ...}) => (ty, [ty])
122 : jhr 2345 | Op.LoadSeq(ty, _) => (ty, [])
123 : jhr 2796 | Op.LoadImage(ty, _) => (ty, [])
124 : jhr 1640 | Op.Print tys => (Ty.TupleTy[], tys)
125 : jhr 2345 | _ => raise Fail("sigOf: invalid operator " ^ Op.toString rator)
126 :     (* end case *))
127 : jhr 458
128 : jhr 2722 fun typeOfCons (Ty.SeqTy(ty, n), tys) =
129 : jhr 1689 List.all (fn ty' => Ty.same(ty, ty')) tys andalso (List.length tys = n)
130 : jhr 1116 | typeOfCons (expectedTy, tys as ty1::_) =
131 : jhr 2345 if List.all (fn ty => Ty.same(ty1, ty)) tys
132 :     then (case (expectedTy, ty1)
133 :     of (Ty.SeqTy(_, n), Ty.IntTy) => (n = List.length tys)
134 :     | (Ty.TensorTy dd, Ty.TensorTy dd') => (dd = List.length tys :: dd')
135 :     | _ => false
136 :     (* end case *))
137 :     else false
138 : jhr 3073 | typeOfCons _ = false
139 : jhr 458
140 :     end
141 :    
142 :     structure CheckLowIL = CheckILFn (
143 :     structure IL = LowIL
144 :     structure OpTy = CheckOps)
145 : jhr 1116
146 :     structure LowPP = SSAPPFn (LowIL)

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