book.tex 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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. language=Lisp,
  17. basicstyle=\ttfamily\small,
  18. escapechar=@
  19. }
  20. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  21. % 'dedication' environment: To add a dedication paragraph at the start of book %
  22. % Source: http://www.tug.org/pipermail/texhax/2010-June/015184.html %
  23. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  24. \newenvironment{dedication}
  25. {
  26. \cleardoublepage
  27. \thispagestyle{empty}
  28. \vspace*{\stretch{1}}
  29. \hfill\begin{minipage}[t]{0.66\textwidth}
  30. \raggedright
  31. }
  32. {
  33. \end{minipage}
  34. \vspace*{\stretch{3}}
  35. \clearpage
  36. }
  37. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  38. % Chapter quote at the start of chapter %
  39. % Source: http://tex.stackexchange.com/a/53380 %
  40. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  41. \makeatletter
  42. \renewcommand{\@chapapp}{}% Not necessary...
  43. \newenvironment{chapquote}[2][2em]
  44. {\setlength{\@tempdima}{#1}%
  45. \def\chapquote@author{#2}%
  46. \parshape 1 \@tempdima \dimexpr\textwidth-2\@tempdima\relax%
  47. \itshape}
  48. {\par\normalfont\hfill--\ \chapquote@author\hspace*{\@tempdima}\par\bigskip}
  49. \makeatother
  50. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  51. \newcommand{\itm}[1]{\ensuremath{\mathit{#1}}}
  52. \newcommand{\Stmt}{\itm{stmt}}
  53. \newcommand{\Exp}{\itm{exp}}
  54. \newcommand{\Instr}{\itm{instr}}
  55. \newcommand{\Prog}{\itm{prog}}
  56. \newcommand{\Arg}{\itm{arg}}
  57. \newcommand{\Int}{\itm{int}}
  58. \newcommand{\Var}{\itm{var}}
  59. \newcommand{\Op}{\itm{op}}
  60. \newcommand{\key}[1]{\texttt{#1}}
  61. \newcommand{\READ}{(\key{read})}
  62. \newcommand{\UNIOP}[2]{(\key{#1}\,#2)}
  63. \newcommand{\BINOP}[3]{(\key{#1}\,#2\,#3)}
  64. \newcommand{\LET}[3]{(\key{let}\,([#1\;#2])\,#3)}
  65. \newcommand{\ASSIGN}[2]{(\key{assign}\,#1\;#2)}
  66. \newcommand{\RETURN}[1]{(\key{return}\,#1)}
  67. \newcommand{\INT}[1]{(\key{int}\;#1)}
  68. \newcommand{\REG}[1]{(\key{reg}\;#1)}
  69. \newcommand{\VAR}[1]{(\key{var}\;#1)}
  70. \newcommand{\STACKLOC}[1]{(\key{stack}\;#1)}
  71. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  72. \title{\Huge \textbf{Essentials of Compilation} \\
  73. \huge An Incremental Approach}
  74. \author{\textsc{Jeremy G. Siek}
  75. \thanks{\url{http://homes.soic.indiana.edu/jsiek/}}
  76. }
  77. \begin{document}
  78. \frontmatter
  79. \maketitle
  80. \begin{dedication}
  81. This book is dedicated to the programming language wonks at Indiana
  82. University.
  83. \end{dedication}
  84. \tableofcontents
  85. %\listoffigures
  86. %\listoftables
  87. \mainmatter
  88. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  89. \chapter*{Preface}
  90. Talk about nano-pass \citep{Sarkar:2004fk,Keep:2012aa} and incremental
  91. compilers \citep{Ghuloum:2006bh}.
  92. %\section*{Structure of book}
  93. % You might want to add short description about each chapter in this book.
  94. %\section*{About the companion website}
  95. %The website\footnote{\url{https://github.com/amberj/latex-book-template}} for %this file contains:
  96. %\begin{itemize}
  97. % \item A link to (freely downlodable) latest version of this document.
  98. % \item Link to download LaTeX source for this document.
  99. % \item Miscellaneous material (e.g. suggested readings etc).
  100. %\end{itemize}
  101. \section*{Acknowledgments}
  102. Need to give thanks to
  103. \begin{itemize}
  104. \item Kent Dybvig
  105. \item Daniel P. Friedman
  106. \item Abdulaziz Ghuloum
  107. \item Oscar Waddell
  108. \item Dipanwita Sarkar
  109. \item Ronald Garcia
  110. \item Bor-Yuh Evan Chang
  111. \end{itemize}
  112. %\mbox{}\\
  113. %\noindent Amber Jain \\
  114. %\noindent \url{http://amberj.devio.us/}
  115. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  116. \chapter{Integers and Variables}
  117. \label{ch:int-exp}
  118. %\begin{chapquote}{Author's name, \textit{Source of this quote}}
  119. %``This is a quote and I don't know who said this.''
  120. %\end{chapquote}
  121. \section{The $S_0$ Language}
  122. The $S_0$ language includes integers, operations on integers,
  123. (arithmetic and input), and variable definitions. The syntax of the
  124. $S_0$ language is defined by the grammar in
  125. Figure~\ref{fig:s0-syntax}. This language is rich enough to exhibit
  126. several compilation techniques but simple enough so that we can
  127. implement a compiler for it in two weeks of hard work. To give the
  128. reader a feeling for the scale of this first compiler, the instructor
  129. solution for the $S_0$ compiler consists of 6 recursive functions and
  130. a few small helper functions that together span 256 lines of code.
  131. \begin{figure}[htbp]
  132. \centering
  133. \fbox{
  134. \begin{minipage}{0.85\textwidth}
  135. \[
  136. \begin{array}{lcl}
  137. \Op &::=& \key{+} \mid \key{-} \mid \key{*} \mid \key{read} \\
  138. \Exp &::=& \Int \mid (\Op \; \Exp^{*}) \mid \Var \mid \LET{\Var}{\Exp}{\Exp}
  139. \end{array}
  140. \]
  141. \end{minipage}
  142. }
  143. \caption{The syntax of the $S_0$ language. The abbreviation \Op{} is
  144. short for operator, \Exp{} is short for expression, \Int{} for integer,
  145. and \Var{} for variable.}
  146. \label{fig:s0-syntax}
  147. \end{figure}
  148. The result of evaluating an expression is a value. For $S_0$, values
  149. are integers. To make it straightforward to map these integers onto
  150. x86-64 assembly~\citep{Matz:2013aa}, we restrict the integers to just
  151. those representable with 64-bits, the range $-2^{63}$ to $2^{63}$.
  152. We will walk through some examples of $S_0$ programs, commenting on
  153. aspects of the language that will be relevant to compiling it. We
  154. start with one of the simplest $S_0$ programs; it adds two integers.
  155. \[
  156. \BINOP{+}{10}{32}
  157. \]
  158. The result is $42$, as you might expected.
  159. %
  160. The next example demonstrates that expressions may be nested within
  161. each other, in this case nesting several additions and negations.
  162. \[
  163. \BINOP{+}{10}{ \UNIOP{-}{ \BINOP{+}{12}{20} } }
  164. \]
  165. What is the result of the above program?
  166. The \key{let} construct stores a value in a variable which can then be
  167. used within the body of the \key{let}. So the following program stores
  168. $32$ in $x$ and then computes $\BINOP{+}{10}{x}$, producing $42$.
  169. \[
  170. \LET{x}{ \BINOP{+}{12}{20} }{ \BINOP{+}{10}{x} }
  171. \]
  172. When there are multiple \key{let}'s for the same variable, the closest
  173. enclosing \key{let} is used. Consider the following program with two
  174. \key{let}'s that define variables named $x$.
  175. \[
  176. \LET{x}{32}{ \BINOP{+}{ \LET{x}{10}{x} }{ x } }
  177. \]
  178. For the purposes of showing which variable uses correspond to which
  179. definitions, the following shows the $x$'s annotated with subscripts
  180. to distinguish them.
  181. \[
  182. \LET{x_1}{32}{ \BINOP{+}{ \LET{x_2}{10}{x_2} }{ x_1 } }
  183. \]
  184. The \key{read} operation prompts the user of the program for an
  185. integer. Given an input of $10$, the following program produces $42$.
  186. \[
  187. \BINOP{+}{(\key{read})}{32}
  188. \]
  189. We include the \key{read} operation in $S_0$ to demonstrate that order
  190. of evaluation can make a different. Given the input $52$ then $10$,
  191. the following produces $42$ (and not $-42$).
  192. \[
  193. \LET{x}{\READ}{ \LET{y}{\READ}{ \BINOP{-}{x}{y} } }
  194. \]
  195. The initializing expression is always evaluated before the body of the
  196. \key{let}, so in the above, the \key{read} for $x$ is performed before
  197. the \key{read} for $y$.
  198. %
  199. The behavior of the following program is somewhat subtle because
  200. Scheme does not specify an evaluation order for arguments of an
  201. operator such as $-$.
  202. \[
  203. \BINOP{-}{\READ}{\READ}
  204. \]
  205. Given the input $42$ then $10$, the above program can result in either
  206. $42$ or $-42$, depending on the whims of the Scheme implementation.
  207. The goal for this chapter is to implement a compiler that translates
  208. any program $p \in S_0$ into a x86-64 assembly program $p'$ such that
  209. the assembly program exhibits the same behavior on an x86 computer as
  210. the $S_0$ program running in a Scheme implementation.
  211. \[
  212. \xymatrix{
  213. 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}}\\
  214. & & n \in \mathbb{Z}
  215. }
  216. \]
  217. In the next section we introduce enough of the x86-64 assembly
  218. language to compile $S_0$.
  219. \section{The x86-64 Assembly Language}
  220. An x86-64 program is a sequence of instructions. The instructions
  221. manipulate 16 variables called \emph{registers} and can also load and
  222. store values into \emph{memory}. Memory is a mapping of 64-bit
  223. addresses to 64-bit values. The syntax $n(r)$ is used to read the
  224. address $a$ stored in register $r$ and then offset it by $n$ bytes (8
  225. bits), producing the address $a + n$. The arithmetic instructions,
  226. such as $\key{addq}\,s\,d$, read from the source $s$ and destination
  227. argument $d$, apply the arithmetic operation, then stores the result
  228. in the destination $d$. In this case, computing $d \gets d + s$. The
  229. move instruction, $\key{movq}\,s\,d$ reads from $s$ and stores the
  230. result in $d$. The $\key{callq}\,\mathit{label}$ instruction executes
  231. the procedure specified by the label, which we shall use to implement
  232. \key{read}. Figure~\ref{fig:x86-a} defines the syntax for this subset
  233. of the x86-64 assembly language.
  234. \begin{figure}[tbp]
  235. \fbox{
  236. \begin{minipage}{0.96\textwidth}
  237. \[
  238. \begin{array}{lcl}
  239. \itm{register} &::=& \key{rsp} \mid \key{rbp} \mid \key{rax} \mid \key{rbx} \mid \key{rcx}
  240. \mid \key{rdx} \mid \key{rsi} \mid \key{rdi} \mid \\
  241. && \key{r8} \mid \key{r9} \mid \key{r10}
  242. \mid \key{r11} \mid \key{r12} \mid \key{r13}
  243. \mid \key{r14} \mid \key{r15} \\
  244. \Arg &::=& \key{\$}\Int \mid \key{\%}\itm{register} \mid \Int(\key{\%}\itm{register}) \\
  245. \Instr &::=& \key{addq} \; \Arg, \Arg \mid
  246. \key{subq} \; \Arg, \Arg \mid
  247. \key{imulq} \; \Arg,\Arg \mid
  248. \key{negq} \; \Arg \mid \\
  249. && \key{movq} \; \Arg, \Arg \mid
  250. \key{callq} \; \mathit{label} \mid
  251. \key{pushq}\;\Arg \mid \key{popq}\;\Arg \mid \key{retq} \\
  252. \Prog &::= & \key{.globl \_main}\\
  253. & & \key{\_main:} \; \Instr^{+}
  254. \end{array}
  255. \]
  256. \end{minipage}
  257. }
  258. \caption{A subset of the x86-64 assembly language.}
  259. \label{fig:x86-a}
  260. \end{figure}
  261. Figure~\ref{fig:p0-x86} depicts an x86-64 program that is equivalent
  262. to $\BINOP{+}{10}{32}$. The \key{globl} directive says that the
  263. \key{\_main} procedure is externally visible, which is necessary so
  264. that the operating system can call it. The label \key{\_main:}
  265. indicates the beginning of the \key{\_main} procedure. The
  266. instruction $\key{movq}\,\$10, \%\key{rax}$ puts $10$ into the
  267. register \key{rax}. The following instruction $\key{addq}\,\key{\$}32,
  268. \key{\%rax}$ adds $32$ to the $10$ in \key{rax} and puts the result,
  269. $42$, back into \key{rax}. The instruction \key{retq} finishes the
  270. \key{\_main} function by returning the integer in the \key{rax}
  271. register to the operating system.
  272. \begin{figure}[htbp]
  273. \centering
  274. \begin{minipage}{0.6\textwidth}
  275. \begin{lstlisting}
  276. .globl _main
  277. _main:
  278. movq $10, %rax
  279. addq $32, %rax
  280. retq
  281. \end{lstlisting}
  282. \end{minipage}
  283. \caption{A simple x86-64 program equivalent to $\BINOP{+}{10}{32}$.}
  284. \label{fig:p0-x86}
  285. \end{figure}
  286. The next example exhibits the use of memory. Figure~\ref{fig:p1-x86}
  287. lists an x86-64 program that is equivalent to $\BINOP{+}{52}{
  288. \UNIOP{-}{10} }$. To understand how this x86-64 program uses memory,
  289. we need to explain a region of memory called called the
  290. \emph{procedure call stack} (\emph{stack} for short). The stack
  291. consists of a separate \emph{frame} for each procedure call. The
  292. memory layout for an individual frame is shown in
  293. Figure~\ref{fig:frame}. The register \key{rsp} is called the
  294. \emph{stack pointer} and points to the item at the top of the
  295. stack. The stack grows downward in memory, so we increase the size of
  296. the stack by subtracting from the stack pointer. The frame size is
  297. required to be a multiple of 16 bytes. The register \key{rbp} is the
  298. \emph{base pointer} which serves two purposes: 1) it saves the
  299. location of the stack pointer for the procedure that called the
  300. current one and 2) it is used to access variables associated with the
  301. current procedure. We number the variables from $1$ to $n$. Variable
  302. $1$ is stored at address $-8\key{(\%rbp)}$, variable $2$ at
  303. $-16\key{(\%rbp)}$, etc.
  304. \begin{figure}
  305. \centering
  306. \begin{minipage}{0.6\textwidth}
  307. \begin{lstlisting}
  308. .globl _main
  309. _main:
  310. pushq %rbp
  311. movq %rsp, %rbp
  312. subq $16, %rsp
  313. movq $10, -8(%rbp)
  314. negq -8(%rbp)
  315. movq $52, %rax
  316. addq -8(%rbp), %rax
  317. addq $16, %rsp
  318. popq %rbp
  319. retq
  320. \end{lstlisting}
  321. \end{minipage}
  322. \caption{An x86-64 program equivalent to $\BINOP{+}{52}{\UNIOP{-}{10} }$.}
  323. \label{fig:p1-x86}
  324. \end{figure}
  325. \begin{figure}
  326. \centering
  327. \begin{tabular}{|r|l|} \hline
  328. Position & Contents \\ \hline
  329. 8(\key{\%rbp}) & return address \\
  330. 0(\key{\%rbp}) & old \key{rbp} \\
  331. -8(\key{\%rbp}) & variable $1$ \\
  332. -16(\key{\%rbp}) & variable $2$ \\
  333. \ldots & \ldots \\
  334. 0(\key{\%rsp}) & variable $n$\\ \hline
  335. \end{tabular}
  336. \caption{Memory layout of a frame.}
  337. \label{fig:frame}
  338. \end{figure}
  339. Getting back to the program in Figure~\ref{fig:p1-x86}, the first
  340. three instructions are the typical prelude for a procedure. The
  341. instruction \key{pushq \%rbp} saves the base pointer for the procedure
  342. that called the current one onto the stack and subtracts $8$ from the
  343. stack pointer. The second instruction \key{movq \%rsp, \%rbp} changes
  344. the base pointer to the top of the stack. The instruction \key{subq
  345. \$16, \%rsp} moves the stack pointer down to make enough room for
  346. storing variables. This program just needs one variable ($8$ bytes)
  347. but because the frame size is required to be a multiple of 16 bytes,
  348. it rounds to 16 bytes.
  349. The next four instructions carry out the work of computing
  350. $\BINOP{+}{52}{\UNIOP{-}{10} }$. The first instruction \key{movq \$10,
  351. -8(\%rbp)} stores $10$ in variable $1$. The instruction \key{negq
  352. -8(\%rbp)} changes variable $1$ to $-10$. The \key{movq \$52, \%rax}
  353. places $52$ in the register \key{rax} and \key{addq -8(\%rbp), \%rax}
  354. adds the contents of variable $1$ to \key{rax}, at which point
  355. \key{rax} contains $42$.
  356. The last three instructions are the typical \emph{conclusion} of a
  357. procedure. The \key{addq \$16, \%rsp} instruction moves the stack
  358. pointer back to point at the old base pointer. The amount added here
  359. needs to match the amount that was subtracted in the prelude of the
  360. procedure. Then \key{popq \%rbp} returns the old base pointer to
  361. \key{rbp} and adds $8$ to the stack pointer. The \key{retq}
  362. instruction jumps back to the procedure that called this one and
  363. subtracts 8 from the stack pointer.
  364. The compiler will need a convenient representation for manipulating
  365. x86 programs, so we define an abstract syntax for x86 in
  366. Figure~\ref{fig:x86-ast-a}. The \itm{info} field of the \key{program}
  367. AST node is for storing auxilliary information that needs to be
  368. communicated from one pass to the next. The function \key{print-x86}
  369. provided in the supplemental code converts an x86 abstract syntax tree
  370. into the text representation for x86 (Figure~\ref{fig:x86-a}).
  371. \begin{figure}[tbp]
  372. \fbox{
  373. \begin{minipage}{0.96\textwidth}
  374. \[
  375. \begin{array}{lcl}
  376. \Arg &::=& \INT{\Int} \mid \REG{\itm{register}}
  377. \mid \STACKLOC{\Int} \\
  378. \Instr &::=& (\key{add} \; \Arg\; \Arg) \mid
  379. (\key{sub} \; \Arg\; \Arg) \mid
  380. (\key{imul} \; \Arg\;\Arg) \mid
  381. (\key{neg} \; \Arg) \mid \\
  382. && (\key{mov} \; \Arg\; \Arg) \mid
  383. (\key{call} \; \mathit{label}) \mid
  384. (\key{push}\;\Arg) \mid (\key{pop}\;\Arg) \mid (\key{ret}) \\
  385. \Prog &::= & (\key{program} \;\itm{info} \; \Instr^{+})
  386. \end{array}
  387. \]
  388. \end{minipage}
  389. }
  390. \caption{Abstract syntax for x86-64 assembly.}
  391. \label{fig:x86-ast-a}
  392. \end{figure}
  393. \section{Planning the route from $S_0$ to x86-64}
  394. \label{sec:plan-s0-x86}
  395. To compile one language to another it helps to focus on the
  396. differences between the two languages. It is these differences that
  397. the compiler will need to bridge. What are the differences between
  398. $S_0$ and x86-64 assembly? Here we list some of the most important the
  399. differences.
  400. \begin{enumerate}
  401. \item x86-64 arithmetic instructions typically take two arguments and
  402. update the second argument in place. In contrast, $S_0$ arithmetic
  403. operations only read their arguments and produce a new value.
  404. \item An argument to an $S_0$ operator can be any expression, whereas
  405. x86-64 instructions restrict their arguments to integers, registers,
  406. and memory locations.
  407. \item An $S_0$ program can have any number of variables whereas x86-64
  408. has only 16 registers.
  409. \item Variables in $S_0$ can overshadow other variables with the same
  410. name. The registers and memory locations of x86-64 all have unique
  411. names.
  412. \end{enumerate}
  413. We ease the challenge of compiling from $S_0$ to x86 by breaking down
  414. the problem into several steps, dealing with the above differences one
  415. at a time. The main question then becomes: in what order to we tackle
  416. these differences? This is often one of the most challenging questions
  417. that a compiler writer must answer because some orderings may be much
  418. more difficult to implement than others. It is difficult to know ahead
  419. of time which orders will be better so often some trial-and-error is
  420. involved. However, we can try to plan ahead and choose the orderings
  421. based on what we find out.
  422. For example, to handle difference \#2 (nested expressions), we shall
  423. introduce new variables and pull apart the nested expressions into a
  424. sequence of assignment statements. To deal with difference \#3 we
  425. will be replacing variables with registers and/or stack
  426. locations. Thus, it makes sense to deal with \#2 before \#3 so that
  427. \#3 can replace both the original variables and the new ones. Next,
  428. consider where \#1 should fit in. Because it has to do with the format
  429. of x86 instructions, it makes more sense after we have flattened the
  430. nested expressions (\#2). Finally, when should we deal with \#4
  431. (variable overshadowing)? We shall be solving this problem by
  432. renaming variables to make sure they have unique names. Recall that
  433. our plan for \#2 involves moving nested expressions, which could be
  434. problematic if it changes the shadowing of variables. However, if we
  435. deal with \#4 first, then it will not be an issue. Thus, we arrive at
  436. the following ordering.
  437. \[
  438. \xymatrix{
  439. 4 \ar[r] & 2 \ar[r] & 1 \ar[r] & 3
  440. }
  441. \]
  442. We further simplify the translation from $S_0$ to x86 by identifying
  443. an intermediate language named $C_0$, roughly half-way between $S_0$
  444. and x86, to provide a rest stop along the way. The name $C_0$ comes
  445. from this language being vaguely similar to the $C$ language. The
  446. differences \#4 and \#1, regarding variables and nested expressions,
  447. are handled by the passes \textsf{uniquify} and \textsf{flatten} that
  448. bring us to $C_0$.
  449. \[\large
  450. \xymatrix@=50pt{
  451. S_0 \ar@/^/[r]^-{\textsf{uniquify}} &
  452. S_0 \ar@/^/[r]^-{\textsf{flatten}} &
  453. C_0
  454. }
  455. \]
  456. The syntax for $C_0$ is defined in Figure~\ref{fig:c0-syntax}. The
  457. $C_0$ language supports the same operators as $S_0$ but the arguments
  458. of operators are now restricted to just variables and integers. The
  459. \key{let} construct of $S_0$ is replaced by an assignment statement
  460. and there is a \key{return} construct to specify the return value of
  461. the program. A program consists of a sequence of statements that
  462. include at least one \key{return} statement.
  463. \begin{figure}[htbp]
  464. \[
  465. \begin{array}{lcl}
  466. \Arg &::=& \Int \mid \Var \\
  467. \Exp &::=& \Arg \mid (\Op \; \Arg^{*})\\
  468. \Stmt &::=& \ASSIGN{\Var}{\Exp} \mid \RETURN{\Arg} \\
  469. \Prog & ::= & (\key{program}\;\itm{info}\;\Stmt^{+})
  470. \end{array}
  471. \]
  472. \caption{The $C_0$ intermediate language.}
  473. \label{fig:c0-syntax}
  474. \end{figure}
  475. To get from $C_0$ to x86-64 assembly requires three more steps, which
  476. we discuss below.
  477. \[\large
  478. \xymatrix@=50pt{
  479. C_0 \ar@/^/[r]^-{\textsf{select\_instr.}}
  480. & \text{x86}^{*} \ar@/^/[r]^-{\textsf{assign\_homes}}
  481. & \text{x86}^{*} \ar@/^/[r]^-{\textsf{patch\_instr.}}
  482. & \text{x86}
  483. }
  484. \]
  485. We handle difference \#1, concerning the format of arithmetic
  486. instructions, in the \textsf{select\_instructions} pass. The result
  487. of this pass produces programs consisting of x86-64 instructions that
  488. use variables.
  489. %
  490. As there are only 16 registers, we cannot always map variables to
  491. registers (difference \#3). Fortunately, the stack can grow quite, so
  492. we can map variables to locations on the stack. This is handled in the
  493. \textsf{assign\_homes} pass. The topic of
  494. Chapter~\ref{ch:register-allocation} is implementing a smarter
  495. approach in which we make a best-effort to map variables to registers,
  496. resorting to the stack only when necessary.
  497. The final pass in our journey to x86 handles an indiosycracy of x86
  498. assembly. Many x86 instructions have two arguments but only one of the
  499. arguments may be a memory reference. Because we are mapping variables
  500. to stack locations, many of our generated instructions will violate
  501. this restriction. The purpose of the \textsf{patch\_instructions} pass
  502. is to fix this problem by replacing every bad instruction with a short
  503. sequence of instructions that use the \key{rax} register.
  504. \section{Uniquify Variables}
  505. The purpose of this pass is to make sure that each \key{let} uses a
  506. unique variable name. For example, the \textsf{uniquify} pass could
  507. translate
  508. \[
  509. \LET{x}{32}{ \BINOP{+}{ \LET{x}{10}{x} }{ x } }
  510. \]
  511. to
  512. \[
  513. \LET{x.1}{32}{ \BINOP{+}{ \LET{x.2}{10}{x.2} }{ x.1 } }
  514. \]
  515. We recommend implementing \textsf{uniquify} as a recursive function
  516. that mostly just copies the input program. However, when encountering
  517. a \key{let}, it should generate a unique name for the variable (the
  518. Racket function \key{gensym} is handy for this) and associate the old
  519. name with the new unique name in an association list. The
  520. \textsf{uniquify} function will need to access this association list
  521. when it gets to a variable reference, so we add another paramter to
  522. \textsf{uniquify} for the association list.
  523. \section{Flatten Expressions}
  524. The purpose of the \textsf{flatten} pass is to get rid of nested
  525. expressions, such as the $\UNIOP{-}{10}$ in the following program,
  526. without changing the behavior of the program.
  527. \[
  528. \BINOP{+}{52}{ \UNIOP{-}{10} }
  529. \]
  530. This can be accomplished by introducing a new variable, assigning the
  531. nested expression to the new variable, and then using the new variable
  532. in place of the nested expressions. For example, the above program is
  533. translated to the following one.
  534. \[
  535. \begin{array}{l}
  536. \ASSIGN{ \itm{x} }{ \UNIOP{-}{10} } \\
  537. \RETURN{ \BINOP{+}{52}{ \itm{x} } }
  538. \end{array}
  539. \]
  540. We recommend implementing \textsf{flatten} as a recursive function
  541. that returns two things, 1) the newly flattened expression, and 2) a
  542. list of assignment statements, one for each of the new variables
  543. introduced while flattening the expression.
  544. Take special care for programs such as the following that initialize
  545. variables with integers or other variables.
  546. \[
  547. \LET{a}{42}{ \LET{b}{a}{ b }}
  548. \]
  549. This program should be translated to
  550. \[
  551. \ASSIGN{a}{42} \;
  552. \ASSIGN{b}{a} \;
  553. \RETURN{b}
  554. \]
  555. and not the following, which could result from a naive implementation
  556. of \textsf{flatten}.
  557. \[
  558. \ASSIGN{x.1}{42}\;
  559. \ASSIGN{a}{x.1}\;
  560. \ASSIGN{x.2}{a}\;
  561. \ASSIGN{b}{x.2}\;
  562. \RETURN{b}
  563. \]
  564. \section{Select Instructions}
  565. In the \textsf{select\_instructions} pass we begin the work of
  566. translating from $C_0$ to x86. The target language of this pass is a
  567. pseudo-x86 language that still uses variables, so we add an AST node
  568. of the form $\VAR{\itm{var}}$. The \textsf{select\_instructions} pass
  569. deals with the differing format of arithmetic operations. For example,
  570. in $C_0$ an addition operation could take the following form:
  571. \[
  572. \ASSIGN{x}{ \BINOP{+}{10}{32} }
  573. \]
  574. To translate to x86, we need to express this addition using the
  575. \key{add} instruction that does an inplace update. So we first move
  576. $10$ to $x$ then perform the \key{add}.
  577. \[
  578. (\key{mov}\,\INT{10}\, \VAR{x})\; (\key{add} \;\INT{32}\; \VAR{x})
  579. \]
  580. There are some cases that require special care to avoid generating
  581. needlessly complicated code. If one of the arguments is the same as
  582. the left-hand side of the assignment, then there is no need for the
  583. extra move instruction. For example, the following
  584. \[
  585. \ASSIGN{x}{ \BINOP{+}{10}{x} }
  586. \quad\text{should translate to}\quad
  587. (\key{add} \; \INT{10}\; \VAR{x})
  588. \]
  589. Regarding the \RETURN{e} statement of $C_0$, we recommend treating it
  590. as an assignment to the \key{rax} register and let the procedure
  591. conclusion handle the transfer of control back to the calling
  592. procedure.
  593. \section{Assign Homes}
  594. As discussed in Section~\ref{sec:plan-s0-x86}, the
  595. \textsf{assign\_homes} pass places all of the variables on the stack.
  596. Consider again the example $S_0$ program $\BINOP{+}{52}{ \UNIOP{-}{10} }$,
  597. which after \textsf{select\_instructions} looks like the following.
  598. \[
  599. \begin{array}{l}
  600. (\key{mov}\;\INT{10}\; \VAR{x})\\
  601. (\key{neg}\; \VAR{x})\\
  602. (\key{mov}\; \INT{52}\; \REG{\itm{rax}})\\
  603. (\key{add}\; \VAR{x} \REG{\itm{rax}})
  604. \end{array}
  605. \]
  606. The one and only variable $x$ is assigned to stack location
  607. \key{-8(\%rbp)}, so the \textsf{assign\_homes} pass translates the
  608. above to
  609. \[
  610. \begin{array}{l}
  611. (\key{mov}\;\INT{10}\; \STACKLOC{{-}8})\\
  612. (\key{neg}\; \STACKLOC{{-}8})\\
  613. (\key{mov}\; \INT{52}\; \REG{\itm{rax}})\\
  614. (\key{add}\; \STACKLOC{{-}8}\; \REG{\itm{rax}})
  615. \end{array}
  616. \]
  617. In the process of assigning stack locations to variables, it is
  618. convenient to compute and store the size of the frame which will be
  619. needed later to generate the procedure conclusion.
  620. \section{Patch Instructions}
  621. The purpose of this pass is to make sure that each instruction adheres
  622. to the restrictions regarding which arguments can be memory
  623. references. For most instructions, the rule is that at most one
  624. argument may be a memory reference.
  625. Consider again the following example.
  626. \[
  627. \LET{a}{42}{ \LET{b}{a}{ b }}
  628. \]
  629. After \textsf{assign\_homes} pass, the above has been translated to
  630. \[
  631. \begin{array}{l}
  632. (\key{mov} \;\INT{42}\; \STACKLOC{{-}8})\\
  633. (\key{mov}\;\STACKLOC{{-}8}\; \STACKLOC{{-}16})\\
  634. (\key{mov}\;\STACKLOC{{-}16}\; \REG{\itm{rax}})
  635. \end{array}
  636. \]
  637. The second \key{mov} instruction is problematic because both arguments
  638. are stack locations. We suggest fixing this problem by moving from the
  639. source to \key{rax} and then from \key{rax} to the destination, as
  640. follows.
  641. \[
  642. \begin{array}{l}
  643. (\key{mov} \;\INT{42}\; \STACKLOC{{-}8})\\
  644. (\key{mov}\;\STACKLOC{{-}8}\; \REG{\itm{rax}})\\
  645. (\key{mov}\;\REG{\itm{rax}}\; \STACKLOC{{-}16})\\
  646. (\key{mov}\;\STACKLOC{{-}16}\; \REG{\itm{rax}})
  647. \end{array}
  648. \]
  649. The \key{imul} instruction is a special case because the destination
  650. argument must be a register.
  651. \section{Testing with Interpreters}
  652. The typical way to test a compiler is to run the generated assembly
  653. code on a diverse set of programs and check whether they behave as
  654. expected. However, when a compiler is structured as our is, with many
  655. passes, when there is an error in the generated assembly code it can
  656. be hard to determine which pass contains the source of the error. A
  657. good way to isolate the error is to not only test the generated
  658. assembly code but to also test the output of every pass. This requires
  659. having interpreters for all the intermediate languages. Indeed, the
  660. file \key{interp.rkt} in the supplemental code provides interpreters
  661. for all the intermediate languages described in this book, starting
  662. with interpreters for $S_0$, $C_0$, and x86 (in abstract syntax).
  663. The file \key{run-tests.rkt} automates the process of running the
  664. interpreters on the output programs of each pass and checking their
  665. result.
  666. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  667. \chapter{Register Allocation}
  668. \label{ch:register-allocation}
  669. In Chapter~\ref{ch:int-exp} we simplified the generation of x86
  670. assembly by placing all variables on the stack. We can improve the
  671. performance of the generated code considerably if we instead try to
  672. place as many variables as possible into registers. The CPU can
  673. access a register in a single cycle, whereas accessing the stack can
  674. take from several cycles (to go to cache) to hundreds of cycles (to go
  675. to main memory). Figure~\ref{fig:reg-eg} shows a program with four
  676. variables that serves as a running example. We show the source program
  677. and also the output of instruction selection. At that point the
  678. program is almost x86 assembly but not quite; it still contains
  679. variables instead of stack locations or registers.
  680. \begin{figure}
  681. \begin{minipage}{0.45\textwidth}
  682. Source program:
  683. \begin{lstlisting}
  684. (let ([v 1])
  685. (let ([w 46])
  686. (let ([x (+ v 7)])
  687. (let ([y (+ 4 x)])
  688. (let ([z (+ x w)])
  689. (- z y))))))
  690. \end{lstlisting}
  691. \end{minipage}
  692. \begin{minipage}{0.45\textwidth}
  693. After instruction selection:
  694. \begin{lstlisting}
  695. (program (v w x y z)
  696. (mov (int 1) (var v))
  697. (mov (int 46) (var w))
  698. (mov (var v) (var x))
  699. (add (int 7) (var x))
  700. (mov (var x) (var y))
  701. (add (int 4) (var y))
  702. (mov (var x) (var z))
  703. (add (var w) (var z))
  704. (mov (var z) (reg rax))
  705. (sub (var y) (reg rax)))
  706. \end{lstlisting}
  707. \end{minipage}
  708. \caption{Running example for this chapter.}
  709. \label{fig:reg-eg}
  710. \end{figure}
  711. The goal of register allocation is to fit as many variables into
  712. registers as possible. It is often the case that we have more
  713. variables than registers, so we can't naively map each variable to a
  714. register. Fortunately, it is also common for different variables to be
  715. needed during different periods of time, and in such cases the
  716. variables can be mapped to the same register. Consider variables $x$
  717. and $y$ in Figure~\ref{fig:reg-eg}. After the variable $x$ is moved
  718. to $z$ it is no longer needed. Variable $y$, on the other hand, is
  719. used only after this point, so $x$ and $y$ could share the same
  720. register. The topic of the next section is how we compute where a
  721. variable is needed.
  722. \section{Liveness Analysis}
  723. A variable is \emph{live} if the variable is used at some later point
  724. in the program and there is not an intervening assignment to the
  725. variable.
  726. %
  727. To understand the latter condition, consider the following code
  728. fragment in which there are two writes to $b$. Are $a$ and
  729. $b$ both live at the same time?
  730. \begin{lstlisting}[numbers=left,numberstyle=\tiny]
  731. (mov (int 5) (var a)) ; @$a \gets 5$@
  732. (mov (int 30) (var b)) ; @$b \gets 30$@
  733. (mov (var a) (var c)) ; @$c \gets x$@
  734. (mov (int 10) (var b)) ; @$b \gets 10$@
  735. (add (var b) (var c)) ; @$c \gets c + b$@
  736. \end{lstlisting}
  737. The answer is no because the value $30$ written to $b$ on line 2 is
  738. never used. The variable $b$ is read on line 5 and there is an
  739. intervening write to $b$ on line 4, so the read on line 5 receives the
  740. value written on line 4, not line 2.
  741. The live variables can be computed by traversing the instruction
  742. sequence back to front (i.e., backwards in execution order). Let
  743. $I_1,\ldots, I_n$ be the instruction sequence. We write
  744. $L_{\mathsf{after}}(k)$ for the set of live variables after
  745. instruction $I_k$ and $L_{\mathsf{before}}(k)$ for the set of live
  746. variables before instruction $I_k$. The live variables after an
  747. instruction are always the same as the live variables before the next
  748. instruction.
  749. \begin{equation*}
  750. L_{\mathsf{after}}(k) = L_{\mathsf{before}}(k+1)
  751. \end{equation*}
  752. To start things off, there are no live variables after the last
  753. instruction, so
  754. \begin{equation*}
  755. L_{\mathsf{after}}(n) = \emptyset
  756. \end{equation*}
  757. We then apply the following rule repeatedly, traversing the
  758. instruction sequence back to front.
  759. \begin{equation*}
  760. L_{\mathtt{before}}(k) = (L_{\mathtt{after}}(k) - W(k)) \cup R(k),
  761. \end{equation*}
  762. where $W(k)$ are the variables written to by instruction $I_k$ and
  763. $R(k)$ are the variables read by instruction $I_k$.
  764. Figure~\ref{fig:live-eg} shows the results of live variables analysis
  765. for the running example. Next to each instruction we write its
  766. $L_{\mathtt{after}}$ set.
  767. \begin{figure}[tbp]
  768. \begin{lstlisting}
  769. (program (v w x y z)
  770. (mov (int 1) (var v)) @$\{ v \}$@
  771. (mov (int 46) (var w)) @$\{ v, w \}$@
  772. (mov (var v) (var x)) @$\{ w, x \}$@
  773. (add (int 7) (var x)) @$\{ w, x \}$@
  774. (mov (var x) (var y)) @$\{ w, x, y\}$@
  775. (add (int 4) (var y)) @$\{ w, x, y \}$@
  776. (mov (var x) (var z)) @$\{ w, y, z \}$@
  777. (add (var w) (var z)) @$\{ y, z \}$@
  778. (mov (var z) (reg rax)) @$\{ y \}$@
  779. (sub (var y) (reg rax))) @$\{\}$@
  780. \end{lstlisting}
  781. \caption{Running example program annotated with live-after sets.}
  782. \label{fig:live-eg}
  783. \end{figure}
  784. \section{Building the Interference Graph}
  785. Based on the liveness analysis, we know the program regions where each
  786. variable is needed. However, during register allocation, we need to
  787. answer questions of the specific form: are variables $u$ and $v$ ever
  788. live at the same time? (And therefore cannot be assigned to the same
  789. register.) To make this question easier to answer, we create an
  790. explicit data structure, an \emph{interference graph}. An
  791. interference graph is an undirected graph that has an edge between two
  792. variables if they are live at the same time, that is, if they
  793. interfere with each other.
  794. The most obvious way to compute the interference graph is to look at
  795. the set of live variables between each statement in the program, and
  796. add an edge to the graph for every pair of variables in the same set.
  797. This approach is less than ideal for two reasons. First, it can be
  798. rather expensive because it takes $O(n^2)$ time to look at every pair
  799. in a set of $n$ live variables. Second, there is a special case in
  800. which two variables that are live at the same time do not actually
  801. interfere with each other: when they both contain the same value
  802. because we have assigned one to the other.
  803. A better way to compute the edges of the intereference graph is given
  804. by the following rules.
  805. \begin{itemize}
  806. \item If instruction $I_k$ is a move: (\key{mov} $s$\, $d$), then add
  807. the edge $(d,v)$ for every $v \in L_{\mathsf{after}}(k)$ unless $v =
  808. d$ or $v = s$.
  809. \item If instruction $I_k$ is not a move but some other arithmetic
  810. instruction such as (\key{add} $s$\, $d$), then add the edge $(d,v)$
  811. for every $v \in L_{\mathsf{after}}(k)$ unless $v = d$.
  812. \item If instruction $I_k$ is of the form (\key{call}
  813. $\mathit{label}$), then add an edge $(r,v)$ for every caller-save
  814. register $r$ and every variable $v \in L_{\mathsf{after}}(k)$.
  815. \end{itemize}
  816. Working from the top to bottom of Figure~\ref{fig:live-eg}, $z$
  817. interferes with $x$, $y$ interferes with $z$, and $w$ interferes with
  818. $y$ and $z$. The resulting interference graph is shown in
  819. Figure~\ref{fig:interfere}.
  820. \begin{figure}[tbp]
  821. \large
  822. \[
  823. \xymatrix@=40pt{
  824. v \ar@{-}[r] & w \ar@{-}[r]\ar@{-}[d]\ar@{-}[dr] & x \ar@{-}[dl]\\
  825. & y \ar@{-}[r] & z
  826. }
  827. \]
  828. \caption{Interference graph for the running example.}
  829. \label{fig:interfere}
  830. \end{figure}
  831. \section{Graph Coloring via Sudoku}
  832. We now come to the main event, mapping variables to registers (or to
  833. stack locations in the event that we run out of registers). We need
  834. to make sure not to map two variables to the same register if the two
  835. variables interfere with each other. In terms of the interference
  836. graph, this means we cannot map adjacent nodes to the same register.
  837. If we think of registers as colors, the register allocation problem
  838. becomes the widely-studied graph coloring
  839. problem~\citep{Balakrishnan:1996ve,Rosen:2002bh}.
  840. The reader may be more familar with the graph coloring problem then he
  841. or she realizes; the popular game of Sudoku is an instance of the
  842. graph coloring problem. The following describes how to build a graph
  843. out of a Sudoku board.
  844. \begin{itemize}
  845. \item There is one node in the graph for each Sudoku square.
  846. \item There is an edge between two nodes if the corresponding squares
  847. are in the same row or column, or if the squares are in the same
  848. $3\times 3$ region.
  849. \item Choose nine colors to correspond to the numbers $1$ to $9$.
  850. \item Based on the initial assignment of numbers to squares in the
  851. Sudoku board, assign the corresponding colors to the corresponding
  852. nodes in the graph.
  853. \end{itemize}
  854. If you can color the remaining nodes in the graph with the nine
  855. colors, then you've also solved the corresponding game of Sudoku.
  856. Given that Sudoku is graph coloring, one can use Sudoku strategies to
  857. come up with an algorithm for allocating registers. For example, one
  858. of the basic techniques for Sudoku is Pencil Marks. The idea is that
  859. you use a process of elimination to determine what numbers still make
  860. sense for a square, and write down those numbers in the square
  861. (writing very small). At first, each number might be a
  862. possibility, but as the board fills up, more and more of the
  863. possibilities are crossed off (or erased). For example, if the number
  864. $1$ is assigned to a square, then by process of elimination, you can
  865. cross off the $1$ pencil mark from all the squares in the same row,
  866. column, and region. Many Sudoku computer games provide automatic
  867. support for Pencil Marks. This heuristic also reduces the degree of
  868. branching in the search tree.
  869. The Pencil Marks technique corresponds to the notion of color
  870. \emph{saturation} due to \cite{Brelaz:1979eu}. The
  871. saturation of a node, in Sudoku terms, is the number of possibilities
  872. that have been crossed off using the process of elimination mentioned
  873. above. In graph terminology, we have the following definition:
  874. \begin{equation*}
  875. \mathrm{saturation}(u) = |\{ c \;|\; \exists v. v \in \mathrm{Adj}(u)
  876. \text{ and } \mathrm{color}(v) = c \}|
  877. \end{equation*}
  878. where $\mathrm{Adj}(u)$ is the set of nodes adjacent to $u$ and
  879. the notation $|S|$ stands for the size of the set $S$.
  880. Using the Pencil Marks technique leads to a simple strategy for
  881. filling in numbers: if there is a square with only one possible number
  882. left, then write down that number! But what if there are no squares
  883. with only one possibility left? One brute-force approach is to just
  884. make a guess. If that guess ultimately leads to a solution, great. If
  885. not, backtrack to the guess and make a different guess. Of course,
  886. this is horribly time consuming. One standard way to reduce the amount
  887. of backtracking is to use the most-constrained-first heuristic. That
  888. is, when making a guess, always choose a square with the fewest
  889. possibilities left (the node with the highest saturation). The idea
  890. is that choosing highly constrained squares earlier rather than later
  891. is better because later there may not be any possibilities left.
  892. In some sense, register allocation is easier than Sudoku because we
  893. can always cheat and add more numbers by spilling variables to the
  894. stack. Also, we'd like to minimize the time needed to color the graph,
  895. and backtracking is expensive. Thus, it makes sense to keep the
  896. most-constrained-first heuristic but drop the backtracking in favor of
  897. greedy search (guess and just keep going).
  898. Figure~\ref{fig:satur-algo} gives the pseudo-code for this simple
  899. greedy algorithm for register allocation based on saturation and the
  900. most-constrained-first heuristic, which is roughly equivalent to the
  901. DSATUR algorithm of \cite{Brelaz:1979eu} (also known as
  902. saturation degree ordering
  903. (SDO)~\citep{Gebremedhin:1999fk,Omari:2006uq}). Just as in Sudoku,
  904. the algorithm represents colors with integers, with the first $k$
  905. colors corresponding to the $k$ registers in a given machine and the
  906. rest of the integers corresponding to stack locations.
  907. \begin{figure}[btp]
  908. \centering
  909. \begin{lstlisting}[basicstyle=\rmfamily,deletekeywords={for,from,with,is,not,in,find},morekeywords={while},columns=fullflexible]
  910. Algorithm: DSATUR
  911. Input: a graph @$G$@
  912. Output: an assignment @$\mathrm{color}[v]$@ for each node @$v \in G$@
  913. @$W \gets \mathit{vertices}(G)$@
  914. while @$W \neq \emptyset$@ do
  915. pick a node @$u$@ from @$W$@ with the highest saturation,
  916. breaking ties randomly
  917. find the lowest color @$c$@ that is not in @$\{ \mathrm{color}[v] \;|\; v \in \mathrm{Adj}(v)\}$@
  918. @$\mathrm{color}[u] \gets c$@
  919. @$W \gets W - \{u\}$@
  920. \end{lstlisting}
  921. \caption{Saturation-based greedy graph coloring algorithm.}
  922. \label{fig:satur-algo}
  923. \end{figure}
  924. With this algorithm in hand, let us return to the running example and
  925. consider how to color the interference graph in
  926. Figure~\ref{fig:interfere}. Initially, all of the nodes are not yet
  927. colored and they are unsaturated, so we annotate each of them with a
  928. dash for their color and an empty set for the saturation.
  929. \[
  930. \xymatrix{
  931. v:-,\{\} \ar@{-}[r] & w:-,\{\} \ar@{-}[r]\ar@{-}[d]\ar@{-}[dr] & x:-,\{\} \ar@{-}[dl]\\
  932. & y:-,\{\} \ar@{-}[r] & z:-,\{\}
  933. }
  934. \]
  935. We select a maximally saturated node and color it $0$. In this case we
  936. have a 5-way tie, so we arbitrarily pick $y$. The color $0$ is no
  937. longer available for $w$, $x$, and $z$ because they interfere with
  938. $y$.
  939. \[
  940. \xymatrix{
  941. v:-,\{\} \ar@{-}[r] & w:-,\{0\} \ar@{-}[r]\ar@{-}[d]\ar@{-}[dr] & x:-,\{0\} \ar@{-}[dl]\\
  942. & y:0,\{\} \ar@{-}[r] & z:-,\{0\}
  943. }
  944. \]
  945. Now we repeat the process, selecting another maximally saturated node.
  946. This time there is a three-way tie between $w$, $x$, and $z$. We color
  947. $w$ with $1$.
  948. \[
  949. \xymatrix{
  950. v:-,\{1\} \ar@{-}[r] & w:1,\{0\} \ar@{-}[r]\ar@{-}[d]\ar@{-}[dr] & x:-,\{0,1\} \ar@{-}[dl]\\
  951. & y:0,\{1\} \ar@{-}[r] & z:-,\{0,1\}
  952. }
  953. \]
  954. The most saturated nodes are now $x$ and $z$. We color $x$ with the
  955. next avialable color which is $2$.
  956. \[
  957. \xymatrix{
  958. v:-,\{1\} \ar@{-}[r] & w:1,\{0,2\} \ar@{-}[r]\ar@{-}[d]\ar@{-}[dr] & x:2,\{0,1\} \ar@{-}[dl]\\
  959. & y:0,\{1,2\} \ar@{-}[r] & z:-,\{0,1\}
  960. }
  961. \]
  962. We have only two nodes left to color, $v$ and $z$, but $z$ is
  963. more highly saturaded, so we color $z$ with $2$.
  964. \[
  965. \xymatrix{
  966. v:-,\{1\} \ar@{-}[r] & w:1,\{0,2\} \ar@{-}[r]\ar@{-}[d]\ar@{-}[dr] & x:2,\{0,1\} \ar@{-}[dl]\\
  967. & y:0,\{1,2\} \ar@{-}[r] & z:2,\{0,1\}
  968. }
  969. \]
  970. The last iteration of the coloring algorithm assigns color $0$ to $v$.
  971. \[
  972. \xymatrix{
  973. v:0,\{1\} \ar@{-}[r] & w:1,\{0,2\} \ar@{-}[r]\ar@{-}[d]\ar@{-}[dr] & x:2,\{0,1\} \ar@{-}[dl]\\
  974. & y:0,\{1,2\} \ar@{-}[r] & z:2,\{0,1\}
  975. }
  976. \]
  977. With the coloring complete, we can finalize assignment of variables to
  978. registers and stack locations. Recall that if we have $k$ registers,
  979. we map the first $k$ colors to registers and the rest to stack
  980. lcoations. Suppose for the moment that we just have one extra register
  981. to use for register allocation, just \key{rbx}. Then the following is
  982. the mapping of colors to registers and stack allocations.
  983. \[
  984. \{ 0 \mapsto \key{\%rbx}, \; 1 \mapsto \key{-8(\%rbp)}, \; 2 \mapsto \key{-16(\%rbp)}, \ldots \}
  985. \]
  986. Putting this together with the above coloring of the variables, we
  987. arrive at the following assignment.
  988. \[
  989. \{ v \mapsto \key{\%rbx}, \;
  990. w \mapsto \key{-8(\%rbp)}, \;
  991. x \mapsto \key{-16(\%rbp)}, \;
  992. y \mapsto \key{\%rbx}, \;
  993. z\mapsto \key{-16(\%rbp)} \}
  994. \]
  995. Applying this assignment to our running example
  996. (Figure~\ref{fig:reg-eg}) yields the following program.
  997. % why frame size of 32? -JGS
  998. \begin{lstlisting}
  999. (program 32
  1000. (mov (int 1) (reg rbx))
  1001. (mov (int 46) (stack-loc -8))
  1002. (mov (reg rbx) (stack-loc -16))
  1003. (add (int 7) (stack-loc -16))
  1004. (mov (stack-loc 16) (reg rbx))
  1005. (add (int 4) (reg rbx))
  1006. (mov (stack-loc -16) (stack-loc -16))
  1007. (add (stack-loc -8) (stack-loc -16))
  1008. (mov (stack-loc -16) (reg rax))
  1009. (sub (reg rbx) (reg rax)))
  1010. \end{lstlisting}
  1011. This program is almost an x86 program. The remaining step is to apply
  1012. the patch instructions pass. In this example, the trivial move of
  1013. \key{-16(\%rbp)} to itself is deleted and the addition of
  1014. \key{-8(\%rbp)} to \key{-16(\%rbp)} is fixed by going through
  1015. \key{\%rax}. The following shows the portion of the program that
  1016. changed.
  1017. \begin{lstlisting}
  1018. (add (int 4) (reg rbx))
  1019. (mov (stack-loc -8) (reg rax)
  1020. (add (reg rax) (stack-loc -16))
  1021. \end{lstlisting}
  1022. An overview of all of the passes involved in register allocation is
  1023. shown in Figure~\ref{fig:reg-alloc-passes}.
  1024. \begin{figure}[tbp]
  1025. \[
  1026. \xymatrix{
  1027. C_0 \ar@/^/[r]^-{\textsf{select\_instr.}}
  1028. & \text{x86}^{*} \ar[d]^-{\textsf{uncover\_live}} \\
  1029. & \text{x86}^{*} \ar[d]^-{\textsf{build\_interference}} \\
  1030. & \text{x86}^{*} \ar[d]_-{\textsf{allocate\_register}} \\
  1031. & \text{x86}^{*} \ar@/^/[r]^-{\textsf{patch\_instr.}}
  1032. & \text{x86}
  1033. }
  1034. \]
  1035. \caption{Diagram of the passes for register allocation.}
  1036. \label{fig:reg-alloc-passes}
  1037. \end{figure}
  1038. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1039. \chapter{Booleans, Type Checking, and Control Flow}
  1040. \label{ch:bool-types}
  1041. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1042. \chapter{Tuples and Heap Allocation}
  1043. \label{ch:tuples}
  1044. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1045. \chapter{Functions}
  1046. \label{ch:functions}
  1047. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1048. \chapter{Lexically Scoped Functions}
  1049. \label{ch:lambdas}
  1050. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1051. \chapter{Mutable Data}
  1052. \label{ch:mutable-data}
  1053. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1054. \chapter{The Dynamic Type}
  1055. \label{ch:type-dynamic}
  1056. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1057. \chapter{Parametric Polymorphism}
  1058. \label{ch:parametric-polymorphism}
  1059. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1060. \chapter{High-level Optimization}
  1061. \label{ch:high-level-optimization}
  1062. \bibliographystyle{plainnat}
  1063. \bibliography{all}
  1064. \end{document}
  1065. %% LocalWords: Dybvig Waddell Abdulaziz Ghuloum Dipanwita
  1066. %% LocalWords: Sarkar lcl Matz aa representable