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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3696 - (view) (download)

1 : jhr 1115 (* tree-il.sml
2 :     *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 1115 * All rights reserved.
7 :     *
8 :     * This representation restores the block structure and nested expression syntax
9 :     * of the source language.
10 :     *)
11 :    
12 :     structure TreeIL =
13 :     struct
14 :    
15 : cchiw 2615 structure Op = TreeOps
16 :     structure Ty = TreeILTypes
17 : jhr 3060 structure Fnc = TreeFunc
18 :     structure OprSet = Fnc.OprSet
19 :     structure TySet = Fnc.TySet
20 : jhr 1115
21 :     datatype program = Program of {
22 : jhr 2356 props : StrandUtil.program_prop list,
23 : cchiw 3692 types : Ty.ty list,
24 :     operations : Fnc.oprator list,
25 : jhr 2356 globals : var list,
26 :     inputInit : block,
27 :     globalInit : block,
28 :     strands : strand list,
29 :     initially : {
30 :     isArray : bool,
31 :     iterPrefix : block,
32 :     iters : (var * exp * exp) list,
33 :     createPrefix : block,
34 :     strand : Atom.atom,
35 :     args : exp list
36 :     }
37 : jhr 1115 }
38 :    
39 :     and strand = Strand of {
40 : jhr 2356 name : Atom.atom,
41 :     params : var list,
42 :     state : state_var list,
43 :     stateInit : block,
44 :     methods : method list
45 : jhr 1115 }
46 :    
47 : jhr 1640 and state_var = SV of {
48 : jhr 2356 name : string, (* name (should be unique) *)
49 :     id : Stamp.stamp, (* unique ID *)
50 :     ty : Ty.ty, (* type *)
51 :     varying : bool, (* varies over the lifetime of the strand *)
52 :     output : bool (* is the output value of the strand *)
53 : jhr 1115 }
54 :    
55 :     and method = Method of {
56 : jhr 2356 name : StrandUtil.method_name,
57 :     body : block (* method body *)
58 : jhr 1115 }
59 :    
60 :     and block = Block of {
61 : jhr 2356 locals : var list,
62 :     body : stm list
63 : jhr 3060 }
64 : jhr 1115
65 :     and stm
66 :     = S_Comment of string list
67 : jhr 1640 | S_Assign of var list * exp
68 : jhr 1115 | S_IfThen of exp * block
69 :     | S_IfThenElse of exp * block * block
70 :     (* special Diderot forms *)
71 : jhr 3060 | S_LoadNrrd of var * Ty.ty * string
72 :     | S_Input of var * string * string option * exp option (* get input *)
73 :     | S_InputNrrd of var * string * string option * string option (* get image/seq input *)
74 : jhr 2356 | S_New of Atom.atom * exp list (* new strand creation *)
75 : jhr 1640 | S_Save of state_var list * exp (* save strand state *)
76 : cchiw 2827 | S_Copy of exp * exp * int*int (* copy var from exp,offset and copy-sized array*)
77 : jhr 1115 | S_Exit of exp list
78 :     (* return functions for methods *)
79 : jhr 1640 | S_Active
80 :     | S_Stabilize
81 : jhr 1115 | S_Die
82 : jhr 3169 (* FIXME: use a record type for this constructor *)
83 : cchiw 3544 | S_StoreVec of exp * int * bool * bool * int * Ty.ty * int list * exp list (*Vector->Array*)
84 : cchiw 2844 (*var,offset, isAligned, isFill,origSize of lastVector,original Tensor ty,pieces, args*)
85 : jhr 3169 | S_Cons of var * int * exp list (*real ->Array*)
86 : cchiw 2844 (* var,size n, exp arguments*)
87 : jhr 3060
88 : jhr 1115 and exp
89 : jhr 1640 = E_State of state_var
90 :     | E_Var of var
91 : jhr 1115 | E_Lit of Literal.literal
92 : cchiw 2624 | E_Op of Op.rator * exp list
93 : jhr 1923 | E_Apply of MathFuns.name * exp list
94 : jhr 3169 | E_Cons of int * int * exp list (*Reals->Vector size n, origSize, exp arguments *)
95 :     | E_LoadArr of bool * int * int * exp * exp
96 : cchiw 2681 (*aligned, newVecSize,originalVecSize, addr, offset *)
97 : cchiw 3544 | E_Mux of bool * bool * int * int list * exp list
98 :     (*alighed, isFill,originalsize of vectorop, DstTy.vectorLength pieces, args*)
99 : cchiw 2795 (*wraps around vector operations*)
100 : cchiw 2789 | E_Holder of var * var list (*array, to vector var list , used for matrices*)
101 : cchiw 2695
102 :    
103 : cchiw 2687 (*When occurance >1 local variables are created, and the expression list are vars. Then other operators call the vars*)
104 :     (*otherwise use entire expression.*)
105 : cchiw 2681
106 : cchiw 2646
107 : jhr 1115 and var = V of {
108 : jhr 2356 name : string, (* name (should be unique) *)
109 :     id : Stamp.stamp, (* unique ID *)
110 :     kind : var_kind,
111 : cchiw 2789 rTy : Ty.ty, (* register type; it is the LowIL type mapped to the target machine vector register model *)
112 :     mTy : Ty.ty (* in memory (aka true) type of the variable; it corresponds to the LowIL type *)
113 : jhr 1115 }
114 :    
115 :     and var_kind
116 : jhr 3060 = VK_Input (* global input variable *)
117 :     | VK_Global (* global variable *)
118 : jhr 2356 | VK_Local (* includes strand parameters *)
119 : jhr 1115
120 : jhr 3060 structure Var : sig
121 : cchiw 2637
122 : jhr 3060 val new : string * var_kind * Ty.ty -> var
123 :     val newAlter : string * var_kind * Ty.ty * Ty.ty -> var
124 : jhr 2356 val kind : var -> var_kind
125 :     val name : var -> string
126 :     val toString : var -> string
127 :     val ty : var -> Ty.ty
128 : cchiw 2789 val mTy : var -> Ty.ty
129 :     val rTy : var -> Ty.ty
130 : jhr 1115
131 : jhr 2356 structure Map : ORD_MAP where type Key.ord_key = var
132 : jhr 1115
133 :     end = struct
134 : jhr 2356 fun new (name, kind, ty) = V{
135 :     name = name,
136 :     id = Stamp.new(),
137 :     kind = kind,
138 : cchiw 2789 rTy = ty,
139 :     mTy = ty
140 : jhr 2356 }
141 : cchiw 2789
142 :     fun newAlter (name, kind, xty,yty) = V{
143 :     name = name,
144 :     id = Stamp.new(),
145 :     kind = kind,
146 :     rTy = xty,
147 :     mTy = yty
148 :     }
149 : jhr 2356 fun kind (V{kind, ...}) = kind
150 :     fun name (V{name, ...}) = name
151 :     fun toString (V{name, id, ...}) = concat[name, "$", Stamp.toString id]
152 : cchiw 2789 fun ty (V{mTy, ...}) = mTy
153 :     fun rTy (V{rTy, ...}) = rTy
154 :     fun mTy (V{mTy, ...}) = mTy
155 : jhr 2356 local
156 :     structure VarOrd =
157 :     struct
158 :     type ord_key = var
159 :     fun compare (V{id=a, ...}, V{id=b, ...}) = Stamp.compare(a, b)
160 :     end
161 :     in
162 :     structure Map = RedBlackMapFn (VarOrd)
163 :     end (* local *)
164 : jhr 1115 end
165 :    
166 : jhr 1640 structure StateVar : sig
167 :     val name : state_var -> string
168 :     val ty : state_var -> Ty.ty
169 :     val isOutput : state_var -> bool
170 :     val isVarying : state_var -> bool
171 :     val toString : state_var -> string
172 :     end = struct
173 : jhr 2356 fun name (SV{name, ...}) = name
174 :     fun ty (SV{ty, ...}) = ty
175 :     fun isOutput (SV{output, ...}) = output
176 :     fun isVarying (SV{varying, ...}) = varying
177 :     fun toString (SV{name, ...}) = "self." ^ name
178 : jhr 1640 end
179 : jhr 1131
180 :     fun kindToString k = (case k
181 : jhr 3060 of VK_Input => "Input"
182 :     | VK_Global => "Global"
183 : jhr 2356 | VK_Local => "Local"
184 :     (* end case *))
185 : jhr 1131
186 : jhr 1640 fun stateVarToString (SV{name, ...}) = "self." ^ name
187 :    
188 : cchiw 2637
189 : cchiw 2845 fun iTos m=Int.toString m
190 : cchiw 2637
191 : cchiw 2789 fun varTys(x,y)=if Ty.same(x,y) then String.concat[" TySame: ",Ty.toString x," "]
192 :     else String.concat["\t rTy: ",Ty.toString x," \t mty: ",Ty.toString y," "]
193 : cchiw 3444
194 :     fun varToString x=String.concat([" Var: ",Var.name x , " Kind: ",kindToString (Var.kind x),varTys(Var.rTy x,Var.mTy x)])
195 : cchiw 3543
196 : cchiw 2845 fun preBool(pre, true)=pre
197 :     | preBool(_,false)=""
198 : cchiw 2795
199 : cchiw 3544 fun intlistToString tys = String.concat["[", String.concatWith "," (List.map Int.toString tys), "]" ]
200 : cchiw 2691 fun toString(E_LoadArr(_,nSize,oSize, arg, offset)) = String.concat[" LDArray<",iTos nSize ,
201 : cchiw 2838 ":",iTos oSize, ">", "( ", toString arg, ",offset", toString offset, ")"]
202 : cchiw 2845 | toString (E_Op(rator, args)) = String.concat["OP_",Op.toString rator,"\n\t(",
203 : cchiw 2844 String.concatWith",\n\t" (List.map toString args),")"]
204 : cchiw 2845 | toString (E_Cons(nSize, oSize,args)) =String.concat ["ConsExp:", Int.toString nSize,Int.toString oSize, "(",
205 : cchiw 2844 String.concatWith"," (List.map toString args),")"]
206 : cchiw 3197 | toString (E_State x) = String.concat[(*"Exp:STATEVar",*)stateVarToString x]
207 :     | toString (E_Var x) = String.concat[(*"Exp:REGULARVAR",*)varToString x]
208 : cchiw 2845 | toString (E_Lit lit) = Literal.toString lit
209 :     | toString(E_Apply(mathfn, args))=String.concat["Apply (",
210 : cchiw 2844 String.concatWith"," (List.map toString args),")"]
211 : jhr 3172 | toString(E_Mux(_,isFill,oSize,pieces,args)) = String.concat[
212 :     "Mux",preBool("isFilled", isFill),"|",iTos oSize,"@ [",
213 : cchiw 3544 intlistToString pieces ,"]", String.concatWith "," (List.map toString args)
214 : jhr 3172 ]
215 : cchiw 2845 | toString(E_Holder(v,vs))= String.concat(["{Holder_for_ArrVar : ",Var.name v, " Rest :"]@ (List.map Var.name vs)@["}"])
216 : cchiw 2844 fun toStrings e=String.concat(List.map toString e)
217 : cchiw 2795
218 : cchiw 3544
219 : jhr 3060 fun toStringS (S_Assign( [v],e))=String.concat["Assignments : ",Var.name v,"=",toString e]
220 : cchiw 3444 | toStringS (S_Assign( [],e))=String.concat ["Assignments : none =",toString e]
221 : cchiw 3543 | toStringS (S_Assign( vs,e))=String.concat ["Assignments : -multiple =",String.concatWith"," (List.map Var.name vs),"-",toString e]
222 : jhr 3060 | toStringS (S_Comment _ )=String.concat["Comment"]
223 :     | toStringS (S_IfThen (e,b) )=String.concat["{ifthen_"(*,toString e*),"}"]
224 :     | toStringS (S_IfThenElse (e,b1,b2) )=String.concat["{ifthenelse_",(*toString e,*)"}"]
225 : jhr 3172 | toStringS (S_Input _ ) = String.concat["input"]
226 : jhr 3060 | toStringS (S_New _ )=String.concat["new"]
227 :     | toStringS (S_Save ([x],e) )=String.concat["savestmt: x",stateVarToString x," e-", toString e,")"]
228 :     | toStringS (S_Save (xs,e) )=String.concat["savestmt:xs- e-", toString e,")"]
229 :     | toStringS (S_Copy( _ ,e, offset,n ) )=String.concat["Copy:\n\t(", toString e,") " ,iTos n ,"_offset_",iTos offset ]
230 :     | toStringS (S_Exit _ )=String.concat["exit"]
231 :     | toStringS (S_Active )=String.concat["active"]
232 :     | toStringS (S_Stabilize )=String.concat["stablize"]
233 :     | toStringS (S_Die )=String.concat["die"]
234 :     | toStringS (S_StoreVec(x,offset,A,f, tyOrig,ty,pieces,args)) =let
235 : cchiw 2680 val a= String.concatWith "," (List.map toString args)
236 :     in
237 : cchiw 2827 String.concat["STMT ",toString x, "= STORE",preBool("_aligned_",A),preBool("_filled_",f),"_offset_", iTos offset,"|",iTos tyOrig,Ty.toString ty,"@ [",
238 : cchiw 3544 intlistToString pieces ,"]","Args",iTos(length args),a]
239 : cchiw 2680 end
240 : cchiw 2845 | toStringS (S_Cons(x,oSize, args)) = String.concat ["ConsStmt:", Var.name x,kindToString(Var.kind x), "=",iTos oSize, "(\n\t",
241 : jhr 3060 String.concatWith",\n\t" (List.map toString args),")"]
242 :     | toStringS _ = "HELP"
243 : cchiw 2692
244 : cchiw 2827 fun toStringSs e=String.concatWith",\n"(List.map toStringS e)
245 : cchiw 2668
246 : jhr 3172 end

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