Debian Codegraph @

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

raw
1 \section{MLTree Extensions} \label{sec:mltree-extension}
2 Pattern matching over the MLTREE intermediate representation
3 may not be sufficient to provide access to all the registers or
4 operations provided on a specific architecture. MLTREE extensions is a
5 method of extending the MLTREE intermediate language so that it is a
6 better match for the target architecture.
7
8
9 \subsection{Why Extensions}
10
11 Pattern matching over the MLTREE intermediate representation
12 may not be sufficient to provide access to all the registers or
13 operations provided on a specific architecture. MLTREE extensions is a
14 method of extending the MLTREE intermediate language so that it is a
15 better match for the target architecture.
16
17 For example there may be special registers to support the
18 increment-and-test operation on loop indices, or
19 support for complex mathematical functions such as
20 square root, or access to hardware specific registers such as the
21 current register window pointer on the SPARC architecture. It is not
22 usually possible to write expression trees that would directly
23 generate these instructions.
24 Some complex operations can be generated by performing a peephole
25 optimization over simpler instructions, however this is not always the
26 most convenient or simple thing to do.
27
28 \subsection{Cyclic Dependency}
29
30 The easiest way to provide extensions is to parameterize the MLTREE
31 interface with types that extend the various kinds of trees. Thus if
32 the type \sml{sext} represented statement extensions, we might define
33 MLTREE statement trees as :
34 \begin{SML}
35 datatype stm
36 = ...
37 | SEXT of sext * mlrisc list * stm list
38
39 and mlrisc = GPR of rexp | FPR of fexp | CCR of ccexp
40 \end{SML}
41 where the constructor \sml{SEXT} applies the extension to a list of
42 arguments. This approach is unsatisfactory in several ways, for
43 example, if one wanted to extend MLTREEs with for-loops, then the
44 following could be generated:
45 \begin{SML}
46 SEXT(FORLOOP, [GPR from, GPR to, GPR step], body)
47 \end{SML}
48 First, the loop arguments have to be wrapped up in \sml{GPR} and there
49 is little self documentation on the order of elements that are
50 arguments to the for-loop. It would be better to be able to write
51 something like:
52 \begin{SML}
53 SEXT(FORLOOP\{from=f, to=t, step=s, body=b\})
54 \end{SML}
55
56 Where \sml{f}, \sml{t}, and \sml{s} are \sml{rexp} trees representing
57 the loop index start, end, and step size; \sml{b} is a stm list
58 representing the body of the loop. Unfortunately, there is a cyclic
59 dependency as MLTREEs are defined in terms of \sml{sext}, and {\tt
60 sext} is defined in terms of MLTREEs. The usual way to deal with
61 cyclic dependencies is to use polymorphic type variables.
62
63 \subsection{MLTREE EXTENSION}
64
65 The statement extension type \sml{sext}, is now a type constructor
66 with arity four, i.e.
67 \sml{('s, 'r, 'f, 'c) sx} where \sml{sx} is used instead of {\tt
68 sext}, and \sml{'s}, \sml{'r}, \sml{'f}, and \sml{'c} represents
69 MLTREE statement expressions, register expressions, floating point
70 expressions, and condition code expressions. Thus the for-loop
71 extension could be declared using something like:
72 \begin{SML}
73 datatype ('s,'r,'f,'c) sx
74 = FORLOOP of \{from: 'r, to: 'r, step: 'r, body: 's\}
75 \end{SML}
76 and the MLTREE interface is defined as:
77 \begin{SML}
78 signature MLTREE = sig
79 type ('s, 'r, 'f, 'c) sx
80
81 datatype stm =
82 = ...
83 | SEXT of sext
84
85 withtype sext = (stm, rexp, fexp, cexp) sx
86 end
87 \end{SML}
88
89 where \sml{sext} is the user defined statement extension but the
90 type variables have been instantiated to the final form the the MLTREE
91 \sml{stm}, \sml{rexp}, \sml{fexp}, and \sml{cexp} components.
92
93 \subsection{Compilation}
94
95 There are dedicated modules that perform pattern matching over MLTREEs
96 and emit native instructions, and similar modules must be written for
97 extensions. However, the same kinds of choices used in regular MLTREE
98 patterns must be repeated for extensions. For example, one may define
99 an extension for the Intel IA32 of the form:
100
101 \begin{SML}
102 datatype ('s,'r,'f,'c) sx = PUSHL of 'r | POPL of 'r | ...
103 \end{SML}
104
105 that translate directly to the Intel push and pop instructions; the
106 operands in each case are either memory locations or registers, but
107 immediates are allowed in the case of \sml{PUSHL}. Considerable effort
108 has been invested into pattern matching the extensive set of
109 addressing modes for the Intel architecture, and
110 one would like to reuse this when compiling extensions. The pattern
111 matching functions are exposed by a set of functions exported from the
112 instruction selection module, and provided in the MLTREE
113 interface. They are:
114
115 \begin{SML}
116 struture I : INSTRUCTIONS
117 datatype reducer =
118 REDUCER of \{
119 reduceRexp : rexp -> reg,
120 reduceFexp : fexp -> reg,
121 reduceCCexp : ccexp -> reg,
122 reduceStm : stm * an list -> unit,
123 operand : rexp -> I.operand,
124 reduceOperand : I.operand -> reg,
125 addressOf : rexp -> I.addressing_mode,
126 emit : I.instr * an list -> unit,
127 instrStream : (I.instr, I.regmap, I.cellset) stream,
128 mltreeStream : (stm, I.regmap, mlrisc list) stream
129 \}
130 \end{SML}
131
132 where \sml{I} is the native instruction set.
133 \begin{description}
134 \item[\tt reduceRexp]: reduces an MLTREE \sml{rexp} to a register, and
135 similarly for \sml{reduceFexp} and \sml{reduceCCexp}.
136 \item[\tt reduceStm]: reduces an MLTREE \sml{stm} to a set of instructions
137 that implement the set of statements.
138 \item[\tt operand]: reduced an MLTREE \sml{rexp} into an instruction
139 operand --- usually an immediate or memory address.
140 \item[\tt operand]: moves a native operand into a register.
141 \item[\tt addressOf]: reduces an MLTREE \sml{rexp} into a memory address.
142 \item[\tt emit]: emits an instruction together with an annotation.
143 \item[\tt instrStream]: is the native instruction output stream, and
144 \item[\tt mltreeStream]: is the MLTREE output stream.
145 \end{description}
146
147 Each extension must provide a function \sml{compileSext} that compiles
148 a statement extension into native instructions. In the
149 \sml{MLTREE_EXTENSION_COMP} interface we have:
150 \begin{SML}
151 val compileSext: reducer -> {stm: MLTREE.sexp, an:MLTREE.an list} -> unit
152 \end{SML}
153
154 The use of extensions must follow a special structure.
155 \begin{enumerate}
156 \item A module defining the extension type using a type constructor
157 of arity four. Let us call this structure \sml{ExtTy} and must match
158 the \sml{MLTREE_EXTENSION} interface.
159 \item The extension module must be used to specialize MLTREEs.
160 \item A module that describes how to compile the extension must be
161 created, and must match the \sml{MLTREE_EXTENSION_COMP} interace.
162 This module will typically be functorized over the MLTREE interface.
163 Let us call the result of applying the functor, \sml{ExtComp}.
164 \item The extension compiler must be passed as a parameter to the
165 instruction selection module that will invoke it whenever an extension
166 is seen.
167 \end{enumerate}
168
169
170 \subsection{Multiple Extensions}
171
172 Multiple extensions are handled in a similar fashion, except that the
173 extension type used to specialize MLTREEs is a tagged union of the
174 individual extensions. The functor to compile the extension dispatches
175 to the compilation modules for the individual extensions.
176
177 \subsection{Example}
178 Suppose you are in the process of writing a compiler for a digital
179 signal processing(\newdef{DSP}) programming language using the MLRISC
180 framework. This wonderful language that you are developing allows the
181 programmer to specify high level looping and iteration, and
182 aggregation constructs that are common in DSP applications.
183 Furthermore, since saturated and fixed point arithmetic are common
184 constructs in DSP applications, the language and consequently the
185 compiler should directly support these operators. For simplicity, we
186 would like to have a unified intermediate representation that can be
187 used to directly represent high level constructs in our language, and
188 low level constructs that are already present in MLTree. Since,
189 MLTree does not directly support these constructs, it seems that it is
190 not possible to use MLRISC for such a compiler infrastructure without
191 substantial rewrite of the core components.
192
193 Let us suppose that for illustration that we would like to
194 implement high level looping and aggregation constructs such as
195 \begin{verbatim}
196 for i := lower bound ... upper bound
197 body
198 x := sum{i := lower bound ... upper bound} expression
199 \end{verbatim}
200 together with saturated arithmetic mentioned above.
201
202 Here is a first attempt:
203 \begin{SML}
204 structure DSPMLTreeExtension
205 struct
206 structure Basis = MLTreeBasis
207 datatype ('s,'r,'f,'c) sx =
208 FOR of Basis.var * 'r * 'r * 's
209 and ('s,'r,'f,'c) rx =
210 SUM of Basis.var * 'r * 'r * 'r
211 | SADD of 'r * 'r
212 | SSUB of 'r * 'r
213 | SMUL of 'r * 'r
214 | SDIV of 'r * 'r
215 type ('s,'r,'f,'c) fx = unit
216 type ('s,'r,'f,'c) ccx = unit
217 end
218 structure DSPMLTree : MLTreeF
219 (structure Extension = DSPMLTreeExtension
220 ...
221 )
222 \end{SML}
223 In the above signature, we have defined two new datatypes \newtype{sx}
224 and \newtype{rx} that are used for representing the DSP statement
225 and integer expression extensions. Integer expression extensions
226 include the high level sum construct, and the low levels saturated
227 arithmetic operators. The recursive type definition is
228 necessary to ``inject'' these new constructors into the basic MLTree
229 definition.
230
231 The following is an example of how these new constructors that we have defined can be used. Suppose the source program in our DSP language is:
232 \begin{verbatim}
233 for i := a ... b
234 { s := sadd(s, table[i]);
235 }
236 \end{verbatim}
237 \noindent where \verb|sadd| is the saturated add operator.
238 For simplicity, let us also assume that all operations and addresses
239 are in 32-bits.
240 Then the translation of the above into our extended DSP-MLTree could be:
241 \begin{SML}
242 EXT(FOR(\(i\), REG(32, \(a\)), REG(32, \(b\)),
243 MV(32, \(s\), REXT(32, SADD(REG(32, \(s\)),
244 LOAD(32,
245 ADD(32, REG(32, \(table\)),
246 SLL(32, REG(32, \(i\)), LI 2)),
247 \(region\)))))
248 ))
249 \end{SML}
250
251 One potential short coming of our DSP extension to MLTree is that
252 the extension does not allow any further extensions. This restriction
253 may be entirely satisfactory if DSP-MLTree is only used in your compiler
254 applications and no where else. However, if DSP-MLTree is intended
255 to be an extension library for MLRISC, then we must build in the flexibility
256 for extension. This can be done in the same way as in the base MLTree
257 definition, like this:
258 \begin{SML}
259 functor ExtensibleDSPMLTreeExtension
260 (Extension : \mlrischref{mltree/mltree-extension.sig}{MLTREE_EXTENSION}) =
261 struct
262 structure Basis = MLTreeBasis
263 structure Extension = Extension
264 datatype ('s,'r,'f,'c) sx =
265 FOR of Basis.var * 'r * 'r * 's
266 | EXT of ('s,'r,'f,'c) Extension.sx
267 and ('s,'r,'f,'c) rx =
268 SUM of Basis.var * 'r * 'r * 'r
269 | SADD of 'r * 'r
270 | SSUB of 'r * 'r
271 | SMUL of 'r * 'r
272 | SDIV of 'r * 'r
273 | REXT of ('s,'r,'f,'c) Extension.rx
274 withtype
275 ('s,'r,'f,'c) fx = ('s,'r,'f,'c) Extension.fx
276 and ('s,'r,'f,'c) ccx = ('s,'r,'f,'c) Extension.ccx
277 end
278 \end{SML}
279
280 As in MLTREE, we provide two new extension
281 constructors \verb|EXT| and \verb|REXT| in
282 the definition of \sml{DSP_MLTREE}, which can
283 be used to further enhance the extended MLTREE language.