book.tex 20 KB

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