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 3834 - (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 :     (* the translated bindings of LowIR variables. If the variable has a vector type,
12 :     * then it is bound to a vector of expression trees, otherwise it is bound to a
13 :     * single tree.
14 :     *)
15 :     datatype binding
16 :     = TREE of TreeIR.exp (* variable bound to a target expression tree *)
17 :     | VEC of TreeTypes.vec_layout * TreeIR.exp list
18 :    
19 :     type t
20 :    
21 :     (* create a new environment *)
22 :     val new : unit -> t
23 :    
24 :     (* get the binding for a variable *)
25 :     val useVar : t -> LowIR.var -> binding
26 :    
27 :     (* add a binding to the environment *)
28 :     val bindVar : t * LowIR.var * binding -> unit
29 :    
30 :     (* record a local variable *)
31 :     val addLocal : t * TreeVar.t -> t
32 :    
33 :     (* close off a scope by turning the stms and locals into a TreeIR block *)
34 :     val endScope : t * TreeIR.stm list -> TreeIR.block
35 :    
36 :     end = struct
37 :    
38 :     structure VT = LowIR.Var.Tbl
39 :    
40 :     datatype binding
41 :     = TREE of TreeIR.exp
42 :     | VEC of TreeTypes.vec_layout * TreeIR.exp list
43 :    
44 :     datatype t = E of {
45 :     tbl : binding VT.hash_table,
46 :     locals : TreeVar.t list
47 :     }
48 :    
49 :     fun decCount (LowIR.V{useCnt, ...}) = let
50 :     val n = !useCnt - 1
51 :     in
52 :     useCnt := n; (n <= 0)
53 :     end
54 :    
55 :     fun new () = E{tbl = VT.mkTable (256, Fail "tbl"), locals=[]}
56 :    
57 :     (* use a variable. If this is its last use, we remove it from the table *)
58 :     fun useVar (env as E{tbl, ...}) = let
59 :     val find = VT.find tbl
60 :     val remove = VT.remove tbl
61 :     fun use x = let
62 :     fun removeUnused () = if (decCount x) then ignore(VT.remove tbl x) else ()
63 :     in
64 :     case UnifyVars.eqClassOf x
65 :     of SOME y => (case find y
66 :     of SOME binding => binding
67 :     | NONE => raise Fail(concat ["useVar(", LowIR.Var.toString x, ")"])
68 :     (* end case *))
69 :     | NONE => (case find x
70 :     of SOME binding => (removeUnused(); binding)
71 :     | NONE => raise Fail(concat ["useVar(", LowIR.Var.toString x, ")"])
72 :     (* end case *))
73 :     (* end case *)
74 :     end
75 :     in
76 :     use
77 :     end
78 :    
79 :     fun bindVar (E{tbl, ...}, x, b) = VT.insert tbl (x, b)
80 :    
81 :     fun addLocal (E{tbl, locals}, x) = E{tbl=tbl, locals=x::locals}
82 :    
83 :     fun endScope (E{locals, ...}, stms) = TreeIR.Block{
84 :     locals = List.rev locals,
85 :     body = stms
86 :     }
87 :    
88 :     end

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