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

SCM Repository

[diderot] Annotation of /trunk/src/compiler/codegen/codegen-fn.sml
ViewVC logotype

Annotation of /trunk/src/compiler/codegen/codegen-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1116 - (view) (download)

1 : jhr 454 (* codegen-fn.sml
2 :     *
3 : jhr 1116 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 454 * All rights reserved.
5 : jhr 455 *
6 : jhr 1116 * Generic support for translating LowIL code to the target representation.
7 :     *
8 :     * FIXME:
9 :     * We need to run the TreeIL through a splitting pass to match the target's vector widths.
10 : jhr 454 *)
11 :    
12 :     functor CodeGenFn (T : TARGET) : sig
13 :    
14 : jhr 1116 val generate : string * LowIL.program -> unit
15 : jhr 454
16 :     end = struct
17 :    
18 : jhr 1116 structure IL = TreeIL
19 : jhr 454
20 : jhr 1116 structure LowToTree = LowToTreeFn(T)
21 : jhr 456
22 : jhr 1116 fun trMethod (strand, stateVars, env) = let
23 :     val varyingStateVars = List.mapPartial
24 :     (fn (IL.SV{varying = true, var, ...}) => SOME var | _ => NONE)
25 :     stateVars
26 :     val env = T.Env.scopeMethod (env, varyingStateVars)
27 :     fun tr (IL.Method{name, body}) = let
28 :     val body = T.Tr.block (env, body)
29 :     in
30 :     T.Strand.method (strand, Atom.toString name, body)
31 :     end
32 :     in
33 :     tr
34 :     end
35 : jhr 456
36 : jhr 1116 fun trStrand (prog, env) (IL.Strand{name, params, state, stateInit, methods}) = let
37 :     val strand = T.Strand.define(prog, name)
38 :     val env = T.Env.scopeStrand (env, List.map (fn (IL.SV{var, ...}) => var) state)
39 :     (* the output state variable *)
40 :     val outputVar = ref NONE
41 :     (* map the state variables to target state variables and extend the environment *)
42 :     val env = let
43 :     fun cvtSVar (IL.SV{output, var=x, ...}, env) = let
44 :     val x' = T.Var.state(strand, x)
45 :     in
46 :     case (output, !outputVar)
47 :     of (true, NONE) => outputVar := SOME(IL.Var.ty x, x')
48 :     | (false, _) => ()
49 :     | _ => raise Fail("multiple outputs in " ^ Atom.toString name)
50 :     (* end case *);
51 :     T.Env.bind(env, x, x')
52 :     end
53 :     in
54 :     (* note that we folr right-to-left to get the order of state variables in the
55 :     * target strand in the right order.
56 :     *)
57 :     List.foldr cvtSVar env state
58 :     end
59 :     (* define the parameters and add them to the environment *)
60 :     val params' = List.map T.Var.param params
61 :     val env = ListPair.foldlEq (fn (x, y, env) => T.Env.bind(env, x, y)) env (params, params')
62 : jhr 455 in
63 : jhr 1116 T.Strand.init (strand, params', T.Tr.block (env, stateInit));
64 :     case !outputVar
65 :     of NONE => raise Fail("no output specified for strand " ^ Atom.toString name)
66 :     | SOME(ty, x) => T.Strand.output (strand, ty, x)
67 :     (* end case *);
68 :     List.app (trMethod (strand, state, env)) methods
69 : jhr 455 end
70 : jhr 454
71 : jhr 1116 fun trInitially (env0, prog, {isArray, iterPrefix, iters, createPrefix, strand, args}) = let
72 :     val env0 = T.Env.scopeInitially env0
73 :     fun trIter ((param, lo, hi), (env, iters)) = let
74 :     val param' = T.Var.param param
75 :     val env = T.Env.bind (env, param, param')
76 :     val iter = (param', T.Tr.exp (env0, lo), T.Tr.exp (env0, hi))
77 :     in
78 :     (env, iter::iters)
79 :     end
80 :     val (env, iters) = List.foldr trIter (env0, []) iters
81 : jhr 455 in
82 : jhr 1116 T.Program.initially {
83 :     prog = prog,
84 :     isArray = isArray,
85 :     iterPrefix = T.Tr.block (env0, iterPrefix),
86 :     iters = iters,
87 :     createPrefix = T.Tr.block (env, createPrefix),
88 :     strand = strand,
89 :     args = List.map (fn e => T.Tr.exp(env, e)) args
90 :     }
91 : jhr 455 end
92 :    
93 : jhr 1116 fun generate (fileStem, srcProg) = let
94 :     val treeProg as TreeIL.Program{globals, globalInit, strands, initially} =
95 :     PhaseTimer.withTimer Timers.timeLowToTree LowToTree.translate srcProg
96 :     val _ = (
97 :     TextIO.output(Log.logFile(), "********** After translation to TreeIL **********\n");
98 :     TreeILPP.program (Log.logFile(), treeProg))
99 :     val prog = T.Program.new ()
100 :     val env = T.Env.new prog
101 :     (* define the globals and initialize the environment *)
102 :     val env = let
103 :     (* produce a list of target variables for the globals, such that each global
104 :     * has a unique name.
105 :     *)
106 :     fun genName (x, (cnt, globs)) = let
107 :     val name = IL.Var.name x
108 :     fun uniqueName (name, cnt) =
109 :     if List.exists (fn (_, y) => (name = T.Var.name y)) globs
110 :     then uniqueName(name ^ Int.toString cnt, cnt+1)
111 :     else (name, cnt)
112 :     val (name, cnt) = uniqueName (IL.Var.name x, cnt)
113 :     in
114 :     (cnt, (x, T.Var.global(prog, name, IL.Var.ty x))::globs)
115 :     end
116 :     val (_, globs) = List.foldl genName (0, []) globals
117 :     fun gvar ((x, x'), env) = T.Env.bind(env, x, x')
118 : jhr 455 in
119 : jhr 1116 List.foldl gvar (T.Env.scopeGlobal env) globs
120 : jhr 455 end
121 :     in
122 : jhr 1116 (* global initialization *)
123 :     T.Program.init (prog, T.Tr.block (T.Env.scopeGlobal env, globalInit));
124 :     (* translate strands *)
125 :     List.app (trStrand (prog, env)) strands;
126 :     (* generate the initially function *)
127 :     trInitially (env, prog, initially);
128 :     (* output the program *)
129 :     T.Program.generate (fileStem, prog)
130 : jhr 455 end
131 :    
132 : jhr 454 end

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