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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2746 - (view) (download)

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

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