Debian Codegraph @

lib/mlrisc-lib/MLRISC/Doc/latex/mltree-util.tex

raw
1 \section{MLTree Utilities}
2
3 The \MLRISC{} system contains numerous utilities for working with
4 MLTree datatypes. Some of the following utilizes are also useful for clients
5 use:
6 \begin{description}
7 \item[MLTreeUtils] implements basic hashing, equality and pretty
8 printing functions,
9 \item[MLTreeFold] implements a fold function over the MLTree datatypes,
10 \item[MLTreeRewrite] implements a generic rewriting engine,
11 \item[MLTreeSimplify] implements a simplifier that performs algebraic
12 simplification and constant folding.
13 \end{description}
14 \subsubsection{Hashing, Equality, Pretty Printing}
15
16 The functor \mlrischref{mltree/mltree-utils.sml}{MLTreeUtils} provides
17 the basic utilities for hashing an MLTree term, comparing two
18 MLTree terms for equality and pretty printing. The hashing and comparision
19 functions are useful for building hash tables using MLTree datatype as keys.
20 The signature of the functor is:
21 \begin{SML}
22 signature \mlrischref{mltree/mltree-utils.sig}{MLTREE_UTILS} =
23 sig
24 structure T : MLTREE
25
26 (*
27 * Hashing
28 *)
29 val hashStm : T.stm -> word
30 val hashRexp : T.rexp -> word
31 val hashFexp : T.fexp -> word
32 val hashCCexp : T.ccexp -> word
33
34 (*
35 * Equality
36 *)
37 val eqStm : T.stm * T.stm -> bool
38 val eqRexp : T.rexp * T.rexp -> bool
39 val eqFexp : T.fexp * T.fexp -> bool
40 val eqCCexp : T.ccexp * T.ccexp -> bool
41 val eqMlriscs : T.mlrisc list * T.mlrisc list -> bool
42
43 (*
44 * Pretty printing
45 *)
46 val show : (string list * string list) -> T.printer
47
48 val stmToString : T.stm -> string
49 val rexpToString : T.rexp -> string
50 val fexpToString : T.fexp -> string
51 val ccexpToString : T.ccexp -> string
52
53 end
54 functor \mlrischref{mltree/mltree-utils.sml}{MLTreeUtils}
55 (structure T : MLTREE
56 (* Hashing extensions *)
57 val hashSext : T.hasher -> T.sext -> word
58 val hashRext : T.hasher -> T.rext -> word
59 val hashFext : T.hasher -> T.fext -> word
60 val hashCCext : T.hasher -> T.ccext -> word
61
62 (* Equality extensions *)
63 val eqSext : T.equality -> T.sext * T.sext -> bool
64 val eqRext : T.equality -> T.rext * T.rext -> bool
65 val eqFext : T.equality -> T.fext * T.fext -> bool
66 val eqCCext : T.equality -> T.ccext * T.ccext -> bool
67
68 (* Pretty printing extensions *)
69 val showSext : T.printer -> T.sext -> string
70 val showRext : T.printer -> T.ty * T.rext -> string
71 val showFext : T.printer -> T.fty * T.fext -> string
72 val showCCext : T.printer -> T.ty * T.ccext -> string
73 ) : MLTREE_UTILS =
74 \end{SML}
75
76 The types \sml{hasher}, \sml{equality},
77 and \sml{printer} represent functions for hashing,
78 equality and pretty printing. These are defined as:
79 \begin{SML}
80 type hasher =
81 \{stm : T.stm -> word,
82 rexp : T.rexp -> word,
83 fexp : T.fexp -> word,
84 ccexp : T.ccexp -> word
85 \}
86
87 type equality =
88 \{ stm : T.stm * T.stm -> bool,
89 rexp : T.rexp * T.rexp -> bool,
90 fexp : T.fexp * T.fexp -> bool,
91 ccexp : T.ccexp * T.ccexp -> bool
92 \}
93 type printer =
94 \{ stm : T.stm -> string,
95 rexp : T.rexp -> string,
96 fexp : T.fexp -> string,
97 ccexp : T.ccexp -> string,
98 dstReg : T.ty * T.var -> string,
99 srcReg : T.ty * T.var -> string
100 \}
101 \end{SML}
102
103 For example, to instantiate a \sml{Utils} module for our \sml{DSPMLTree},
104 we can write:
105 \begin{SML}
106 structure U = MLTreeUtils
107 (structure T = DSPMLTree
108 fun hashSext \{stm, rexp, fexp, ccexp\} (FOR(i, a, b, s)) =
109 Word.fromIntX i + rexp a + rexp b + stm s
110 and hashRext \{stm, rexp, fexp, ccexp\} e =
111 (case e of
112 SUM(i,a,b,c) => Word.fromIntX i + rexp a + rexp b + rexp c
113 | SADD(a,b) => rexp a + rexp b
114 | SSUB(a,b) => 0w12 + rexp a + rexp b
115 | SMUL(a,b) => 0w123 + rexp a + rexp b
116 | SDIV(a,b) => 0w1245 + rexp a + rexp b
117 )
118 fun hashFext _ _ = 0w0
119 fun hashCCext _ _ = 0w0
120 fun eqSext \{stm, rexp, fexp, ccexp\}
121 (FOR(i, a, b, s), FOR(i', a', b', s')) =
122 i=i' andalso rexp(a,a') andalso rexp(b,b') andalso stm(s,s')
123 fun eqRext \{stm, rexp, fexp, ccexp\} (e,e') =
124 (case (e,e') of
125 (SUM(i,a,b,c),SUM(i',a',b',c')) =>
126 i=i' andalso rexp(a,a') andalso rexp(b,b') andalso stm(c,c')
127 | (SADD(a,b),SADD(a',b')) => rexp(a,a') andalso rexp(b,b')
128 | (SSUB(a,b),SSUB(a',b')) => rexp(a,a') andalso rexp(b,b')
129 | (SMUL(a,b),SMUL(a',b')) => rexp(a,a') andalso rexp(b,b')
130 | (SDIV(a,b),SDIV(a',b')) => rexp(a,a') andalso rexp(b,b')
131 | _ => false
132 )
133 fun eqFext _ _ = true
134 fun eqCCext _ _ = true
135
136 fun showSext \{stm, rexp, fexp, ccexp, dstReg, srcReg\}
137 (FOR(i, a, b, s)) =
138 "for("^dstReg i^":="^rexp a^".."^rexp b^")"^stm s
139 fun ty t = "."^Int.toString t
140 fun showRext \{stm, rexp, fexp, ccexp, dstReg, srcReg\} e =
141 (case (t,e) of
142 SUM(i,a,b,c) =>
143 "sum"^ty t^"("^dstReg i^":="^rexp a^".."^rexp b^")"^rexp c
144 | SADD(a,b) => "sadd"^ty t^"("rexp a^","^rexp b^")"
145 | SSUB(a,b) => "ssub"^ty t^"("rexp a^","^rexp b^")"
146 | SMUL(a,b) => "smul"^ty t^"("rexp a^","^rexp b^")"
147 | SDIV(a,b) => "sdiv"^ty t^"("rexp a^","^rexp b^")"
148 )
149 fun showFext _ _ = ""
150 fun showCCext _ _ = ""
151 )
152 \end{SML}
153
154 \subsubsection{MLTree Fold}
155 The functor \mlrischref{mltree/mltree-fold.sml}{MLTreeFold}
156 provides the basic functionality for implementing various forms of
157 aggregation function over the MLTree datatypes. Its signature is
158 \begin{SML}
159 signature \mlrischref{mltree/mltree-fold.sig}{MLTREE_FOLD} =
160 sig
161 structure T : MLTREE
162
163 val fold : 'b folder -> 'b folder
164 end
165 functor \mlrischref{mltree/mltree-fold.sml}{MLTreeFold}
166 (structure T : MLTREE
167 (* Extension mechnism *)
168 val sext : 'b T.folder -> T.sext * 'b -> 'b
169 val rext : 'b T.folder -> T.ty * T.rext * 'b -> 'b
170 val fext : 'b T.folder -> T.fty * T.fext * 'b -> 'b
171 val ccext : 'b T.folder -> T.ty * T.ccext * 'b -> 'b
172 ) : MLTREE_FOLD =
173 \end{SML}
174 The type \newtype{folder} is defined as:
175 \begin{SML}
176 type 'b folder =
177 \{ stm : T.stm * 'b -> 'b,
178 rexp : T.rexp * 'b -> 'b,
179 fexp : T.fexp * 'b -> 'b,
180 ccexp : T.ccexp * 'b -> 'b
181 \}
182 \end{SML}
183
184
185 \subsubsection{MLTree Rewriting}
186
187 The functor \mlrischref{mltree/mltree-rewrite.sml}{MLTreeRewrite}
188 implements a generic term rewriting engine which is useful for performing
189 various transformations on MLTree terms. Its signature is
190 \begin{SML}
191 signature \mlrischref{mltree/mltree-rewrite.sig}{MLTREE_REWRITE} =
192 sig
193 structure T : MLTREE
194
195 val rewrite :
196 (* User supplied transformations *)
197 \{ rexp : (T.rexp -> T.rexp) -> (T.rexp -> T.rexp),
198 fexp : (T.fexp -> T.fexp) -> (T.fexp -> T.fexp),
199 ccexp : (T.ccexp -> T.ccexp) -> (T.ccexp -> T.ccexp),
200 stm : (T.stm -> T.stm) -> (T.stm -> T.stm)
201 \} -> T.rewriters
202 end
203 functor \mlrischref{mltre/mltree-rewrite.sml}{MLTreeRewrite}
204 (structure T : MLTREE
205 (* Extension *)
206 val sext : T.rewriter -> T.sext -> T.sext
207 val rext : T.rewriter -> T.rext -> T.rext
208 val fext : T.rewriter -> T.fext -> T.fext
209 val ccext : T.rewriter -> T.ccext -> T.ccext
210 ) : MLTREE_REWRITE =
211 \end{SML}
212
213 The type \newtype{rewriter} is defined in signature
214 \mlrischref{mltree/mltree.sig}{MLTREE} as:
215 \begin{SML}
216 type rewriter =
217 \{ stm : T.stm -> T.stm,
218 rexp : T.rexp -> T.rexp,
219 fexp : T.fexp -> T.fexp,
220 ccexp : T.ccexp -> T.ccexp
221 \}
222 \end{SML}
223
224 \subsubsection{MLTree Simplifier}
225
226 The functor \mlrischref{mltree/mltree-simplify.sml}{MLTreeSimplify}
227 implements algebraic simplification and constant folding for MLTree.
228 Its signature is:
229 \begin{SML}
230 signature \mlrischref{mltree/mltree-simplify.sig}{MLTREE_SIMPLIFIER} =
231 sig
232
233 structure T : MLTREE
234
235 val simplify :
236 { addressWidth : int } -> T.simplifier
237
238 end
239 functor \mlrischref{mltree/mltree-simplify.sml}{MLTreeSimplifier}
240 (structure T : MLTREE
241 (* Extension *)
242 val sext : T.rewriter -> T.sext -> T.sext
243 val rext : T.rewriter -> T.rext -> T.rext
244 val fext : T.rewriter -> T.fext -> T.fext
245 val ccext : T.rewriter -> T.ccext -> T.ccext
246 ) : MLTREE_SIMPLIFIER =
247 \end{SML}
248
249 Where type \newdef{simplifier} is defined in signature
250 \mlrischref{mltree/mltree.sig}{MLTREE} as:
251 \begin{SML}
252 type simplifier =
253 \{ stm : T.stm -> T.stm,
254 rexp : T.rexp -> T.rexp,
255 fexp : T.fexp -> T.fexp,
256 ccexp : T.ccexp -> T.ccexp
257 \}
258 \end{SML}
259
260