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

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/compiler/codegen/codegen-fn.sml
ViewVC logotype

Annotation of /branches/pure-cfg/src/compiler/codegen/codegen-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 522 - (view) (download)

1 : jhr 454 (* codegen-fn.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 : jhr 455 *
6 :     * Generic support for translating LowIL code to the target representation. We
7 :     * assume that the LowIL has first been run through the splitting pass to match
8 :     * the target's vector widths.
9 : jhr 454 *)
10 :    
11 :     functor CodeGenFn (T : TARGET) : sig
12 :    
13 : jhr 518 val generate : string * LowIL.program -> unit
14 : jhr 454
15 :     end = struct
16 :    
17 : jhr 512 structure IL = LowIL
18 :     structure V = LowIL.Var
19 :     structure Op = LowOps
20 :     structure Nd = LowIL.Node
21 : jhr 514 structure CFG = LowIL.CFG
22 : jhr 454
23 :     (* a mapping from LowIL variables to target expressions. Variables get
24 :     * removed when the expressions are used as arguments.
25 :     *)
26 :     structure VDefTbl : sig
27 :     type tbl
28 :    
29 :     val newTbl : unit -> tbl
30 : jhr 518 val getDefOf : tbl -> IL.var -> T.exp
31 :     val useDefOf : tbl -> IL.var -> T.exp
32 : jhr 514 val setDefOf : tbl -> (IL.var * T.exp) -> unit
33 : jhr 454
34 :     val clear : tbl -> unit
35 :    
36 : jhr 514 val assign : tbl -> (IL.var * T.exp) -> T.stm list
37 :     val bind : tbl -> (IL.var * T.exp) -> T.stm list
38 : jhr 454
39 : jhr 455 (* force all pending expressions into variables *)
40 :     val flush : tbl -> T.stm list
41 :    
42 : jhr 454 end = struct
43 :    
44 : jhr 455 type info = {
45 :     cnt : int ref, (* count of oustanding uses (usually 1) *)
46 : jhr 518 bind : T.exp
47 : jhr 455 }
48 :    
49 : jhr 512 type tbl = info V.Tbl.hash_table
50 : jhr 455
51 : jhr 512 fun newTbl () = V.Tbl.mkTable (512, Fail "vtbl")
52 : jhr 455
53 : jhr 512 fun getDefOf tbl x = (case V.Tbl.find tbl x
54 : jhr 514 of NONE => raise Fail(concat["getDefOf(", V.toString x, ")"])
55 : jhr 455 | SOME{bind, cnt} => bind
56 :     (* end case *))
57 :    
58 : jhr 512 fun useDefOf tbl x = (case V.Tbl.find tbl x
59 : jhr 514 of NONE => raise Fail(concat["useDefOf(", V.toString x, ")"])
60 : jhr 512 | SOME{cnt=ref 1, bind} => (
61 :     ignore (V.Tbl.remove tbl x);
62 : jhr 455 bind)
63 : jhr 514 | SOME{cnt, bind} => (
64 : jhr 455 cnt := !cnt - 1;
65 :     bind)
66 :     (* end case *))
67 :    
68 :     fun setDefOf tbl (x, exp) =
69 : jhr 518 V.Tbl.insert tbl (x, {cnt = ref(V.useCount x), bind = exp})
70 : jhr 455
71 :     fun assign tbl (x, exp) = let
72 : jhr 522 val lhs = T.Var.tmp()
73 : jhr 455 in
74 : jhr 512 V.Tbl.insert tbl
75 : jhr 518 (x, {cnt = ref(V.useCount x), bind = T.Expr.var lhs});
76 : jhr 455 [T.Stmt.assign(lhs, exp)]
77 :     end
78 :    
79 : jhr 514 fun bind tbl (x, exp) = (case V.useCount x
80 : jhr 518 of 1 => (V.Tbl.insert tbl (x, {cnt = ref 1, bind = exp}); [])
81 : jhr 455 | n => let (* bind exp to a new target variable *)
82 : jhr 522 val lhs = T.Var.tmp()
83 : jhr 455 in
84 : jhr 518 V.Tbl.insert tbl (x, {cnt = ref n, bind = T.Expr.var lhs});
85 : jhr 455 [T.Stmt.assign(lhs, exp)]
86 :     end
87 :     (* end case *))
88 :    
89 : jhr 514 val clear = V.Tbl.clear
90 :    
91 :     fun flush tbl = raise Fail "flush"
92 :    
93 : jhr 454 end (* VDefTbl *)
94 :    
95 : jhr 455 (* FIXME: what about splitting code where the target width doesn't match the
96 :     * source width?
97 :     *)
98 : jhr 514 fun doRator vtbl = let
99 : jhr 518 val exp = VDefTbl.useDefOf vtbl
100 : jhr 514 val bind = VDefTbl.bind vtbl
101 : jhr 518 fun f (lhs, rator, args) = (case (rator, args)
102 :     of (Op.Add ty, [a, b]) => bind (lhs, T.Expr.add(exp a, exp b))
103 :     | (Op.Sub ty, [a, b]) => bind (lhs, T.Expr.sub(exp a, exp b))
104 :     | (Op.Mul ty, [a, b]) => bind (lhs, T.Expr.mul(exp a, exp b))
105 :     | (Op.Div ty, [a, b]) => bind (lhs, T.Expr.divide(exp a, exp b))
106 :     | (Op.Neg ty, [a]) => bind (lhs, T.Expr.neg(exp a))
107 :     | (Op.LT ty, [a, b]) => bind (lhs, T.Expr.lt(exp a, exp b))
108 :     | (Op.LTE ty, [a, b]) => bind (lhs, T.Expr.lte(exp a, exp b))
109 :     | (Op.EQ ty, [a, b]) => bind (lhs, T.Expr.equ(exp a, exp b))
110 :     | (Op.NEQ ty, [a, b]) => bind (lhs, T.Expr.neq(exp a, exp b))
111 :     | (Op.GT ty, [a, b]) => bind (lhs, T.Expr.gt(exp a, exp b))
112 :     | (Op.GTE ty, [a, b]) => bind (lhs, T.Expr.gte(exp a, exp b))
113 :     | (Op.Not, [a]) => bind (lhs, T.Expr.not(exp a))
114 :     | (Op.Max, [a, b]) => bind (lhs, T.Expr.max(exp a, exp b))
115 :     | (Op.Min, [a, b]) => bind (lhs, T.Expr.min(exp a, exp b))
116 :     | (Op.Sin, [a]) => bind (lhs, T.Expr.sin(exp a))
117 :     | (Op.Cos, [a]) => bind (lhs, T.Expr.cos(exp a))
118 :     | (Op.Pow, [a, b]) => bind (lhs, T.Expr.pow(exp a, exp b))
119 :     | (Op.Dot d, [a, b]) => bind (lhs, T.Expr.dot(exp a, exp b))
120 :     | (Op.Cross, [a, b]) => bind (lhs, T.Expr.cross(exp a, exp b))
121 :     | (Op.Select(ty, i), [a]) => bind (lhs, T.Expr.select(i, exp a))
122 :     | (Op.Norm d, [a]) => bind (lhs, T.Expr.length(exp a))
123 :     | (Op.Scale d, [a, b]) => bind (lhs, T.Expr.mul(exp a, exp b))
124 :     | (Op.InvScale d, [a, b]) => bind (lhs, T.Expr.divide(exp a, exp b))
125 :     | (Op.CL, _) => raise Fail "CL unimplemented"
126 :     | (Op.PrincipleEvec ty, _) => raise Fail "PrincipleEvec unimplemented"
127 : jhr 514 (*
128 : jhr 518 | (Op.Subscript ty,
129 : jhr 514 *)
130 : jhr 518 | (Op.Floor d, [a]) => bind (lhs, T.Expr.floor(exp a))
131 :     | (Op.IntToReal, [a]) => bind (lhs, T.Expr.toReal(exp a))
132 :     | (Op.TruncToInt d, [a]) => bind (lhs, T.Expr.truncToInt(exp a))
133 :     | (Op.RoundToInt d, [a]) => bind (lhs, T.Expr.roundToInt(exp a))
134 :     | (Op.CeilToInt d, [a]) => bind (lhs, T.Expr.ceilToInt(exp a))
135 :     | (Op.FloorToInt d, [a]) => bind (lhs, T.Expr.floorToInt(exp a))
136 :     | (Op.ImageAddress, [a]) => bind (lhs, T.Expr.imageAddr(exp a))
137 : jhr 514 (*
138 : jhr 518 | (Op.LoadVoxels(rTy, n), [a]) =>
139 :     | (Op.PosToImgSpace d, [v, x]) =>
140 :     | (Op.GradToWorldSpace d, [v, x]) =>
141 :     | (Op.LoadImage info, [a]) =>
142 :     | (Op.Inside d, [v, x]) =>
143 :     | (Op.Input(ty, name), []) =>
144 :     | (Op.InputWithDefault(ty, name), [a]) =>
145 : jhr 514 *)
146 : jhr 518 (* end case *))
147 : jhr 455 in
148 : jhr 514 f
149 : jhr 455 end
150 : jhr 454
151 : jhr 455 (* translate a LowIL assignment to a list of zero or more target statements *)
152 : jhr 518 fun doAssign vtbl = let
153 : jhr 514 val setDefOf = VDefTbl.setDefOf vtbl
154 :     val doRator = doRator vtbl
155 :     fun assign (lhs, rhs) = let
156 :     fun setDef rhs = (setDefOf (lhs, rhs); [])
157 :     in
158 :     case rhs
159 : jhr 518 of IL.VAR x => setDef (VDefTbl.useDefOf vtbl x)
160 : jhr 514 | IL.LIT(Literal.Int n) => setDef (T.Expr.intLit n)
161 :     | IL.LIT(Literal.Bool b) => setDef (T.Expr.boolLit b)
162 :     | IL.LIT(Literal.Float f) => setDef (T.Expr.floatLit f)
163 :     | IL.LIT(Literal.String s) => setDef (T.Expr.stringLit s)
164 :     | IL.OP(rator, args) => doRator(lhs, rator, args)
165 :     | IL.CONS args =>
166 :     VDefTbl.assign vtbl
167 :     (lhs, T.Expr.vector (List.map (VDefTbl.useDefOf vtbl) args))
168 :     (* end case *)
169 :     end
170 : jhr 455 in
171 : jhr 514 assign
172 : jhr 455 end
173 :    
174 : jhr 512 fun gen (vtbl, cfg) = let
175 :     val doAssign = doAssign vtbl
176 : jhr 513 fun doNode (vtbl, ifCont, stms, nd) = (case Nd.kind nd
177 :     of IL.NULL => raise Fail "unexpected NULL"
178 : jhr 514 | IL.ENTRY{succ} => doNode (vtbl, ifCont, stms, !succ)
179 : jhr 518 | IL.JOIN{phis, succ, ...} => ifCont (stms, Nd.kind nd)
180 : jhr 512 | IL.COND{cond, trueBranch, falseBranch, ...} => let
181 :     fun kThen (stms', _) = let
182 :     val thenBlk = T.Stmt.block (List.rev stms')
183 : jhr 513 fun kElse (stms', IL.JOIN{phis, succ, ...}) = let
184 : jhr 512 val stm = T.Stmt.ifthenelse (
185 :     VDefTbl.useDefOf vtbl cond,
186 :     thenBlk,
187 :     T.Stmt.block (List.rev stms'))
188 :     in
189 : jhr 513 (* FIXME: what do we do about phis? *)
190 : jhr 514 doNode (vtbl, ifCont, stm::stms, !succ)
191 : jhr 512 end
192 :     in
193 : jhr 514 doNode (vtbl, kElse, [], !falseBranch)
194 : jhr 512 end
195 :     in
196 : jhr 514 doNode (vtbl, kThen, [], !trueBranch)
197 : jhr 512 end
198 :     | IL.COM {text, succ, ...} =>
199 : jhr 514 doNode (vtbl, ifCont, T.Stmt.comment text :: stms, !succ)
200 : jhr 512 | IL.ASSIGN{stm, succ, ...} =>
201 : jhr 518 doNode (vtbl, ifCont, doAssign stm @ stms, !succ)
202 : jhr 513 | IL.NEW{strand, args, succ, ...} => raise Fail "NEW unimplemented"
203 : jhr 512 | IL.DIE _ =>
204 : jhr 514 T.Stmt.block (List.rev (T.Stmt.die() :: stms))
205 : jhr 512 | IL.STABILIZE _ =>
206 : jhr 514 T.Stmt.block (List.rev stms)
207 :     | IL.EXIT _ => T.Stmt.block (List.rev (T.Stmt.stabilize() :: stms))
208 : jhr 512 (* end case *))
209 :     in
210 : jhr 513 doNode (vtbl, fn _ => raise Fail "bogus ifCont at JOIN node", [], CFG.entry cfg)
211 : jhr 512 end
212 :    
213 : jhr 518 fun generate (fileStem, IL.Program{globals, globalInit, strands}) = ()
214 :    
215 : jhr 454 end

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