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

SCM Repository

[diderot] View of /branches/charisee/src/compiler/tree-il/tree-il.sml
ViewVC logotype

View of /branches/charisee/src/compiler/tree-il/tree-il.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2845 - (download) (annotate)
Fri Dec 12 06:46:23 2014 UTC (4 years, 7 months ago) by cchiw
File size: 9620 byte(s)
added norm
(* tree-il.sml
 *
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * This representation restores the block structure and nested expression syntax
 * of the source language.
 *)

structure TreeIL =
  struct

    structure Op = TreeOps
    structure Ty = TreeILTypes
    structure Fnc=TreeFunc
    structure OprSet=Fnc.OprSet
    structure TySet=Fnc.TySet


    datatype program = Program of {
        props : StrandUtil.program_prop list,

	types : Ty.ty list,
	oprations : Fnc.oprator list,

        globals : var list,
        inputInit : block,
        globalInit : block,
        strands : strand list,
        initially : {
            isArray : bool,
            iterPrefix : block,
            iters : (var * exp * exp) list,
            createPrefix : block,
            strand : Atom.atom,
            args : exp list
          }
      }

    and strand = Strand of {
        name : Atom.atom,
        params : var list,
        state : state_var list,
        stateInit : block,
        methods : method list
      }

    and state_var = SV of {
        name : string,          (* name (should be unique) *)
        id : Stamp.stamp,       (* unique ID *)
        ty : Ty.ty,             (* type *)
        varying : bool,         (* varies over the lifetime of the strand *)
        output : bool           (* is the output value of the strand *)
      }

    and method = Method of {
        name : StrandUtil.method_name,
        body : block            (* method body *)
      }

    and subblock = BlockWithOpr of {
        locals : var list,
        types:  TySet.set,
        opr:  OprSet.set,
        body : stm list
      }
    and block = Block of {
        locals : var list,
        body : stm list
    }

    and stm
      = S_Comment of string list
      | S_Assign of var list * exp
      | S_IfThen of exp * block
      | S_IfThenElse of exp * block * block
    (* special Diderot forms *)
      | S_LoadImage of var * int * exp  (* load image data *)
      | S_Input of var * string * string * exp option (* get input *)
      | S_New of Atom.atom * exp list   (* new strand creation *)
      | S_Save of state_var list * exp  (* save strand state *)
      | S_Copy of exp  * exp * int*int (* copy var from exp,offset and  copy-sized array*)
      | S_Exit of exp list
    (* return functions for methods *)
      | S_Active
      | S_Stabilize
      | S_Die
      | S_StoreVec of exp * int * bool*bool *int * Ty.ty*Ty.ty* exp list (*Vector->Array*)
            (*var,offset, isAligned, isFill,origSize of lastVector,original Tensor ty,pieces, args*)
      | S_Cons of var *int * exp list (*real ->Array*)
            (* var,size n, exp arguments*)
 
 
    and exp
      = E_State of state_var
      | E_Var of var
      | E_Lit of Literal.literal
      | E_Op of Op.rator * exp list
      | E_Apply of MathFuns.name * exp list
      | E_Cons of int*int*  exp list   (*Reals->Vector size n, origSize,exp arguments*)
      | E_LoadArr of bool*int * int * exp * exp  
        (*aligned, newVecSize,originalVecSize, addr, offset *)
      | E_Mux of bool*bool*int * Ty.ty*exp list
            (*alighed, isFill,originalsize of vectorop, pieces, args*)
            (*wraps around vector operations*)
      | E_Holder of var * var list  (*array, to vector var list , used for matrices*)
      
 
  (*When occurance >1  local variables are created, and the expression list are vars. Then other operators call the vars*)
 (*otherwise use entire expression.*)
 

    and var = V of {
        name : string,                  (* name (should be unique) *)
        id : Stamp.stamp,               (* unique ID *)
        kind : var_kind,
        rTy : Ty.ty,                   (* register type; it is the LowIL type mapped to the target machine vector register model *)
        mTy : Ty.ty                     (* in memory (aka true) type of the variable; it corresponds to the LowIL type *)
      }

    and var_kind
      = VK_Global                       (* global variable *)
      | VK_Local                        (* includes strand parameters *)


    structure Var : sig
        val new : string * var_kind * Ty.ty    -> var
        val newAlter : string * var_kind * Ty.ty * Ty.ty   -> var
        val kind : var -> var_kind
        val name : var -> string
        val toString : var -> string
        val ty : var -> Ty.ty
        val mTy : var -> Ty.ty
        val rTy : var -> Ty.ty

        structure Map : ORD_MAP where type Key.ord_key = var

      end = struct
 
        fun new (name, kind, ty) = V{
                name = name,
                id = Stamp.new(),
                kind = kind,
                rTy = ty,
                mTy = ty
              }
    
        fun newAlter (name, kind, xty,yty) = V{
            name = name,
            id = Stamp.new(),
            kind = kind,           
            rTy = xty,
            mTy = yty
        } 
        fun kind (V{kind, ...}) = kind
        fun name (V{name, ...}) = name
        fun toString (V{name, id, ...}) = concat[name, "$", Stamp.toString id]
        fun ty (V{mTy, ...}) = mTy
        fun rTy (V{rTy, ...}) = rTy
        fun mTy (V{mTy, ...}) = mTy
        local
          structure VarOrd =
            struct
              type ord_key = var
              fun compare (V{id=a, ...}, V{id=b, ...}) = Stamp.compare(a, b)
            end
        in
        structure Map = RedBlackMapFn (VarOrd)
        end (* local *)
      end

    structure StateVar : sig
        val name : state_var -> string
        val ty : state_var -> Ty.ty
        val isOutput : state_var -> bool
        val isVarying : state_var -> bool
        val toString : state_var -> string
      end = struct
        fun name (SV{name, ...}) = name
        fun ty (SV{ty, ...}) = ty
        fun isOutput (SV{output, ...}) = output
        fun isVarying (SV{varying, ...}) = varying
        fun toString (SV{name, ...}) = "self." ^ name
      end

    fun kindToString k = (case k
           of VK_Global => "Global"
            | VK_Local => "Local"
          (* end case *))

    fun stateVarToString (SV{name, ...}) = "self." ^ name

 
    fun iTos m=Int.toString m
 
    fun varTys(x,y)=if Ty.same(x,y) then String.concat[" TySame: ",Ty.toString x," "]
            else String.concat["\t rTy: ",Ty.toString x," \t mty: ",Ty.toString y," "]
 
    fun varToString x=String.concat([" Var: ",Var.name x , " Kind: ",kindToString (Var.kind x),varTys(Var.rTy x,Var.mTy x)])
 
 
    fun preBool(pre, true)=pre
      | preBool(_,false)=""
 
    fun toString(E_LoadArr(_,nSize,oSize, arg, offset)) = String.concat[" LDArray<",iTos nSize ,
            ":",iTos  oSize, ">", "( ", toString arg, ",offset", toString offset, ")"]
      | toString (E_Op(rator, args)) = String.concat["OP_",Op.toString rator,"\n\t(",
                String.concatWith",\n\t" (List.map toString args),")"]
      | toString (E_Cons(nSize, oSize,args)) =String.concat ["ConsExp:", Int.toString nSize,Int.toString oSize, "(",
            String.concatWith"," (List.map toString args),")"]
      | toString (E_State x) = String.concat["Exp:STATEVar",stateVarToString x]
      | toString (E_Var x) = String.concat["Exp:REGULARVAR",varToString x]
      | toString (E_Lit lit) = Literal.toString lit
      | toString(E_Apply(mathfn, args))=String.concat["Apply (",
            String.concatWith"," (List.map toString args),")"]
      | toString(E_Mux(_,isFill,oSize,pieces,args)) =  String.concat["Mux",preBool("isFilled", isFill),"|",iTos oSize,"@ [",
            Ty.toString pieces ,"]",(String.concatWith "," (List.map toString args))]
      | toString(E_Holder(v,vs))= String.concat(["{Holder_for_ArrVar : ",Var.name  v, " Rest :"]@ (List.map Var.name vs)@["}"])
            fun toStrings e=String.concat(List.map toString e)

    fun toStringS(S_Assign( [v],e))=String.concat["Assignments : ",Var.name v,"=",toString e]
      | toStringS(S_Assign( _,e))=String.concat ["Assignments : -multiple =",toString e]
      | toStringS(  S_Comment _ )=String.concat["Comment"]
      | toStringS(S_IfThen (e,b) )=String.concat["{ifthen_"(*,toString e*),"}"]
      | toStringS(S_IfThenElse (e,b1,b2) )=String.concat["{ifthenelse_",(*toString e,*)"}"]
      | toStringS(S_LoadImage _ )=String.concat["ldimage"]
      | toStringS( S_Input _ )=String.concat["input"]
      | toStringS( S_New _ )=String.concat["new"]
      | toStringS( S_Save ([x],e) )=String.concat["savestmt: x",stateVarToString x," e-", toString e,")"]
      | toStringS( S_Save (xs,e) )=String.concat["savestmt:xs- e-", toString e,")"]
      | toStringS( S_Copy( _ ,e, offset,n ) )=String.concat["Copy:\n\t(", toString e,") " ,iTos n ,"_offset_",iTos offset ]
      | toStringS( S_Exit _ )=String.concat["exit"]
      | toStringS(S_Active  )=String.concat["active"]
      | toStringS( S_Stabilize  )=String.concat["stablize"]
      | toStringS( S_Die  )=String.concat["die"]
      | toStringS(S_StoreVec(x,offset,A,f, tyOrig,ty,pieces,args)) =let
            val a= String.concatWith "," (List.map toString args)
            in
                String.concat["STMT ",toString x,  "= STORE",preBool("_aligned_",A),preBool("_filled_",f),"_offset_", iTos offset,"|",iTos tyOrig,Ty.toString ty,"@ [",
                Ty.toString pieces ,"]","Args",iTos(length args),a]
            end
      | toStringS (S_Cons(x,oSize, args)) = String.concat ["ConsStmt:", Var.name x,kindToString(Var.kind x), "=",iTos oSize, "(\n\t",
         String.concatWith",\n\t" (List.map toString args),")"]

    fun toStringSs e=String.concatWith",\n"(List.map toStringS e)

      end

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