浏览代码

updated the rest of the examples in chap 3

Jeremy Siek 9 年之前
父节点
当前提交
11b6981567
共有 1 个文件被更改,包括 149 次插入59 次删除
  1. 149 59
      book.tex

+ 149 - 59
book.tex

@@ -1859,7 +1859,7 @@ Source program:
   (let ([x (+ v 7)])
   (let ([x (+ v 7)])
   (let ([y (+ 4 x)])
   (let ([y (+ 4 x)])
   (let ([z (+ x w)])
   (let ([z (+ x w)])
-       (- z y)))))))
+       (+ z (- y))))))))
 \end{lstlisting}
 \end{lstlisting}
 \end{minipage}
 \end{minipage}
 \begin{minipage}{0.45\textwidth}
 \begin{minipage}{0.45\textwidth}
@@ -2262,6 +2262,9 @@ with a dash for their color and an empty set for the saturation.
 \node (x) at (6,0)    {$x:-,\{\}$};
 \node (x) at (6,0)    {$x:-,\{\}$};
 \node (y) at (3,-1.5) {$y:-,\{\}$};
 \node (y) at (3,-1.5) {$y:-,\{\}$};
 \node (z) at (6,-1.5) {$z:-,\{\}$};
 \node (z) at (6,-1.5) {$z:-,\{\}$};
+\node (t1) at (9,0)   {$t.1:-,\{\}$};
+\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+
 \draw (v) to (w);
 \draw (v) to (w);
 \foreach \i in {w,x,y} 
 \foreach \i in {w,x,y} 
 {
 {
@@ -2272,10 +2275,12 @@ with a dash for their color and an empty set for the saturation.
 }
 }
 \draw (z) to (w);
 \draw (z) to (w);
 \draw (z) to (y);
 \draw (z) to (y);
+\draw (t1) to (z);
+\draw (t2) to (t1);
 \end{tikzpicture}
 \end{tikzpicture}
 \]
 \]
 We select a maximally saturated node and color it $0$. In this case we
 We select a maximally saturated node and color it $0$. In this case we
-have a 5-way tie, so we arbitrarily pick $y$. The then mark color $0$
+have a 7-way tie, so we arbitrarily pick $y$. The then mark color $0$
 as no longer available for $w$, $x$, and $z$ because they interfere
 as no longer available for $w$, $x$, and $z$ because they interfere
 with $y$.
 with $y$.
 \[
 \[
@@ -2285,6 +2290,8 @@ with $y$.
 \node (x) at (6,0)    {$x:-,\{0\}$};
 \node (x) at (6,0)    {$x:-,\{0\}$};
 \node (y) at (3,-1.5) {$y:0,\{\}$};
 \node (y) at (3,-1.5) {$y:0,\{\}$};
 \node (z) at (6,-1.5) {$z:-,\{0\}$};
 \node (z) at (6,-1.5) {$z:-,\{0\}$};
+\node (t1) at (9,0)   {$t.1:-,\{\}$};
+\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
 \draw (v) to (w);
 \draw (v) to (w);
 \foreach \i in {w,x,y} 
 \foreach \i in {w,x,y} 
 {
 {
@@ -2295,6 +2302,8 @@ with $y$.
 }
 }
 \draw (z) to (w);
 \draw (z) to (w);
 \draw (z) to (y);
 \draw (z) to (y);
+\draw (t1) to (z);
+\draw (t2) to (t1);
 \end{tikzpicture}
 \end{tikzpicture}
 \]
 \]
 Now we repeat the process, selecting another maximally saturated node.
 Now we repeat the process, selecting another maximally saturated node.
@@ -2307,6 +2316,10 @@ $w$ with $1$.
 \node (x) at (6,0)    {$x:-,\{0,1\}$};
 \node (x) at (6,0)    {$x:-,\{0,1\}$};
 \node (y) at (3,-1.5) {$y:0,\{1\}$};
 \node (y) at (3,-1.5) {$y:0,\{1\}$};
 \node (z) at (6,-1.5) {$z:-,\{0,1\}$};
 \node (z) at (6,-1.5) {$z:-,\{0,1\}$};
+\node (t1) at (9,0)   {$t.1:-,\{\}$};
+\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\draw (t1) to (z);
+\draw (t2) to (t1);
 \draw (v) to (w);
 \draw (v) to (w);
 \foreach \i in {w,x,y} 
 \foreach \i in {w,x,y} 
 {
 {
@@ -2328,6 +2341,10 @@ next available color which is $2$.
 \node (x) at (6,0)    {$x:2,\{0,1\}$};
 \node (x) at (6,0)    {$x:2,\{0,1\}$};
 \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
 \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
 \node (z) at (6,-1.5) {$z:-,\{0,1\}$};
 \node (z) at (6,-1.5) {$z:-,\{0,1\}$};
+\node (t1) at (9,0)   {$t.1:-,\{\}$};
+\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\draw (t1) to (z);
+\draw (t2) to (t1);
 \draw (v) to (w);
 \draw (v) to (w);
 \foreach \i in {w,x,y} 
 \foreach \i in {w,x,y} 
 {
 {
@@ -2340,8 +2357,7 @@ next available color which is $2$.
 \draw (z) to (y);
 \draw (z) to (y);
 \end{tikzpicture}
 \end{tikzpicture}
 \]
 \]
-We have only two nodes left to color, $v$ and $z$, but $z$ is
-more highly saturated, so we color $z$ with $2$.
+Node $z$ is the next most highly saturated, so we color $z$ with $2$.
 \[
 \[
 \begin{tikzpicture}[baseline=(current  bounding  box.center)]
 \begin{tikzpicture}[baseline=(current  bounding  box.center)]
 \node (v) at (0,0)   {$v:-,\{1\}$};
 \node (v) at (0,0)   {$v:-,\{1\}$};
@@ -2349,6 +2365,10 @@ more highly saturated, so we color $z$ with $2$.
 \node (x) at (6,0)   {$x:2,\{0,1\}$};
 \node (x) at (6,0)   {$x:2,\{0,1\}$};
 \node (y) at (3,-1.5)  {$y:0,\{1,2\}$};
 \node (y) at (3,-1.5)  {$y:0,\{1,2\}$};
 \node (z) at (6,-1.5)  {$z:2,\{0,1\}$};
 \node (z) at (6,-1.5)  {$z:2,\{0,1\}$};
+\node (t1) at (9,0)   {$t.1:-,\{2\}$};
+\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\draw (t1) to (z);
+\draw (t2) to (t1);
 \draw (v) to (w);
 \draw (v) to (w);
 \foreach \i in {w,x,y} 
 \foreach \i in {w,x,y} 
 {
 {
@@ -2361,7 +2381,8 @@ more highly saturated, so we color $z$ with $2$.
 \draw (z) to (y);
 \draw (z) to (y);
 \end{tikzpicture}
 \end{tikzpicture}
 \]
 \]
-The last iteration of the coloring algorithm assigns color $0$ to $v$.
+We have a 2-way tie between $v$ and $t.1$. We choose to color $v$ with
+$0$.
 \[
 \[
 \begin{tikzpicture}[baseline=(current  bounding  box.center)]
 \begin{tikzpicture}[baseline=(current  bounding  box.center)]
 \node (v) at (0,0)   {$v:0,\{1\}$};
 \node (v) at (0,0)   {$v:0,\{1\}$};
@@ -2369,6 +2390,10 @@ The last iteration of the coloring algorithm assigns color $0$ to $v$.
 \node (x) at (6,0)   {$x:2,\{0,1\}$};
 \node (x) at (6,0)   {$x:2,\{0,1\}$};
 \node (y) at (3,-1.5)  {$y:0,\{1,2\}$};
 \node (y) at (3,-1.5)  {$y:0,\{1,2\}$};
 \node (z) at (6,-1.5)  {$z:2,\{0,1\}$};
 \node (z) at (6,-1.5)  {$z:2,\{0,1\}$};
+\node (t1) at (9,0)   {$t.1:-,\{2\}$};
+\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\draw (t1) to (z);
+\draw (t2) to (t1);
 \draw (v) to (w);
 \draw (v) to (w);
 \foreach \i in {w,x,y} 
 \foreach \i in {w,x,y} 
 {
 {
@@ -2381,6 +2406,33 @@ The last iteration of the coloring algorithm assigns color $0$ to $v$.
 \draw (z) to (y);
 \draw (z) to (y);
 \end{tikzpicture}
 \end{tikzpicture}
 \]
 \]
+In the last two steps of the algorithm, we color $t.1$ with $0$
+then $t.2$ with $1$.
+\[
+\begin{tikzpicture}[baseline=(current  bounding  box.center)]
+\node (v) at (0,0)   {$v:0,\{1\}$};
+\node (w) at (3,0)   {$w:1,\{0,2\}$};
+\node (x) at (6,0)   {$x:2,\{0,1\}$};
+\node (y) at (3,-1.5)  {$y:0,\{1,2\}$};
+\node (z) at (6,-1.5)  {$z:2,\{0,1\}$};
+\node (t1) at (9,0)   {$t.1:0,\{2,1\}$};
+\node (t2) at (9,-1.5) {$t.2:1,\{0\}$};
+\draw (t1) to (z);
+\draw (t2) to (t1);
+\draw (v) to (w);
+\foreach \i in {w,x,y} 
+{
+  \foreach \j in {w,x,y}
+  { 
+    \draw (\i) to (\j);
+  }
+}
+\draw (z) to (w);
+\draw (z) to (y);
+\end{tikzpicture}
+\]
+
+
 With the coloring complete, we can finalize the assignment of
 With the coloring complete, we can finalize the assignment of
 variables to registers and stack locations. Recall that if we have $k$
 variables to registers and stack locations. Recall that if we have $k$
 registers, we map the first $k$ colors to registers and the rest to
 registers, we map the first $k$ colors to registers and the rest to
@@ -2392,13 +2444,15 @@ following is the mapping of colors to registers and stack allocations.
 \]
 \]
 Putting this mapping together with the above coloring of the variables, we
 Putting this mapping together with the above coloring of the variables, we
 arrive at the assignment:
 arrive at the assignment:
-\[
-  \{ v \mapsto \key{\%rbx}, \;
-  w \mapsto \key{-8(\%rbp)},  \;
-  x \mapsto \key{-16(\%rbp)}, \;
-  y \mapsto \key{\%rbx},  \;
-  z\mapsto \key{-16(\%rbp)} \}
-\]
+\begin{gather*}
+  \{ v \mapsto \key{\%rbx}, \,
+  w \mapsto \key{-8(\%rbp)},  \,
+  x \mapsto \key{-16(\%rbp)}, \,
+  y \mapsto \key{\%rbx},  \,
+  z\mapsto \key{-16(\%rbp)}, \\
+  t.1\mapsto \key{\%rbx} ,\,
+  t.2\mapsto \key{-8(\%rbp)} \}
+\end{gather*}
 Applying this assignment to our running example
 Applying this assignment to our running example
 (Figure~\ref{fig:reg-eg}) yields the program on the right.
 (Figure~\ref{fig:reg-eg}) yields the program on the right.
 
 
@@ -2414,14 +2468,17 @@ Applying this assignment to our running example
     (addq (int 4) (var y))
     (addq (int 4) (var y))
     (movq (var x) (var z))
     (movq (var x) (var z))
     (addq (var w) (var z))
     (addq (var w) (var z))
-    (movq (var z) (reg rax))
-    (subq (var y) (reg rax)))
+    (movq (var y) (var t.1))
+    (negq (var t.1))
+    (movq (var z) (var t.2))
+    (addq (var t.1) (var t.2))
+    (movq (var t.2) (reg rax)))
 \end{lstlisting}
 \end{lstlisting}
 \end{minipage}
 \end{minipage}
 $\Rightarrow$
 $\Rightarrow$
 \begin{minipage}{0.45\textwidth}
 \begin{minipage}{0.45\textwidth}
 \begin{lstlisting}
 \begin{lstlisting}
-(program 32
+(program 16
   (movq (int 1) (reg rbx))
   (movq (int 1) (reg rbx))
   (movq (int 46) (stack -8))
   (movq (int 46) (stack -8))
   (movq (reg rbx) (stack -16))
   (movq (reg rbx) (stack -16))
@@ -2430,8 +2487,11 @@ $\Rightarrow$
   (addq (int 4) (reg rbx))
   (addq (int 4) (reg rbx))
   (movq (stack -16) (stack -16))
   (movq (stack -16) (stack -16))
   (addq (stack -8) (stack -16))
   (addq (stack -8) (stack -16))
-  (movq (stack -16) (reg rax))
-  (subq (reg rbx) (reg rax)))
+  (movq (reg rbx) (reg rbx))
+  (negq (reg rbx))
+  (movq (stack -16) (stack -8))
+  (addq (reg rbx) (stack -8))
+  (movq (stack -8) (reg rax)))
 \end{lstlisting}
 \end{lstlisting}
 \end{minipage}
 \end{minipage}
 
 
@@ -2566,24 +2626,30 @@ described in the last section, we get the following program.
     (addq (int 4) (var y))
     (addq (int 4) (var y))
     (movq (var x) (var z))
     (movq (var x) (var z))
     (addq (var w) (var z))
     (addq (var w) (var z))
-    (movq (var z) (reg rax))
-    (subq (var y) (reg rax)))
+    (movq (var y) (var t.1))
+    (negq (var t.1))
+    (movq (var z) (var t.2))
+    (addq (var t.1) (var t.2))
+    (movq (var t.2) (reg rax)))
 \end{lstlisting}
 \end{lstlisting}
 \end{minipage}
 \end{minipage}
 $\Rightarrow$
 $\Rightarrow$
 \begin{minipage}{0.45\textwidth}
 \begin{minipage}{0.45\textwidth}
 \begin{lstlisting}
 \begin{lstlisting}
-  (program 0
-    (movq (int 1) (reg rbx))
-    (movq (int 46) (reg rcx))
-    (movq (reg rbx) (reg rdx))
-    (addq (int 7) (reg rdx))
-    (movq (reg rdx) (reg rbx))
-    (addq (int 4) (reg rbx))
-    (movq (reg rdx) (reg rdx))
-    (addq (reg rcx) (reg rdx))
-    (movq (reg rdx) (reg rax))
-    (subq (reg rbx) (reg rax)))
+(program 0
+  (movq (int 1) (reg rbx))
+  (movq (int 46) (reg rcx))
+  (movq (reg rbx) (reg rdx))
+  (addq (int 7) (reg rdx))
+  (movq (reg rdx) (reg rbx))
+  (addq (int 4) (reg rbx))
+  (movq (reg rdx) (reg rdx))
+  (addq (reg rcx) (reg rdx))
+  (movq (reg rbx) (reg rbx))
+  (negq (reg rbx))
+  (movq (reg rdx) (reg rcx))
+  (addq (reg rbx) (reg rcx))
+  (movq (reg rcx) (reg rax)))
 \end{lstlisting}
 \end{lstlisting}
 \end{minipage}
 \end{minipage}
 
 
@@ -2612,6 +2678,10 @@ similar to how we represented interference.  The following is the
 \node (x) at (6,0)    {$x$};
 \node (x) at (6,0)    {$x$};
 \node (y) at (3,-1.5) {$y$};
 \node (y) at (3,-1.5) {$y$};
 \node (z) at (6,-1.5) {$z$};
 \node (z) at (6,-1.5) {$z$};
+\node (t1) at (9,0)   {$t.1$};
+\node (t2) at (9,-1.5) {$t.2$};
+\draw (t1) to (y);
+\draw (t2) to (z);
 \draw[bend left=20] (v) to (x);
 \draw[bend left=20] (v) to (x);
 \draw (x) to (y);
 \draw (x) to (y);
 \draw (x) to (z);
 \draw (x) to (z);
@@ -2628,6 +2698,10 @@ saturated vertex is $z$.
 \node (x) at (6,0)    {$x:2,\{0,1\}$};
 \node (x) at (6,0)    {$x:2,\{0,1\}$};
 \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
 \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
 \node (z) at (6,-1.5) {$z:-,\{0,1\}$};
 \node (z) at (6,-1.5) {$z:-,\{0,1\}$};
+\node (t1) at (9,0)   {$t.1:-,\{\}$};
+\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\draw (t1) to (z);
+\draw (t2) to (t1);
 \draw (v) to (w);
 \draw (v) to (w);
 \foreach \i in {w,x,y} 
 \foreach \i in {w,x,y} 
 {
 {
@@ -2654,6 +2728,10 @@ case choosing $2$ because that's the color of $x$.
 \node (x) at (6,0)    {$x:2,\{0,1\}$};
 \node (x) at (6,0)    {$x:2,\{0,1\}$};
 \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
 \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
 \node (z) at (6,-1.5) {$z:2,\{0,1\}$};
 \node (z) at (6,-1.5) {$z:2,\{0,1\}$};
+\node (t1) at (9,0)   {$t.1:-,\{2\}$};
+\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\draw (t1) to (z);
+\draw (t2) to (t1);
 \draw (v) to (w);
 \draw (v) to (w);
 \foreach \i in {w,x,y} 
 \foreach \i in {w,x,y} 
 {
 {
@@ -2667,9 +2745,9 @@ case choosing $2$ because that's the color of $x$.
 \end{tikzpicture}
 \end{tikzpicture}
 \]
 \]
 
 
-The last variable to color is $v$, and we just need to avoid choosing
-$1$ because of the interference with $w$. Last time we choose the
-color $0$, simply because it was the lowest, but this time we know
+Next we consider coloring the variable $v$, and we just need to avoid
+choosing $1$ because of the interference with $w$. Last time we choose
+the color $0$, simply because it was the lowest, but this time we know
 that $v$ is move related to $x$, so we choose the color $2$.
 that $v$ is move related to $x$, so we choose the color $2$.
 \[
 \[
 \begin{tikzpicture}[baseline=(current  bounding  box.center)]
 \begin{tikzpicture}[baseline=(current  bounding  box.center)]
@@ -2678,6 +2756,10 @@ that $v$ is move related to $x$, so we choose the color $2$.
 \node (x) at (6,0)    {$x:2,\{0,1\}$};
 \node (x) at (6,0)    {$x:2,\{0,1\}$};
 \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
 \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
 \node (z) at (6,-1.5) {$z:2,\{0,1\}$};
 \node (z) at (6,-1.5) {$z:2,\{0,1\}$};
+\node (t1) at (9,0)   {$t.1:-,\{2\}$};
+\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\draw (t1) to (z);
+\draw (t2) to (t1);
 \draw (v) to (w);
 \draw (v) to (w);
 \foreach \i in {w,x,y} 
 \foreach \i in {w,x,y} 
 {
 {
@@ -2705,40 +2787,48 @@ to obtain the code on right.
     (addq (int 4) (var y))
     (addq (int 4) (var y))
     (movq (var x) (var z))
     (movq (var x) (var z))
     (addq (var w) (var z))
     (addq (var w) (var z))
-    (movq (var z) (reg rax))
-    (subq (var y) (reg rax)))
+    (movq (var y) (var t.1))
+    (negq (var t.1))
+    (movq (var z) (var t.2))
+    (addq (var t.1) (var t.2))
+    (movq (var t.2) (reg rax)))
 \end{lstlisting}
 \end{lstlisting}
 \end{minipage}
 \end{minipage}
 $\Rightarrow$
 $\Rightarrow$
 \begin{minipage}{0.45\textwidth}
 \begin{minipage}{0.45\textwidth}
 \begin{lstlisting}
 \begin{lstlisting}
-  (program 0
-    (movq (int 1) (reg rdx))
-    (movq (int 46) (reg rcx))
-    (movq (reg rdx) (reg rdx))
-    (addq (int 7) (reg rdx))
-    (movq (reg rdx) (reg rbx))
-    (addq (int 4) (reg rbx))
-    (movq (reg rdx) (reg rdx))
-    (addq (reg rcx) (reg rdx))
-    (movq (reg rdx) (reg rax))
-    (subq (reg rbx) (reg rax)))
+(program 0
+  (movq (int 1) (reg rdx))
+  (movq (int 46) (reg rcx))
+  (movq (reg rdx) (reg rdx))
+  (addq (int 7) (reg rdx))
+  (movq (reg rdx) (reg rbx))
+  (addq (int 4) (reg rbx))
+  (movq (reg rdx) (reg rdx))
+  (addq (reg rcx) (reg rdx))
+  (movq (reg rbx) (reg rbx))
+  (negq (reg rbx))
+  (movq (reg rdx) (reg rcx))
+  (addq (reg rbx) (reg rcx))
+  (movq (reg rcx) (reg rax)))
 \end{lstlisting}
 \end{lstlisting}
 \end{minipage}
 \end{minipage}
 
 
 The \code{patch-instructions} then removes the trivial moves from
 The \code{patch-instructions} then removes the trivial moves from
-\key{v} to \key{x} and from \key{x} to \key{z} to obtain the following
-result.
+\key{v} to \key{x}, from \key{x} to \key{z}, and from \key{y} to
+\key{t.1}, to obtain the following result.
 \begin{lstlisting}
 \begin{lstlisting}
-  (program 0
-    (movq (int 1) (reg rdx))
-    (movq (int 46) (reg rcx))
-    (addq (int 7) (reg rdx))
-    (movq (reg rdx) (reg rbx))
-    (addq (int 4) (reg rbx))
-    (addq (reg rcx) (reg rdx))
-    (movq (reg rdx) (reg rax))
-    (subq (reg rbx) (reg rax)))
+(program 0
+  (movq (int 1) (reg rdx))
+  (movq (int 46) (reg rcx))
+  (addq (int 7) (reg rdx))
+  (movq (reg rdx) (reg rbx))
+  (addq (int 4) (reg rbx))
+  (addq (reg rcx) (reg rdx))
+  (negq (reg rbx))
+  (movq (reg rdx) (reg rcx))
+  (addq (reg rbx) (reg rcx))
+  (movq (reg rcx) (reg rax)))
 \end{lstlisting}
 \end{lstlisting}
 
 
 \begin{exercise}\normalfont
 \begin{exercise}\normalfont
@@ -3678,8 +3768,8 @@ short-circuiting behavior in the order of evaluation of its arguments.
 \begin{array}{lcl}
 \begin{array}{lcl}
   \Type &::=& \ldots \mid (\key{Vector}\;\Type^{+}) \\
   \Type &::=& \ldots \mid (\key{Vector}\;\Type^{+}) \\
   \Exp &::=& \ldots \mid (\key{vector}\;\Exp^{+}) \mid 
   \Exp &::=& \ldots \mid (\key{vector}\;\Exp^{+}) \mid 
-    (\key{vector-ref}\;\Exp\;\Exp) \\
-  &\mid& (\key{vector-set!}\;\Exp\;\Exp\;\Exp)\\
+    (\key{vector-ref}\;\Exp\;\Int) \\
+  &\mid& (\key{vector-set!}\;\Exp\;\Int\;\Exp)\\
   R_3 &::=& (\key{program} \; \Exp)
   R_3 &::=& (\key{program} \; \Exp)
 \end{array}
 \end{array}
 \]
 \]