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

SCM Repository

[diderot] Annotation of /branches/charisee/src/compiler/simplify/inliner.sml
ViewVC logotype

Annotation of /branches/charisee/src/compiler/simplify/inliner.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 2356 (* inliner.sml
2 :     *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 2356 * All rights reserved.
7 :     *
8 :     * This pass eliminates the function definitions by inlining them.
9 :     *)
10 :    
11 :     structure Inliner : sig
12 :    
13 :     val transform : Simple.program -> Simple.program
14 :    
15 :     end = struct
16 :    
17 :     structure S = Simple
18 : jhr 2490 structure V = SimpleVar
19 : jhr 2356
20 :     (* beta reduce the application "lhs = f(args)" by creating a fresh copy of f's body
21 :     * while mapping the parameters to arguments.
22 :     *)
23 :     fun beta (lhs, S.Func{f, params, body}, args) = let
24 : jhr 3060 val needsLHSPreDecl = ref false (* set to true if the lhs needs to be declared before the body *)
25 :     fun rename env x = (case V.Map.find(env, x)
26 :     of SOME x' => x'
27 :     | NONE => if SimpleVar.isGlobal x
28 :     then x
29 :     else raise Fail("unknown variable " ^ V.uniqueNameOf x)
30 :     (* end case *))
31 : jhr 2604 fun doBlock (env, isTop, S.Block stms) = let
32 : jhr 2356 fun f (stm, (env, stms)) = let
33 : jhr 2604 val (env, stm) = doStmt (env, isTop, stm)
34 : jhr 2356 in
35 :     (env, stm::stms)
36 :     end
37 :     val (_, stms) = List.foldl f (env, []) stms
38 :     in
39 :     S.Block(List.rev stms)
40 :     end
41 : jhr 2604 and doStmt (env, isTop, stm) = (case stm
42 : jhr 2356 of S.S_Var x => let
43 : jhr 3060 val x' = V.copy x
44 :     in
45 :     (V.Map.insert(env, x, x'), S.S_Var x')
46 :     end
47 : jhr 2356 | S.S_Assign(x, e) => (case V.Map.find(env, x)
48 : jhr 3060 of SOME x' => (env, S.S_Assign(x', doExp env e))
49 :     | NONE => let
50 :     val x' = V.copy x
51 :     in
52 :     (V.Map.insert(env, x, x'), S.S_Assign(x', doExp env e))
53 :     end
54 :     (* end case *))
55 : jhr 2356 | S.S_IfThenElse(x, b1, b2) =>
56 : jhr 3060 (env, S.S_IfThenElse(rename env x, doBlock(env, false, b1), doBlock(env, false, b2)))
57 : jhr 2356 | S.S_New(strnd, xs) => (env, S.S_New(strnd, List.map (rename env) xs))
58 :     | S.S_Die => (env, stm)
59 :     | S.S_Stabilize => (env, stm)
60 : jhr 2604 | S.S_Return x => (
61 : jhr 3060 if not isTop then needsLHSPreDecl := true else ();
62 :     (env, S.S_Assign(lhs, S.E_Var(rename env x))))
63 : jhr 2356 | S.S_Print xs => (env, S.S_Print(List.map (rename env) xs))
64 :     (* end case *))
65 :     and doExp env exp = (case exp
66 :     of S.E_Var x => S.E_Var(rename env x)
67 :     | S.E_Lit _ => exp
68 :     | S.E_Tuple xs => S.E_Tuple(List.map (rename env) xs)
69 : jhr 2490 | S.E_Apply(f, xs, ty) => S.E_Apply(f, List.map (rename env) xs, ty)
70 :     | S.E_Prim(f, tys, xs, ty) =>
71 : jhr 3060 S.E_Prim(f, tys, List.map (rename env) xs, ty)
72 : jhr 2356 | S.E_Cons xs => S.E_Cons(List.map (rename env) xs)
73 : jhr 3060 | S.E_Seq xs => S.E_Seq(List.map (rename env) xs)
74 : jhr 2356 | S.E_Slice(x, xs, ty) =>
75 : jhr 3060 S.E_Slice(rename env x, List.map (Option.map (rename env)) xs, ty)
76 : jhr 2356 | S.E_Coerce{srcTy, dstTy, x} =>
77 : jhr 3060 S.E_Coerce{srcTy=srcTy, dstTy=dstTy, x=rename env x}
78 : jhr 2356 | S.E_LoadImage _ => exp
79 :     (* end case *))
80 : jhr 3060 (* build the initial environment by mapping parameters to arguments *)
81 :     val env = ListPair.foldlEq
82 :     (fn (x, x', env) => V.Map.insert(env, x, x'))
83 :     V.Map.empty (params, args)
84 :     val blk as S.Block stms = doBlock (env, true, body)
85 : jhr 2356 in
86 : jhr 3060 if !needsLHSPreDecl
87 :     then S.Block(S.S_Var lhs :: stms)
88 :     else blk
89 : jhr 2356 end
90 :    
91 :     (* inline expand user-function calls in a block *)
92 :     fun expandBlock funcTbl = let
93 : jhr 3060 val findFunc = V.Tbl.find funcTbl
94 :     fun expandBlk (S.Block stms) =
95 :     S.Block(List.foldr expandStm [] stms)
96 :     and expandStm (stm, stms') = (case stm
97 :     of S.S_Assign(x, S.E_Apply(f, xs, _)) => (case findFunc f
98 :     of NONE => stm :: stms'
99 :     | SOME func => let
100 :     val S.Block stms = beta(x, func, xs)
101 :     in
102 :     stms @ stms'
103 :     end
104 :     (* end case *))
105 :     | S.S_IfThenElse(x, b1, b2) =>
106 :     S.S_IfThenElse(x, expandBlk b1, expandBlk b2) :: stms'
107 :     | _ => stm :: stms'
108 :     (* end case *))
109 :     in
110 :     expandBlk
111 :     end
112 : jhr 2356
113 :     fun expandFunc funcTbl (S.Func{f, params, body}) = let
114 : jhr 3060 val body' = expandBlock funcTbl body
115 :     val func' = S.Func{f=f, params=params, body=body'}
116 :     in
117 :     V.Tbl.insert funcTbl (f, func')
118 :     end
119 : jhr 2356
120 :     fun expandStrand funcTbl = let
121 : jhr 3060 val expandBlock = expandBlock funcTbl
122 :     fun expandMeth (S.Method(m, blk)) = S.Method(m, expandBlock blk)
123 :     fun expand (S.Strand{name, params, state, stateInit, methods}) = S.Strand{
124 :     name = name,
125 :     params = params,
126 :     state = state,
127 :     stateInit = expandBlock stateInit,
128 :     methods = List.map expandMeth methods
129 :     }
130 :     in
131 :     expand
132 :     end
133 : jhr 2356
134 :     fun expandInit funcTbl (S.Initially{isArray, rangeInit, iters, create}) = let
135 : jhr 3060 val expandBlock = expandBlock funcTbl
136 :     fun expandCreate (S.C_Create{argInit, name, args}) = S.C_Create{
137 :     argInit = expandBlock argInit,
138 :     name = name, args = args
139 :     }
140 :     in
141 :     S.Initially{
142 :     isArray = isArray,
143 :     rangeInit = expandBlock rangeInit,
144 :     iters = iters,
145 :     create = expandCreate create
146 :     }
147 :     end
148 : jhr 2356
149 :     fun transform (prog as S.Program{funcs=[], ...}) = prog
150 : jhr 3060 | transform (S.Program{props, inputs, globals, globalInit, funcs, strands, init}) = let
151 :     (* a table that maps function names to their definitions *)
152 :     val funcTbl = V.Tbl.mkTable (List.length funcs, Fail "funcTbl")
153 :     (* first we inline expand the function bodies in definition order *)
154 :     val _ = List.app (expandFunc funcTbl) funcs
155 :     val expandBlock = expandBlock funcTbl
156 :     in
157 :     S.Program{
158 :     props = props,
159 :     inputs = inputs,
160 :     globals = globals,
161 :     globalInit = expandBlock globalInit,
162 :     funcs = [],
163 :     strands = List.map (expandStrand funcTbl) strands,
164 :     init = expandInit funcTbl init
165 :     }
166 :     end
167 : jhr 2356
168 :     end

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