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

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/high-to-mid/high-to-mid.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/high-to-mid/high-to-mid.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 392 - (view) (download)
Original Path: trunk/src/compiler/high-to-mid/high-to-mid.sml

1 : jhr 280 (* high-to-mid.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * Translation from HighIL to MidIL representations.
7 :     *)
8 :    
9 :     structure HighToMid : sig
10 :    
11 :     val translate : HighIL.program -> MidIL.program
12 :    
13 :     end = struct
14 :    
15 :     structure SrcIL = HighIL
16 : jhr 392 structure SrcTy = HighILTypes
17 : jhr 334 structure SrcOp = HighOps
18 : jhr 364 structure VTbl = SrcIL.Var.Tbl
19 : jhr 280 structure DstIL = MidIL
20 : jhr 391 structure DstTy = MidILTypes
21 : jhr 334 structure DstOp = MidOps
22 : jhr 280
23 : jhr 367 type var_env = DstIL.var VTbl.hash_table
24 : jhr 364
25 :     fun rename (env : var_env, x) = (case VTbl.find env x
26 : jhr 334 of SOME x' => x'
27 : jhr 364 | NONE => let
28 :     val x' = DstIL.Var.new (SrcIL.Var.name x)
29 :     in
30 :     VTbl.insert env (x, x');
31 :     x'
32 :     end
33 : jhr 334 (* end case *))
34 : jhr 367 fun renameList (env, xs) = List.map (fn x => rename(env, x)) xs
35 : jhr 334
36 : jhr 280 (* expand the field Inside operator into a image-space test *)
37 :     fun expandInside (env, result, pos, fld) = let
38 : jhr 365 val pos = rename (env, pos)
39 :     val fld = (case SrcIL.Var.binding fld
40 :     of SrcIL.VB_RHS(SrcIL.OP(SrcOp.Field fld, [])) => fld
41 : jhr 280 | _ => raise Fail "bogus field binding"
42 :     (* end case *))
43 : jhr 328 fun expand (FieldDef.CONV(_, img, _)) = let
44 : jhr 334 val imgPos = DstIL.Var.new "x"
45 : jhr 280 in [
46 : jhr 364 (imgPos, DstIL.OP(DstOp.Transform img, [pos])),
47 : jhr 280 (result, DstIL.OP(DstOp.Inside img, [imgPos]))
48 :     ] end
49 : jhr 328 | expand (FieldDef.NEG fld) = expand fld
50 :     | expand (FieldDef.SUM(fld1, dlf2)) = raise Fail "expandInside: SUM"
51 : jhr 280 in
52 :     expand fld
53 :     end
54 :    
55 : jhr 358 fun expandProbe (env, result, fld, pos) = let
56 : jhr 365 val pos = rename (env, pos)
57 :     val fld = (case SrcIL.Var.binding fld
58 :     of SrcIL.VB_RHS(SrcIL.OP(SrcOp.Field fld, [])) => fld
59 : jhr 358 | _ => raise Fail "bogus field binding"
60 :     (* end case *))
61 :     in
62 : jhr 364 Probe.expand (result, fld, pos)
63 : jhr 358 end
64 : jhr 334
65 : jhr 392 fun cvtTy SrcTy.BoolTy = DstTy.BoolTy
66 :     | cvtTy SrcTy.StringTy = DstTy.StringTy
67 :     | cvtTy SrcTy.IntTy = DstTy.intTy
68 :     | cvtTy (SrcTy.TensorTy[]) = DstTy.realTy
69 :     | cvtTy (SrcTy.TensorTy[d]) = DstTy.VecTy d
70 : jhr 365 | cvtTy _ = raise Fail "unexpected higher-order tensor type"
71 :    
72 : jhr 392 fun arity (SrcTy.TensorTy[]) = 1
73 :     | arity (SrcTy.TensorTy[d]) = d
74 : jhr 365 | arity _ = raise Fail "arity"
75 :    
76 : jhr 364 fun expandOp (env, y, rator, args) = let
77 : jhr 365 fun assign rator' =
78 : jhr 367 [(y, DstIL.OP(rator', renameList(env, args)))]
79 : jhr 364 in
80 :     case rator
81 : jhr 365 of SrcOp.Add ty => assign (DstOp.Add(cvtTy ty))
82 :     | SrcOp.Sub ty => assign (DstOp.Sub(cvtTy ty))
83 :     | SrcOp.Mul ty => assign (DstOp.Mul(cvtTy ty))
84 :     | SrcOp.Div ty => assign (DstOp.Div(cvtTy ty))
85 :     | SrcOp.Neg ty => assign (DstOp.Neg(cvtTy ty))
86 :     | SrcOp.LT ty => assign (DstOp.LT(cvtTy ty))
87 :     | SrcOp.LTE ty => assign (DstOp.LTE(cvtTy ty))
88 :     | SrcOp.EQ ty => assign (DstOp.EQ(cvtTy ty))
89 :     | SrcOp.NEQ ty => assign (DstOp.NEQ(cvtTy ty))
90 :     | SrcOp.GT ty => assign (DstOp.GT(cvtTy ty))
91 :     | SrcOp.GTE ty => assign (DstOp.GTE(cvtTy ty))
92 :     | SrcOp.Dot ty => assign (DstOp.Dot(arity ty))
93 :     | SrcOp.Cross => assign DstOp.Cross
94 :     | SrcOp.Norm ty => assign (DstOp.Norm(arity ty))
95 :     | SrcOp.Scale ty => assign (DstOp.Scale(arity ty))
96 :     | SrcOp.InvScale ty => assign (DstOp.InvScale(arity ty))
97 :     | SrcOp.CL => assign DstOp.CL
98 :     | SrcOp.PrincipleEvec ty => assign (DstOp.PrincipleEvec(cvtTy ty))
99 :     | SrcOp.Subscript ty => assign (DstOp.Subscript(cvtTy ty))
100 :     | SrcOp.Max => assign DstOp.Max
101 :     | SrcOp.Min => assign DstOp.Min
102 :     | SrcOp.Sin => assign DstOp.Sin
103 :     | SrcOp.Cos => assign DstOp.Cos
104 :     | SrcOp.Pow => assign DstOp.Pow
105 :     | SrcOp.Not => assign DstOp.Not
106 :     | SrcOp.IntToReal => assign DstOp.IntToReal
107 :     | SrcOp.TruncToInt => assign (DstOp.TruncToInt 1)
108 :     | SrcOp.RoundToInt => assign (DstOp.RoundToInt 1)
109 :     | SrcOp.CeilToInt => assign (DstOp.CeilToInt 1)
110 :     | SrcOp.FloorToInt => assign (DstOp.FloorToInt 1)
111 :     | SrcOp.LoadImage info => assign (DstOp.LoadImage info)
112 :     | SrcOp.Inside => (case args
113 :     of [pos, fld] => expandInside(env, y, pos, fld)
114 :     (* end case *))
115 :     (* QUESTION: there is no MidIL equivalant, but these might be floating
116 :     * around, which could cause invalid MidIL to be generated.
117 :     *)
118 :     | SrcOp.Field fld => []
119 :     | SrcOp.Probe => (case args
120 :     of [fld, pos] => expandProbe(env, y, fld, pos)
121 :     (* end case *))
122 :     | SrcOp.Input s => assign (DstOp.Input s)
123 :     | SrcOp.InputWithDefault s => assign (DstOp.InputWithDefault s)
124 :     | _ => raise Fail("unexpected " ^ SrcOp.toString rator)
125 :     (* end case *)
126 :     end
127 : jhr 314
128 : jhr 387 (* expand a SrcIL assignment to a list of DstIL assignments *)
129 : jhr 364 fun expand (env, (y, rhs)) = let
130 :     val y' = rename (env, y)
131 :     fun assign rhs = [(y', rhs)]
132 :     in
133 :     case rhs
134 :     of SrcIL.VAR x => assign (DstIL.VAR(rename(env, x)))
135 :     | SrcIL.LIT lit => assign (DstIL.LIT lit)
136 : jhr 367 | SrcIL.OP(rator, args) => expandOp (env, y', rator, args)
137 :     | SrcIL.CONS args => assign (DstIL.CONS(renameList(env, args)))
138 : jhr 364 (* end case *)
139 :     end
140 :    
141 :     structure Trans = TranslateFn (
142 :     struct
143 :     structure SrcIL = SrcIL
144 :     structure DstIL = DstIL
145 :    
146 :     type var_env = var_env
147 :    
148 :     val rename = rename
149 :     val expand = expand
150 :     end)
151 :    
152 : jhr 367 fun translate (SrcIL.Program{globals, globalInit, actors}) = let
153 :     val env = VTbl.mkTable (256, Fail "env")
154 :     fun transMethod (SrcIL.Method{name, stateIn, stateOut, body}) =
155 :     DstIL.Method{
156 :     name = name,
157 :     stateIn = renameList (env, stateIn),
158 :     stateOut = renameList (env, stateOut),
159 :     body = Trans.translate (env, body)
160 :     }
161 :     fun transActor (SrcIL.Actor{name, params, state, stateInit, methods}) =
162 :     DstIL.Actor{
163 :     name = name,
164 :     params = renameList (env, params),
165 :     state = renameList (env, state),
166 :     stateInit = Trans.translate (env, stateInit),
167 :     methods = List.map transMethod methods
168 :     }
169 :     in
170 :     DstIL.Program{
171 :     globals = renameList (env, globals),
172 :     globalInit = Trans.translate (env, globalInit),
173 :     actors = List.map transActor actors
174 :     }
175 :     end
176 : jhr 364
177 : jhr 280 end

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