|
@@ -2045,8 +2045,8 @@ $\Rightarrow$
|
|
|
&
|
|
|
\begin{minipage}{0.4\textwidth}
|
|
|
\begin{lstlisting}
|
|
|
-movq $10 x
|
|
|
-addq $32 x
|
|
|
+movq $10, x
|
|
|
+addq $32, x
|
|
|
\end{lstlisting}
|
|
|
\end{minipage}
|
|
|
\end{tabular} \\
|
|
@@ -2068,7 +2068,7 @@ $\Rightarrow$
|
|
|
&
|
|
|
\begin{minipage}{0.4\textwidth}
|
|
|
\begin{lstlisting}
|
|
|
-addq $10 x
|
|
|
+addq $10, x
|
|
|
\end{lstlisting}
|
|
|
\end{minipage}
|
|
|
\end{tabular} \\
|
|
@@ -2099,7 +2099,7 @@ $\Rightarrow$
|
|
|
\begin{minipage}{0.4\textwidth}
|
|
|
\begin{lstlisting}
|
|
|
callq read_int
|
|
|
-movq %rax |$\itm{lhs}$|
|
|
|
+movq %rax, |$\itm{lhs}$|
|
|
|
\end{lstlisting}
|
|
|
\end{minipage}
|
|
|
\end{tabular} \\
|
|
@@ -2121,6 +2121,7 @@ interesting code in this pass. Use the \key{interp-tests} function
|
|
|
your passes on the example programs.
|
|
|
\end{exercise}
|
|
|
|
|
|
+
|
|
|
\section{Assign Homes}
|
|
|
\label{sec:assign-r1}
|
|
|
|
|
@@ -2129,18 +2130,18 @@ $\text{x86}^{*}_0$ programs that no longer use program variables.
|
|
|
Thus, the \key{assign-homes} pass is responsible for placing all of
|
|
|
the program variables in registers or on the stack. For runtime
|
|
|
efficiency, it is better to place variables in registers, but as there
|
|
|
-are only 16 registers, some programs must necessarily place some
|
|
|
-variables on the stack. In this chapter we focus on the mechanics of
|
|
|
-placing variables on the stack. We study an algorithm for placing
|
|
|
-variables in registers in Chapter~\ref{ch:register-allocation-r1}.
|
|
|
+are only 16 registers, some programs must necessarily resort to
|
|
|
+placing some variables on the stack. In this chapter we focus on the
|
|
|
+mechanics of placing variables on the stack. We study an algorithm for
|
|
|
+placing variables in registers in
|
|
|
+Chapter~\ref{ch:register-allocation-r1}.
|
|
|
|
|
|
Consider again the following $R_1$ program.
|
|
|
% s0_20.rkt
|
|
|
\begin{lstlisting}
|
|
|
-(program ()
|
|
|
- (let ([a 42])
|
|
|
- (let ([b a])
|
|
|
- b)))
|
|
|
+(let ([a 42])
|
|
|
+ (let ([b a])
|
|
|
+ b))
|
|
|
\end{lstlisting}
|
|
|
For reference, we repeat the output of \code{select-instructions} on
|
|
|
the left and show the output of \code{assign-homes} on the right.
|
|
@@ -2152,32 +2153,30 @@ this example, we assign variable \code{a} to stack location
|
|
|
\begin{tabular}{l}
|
|
|
\begin{minipage}{0.4\textwidth}
|
|
|
\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
|
|
|
-(program ((locals . (a b)))
|
|
|
- ((start .
|
|
|
- (block ()
|
|
|
- (movq (int 42) (var a))
|
|
|
- (movq (var a) (var b))
|
|
|
- (movq (var b) (reg rax))
|
|
|
- (jmp conclusion)))))
|
|
|
+locals: a b
|
|
|
+start:
|
|
|
+ movq $42, a
|
|
|
+ movq a, b
|
|
|
+ movq b, %rax
|
|
|
+ jmp conclusion
|
|
|
\end{lstlisting}
|
|
|
\end{minipage}
|
|
|
{$\Rightarrow$}
|
|
|
\begin{minipage}{0.4\textwidth}
|
|
|
\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
|
|
|
-(program ((stack-space . 16))
|
|
|
- ((start .
|
|
|
- (block ()
|
|
|
- (movq (int 42) (deref rbp -8))
|
|
|
- (movq (deref rbp -8) (deref rbp -16))
|
|
|
- (movq (deref rbp -16) (reg rax))
|
|
|
- (jmp conclusion)))))
|
|
|
+stack-space: 16
|
|
|
+start:
|
|
|
+ movq $42, -8(%rbp)
|
|
|
+ movq -8(%rbp), -16(%rbp)
|
|
|
+ movq -16(%rbp), %rax
|
|
|
+ jmp conclusion
|
|
|
\end{lstlisting}
|
|
|
\end{minipage}
|
|
|
\end{tabular} \\
|
|
|
|
|
|
In the process of assigning variables to stack locations, it is
|
|
|
convenient to compute and store the size of the frame (in bytes) in
|
|
|
-the $\itm{info}$ field of the \key{program} node, with the key
|
|
|
+the $\itm{info}$ field of the \key{Program} node, with the key
|
|
|
\code{stack-space}, which will be needed later to generate the
|
|
|
procedure conclusion. Some operating systems place restrictions on
|
|
|
the frame size. For example, Mac OS X requires the frame size to be a
|
|
@@ -2192,6 +2191,7 @@ mapping of variable names to homes (stack locations for now). Use the
|
|
|
\key{utilities.rkt} to test your passes on the example programs.
|
|
|
\end{exercise}
|
|
|
|
|
|
+
|
|
|
\section{Patch Instructions}
|
|
|
\label{sec:patch-s0}
|
|
|
|
|
@@ -2212,13 +2212,12 @@ After the \key{assign-homes} pass, the above program has been translated to
|
|
|
the following. \\
|
|
|
\begin{minipage}{0.5\textwidth}
|
|
|
\begin{lstlisting}
|
|
|
-(program ((stack-space . 16))
|
|
|
- ((start .
|
|
|
- (block ()
|
|
|
- (movq (int 42) (deref rbp -8))
|
|
|
- (movq (deref rbp -8) (deref rbp -16))
|
|
|
- (movq (deref rbp -16) (reg rax))
|
|
|
- (jmp conclusion)))))
|
|
|
+stack-space: 16
|
|
|
+start:
|
|
|
+ movq $42, -8(%rbp)
|
|
|
+ movq -8(%rbp), -16(%rbp)
|
|
|
+ movq -16(%rbp), %rax
|
|
|
+ jmp conclusion
|
|
|
\end{lstlisting}
|
|
|
\end{minipage}\\
|
|
|
The second \key{movq} instruction is problematic because both
|
|
@@ -2226,8 +2225,8 @@ arguments are stack locations. We suggest fixing this problem by
|
|
|
moving from the source location to the register \key{rax} and then
|
|
|
from \key{rax} to the destination location, as follows.
|
|
|
\begin{lstlisting}
|
|
|
- (movq (deref rbp -8) (reg rax))
|
|
|
- (movq (reg rax) (deref rbp -16))
|
|
|
+ movq -8(%rbp), %rax
|
|
|
+ movq %rax, -16(%rbp)
|
|
|
\end{lstlisting}
|
|
|
|
|
|
\begin{exercise}
|