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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/low-to-tree/env.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/low-to-tree/env.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3851 - (view) (download)

1 : jhr 3834 (* env.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2016 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     structure Env : sig
10 :    
11 : jhr 3851 type target_info = {
12 :     layout : int -> TreeTypes.vec_layout,
13 :     isInline : TreeOps.rator -> bool
14 :     }
15 :    
16 : jhr 3834 (* the translated bindings of LowIR variables. If the variable has a vector type,
17 :     * then it is bound to a vector of expression trees, otherwise it is bound to a
18 :     * single tree.
19 :     *)
20 :     datatype binding
21 :     = TREE of TreeIR.exp (* variable bound to a target expression tree *)
22 :     | VEC of TreeTypes.vec_layout * TreeIR.exp list
23 :    
24 :     type t
25 :    
26 :     (* create a new environment *)
27 : jhr 3851 val new : target_info -> t
28 : jhr 3834
29 : jhr 3843 (* return the layout of a Low IR vector of a given width as a Tree IR composite vector *)
30 :     val layoutVec : t -> int -> TreeTypes.vec_layout
31 :    
32 : jhr 3851 val isInlineOp : t -> TreeOps.rator -> bool
33 :    
34 : jhr 3834 (* get the binding for a variable *)
35 :     val useVar : t -> LowIR.var -> binding
36 :    
37 :     (* add a binding to the environment *)
38 : jhr 3851 val bindVar : t * LowIR.var * binding -> unit
39 : jhr 3834
40 : jhr 3836 (* set the definition of a variable, where the RHS is either a literal constant or a variable *)
41 :     val bindSimple : t * LowIR.var * TreeIR.exp -> unit
42 :    
43 :     (* at the end of a block, we need to assign any pending expressions to locals. The
44 :     * blkStms list and the resulting statement list are in reverse order.
45 :     *)
46 : jhr 3851 val flushPending : t * TreeIR.stm list -> TreeIR.stm list
47 : jhr 3836
48 : jhr 3834 end = struct
49 :    
50 :     structure VT = LowIR.Var.Tbl
51 :    
52 : jhr 3851 type target_info = {
53 :     layout : int -> TreeTypes.vec_layout,
54 :     isInline : TreeOps.rator -> bool
55 :     }
56 :    
57 : jhr 3834 datatype binding
58 :     = TREE of TreeIR.exp
59 :     | VEC of TreeTypes.vec_layout * TreeIR.exp list
60 :    
61 :     datatype t = E of {
62 : jhr 3836 tbl : (bool * binding) VT.hash_table,
63 : jhr 3851 info : target_info
64 : jhr 3834 }
65 :    
66 :     fun decCount (LowIR.V{useCnt, ...}) = let
67 :     val n = !useCnt - 1
68 :     in
69 :     useCnt := n; (n <= 0)
70 :     end
71 :    
72 : jhr 3851 fun new info = E{
73 : jhr 3843 tbl = VT.mkTable (256, Fail "tbl"),
74 : jhr 3851 info = info
75 : jhr 3843 }
76 : jhr 3834
77 : jhr 3851 fun layoutVec (E{info={layout, ...}, ...}) wid = layout wid
78 : jhr 3843
79 : jhr 3851 fun isInlineOp (E{info={isInline, ...}, ...}) rator = isInline rator
80 :    
81 : jhr 3834 (* use a variable. If this is its last use, we remove it from the table *)
82 :     fun useVar (env as E{tbl, ...}) = let
83 :     val find = VT.find tbl
84 :     val remove = VT.remove tbl
85 :     fun use x = let
86 :     fun removeUnused () = if (decCount x) then ignore(VT.remove tbl x) else ()
87 :     in
88 :     case UnifyVars.eqClassOf x
89 :     of SOME y => (case find y
90 : jhr 3836 of SOME(isSimple, binding) => binding
91 : jhr 3834 | NONE => raise Fail(concat ["useVar(", LowIR.Var.toString x, ")"])
92 :     (* end case *))
93 :     | NONE => (case find x
94 : jhr 3836 of SOME(true, binding) => (removeUnused(); binding)
95 :     | SOME(false, binding) => (removeUnused(); binding)
96 : jhr 3834 | NONE => raise Fail(concat ["useVar(", LowIR.Var.toString x, ")"])
97 :     (* end case *))
98 :     (* end case *)
99 :     end
100 :     in
101 :     use
102 :     end
103 :    
104 : jhr 3851 fun bindVar (E{tbl, ...}, x, b) = VT.insert tbl (x, (false, b))
105 : jhr 3834
106 : jhr 3836 fun bindSimple (E{tbl, ...}, x, b) = VT.insert tbl (x, (true, TREE b))
107 :    
108 : jhr 3851 fun flushPending (E{tbl, ...}, blkStms) = let
109 : jhr 3848 fun doVar (x, (false, TREE e), stms) = let
110 : jhr 3842 val t = Util.newLocalVar x
111 : jhr 3836 in
112 :     VT.insert tbl (x, (true, TREE(TreeIR.E_Var t)));
113 : jhr 3848 TreeIR.S_Assign(t, e)::stms
114 : jhr 3836 end
115 : jhr 3848 | doVar (x, (false, VEC(layout, es)), stms) = raise Fail "FIXME"
116 : jhr 3836 | doVar (_, _, acc) = acc
117 : jhr 3848 val stms = VT.foldi doVar blkStms tbl
118 : jhr 3836 in
119 : jhr 3851 stms
120 : jhr 3836 end
121 :    
122 : jhr 3834 end

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