浏览代码

ch 4 progress

Jeremy Siek 9 年之前
父节点
当前提交
893157ec54
共有 2 个文件被更改,包括 144 次插入7 次删除
  1. 11 1
      all.bib
  2. 133 6
      book.tex

+ 11 - 1
all.bib

@@ -2,13 +2,23 @@
 %% http://bibdesk.sourceforge.net/
 %% http://bibdesk.sourceforge.net/
 
 
 
 
-%% Created for Jeremy Siek at 2016-01-05 12:54:43 -0500 
+%% Created for Jeremy Siek at 2016-01-07 14:36:00 -0500 
 
 
 
 
 %% Saved with string encoding Unicode (UTF-8) 
 %% Saved with string encoding Unicode (UTF-8) 
 
 
 
 
 
 
+@manual{Intel:2015aa,
+	Author = {Intel},
+	Date-Added = {2016-01-07 19:31:54 +0000},
+	Date-Modified = {2016-01-07 19:35:40 +0000},
+	Institution = {Intel},
+	Month = {December},
+	Title = {Intel 64 and IA-32 Architectures Software Developer's Manual Combined Volumes: 1, 2A, 2B, 2C, 3A, 3B, 3C and 3D},
+	Year = {2015},
+	Bdsk-File-1 = {YnBsaXN0MDDUAQIDBAUGJCVYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3ASAAGGoKgHCBMUFRYaIVUkbnVsbNMJCgsMDxJXTlMua2V5c1pOUy5vYmplY3RzViRjbGFzc6INDoACgAOiEBGABIAFgAdccmVsYXRpdmVQYXRoWWFsaWFzRGF0YV8QOzY0LWlhLTMyLWFyY2hpdGVjdHVyZXMtc29mdHdhcmUtZGV2ZWxvcGVyLW1hbnVhbC0zMjU0NjIucGRm0hcLGBlXTlMuZGF0YU8RAgQAAAAAAgQAAgAADE1hY2ludG9zaCBIRAAAAAAAAAAAAAAAAAAAAM3IwOpIKwAAAL9t8R82NC1pYS0zMi1hcmNoaXRlY3R1IzEzMDU3MzQucGRmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABMFc00rQnoFBERiAAAAAAAAEAAgAACSAAAAAAAAAAAAAAAAAAAAADYmliAAAQAAgAAM3I+SoAAAARAAgAANK0bfAAAAABAAwAv23xAAXAcgACEikAAgA/TWFjaW50b3NoIEhEOlVzZXJzOgBqc2llazoAYmliOgA2NC1pYS0zMi1hcmNoaXRlY3R1IzEzMDU3MzQucGRmAAAOAHgAOwA2ADQALQBpAGEALQAzADIALQBhAHIAYwBoAGkAdABlAGMAdAB1AHIAZQBzAC0AcwBvAGYAdAB3AGEAcgBlAC0AZABlAHYAZQBsAG8AcABlAHIALQBtAGEAbgB1AGEAbAAtADMAMgA1ADQANgAyAC4AcABkAGYADwAaAAwATQBhAGMAaQBuAHQAbwBzAGgAIABIAEQAEgBLVXNlcnMvanNpZWsvYmliLzY0LWlhLTMyLWFyY2hpdGVjdHVyZXMtc29mdHdhcmUtZGV2ZWxvcGVyLW1hbnVhbC0zMjU0NjIucGRmAAATAAEvAAAVAAIADP//AACABtIbHB0eWiRjbGFzc25hbWVYJGNsYXNzZXNdTlNNdXRhYmxlRGF0YaMdHyBWTlNEYXRhWE5TT2JqZWN00hscIiNcTlNEaWN0aW9uYXJ5oiIgXxAPTlNLZXllZEFyY2hpdmVy0SYnVHJvb3SAAQAIABEAGgAjAC0AMgA3AEAARgBNAFUAYABnAGoAbABuAHEAcwB1AHcAhACOAMwA0QDZAuEC4wLoAvMC/AMKAw4DFQMeAyMDMAMzA0UDSANNAAAAAAAAAgEAAAAAAAAAKAAAAAAAAAAAAAAAAAAAA08=}}
+
 @book{Bryant:2010aa,
 @book{Bryant:2010aa,
 	Address = {USA},
 	Address = {USA},
 	Author = {Bryant, Randal E. and O'Hallaron, David R.},
 	Author = {Bryant, Randal E. and O'Hallaron, David R.},

+ 133 - 6
book.tex

@@ -192,9 +192,10 @@ prior to reading this book. There are many other excellent resources
 for learning Racket and
 for learning Racket and
 Scheme~\citep{Dybvig:1987aa,Abelson:1996uq,Friedman:1996aa,Felleisen:2001aa,Felleisen:2013aa,Flatt:2014aa}. It
 Scheme~\citep{Dybvig:1987aa,Abelson:1996uq,Friedman:1996aa,Felleisen:2001aa,Felleisen:2013aa,Flatt:2014aa}. It
 is helpful but not necessary for the student to have prior exposure to
 is helpful but not necessary for the student to have prior exposure to
-x86 (or x86-64) assembly language, as one might obtain from a computer
-systems course~\citep{Bryant:2005aa,Bryant:2010aa}.  This book
-introduces the parts of x86-64 assembly language that are needed.
+x86 (or x86-64) assembly language~\citep{Intel:2015aa}, as one might
+obtain from a computer systems
+course~\citep{Bryant:2005aa,Bryant:2010aa}.  This book introduces the
+parts of x86-64 assembly language that are needed.
 
 
 %\section*{Structure of book}
 %\section*{Structure of book}
 % You might want to add short description about each chapter in this book.
 % You might want to add short description about each chapter in this book.
@@ -751,7 +752,7 @@ following grammar.
 \label{ch:int-exp}
 \label{ch:int-exp}
 
 
 This chapter concerns the challenge of compiling a subset of Racket,
 This chapter concerns the challenge of compiling a subset of Racket,
-which we name $R_1$, to x86-64 assembly code~\citep{Matz:2013aa}. The
+which we name $R_1$, to x86-64 assembly code~\citep{Intel:2015aa}. The
 chapter begins with a description of the $R_1$ language
 chapter begins with a description of the $R_1$ language
 (Section~\ref{sec:s0}) and then a description of x86-64
 (Section~\ref{sec:s0}) and then a description of x86-64
 (Section~\ref{sec:x86-64}). The x86-64 assembly language is quite
 (Section~\ref{sec:x86-64}). The x86-64 assembly language is quite
@@ -1090,7 +1091,7 @@ communicated from one step of the compiler to the next.
              (\key{pushq}\;\Arg) \mid 
              (\key{pushq}\;\Arg) \mid 
              (\key{popq}\;\Arg) \mid 
              (\key{popq}\;\Arg) \mid 
              (\key{retq}) \\
              (\key{retq}) \\
-\Prog &::= & (\key{program} \;\itm{info} \; \Instr^{+})
+x86^{*}_0 &::= & (\key{program} \;\itm{info} \; \Instr^{+})
 \end{array}
 \end{array}
 \]
 \]
 \end{minipage}
 \end{minipage}
@@ -2010,6 +2011,8 @@ The reader may be more familar with the graph coloring problem then he
 or she realizes; the popular game of Sudoku is an instance of the
 or she realizes; the popular game of Sudoku is an instance of the
 graph coloring problem. The following describes how to build a graph
 graph coloring problem. The following describes how to build a graph
 out of an initial Sudoku board.
 out of an initial Sudoku board.
+\marginpar{\scriptsize To do: create a figure with a Sudoku
+board and its corresponding graph. --Jeremy}
 \begin{itemize}
 \begin{itemize}
 \item There is one node in the graph for each Sudoku square.
 \item There is one node in the graph for each Sudoku square.
 \item There is an edge between two nodes if the corresponding squares
 \item There is an edge between two nodes if the corresponding squares
@@ -2619,11 +2622,135 @@ running the output programs with \code{interp-C}
 \end{exercise}
 \end{exercise}
 
 
 
 
+\section{More x86-64}
+\label{sec:x86-1}
+
+To implement the new logical operations, the comparison \key{eq?}, and
+the \key{if} statement, we need to delve further into the x86-64
+language. Figure~\ref{fig:x86-ast-b} defines the abstract syntax for a
+larger subset of x86-64 that includes instructions for logical
+operations, comparisons, and jumps.  The logical instructions
+(\key{andq} and \key{notq}) are quite similar to the arithmetic
+instructions, so we focus on the comparison and jump instructions.
+
+\begin{figure}[tbp]
+\fbox{
+\begin{minipage}{0.96\textwidth}
+\[
+\begin{array}{lcl}
+\Arg &::=&  \ldots \mid (\key{byte-reg}\; \itm{register}) \\ 
+\Instr &::=& \ldots \mid (\key{andq} \; \Arg\; \Arg) \mid (\key{notq} \; \Arg)\\
+       &\mid& (\key{cmpq} \; \Arg\; \Arg) \mid (\key{sete} \; \Arg) 
+              \mid (\key{movzx}\;\Arg\;\Arg) \\
+      &\mid&  (\key{jmp} \; \itm{label}) \mid (\key{je} \; \itm{label}) \mid
+             (\key{label} \; \itm{label}) \\
+x86^{*}_1 &::= & (\key{program} \;\itm{info} \; \Instr^{+})
+\end{array}
+\]
+\end{minipage}
+}
+\caption{The x86$^{*}_1$ language (extends x86$^{*}_0$ of Figure~\ref{fig:x86-ast-a}).}
+\label{fig:x86-ast-b}
+\end{figure}
+
+The \key{cmpq} instruction is somewhat unusual in that its arguments
+are the two things to be compared and the result (less than, greater
+than, equal, not equal, etc.) is placed in the special EFLAGS
+register. This register cannot be accessed directly but it can be
+queried by a number of instructions, including the \key{sete}
+instruction. The \key{sete} instruction puts a \key{1} or \key{0} into
+its destination depending on whether the comparison came out as equal
+or not, respectively. The \key{sete} instruction has an annoying quirk
+in that its destination argument must be single byte register, such as
+\code{al}, which is part of the \code{rax} register.  The following
+instruction sequence shows an example of comparing two integers stored
+in \code{rbx} and \code{rcx} and store the result of the comparison in
+\code{rdx}. The \key{movzx} instruction is used to move from a smaller
+register to a larger register, filling the rest of the bits in the
+larger register with $0$'s.
+\begin{lstlisting}
+	cmpq	%rbx, %rcx
+	sete	%al
+	movzx	%al, %rdx
+\end{lstlisting}
+
+The \key{jmp} instruction jumps to the instruction after the indicated
+label.  The \key{je} instruction jumps to the instruction after the
+indicated label if the result in the EFLAGS register is equal, whereas
+the \key{je} instruction just falls through to the next instruction if
+EFLAGS is not equal.
 
 
 \section{Select Instructions}
 \section{Select Instructions}
 
 
+
+
+Figure~\ref{fig:if-example-x86} shows a simple example program in
+$R_2$ together with its translation to $C_1$ and x86-64.
+
+
+
+\begin{figure}[tbp]
+\begin{tabular}{lll}
+\begin{minipage}{0.4\textwidth}
+\begin{lstlisting}
+(if (eq? (read) 1) 42 0)
+\end{lstlisting}
+$\Downarrow$
+\begin{lstlisting}
+(program (t.1 t.2 if.1)
+  (assign t.1 (read))
+  (assign t.2 (eq? t.1 1))
+  (if t.2
+    ((assign if.1 42))
+    ((assign if.1 0)))
+  (return if.1))
+\end{lstlisting}
+\end{minipage}
+&
+$\Rightarrow$
+&
+\begin{minipage}{0.4\textwidth}
+\begin{lstlisting}
+	.globl _main
+_main:
+	pushq	%rbp
+	movq	%rsp, %rbp
+	subq	$16, %rsp
+	callq	_read_int
+	movq	%rax, %rcx
+	cmpq	$1, %rcx
+	movq	$0, %rax
+	sete	%al
+	movq	%rax, %rcx
+	cmpq	$0, %rcx
+	je else_1
+	movq	$42, %rbx
+	jmp if_end_1
+else_1:
+	movq	$0, %rbx
+if_end_1:
+	movq	%rbx, %rax
+	addq	$16, %rsp
+	popq	%rbp
+	retq
+\end{lstlisting}
+\end{minipage}
+\end{tabular} 
+\caption{Example compilation of \key{if} expression to x86-64.}
+\label{fig:if-example-x86}
+\end{figure}
+
+
+
+
+
+
+
 \section{Register Allocation}
 \section{Register Allocation}
 
 
+
+
+
 \section{Patch Instructions}
 \section{Patch Instructions}
 
 
 
 
@@ -2651,7 +2778,7 @@ running the output programs with \code{interp-C}
 \label{ch:mutable-data}
 \label{ch:mutable-data}
 
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\chapter{The Dynamic Type}
+\chapter{Dynamic Typing}
 \label{ch:type-dynamic}
 \label{ch:type-dynamic}