author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Mon, 16 Nov 2015 15:16:17 +0000 | |
changeset 370 | a65767fe5d71 |
parent 369 | 43c0ed473720 |
child 372 | d6af4b1239de |
permissions | -rw-r--r-- |
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\usepackage{../style} |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
\usepackage{../langs} |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
4 |
\usepackage{../grammar} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
5 |
\usepackage{tikz-qtree} |
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
\begin{document} |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
9 |
\section*{Handout 7 (Compilation of the WHILE-Language)} |
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
|
369
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
11 |
The purpose of a compiler is to transform a program, a human |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
12 |
can write, into code the machine can run as fast as possible. |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
13 |
The fastest code would be machine code the CPU can run |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
14 |
directly, but it is often enough to improve the speed of a |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
15 |
program by just targeting a virtual machine. This produces not |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
16 |
the fastest possible code, but code that is fast enough and |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
17 |
has the advantage that the virtual machine care of things a |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
18 |
compiler would normally need to take care of (like explicit |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
19 |
memory management). |
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
|
369
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
21 |
We will be generating code for the Java Virtual Machine. This |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
22 |
is a stack-based virtual machine which will make it easy to |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
23 |
generate code for arithmetic expressions. For example for |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
24 |
generating code for the expression $1 + 2$ we need to issue |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
25 |
the following three instructions |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
26 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
27 |
\begin{lstlisting}[numbers=none] |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
28 |
ldc 1 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
29 |
ldc 2 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
30 |
iadd |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
31 |
\end{lstlisting} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
32 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
33 |
\noindent The first instruction loads the constant $1$ on the |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
34 |
stack, the next one $2$, the third instruction add both |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
35 |
numbers together replacing the top elements of the stack with |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
36 |
the result $3$. We will throughout consider only integer |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
37 |
numbers and results, therefore we can use the instructions |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
38 |
\code{iadd}, \code{isub}, \code{imul}, \code{idiv} and so on. |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
39 |
The \code{i} stands for integer instructions (alternatives are |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
40 |
\code{d} for doubles, \code{l} for longs and \code{f} for |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
41 |
floats). |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
42 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
43 |
Recall our grammar for arithmetic expressions: |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
44 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
45 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
46 |
\begin{plstx}[rhs style=, margin=3cm] : \meta{E} ::= \meta{T} $+$ \meta{E} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
47 |
| \meta{T} $-$ \meta{E} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
48 |
| \meta{T}\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
49 |
: \meta{T} ::= \meta{F} $*$ \meta{T} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
50 |
| \meta{F} $\backslash$ \meta{T} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
51 |
| \meta{F}\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
52 |
: \meta{F} ::= ( \meta{E} ) |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
53 |
| \meta{Id} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
54 |
| \meta{Num}\\ \end{plstx} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
55 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
56 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
57 |
\noindent where \meta{Id} stands for variables and |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
58 |
\meta{Num} for number. For the moment let us omit variables from |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
59 |
arithmetic expressions. Our parser will take this grammar and |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
60 |
produce abstract syntax trees, for |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
61 |
example for the expression $1 + ((2 * 3) + (4 - 3))$ it will |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
62 |
produce the following tree. |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
63 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
64 |
\begin{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
65 |
\begin{tikzpicture} \Tree [.$+$ [.$1$ ] [.$+$ [.$*$ $2$ $3$ ] [.$-$ $4$ $3$ ]]] \end{tikzpicture} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
66 |
\end{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
67 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
68 |
\noindent |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
69 |
To generate code for this expression, we need to traverse this |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
70 |
tree in post-order fashion---this will produce code for a |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
71 |
stack-machine, like the JVM. Doing so gives the instructions |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
72 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
73 |
\begin{lstlisting}[numbers=none] |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
74 |
ldc 1 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
75 |
ldc 2 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
76 |
ldc 3 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
77 |
imul |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
78 |
ldc 4 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
79 |
ldc 3 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
80 |
isub |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
81 |
iadd |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
82 |
iadd |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
83 |
\end{lstlisting} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
84 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
85 |
\noindent If we ``run'' these instructions, the result $8$ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
86 |
will be on top of the stack. This will be a convention we |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
87 |
always observe, namely that the results of arithmetic |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
88 |
expressions will always be on top of the stack. Note, that a |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
89 |
different bracketing, for example $(1 + (2 * 3)) + (4 - 3)$, |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
90 |
produces a different abstract syntax tree and thus potentially |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
91 |
also a different list of instructions. Generating code in this |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
92 |
fashion is rather simple: it can be done by the following |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
93 |
\textit{compile}-function: |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
94 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
95 |
\begin{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
96 |
\begin{tabular}{lcl} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
97 |
$\textit{compile}(n)$ & $\dn$ & $\pcode{ldc}\; n$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
98 |
$\textit{compile}(a_1 + a_2)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
99 |
$\textit{compile}(a_1) \;@\;\textit{compile}(a_2)\;@\; \pcode{iadd}$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
100 |
$\textit{compile}(a_1 - a_2)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
101 |
$\textit{compile}(a_1) \;@\; \textit{compile}(a_2)\;@\; \pcode{isub}$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
102 |
$\textit{compile}(a_1 * a_2)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
103 |
$\textit{compile}(a_1) \;@\; \textit{compile}(a_2)\;@\; \pcode{imul}$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
104 |
$\textit{compile}(a_1 \backslash a_2)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
105 |
$\textit{compile}(a_1) \;@\; \textit{compile}(a_2)\;@\; \pcode{idiv}$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
106 |
\end{tabular} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
107 |
\end{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
108 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
109 |
\noindent However, our arithmetic expressions can also contain |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
110 |
variables. We will represent them as \emph{local variables} in |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
111 |
the JVM. Essentially, local variables are an array or pointers |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
112 |
to the memory containing in our case only integers. Looking up |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
113 |
a variable can be done by the instruction |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
114 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
115 |
\begin{lstlisting}[mathescape,numbers=none] |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
116 |
iload $index$ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
117 |
\end{lstlisting} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
118 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
119 |
\noindent |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
120 |
which places the content of the local variable $index$ onto |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
121 |
thestack. Storing the top of the stack into a local variable |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
122 |
can be done by the instruction |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
123 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
124 |
\begin{lstlisting}[mathescape,numbers=none] |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
125 |
istore $index$ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
126 |
\end{lstlisting} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
127 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
128 |
\noindent Note that this also pops off the top of the stack. |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
129 |
One problem we have to overcome is that local variables are |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
130 |
addressed, not by identifiers, but by numbers (starting from |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
131 |
$0$). Therefore our compiler needs to maintain a kind of |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
132 |
environment (similar to the interpreter) where variables are |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
133 |
associated to numbers. This association needs to be unique: if |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
134 |
we muddle up the numbers, then we essentially confuse |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
135 |
variables and the result will usually be an erroneous result. |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
136 |
Therefore our \textit{compile}-function will take two |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
137 |
arguments: the abstract syntax tree and the environment, $E$, |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
138 |
that maps identifiers to index-numbers. |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
139 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
140 |
\begin{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
141 |
\begin{tabular}{lcl} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
142 |
$\textit{compile}(n, E)$ & $\dn$ & $\pcode{ldc}\;n$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
143 |
$\textit{compile}(a_1 + a_2, E)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
144 |
$\textit{compile}(a_1, E) \;@\;\textit{compile}(a_2, E)\;@\; \pcode{iadd}$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
145 |
$\textit{compile}(a_1 - a_2, E)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
146 |
$\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{isub}$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
147 |
$\textit{compile}(a_1 * a_2, E)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
148 |
$\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{imul}$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
149 |
$\textit{compile}(a_1 \backslash a_2, E)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
150 |
$\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{idiv}$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
151 |
$\textit{compile}(x, E)$ & $\dn$ & $\pcode{iload}\;E(x)$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
152 |
\end{tabular} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
153 |
\end{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
154 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
155 |
\noindent In the last line we generate the code for variables |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
156 |
where $E(x)$ stands for looking up to which index the variable |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
157 |
$x$ maps to. |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
158 |
|
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
159 |
|
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
160 |
\end{document} |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
161 |
|
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
162 |
%%% Local Variables: |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
163 |
%%% mode: latex |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
164 |
%%% TeX-master: t |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
%%% End: |