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

SCM Repository

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

Annotation of /trunk/src/compiler/IL/translate-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 360 - (view) (download)

1 : jhr 359 (* translate-fn.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * This functor supports the common parts of translating between different
7 :     * instances of the SSA-based ILs (e.g., from HighIL to MidIL).
8 :     *)
9 :    
10 :     signature TRANSLATE_PARAMS =
11 :     sig
12 :    
13 :     structure SrcIL : SSA
14 :     structure DstIL : SSA
15 :    
16 :     type env = DstIL.var SrcIL.Var.Tbl.hash_table
17 :    
18 :     val rename : (env * SrcIL.var) -> DstIL.var
19 :     val expand : (env * SrcIL.assign) -> DstIL.assign list
20 :    
21 :     end
22 :    
23 :     functor Translate (Params : TRANSLATE_PARAMS) : sig
24 :    
25 :     structure SrcIL : SSA
26 :     structure DstIL : SSA
27 :    
28 :     val translate : env * SrcIL.stmt -> DstIL.stmt
29 :    
30 :     end = struct
31 :    
32 :     structure SrcIL : SSA = Params.SrcIL
33 :     structure SrcNd = SrcIL.Node
34 :     structure DstIL : SSA = Params.DstIL
35 :    
36 :     datatype env = E of {
37 :     ndMap : DstIL.node Stamp.Tbl.hash_table,
38 :     vMap : Params.env
39 :     }
40 :    
41 :     fun rename (E{vMap, ...}, x) = Params.rename(vMap, x)
42 :    
43 : jhr 360 fun expand (E{vMap, ...}) (assign, assigns') =
44 :     Params.expand (vMap, assign) @ assigns'
45 : jhr 359
46 :     (* the first pass creates the nodes of the DstIL CFG and defines
47 :     * the environment that maps from SrcIL nodes and variables to
48 :     * DstIL nodes and variables.
49 :     *)
50 :     fun translateNodes (env, stm) = let
51 : jhr 360 fun trans (SrcIL.ND{id, kind, ...}) = let
52 : jhr 359 val newNd = (case kind
53 : jhr 360 of SrcIL.NULL => raise Fail "unexpected NULL node"
54 :     | SrcIL.ENTRY _ => DstNd.mkENTRY()
55 :     | SrcIL.JOIN{phis, ...} => let
56 :     fun cvtPhi (x, xs) =
57 :     (rename(env, x), List.map (fn x => rename(env, x)) xs)
58 :     in
59 :     DstNd.mkJOIN(List.map cvtPhi phis)
60 :     end
61 :     | SrcIL.COND{cond, trueBranch, falseBranch, ...} => DstNd.mkCOND{
62 :     cond = rename(env, cond),
63 :     trueBranch = DstNd.dummy,
64 :     trueBranch = DstNd.dummy
65 :     }
66 :     | SrcIL.BLOCK{body, ...} => let
67 :     val body' = List.foldr (expand env) [] (!body)
68 :     in
69 :     DstNd.mkBLOCK body'
70 :     end
71 :     | SrcIL.NEW{actor, args, ...} => DstNd.mkNEW{
72 :     actor = actor,
73 :     args = List.map (fn x => rename(env, x)) args
74 :     }
75 :     | SrcIL.DIE _ => DstNd.mkDIE()
76 :     | SrcIL.STABILIZE _ => DstNd.mkSTABILIZE()
77 :     | SrcIL.EXIT _ => DstNd.mkEXIT()
78 : jhr 359 (* end case *))
79 : jhr 360 in
80 :     insertNd (env, id, newNd)
81 :     end
82 : jhr 359 in
83 : jhr 360 SrcIL.applyToNodes trans stm
84 : jhr 359 end
85 :    
86 : jhr 360 fun translate (env, stm) = (case kind
87 : jhr 359 of SrcIL.S_SIMPLE(SrcIL.ND{kind, ...}) => (case kind
88 :     of NULL =>
89 :     | ENTRY{succ} =>
90 :     | JOIN{preds, phis, succ} =>
91 :     | COND{pred, cond, trueBranch, falseBranch} =>
92 :     raise Fail "impossible"
93 :     | BLOCK{pred, body, succ} =>
94 :     | NEW{pred, actor, args, succ} =>
95 :     | DIE{pred, ...} =>
96 :     | STABILIZE{pred} =>
97 :     | EXIT{pred} =>
98 :     (* end case *))
99 :     | SrcIL.S_IF{cond, thenBranch, elseBranch} =>
100 :     | SrcIL.S_LOOP{hdr, cond, body} =>
101 :     (* end case *))
102 :    
103 :     and translateOpt (env, NONE) = NONE
104 :     | translateOpt (env, SOME stm) = translate (env, stm)
105 :    
106 :     end

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