Debian Codegraph @

lib/mlrisc-lib/MLRISC/Tools/Doc/nowhere.tex

raw
1 \documentclass{article}
2 \usepackage{alltt}
3 \newcommand{\nowhere}{{\sf nowhere}}
4 \newcommand{\Nowhere}{{\sf Nowhere}}
5 \newcommand{\KWW}[2]{\newcommand{#1}[1]{{\bf #2}}}
6 \newcommand{\KW}[2]{\newcommand{#1}[1]{{\bf #2}\ }}
7 \newcommand{\END}{{\bf end}}
8 \newcommand{\OF}{{\bf of}}
9 \KW{\FUN}{fun} \KW{\VAL}{val}
10 \KWW{\LOCAL}{local} \KW{\ANDALSO}{andalso}
11 \KW{\CASE}{case} \KW{\LET}{let} \KW{\WHERE}{where}
12 \KW{\INCLUDE}{include}
13 \KW{\SIGNATURE}{signature}
14 \KW{\STRUCTURE}{structure}
15 \KWW{\STRUCT}{struct}
16 \KW{\DATATYPE}{datatype} \KWW{\NOT}{not}
17 \newcommand{\SIG}{{\bf sig}}
18 \KW{\IN}{in}
19 \KW{\AND}{and}
20
21 \title{\Nowhere: a pattern matching tool for Standard ML\\
22 Version 1.1 Manual
23 }
24 \author{Allen Leung\thanks{New York University. Email:{\tt [email protected]}}}
25 \date{December 12th, 2000}
26 \begin{document}
27 \maketitle
28 \section{Introduction}
29 If you are like me, you lament the fact that SML doesn't have conditional
30 pattern matching. Personally, I don't fully understand the rationale for its
31 omission in the standard--- even though there are semantics ambiguity
32 issues if guard expressions have side effects, conditional pattern matching
33 is just too useful in practice. When writing non-trivial transformations, I
34 find myself having to manually factor pattern matching code,
35 whose job should be the compiler's.
36 I consider its lack in SML a big flaw in an otherwise great language.
37
38 As a remedy, \nowhere{} is a simple source to source tool that translates
39 SML programs augmented with where-clauses and other complex patterns
40 into normal SML code, i.e., code without where-clauses\footnote{Thus the name of the tool.}.
41
42 \subsection{Syntax}
43 Where-clauses that we recognize are of two forms.
44 In case statements:
45 \begin{alltt}
46 \(pat\) \WHERE \(exp\) => \(exp\)
47 \end{alltt}
48 and in function clauses:
49 \begin{alltt}
50 \(pat\sb{1}\) \ldots \(pat\sb{n}\) \WHERE (\(exp\)) = \(exp\)
51 \end{alltt}
52 Note that parentheses are not optional in the latter form, because
53 of syntactic ambiguity with \verb|=|.
54
55 For example, the expression
56 \begin{alltt}
57 \CASE [1,2,3] \OF
58 [x,y,z] \WHERE x>y \ANDALSO y>z => 1
59 | [x,y,z] \WHERE x<y \ANDALSO y<z => 2
60 | _ => 3
61 \end{alltt}
62 evaluates to 2.
63
64 \Nowhere{} takes input files of the following form:
65 \begin{alltt}
66 {\bf local}
67 {\sl datatype definitions prologue}
68 {\bf in}
69 {\sl code}
70 {\bf end}
71 \end{alltt}
72 It looks for where-clauses within the {\sl code} section, and rewrite it into
73 real SML code. The {\sl prologue} section is for the benefit
74 of \nowhere, since it is rather stupid at the moment and doesn't understand
75 SML's scoping rule and environments.
76 All datatypes that are used within the {\sl code}
77 section should be mentioned here, so that \nowhere{} can find its definitions.
78
79 You can use the special file import declaration:
80 \begin{alltt}
81 {\bf include} "filename"
82 \end{alltt}
83 to import the definitions from a file. Note that \nowhere{} will look
84 inside signature definitions. So if all datatypes are
85 properly defined in signature files, all you have to do is import these
86 files.
87
88 \subsection{A Note on Semantics}
89 Expressions evaluated inside where-clauses should be side-effect free.
90 This is a built-in assumption in the match compiler. If it is violated
91 the result may not be as expected.
92
93 \subsection{Other Extensions in Version 1.1}
94
95 Aside from the standard set of SML patterns and where-clauses,
96 the following extensions are also handled:
97 \subsubsection{OR-patterns} OR-patterns are written as
98 \begin{alltt}
99 (\(pat\sb{1}\) | \ldots | \(pat\sb{n}\))
100 \end{alltt}
101 Note that the parenthesis are {\em not} optional. A rule such as:
102 \begin{alltt}
103 (\(pat\sb{1}\) | \ldots | \(pat\sb{n}\)) => e
104 \end{alltt}
105 means the same thing as
106 \begin{alltt}
107 \(pat\sb{1}\) => e
108 \vdots
109 \(pat\sb{n}\) => e
110 \end{alltt}
111 The semantics of OR-patterns are identical to the treatment in SML/NJ.
112 In particular, all disjuncts must have the same set of pattern variables
113 and each variable must have the same type in all occurrances in the OR-pattern.
114
115 \subsubsection{NOT-patterns} NOT patterns negate the meaning of a pattern.
116 The general syntax is
117 \begin{alltt}
118 \NOT \(pat\)
119 \end{alltt}
120 For example, a pattern \verb|not p|, only matches forms other than \verb|p|.
121 Note that the bindings of a NOT pattern are not visible, for obvious reasons.
122
123 \subsubsection{WHERE-patterns} WHERE-patterns modifies a pattern by adding
124 a condition. For example, the pattern
125 \begin{alltt}
126 ([x,y,z] \WHERE x<y \ANDALSO y<z)
127 \end{alltt}
128 matches only lists of length 3 listed in increasing order.
129 WHERE-pattern can be seen as a simple generalization
130 of where-clauses. The general syntax of a WHERE-pattern is:
131 \begin{alltt}
132 (\(pat\) \WHERE \(exp\))
133 \end{alltt}
134 Note that the parentheses are required. Furthermore, $exp$ can only
135 refer to pattern variables introduced in \(pat\).
136
137 Because WHERE-patterns can be nested in subpatterns,
138 they are very useful when combined with other pattern connectives.
139 For example, the pattern
140 \begin{alltt}
141 \NOT ([x,y,z] \WHERE x<y \ANDALSO y<z)
142 \end{alltt}
143 matches all integer lists {\em except} lists of length 3 listed in increasing order.
144
145 \subsubsection{Nested patterns} Nested patterns allow conditional case statements to
146 be embedded in a pattern. They are written as\footnote{This syntax may be
147 changed in future versions.}:
148 \begin{alltt}
149 (\(pat\sb{1}\) \WHERE \(exp\) \IN \(pat\sb{2}\))
150 \end{alltt}
151 The semantics is to match $pat_1$, and if it matches, evaluates
152 $exp$, and matches it with $pat_2$. The entire pattern matches if
153 both $pat_1$ and $pat_2$ match. Note that $exp$ may refer to pattern
154 variables introduced in $pat_1$, and the result of a successful match introduces
155 bindings from both $pat_1$ and $pat_2$.
156
157 For example, the pattern
158 \begin{alltt}
159 ((l as h::t) \WHERE List.last l \IN [x])
160 \end{alltt}
161 matches only non-nil lists of lists whose last element is a singleton.
162 Upon a successful match, pattern variables \verb|h|,
163 \verb|t| and \verb|x| are bound to the appropriate values.
164
165 \subsubsection{Real and IntInf literals}
166 Real and IntInf literal patterns are supported.
167 IntInf literals are written with the suffix \verb|i|.
168 For example, \verb|0i| is 0 (of type \verb|IntInf.int|.)
169 Binary and hex prefixes can also be used.
170 For example, the literals \verb|0xffi|, \verb|0b11111111i|
171 and \verb|256i| mean the same thing.
172
173 \section{An Example}
174 Here's a larger example taken from the x86 peephole phase in MLRISC.
175 We only show the push and pop folding rules. The first rule
176 folds the instructions
177 \begin{verbatim}
178 subl 4, %esp
179 movl src, 0(%esp)
180 \end{verbatim}
181 into \verb|push src|, assuming src is not \verb|%esp|.
182 The second rule folds
183 \begin{verbatim}
184 movl 0(%esp), dst
185 addl 4, %esp
186 \end{verbatim}
187 into \verb|pop dst|, again assuming dst is not \verb|%esp|.
188
189 \begin{alltt}
190 \LOCAL\
191 (* import instruction definition *)
192 \STRUCTURE I =
193 \STRUCT
194 include "x86Instr.sml"
195 \END
196 \IN\
197 {\sl ...}
198 \FUN loop(current, instrs) =
199 \CASE current \OF
200 {\sl ...}
201 | I.BINARY\{binOp=I.SUBL, src=I.Immed 4, dst=I.Direct dst_i\}::
202 I.MOVE\{mvOp=I.MOVL,src,dst=I.Displace\{base,disp=I.Immed 0,...\}\}
203 ::rest
204 \WHERE C.sameColor(base, C.esp) andalso
205 C.sameColor(dst_i, C.esp) andalso
206 not(isStackPtr src) =>
207 loop(rest, I.PUSHL src::instrs)
208
209 | I.MOVE\{mvOp=I.MOVL,
210 src=I.Displace\{base, disp=I.Immed 0, ...\}, dst\}::
211 I.BINARY\{binOp=I.ADDL, src=I.Immed 4, dst=I.Direct dst_i\}::
212 rest
213 \WHERE C.sameColor(base, C.esp) andalso
214 C.sameColor(dst_i,C.esp) andalso
215 not(isStackPtr dst) =>
216 loop(rest, I.POP dst::instrs)
217
218 | i::rest => loop(rest, i::instrs)
219 {\sl ...}
220 \END
221 \end{alltt}
222 where function \verb|isStackPtr| is defined as:
223 \begin{alltt}
224 \FUN isStackPtr(I.Direct r) = C.sameColor(r,C.esp)
225 | isStackPtr _ = false
226 \end{alltt}
227
228 The same set of rules may be written in many different ways.
229 For example, an alternative way of writing the push folding rule, using
230 WHERE-patterns and NOT-patterns, is:
231 \begin{alltt}
232 I.BINARY\{binOp=I.SUBL,
233 src=I.Immed 4,
234 dst=(I.Direct dst \WHERE C.sameColor(dst,C.esp))
235 \}::
236 I.MOVE\{mvOp=I.MOVL,
237 src=\NOT (I.Direct src \WHERE C.sameColor(src,C.esp)),
238 dst=I.Displace\{base \WHERE C.sameColor(base,C.esp),
239 disp=I.Immed 0,...\}
240 \}
241 ::rest
242 =>
243 loop(rest, I.PUSHL src::instrs)
244 \end{alltt}
245 The labelled pattern
246 \begin{alltt}
247 base \WHERE C.sameColor(base,C.esp)
248 \end{alltt}
249 is an abbreviation for
250 \begin{alltt}
251 base=(base \WHERE C.sameColor(base,C.esp))
252 \end{alltt}
253
254 Note that since all constructors are accessed via the structure \verb|I|
255 inside the body,
256 the datatype definitions are defined accordingly in the prologue.
257 \begin{alltt}
258 \STRUCTURE I =
259 \STRUCT
260 include "x86Instr.sml"
261 \END
262 \end{alltt}
263
264 \section{Compiling and Running the Tool}
265
266 To compile the tool, go to the directory Tools/WhereGen, and in sml, type
267 \begin{alltt}
268 use "build.sml"
269 \end{alltt}
270 When this completes, the image \verb|nowhere.<arch>| will be generated.
271
272 The input files to \nowhere{} should have any suffix other than \verb|.sml|.
273 To run the tool, type:
274 \begin{alltt}
275 sml @SMLload=nowhere inputfile
276 \end{alltt}
277
278 \section{Bugs and Missing Features}
279 Currently, these things are missing:
280 \begin{enumerate}
281 \item SML/NJ extensions, such as array and vector patterns, are currently not supported.
282 \item Type checking of patterns is missing.
283 \item The error messages may be less than clear. Furthermore,
284 line reporting may be out-of-sync.
285 \end{enumerate}
286
287 \end{document}