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

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