SCM Repository
Annotation of /branches/pure-cfg/src/compiler/codegen/codegen-fn.sml
Parent Directory
|
Revision Log
Revision 617 - (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 | 532 | structure IL = TreeIL |
18 : | structure Ty = IL.Ty | ||
19 : | structure Op = IL.Op | ||
20 : | structure V = IL.Var | ||
21 : | jhr | 454 | |
22 : | jhr | 525 | (* convert LowIL types to T types *) |
23 : | fun cvtTy ty = (case ty | ||
24 : | of Ty.BoolTy => T.boolTy | ||
25 : | jhr | 533 | | Ty.StringTy => T.stringTy |
26 : | jhr | 525 | | Ty.IVecTy 1 => T.intTy |
27 : | | Ty.IVecTy n => T.ivecTy n (* FIXME: what about vector splits? *) | ||
28 : | | Ty.VecTy 1 => T.realTy | ||
29 : | | Ty.VecTy n => T.vecTy n (* FIXME: what about vector splits? *) | ||
30 : | jhr | 548 | | Ty.AddrTy info => T.imageDataTy info |
31 : | | Ty.ImageTy info => T.imageTy info | ||
32 : | jhr | 525 | (* end case *)) |
33 : | |||
34 : | jhr | 544 | fun addBindings (env, xs, ys) = |
35 : | ListPair.foldlEq (fn (x, y, env) => V.Map.insert(env, x, y)) env (xs, ys) | ||
36 : | |||
37 : | jhr | 532 | fun lookup (env, x) = (case V.Map.find (env, x) |
38 : | of SOME x' => x' | ||
39 : | jhr | 534 | | NONE => raise Fail(concat["lookup(_, ", V.name x, ")"]) |
40 : | jhr | 532 | (* end case *)) |
41 : | |||
42 : | fun trExp (env, e) = (case e | ||
43 : | of IL.E_Var x => (case V.kind x | ||
44 : | of IL.VK_Global => T.Expr.global(lookup(env, x)) | ||
45 : | jhr | 552 | | IL.VK_State strand => T.Expr.getState(lookup(env, x)) |
46 : | jhr | 532 | | IL.VK_Local => T.Expr.var(lookup(env, x)) |
47 : | (* end case *)) | ||
48 : | | IL.E_Lit(Literal.Int n) => T.Expr.intLit n | ||
49 : | | IL.E_Lit(Literal.Bool b) => T.Expr.boolLit b | ||
50 : | | IL.E_Lit(Literal.Float f) => T.Expr.floatLit f | ||
51 : | | IL.E_Lit(Literal.String s) => T.Expr.stringLit s | ||
52 : | | IL.E_Op(rator, args) => (case (rator, trExps(env, args)) | ||
53 : | of (Op.Add ty, [a, b]) => T.Expr.add(a, b) | ||
54 : | | (Op.Sub ty, [a, b]) => T.Expr.sub(a, b) | ||
55 : | | (Op.Mul ty, [a, b]) => T.Expr.mul(a, b) | ||
56 : | | (Op.Div ty, [a, b]) => T.Expr.divide(a, b) | ||
57 : | | (Op.Neg ty, [a]) => T.Expr.neg a | ||
58 : | | (Op.LT ty, [a, b]) => T.Expr.lt(a, b) | ||
59 : | | (Op.LTE ty, [a, b]) => T.Expr.lte(a, b) | ||
60 : | | (Op.EQ ty, [a, b]) => T.Expr.equ(a, b) | ||
61 : | | (Op.NEQ ty, [a, b]) => T.Expr.neq(a, b) | ||
62 : | | (Op.GT ty, [a, b]) => T.Expr.gt(a, b) | ||
63 : | | (Op.GTE ty, [a, b]) => T.Expr.gte(a, b) | ||
64 : | | (Op.Not, [a]) => T.Expr.not a | ||
65 : | | (Op.Max, [a, b]) => T.Expr.max(a, b) | ||
66 : | | (Op.Min, [a, b]) => T.Expr.min(a, b) | ||
67 : | | (Op.Sin, [a]) => T.Expr.sin a | ||
68 : | | (Op.Cos, [a]) => T.Expr.cos a | ||
69 : | | (Op.Pow, [a, b]) => T.Expr.pow(a, b) | ||
70 : | | (Op.Dot d, [a, b]) => T.Expr.dot(a, b) | ||
71 : | | (Op.Cross, [a, b]) => T.Expr.cross(a, b) | ||
72 : | | (Op.Select(ty, i), [a]) => T.Expr.select(i, a) | ||
73 : | | (Op.Norm d, [a]) => T.Expr.length a | ||
74 : | | (Op.Scale d, [a, b]) => T.Expr.mul(a, b) | ||
75 : | | (Op.InvScale d, [a, b]) => T.Expr.divide(a, b) | ||
76 : | jhr | 518 | | (Op.CL, _) => raise Fail "CL unimplemented" |
77 : | | (Op.PrincipleEvec ty, _) => raise Fail "PrincipleEvec unimplemented" | ||
78 : | jhr | 514 | (* |
79 : | jhr | 518 | | (Op.Subscript ty, |
80 : | jhr | 514 | *) |
81 : | jhr | 565 | | (Op.Ceiling d, [a]) => T.Expr.ceil a |
82 : | jhr | 532 | | (Op.Floor d, [a]) => T.Expr.floor a |
83 : | jhr | 565 | | (Op.Round d, [a]) => T.Expr.round a |
84 : | | (Op.Trunc d, [a]) => T.Expr.trunc a | ||
85 : | jhr | 532 | | (Op.IntToReal, [a]) => T.Expr.toReal a |
86 : | jhr | 565 | | (Op.RealToInt d, [a]) => T.Expr.toInt a |
87 : | jhr | 548 | | (Op.ImageAddress d, [a]) => T.Expr.imageAddr a |
88 : | | (Op.LoadVoxels(info, 1), [a]) => T.Expr.getImgData a | ||
89 : | | (Op.LoadVoxels _, [a]) => raise Fail "impossible" | ||
90 : | | (Op.PosToImgSpace d, [v, x]) => T.Expr.posToImgSpace(v, x) | ||
91 : | jhr | 532 | | (Op.GradToWorldSpace d, [v, x]) => T.Expr.intLit 0 (* FIXME *) |
92 : | jhr | 547 | | (Op.LoadImage info, [a]) => raise Fail "impossible" |
93 : | jhr | 548 | | (Op.Inside(d, s), [x, v]) => T.Expr.inside(x, v, s) |
94 : | jhr | 547 | | (Op.Input(ty, name), []) => raise Fail "impossible" |
95 : | jhr | 532 | | (Op.InputWithDefault(ty, name), [a]) => T.Expr.intLit 0 (* FIXME *) |
96 : | jhr | 528 | | _ => raise Fail(concat[ |
97 : | "incorrect number of arguments for ", Op.toString rator | ||
98 : | ]) | ||
99 : | jhr | 518 | (* end case *)) |
100 : | jhr | 532 | (* end case *)) |
101 : | jhr | 454 | |
102 : | jhr | 532 | and trExps (env, exps) = List.map (fn exp => trExp(env, exp)) exps |
103 : | jhr | 455 | |
104 : | jhr | 563 | fun trBody (stateVars, env, body) = let |
105 : | fun trStmt (env, stm) = (case stm | ||
106 : | of IL.S_Comment text => [T.Stmt.comment text] | ||
107 : | | IL.S_Assign(x, exp) => (case V.kind x | ||
108 : | of IL.VK_Global => [T.Stmt.assign(lookup(env, x), trExp(env, exp))] | ||
109 : | | IL.VK_State strand => | ||
110 : | [T.Stmt.assignState(lookup(env, x), trExp(env, exp))] | ||
111 : | | IL.VK_Local => [T.Stmt.assign(lookup(env, x), trExp(env, exp))] | ||
112 : | (* end case *)) | ||
113 : | jhr | 615 | | IL.S_IfThen(cond, thenBlk) => |
114 : | [T.Stmt.ifthen(trExp(env, cond), trBlock(env, thenBlk))] | ||
115 : | | IL.S_IfThenElse(cond, thenBlk, elseBlk) => | ||
116 : | [T.Stmt.ifthenelse(trExp(env, cond), | ||
117 : | trBlock(env, thenBlk), | ||
118 : | trBlock(env, elseBlk))] | ||
119 : | | IL.S_For(x, e1, e2, blk) => let | ||
120 : | val x' = T.Var.var(cvtTy(V.ty x), V.name x) | ||
121 : | val env' = V.Map.insert(env, x, x') | ||
122 : | in [ | ||
123 : | jhr | 617 | T.Stmt.for(x', trExp(env, e1), trExp(env, e2), trBlock(env', blk)) |
124 : | jhr | 615 | ] end |
125 : | jhr | 563 | | IL.S_Cons(lhs, args) => |
126 : | [T.Stmt.cons(lookup(env, lhs), trExps(env, args))] | ||
127 : | | IL.S_LoadVoxels(lhs, dim, addr) => | ||
128 : | T.Stmt.getImgData(lookup(env, lhs), trExp(env, addr)) | ||
129 : | | IL.S_LoadImage(lhs, dim, name) => | ||
130 : | T.Stmt.loadImage (lookup(env, lhs), dim, trExp(env, name)) | ||
131 : | | IL.S_Input(lhs, name, optDflt) => | ||
132 : | T.Stmt.input(lookup(env, lhs), name, Option.map (fn e => trExp(env, e)) optDflt) | ||
133 : | (* FIXME: what about the args? *) | ||
134 : | | IL.S_Exit args => [T.Stmt.exit()] | ||
135 : | | IL.S_Active args => | ||
136 : | ListPair.foldrEq | ||
137 : | (fn (x, e, stms) => T.Stmt.assignState(x, trExp(env, e))::stms) | ||
138 : | jhr | 564 | [T.Stmt.active()] |
139 : | jhr | 563 | (stateVars, args) |
140 : | | IL.S_Stabilize args => | ||
141 : | ListPair.foldrEq | ||
142 : | (fn (x, e, stms) => T.Stmt.assignState(x, trExp(env, e))::stms) | ||
143 : | [T.Stmt.stabilize()] | ||
144 : | (stateVars, args) | ||
145 : | | IL.S_Die => [T.Stmt.die()] | ||
146 : | jhr | 512 | (* end case *)) |
147 : | jhr | 563 | and trBlock (env, IL.Block{locals, body}) = let |
148 : | val env = List.foldl | ||
149 : | (fn (x, env) => V.Map.insert(env, x, T.Var.var(cvtTy(V.ty x), V.name x))) | ||
150 : | env locals | ||
151 : | val stms = List.foldr (fn (stm, stms) => trStmt(env, stm)@stms) [] body | ||
152 : | val stms = List.foldr | ||
153 : | (fn (x, stms) => T.Stmt.decl(lookup(env, x), NONE)::stms) | ||
154 : | stms locals | ||
155 : | in | ||
156 : | T.Stmt.block stms | ||
157 : | end | ||
158 : | jhr | 544 | in |
159 : | jhr | 563 | trBlock (env, body) |
160 : | jhr | 544 | end |
161 : | jhr | 528 | |
162 : | jhr | 563 | fun trMethod (strand, state, env) (IL.Method{name, body}) = |
163 : | T.Strand.method (strand, Atom.toString name, trBody (state, env, body)) | ||
164 : | jhr | 547 | |
165 : | jhr | 544 | fun trStrand (prog, env) (IL.Strand{name, params, state, stateInit, methods}) = let |
166 : | val strand = T.Strand.define(prog, Atom.toString name) | ||
167 : | val state' = | ||
168 : | List.map (fn x => T.Var.state(strand, cvtTy(V.ty x), V.name x)) state | ||
169 : | val env = addBindings (env, state, state') | ||
170 : | (* define the parameters and add them to the environment *) | ||
171 : | val params' = List.map (fn x => T.Var.param(cvtTy(V.ty x), V.name x)) params | ||
172 : | val env = addBindings (env, params, params') | ||
173 : | in | ||
174 : | jhr | 563 | T.Strand.init (strand, params', trBody ([], env, stateInit)); |
175 : | List.app (trMethod (strand, state', env)) methods | ||
176 : | jhr | 544 | end |
177 : | |||
178 : | jhr | 531 | fun generate (fileStem, srcProg) = let |
179 : | jhr | 535 | val treeProg as TreeIL.Program{globals, globalInit, strands} = LowToTree.translate srcProg |
180 : | val _ = ( | ||
181 : | TextIO.output(Log.logFile(), "********** After translation to TreeIL **********\n"); | ||
182 : | TreeILPP.program (Log.logFile(), treeProg)) | ||
183 : | jhr | 527 | val prog = T.newProgram () |
184 : | jhr | 532 | (* define the globals and initialize the environment *) |
185 : | val env = let | ||
186 : | fun gvar (x, env) = | ||
187 : | V.Map.insert(env, x, T.Var.global(prog, cvtTy(V.ty x), V.name x)) | ||
188 : | in | ||
189 : | List.foldl gvar V.Map.empty globals | ||
190 : | end | ||
191 : | jhr | 527 | in |
192 : | jhr | 533 | (* global initialization *) |
193 : | jhr | 563 | T.globalInit (prog, trBody ([], env, globalInit)); |
194 : | jhr | 544 | (* translate strands *) |
195 : | List.app (trStrand (prog, env)) strands; | ||
196 : | jhr | 528 | (* output the program *) |
197 : | jhr | 527 | T.generate (fileStem, prog) |
198 : | end | ||
199 : | jhr | 518 | |
200 : | jhr | 454 | end |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |