book.tex 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. \newcommand{\INT}[1]{(\key{int}\;#1)}
  66. \newcommand{\REG}[1]{(\key{reg}\;#1)}
  67. \newcommand{\VAR}[1]{(\key{var}\;#1)}
  68. \newcommand{\STACKLOC}[1]{(\key{stack}\;#1)}
  69. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  70. \title{\Huge \textbf{Essentials of Compilation} \\
  71. \huge An Incremental Approach}
  72. \author{\textsc{Jeremy G. Siek}
  73. \thanks{\url{http://homes.soic.indiana.edu/jsiek/}}
  74. }
  75. \begin{document}
  76. \frontmatter
  77. \maketitle
  78. \begin{dedication}
  79. This book is dedicated to the programming languages group at Indiana University.
  80. \end{dedication}
  81. \tableofcontents
  82. %\listoffigures
  83. %\listoftables
  84. \mainmatter
  85. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  86. \chapter*{Preface}
  87. \cite{Sarkar:2004fk}
  88. \cite{Keep:2012aa}
  89. \cite{Ghuloum:2006bh}
  90. %\section*{Structure of book}
  91. % You might want to add short description about each chapter in this book.
  92. %\section*{About the companion website}
  93. %The website\footnote{\url{https://github.com/amberj/latex-book-template}} for %this file contains:
  94. %\begin{itemize}
  95. % \item A link to (freely downlodable) latest version of this document.
  96. % \item Link to download LaTeX source for this document.
  97. % \item Miscellaneous material (e.g. suggested readings etc).
  98. %\end{itemize}
  99. \section*{Acknowledgments}
  100. Need to give thanks to
  101. \begin{itemize}
  102. \item Kent Dybvig
  103. \item Daniel P. Friedman
  104. \item Oscar Waddell
  105. \item Abdulaziz Ghuloum
  106. \item Dipanwita Sarkar
  107. \end{itemize}
  108. %\mbox{}\\
  109. %\noindent Amber Jain \\
  110. %\noindent \url{http://amberj.devio.us/}
  111. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  112. \chapter{Integers and Variables}
  113. \label{ch:int-exp}
  114. %\begin{chapquote}{Author's name, \textit{Source of this quote}}
  115. %``This is a quote and I don't know who said this.''
  116. %\end{chapquote}
  117. \section{The $S_0$ Language}
  118. The $S_0$ language includes integers, operations on integers,
  119. (arithmetic and input), and variable definitions. The syntax of the
  120. $S_0$ language is defined by the grammar in
  121. Figure~\ref{fig:s0-syntax}. This language is rich enough to exhibit
  122. several compilation techniques but simple enough so that we can
  123. implement a compiler for it in two weeks of hard work. To give the
  124. reader a feeling for the scale of this first compiler, the instructor
  125. solution for the $S_0$ compiler consists of 6 recursive functions and
  126. a few small helper functions that together span 256 lines of code.
  127. \begin{figure}[htbp]
  128. \centering
  129. \fbox{
  130. \begin{minipage}{0.85\textwidth}
  131. \[
  132. \begin{array}{lcl}
  133. \Op &::=& \key{+} \mid \key{-} \mid \key{*} \mid \key{read} \\
  134. \Exp &::=& \Int \mid (\Op \; \Exp^{*}) \mid \Var \mid \LET{\Var}{\Exp}{\Exp}
  135. \end{array}
  136. \]
  137. \end{minipage}
  138. }
  139. \caption{The syntax of the $S_0$ language. The abbreviation \Op{} is
  140. short for operator, \Exp{} is short for expression, \Int{} for integer,
  141. and \Var{} for variable.}
  142. \label{fig:s0-syntax}
  143. \end{figure}
  144. The result of evaluating an expression is a value. For $S_0$, values
  145. are integers. To make it straightforward to map these integers onto
  146. x86-64 assembly~\citep{Matz:2013aa}, we restrict the integers to just
  147. those representable with 64-bits, the range $-2^{63}$ to $2^{63}$.
  148. We will walk through some examples of $S_0$ programs, commenting on
  149. aspects of the language that will be relevant to compiling it. We
  150. start with one of the simplest $S_0$ programs; it adds two integers.
  151. \[
  152. \BINOP{+}{10}{32}
  153. \]
  154. The result is $42$, as you might expected.
  155. %
  156. The next example demonstrates that expressions may be nested within
  157. each other, in this case nesting several additions and negations.
  158. \[
  159. \BINOP{+}{10}{ \UNIOP{-}{ \BINOP{+}{12}{20} } }
  160. \]
  161. What is the result of the above program?
  162. The \key{let} construct stores a value in a variable which can then be
  163. used within the body of the \key{let}. So the following program stores
  164. $32$ in $x$ and then computes $\BINOP{+}{10}{x}$, producing $42$.
  165. \[
  166. \LET{x}{ \BINOP{+}{12}{20} }{ \BINOP{+}{10}{x} }
  167. \]
  168. When there are multiple \key{let}'s for the same variable, the closest
  169. enclosing \key{let} is used. Consider the following program with two
  170. \key{let}'s that define variables named $x$.
  171. \[
  172. \LET{x}{32}{ \BINOP{+}{ \LET{x}{10}{x} }{ x } }
  173. \]
  174. For the purposes of showing which variable uses correspond to which
  175. definitions, the following shows the $x$'s annotated with subscripts
  176. to distinguish them.
  177. \[
  178. \LET{x_1}{32}{ \BINOP{+}{ \LET{x_2}{10}{x_2} }{ x_1 } }
  179. \]
  180. The \key{read} operation prompts the user of the program for an
  181. integer. Given an input of $10$, the following program produces $42$.
  182. \[
  183. \BINOP{+}{(\key{read})}{32}
  184. \]
  185. We include the \key{read} operation in $S_0$ to demonstrate that order
  186. of evaluation can make a different. Given the input $52$ then $10$,
  187. the following produces $42$ (and not $-42$).
  188. \[
  189. \LET{x}{\READ}{ \LET{y}{\READ}{ \BINOP{-}{x}{y} } }
  190. \]
  191. The initializing expression is always evaluated before the body of the
  192. \key{let}, so in the above, the \key{read} for $x$ is performed before
  193. the \key{read} for $y$.
  194. %
  195. The behavior of the following program is somewhat subtle because
  196. Scheme does not specify an evaluation order for arguments of an
  197. operator such as $-$.
  198. \[
  199. \BINOP{-}{\READ}{\READ}
  200. \]
  201. Given the input $42$ then $10$, the above program can result in either
  202. $42$ or $-42$, depending on the whims of the Scheme implementation.
  203. The goal for this chapter is to implement a compiler that translates
  204. any program $p \in S_0$ into a x86-64 assembly program $p'$ such that
  205. the assembly program exhibits the same behavior on an x86 computer as
  206. the $S_0$ program running in a Scheme implementation.
  207. \[
  208. \xymatrix{
  209. 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}}\\
  210. & & n \in \mathbb{Z}
  211. }
  212. \]
  213. In the next section we introduce enough of the x86-64 assembly
  214. language to compile $S_0$.
  215. \section{The x86-64 Assembly Language}
  216. An x86-64 program is a sequence of instructions. The instructions
  217. manipulate 16 variables called \emph{registers} and can also load and
  218. store values into \emph{memory}. Memory is a mapping of 64-bit
  219. addresses to 64-bit values. The syntax $n(r)$ is used to read the
  220. address $a$ stored in register $r$ and then offset it by $n$ bytes (8
  221. bits), producing the address $a + n$. The arithmetic instructions,
  222. such as $\key{addq}\,s\,d$, read from the source $s$ and destination
  223. argument $d$, apply the arithmetic operation, then stores the result
  224. in the destination $d$. In this case, computing $d \gets d + s$. The
  225. move instruction, $\key{movq}\,s\,d$ reads from $s$ and stores the
  226. result in $d$. The $\key{callq}\,\mathit{label}$ instruction executes
  227. the procedure specified by the label, which we shall use to implement
  228. \key{read}. Figure~\ref{fig:x86-a} defines the syntax for this subset
  229. of the x86-64 assembly language.
  230. \begin{figure}[tbp]
  231. \fbox{
  232. \begin{minipage}{0.96\textwidth}
  233. \[
  234. \begin{array}{lcl}
  235. \itm{register} &::=& \key{rsp} \mid \key{rbp} \mid \key{rax} \mid \key{rbx} \mid \key{rcx}
  236. \mid \key{rdx} \mid \key{rsi} \mid \key{rdi} \mid \\
  237. && \key{r8} \mid \key{r9} \mid \key{r10}
  238. \mid \key{r11} \mid \key{r12} \mid \key{r13}
  239. \mid \key{r14} \mid \key{r15} \\
  240. \Arg &::=& \key{\$}\Int \mid \key{\%}\itm{register} \mid \Int(\key{\%}\itm{register}) \\
  241. \Instr &::=& \key{addq} \; \Arg, \Arg \mid
  242. \key{subq} \; \Arg, \Arg \mid
  243. \key{imulq} \; \Arg,\Arg \mid
  244. \key{negq} \; \Arg \mid \\
  245. && \key{movq} \; \Arg, \Arg \mid
  246. \key{callq} \; \mathit{label} \mid
  247. \key{pushq}\;\Arg \mid \key{popq}\;\Arg \mid \key{retq} \\
  248. \Prog &::= & \key{.globl \_main}\\
  249. & & \key{\_main:} \; \Instr^{+}
  250. \end{array}
  251. \]
  252. \end{minipage}
  253. }
  254. \caption{A subset of the x86-64 assembly language.}
  255. \label{fig:x86-a}
  256. \end{figure}
  257. Figure~\ref{fig:p0-x86} depicts an x86-64 program that is equivalent
  258. to $\BINOP{+}{10}{32}$. The \key{globl} directive says that the
  259. \key{\_main} procedure is externally visible, which is necessary so
  260. that the operating system can call it. The label \key{\_main:}
  261. indicates the beginning of the \key{\_main} procedure. The
  262. instruction $\key{movq}\,\$10, \%\key{rax}$ puts $10$ into the
  263. register \key{rax}. The following instruction $\key{addq}\,\key{\$}32,
  264. \key{\%rax}$ adds $32$ to the $10$ in \key{rax} and puts the result,
  265. $42$, back into \key{rax}. The instruction \key{retq} finishes the
  266. \key{\_main} function by returning the integer in the \key{rax}
  267. register to the operating system.
  268. \begin{figure}[htbp]
  269. \centering
  270. \begin{minipage}{0.6\textwidth}
  271. \begin{lstlisting}
  272. .globl _main
  273. _main:
  274. movq $10, %rax
  275. addq $32, %rax
  276. retq
  277. \end{lstlisting}
  278. \end{minipage}
  279. \caption{A simple x86-64 program equivalent to $\BINOP{+}{10}{32}$.}
  280. \label{fig:p0-x86}
  281. \end{figure}
  282. The next example exhibits the use of memory. Figure~\ref{fig:p1-x86}
  283. lists an x86-64 program that is equivalent to $\BINOP{+}{52}{
  284. \UNIOP{-}{10} }$. To understand how this x86-64 program uses memory,
  285. we need to explain a region of memory called called the
  286. \emph{procedure call stack} (\emph{stack} for short). The stack
  287. consists of a separate \emph{frame} for each procedure call. The
  288. memory layout for an individual frame is shown in
  289. Figure~\ref{fig:frame}. The register \key{rsp} is called the
  290. \emph{stack pointer} and points to the item at the top of the
  291. stack. The stack grows downward in memory, so we increase the size of
  292. the stack by subtracting from the stack pointer. The frame size is
  293. required to be a multiple of 16 bytes. The register \key{rbp} is the
  294. \emph{base pointer} which serves two purposes: 1) it saves the
  295. location of the stack pointer for the procedure that called the
  296. current one and 2) it is used to access variables associated with the
  297. current procedure. We number the variables from $1$ to $n$. Variable
  298. $1$ is stored at address $-8\key{(\%rbp)}$, variable $2$ at
  299. $-16\key{(\%rbp)}$, etc.
  300. \begin{figure}
  301. \centering
  302. \begin{minipage}{0.6\textwidth}
  303. \begin{lstlisting}
  304. .globl _main
  305. _main:
  306. pushq %rbp
  307. movq %rsp, %rbp
  308. subq $16, %rsp
  309. movq $10, -8(%rbp)
  310. negq -8(%rbp)
  311. movq $52, %rax
  312. addq -8(%rbp), %rax
  313. addq $16, %rsp
  314. popq %rbp
  315. retq
  316. \end{lstlisting}
  317. \end{minipage}
  318. \caption{An x86-64 program equivalent to $\BINOP{+}{52}{\UNIOP{-}{10} }$.}
  319. \label{fig:p1-x86}
  320. \end{figure}
  321. \begin{figure}
  322. \centering
  323. \begin{tabular}{|r|l|} \hline
  324. Position & Contents \\ \hline
  325. 8(\key{\%rbp}) & return address \\
  326. 0(\key{\%rbp}) & old \key{rbp} \\
  327. -8(\key{\%rbp}) & variable $1$ \\
  328. -16(\key{\%rbp}) & variable $2$ \\
  329. \ldots & \ldots \\
  330. 0(\key{\%rsp}) & variable $n$\\ \hline
  331. \end{tabular}
  332. \caption{Memory layout of a frame.}
  333. \label{fig:frame}
  334. \end{figure}
  335. Getting back to the program in Figure~\ref{fig:p1-x86}, the first
  336. three instructions are the typical prelude for a procedure. The
  337. instruction \key{pushq \%rbp} saves the base pointer for the procedure
  338. that called the current one onto the stack and subtracts $8$ from the
  339. stack pointer. The second instruction \key{movq \%rsp, \%rbp} changes
  340. the base pointer to the top of the stack. The instruction \key{subq
  341. \$16, \%rsp} moves the stack pointer down to make enough room for
  342. storing variables. This program just needs one variable ($8$ bytes)
  343. but because the frame size is required to be a multiple of 16 bytes,
  344. it rounds to 16 bytes.
  345. The next four instructions carry out the work of computing
  346. $\BINOP{+}{52}{\UNIOP{-}{10} }$. The first instruction \key{movq \$10,
  347. -8(\%rbp)} stores $10$ in variable $1$. The instruction \key{negq
  348. -8(\%rbp)} changes variable $1$ to $-10$. The \key{movq \$52, \%rax}
  349. places $52$ in the register \key{rax} and \key{addq -8(\%rbp), \%rax}
  350. adds the contents of variable $1$ to \key{rax}, at which point
  351. \key{rax} contains $42$.
  352. The last three instructions are the typical \emph{conclusion} of a
  353. procedure. The \key{addq \$16, \%rsp} instruction moves the stack
  354. pointer back to point at the old base pointer. The amount added here
  355. needs to match the amount that was subtracted in the prelude of the
  356. procedure. Then \key{popq \%rbp} returns the old base pointer to
  357. \key{rbp} and adds $8$ to the stack pointer. The \key{retq}
  358. instruction jumps back to the procedure that called this one and
  359. subtracts 8 from the stack pointer.
  360. The compiler will need a convenient representation for manipulating
  361. x86 programs, so we define an abstract syntax for x86 in
  362. Figure~\ref{fig:x86-ast-a}. The \itm{info} field of the \key{program}
  363. AST node is for storing auxilliary information that needs to be
  364. communicated from one pass to the next. The function \key{print-x86}
  365. provided in the supplemental code converts an x86 abstract syntax tree
  366. into the text representation for x86 (Figure~\ref{fig:x86-a}).
  367. \begin{figure}[tbp]
  368. \fbox{
  369. \begin{minipage}{0.96\textwidth}
  370. \[
  371. \begin{array}{lcl}
  372. \Arg &::=& \INT{\Int} \mid \REG{\itm{register}}
  373. \mid \STACKLOC{\Int} \\
  374. \Instr &::=& (\key{add} \; \Arg\; \Arg) \mid
  375. (\key{sub} \; \Arg\; \Arg) \mid
  376. (\key{imul} \; \Arg\;\Arg) \mid
  377. (\key{neg} \; \Arg) \mid \\
  378. && (\key{mov} \; \Arg\; \Arg) \mid
  379. (\key{call} \; \mathit{label}) \mid
  380. (\key{push}\;\Arg) \mid (\key{pop}\;\Arg) \mid (\key{ret}) \\
  381. \Prog &::= & (\key{program} \;\itm{info} \; \Instr^{+})
  382. \end{array}
  383. \]
  384. \end{minipage}
  385. }
  386. \caption{Abstract syntax for x86-64 assembly.}
  387. \label{fig:x86-ast-a}
  388. \end{figure}
  389. \section{Planning the route from $S_0$ to x86-64}
  390. \label{sec:plan-s0-x86}
  391. To compile one language to another it helps to focus on the
  392. differences between the two languages. It is these differences that
  393. the compiler will need to bridge. What are the differences between
  394. $S_0$ and x86-64 assembly? Here we list some of the most important the
  395. differences.
  396. \begin{enumerate}
  397. \item x86-64 arithmetic instructions typically take two arguments and
  398. update the second argument in place. In contrast, $S_0$ arithmetic
  399. operations only read their arguments and produce a new value.
  400. \item An argument to an $S_0$ operator can be any expression, whereas
  401. x86-64 instructions restrict their arguments to integers, registers,
  402. and memory locations.
  403. \item An $S_0$ program can have any number of variables whereas x86-64
  404. has only 16 registers.
  405. \item Variables in $S_0$ can overshadow other variables with the same
  406. name. The registers and memory locations of x86-64 all have unique
  407. names.
  408. \end{enumerate}
  409. We ease the challenge of compiling from $S_0$ to x86 by breaking down
  410. the problem into several steps, dealing with the above differences one
  411. at a time. The main question then becomes: in what order to we tackle
  412. these differences? This is often one of the most challenging questions
  413. that a compiler writer must answer because some orderings may be much
  414. more difficult to implement than others. It is difficult to know ahead
  415. of time which orders will be better so often some trial-and-error is
  416. involved. However, we can try to plan ahead and choose the orderings
  417. based on what we find out.
  418. For example, to handle difference \#2 (nested expressions), we shall
  419. introduce new variables and pull apart the nested expressions into a
  420. sequence of assignment statements. To deal with difference \#3 we
  421. will be replacing variables with registers and/or stack
  422. locations. Thus, it makes sense to deal with \#2 before \#3 so that
  423. \#3 can replace both the original variables and the new ones. Next,
  424. consider where \#1 should fit in. Because it has to do with the format
  425. of x86 instructions, it makes more sense after we have flattened the
  426. nested expressions (\#2). Finally, when should we deal with \#4
  427. (variable overshadowing)? We shall be solving this problem by
  428. renaming variables to make sure they have unique names. Recall that
  429. our plan for \#2 involves moving nested expressions, which could be
  430. problematic if it changes the shadowing of variables. However, if we
  431. deal with \#4 first, then it will not be an issue. Thus, we arrive at
  432. the following ordering.
  433. \[
  434. \xymatrix{
  435. 4 \ar[r] & 2 \ar[r] & 1 \ar[r] & 3
  436. }
  437. \]
  438. We further simplify the translation from $S_0$ to x86 by identifying
  439. an intermediate language named $C_0$, roughly half-way between $S_0$
  440. and x86, to provide a rest stop along the way. The name $C_0$ comes
  441. from this language being vaguely similar to the $C$ language. The
  442. differences \#4 and \#1, regarding variables and nested expressions,
  443. are handled by the passes \textsf{uniquify} and \textsf{flatten} that
  444. bring us to $C_0$.
  445. \[\large
  446. \xymatrix@=60pt{
  447. S_0 \ar[r]^-{\textsf{uniquify}} & S_0 \ar[r]^-{\textsf{flatten}} & C_0
  448. }
  449. \]
  450. The syntax for $C_0$ is defined in Figure~\ref{fig:c0-syntax}. The
  451. $C_0$ language supports the same operators as $S_0$ but the arguments
  452. of operators are now restricted to just variables and integers. The
  453. \key{let} construct of $S_0$ is replaced by an assignment statement
  454. and there is a \key{return} construct to specify the return value of
  455. the program. A program consists of a sequence of statements that
  456. include at least one \key{return} statement.
  457. \begin{figure}[htbp]
  458. \[
  459. \begin{array}{lcl}
  460. \Arg &::=& \Int \mid \Var \\
  461. \Exp &::=& \Arg \mid (\Op \; \Arg^{*})\\
  462. \Stmt &::=& \ASSIGN{\Var}{\Exp} \mid \RETURN{\Arg} \\
  463. \Prog & ::= & (\key{program}\;\itm{info}\;\Stmt^{+})
  464. \end{array}
  465. \]
  466. \caption{The $C_0$ intermediate language.}
  467. \label{fig:c0-syntax}
  468. \end{figure}
  469. To get from $C_0$ to x86-64 assembly requires three more steps, which
  470. we discuss below.
  471. \[\large
  472. \xymatrix@=60pt{
  473. C_0 \ar[r]^-{\textsf{select\_instr.}}
  474. & \text{x86}^{*} \ar[r]^-{\textsf{assign\_homes}} & \text{x86}^{*}
  475. \ar[r]^-{\textsf{patch\_instr.}}
  476. & \text{x86}
  477. }
  478. \]
  479. We handle difference \#1, concerning the format of arithmetic
  480. instructions, in the \textsf{select\_instructions} pass. The result
  481. of this pass produces programs consisting of x86-64 instructions that
  482. use variables.
  483. %
  484. As there are only 16 registers, we cannot always map variables to
  485. registers (difference \#3). Fortunately, the stack can grow quite, so
  486. we can map variables to locations on the stack. This is handled in the
  487. \textsf{assign\_homes} pass. The topic of
  488. Chapter~\ref{ch:register-allocation} is implementing a smarter
  489. approach in which we make a best-effort to map variables to registers,
  490. resorting to the stack only when necessary.
  491. The final pass in our journey to x86 handles an indiosycracy of x86
  492. assembly. Many x86 instructions have two arguments but only one of the
  493. arguments may be a memory reference. Because we are mapping variables
  494. to stack locations, many of our generated instructions will violate
  495. this restriction. The purpose of the \textsf{patch\_instructions} pass
  496. is to fix this problem by replacing every bad instruction with a short
  497. sequence of instructions that use the \key{rax} register.
  498. \section{Uniquify}
  499. The purpose of this pass is to make sure that each \key{let} uses a
  500. unique variable name. For example, the \textsf{uniquify} pass could
  501. translate
  502. \[
  503. \LET{x}{32}{ \BINOP{+}{ \LET{x}{10}{x} }{ x } }
  504. \]
  505. to
  506. \[
  507. \LET{x.1}{32}{ \BINOP{+}{ \LET{x.2}{10}{x.2} }{ x.1 } }
  508. \]
  509. We recommend implementing \textsf{uniquify} as a recursive function
  510. that mostly just copies the input program. However, when encountering
  511. a \key{let}, it should generate a unique name for the variable (the
  512. Racket function \key{gensym} is handy for this) and associate the old
  513. name with the new unique name in an association list. The
  514. \textsf{uniquify} function will need to access this association list
  515. when it gets to a variable reference, so we add another paramter to
  516. \textsf{uniquify} for the association list.
  517. \section{Flatten}
  518. The purpose of the \textsf{flatten} pass is to get rid of nested
  519. expressions, such as the $\UNIOP{-}{10}$ in the following program,
  520. without changing the behavior of the program.
  521. \[
  522. \BINOP{+}{52}{ \UNIOP{-}{10} }
  523. \]
  524. This can be accomplished by introducing a new variable, assigning the
  525. nested expression to the new variable, and then using the new variable
  526. in place of the nested expressions. For example, the above program is
  527. translated to the following one.
  528. \[
  529. \begin{array}{l}
  530. \ASSIGN{ \itm{x} }{ \UNIOP{-}{10} } \\
  531. \RETURN{ \BINOP{+}{52}{ \itm{x} } }
  532. \end{array}
  533. \]
  534. We recommend implementing \textsf{flatten} as a recursive function
  535. that returns two things, 1) the newly flattened expression, and 2) a
  536. list of assignment statements, one for each of the new variables
  537. introduced while flattening the expression.
  538. Take special care for programs such as the following that initialize
  539. variables with integers or other variables.
  540. \[
  541. \LET{a}{42}{ \LET{b}{a}{ b }}
  542. \]
  543. This program should be translated to
  544. \[
  545. \ASSIGN{a}{42} \;
  546. \ASSIGN{b}{a} \;
  547. \RETURN{b}
  548. \]
  549. and not the following, which could result from a naive implementation
  550. of \textsf{flatten}.
  551. \[
  552. \ASSIGN{x.1}{42}\;
  553. \ASSIGN{a}{x.1}\;
  554. \ASSIGN{x.2}{a}\;
  555. \ASSIGN{b}{x.2}\;
  556. \RETURN{b}
  557. \]
  558. \section{Select Instructions}
  559. In the \textsf{select\_instructions} pass we begin the work of
  560. translating from $C_0$ to x86. The target language of this pass is a
  561. pseudo-x86 language that still uses variables, so we add an AST node
  562. of the form $\VAR{\itm{var}}$. The \textsf{select\_instructions} pass
  563. deals with the differing format of arithmetic operations. For example,
  564. in $C_0$ an addition operation could take the following form:
  565. \[
  566. \ASSIGN{x}{ \BINOP{+}{10}{32} }
  567. \]
  568. To translate to x86, we need to express this addition using the
  569. \key{add} instruction that does an inplace update. So we first move
  570. $10$ to $x$ then perform the \key{add}.
  571. \[
  572. (\key{mov}\,\INT{10}\, \VAR{x})\; (\key{add} \;\INT{32}\; \VAR{x})
  573. \]
  574. There are some cases that require special care to avoid generating
  575. needlessly complicated code. If one of the arguments is the same as
  576. the left-hand side of the assignment, then there is no need for the
  577. extra move instruction. For example, the following
  578. \[
  579. \ASSIGN{x}{ \BINOP{+}{10}{x} }
  580. \quad\text{should translate to}\quad
  581. (\key{add} \; \INT{10}\; \VAR{x})
  582. \]
  583. Regarding the \RETURN{e} statement of $C_0$, we recommend treating it
  584. as an assignment to the \key{rax} register and let the procedure
  585. conclusion handle the transfer of control back to the calling
  586. procedure.
  587. \section{Assign Homes}
  588. As discussed in Section~\ref{sec:plan-s0-x86}, the
  589. \textsf{assign\_homes} pass places all of the variables on the stack.
  590. Consider again the example $S_0$ program $\BINOP{+}{52}{ \UNIOP{-}{10} }$,
  591. which after \textsf{select\_instructions} looks like the following.
  592. \[
  593. \begin{array}{l}
  594. (\key{mov}\;\INT{10}\; \VAR{x})\\
  595. (\key{neg}\; \VAR{x})\\
  596. (\key{mov}\; \INT{52}\; \REG{\itm{rax}})\\
  597. (\key{add}\; \VAR{x} \REG{\itm{rax}})
  598. \end{array}
  599. \]
  600. The one and only variable $x$ is assigned to stack location
  601. \key{-8(\%rbp)}, so the \textsf{assign\_homes} pass translates the
  602. above to
  603. \[
  604. \begin{array}{l}
  605. (\key{mov}\;\INT{10}\; \STACKLOC{{-}8})\\
  606. (\key{neg}\; \STACKLOC{{-}8})\\
  607. (\key{mov}\; \INT{52}\; \REG{\itm{rax}})\\
  608. (\key{add}\; \STACKLOC{{-}8}\; \REG{\itm{rax}})
  609. \end{array}
  610. \]
  611. In the process of assigning stack locations to variables, it is
  612. convenient to compute and store the size of the frame which will be
  613. needed later to generate the procedure conclusion.
  614. \section{Patch Instructions}
  615. The purpose of this pass is to make sure that each instruction adheres
  616. to the restrictions regarding which arguments can be memory
  617. references. For most instructions, the rule is that at most one
  618. argument may be a memory reference.
  619. Consider again the following example.
  620. \[
  621. \LET{a}{42}{ \LET{b}{a}{ b }}
  622. \]
  623. After \textsf{assign\_homes} pass, the above has been translated to
  624. \[
  625. \begin{array}{l}
  626. (\key{mov} \;\INT{42}\; \STACKLOC{{-}8})\\
  627. (\key{mov}\;\STACKLOC{{-}8}\; \STACKLOC{{-}16})\\
  628. (\key{mov}\;\STACKLOC{{-}16}\; \REG{\itm{rax}})
  629. \end{array}
  630. \]
  631. The second \key{mov} instruction is problematic because both arguments
  632. are stack locations. We suggest fixing this problem by moving from the
  633. source to \key{rax} and then from \key{rax} to the destination, as
  634. follows.
  635. \[
  636. \begin{array}{l}
  637. (\key{mov} \;\INT{42}\; \STACKLOC{{-}8})\\
  638. (\key{mov}\;\STACKLOC{{-}8}\; \REG{\itm{rax}})\\
  639. (\key{mov}\;\REG{\itm{rax}}\; \STACKLOC{{-}16})\\
  640. (\key{mov}\;\STACKLOC{{-}16}\; \REG{\itm{rax}})
  641. \end{array}
  642. \]
  643. The \key{imul} instruction is a special case because the destination
  644. argument must be a register.
  645. \section{Testing with the Interpreters}
  646. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  647. \chapter{Register Allocation}
  648. \label{ch:register-allocation}
  649. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  650. \chapter{Booleans, Conditions, and Type Checking}
  651. \label{ch:bool-types}
  652. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  653. \chapter{Tuples and Heap Allocation}
  654. \label{ch:tuples}
  655. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  656. \chapter{Functions}
  657. \label{ch:functions}
  658. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  659. \chapter{Lexically Scoped Functions}
  660. \label{ch:lambdas}
  661. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  662. \chapter{The Dynamic Type}
  663. \label{ch:type-dynamic}
  664. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  665. \chapter{Mutable Lists}
  666. \label{ch:mutable-lists}
  667. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  668. \chapter{Parametric Polymorphism}
  669. \label{ch:parametric-polymorphism}
  670. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  671. \chapter{High-level Optimization}
  672. \label{ch:high-level-optimization}
  673. \bibliographystyle{plainnat}
  674. \bibliography{all}
  675. \end{document}
  676. %% LocalWords: Dybvig Waddell Abdulaziz Ghuloum Dipanwita
  677. %% LocalWords: Sarkar lcl Matz aa representable