539
|
1 |
\documentclass{article}
|
|
2 |
\usepackage{../style}
|
|
3 |
\usepackage{../langs}
|
|
4 |
\usepackage{../graphics}
|
|
5 |
\usepackage{../grammar}
|
|
6 |
\usepackage{multicol}
|
|
7 |
|
|
8 |
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}
|
|
9 |
|
|
10 |
\begin{document}
|
|
11 |
\fnote{\copyright{} Christian Urban, King's College London, 2014}
|
|
12 |
|
|
13 |
%% why are shuttle flights so good with software
|
|
14 |
%%http://www.fastcompany.com/28121/they-write-right-stuff
|
|
15 |
|
|
16 |
\section*{Handout 9 (Static Analysis)}
|
|
17 |
|
|
18 |
If we want to improve the safety and security of our programs,
|
|
19 |
we need a more principled approach to programming. Testing is
|
|
20 |
good, but as Edsger Dijkstra famously wrote:
|
|
21 |
|
|
22 |
\begin{quote}\it
|
|
23 |
``Program testing can be a very effective way to show the
|
|
24 |
\underline{\smash{presence}} of bugs, but it is hopelessly
|
|
25 |
inadequate for showing their \underline{\smash{absence}}.''
|
|
26 |
\end{quote}
|
|
27 |
|
|
28 |
\noindent While such a more principled approach has been the
|
|
29 |
subject of intense study for a long, long time, only in the
|
|
30 |
past few years some impressive results have been achieved. One
|
|
31 |
is the complete formalisation and (mathematical) verification
|
|
32 |
of a microkernel operating system called seL4.
|
|
33 |
|
|
34 |
\begin{center}
|
|
35 |
\url{http://sel4.systems}
|
|
36 |
\end{center}
|
|
37 |
|
|
38 |
\noindent In 2011 this work was included in the MIT Technology
|
|
39 |
Review in the annual list of the world’s ten most important
|
|
40 |
emerging
|
|
41 |
technologies.\footnote{\url{http://www2.technologyreview.com/tr10/?year=2011}}
|
|
42 |
While this work is impressive, its technical details are too
|
|
43 |
enormous for an explanation here. Therefore let us look at
|
|
44 |
something much simpler, namely finding out properties about
|
|
45 |
programs using \emph{static analysis}.
|
|
46 |
|
|
47 |
Static analysis is a technique that checks properties of a
|
|
48 |
program without actually running the program. This should
|
|
49 |
raise alarm bells with you---because almost all interesting
|
|
50 |
properties about programs are equivalent to the halting
|
|
51 |
problem, which we know is undecidable. For example estimating
|
|
52 |
the memory consumption of programs is in general undecidable,
|
|
53 |
just like the halting problem. Static analysis circumvents
|
|
54 |
this undecidability-problem by essentially allowing answers
|
|
55 |
\emph{yes} and \emph{no}, but also \emph{don't know}. With
|
|
56 |
this ``trick'' even the halting problem becomes
|
|
57 |
decidable\ldots{}for example we could always say \emph{don't
|
|
58 |
know}. Of course this would be silly. The point is that we
|
|
59 |
should be striving for a method that answers as often as
|
|
60 |
possible either \emph{yes} or \emph{no}---just in cases when
|
|
61 |
it is too difficult we fall back on the
|
|
62 |
\emph{don't-know}-answer. This might sound all like abstract
|
|
63 |
nonsense. Therefore let us look at a concrete example.
|
|
64 |
|
|
65 |
|
|
66 |
\subsubsection*{A Simple, Idealised Programming Language}
|
|
67 |
|
|
68 |
Our starting point is a small, idealised programming language.
|
|
69 |
It is idealised because we cut several corners in comparison
|
|
70 |
with real programming languages. The language we will study
|
|
71 |
contains, amongst other things, variables holding integers.
|
|
72 |
Using static analysis, we want to find out what the sign of
|
|
73 |
these integers (positive or negative) will be when the program
|
|
74 |
runs. This sign-analysis seems like a very simple problem. But
|
|
75 |
even such simple problems, if approached naively, are in
|
|
76 |
general undecidable, just like Turing's halting problem. I let
|
|
77 |
you think why?
|
|
78 |
|
|
79 |
|
|
80 |
Is sign-analysis of variables an interesting problem? Well,
|
|
81 |
yes---if a compiler can find out that for example a variable
|
|
82 |
will never be negative and this variable is used as an index
|
|
83 |
for an array, then the compiler does not need to generate code
|
|
84 |
for an underflow-check. Remember some languages are immune to
|
|
85 |
buffer-overflow attacks, but they need to add underflow and
|
|
86 |
overflow checks everywhere. According to John Regher, an
|
|
87 |
expert in the field of compilers, overflow checks can cause
|
|
88 |
5-10\% slowdown, in some languages even 100\% for tight
|
|
89 |
loops.\footnote{\url{http://blog.regehr.org/archives/1154}} If
|
|
90 |
the compiler can omit the underflow check, for example, then
|
|
91 |
this can potentially drastically speed up the generated code.
|
|
92 |
|
|
93 |
What do programs in our simple programming language look like?
|
|
94 |
The following grammar gives a first specification:
|
|
95 |
|
|
96 |
\begin{multicols}{2}
|
|
97 |
\begin{plstx}[rhs style=,one per line,left margin=9mm]
|
|
98 |
: \meta{Stmt} ::= \meta{label} \texttt{:}
|
|
99 |
| \meta{var} \texttt{:=} \meta{Exp}
|
|
100 |
| \texttt{jmp?} \meta{Exp} \meta{label}
|
|
101 |
| \texttt{goto} \meta{label}\\
|
|
102 |
: \meta{Prog} ::= \meta{Stmt} \ldots{} \meta{Stmt}\\
|
|
103 |
\end{plstx}
|
|
104 |
\columnbreak
|
|
105 |
\begin{plstx}[rhs style=,one per line]
: \meta{Exp} ::= \meta{Exp} \texttt{+} \meta{Exp}
|
|
106 |
| \meta{Exp} \texttt{*} \meta{Exp}
|
|
107 |
| \meta{Exp} \texttt{=} \meta{Exp}
|
|
108 |
| \meta{num}
|
|
109 |
| \meta{var}\\
|
|
110 |
\end{plstx}
|
|
111 |
\end{multicols}
|
|
112 |
|
|
113 |
\noindent I assume you are familiar with such
|
|
114 |
grammars.\footnote{\url{http://en.wikipedia.org/wiki/Backus–Naur_Form}}
|
|
115 |
There are three main syntactic categories: \emph{statments}
|
|
116 |
and \emph{expressions} as well as \emph{programs}, which are
|
|
117 |
sequences of statements. Statements are either labels,
|
|
118 |
variable assignments, conditional jumps (\pcode{jmp?}) and
|
|
119 |
unconditional jumps (\pcode{goto}). Labels are just strings,
|
|
120 |
which can be used as the target of a jump. We assume that in
|
|
121 |
every program the labels are unique---if there is a clash,
|
|
122 |
then we do not know where to jump to. The conditional jumps
|
|
123 |
and variable assignments involve (arithmetic) expressions.
|
|
124 |
Expressions are either numbers, variables or compound
|
|
125 |
expressions built up from \pcode{+}, \pcode{*} and \emph{=}
|
|
126 |
(for simplicity reasons we do not consider any other
|
|
127 |
operations). We assume we have negative and positive numbers,
|
|
128 |
\ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1},
|
|
129 |
\pcode{2}\ldots{} An example program that calculates the
|
|
130 |
factorial of 5 is in our programming language as follows:
|
|
131 |
|
|
132 |
\begin{lstlisting}[language={},xleftmargin=10mm]
|
|
133 |
a := 1
|
|
134 |
n := 5
|
|
135 |
top:
|
|
136 |
jmp? n = 0 done
|
|
137 |
a := a * n
|
|
138 |
n := n + -1
|
|
139 |
goto top
|
|
140 |
done:
|
|
141 |
\end{lstlisting}
|
|
142 |
|
|
143 |
\noindent As can be seen each line of the program contains a
|
|
144 |
statement. In the first two lines we assign values to the
|
|
145 |
variables \pcode{a} and \pcode{n}. In line 4 we test whether
|
|
146 |
\pcode{n} is zero, in which case we jump to the end of the
|
|
147 |
program marked with the label \pcode{done}. If \pcode{n} is
|
|
148 |
not zero, we multiply the content of \pcode{a} by \pcode{n},
|
|
149 |
decrease \pcode{n} by one and jump back to the beginning of
|
|
150 |
the loop, marked with the label \pcode{top}. Another program
|
|
151 |
in our language is shown in Figure~\ref{fib}. I let you think
|
|
152 |
what it calculates.
|
|
153 |
|
|
154 |
\begin{figure}[t]
|
|
155 |
\begin{lstlisting}[numbers=none,
|
|
156 |
language={},xleftmargin=10mm]
|
|
157 |
n := 6
|
|
158 |
m1 := 0
|
|
159 |
m2 := 1
|
|
160 |
loop:
|
|
161 |
jmp? n = 0 done
|
|
162 |
tmp := m2
|
|
163 |
m2 := m1 + m2
|
|
164 |
m1 := tmp
|
|
165 |
n := n + -1
|
|
166 |
goto top
|
|
167 |
done:
|
|
168 |
\end{lstlisting}
|
|
169 |
\caption{A mystery program in our idealised programming language.
|
|
170 |
Try to find out what it calculates! \label{fib}}
|
|
171 |
\end{figure}
|
|
172 |
|
|
173 |
Even if our language is rather small, it is still Turing
|
|
174 |
complete---meaning quite powerful. However, discussing this
|
|
175 |
fact in more detail would lead us too far astray. Clearly, our
|
|
176 |
programming is rather low-level and not very comfortable for
|
|
177 |
writing programs. It is inspired by real machine code, which
|
|
178 |
is the code that is executed by a CPU. So a more interesting
|
|
179 |
question is what is missing in comparison with real machine
|
|
180 |
code? Well, not much\ldots{}in principle. Real machine code,
|
|
181 |
of course, contains many more arithmetic instructions (not
|
|
182 |
just addition and multiplication) and many more conditional
|
|
183 |
jumps. We could add these to our language if we wanted, but
|
|
184 |
complexity is really beside the point here. Furthermore, real
|
|
185 |
machine code has many instructions for manipulating memory. We
|
|
186 |
do not have this at all. This is actually a more serious
|
|
187 |
simplification because we assume numbers to be arbitrary small
|
|
188 |
or large, which is not the case with real machine code. In
|
|
189 |
real machine code, basic number formats have a range and might
|
|
190 |
over-flow or under-flow this range. Also the number of
|
|
191 |
variables in our programs is potentially unlimited, while
|
|
192 |
memory in an actual computer, of course, is always limited. To
|
|
193 |
sum up, our language might look ridiculously simple, but it is
|
|
194 |
not too far removed from practically relevant issues.
|
|
195 |
|
|
196 |
|
|
197 |
\subsubsection*{An Interpreter}
|
|
198 |
|
|
199 |
Designing a language is like playing god: you can say what
|
|
200 |
names for variables you allow; what programs should look like;
|
|
201 |
most importantly you can decide what each part of the program
|
|
202 |
should mean and do. While our language is quite simple and the
|
|
203 |
meaning of statements, for example, is rather straightforward,
|
|
204 |
there are still places where we need to make real choices. For
|
|
205 |
example consider the conditional jumps, say the one in the
|
|
206 |
factorial program:
|
|
207 |
|
|
208 |
\begin{center}
|
|
209 |
\code{jmp? n = 0 done}
|
|
210 |
\end{center}
|
|
211 |
|
|
212 |
\noindent How should they work? We could introduce Booleans
|
|
213 |
(\pcode{true} and \pcode{false}) and then jump only when the
|
|
214 |
condition is \pcode{true}. However, since we have numbers in
|
|
215 |
our language anyway, why not just encoding \pcode{true} as
|
|
216 |
one, and \pcode{false} as zero? In this way we can dispense
|
|
217 |
with the additional concept of Booleans.
|
|
218 |
|
|
219 |
I hope the above discussion makes it already clear we need to
|
|
220 |
be a bit more careful with our programs. Below we shall
|
|
221 |
describe an interpreter for our programming language, which
|
|
222 |
specifies exactly how programs are supposed to be
|
|
223 |
run\ldots{}at least we will specify this for all \emph{good}
|
|
224 |
programs. By good programs I mean where all variables are
|
|
225 |
initialised, for example. Our interpreter will just crash if
|
|
226 |
it cannot find out the value for a variable when it is not
|
|
227 |
initialised. Also, we will assume that labels in good programs
|
|
228 |
are unique, otherwise our programs will calculate ``garbage''.
|
|
229 |
|
|
230 |
First we will pre-process our programs. This will simplify the
|
|
231 |
definition of our interpreter later on. By pre-processing our
|
|
232 |
programs we will transform programs into \emph{snippets}. A
|
|
233 |
snippet is a label and all the code that comes after the
|
|
234 |
label. This essentially means a snippet is a \emph{map} from
|
|
235 |
labels to code.\footnote{Be sure you know what maps are. In a
|
|
236 |
programming context they are often represented as association
|
|
237 |
list where some data is associated with a key.}
|
|
238 |
|
|
239 |
Given that programs are sequences (or lists) of statements, we
|
|
240 |
can easily calculate the snippets by just traversing this
|
|
241 |
sequence and recursively generating the map. Suppose a program
|
|
242 |
is of the general form
|
|
243 |
|
|
244 |
\begin{center}
|
|
245 |
$stmt_1\;stmt_2\; \ldots\; stmt_n$
|
|
246 |
\end{center}
|
|
247 |
|
|
248 |
\noindent The idea is to go through this sequence of
|
|
249 |
statements one by one and check whether they are a label. If
|
|
250 |
yes, we add the label and the remaining statements to our map.
|
|
251 |
If no, we just continue with the next statement. To come up
|
|
252 |
with a recursive definition for generating snippets, let us
|
|
253 |
write $[]$ for the program that does not contain any
|
|
254 |
statement. Consider the following definition:
|
|
255 |
|
|
256 |
\begin{center}
|
|
257 |
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
|
|
258 |
$\textit{snippets}([])$ & $\dn$ & $\varnothing$\\
|
|
259 |
$\textit{snippets}(stmt\;\; rest)$ & $\dn$ &
|
|
260 |
$\begin{cases}
|
|
261 |
\textit{snippets}(rest)[label := rest] & \text{if}\;stmt = \textit{label:}\\
|
|
262 |
\textit{snippets}(rest) & \text{otherwise}
|
|
263 |
\end{cases}$
|
|
264 |
\end{tabular}
|
|
265 |
\end{center}
|
|
266 |
|
|
267 |
\noindent In the first clause we just return the empty map for
|
|
268 |
the program that does not contain any statement. In the second
|
|
269 |
clause, we have to distinguish the case where the first
|
|
270 |
statement is a label or not. As said before, if not, then we
|
|
271 |
just ``throw away'' the label and recursively calculate the
|
|
272 |
snippets for the rest of the program (the otherwise clause).
|
|
273 |
If yes, then we do the same, but also update the map so that
|
|
274 |
$label$ now points to the rest of the statements (the if
|
|
275 |
clause). This looks all realtively straightforward, but there
|
|
276 |
is one small problem we need to overcome: our two programs
|
|
277 |
shown so far have no label as \emph{entry point}---that is
|
|
278 |
where the execution is supposed to start. We usually assume
|
|
279 |
that the first statement will be run first. To make this the
|
|
280 |
default, it is convenient if we add to all our programs a
|
|
281 |
default label, say \pcode{""} (the empty string). With this we
|
|
282 |
can define our pre-processing of programs as follows
|
|
283 |
|
|
284 |
\begin{center}
|
|
285 |
$\textit{preproc}(prog) \dn \textit{snippets}(\pcode{"":}\;\; prog)$
|
|
286 |
\end{center}
|
|
287 |
|
|
288 |
\noindent Let us see how this pans out in practice. If we
|
|
289 |
pre-process the factorial program shown earlier, we obtain the
|
|
290 |
following map:
|
|
291 |
|
|
292 |
\begin{center}
|
|
293 |
\small
|
|
294 |
\lstset{numbers=none,
|
|
295 |
language={},
|
|
296 |
xleftmargin=0mm,
|
|
297 |
aboveskip=0.5mm,
|
|
298 |
belowskip=0.5mm,
|
|
299 |
frame=single,
|
|
300 |
framerule=0mm,
|
|
301 |
framesep=0mm}
|
|
302 |
\begin{tikzpicture}[node distance=0mm]
|
|
303 |
\node (A1) [draw]{\pcode{""}};
|
|
304 |
\node (B1) [right=of A1] {$\mapsto$};
|
|
305 |
\node (C1) [right=of B1,anchor=north west] {
|
|
306 |
\begin{minipage}{3.5cm}
|
|
307 |
\begin{lstlisting}[language={},xleftmargin=0mm]
|
|
308 |
a := 1
|
|
309 |
n := 5
|
|
310 |
top:
|
|
311 |
jmp? n = 0 done
|
|
312 |
a := a * n
|
|
313 |
n := n + -1
|
|
314 |
goto top
|
|
315 |
done:
|
|
316 |
\end{lstlisting}
|
|
317 |
\end{minipage}};
|
|
318 |
\node (A2) [right=of C1.north east,draw] {\pcode{top}};
|
|
319 |
\node (B2) [right=of A2] {$\mapsto$};
|
|
320 |
\node (C2) [right=of B2, anchor=north west] {
|
|
321 |
\begin{minipage}{3.5cm}
|
|
322 |
\begin{lstlisting}[language={},xleftmargin=0mm]
|
|
323 |
jmp? n = 0 done
|
|
324 |
a := a * n
|
|
325 |
n := n + -1
|
|
326 |
goto top
|
|
327 |
done:
|
|
328 |
\end{lstlisting}
|
|
329 |
\end{minipage}};
|
|
330 |
\node (A3) [right=of C2.north east,draw] {\pcode{done}};
|
|
331 |
\node (B3) [right=of A3] {$\mapsto$};
|
|
332 |
\node (C3) [right=of B3] {$[]$};
|
|
333 |
\end{tikzpicture}
|
|
334 |
\end{center}
|
|
335 |
|
|
336 |
\noindent I highlighted the \emph{keys} in this map. Since
|
|
337 |
there are three labels in the factorial program (remember we
|
|
338 |
added \pcode{""}), there are three keys. When running the
|
|
339 |
factorial program and encountering a jump, then we only have
|
|
340 |
to consult this snippets-map in order to find out what the
|
|
341 |
next statements should be.
|
|
342 |
|
|
343 |
We should now be in the position to define how a program
|
|
344 |
should be run. In the context of interpreters, this
|
|
345 |
``running'' of programs is often called \emph{evaluation}. Let
|
|
346 |
us start with the definition of how expressions are evaluated.
|
|
347 |
A first attempt might be the following recursive function:
|
|
348 |
|
|
349 |
\begin{center}
|
|
350 |
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
|
|
351 |
$\textit{eval\_exp}(\texttt{n})$ & $\dn$ & $n$
|
|
352 |
\qquad\text{if}\; \texttt{n}\; \text{is a number like}
|
|
353 |
\ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1},
|
|
354 |
\pcode{2}\ldots{}\\
|
|
355 |
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{+}\,
|
|
356 |
\texttt{e}_\texttt{2})$ &
|
|
357 |
$\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}) +
|
|
358 |
\textit{eval\_exp}(\texttt{e}_\texttt{2})$\\
|
|
359 |
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{*}\,
|
|
360 |
\texttt{e}_\texttt{2})$ &
|
|
361 |
$\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}) *
|
|
362 |
\textit{eval\_exp}(\texttt{e}_\texttt{2})$\\
|
|
363 |
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{=}\,
|
|
364 |
\texttt{e}_\texttt{2})$ &
|
|
365 |
$\dn$ & $\begin{cases}
|
|
366 |
1 & \text{if}\;\textit{eval\_exp}(\texttt{e}_\texttt{1}) =
|
|
367 |
\textit{eval\_exp}(\texttt{e}_\texttt{2})\\
|
|
368 |
0 & \text{otherwise}
|
|
369 |
\end{cases}$
|
|
370 |
\end{tabular}
|
|
371 |
\end{center}
|
|
372 |
|
|
373 |
\noindent While this should look all rather intuitive`, still
|
|
374 |
be very careful. There is a subtlety which can be easily
|
|
375 |
overlooked: The function \textit{eval\_exp} takes an
|
|
376 |
expression of our programming language as input and returns a
|
|
377 |
number as output. Therefore whenever we encounter a number in
|
|
378 |
our program, we just return this number---this is defined in
|
|
379 |
the first clause above. Whenever we encounter an addition,
|
|
380 |
well then we first evaluate the left-hand side
|
|
381 |
$\texttt{e}_\texttt{1}$ of the addition (this will give a
|
|
382 |
number), then evaluate the right-hand side
|
|
383 |
$\texttt{e}_\texttt{2}$ (this gives another number), and
|
|
384 |
finally add both numbers together. Here is the subtlety: on
|
|
385 |
the left-hand side of the $\dn$ we have a \texttt{+} (in the
|
|
386 |
teletype font) which is the symbol for addition in our
|
|
387 |
programming language. On the right-hand side we have $+$ which
|
|
388 |
stands for the arithmetic operation from ``mathematics'' of
|
|
389 |
adding two numbers. These are rather different concepts---one
|
|
390 |
is a symbol (which we made up), and the other a mathematical
|
|
391 |
operation. When we will have a look at an actual
|
|
392 |
implementation of our interpreter, the mathematical operation
|
|
393 |
will be the function for addition from the programming
|
|
394 |
language in which we \underline{\smash{implement}} our
|
|
395 |
interpreter. While the \texttt{+} is just a symbol that is
|
|
396 |
used in our programming language. Clearly we have to use a
|
|
397 |
symbol that is a good mnemonic for addition, otherwise we will
|
|
398 |
confuse the programmers working with our language. Therefore
|
|
399 |
we use $\texttt{+}$. A similar choice is made for times in the
|
|
400 |
third clause and equality in the fourth clause.
|
|
401 |
|
|
402 |
Remember I wrote at the beginning of this section about being
|
|
403 |
god when designing a programming language. You can see this
|
|
404 |
here: we need to give meaning to symbols. At the moment
|
|
405 |
however, we are a poor fallible god. Look again at the grammar
|
|
406 |
of our programming language and our definition. Clearly, an
|
|
407 |
expression can contain variables. So far we have ignored them.
|
|
408 |
What should our interpreter do with variables? They might
|
|
409 |
change during the evaluation of a program. For example the
|
|
410 |
variable \pcode{n} in the factorial program counts down from 5
|
|
411 |
down to 0. How can we improve our definition above to give also
|
|
412 |
an answer whenever our interpreter encounters a variable in an
|
|
413 |
expression? The solution is to add an \emph{environment},
|
|
414 |
written $env$, as an additional input argument to our
|
|
415 |
\textit{eval\_exp} function.
|
|
416 |
|
|
417 |
\begin{center}
|
|
418 |
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
|
|
419 |
$\textit{eval\_exp}(\texttt{n}, env)$ & $\dn$ & $n$
|
|
420 |
\qquad\text{if}\; \texttt{n}\; \text{is a number like}
|
|
421 |
\ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1},
|
|
422 |
\pcode{2}\ldots{}\\
|
|
423 |
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{+}\,
|
|
424 |
\texttt{e}_\texttt{2}, env)$ &
|
|
425 |
$\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}, env) +
|
|
426 |
\textit{eval\_exp}(\texttt{e}_\texttt{2}, env)$\\
|
|
427 |
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{*}\,
|
|
428 |
\texttt{e}_\texttt{2}, env)$ &
|
|
429 |
$\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}, env) *
|
|
430 |
\textit{eval\_exp}(\texttt{e}_\texttt{2}, env)$\\
|
|
431 |
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{=}\,
|
|
432 |
\texttt{e}_\texttt{2}, env)$ &
|
|
433 |
$\dn$ & $\begin{cases}
|
|
434 |
1 & \text{if}\;\textit{eval\_exp}(\texttt{e}_\texttt{1}, env) =
|
|
435 |
\textit{eval\_exp}(\texttt{e}_\texttt{2}, env)\\
|
|
436 |
0 & \text{otherwise}
|
|
437 |
\end{cases}$\\
|
|
438 |
$\textit{eval\_exp}(\texttt{x}, env)$ & $\dn$ & $env(x)$
|
|
439 |
\end{tabular}
|
|
440 |
\end{center}
|
|
441 |
|
|
442 |
\noindent This environment $env$ also acts like a map: it
|
|
443 |
associates variables with their current values. For example
|
|
444 |
after evaluating the first two lines in our factorial
|
|
445 |
program, such an environment might look as follows
|
|
446 |
|
|
447 |
\begin{center}
|
|
448 |
\begin{tabular}{ll}
|
|
449 |
$\fbox{\texttt{a}} \mapsto \texttt{1}$ &
|
|
450 |
$\fbox{\texttt{n}} \mapsto \texttt{5}$
|
|
451 |
\end{tabular}
|
|
452 |
\end{center}
|
|
453 |
|
|
454 |
\noindent Again I highlighted the keys. In the clause for
|
|
455 |
variables, we can therefore consult this environment and
|
|
456 |
return whatever value is currently stored for this variable.
|
|
457 |
This is written $env(x)$---meaning we query this map with $x$
|
|
458 |
we obtain the corresponding number. You might ask what happens
|
|
459 |
if an environment does not contain any value for, say, the
|
|
460 |
variable $x$? Well, then our interpreter just ``crashes'', or
|
|
461 |
more precisely will raise an exception. In this case we have a
|
|
462 |
``bad'' program that tried to use a variable before it was
|
|
463 |
initialised. The programmer should not have done this. In a
|
|
464 |
real programming language, we would of course try a bit harder
|
|
465 |
and for example give an error at compile time, or design our
|
|
466 |
language in such a way that this can never happen. With the
|
|
467 |
second version of \textit{eval\_exp} we completed our
|
|
468 |
definition for evaluating expressions.
|
|
469 |
|
|
470 |
Next comes the evaluation function for statements. We define
|
|
471 |
this function in such a way that we recursively evaluate a
|
|
472 |
whole sequence of statements. Assume a program $p$ (you want
|
|
473 |
to evaluate) and its pre-processed snippets $sn$. Then we can
|
|
474 |
define:
|
|
475 |
|
|
476 |
\begin{center}
|
|
477 |
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
|
|
478 |
$\textit{eval\_stmts}([], env)$ & $\dn$ & $env$\\
|
|
479 |
$\textit{eval\_stmts}(\texttt{label:}\;rest, env)$ &
|
|
480 |
$\dn$ & $\textit{eval\_stmts}(rest, env)$ \\
|
|
481 |
$\textit{eval\_stmts}(\texttt{x\,:=\,e}\;rest, env)$ &
|
|
482 |
$\dn$ & $\textit{eval\_stmts}(rest,
|
|
483 |
env[x := \textit{eval\_exp}(\texttt{e}, env)])$\\
|
|
484 |
$\textit{eval\_stmts}(\texttt{goto\,lbl}\;rest, env)$
|
|
485 |
& $\dn$ & $\textit{eval\_stmts}(sn(\texttt{lbl}), env)$\\
|
|
486 |
$\textit{eval\_stmts}(\texttt{jmp?\,e\,lbl}\;rest, env)$
|
|
487 |
& $\dn$ & $\begin{cases}\begin{array}{@{}l@{\hspace{-12mm}}r@{}}
|
|
488 |
\textit{eval\_stmts}(sn(\texttt{lbl}), env)\\
|
|
489 |
& \text{if}\;\textit{eval\_exp}(\texttt{e}, env) = 1\\
|
|
490 |
\textit{eval\_stmts}(rest, env) & \text{otherwise}\\
|
|
491 |
\end{array}\end{cases}$
|
|
492 |
\end{tabular}
|
|
493 |
\end{center}
|
|
494 |
|
|
495 |
\noindent The first clause is for the empty program, or when
|
|
496 |
we arrived at the end of the program. In this case we just
|
|
497 |
return the environment. The second clause is for when the next
|
|
498 |
statement is a label. That means the program is of the form
|
|
499 |
$\texttt{label:}\;rest$ where the label is some string and
|
|
500 |
$rest$ stands for all following statements. This case is easy,
|
|
501 |
because our evaluation function just discards the label and
|
|
502 |
evaluates the rest of the statements (we already extracted all
|
|
503 |
important information about labels when we pre-processed our
|
|
504 |
programs and generated the snippets). The third clause is for
|
|
505 |
variable assignments. Again we just evaluate the rest for the
|
|
506 |
statements, but with a modified environment---since the
|
|
507 |
variable assignment is supposed to introduce a new variable or
|
|
508 |
change the current value of a variable. For this modification
|
|
509 |
of the environment we first evaluate the expression
|
|
510 |
$\texttt{e}$ using our evaluation function for expressions.
|
|
511 |
This gives us a number. Then we assign this number to the
|
|
512 |
variable $x$ in the environment. This modified environment
|
|
513 |
will be used to evaluate the rest of the program. The fourth
|
|
514 |
clause is for the unconditional jump to a label, called
|
|
515 |
\texttt{lbl}. That means we have to look up in our snippets
|
|
516 |
map $sn$ what are the next statements for this label.
|
|
517 |
Therefore we will continue with evaluating, not with the rest
|
|
518 |
of the program, but with the statements stored in the
|
|
519 |
snippets-map under the label $\texttt{lbl}$. The fifth clause
|
|
520 |
for conditional jumps is similar, but to decide whether to
|
|
521 |
make the jump we first need to evaluate the expression
|
|
522 |
$\texttt{e}$ in order to find out whether it is $1$. If yes,
|
|
523 |
we jump, otherwise we just continue with evaluating the rest
|
|
524 |
of the program.
|
|
525 |
|
|
526 |
Our interpreter works in two stages: First we pre-process our
|
|
527 |
program generating the snippets map $sn$, say. Second we call
|
|
528 |
the evaluation function with the default entry point and the
|
|
529 |
empty environment:
|
|
530 |
|
|
531 |
\begin{center}
|
|
532 |
$\textit{eval\_stmts}(sn(\pcode{""}), \varnothing)$
|
|
533 |
\end{center}
|
|
534 |
|
|
535 |
\noindent It is interesting to note that our interpreter when
|
|
536 |
it comes to the end of the program returns an environment. Our
|
|
537 |
programming language does not contain any constructs for input
|
|
538 |
and output. Therefore this environment is the only effect we
|
|
539 |
can observe when running the program (apart from that our
|
|
540 |
interpreter might need some time before finishing the
|
|
541 |
evaluation of the program and the CPU getting hot). Evaluating
|
|
542 |
the factorial program with our interpreter we receive as
|
|
543 |
``answer''-environment
|
|
544 |
|
|
545 |
\begin{center}
|
|
546 |
\begin{tabular}{ll}
|
|
547 |
$\fbox{\texttt{a}} \mapsto \texttt{120}$ &
|
|
548 |
$\fbox{\texttt{n}} \mapsto \texttt{0}$
|
|
549 |
\end{tabular}
|
|
550 |
\end{center}
|
|
551 |
|
|
552 |
\noindent While the discussion above should have illustrated
|
|
553 |
the ideas, in order to do some serious calculations, we clearly
|
|
554 |
need to implement the interpreter.
|
|
555 |
|
|
556 |
|
|
557 |
\subsubsection*{Scala Code for the Interpreter}
|
|
558 |
|
|
559 |
Functional programming languages are very convenient for
|
|
560 |
implementations of interpreters. A good choice for a
|
|
561 |
functional programming language is Scala, a programming
|
|
562 |
language that combines functional and object-oriented
|
|
563 |
pro\-gramming-styles. It has received in the last five years or
|
|
564 |
so quite a bit of attention. One reason for this attention is
|
|
565 |
that, like the Java programming language, Scala compiles to
|
|
566 |
the Java Virtual Machine (JVM) and therefore Scala programs
|
|
567 |
can run under MacOSX, Linux and Windows.\footnote{There are
|
|
568 |
also experimental backends for Android and JavaScript.} Unlike
|
|
569 |
Java, however, Scala often allows programmers to write very
|
|
570 |
concise and elegant code. Some therefore say Scala is the much
|
|
571 |
better Java. A number of companies, The Guardian, Twitter,
|
|
572 |
Coursera, FourSquare, LinkedIn to name a few, either use Scala
|
|
573 |
exclusively in production code, or at least to some
|
|
574 |
substantial degree. If you want to try out Scala yourself, the
|
|
575 |
Scala compiler can be downloaded from
|
|
576 |
|
|
577 |
\begin{quote}
|
|
578 |
\url{http://www.scala-lang.org}
|
|
579 |
\end{quote}
|
|
580 |
|
|
581 |
Let us have a look at the Scala code shown in
|
|
582 |
Figure~\ref{code}. It shows the entire code for the
|
|
583 |
interpreter, though the implementation is admittedly no
|
|
584 |
frills.
|
|
585 |
|
|
586 |
\begin{figure}[t]
|
|
587 |
\small
|
|
588 |
\lstinputlisting[language=Scala]{../progs/inter.scala}
|
|
589 |
\caption{The entire code of the interpreter for our
|
|
590 |
idealised programming language.\label{code}}
|
|
591 |
\end{figure}
|
|
592 |
|
|
593 |
|
|
594 |
\subsubsection*{Static Analysis}
|
|
595 |
|
|
596 |
Finally we can come back to our original problem, namely
|
|
597 |
finding out what the signs of variables are
|
|
598 |
|
|
599 |
\begin{center}
|
|
600 |
|
|
601 |
|
|
602 |
\end{center}
|
|
603 |
|
|
604 |
\end{document}
|
|
605 |
|
|
606 |
%% list of static analysers for C
|
|
607 |
http://spinroot.com/static/index.html
|
|
608 |
|
|
609 |
%% NASA coding rules for C
|
|
610 |
http://pixelscommander.com/wp-content/uploads/2014/12/P10.pdf
|
|
611 |
|
|
612 |
%%% Local Variables:
|
|
613 |
%%% mode: latex
|
|
614 |
%%% TeX-master: t
|
|
615 |
%%% End:
|