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

SCM Repository

[diderot] Annotation of /trunk/src/compiler/simplify/simple.sml
ViewVC logotype

Annotation of /trunk/src/compiler/simplify/simple.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 171 (* simple.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 171 * All rights reserved.
7 :     *
8 : jhr 511 * A simplified AST representation of a Diderot program. This representation has the property
9 :     * that the arguments to ifs, operators, etc. are variables and that the rhs of assignments
10 :     * consist of a single operation. It is not, however, a single-assignment representation.
11 : jhr 171 *)
12 :    
13 :     structure Simple =
14 :     struct
15 :    
16 : jhr 2491 (* types with the meta variables resolved *)
17 :     type ty = SimpleTypes.ty
18 :    
19 :     (* resolved meta-variable arguments to basis functions *)
20 :     datatype meta_arg = datatype SimpleTypes.meta_arg
21 :    
22 : jhr 171 datatype var_kind = datatype AST.var_kind
23 :    
24 : jhr 2491 datatype var = V of {
25 :     name : string, (* print name of variable *)
26 :     id : Stamp.stamp, (* unique ID *)
27 :     kind : var_kind, (* variable kind *)
28 : jhr 2493 ty : ty (* type *)
29 : jhr 2491 }
30 : jhr 171
31 : jhr 175 datatype program = Program of {
32 : jhr 2636 props : StrandUtil.program_prop list,
33 :     inputs : (var * SimpleTypes.ty Inputs.input) list,
34 : jhr 2356 globals : var list,
35 :     globalInit : block,
36 :     funcs : func list,
37 :     strands : strand list,
38 :     init : init (* block evaluates any vars used in init *)
39 : jhr 171 }
40 :    
41 : jhr 2356 and func = Func of {
42 :     f : var,
43 :     params : var list,
44 :     body : block
45 :     }
46 :    
47 : jhr 1116 and init = Initially of {
48 : jhr 2356 isArray : bool,
49 :     rangeInit : block,
50 :     iters : {param : var, lo : var, hi : var} list,
51 :     create : create
52 : jhr 1116 }
53 :    
54 :     and create = C_Create of {
55 : jhr 2356 argInit : block,
56 :     name : Atom.atom,
57 :     args : var list
58 : jhr 1116 }
59 :    
60 : jhr 2356 (* do we use this?
61 : jhr 1116 and iter = I_Range of {
62 : jhr 2356 rangeInit : block,
63 :     param : var,
64 :     lo : var,
65 :     hi : var
66 : jhr 1116 }
67 : jhr 2356 *)
68 : jhr 1116
69 : jhr 511 and strand = Strand of {
70 : jhr 2356 name : Atom.atom,
71 :     params : var list,
72 :     state : var list,
73 :     stateInit : block,
74 :     methods : method list
75 : jhr 171 }
76 :    
77 : jhr 1640 and method = Method of StrandUtil.method_name * block
78 : jhr 171
79 : jhr 192 and block = Block of stmt list
80 :    
81 : jhr 171 and stmt
82 : jhr 2356 = S_Var of var (* introduce an uninitialized local variable. *)
83 :     (* These stmts are needed for the results of *)
84 :     (* conditional expressions *)
85 : jhr 1116 | S_Assign of var * exp
86 : jhr 192 | S_IfThenElse of var * block * block
87 : jhr 171 | S_New of Atom.atom * var list
88 :     | S_Die
89 :     | S_Stabilize
90 : jhr 2356 | S_Return of var
91 : jhr 1640 | S_Print of var list
92 : jhr 171
93 :     and exp
94 :     = E_Var of var
95 :     | E_Lit of Literal.literal
96 :     | E_Tuple of var list
97 : jhr 2491 | E_Apply of var * var list * ty (* user-defined function *)
98 :     | E_Prim of AST.var * meta_arg list * var list * ty (* Diderot builtin *)
99 : jhr 171 | E_Cons of var list
100 : jhr 2636 | E_Seq of var list
101 : jhr 2491 | E_Slice of var * var option list * ty (* tensor slicing *)
102 :     | E_Coerce of {srcTy : ty, dstTy : ty, x : var}
103 : jhr 2636 | E_LoadImage of ty * string * ImageInfo.info
104 : jhr 171
105 : jhr 2491 fun typeOf (E_Var(V{ty, ...})) = ty
106 : jhr 171 | typeOf (E_Lit lit) = (case lit
107 : jhr 2491 of (Literal.Int _) => SimpleTypes.T_Int
108 :     | (Literal.Float _) => SimpleTypes.T_Tensor[]
109 :     | (Literal.String s) => SimpleTypes.T_String
110 :     | (Literal.Bool _) => SimpleTypes.T_Bool
111 : jhr 2356 (* end case *))
112 : jhr 171 | typeOf (E_Tuple _) = raise Fail "E_Tuple"
113 : jhr 2491 | typeOf (E_Apply(_, _, ty)) = ty
114 :     | typeOf (E_Prim(_, _, _, ty)) = ty
115 : jhr 2356 | typeOf (E_Cons[]) = raise Fail "impossible empty E_Cons"
116 : jhr 2491 | typeOf (E_Cons(V{ty, ...}::xs)) = let
117 : jhr 2356 val d = List.length xs + 1
118 :     in
119 : jhr 2491 case ty
120 :     of SimpleTypes.T_Tensor shape => SimpleTypes.T_Tensor(shape @ [d])
121 : jhr 2356 | _ => raise Fail(concat[
122 : jhr 2491 "element of tensor construction is ", SimpleTypes.toString ty,
123 :     ", expected tensor"
124 : jhr 2356 ])
125 :     (* end case *)
126 :     end
127 : jhr 2636 | typeOf (E_Seq[]) = raise Fail "empty sequence not supported yet"
128 :     | typeOf (E_Seq(V{ty, ...}::xs)) = let
129 :     val d = List.length xs + 1
130 :     in
131 :     SimpleTypes.T_Sequence(ty, d)
132 :     end
133 : jhr 399 | typeOf (E_Slice(_, _, ty)) = ty
134 : jhr 2636 | typeOf (E_Coerce{dstTy, ...}) = dstTy
135 :     | typeOf (E_LoadImage(ty, _, _)) = ty
136 : jhr 171
137 :     end

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