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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3861 - (download) (annotate)
Sun May 15 14:33:03 2016 UTC (3 years, 2 months ago) by jhr
File size: 3384 byte(s)
  Working on merge: added checking for TreeIR
(* env.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.
 *)

structure Env : sig

  (* the translated bindings of LowIR variables.  If the variable has a vector type,
   * then it is bound to a vector of expression trees, otherwise it is bound to a
   * single tree.
   *)
    datatype binding
      = TREE of TreeIR.exp         (* variable bound to a target expression tree *)
      | VEC of TreeTypes.vec_layout * TreeIR.exp list

    type t

  (* create a new environment *)
    val new : TreeIR.target_info -> t

  (* return the layout of a Low IR vector of a given width as a Tree IR composite vector *)
    val layoutVec : t -> int -> TreeTypes.vec_layout

    val isInlineOp : t -> LowOps.rator -> bool

  (* get the binding for a variable *)
    val useVar : t -> LowIR.var -> binding

  (* add a binding to the environment *)
    val bindVar : t * LowIR.var * binding -> unit

  (* set the definition of a variable, where the RHS is a simple expression that can be
   * replicated without code-size or performance hit (e.g., a literal or variable).
   *)
    val bindSimple : t * LowIR.var * binding -> unit

  (* at the end of a block, we need to assign any pending expressions to locals.  The
   * blkStms list and the resulting statement list are in reverse order.
   *)
    val flushPending : t * TreeIR.stm list -> TreeIR.stm list

  end = struct

    structure VT = LowIR.Var.Tbl

    datatype binding
      = TREE of TreeIR.exp
      | VEC of TreeTypes.vec_layout * TreeIR.exp list

    datatype t = E of {
	tbl : (bool * binding) VT.hash_table,
	info : TreeIR.target_info
      }

    fun decCount (LowIR.V{useCnt, ...}) = let
	  val n = !useCnt - 1
	  in
	    useCnt := n;  (n <= 0)
	  end

    fun new info = E{
	    tbl = VT.mkTable (256, Fail "tbl"),
	    info = info
	  }

    fun layoutVec (E{info={layout, ...}, ...}) wid = layout wid

    fun isInlineOp (E{info={isInline, ...}, ...}) rator = isInline rator

  (* use a variable.  If this is its last use, we remove it from the table.
   * NOTE: we assume that this function is _not_ called on variables that are
   * in an equivalence class; those are handled in low-to-tree.sml.
   *)
    fun useVar (env as E{tbl, ...}) = let
	  val find = VT.find tbl
	  val remove = VT.remove tbl
	  fun use x = let
		fun removeUnused () = if (decCount x) then ignore(VT.remove tbl x) else ()
		in
		  case find x
		   of SOME(true, binding) => (removeUnused(); binding)
		    | SOME(false, binding) => (removeUnused(); binding)
		    | NONE => raise Fail(concat ["useVar(", LowIR.Var.toString x, ")"])
		  (* end case *)
		end
	  in
	    use
	  end

    fun bindVar (E{tbl, ...}, x, b) = VT.insert tbl (x, (false, b))

    fun bindSimple (E{tbl, ...}, x, b) = VT.insert tbl (x, (true, b))

    fun flushPending (E{tbl, ...}, blkStms) = let
          fun doVar (x, (false, TREE e), stms) = let
                val t = Util.newLocalVar x
                in
                  VT.insert tbl (x, (true, TREE(TreeIR.E_Var t)));
                  TreeIR.S_Assign(true, t, e)::stms
                end
	    | doVar (x, (false, VEC(layout, es)), stms) = raise Fail "FIXME"
            | doVar (_, _, acc) = acc
          val stms = VT.foldi doVar blkStms tbl
          in
            stms
          end

  end

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