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 535 - (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 533 | Ty.AddrTy => T.boolTy (* FIXME *)
31 :     | Ty.ImageTy => T.boolTy (* FIXME *)
32 : jhr 525 (* end case *))
33 :    
34 : jhr 532 fun lookup (env, x) = (case V.Map.find (env, x)
35 :     of SOME x' => x'
36 : jhr 534 | NONE => raise Fail(concat["lookup(_, ", V.name x, ")"])
37 : jhr 532 (* end case *))
38 :    
39 :     fun trExp (env, e) = (case e
40 :     of IL.E_Var x => (case V.kind x
41 :     of IL.VK_Global => T.Expr.global(lookup(env, x))
42 :     | IL.VK_State strand => raise Fail "FIXME: state var"
43 :     | IL.VK_Local => T.Expr.var(lookup(env, x))
44 :     (* end case *))
45 :     | IL.E_Lit(Literal.Int n) => T.Expr.intLit n
46 :     | IL.E_Lit(Literal.Bool b) => T.Expr.boolLit b
47 :     | IL.E_Lit(Literal.Float f) => T.Expr.floatLit f
48 :     | IL.E_Lit(Literal.String s) => T.Expr.stringLit s
49 :     | IL.E_Op(rator, args) => (case (rator, trExps(env, args))
50 :     of (Op.Add ty, [a, b]) => T.Expr.add(a, b)
51 :     | (Op.Sub ty, [a, b]) => T.Expr.sub(a, b)
52 :     | (Op.Mul ty, [a, b]) => T.Expr.mul(a, b)
53 :     | (Op.Div ty, [a, b]) => T.Expr.divide(a, b)
54 :     | (Op.Neg ty, [a]) => T.Expr.neg a
55 :     | (Op.LT ty, [a, b]) => T.Expr.lt(a, b)
56 :     | (Op.LTE ty, [a, b]) => T.Expr.lte(a, b)
57 :     | (Op.EQ ty, [a, b]) => T.Expr.equ(a, b)
58 :     | (Op.NEQ ty, [a, b]) => T.Expr.neq(a, b)
59 :     | (Op.GT ty, [a, b]) => T.Expr.gt(a, b)
60 :     | (Op.GTE ty, [a, b]) => T.Expr.gte(a, b)
61 :     | (Op.Not, [a]) => T.Expr.not a
62 :     | (Op.Max, [a, b]) => T.Expr.max(a, b)
63 :     | (Op.Min, [a, b]) => T.Expr.min(a, b)
64 :     | (Op.Sin, [a]) => T.Expr.sin a
65 :     | (Op.Cos, [a]) => T.Expr.cos a
66 :     | (Op.Pow, [a, b]) => T.Expr.pow(a, b)
67 :     | (Op.Dot d, [a, b]) => T.Expr.dot(a, b)
68 :     | (Op.Cross, [a, b]) => T.Expr.cross(a, b)
69 :     | (Op.Select(ty, i), [a]) => T.Expr.select(i, a)
70 :     | (Op.Norm d, [a]) => T.Expr.length a
71 :     | (Op.Scale d, [a, b]) => T.Expr.mul(a, b)
72 :     | (Op.InvScale d, [a, b]) => T.Expr.divide(a, b)
73 : jhr 518 | (Op.CL, _) => raise Fail "CL unimplemented"
74 :     | (Op.PrincipleEvec ty, _) => raise Fail "PrincipleEvec unimplemented"
75 : jhr 514 (*
76 : jhr 518 | (Op.Subscript ty,
77 : jhr 514 *)
78 : jhr 532 | (Op.Floor d, [a]) => T.Expr.floor a
79 :     | (Op.IntToReal, [a]) => T.Expr.toReal a
80 :     | (Op.TruncToInt d, [a]) => T.Expr.truncToInt a
81 :     | (Op.RoundToInt d, [a]) => T.Expr.roundToInt a
82 :     | (Op.CeilToInt d, [a]) => T.Expr.ceilToInt a
83 :     | (Op.FloorToInt d, [a]) => T.Expr.floorToInt a
84 :     | (Op.ImageAddress, [a]) => T.Expr.imageAddr a
85 :     | (Op.LoadVoxels(rTy, n), [a]) => T.Expr.intLit 0 (* FIXME *)
86 :     | (Op.PosToImgSpace d, [v, x]) => T.Expr.intLit 0 (* FIXME *)
87 :     | (Op.GradToWorldSpace d, [v, x]) => T.Expr.intLit 0 (* FIXME *)
88 : jhr 533 | (Op.LoadImage info, [a]) => raise Fail "impossible"
89 :     | (Op.Inside d, [v, x]) => raise Fail "impossible"
90 :     | (Op.Input(ty, name), []) => raise Fail "impossible"
91 : jhr 532 | (Op.InputWithDefault(ty, name), [a]) => T.Expr.intLit 0 (* FIXME *)
92 : jhr 528 | _ => raise Fail(concat[
93 :     "incorrect number of arguments for ", Op.toString rator
94 :     ])
95 : jhr 518 (* end case *))
96 : jhr 532 (* end case *))
97 : jhr 454
98 : jhr 532 and trExps (env, exps) = List.map (fn exp => trExp(env, exp)) exps
99 : jhr 455
100 : jhr 532 fun trStmt (env, stm) = (case stm
101 : jhr 533 of IL.S_Comment text => [T.Stmt.comment text]
102 : jhr 532 (* FIXME: special case for when x is a strand-state variable *)
103 :     | IL.S_Assign(x, exp) => (case V.kind x
104 : jhr 533 of IL.VK_Global => [T.Stmt.assign(lookup(env, x), trExp(env, exp))]
105 : jhr 532 | IL.VK_State strand => raise Fail "FIXME: state var"
106 : jhr 533 | IL.VK_Local => [T.Stmt.assign(lookup(env, x), trExp(env, exp))]
107 : jhr 512 (* end case *))
108 : jhr 533 | IL.S_Cons(lhs, args) =>
109 :     [T.Stmt.cons(lookup(env, lhs), trExps(env, args))]
110 :     | IL.S_LoadImage(lhs, dim, name) =>
111 :     T.Stmt.loadImage (lookup(env, lhs), dim, trExp(env, name))
112 :     | IL.S_Input(lhs, name, optDflt) =>
113 :     T.Stmt.input(lookup(env, lhs), name, Option.map (fn e => trExp(env, e)) optDflt)
114 : jhr 532 | IL.S_IfThen(cond, thenBlk) =>
115 : jhr 533 [T.Stmt.ifthen(trExp(env, cond), trBlock(env, thenBlk))]
116 : jhr 532 | IL.S_IfThenElse(cond, thenBlk, elseBlk) =>
117 : jhr 533 [T.Stmt.ifthenelse(trExp(env, cond),
118 : jhr 532 trBlock(env, thenBlk),
119 : jhr 533 trBlock(env, elseBlk))]
120 :     | IL.S_Die => [T.Stmt.die()]
121 :     | IL.S_Stabilize => [T.Stmt.stabilize()]
122 : jhr 532 (* end case *))
123 : jhr 512
124 : jhr 532 and trBlock (env, IL.Block{locals, body}) = (
125 :     (* what about the locals?? *)
126 : jhr 533 T.Stmt.block(List.foldr (fn (stm, stms) => trStmt(env, stm)@stms) [] body))
127 : jhr 528
128 : jhr 531 fun generate (fileStem, srcProg) = let
129 : jhr 535 val treeProg as TreeIL.Program{globals, globalInit, strands} = LowToTree.translate srcProg
130 :     val _ = (
131 :     TextIO.output(Log.logFile(), "********** After translation to TreeIL **********\n");
132 :     TreeILPP.program (Log.logFile(), treeProg))
133 : jhr 527 val prog = T.newProgram ()
134 : jhr 532 (* define the globals and initialize the environment *)
135 :     val env = let
136 :     fun gvar (x, env) =
137 :     V.Map.insert(env, x, T.Var.global(prog, cvtTy(V.ty x), V.name x))
138 :     in
139 :     List.foldl gvar V.Map.empty globals
140 :     end
141 : jhr 527 in
142 : jhr 533 (* global initialization *)
143 :     T.globalInit (prog, trBlock (env, globalInit));
144 : jhr 528 (* output the program *)
145 : jhr 527 T.generate (fileStem, prog)
146 :     end
147 : jhr 518
148 : jhr 454 end

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