2323
2424\def\racketEd{0}
2525\def\pythonEd{1}
26- \def\edition{0 }
26+ \def\edition{1 }
2727
2828% material that is specific to the Racket edition of the book
2929\newcommand{\racket}[1]{{\if\edition\racketEd{#1}\fi}}
@@ -13997,6 +13997,257 @@ \section{Register Allocation}
1399713997\racket{\code{Program}}\python{\code{CProgram}} output of this pass
1399813998changes to also record the number of spills to the root stack.
1399913999
14000+ \begin{figure}[tbp]
14001+ \centering
14002+ \begin{tcolorbox}[colback=white]
14003+ {\if\edition\racketEd
14004+ % tests/vectors_test_17.rkt
14005+ \begin{tabular}{lll}
14006+ \begin{minipage}{0.5\textwidth}
14007+ \begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
14008+ start:
14009+ movq free_ptr(%rip), %rcx
14010+ movq %rcx, %rdx
14011+ addq $16, %rdx
14012+ movq fromspace_end(%rip), %rcx
14013+ cmpq %rcx, %rdx
14014+ jl block0
14015+ jmp block1
14016+
14017+ block0:
14018+ movq $0, %rcx
14019+ jmp block9
14020+
14021+ block1:
14022+ movq %r15, %rdi
14023+ movq $16, %rsi
14024+ callq collect
14025+ jmp block9
14026+
14027+ block9:
14028+ movq free_ptr(%rip), %r11
14029+ addq $16, free_ptr(%rip)
14030+ movq $3, 0(%r11)
14031+ movq %r11, %rcx
14032+ movq %rcx, %r11
14033+ movq $42, 8(%r11)
14034+ movq $0, %rdx
14035+ movq %rcx, -8(%r15)
14036+ movq free_ptr(%rip), %rcx
14037+ movq %rcx, %rcx
14038+ addq $16, %rcx
14039+ movq fromspace_end(%rip), %rdx
14040+ cmpq %rdx, %rcx
14041+ jl block7
14042+ jmp block8
14043+ \end{lstlisting}
14044+ \end{minipage}
14045+ &
14046+ \begin{minipage}{0.5\textwidth}
14047+ \begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
14048+ block7:
14049+ movq $0, %rcx
14050+ jmp block6
14051+
14052+ block8:
14053+ movq %r15, %rdi
14054+ movq $16, %rsi
14055+ callq collect
14056+ jmp block6
14057+
14058+ block6:
14059+ movq free_ptr(%rip), %r11
14060+ addq $16, free_ptr(%rip)
14061+ movq $131, 0(%r11)
14062+ movq %r11, %rcx
14063+ movq %rcx, %r11
14064+ movq -8(%r15), 8(%r11)
14065+ movq $0, %rdx
14066+ movq %rcx, %r11
14067+ movq 8(%r11), %rcx
14068+ movq %rcx, %r11
14069+ movq 8(%r11), %rax
14070+ jmp conclusion
14071+ \end{lstlisting}
14072+ \end{minipage}
14073+ \end{tabular}
14074+
14075+ \fi}
14076+ {\if\edition\pythonEd
14077+ % tests/tuple/get_get.py
14078+ \begin{tabular}{lll}
14079+ \begin{minipage}{0.5\textwidth}
14080+ \begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
14081+ start:
14082+ movq $42, %rbx
14083+ movq free_ptr(%rip), %rcx
14084+ addq $16, %rcx
14085+ cmpq fromspace_end(%rip), %rcx
14086+ jl block.10
14087+ jmp block.11
14088+
14089+ block.10:
14090+ movq free_ptr(%rip), %r11
14091+ addq $16, free_ptr(%rip)
14092+ movq $3, 0(%r11)
14093+ movq %r11, %rcx
14094+ movq %rcx, %r11
14095+ movq %rbx, 8(%r11)
14096+ movq %rcx, %rcx
14097+ movq %rcx, -8(%r15)
14098+ movq free_ptr(%rip), %rcx
14099+ addq $16, %rcx
14100+ cmpq fromspace_end(%rip), %rcx
14101+ jl block.8
14102+ jmp block.9
14103+
14104+ block.11:
14105+ movq %r15, %rdi
14106+ movq $16, %rsi
14107+ callq collect
14108+ jmp block.10
14109+ \end{lstlisting}
14110+ \end{minipage}
14111+ &
14112+ \begin{minipage}{0.5\textwidth}
14113+ \begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
14114+ block.8:
14115+ movq free_ptr(%rip), %r11
14116+ addq $16, free_ptr(%rip)
14117+ movq $131, 0(%r11)
14118+ movq %r11, %rcx
14119+ movq %rcx, %r11
14120+ movq -8(%r15), 8(%r11)
14121+ movq %rcx, %rcx
14122+ movq %rcx, %r11
14123+ movq 8(%r11), %rcx
14124+ movq %rcx, %r11
14125+ movq 8(%r11), %rcx
14126+ movq %rcx, %rdi
14127+ callq print_int
14128+ movq $0, %rax
14129+ jmp conclusion
14130+
14131+ block.9:
14132+ movq %r15, %rdi
14133+ movq $16, %rsi
14134+ callq collect
14135+ jmp block.8
14136+ \end{lstlisting}
14137+ \end{minipage}
14138+ \end{tabular}
14139+ \fi}
14140+ \end{tcolorbox}
14141+
14142+ \caption{Output of register allocation for the running example.}
14143+ \label{fig:reg-alloc-output}
14144+ \end{figure}
14145+
14146+ \python{
14147+ Figure~\ref{fig:reg-alloc-output} shows the output of register
14148+ allocation on the running example. The register allocator chose the
14149+ below assignment of variables to locations. Most of the variables were
14150+ assigned to register \key{\%rcx}. Variable \key{init.1} was instead
14151+ assigned to \key{\%rbx} because \key{init.1} conflicts with variables
14152+ \key{tmp.4} and \key{alloc.0} which were assigned to \key{\%rcx}.
14153+ Variable \key{init.3} was spilled to the root stack because its type
14154+ is \key{tuple[int]} and it is live during a call to \key{collect}.
14155+
14156+ \begin{center}\small
14157+ \begin{tabular}{ll}
14158+ \begin{minipage}{0.45\textwidth}
14159+ \[
14160+ \begin{array}{l}
14161+ \key{alloc.0} \mapsto \key{\%rcx} \\
14162+ \key{alloc.2} \mapsto \key{\%rcx} \\
14163+ \key{init.1} \mapsto \key{\%rbx} \\
14164+ \key{init.3} \mapsto \key{-8(\%r15)}
14165+ \end{array}
14166+ \]
14167+ \end{minipage}
14168+ &
14169+ \begin{minipage}{0.45\textwidth}
14170+ \[
14171+ \begin{array}{l}
14172+ \key{tmp.4} \mapsto \key{\%rcx} \\
14173+ \key{tmp.5} \mapsto \key{\%rcx} \\
14174+ \key{tmp.6} \mapsto \key{\%rcx} \\
14175+ \key{tmp.7} \mapsto \key{\%rcx} \\
14176+ \key{v1} \mapsto \key{\%rcx} \\
14177+ \key{v2} \mapsto \key{\%rcx}
14178+ \end{array}
14179+ \]
14180+ \end{minipage}
14181+ \end{tabular}
14182+ \end{center}
14183+ }
14184+
14185+ %% home:
14186+ %% {Variable(id='tmp.6'): Reg(id='rcx'), Variable(id='tmp.4'): Reg(id='rcx'), Variable(id='init.1'): Reg(id='rbx'), Variable(id='tmp.7'): Reg(id='rcx'), Variable(id='tmp.5'): Reg(id='rcx'), Variable(id='alloc.2'): Reg(id='rcx'), Variable(id='v2'): Reg(id='rcx'), Variable(id='v1'): Reg(id='rcx'), Variable(id='init.3'): Deref(reg='r15', offset=-8), Variable(id='alloc.0'): Reg(id='rcx')}
14187+
14188+
14189+ \racket{
14190+ Figure~\ref{fig:reg-alloc-output} shows the output of register
14191+ allocation on the running example. The register allocator chose the
14192+ below assignment of variables to locations. Many of the variables were
14193+ assigned to register \key{\%rcx}. Variables \key{\_3}, \key{\_7}, \key{tmp0},
14194+ and \key{tmp4} were instead
14195+ assigned to \key{\%rdx} because they conflict with variables
14196+ that were assigned to \key{\%rcx}.
14197+ Variable \key{vecinit6} was spilled to the root stack because its
14198+ type is \key{(Vector Integer)} and it is live during a call to \key{collect}.
14199+
14200+ \begin{center}\small
14201+ \begin{tabular}{ll}
14202+ \begin{minipage}{0.45\textwidth}
14203+ \[
14204+ \begin{array}{l}
14205+ \key{alloc2} \mapsto \key{\%rcx} \\
14206+ \key{alloc5} \mapsto \key{\%rcx} \\
14207+ \key{\_3} \mapsto \key{\%rdx} \\
14208+ \key{\_4} \mapsto \key{\%rcx} \\
14209+ \key{\_7} \mapsto \key{\%rdx} \\
14210+ \key{\_8} \mapsto \key{\%rcx} \\
14211+ \key{vecinit6} \mapsto \key{-8(\%r15)}
14212+ \end{array}
14213+ \]
14214+ \end{minipage}
14215+ &
14216+ \begin{minipage}{0.45\textwidth}
14217+ \[
14218+ \begin{array}{l}
14219+ \key{tmp0} \mapsto \key{\%rdx} \\
14220+ \key{tmp1} \mapsto \key{\%rcx} \\
14221+ \key{tmp2} \mapsto \key{\%rcx} \\
14222+ \key{tmp3} \mapsto \key{\%rcx} \\
14223+ \key{tmp4} \mapsto \key{\%rdx} \\
14224+ \key{tmp5} \mapsto \key{\%rcx} \\
14225+ \key{tmp9} \mapsto \key{\%rcx}
14226+ \end{array}
14227+ \]
14228+ \end{minipage}
14229+ \end{tabular}
14230+ \end{center}
14231+ }
14232+
14233+ %% homes:
14234+ %% alloc2 %rcx
14235+ %% alloc5 %rcx
14236+ %% _3 %rdx
14237+ %% _4 %rcx
14238+ %% _7 %rdx
14239+ %% _8 %rcx
14240+ %% vecinit6 -8(%r15)
14241+
14242+ %% tmp0 %rdx
14243+ %% tmp1 %rcx
14244+ %% tmp2 %rcx
14245+ %% tmp3 %rcx
14246+ %% tmp4 %rdx
14247+ %% tmp5 %rcx
14248+ %% tmp9 %rcx
14249+
14250+
1400014251% build-interference
1400114252%
1400214253% callq
@@ -14044,7 +14295,8 @@ \section{Generate Prelude and Conclusion}
1404414295\begin{figure}[htbp]
1404514296 \begin{tcolorbox}[colback=white]
1404614297{\if\edition\racketEd
14047- \begin{minipage}[t]{0.5\textwidth}
14298+ \begin{tabular}{ll}
14299+ \begin{minipage}[t]{0.45\textwidth}
1404814300\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
1404914301 .globl main
1405014302main:
@@ -14058,15 +14310,20 @@ \section{Generate Prelude and Conclusion}
1405814310 movq $0, 0(%r15)
1405914311 addq $8, %r15
1406014312 jmp start
14061-
14313+ \end{lstlisting}
14314+ \end{minipage}
14315+ &
14316+ \begin{minipage}[t]{0.45\textwidth}
14317+ \begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
1406214318conclusion:
1406314319 subq $8, %r15
1406414320 addq $0, %rsp
1406514321 popq %rbp
1406614322 retq
1406714323\end{lstlisting}
1406814324\end{minipage}
14069- \fi}
14325+ \end{tabular}
14326+ \fi}
1407014327{\if\edition\pythonEd
1407114328\begin{minipage}[t]{0.5\textwidth}
1407214329\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
0 commit comments