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

SCM Repository

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

Annotation of /branches/charisee/src/compiler/mid-to-low/mid-to-low.sml

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : lamonts 345 (* mid-to-low.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : lamonts 345 * All rights reserved.
5 :     *
6 :     * Translation from MidIL to LowIL representations.
7 :     *)
8 :    
9 :     structure MidToLow : sig
10 :    
11 : jhr 459 val translate : MidIL.program -> LowIL.program
12 : lamonts 345
13 : jhr 387 end = struct
14 : lamonts 345
15 : jhr 387
16 : lamonts 345 structure SrcIL = MidIL
17 :     structure SrcOp = MidOps
18 : jhr 387 structure VTbl = SrcIL.Var.Tbl
19 : lamonts 345 structure DstIL = LowIL
20 : jhr 463 structure DstTy = LowTypes
21 : lamonts 345 structure DstOp = LowOps
22 :    
23 : jhr 387 type var_env = DstIL.var VTbl.hash_table
24 :    
25 :     fun rename (env : var_env, x) = (case VTbl.find env x
26 : lamonts 345 of SOME x' => x'
27 : jhr 387 | NONE => let
28 : jhr 460 val x' = DstIL.Var.new (SrcIL.Var.name x, SrcIL.Var.ty x)
29 : jhr 387 in
30 :     VTbl.insert env (x, x');
31 :     x'
32 :     end
33 : lamonts 345 (* end case *))
34 : jhr 387 fun renameList (env, xs) = List.map (fn x => rename(env, x)) xs
35 : lamonts 345
36 : jhr 463 (* convert a rational to a FloatLit.float value. We do this by long division
37 :     * with a cutoff when we get to 12 digits.
38 :     *)
39 :     fun ratToFloat r = (case Rational.explode r
40 :     of {sign=0, ...} => FloatList.zero
41 :     | {sign, num, denom=1} => FloatLit.fromInt(sign * IntInf.toInt num)
42 :     | {sign, num, denom} => let
43 :     (* normalize so that num <= denom *)
44 :     val (denom, exp) = let
45 :     fun lp (n, denom) = if (denom < num)
46 :     then lp(n+1, denom*10)
47 :     else (n, denom)
48 :     in
49 :     lp (0, denom)
50 :     end
51 :     (* normalize so that num <= denom < 10*num *)
52 :     (* FIXME *)
53 :     (* divide num/denom, computing the resulting digits *)
54 :     fun divLp (n, a) = let
55 :     val (q, r) = IntInf.divMod(a, denom)
56 :     in
57 :     if (r = 0) then (q, [])
58 :     else if (n < 12) then let
59 :     val (d, dd) = divLp(n+1, 10*r)
60 :     in
61 :     if (d < 10) then (q, d::dd) else (q+1, 0::dd)
62 :     end
63 :     else if (IntInf.div(10*r, denom) < 5)
64 :     then (q, [])
65 :     else (q+1, []) (* round up *)
66 :     end
67 :     val digits = divLp (0, a)
68 :     in
69 :     (* FIXME *)
70 :     end
71 :    
72 :     (* variable names for kernel coefficients *)
73 : jhr 459 (* expand the EvalKernel operations into vector operations. The parameters are
74 :     * result -- the lhs variable to store the result
75 :     * d -- the vector width of the operation, which should be equal to twice the
76 :     * support of the kernel
77 :     * h -- the kernel
78 :     * k -- the derivative of the kernel to evaluate
79 :     *)
80 : jhr 463 fun expandEvalKernel (result, d, h, k, [x]) = let
81 : jhr 459 val {isCont, segs} = Kernel.curve (h, k)
82 : jhr 463 val deg = List.length segs - i
83 :     (* convert to a vector of vectors to give fast access *)
84 :     val segs = Vector.fromList (List.map Vector.fromList segs)
85 :     (* get the kernel coefficient value for the d'th term of the i'th
86 :     * segment.
87 :     *)
88 :     fun coefficient d i = ratToFloat (Vector.sub (Vector.sub(segs, i), d))
89 :     val ty = DstTy.VecTy d
90 :     val coeffs = List.tabulate (deg+1,
91 :     fn i => DstIL.Var.new(str(chr(ord #"a" + (deg - i)))), ty)
92 :     (* define coefficient vectors *)
93 :     val coeffVecs = ???
94 :     (* build the evaluation of the polynomials in reverse order *)
95 :     fun eval [coeff] = (coeff, coeffVecs)
96 :     | eval (coeff::r) = let
97 :     val (t1, stms) = eval r
98 :     val t2 = DstIL.Var.new ("_t", ty)
99 :     val t3 = DstIL.Var.new ("_s", ty)
100 :     val stms =
101 :     (t3, DstIL.OP(DstOp.Add ty, [coeff, t2])) ::
102 :     (t2, DstIL.OP(DstOp.Mul ty, [x, t1])) ::
103 :     stms
104 :     in
105 :     (t3, stms)
106 :     end
107 : jhr 459 in
108 : jhr 463 List.rev (eval coeffs)
109 : jhr 459 end
110 : jhr 387
111 : lamonts 345 (* compute the load address for a given set of voxels indices *)
112 : jhr 460 fun expandVoxelAddress (result, info) = raise Fail "unimplemented"
113 : lamonts 345
114 : jhr 431 fun expandOp (env, y, rator, args) = let
115 :     fun assign rator' =
116 :     [(y, DstIL.OP(rator', renameList(env, args)))]
117 :     in
118 :     case rator
119 : jhr 459 of SrcOp.Add ty => assign (DstOp.Add ty)
120 :     | SrcOp.Sub ty => assign (DstOp.Sub ty)
121 :     | SrcOp.Mul ty => assign (DstOp.Mul ty)
122 :     | SrcOp.Div ty => assign (DstOp.Div ty)
123 :     | SrcOp.Neg ty => assign (DstOp.Neg ty)
124 :     | SrcOp.LT ty => assign (DstOp.LT ty)
125 :     | SrcOp.LTE ty => assign (DstOp.LTE ty)
126 :     | SrcOp.EQ ty => assign (DstOp.EQ ty)
127 :     | SrcOp.NEQ ty => assign (DstOp.NEQ ty)
128 :     | SrcOp.GT ty => assign (DstOp.GT ty)
129 :     | SrcOp.GTE ty => assign (DstOp.GTE ty)
130 :     | SrcOp.Not => assign (DstOp.Not)
131 :     | SrcOp.Max => assign (DstOp.Max)
132 :     | SrcOp.Min => assign (DstOp.Min)
133 :     | SrcOp.Sin => assign (DstOp.Sin)
134 :     | SrcOp.Cos => assign (DstOp.Cos)
135 :     | SrcOp.Pow => assign (DstOp.Pow)
136 :     | SrcOp.Dot d => assign (DstOp.Dot d)
137 :     | SrcOp.Cross => assign (DstOp.Cross)
138 : jhr 460 | SrcOp.Select(ty, i)=> assign (DstOp.Select(ty, i))
139 : jhr 459 | SrcOp.Norm d => assign (DstOp.Norm d)
140 :     | SrcOp.Scale d => assign (DstOp.Scale d)
141 :     | SrcOp.InvScale d => assign (DstOp.InvScale d)
142 :     | SrcOp.CL => assign (DstOp.CL)
143 :     | SrcOp.PrincipleEvec ty => assign (DstOp.PrincipleEvec ty)
144 :     | SrcOp.Subscript ty => assign (DstOp.Subscript ty)
145 :     | SrcOp.Floor d => assign (DstOp.Floor d)
146 :     | SrcOp.IntToReal => assign (DstOp.IntToReal)
147 :     | SrcOp.TruncToInt d => assign (DstOp.TruncToInt d)
148 :     | SrcOp.RoundToInt d => assign (DstOp.RoundToInt d)
149 :     | SrcOp.CeilToInt d => assign (DstOp.CeilToInt d)
150 :     | SrcOp.FloorToInt d => assign (DstOp.FloorToInt d)
151 :     | SrcOp.VoxelAddress info => expandVoxelAddress (y, info)
152 :     | SrcOp.LoadVoxels(rty, d) => assign (DstOp.LoadVoxels(rty, d))
153 : jhr 460 | SrcOp.PosToImgSpace info => assign (DstOp.PosToImgSpace info)
154 :     | SrcOp.GradToWorldSpace info => assign (DstOp.GradToWorldSpace info)
155 : jhr 459 | SrcOp.EvalKernel(d, h, k) => expandEvalKernel(y, d, h, k)
156 :     | SrcOp.LoadImage info => assign (DstOp.LoadImage info)
157 :     | SrcOp.Inside info => assign (DstOp.Inside info)
158 :     | SrcOp.Input(ty, name) => assign (DstOp.Input(ty, name))
159 :     | SrcOp.InputWithDefault(ty, name) => assign (DstOp.InputWithDefault(ty, name))
160 : jhr 431 (* end case *)
161 :     end
162 :    
163 : jhr 387 (* expand a SrcIL assignment to a list of DstIL assignments *)
164 :     fun expand (env, (y, rhs)) = let
165 :     val y' = rename (env, y)
166 :     fun assign rhs = [(y', rhs)]
167 :     in
168 :     case rhs
169 :     of SrcIL.VAR x => assign (DstIL.VAR(rename(env, x)))
170 :     | SrcIL.LIT lit => assign (DstIL.LIT lit)
171 :     | SrcIL.OP(rator, args) => expandOp (env, y', rator, args)
172 :     | SrcIL.CONS args => assign (DstIL.CONS(renameList(env, args)))
173 :     (* end case *)
174 :     end
175 : lamonts 345
176 : jhr 387 structure Trans = TranslateFn (
177 :     struct
178 :     structure SrcIL = SrcIL
179 :     structure DstIL = DstIL
180 :    
181 :     type var_env = var_env
182 :    
183 :     val rename = rename
184 :     val expand = expand
185 :     end)
186 :    
187 :     fun translate (SrcIL.Program{globals, globalInit, actors}) = let
188 :     val env = VTbl.mkTable (256, Fail "env")
189 :     fun transMethod (SrcIL.Method{name, stateIn, stateOut, body}) =
190 :     DstIL.Method{
191 :     name = name,
192 :     stateIn = renameList (env, stateIn),
193 :     stateOut = renameList (env, stateOut),
194 :     body = Trans.translate (env, body)
195 :     }
196 :     fun transActor (SrcIL.Actor{name, params, state, stateInit, methods}) =
197 :     DstIL.Actor{
198 :     name = name,
199 :     params = renameList (env, params),
200 :     state = renameList (env, state),
201 :     stateInit = Trans.translate (env, stateInit),
202 :     methods = List.map transMethod methods
203 :     }
204 :     in
205 :     DstIL.Program{
206 :     globals = renameList (env, globals),
207 :     globalInit = Trans.translate (env, globalInit),
208 :     actors = List.map transActor actors
209 :     }
210 :     end
211 :    
212 : jhr 435 end

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