book.tex 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. \documentclass[12pt]{book}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[utf8]{inputenc}
  4. \usepackage{lmodern}
  5. \usepackage{hyperref}
  6. \usepackage{graphicx}
  7. \usepackage[english]{babel}
  8. \usepackage{listings}
  9. \usepackage{amsmath}
  10. \usepackage{amsthm}
  11. \usepackage{amssymb}
  12. \usepackage{natbib}
  13. \usepackage{stmaryrd}
  14. \usepackage{xypic}
  15. \lstset{%
  16. basicstyle=\ttfamily%
  17. }
  18. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  19. % 'dedication' environment: To add a dedication paragraph at the start of book %
  20. % Source: http://www.tug.org/pipermail/texhax/2010-June/015184.html %
  21. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  22. \newenvironment{dedication}
  23. {
  24. \cleardoublepage
  25. \thispagestyle{empty}
  26. \vspace*{\stretch{1}}
  27. \hfill\begin{minipage}[t]{0.66\textwidth}
  28. \raggedright
  29. }
  30. {
  31. \end{minipage}
  32. \vspace*{\stretch{3}}
  33. \clearpage
  34. }
  35. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  36. % Chapter quote at the start of chapter %
  37. % Source: http://tex.stackexchange.com/a/53380 %
  38. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  39. \makeatletter
  40. \renewcommand{\@chapapp}{}% Not necessary...
  41. \newenvironment{chapquote}[2][2em]
  42. {\setlength{\@tempdima}{#1}%
  43. \def\chapquote@author{#2}%
  44. \parshape 1 \@tempdima \dimexpr\textwidth-2\@tempdima\relax%
  45. \itshape}
  46. {\par\normalfont\hfill--\ \chapquote@author\hspace*{\@tempdima}\par\bigskip}
  47. \makeatother
  48. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  49. \newcommand{\itm}[1]{\ensuremath{\mathit{#1}}}
  50. \newcommand{\Stmt}{\itm{stmt}}
  51. \newcommand{\Exp}{\itm{exp}}
  52. \newcommand{\Instr}{\itm{instr}}
  53. \newcommand{\Prog}{\itm{prog}}
  54. \newcommand{\Arg}{\itm{arg}}
  55. \newcommand{\Int}{\itm{int}}
  56. \newcommand{\Var}{\itm{var}}
  57. \newcommand{\Op}{\itm{op}}
  58. \newcommand{\key}[1]{\texttt{#1}}
  59. \newcommand{\READ}{(\key{read})}
  60. \newcommand{\UNIOP}[2]{(\key{#1}\,#2)}
  61. \newcommand{\BINOP}[3]{(\key{#1}\,#2\,#3)}
  62. \newcommand{\LET}[3]{(\key{let}\,([#1\;#2])\,#3)}
  63. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  64. \title{\Huge \textbf{Essentials of Compilation} \\
  65. \huge An Incremental Approach}
  66. \author{\textsc{Jeremy G. Siek}
  67. \thanks{\url{http://homes.soic.indiana.edu/jsiek/}}
  68. }
  69. \begin{document}
  70. \frontmatter
  71. \maketitle
  72. \begin{dedication}
  73. This book is dedicated to the programming languages group at Indiana University.
  74. \end{dedication}
  75. \tableofcontents
  76. %\listoffigures
  77. %\listoftables
  78. \mainmatter
  79. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  80. \chapter*{Preface}
  81. \cite{Sarkar:2004fk}
  82. \cite{Keep:2012aa}
  83. \cite{Ghuloum:2006bh}
  84. %\section*{Structure of book}
  85. % You might want to add short description about each chapter in this book.
  86. %\section*{About the companion website}
  87. %The website\footnote{\url{https://github.com/amberj/latex-book-template}} for %this file contains:
  88. %\begin{itemize}
  89. % \item A link to (freely downlodable) latest version of this document.
  90. % \item Link to download LaTeX source for this document.
  91. % \item Miscellaneous material (e.g. suggested readings etc).
  92. %\end{itemize}
  93. \section*{Acknowledgments}
  94. Need to give thanks to
  95. \begin{itemize}
  96. \item Kent Dybvig
  97. \item Daniel P. Friedman
  98. \item Oscar Waddell
  99. \item Abdulaziz Ghuloum
  100. \item Dipanwita Sarkar
  101. \end{itemize}
  102. %\mbox{}\\
  103. %\noindent Amber Jain \\
  104. %\noindent \url{http://amberj.devio.us/}
  105. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  106. \chapter{Integers and Variables}
  107. \label{ch:int-exp}
  108. %\begin{chapquote}{Author's name, \textit{Source of this quote}}
  109. %``This is a quote and I don't know who said this.''
  110. %\end{chapquote}
  111. \section{The $S_0$ Language}
  112. The $S_0$ language includes integers, operations on integers,
  113. (arithmetic and input), and variable definitions. The syntax of the
  114. $S_0$ language is defined by the grammar in
  115. Figure~\ref{fig:s0-syntax}. This language is rich enough to exhibit
  116. several compilation techniques but simple enough so that we can
  117. implement a compiler for it in two weeks of hard work. To give the
  118. reader a feeling for the scale of this first compiler, the instructor
  119. solution for the $S_0$ compiler consists of 6 recursive functions and
  120. a few small helper functions that together span 256 lines of code.
  121. \begin{figure}[htbp]
  122. \centering
  123. \fbox{
  124. \begin{minipage}{0.85\textwidth}
  125. \[
  126. \begin{array}{lcl}
  127. \Op &::=& \key{+} \mid \key{-} \mid \key{*} \mid \key{read} \\
  128. \Exp &::=& \Int \mid (\Op \; \Exp^{*}) \mid \Var \mid \LET{\Var}{\Exp}{\Exp}
  129. \end{array}
  130. \]
  131. \end{minipage}
  132. }
  133. \caption{The syntax of the $S_0$ language. The abbreviation \Op{} is
  134. short for operator, \Exp{} is short for expression, \Int{} for integer,
  135. and \Var{} for variable.}
  136. \label{fig:s0-syntax}
  137. \end{figure}
  138. The result of evaluating an expression is a value. For $S_0$, values
  139. are integers. To make it straightforward to map these integers onto
  140. x86-64 assembly~\citep{Matz:2013aa}, we restrict the integers to just
  141. those representable with 64-bits, the range $-2^{63}$ to $2^{63}$.
  142. We will walk through some examples of $S_0$ programs, commenting on
  143. aspects of the language that will be relevant to compiling it. We
  144. start with one of the simplest $S_0$ programs; it adds two integers.
  145. \[
  146. \BINOP{+}{10}{32}
  147. \]
  148. The result is $42$, as you might expected.
  149. %
  150. The next example demonstrates that expressions may be nested within
  151. each other, in this case nesting several additions and negations.
  152. \[
  153. \BINOP{+}{10}{ \UNIOP{-}{ \BINOP{+}{12}{20} } }
  154. \]
  155. What is the result of the above program?
  156. The \key{let} construct stores a value in a variable which can then be
  157. used within the body of the \key{let}. So the following program stores
  158. $32$ in $x$ and then computes $\BINOP{+}{10}{x}$, producing $42$.
  159. \[
  160. \LET{x}{ \BINOP{+}{12}{20} }{ \BINOP{+}{10}{x} }
  161. \]
  162. When there are multiple \key{let}'s for the same variable, the closest
  163. enclosing \key{let} is used. Consider the following program with two
  164. \key{let}'s that define variables named $x$.
  165. \[
  166. \LET{x}{32}{ \BINOP{+}{ \LET{x}{10}{x} }{ x } }
  167. \]
  168. For the purposes of showing which variable uses correspond to which
  169. definitions, the following shows the $x$'s annotated with subscripts
  170. to distinguish them.
  171. \[
  172. \LET{x_1}{32}{ \BINOP{+}{ \LET{x_2}{10}{x_2} }{ x_1 } }
  173. \]
  174. The \key{read} operation prompts the user of the program for an
  175. integer. Given an input of $10$, the following program produces $42$.
  176. \[
  177. \BINOP{+}{(\key{read})}{32}
  178. \]
  179. We include the \key{read} operation in $S_0$ to demonstrate that order
  180. of evaluation can make a different. Given the input $52$ then $10$,
  181. the following produces $42$ (and not $-42$).
  182. \[
  183. \LET{x}{\READ}{ \LET{y}{\READ}{ \BINOP{-}{x}{y} } }
  184. \]
  185. The initializing expression is always evaluated before the body of the
  186. \key{let}, so in the above, the \key{read} for $x$ is performed before
  187. the \key{read} for $y$.
  188. %
  189. The behavior of the following program is somewhat subtle because
  190. Scheme does not specify an evaluation order for arguments of an
  191. operator such as $-$.
  192. \[
  193. \BINOP{-}{\READ}{\READ}
  194. \]
  195. Given the input $42$ then $10$, the above program can result in either
  196. $42$ or $-42$, depending on the whims of the Scheme implementation.
  197. The goal for this chapter is to implement a compiler that translates
  198. any program $p \in S_0$ into a x86-64 assembly program $p'$ such that
  199. the assembly program exhibits the same behavior on an x86 computer as
  200. the $S_0$ program running in a Scheme implementation.
  201. \[
  202. \xymatrix{
  203. p \in S_0 \ar[rr]^{\text{compile}} \ar[drr]_{\text{run in Scheme}\quad} && p' \in \text{x86-64} \ar[d]^{\quad\text{run on an x86 machine}}\\
  204. & & n \in \mathbb{Z}
  205. }
  206. \]
  207. In the next section we introduce enough of the x86-64 assembly
  208. language to compile $S_0$.
  209. \section{x86-64 Assembly}
  210. An x86-64 program is a sequence of instructions. The instructions
  211. manipulate 16 variables called \emph{registers} and can also load and
  212. store values into \emph{memory}. Memory is a mapping of 64-bit
  213. addresses to 64-bit values. The syntax $n(r)$ is used to read the
  214. address $a$ stored in register $r$ and then offset it by $n$ bytes (8
  215. bits), producing the address $a + n$. The arithmetic instructions,
  216. such as $\key{addq}\,s\,d$, read from the source $s$ and destination
  217. argument $d$, apply the arithmetic operation, then stores the result
  218. in the destination $d$. In this case, computing $d \gets d + s$. The
  219. move instruction, $\key{movq}\,s\,d$ reads from $s$ and stores the
  220. result in $d$. The $\key{callq}\,\mathit{label}$ instruction executes
  221. the procedure specified by the label, which we shall use to implement
  222. \key{read}. Figure~\ref{fig:x86-a} defines the syntax for this subset
  223. of the x86-64 assembly language.
  224. \begin{figure}[tbp]
  225. \fbox{
  226. \begin{minipage}{0.96\textwidth}
  227. \[
  228. \begin{array}{lcl}
  229. \itm{register} &::=& \key{rsp} \mid \key{rbp} \mid \key{rax} \mid \key{rbx} \mid \key{rcx}
  230. \mid \key{rdx} \mid \key{rsi} \mid \key{rdi} \mid \\
  231. && \key{r8} \mid \key{r9} \mid \key{r10}
  232. \mid \key{r11} \mid \key{r12} \mid \key{r13}
  233. \mid \key{r14} \mid \key{r15} \\
  234. \Arg &::=& \key{\$}\Int \mid \key{\%}\itm{register} \mid \Int(\key{\%}\itm{register}) \\
  235. \Instr &::=& \key{addq} \; \Arg \; \Arg \mid
  236. \key{subq} \; \Arg \; \Arg \mid
  237. \key{imulq} \; \Arg \; \Arg \mid
  238. \key{negq} \; \Arg \mid \\
  239. && \key{movq} \; \Arg \; \Arg \mid
  240. \key{callq} \; \mathit{label} \mid
  241. \key{pushq}\;\Arg \mid \key{popq};\Arg \mid \key{retq} \\
  242. \Prog &::= & \key{.globl \_main}\\
  243. & & \key{\_main:} \; \Instr^{*}
  244. \end{array}
  245. \]
  246. \end{minipage}
  247. }
  248. \caption{A subset of the x86-64 assembly language.}
  249. \label{fig:x86-a}
  250. \end{figure}
  251. Figure~\ref{fig:p0-x86} depicts an x86-64 program that is equivalent
  252. to $\BINOP{+}{10}{32}$. The \key{globl} directive says that the
  253. \key{\_main} procedure is externally visible, which is necessary so
  254. that the operating system can call it. The label \key{\_main:}
  255. indicates the beginning of the \key{\_main} procedure. The
  256. instruction $\key{movq}\,\$10, \%\key{rax}$ puts $10$ into the
  257. register \key{rax}. The following instruction $\key{addq}\,\key{\$}32,
  258. \key{\%rax}$ adds $32$ to the $10$ in \key{rax} and puts the result,
  259. $42$, back into \key{rax}. The instruction \key{retq} finishes the
  260. \key{\_main} function by returning the integer in the \key{rax}
  261. register to the operating system.
  262. \begin{figure}[htbp]
  263. \centering
  264. \begin{minipage}{0.6\textwidth}
  265. \begin{lstlisting}
  266. .globl _main
  267. _main:
  268. movq $10, %rax
  269. addq $32, %rax
  270. retq
  271. \end{lstlisting}
  272. \end{minipage}
  273. \caption{A simple x86-64 program equivalent to $\BINOP{+}{10}{32}$.}
  274. \label{fig:p0-x86}
  275. \end{figure}
  276. The next example exhibits the use of memory. Figure~\ref{fig:p1-x86}
  277. lists an x86-64 program that is equivalent to $\BINOP{+}{52}{
  278. \UNIOP{-}{10} }$. To understand how this x86-64 program uses memory,
  279. we need to explain a region of memory called called the
  280. \emph{procedure call stack} (\emph{stack} for short). The stack
  281. consists of a separate \emph{frame} for each procedure call. The
  282. memory layout for an individual frame is shown in
  283. Figure~\ref{fig:frame}. The register \key{rsp} is called the
  284. \emph{stack pointer} and points to the item at the top of the
  285. stack. The stack grows downward in memory, so we increase the size of
  286. the stack by subtracting from the stack pointer. The frame size is
  287. required to be a multiple of 16 bytes. The register \key{rbp} is the
  288. \emph{base pointer} which serves two purposes: 1) it saves the
  289. location of the stack pointer for the procedure that called the
  290. current one and 2) it is used to access variables associated with the
  291. current procedure. We number the variables from $1$ to $n$. Variable
  292. $1$ is stored at address $-8\key{(\%rbp)}$, variable $2$ at
  293. $-16\key{(\%rbp)}$, etc.
  294. \begin{figure}
  295. \centering
  296. \begin{minipage}{0.6\textwidth}
  297. \begin{lstlisting}
  298. .globl _main
  299. _main:
  300. pushq %rbp
  301. movq %rsp, %rbp
  302. subq $16, %rsp
  303. movq $10, -8(%rbp)
  304. negq -8(%rbp)
  305. movq $52, %rax
  306. addq -8(%rbp), %rax
  307. addq $16, %rsp
  308. popq %rbp
  309. retq
  310. \end{lstlisting}
  311. \end{minipage}
  312. \caption{An x86-64 program equivalent to $\BINOP{+}{52}{\UNIOP{-}{10} }$.}
  313. \label{fig:p1-x86}
  314. \end{figure}
  315. \begin{figure}
  316. \centering
  317. \begin{tabular}{|r|l|} \hline
  318. Position & Contents \\ \hline
  319. 8(\key{\%rbp}) & return address \\
  320. 0(\key{\%rbp}) & old \key{rbp} \\
  321. -8(\key{\%rbp}) & variable $1$ \\
  322. -16(\key{\%rbp}) & variable $2$ \\
  323. \ldots & \ldots \\
  324. 0(\key{\%rsp}) & variable $n$\\ \hline
  325. \end{tabular}
  326. \caption{Memory layout of a frame.}
  327. \label{fig:frame}
  328. \end{figure}
  329. Getting back to the program in Figure~\ref{fig:p1-x86}, the first
  330. three instructions are the typical prelude for a procedure. The
  331. instruction \key{pushq \%rbp} saves the base pointer for the procedure
  332. that called the current one onto the stack and subtracts $8$ from the
  333. stack pointer. The second instruction \key{movq \%rsp, \%rbp} changes
  334. the base pointer to the top of the stack. The instruction \key{subq
  335. \$16, \%rsp} moves the stack pointer down to make enough room for
  336. storing variables. This program just needs one variable ($8$ bytes)
  337. but because the frame size is required to be a multiple of 16 bytes,
  338. it rounds to 16 bytes.
  339. The next four instructions carry out the work of computing
  340. $\BINOP{+}{52}{\UNIOP{-}{10} }$. The first instruction \key{movq \$10,
  341. -8(\%rbp)} stores $10$ in variable $1$. The instruction \key{negq
  342. -8(\%rbp)} changes variable $1$ to $-10$. The \key{movq \$52, \%rax}
  343. places $52$ in the register \key{rax} and \key{addq -8(\%rbp), \%rax}
  344. adds the contents of variable $1$ to \key{rax}, at which point
  345. \key{rax} contains $42$.
  346. The last three instructions are the typical conclusion of a procedure.
  347. The \key{addq \$16, \%rsp} instruction moves the stack pointer back to
  348. point at the old base pointer. The amount added here needs to match
  349. the amount that was subtracted in the prelude of the procedure. Then
  350. \key{popq \%rbp} returns the old base pointer to \key{rbp} and adds
  351. $8$ to the stack pointer. The \key{retq} instruction jumps back to
  352. the procedure that called this one and subtracts 8 from the stack
  353. pointer.
  354. \section{Planning the route from $S_0$ to x86-64}
  355. To compile one language to another it helps to focus on the
  356. differences between the two languages. It is these differences that
  357. the compiler will need to bridge. What are the differences between
  358. $S_0$ and x86-64 assembly? Here we list some of the most important the
  359. differences.
  360. \begin{enumerate}
  361. \item Variables in $S_0$ can overshadow other variables with the same
  362. name. The registers and memory locations of x86-64 all have unique
  363. names.
  364. \item An argument to an $S_0$ operator can be any expression, whereas
  365. x86-64 instructions restrict their arguments to integers, registers,
  366. and memory locations.
  367. \item x86-64 arithmetic instructions typically take two arguments and
  368. update the second argument in place. In contrast, $S_0$ arithmetic
  369. operations only read their arguments and produce a new value.
  370. \item An $S_0$ program can have any number of variables whereas x86-64
  371. has only 16 registers.
  372. \end{enumerate}
  373. We ease the challenge of compiling from $S_0$ to x86 by breaking down
  374. the problem into several steps, dealing with the above differences one
  375. at a time. Further, we identify an intermediate language named $C_0$,
  376. roughly half-way between $S_0$ and x86, to provide a rest stop along
  377. the way. The name $C_0$ comes from this language being vaguely similar
  378. to the $C$ language. first two differences discussed above, regarding
  379. variables and nested expressions, are handled by the passes
  380. \textsf{uniquify} and \textsf{flatten} that bring us to $C_0$.
  381. \[\large
  382. \xymatrix@=55pt{
  383. S_0 \ar[r]^-{\textsf{uniquify}} & S_0 \ar[r]^-{\textsf{flatten}} & C_0
  384. }
  385. \]
  386. The syntax for $C_0$ is defined in Figure~\ref{fig:c0-syntax}. The
  387. $C_0$ language supports the same operators as $S_0$ but the arguments
  388. of operators are now restricted to just variables and integers. The
  389. \key{let} construct of $S_0$ is replaced by an assignment statement
  390. and there is a \key{return} construct to specify the return value of
  391. the program. A program consists of a sequence of statements that
  392. include at least one \key{return} statement.
  393. \begin{figure}[htbp]
  394. \[
  395. \begin{array}{lcl}
  396. \Arg &::=& \Int \mid \Var \\
  397. \Exp &::=& \Arg \mid (\Op \; \Arg^{*})\\
  398. \Stmt &::=& (\key{assign} \; \Var \; \Exp) \mid (\key{return}\; \Exp) \\
  399. \Prog & ::= & \Stmt^{+}
  400. \end{array}
  401. \]
  402. \caption{The $C_0$ intermediate language.}
  403. \label{fig:c0-syntax}
  404. \end{figure}
  405. To get from $C_0$ to x86-64 assembly requires three more steps, which
  406. we discuss below.
  407. \[\large
  408. \xymatrix@=55pt{
  409. C_0 \ar[r]^-{\textsf{select\_instr.}}
  410. & \text{x86}^{*} \ar[r]^-{\textsf{assign\_homes}} & \text{x86}^{*}
  411. \ar[r]^-{\textsf{spill\_code}}
  412. & \text{x86}
  413. }
  414. \]
  415. We handle the third difference listed above, concerning the format of
  416. arithmetic instructions, in the \textsf{select\_instructions} pass.
  417. The result of this pass produces programs consisting of x86-64
  418. instructions that use variables.
  419. %
  420. As there are only 16 registers, we cannot always map variables to
  421. registers. Fortunately, the stack can grow arbitrarily, so we can
  422. always map variables to locations on the stack. This is handled in the
  423. \textsf{assign\_homes} pass. The topic of
  424. Chapter~\ref{ch:register-allocation} is implementing a smarter
  425. approach in which we make a best-effort to map variables to registers,
  426. resorting to the stack only when necessary.
  427. %
  428. The final pass in our journey to x86 handles an indiosycracy of x86
  429. assembly. Many x86 instructions have two arguments but only one of the
  430. arguments may be a memory reference. Because we are mapping variables
  431. to stack locations, many of our generated instructions will violate
  432. this restriction. The purpose of the \textsf{spill\_code} pass is to
  433. patch up this problem by replacing each bad instructions with a short
  434. sequence of instructions that use the \key{rax} register.
  435. \section{Uniquify}
  436. The purpose of this pass is to make sure that each \key{let} uses a
  437. unique variable name. For example, the \textsf{uniquify} pass could
  438. translate
  439. \[
  440. \LET{x}{32}{ \BINOP{+}{ \LET{x}{10}{x} }{ x } }
  441. \]
  442. to
  443. \[
  444. \LET{x.1}{32}{ \BINOP{+}{ \LET{x.2}{10}{x.2} }{ x.1 } }
  445. \]
  446. We recommend implementing \textsf{uniquify} as a recursive function
  447. that mostly just copies the input program. However, when encountering
  448. a \key{let}, it should generate a unique name for the variable (the
  449. Racket function \key{gensym} is handy for this) and associate the old
  450. name with the new unique name in an association list. The
  451. \textsf{uniquify} function will need to access this association list
  452. when it gets to a variable reference, so we add another paramter to
  453. \textsf{uniquify} for the association list.
  454. \section{Flatten}
  455. \section{Select Instructions}
  456. \section{Assign Homes}
  457. \section{Spill Code}
  458. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  459. \chapter{Register Allocation}
  460. \label{ch:register-allocation}
  461. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  462. \chapter{Booleans, Conditions, and Type Checking}
  463. \label{ch:bool-types}
  464. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  465. \chapter{Tuples and Heap Allocation}
  466. \label{ch:tuples}
  467. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  468. \chapter{Functions}
  469. \label{ch:functions}
  470. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  471. \chapter{Lexically Scoped Functions}
  472. \label{ch:lambdas}
  473. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  474. \chapter{The Dynamic Type}
  475. \label{ch:type-dynamic}
  476. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  477. \chapter{Mutable Lists}
  478. \label{ch:mutable-lists}
  479. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  480. \chapter{Parametric Polymorphism}
  481. \label{ch:parametric-polymorphism}
  482. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  483. \chapter{High-level Optimization}
  484. \label{ch:high-level-optimization}
  485. \bibliographystyle{plainnat}
  486. \bibliography{all}
  487. \end{document}
  488. %% LocalWords: Dybvig Waddell Abdulaziz Ghuloum Dipanwita
  489. %% LocalWords: Sarkar lcl Matz aa representable