book.tex 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425
  1. \documentclass[11pt]{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. \usepackage{semantic}
  16. \usepackage{wrapfig}
  17. \usepackage{multirow}
  18. \usepackage{color}
  19. \definecolor{lightgray}{gray}{1}
  20. \newcommand{\black}[1]{{\color{black} #1}}
  21. \newcommand{\gray}[1]{{\color{lightgray} #1}}
  22. %% For pictures
  23. \usepackage{tikz}
  24. \usetikzlibrary{arrows.meta}
  25. \tikzset{baseline=(current bounding box.center), >/.tip={Triangle[scale=1.4]}}
  26. % Computer Modern is already the default. -Jeremy
  27. %\renewcommand{\ttdefault}{cmtt}
  28. \lstset{%
  29. language=Lisp,
  30. basicstyle=\ttfamily\small,
  31. escapechar=|,
  32. columns=flexible
  33. }
  34. \newtheorem{theorem}{Theorem}
  35. \newtheorem{lemma}[theorem]{Lemma}
  36. \newtheorem{corollary}[theorem]{Corollary}
  37. \newtheorem{proposition}[theorem]{Proposition}
  38. \newtheorem{constraint}[theorem]{Constraint}
  39. \newtheorem{definition}[theorem]{Definition}
  40. \newtheorem{exercise}[theorem]{Exercise}
  41. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  42. % 'dedication' environment: To add a dedication paragraph at the start of book %
  43. % Source: http://www.tug.org/pipermail/texhax/2010-June/015184.html %
  44. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  45. \newenvironment{dedication}
  46. {
  47. \cleardoublepage
  48. \thispagestyle{empty}
  49. \vspace*{\stretch{1}}
  50. \hfill\begin{minipage}[t]{0.66\textwidth}
  51. \raggedright
  52. }
  53. {
  54. \end{minipage}
  55. \vspace*{\stretch{3}}
  56. \clearpage
  57. }
  58. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  59. % Chapter quote at the start of chapter %
  60. % Source: http://tex.stackexchange.com/a/53380 %
  61. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  62. \makeatletter
  63. \renewcommand{\@chapapp}{}% Not necessary...
  64. \newenvironment{chapquote}[2][2em]
  65. {\setlength{\@tempdima}{#1}%
  66. \def\chapquote@author{#2}%
  67. \parshape 1 \@tempdima \dimexpr\textwidth-2\@tempdima\relax%
  68. \itshape}
  69. {\par\normalfont\hfill--\ \chapquote@author\hspace*{\@tempdima}\par\bigskip}
  70. \makeatother
  71. \input{defs}
  72. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  73. \title{\Huge \textbf{Essentials of Compilation} \\
  74. \huge An Incremental Approach}
  75. \author{\textsc{Jeremy G. Siek} \\
  76. %\thanks{\url{http://homes.soic.indiana.edu/jsiek/}} \\
  77. Indiana University \\
  78. \\
  79. with contributions from: \\
  80. Carl Factora \\
  81. Michael M. Vitousek \\
  82. Cameron Swords
  83. }
  84. \begin{document}
  85. \frontmatter
  86. \maketitle
  87. \begin{dedication}
  88. This book is dedicated to the programming language wonks at Indiana
  89. University.
  90. \end{dedication}
  91. \tableofcontents
  92. %\listoffigures
  93. %\listoftables
  94. \mainmatter
  95. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  96. \chapter*{Preface}
  97. The tradition of compiler writing at Indiana University goes back to
  98. programming language research and courses taught by Daniel Friedman in
  99. the 1970's and 1980's. Dan had conducted research on lazy evaluation
  100. in the context of Lisp~\citep{McCarthy:1960dz} and then studied
  101. continuations and macros in the context of the
  102. Scheme~\citep{Sussman:1975ab}, a dialect of Lisp. One of students of
  103. those courses, Kent Dybvig, went on to build Chez
  104. Scheme~\citep{Dybvig:2006aa}, a production-quality and efficient
  105. compiler for Scheme. After completing his Ph.D. at the University of
  106. North Carolina, Kent returned to teach at Indiana University.
  107. Throughout the 1990's and early 2000's, Kent continued development of
  108. Chez Scheme and rotated with Dan in teaching the compiler course.
  109. Thanks to this collaboration between Dan and Kent, the compiler course
  110. evolved to incorporate novel pedagogical ideas while also including
  111. elements of effective real-world compilers. One of Dan's ideas was to
  112. split the compiler into many small passes over the input program and
  113. subsequent intermediate representations, so that the code for each
  114. pass would be easy to understood in isolation. (In contrast, most
  115. compilers of the time were organized into only a few monolithic passes
  116. for reasons of compile-time efficiency.) Kent and his students,
  117. Dipanwita Sarkar and Andrew Keep, developed infrastructure to support
  118. this approach and evolved the course, first to use micro-sized passes
  119. and then into even smaller nano
  120. passes~\citep{Sarkar:2004fk,Keep:2012aa}. I took this compiler course
  121. in the early 2000's, as part of my Ph.D. studies at Indiana
  122. University. Needless to say, I enjoyed the course immensely.
  123. One of my classmates, Abdulaziz Ghuloum, observed that the
  124. front-to-back organization of the course made it difficult for
  125. students to understand the rationale for the compiler
  126. design. Abdulaziz proposed an incremental approach in which the
  127. students build the compiler in stages; they start by implementing a
  128. complete compiler for a very small subset of the input language, then
  129. in each subsequent stage they add a feature to the input language and
  130. add or modify passes to handle the new feature~\citep{Ghuloum:2006bh}.
  131. In this way, the students see how the language features motivate
  132. aspects of the compiler design.
  133. After graduating from Indiana University in 2005, I went on to teach
  134. at the University of Colorado. I adapted the nano pass and incremental
  135. approaches to compiling a subset of the Python
  136. language~\citep{Siek:2012ab}. Python and Scheme are quite different
  137. on the surface but there is a large overlap in the compiler techniques
  138. required for the two languages. Thus, I was able to teach much of the
  139. same content from the Indiana compiler course. I very much enjoyed
  140. teaching the course organized in this way, and even better, many of
  141. the students learned a lot and got excited about compilers. (No, I
  142. didn't do a quantitative study to support this claim.)
  143. It is now 2016 and I too have returned to teach at Indiana University.
  144. In my absence the compiler course had switched from the front-to-back
  145. organization to a back-to-front organization. Seeing how well the
  146. incremental approach worked at Colorado, I found this unsatisfactory
  147. and have reorganized the course, porting and adapting the structure of
  148. the Colorado course back into the land of Scheme. In the meantime
  149. Scheme has been superseded by Racket (at least in Indiana), so the
  150. course is now about compiling a subset of Racket to the x86 assembly
  151. language and the compiler is implemented in Racket~\citep{plt-tr}.
  152. This is the textbook for the incremental version of the compiler
  153. course at Indiana University (Spring 2016) and it is the first
  154. textbook for an Indiana compiler course. With this book I hope to
  155. make the Indiana compiler course available to people that have not had
  156. the chance to study here in person. Many of the compiler design
  157. decisions in this book are drawn from the assignment descriptions of
  158. \cite{Dybvig:2010aa}. I have captured what I think are the most
  159. important topics from \cite{Dybvig:2010aa} but have omitted topics
  160. that I think are less interesting conceptually and I have made
  161. simplifications to reduce complexity. In this way, this book leans
  162. more towards pedagogy than towards absolute efficiency. Also, the book
  163. differs in places where I saw the opportunity to make the topics more
  164. fun, such as in relating register allocation to Sudoku
  165. (Chapter~\ref{ch:register-allocation}).
  166. \section*{Prerequisites}
  167. The material in this book is challenging but rewarding. It is meant to
  168. prepare students for a lifelong career in programming languages. I do
  169. not recommend this book for students who want to dabble in programming
  170. languages. Because the book uses the Racket language both for the
  171. implementation of the compiler and for the language that is compiled,
  172. a student should be proficient with Racket (or Scheme) prior to
  173. reading this book. There are many other excellent resources for
  174. learning Scheme and
  175. Racket~\citep{Dybvig:1987aa,Abelson:1996uq,Friedman:1996aa,Felleisen:2001aa,Felleisen:2013aa,Flatt:2014aa}. It
  176. is helpful but not necessary for the student to have prior exposure to
  177. x86 (or x86-64) assembly language~\citep{Intel:2015aa}, as one might
  178. obtain from a computer systems
  179. course~\citep{Bryant:2005aa,Bryant:2010aa}. This book introduces the
  180. parts of x86-64 assembly language that are needed.
  181. %\section*{Structure of book}
  182. % You might want to add short description about each chapter in this book.
  183. %\section*{About the companion website}
  184. %The website\footnote{\url{https://github.com/amberj/latex-book-template}} for %this file contains:
  185. %\begin{itemize}
  186. % \item A link to (freely downlodable) latest version of this document.
  187. % \item Link to download LaTeX source for this document.
  188. % \item Miscellaneous material (e.g. suggested readings etc).
  189. %\end{itemize}
  190. \section*{Acknowledgments}
  191. Need to give thanks to
  192. \begin{itemize}
  193. \item Bor-Yuh Evan Chang
  194. \item Kent Dybvig
  195. \item Daniel P. Friedman
  196. \item Ronald Garcia
  197. \item Abdulaziz Ghuloum
  198. \item Ryan Newton
  199. \item Dipanwita Sarkar
  200. \item Andrew Keep
  201. \item Oscar Waddell
  202. \end{itemize}
  203. \mbox{}\\
  204. \noindent Jeremy G. Siek \\
  205. \noindent \url{http://homes.soic.indiana.edu/jsiek} \\
  206. \noindent Spring 2016
  207. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  208. \chapter{Preliminaries}
  209. \label{ch:trees-recur}
  210. In this chapter, we review the basic tools that are needed for
  211. implementing a compiler. We use abstract syntax trees (ASTs) in the
  212. form of S-expressions to represent programs (Section~\ref{sec:ast})
  213. and pattern matching to inspect individual nodes in an AST
  214. (Section~\ref{sec:pattern-matching}). We use recursion to construct
  215. and deconstruct entire ASTs (Section~\ref{sec:recursion}).
  216. \section{Abstract Syntax Trees}
  217. \label{sec:ast}
  218. The primary data structure that is commonly used for representing
  219. programs is the \emph{abstract syntax tree} (AST). When considering
  220. some part of a program, a compiler needs to ask what kind of part it
  221. is and what sub-parts it has. For example, the program on the left is
  222. represented by the AST on the right.
  223. \begin{center}
  224. \begin{minipage}{0.4\textwidth}
  225. \begin{lstlisting}
  226. (+ (read) (- 8))
  227. \end{lstlisting}
  228. \end{minipage}
  229. \begin{minipage}{0.4\textwidth}
  230. \begin{equation}
  231. \begin{tikzpicture}
  232. \node[draw, circle] (plus) at (0 , 0) {\key{+}};
  233. \node[draw, circle] (read) at (-1, -1.5) {{\footnotesize\key{read}}};
  234. \node[draw, circle] (minus) at (1 , -1.5) {$\key{-}$};
  235. \node[draw, circle] (8) at (1 , -3) {\key{8}};
  236. \draw[->] (plus) to (read);
  237. \draw[->] (plus) to (minus);
  238. \draw[->] (minus) to (8);
  239. \end{tikzpicture}
  240. \label{eq:arith-prog}
  241. \end{equation}
  242. \end{minipage}
  243. \end{center}
  244. We shall use the standard terminology for trees: each circle above is
  245. called a \emph{node}. The arrows connect a node to its \emph{children}
  246. (which are also nodes). The top-most node is the \emph{root}. Every
  247. node except for the root has a \emph{parent} (the node it is the child
  248. of). If a node has no children, it is a \emph{leaf} node. Otherwise
  249. it is an \emph{internal} node.
  250. When deciding how to compile the above program, we need to know that
  251. the root node operation is addition and that it has two children:
  252. \texttt{read} and a negation. The abstract syntax tree data structure
  253. directly supports these queries and hence is a good choice. In this
  254. book, we will often write down the textual representation of a program
  255. even when we really have in mind the AST because the textual
  256. representation is more concise. We recommend that, in your mind, you
  257. always interpret programs as abstract syntax trees.
  258. \section{Grammars}
  259. \label{sec:grammar}
  260. A programming language can be thought of as a \emph{set} of programs.
  261. The set is typically infinite (one can always create larger and larger
  262. programs), so one cannot simply describe a language by listing all of
  263. the programs in the language. Instead we write down a set of rules, a
  264. \emph{grammar}, for building programs. We shall write our rules in a
  265. variant of Backus-Naur Form (BNF)~\citep{Backus:1960aa,Knuth:1964aa}.
  266. As an example, we describe a small language, named $R_0$, of
  267. integers and arithmetic operations. The first rule says that any
  268. integer is an expression, $\Exp$, in the language:
  269. \begin{equation}
  270. \Exp ::= \Int \label{eq:arith-int}
  271. \end{equation}
  272. Each rule has a left-hand-side and a right-hand-side. The way to read
  273. a rule is that if you have all the program parts on the
  274. right-hand-side, then you can create an AST node and categorize it
  275. according to the left-hand-side. (We do not define $\Int$ because the
  276. reader already knows what an integer is.) We make the simplifying
  277. design decision that all of the languages in this book only handle
  278. machine-representable integers (those representable with 64-bits,
  279. i.e., the range $-2^{63}$ to $2^{63}$) which corresponds to the
  280. \texttt{fixnum} datatype in Racket. A name such as $\Exp$ that is
  281. defined by the grammar rules is a \emph{non-terminal}.
  282. The second grammar rule is the \texttt{read} operation that receives
  283. an input integer from the user of the program.
  284. \begin{equation}
  285. \Exp ::= (\key{read}) \label{eq:arith-read}
  286. \end{equation}
  287. The third rule says that, given an $\Exp$ node, you can build another
  288. $\Exp$ node by negating it.
  289. \begin{equation}
  290. \Exp ::= (\key{-} \; \Exp) \label{eq:arith-neg}
  291. \end{equation}
  292. Symbols such as \key{-} in typewriter font are \emph{terminal} symbols
  293. and must literally appear in the program for the rule to be
  294. applicable.
  295. We can apply the rules to build ASTs in the $R_0$
  296. language. For example, by rule \eqref{eq:arith-int}, \texttt{8} is an
  297. $\Exp$, then by rule \eqref{eq:arith-neg}, the following AST is
  298. an $\Exp$.
  299. \begin{center}
  300. \begin{minipage}{0.25\textwidth}
  301. \begin{lstlisting}
  302. (- 8)
  303. \end{lstlisting}
  304. \end{minipage}
  305. \begin{minipage}{0.25\textwidth}
  306. \begin{equation}
  307. \begin{tikzpicture}
  308. \node[draw, circle] (minus) at (0, 0) {$\text{--}$};
  309. \node[draw, circle] (8) at (0, -1.2) {$8$};
  310. \draw[->] (minus) to (8);
  311. \end{tikzpicture}
  312. \label{eq:arith-neg8}
  313. \end{equation}
  314. \end{minipage}
  315. \end{center}
  316. The following grammar rule defines addition expressions:
  317. \begin{equation}
  318. \Exp ::= (\key{+} \; \Exp \; \Exp) \label{eq:arith-add}
  319. \end{equation}
  320. Now we can see that the AST \eqref{eq:arith-prog} is an $\Exp$ in
  321. $R_0$. We know that \lstinline{(read)} is an $\Exp$ by rule
  322. \eqref{eq:arith-read} and we have shown that \texttt{(- 8)} is an
  323. $\Exp$, so we can apply rule \eqref{eq:arith-add} to show that
  324. \texttt{(+ (read) (- 8))} is an $\Exp$ in the $R_0$ language.
  325. If you have an AST for which the above rules do not apply, then the
  326. AST is not in $R_0$. For example, the AST \texttt{(- (read) (+ 8))} is
  327. not in $R_0$ because there are no rules for \key{+} with only one
  328. argument, nor for \key{-} with two arguments. Whenever we define a
  329. language with a grammar, we implicitly mean for the language to be the
  330. smallest set of programs that are justified by the rules. That is, the
  331. language only includes those programs that the rules allow.
  332. The last grammar for $R_0$ states that there is a \key{program} node
  333. to mark the top of the whole program:
  334. \[
  335. R_0 ::= (\key{program} \; \Exp)
  336. \]
  337. The \code{read-program} function provided in \code{utilities.rkt}
  338. reads programs in from a file (the sequence of characters in the
  339. concrete syntax of Racket) and parses them into the abstract syntax
  340. tree. The concrete syntax does not include a \key{program} form; that
  341. is added by the \code{read-program} function as it creates the
  342. AST. See the description of \code{read-program} in
  343. Appendix~\ref{appendix:utilities} for more details.
  344. It is common to have many rules with the same left-hand side, such as
  345. $\Exp$ in the grammar for $R_0$, so there is a vertical bar notation
  346. for gathering several rules, as shown in
  347. Figure~\ref{fig:r0-syntax}. Each clause between a vertical bar is
  348. called an {\em alternative}.
  349. \begin{figure}[tbp]
  350. \fbox{
  351. \begin{minipage}{0.96\textwidth}
  352. \[
  353. \begin{array}{rcl}
  354. \Exp &::=& \Int \mid ({\tt \key{read}}) \mid (\key{-} \; \Exp) \mid
  355. (\key{+} \; \Exp \; \Exp) \\
  356. R_0 &::=& (\key{program} \; \Exp)
  357. \end{array}
  358. \]
  359. \end{minipage}
  360. }
  361. \caption{The syntax of the $R_0$ language.}
  362. \label{fig:r0-syntax}
  363. \end{figure}
  364. \section{S-Expressions}
  365. \label{sec:s-expr}
  366. Racket, as a descendant of Lisp, has
  367. convenient support for creating and manipulating abstract syntax trees
  368. with its \emph{symbolic expression} feature, or S-expression for
  369. short. We can create an S-expression simply by writing a backquote
  370. followed by the textual representation of the AST. (Technically
  371. speaking, this is called a \emph{quasiquote} in Racket.) For example,
  372. an S-expression to represent the AST \eqref{eq:arith-prog} is created
  373. by the following Racket expression:
  374. \begin{center}
  375. \texttt{`(+ (read) (- 8))}
  376. \end{center}
  377. To build larger S-expressions one often needs to splice together
  378. several smaller S-expressions. Racket provides the comma operator to
  379. splice an S-expression into a larger one. For example, instead of
  380. creating the S-expression for AST \eqref{eq:arith-prog} all at once,
  381. we could have first created an S-expression for AST
  382. \eqref{eq:arith-neg8} and then spliced that into the addition
  383. S-expression.
  384. \begin{lstlisting}
  385. (define ast1.4 `(- 8))
  386. (define ast1.1 `(+ (read) ,ast1.4))
  387. \end{lstlisting}
  388. In general, the Racket expression that follows the comma (splice)
  389. can be any expression that computes an S-expression.
  390. \section{Pattern Matching}
  391. \label{sec:pattern-matching}
  392. As mentioned above, one of the operations that a compiler needs to
  393. perform on an AST is to access the children of a node. Racket
  394. provides the \texttt{match} form to access the parts of an
  395. S-expression. Consider the following example and the output on the
  396. right.
  397. \begin{center}
  398. \begin{minipage}{0.5\textwidth}
  399. \begin{lstlisting}
  400. (match ast1.1
  401. [`(,op ,child1 ,child2)
  402. (print op) (newline)
  403. (print child1) (newline)
  404. (print child2)])
  405. \end{lstlisting}
  406. \end{minipage}
  407. \vrule
  408. \begin{minipage}{0.25\textwidth}
  409. \begin{lstlisting}
  410. '+
  411. '(read)
  412. '(- 8)
  413. \end{lstlisting}
  414. \end{minipage}
  415. \end{center}
  416. The \texttt{match} form takes AST \eqref{eq:arith-prog} and binds its
  417. parts to the three variables \texttt{op}, \texttt{child1}, and
  418. \texttt{child2}. In general, a match clause consists of a
  419. \emph{pattern} and a \emph{body}. The pattern is a quoted S-expression
  420. that may contain pattern-variables (preceded by a comma). The body
  421. may contain any Racket code.
  422. A \texttt{match} form may contain several clauses, as in the following
  423. function \texttt{leaf?} that recognizes when an $R_0$ node is
  424. a leaf. The \texttt{match} proceeds through the clauses in order,
  425. checking whether the pattern can match the input S-expression. The
  426. body of the first clause that matches is executed. The output of
  427. \texttt{leaf?} for several S-expressions is shown on the right. In the
  428. below \texttt{match}, we see another form of pattern: the \texttt{(?
  429. fixnum?)} applies the predicate \texttt{fixnum?} to the input
  430. S-expression to see if it is a machine-representable integer.
  431. \begin{center}
  432. \begin{minipage}{0.5\textwidth}
  433. \begin{lstlisting}
  434. (define (leaf? arith)
  435. (match arith
  436. [(? fixnum?) #t]
  437. [`(read) #t]
  438. [`(- ,c1) #f]
  439. [`(+ ,c1 ,c2) #f]))
  440. (leaf? `(read))
  441. (leaf? `(- 8))
  442. (leaf? `(+ (read) (- 8)))
  443. \end{lstlisting}
  444. \end{minipage}
  445. \vrule
  446. \begin{minipage}{0.25\textwidth}
  447. \begin{lstlisting}
  448. #t
  449. #f
  450. #f
  451. \end{lstlisting}
  452. \end{minipage}
  453. \end{center}
  454. \section{Recursion}
  455. \label{sec:recursion}
  456. Programs are inherently recursive in that an $R_0$ AST is made
  457. up of smaller $R_0$ ASTs. Thus, the natural way to process in
  458. entire program is with a recursive function. As a first example of
  459. such a function, we define \texttt{R0?} below, which takes an
  460. arbitrary S-expression, {\tt sexp}, and determines whether or not {\tt
  461. sexp} is in {\tt arith}. Note that each match clause corresponds to
  462. one grammar rule for $R_0$ and the body of each clause makes a
  463. recursive call for each child node. This pattern of recursive function
  464. is so common that it has a name, \emph{structural recursion}. In
  465. general, when a recursive function is defined using a sequence of
  466. match clauses that correspond to a grammar, and each clause body makes
  467. a recursive call on each child node, then we say the function is
  468. defined by structural recursion.
  469. \begin{center}
  470. \begin{minipage}{0.7\textwidth}
  471. \begin{lstlisting}
  472. (define (R0? sexp)
  473. (match sexp
  474. [(? fixnum?) #t]
  475. [`(read) #t]
  476. [`(- ,e) (R0? e)]
  477. [`(+ ,e1 ,e2)
  478. (and (R0? e1) (R0? e2))]
  479. [`(program ,e) (R0? e)]
  480. [else #f]))
  481. (R0? `(+ (read) (- 8)))
  482. (R0? `(- (read) (+ 8)))
  483. \end{lstlisting}
  484. \end{minipage}
  485. \vrule
  486. \begin{minipage}{0.25\textwidth}
  487. \begin{lstlisting}
  488. #t
  489. #f
  490. \end{lstlisting}
  491. \end{minipage}
  492. \end{center}
  493. \section{Interpreters}
  494. \label{sec:interp-R0}
  495. The meaning, or semantics, of a program is typically defined in the
  496. specification of the language. For example, the Scheme language is
  497. defined in the report by \cite{SPERBER:2009aa}. The Racket language is
  498. defined in its reference manual~\citep{plt-tr}. In this book we use an
  499. interpreter to define the meaning of each language that we consider,
  500. following Reynold's advice in this
  501. regard~\citep{reynolds72:_def_interp}. Here we will warm up by writing
  502. an interpreter for the $R_0$ language, which will also serve
  503. as a second example of structural recursion. The \texttt{interp-R0}
  504. function is defined in Figure~\ref{fig:interp-R0}. The body of the
  505. function is a match on the input expression \texttt{e} and there is
  506. one clause per grammar rule for $R_0$. The clauses for
  507. internal AST nodes make recursive calls to \texttt{interp-R0} on
  508. each child node.
  509. \begin{figure}[tbp]
  510. \begin{lstlisting}
  511. (define (interp-R0 e)
  512. (match e
  513. [(? fixnum?) e]
  514. [`(read)
  515. (define r (read))
  516. (cond [(fixnum? r) r]
  517. [else (error 'interp-R0 "expected an integer" r)])]
  518. [`(- ,e)
  519. (fx- 0 (interp-R0 e))]
  520. [`(+ ,e1 ,e2)
  521. (fx+ (interp-R0 e1) (interp-R0 e2))]
  522. [`(program ,e) (interp-R0 e)]
  523. ))
  524. \end{lstlisting}
  525. \caption{Interpreter for the $R_0$ language.}
  526. \label{fig:interp-R0}
  527. \end{figure}
  528. Let us consider the result of interpreting some example $R_0$
  529. programs. The following program simply adds two integers.
  530. \begin{lstlisting}
  531. (+ 10 32)
  532. \end{lstlisting}
  533. The result is \key{42}, as you might have expected.
  534. %
  535. The next example demonstrates that expressions may be nested within
  536. each other, in this case nesting several additions and negations.
  537. \begin{lstlisting}
  538. (+ 10 (- (+ 12 20)))
  539. \end{lstlisting}
  540. What is the result of the above program?
  541. If we interpret the AST \eqref{eq:arith-prog} and give it the input
  542. \texttt{50}
  543. \begin{lstlisting}
  544. (interp-R0 ast1.1)
  545. \end{lstlisting}
  546. we get the answer to life, the universe, and everything:
  547. \begin{lstlisting}
  548. 42
  549. \end{lstlisting}
  550. Moving on, the \key{read} operation prompts the user of the program
  551. for an integer. Given an input of \key{10}, the following program
  552. produces \key{42}.
  553. \begin{lstlisting}
  554. (+ (read) 32)
  555. \end{lstlisting}
  556. We include the \key{read} operation in $R_1$ so that a compiler for
  557. $R_1$ cannot be implemented simply by running the interpreter at
  558. compilation time to obtain the output and then generating the trivial
  559. code to return the output. (A clever student at Colorado did this the
  560. first time I taught the course.)
  561. The job of a compiler is to translate a program in one language into a
  562. program in another language so that the output program behaves the
  563. same way as the input program. This idea is depicted in the following
  564. diagram. Suppose we have two languages, $\mathcal{L}_1$ and
  565. $\mathcal{L}_2$, and an interpreter for each language. Suppose that
  566. the compiler translates program $P_1$ in language $\mathcal{L}_1$ into
  567. program $P_2$ in language $\mathcal{L}_2$. Then interpreting $P_1$
  568. and $P_2$ on their respective interpreters with input $i$ should yield
  569. the same output $o$.
  570. \begin{equation} \label{eq:compile-correct}
  571. \begin{tikzpicture}[baseline=(current bounding box.center)]
  572. \node (p1) at (0, 0) {$P_1$};
  573. \node (p2) at (3, 0) {$P_2$};
  574. \node (o) at (3, -2.5) {$o$};
  575. \path[->] (p1) edge [above] node {compile} (p2);
  576. \path[->] (p2) edge [right] node {interp-$\mathcal{L}_2$($i$)} (o);
  577. \path[->] (p1) edge [left] node {interp-$\mathcal{L}_1$($i$)} (o);
  578. \end{tikzpicture}
  579. \end{equation}
  580. In the next section we see our first example of a compiler, which is
  581. another example of structural recursion.
  582. \section{Partial Evaluation}
  583. \label{sec:partial-evaluation}
  584. In this section we consider a compiler that translates $R_0$
  585. programs into $R_0$ programs that are more efficient, that is,
  586. this compiler is an optimizer. Our optimizer will accomplish this by
  587. trying to eagerly compute the parts of the program that do not depend
  588. on any inputs. For example, given the following program
  589. \begin{lstlisting}
  590. (+ (read) (- (+ 5 3)))
  591. \end{lstlisting}
  592. our compiler will translate it into the program
  593. \begin{lstlisting}
  594. (+ (read) -8)
  595. \end{lstlisting}
  596. Figure~\ref{fig:pe-arith} gives the code for a simple partial
  597. evaluator for the $R_0$ language. The output of the partial evaluator
  598. is an $R_0$ program, which we build up using a combination of
  599. quasiquotes and commas. (Though no quasiquote is necessary for
  600. integers.) In Figure~\ref{fig:pe-arith}, the normal structural
  601. recursion is captured in the main \texttt{pe-arith} function whereas
  602. the code for partially evaluating negation and addition is factored
  603. into two separate helper functions: \texttt{pe-neg} and
  604. \texttt{pe-add}. The input to these helper functions is the output of
  605. partially evaluating the children nodes.
  606. \begin{figure}[tbp]
  607. \begin{lstlisting}
  608. (define (pe-neg r)
  609. (cond [(fixnum? r) (fx- 0 r)]
  610. [else `(- ,r)]))
  611. (define (pe-add r1 r2)
  612. (cond [(and (fixnum? r1) (fixnum? r2)) (fx+ r1 r2)]
  613. [else `(+ ,r1 ,r2)]))
  614. (define (pe-arith e)
  615. (match e
  616. [(? fixnum?) e]
  617. [`(read) `(read)]
  618. [`(- ,e1) (pe-neg (pe-arith e1))]
  619. [`(+ ,e1 ,e2) (pe-add (pe-arith e1) (pe-arith e2))]))
  620. \end{lstlisting}
  621. \caption{A partial evaluator for the $R_0$ language.}
  622. \label{fig:pe-arith}
  623. \end{figure}
  624. Our code for \texttt{pe-neg} and \texttt{pe-add} implements the simple
  625. idea of checking whether the inputs are integers and if they are, to
  626. go ahead and perform the arithmetic. Otherwise, we use quasiquote to
  627. create an AST node for the appropriate operation (either negation or
  628. addition) and use comma to splice in the child nodes.
  629. To gain some confidence that the partial evaluator is correct, we can
  630. test whether it produces programs that get the same result as the
  631. input program. That is, we can test whether it satisfies Diagram
  632. \eqref{eq:compile-correct}. The following code runs the partial
  633. evaluator on several examples and tests the output program. The
  634. \texttt{assert} function is defined in Appendix~\ref{appendix:utilities}.
  635. \begin{lstlisting}
  636. (define (test-pe p)
  637. (assert "testing pe-arith"
  638. (equal? (interp-R0 p) (interp-R0 (pe-arith p)))))
  639. (test-pe `(+ (read) (- (+ 5 3))))
  640. (test-pe `(+ 1 (+ (read) 1)))
  641. (test-pe `(- (+ (read) (- 5))))
  642. \end{lstlisting}
  643. \begin{exercise}
  644. \normalfont % I don't like the italics for exercises. -Jeremy
  645. We challenge the reader to improve on the simple partial evaluator in
  646. Figure~\ref{fig:pe-arith} by replacing the \texttt{pe-neg} and
  647. \texttt{pe-add} helper functions with functions that know more about
  648. arithmetic. For example, your partial evaluator should translate
  649. \begin{lstlisting}
  650. (+ 1 (+ (read) 1))
  651. \end{lstlisting}
  652. into
  653. \begin{lstlisting}
  654. (+ 2 (read))
  655. \end{lstlisting}
  656. To accomplish this, we recommend that your partial evaluator produce
  657. output that takes the form of the $\itm{residual}$ non-terminal in the
  658. following grammar.
  659. \[
  660. \begin{array}{lcl}
  661. \Exp &::=& (\key{read}) \mid (\key{-} \;(\key{read})) \mid (\key{+} \; \Exp \; \Exp)\\
  662. \itm{residual} &::=& \Int \mid (\key{+}\; \Int\; \Exp) \mid \Exp
  663. \end{array}
  664. \]
  665. \end{exercise}
  666. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  667. \chapter{Compiling Integers and Variables}
  668. \label{ch:int-exp}
  669. This chapter concerns the challenge of compiling a subset of Racket,
  670. which we name $R_1$, to x86-64 assembly code~\citep{Intel:2015aa}.
  671. (Henceforce we shall refer to x86-64 simply as x86). The chapter
  672. begins with a description of the $R_1$ language (Section~\ref{sec:s0})
  673. and then a description of x86 (Section~\ref{sec:x86}). The
  674. x86 assembly language is quite large, so we only discuss what is
  675. needed for compiling $R_1$. We introduce more of x86 in later
  676. chapters. Once we have introduced $R_1$ and x86, we reflect on
  677. their differences and come up with a plan breaking down the
  678. translation from $R_1$ to x86 into a handful of steps
  679. (Section~\ref{sec:plan-s0-x86}). The rest of the sections in this
  680. Chapter give detailed hints regarding each step
  681. (Sections~\ref{sec:uniquify-s0} through \ref{sec:patch-s0}). We hope
  682. to give enough hints that the well-prepared reader can implement a
  683. compiler from $R_1$ to x86 while at the same time leaving room for
  684. some fun and creativity.
  685. \section{The $R_1$ Language}
  686. \label{sec:s0}
  687. The $R_1$ language extends the $R_0$ language
  688. (Figure~\ref{fig:r0-syntax}) with variable definitions. The syntax of
  689. the $R_1$ language is defined by the grammar in
  690. Figure~\ref{fig:r1-syntax}. As in $R_0$, \key{read} is a nullary
  691. operator, \key{-} is a unary operator, and \key{+} is a binary
  692. operator. In addition to variable definitions, the $R_1$ language
  693. includes the \key{program} form to mark the top of the program, which
  694. is helpful in some of the compiler passes. The $R_1$ language is rich
  695. enough to exhibit several compilation techniques but simple enough so
  696. that the reader can implement a compiler for it in a week of part-time
  697. work. To give the reader a feeling for the scale of this first
  698. compiler, the instructor solution for the $R_1$ compiler consists of 6
  699. recursive functions and a few small helper functions that together
  700. span 256 lines of code.
  701. \begin{figure}[btp]
  702. \centering
  703. \fbox{
  704. \begin{minipage}{0.96\textwidth}
  705. \[
  706. \begin{array}{rcl}
  707. \Exp &::=& \Int \mid (\key{read}) \mid (\key{-}\;\Exp) \mid (\key{+} \; \Exp\;\Exp) \\
  708. &\mid& \Var \mid \LET{\Var}{\Exp}{\Exp} \\
  709. R_1 &::=& (\key{program} \; \Exp)
  710. \end{array}
  711. \]
  712. \end{minipage}
  713. }
  714. \caption{The syntax of the $R_1$ language.
  715. The non-terminal \Var{} may be any Racket identifier.}
  716. \label{fig:r1-syntax}
  717. \end{figure}
  718. The \key{let} construct defines a variable for use within its body
  719. and initializes the variable with the value of an expression. So the
  720. following program initializes \code{x} to \code{32} and then evaluates
  721. the body \code{(+ 10 x)}, producing \code{42}.
  722. \begin{lstlisting}
  723. (program
  724. (let ([x (+ 12 20)]) (+ 10 x)))
  725. \end{lstlisting}
  726. When there are multiple \key{let}'s for the same variable, the closest
  727. enclosing \key{let} is used. That is, variable definitions overshadow
  728. prior definitions. Consider the following program with two \key{let}'s
  729. that define variables named \code{x}. Can you figure out the result?
  730. \begin{lstlisting}
  731. (program
  732. (let ([x 32]) (+ (let ([x 10]) x) x)))
  733. \end{lstlisting}
  734. For the purposes of showing which variable uses correspond to which
  735. definitions, the following shows the \code{x}'s annotated with subscripts
  736. to distinguish them. Double check that your answer for the above is
  737. the same as your answer for this annotated version of the program.
  738. \begin{lstlisting}
  739. (program
  740. (let ([x|$_1$| 32]) (+ (let ([x|$_2$| 10]) x|$_2$|) x|$_1$|)))
  741. \end{lstlisting}
  742. The initializing expression is always evaluated before the body of the
  743. \key{let}, so in the following, the \key{read} for \code{x} is
  744. performed before the \key{read} for \code{y}. Given the input
  745. \code{52} then \code{10}, the following produces \code{42} (and not
  746. \code{-42}).
  747. \begin{lstlisting}
  748. (program
  749. (let ([x (read)]) (let ([y (read)]) (- x y))))
  750. \end{lstlisting}
  751. Figure~\ref{fig:interp-R1} shows the interpreter for the $R_1$
  752. language. It extends the interpreter for $R_0$ with two new
  753. \key{match} clauses for variables and for \key{let}. For \key{let},
  754. we will need a way to communicate the initializing value of a variable
  755. to all the uses of a variable. To accomplish this, we maintain a
  756. mapping from variables to values, which is traditionally called an
  757. \emph{environment}. For simplicity, here we use an association list to
  758. represent the environment. The \code{interp-R1} function takes the
  759. current environment, \code{env}, as an extra parameter. When the
  760. interpreter encounters a variable, it finds the corresponding value
  761. using the \code{lookup} function (Appendix~\ref{appendix:utilities}).
  762. When the interpreter encounters a \key{let}, it evaluates the
  763. initializing expression, extends the environment with the result bound
  764. to the variable, then evaluates the body of the \key{let}.
  765. \begin{figure}[tbp]
  766. \begin{lstlisting}
  767. (define (interp-R1 env e)
  768. (match e
  769. [(? symbol?) (lookup e env)]
  770. [`(let ([,x ,e]) ,body)
  771. (define v (interp-R1 env e))
  772. (define new-env (cons (cons x v) env))
  773. (interp-R1 new-env body)]
  774. [(? fixnum?) e]
  775. [`(read)
  776. (define r (read))
  777. (cond [(fixnum? r) r]
  778. [else (error 'interp-R1 "expected an integer" r)])]
  779. [`(- ,e)
  780. (fx- 0 (interp-R1 env e))]
  781. [`(+ ,e1 ,e2)
  782. (fx+ (interp-R1 env e1) (interp-R1 env e2))]
  783. [`(program ,e) (interp-R1 '() e)]
  784. ))
  785. \end{lstlisting}
  786. \caption{Interpreter for the $R_1$ language.}
  787. \label{fig:interp-R1}
  788. \end{figure}
  789. The goal for this chapter is to implement a compiler that translates
  790. any program $P_1$ in the $R_1$ language into an x86 assembly
  791. program $P_2$ such that $P_2$ exhibits the same behavior on an x86
  792. computer as the $R_1$ program running in a Racket implementation.
  793. That is, they both output the same integer $n$.
  794. \[
  795. \begin{tikzpicture}[baseline=(current bounding box.center)]
  796. \node (p1) at (0, 0) {$P_1$};
  797. \node (p2) at (4, 0) {$P_2$};
  798. \node (o) at (4, -2) {$n$};
  799. \path[->] (p1) edge [above] node {\footnotesize compile} (p2);
  800. \path[->] (p1) edge [left] node {\footnotesize interp-$R_1$} (o);
  801. \path[->] (p2) edge [right] node {\footnotesize interp-x86} (o);
  802. \end{tikzpicture}
  803. \]
  804. In the next section we introduce enough of the x86 assembly
  805. language to compile $R_1$.
  806. \section{The x86 Assembly Language}
  807. \label{sec:x86}
  808. An x86 program is a sequence of instructions. The instructions may
  809. refer to integer constants (called \emph{immediate values}), variables
  810. called \emph{registers}, and instructions may load and store values
  811. into \emph{memory}. Memory is a mapping of 64-bit addresses to 64-bit
  812. values. Figure~\ref{fig:x86-a} defines the syntax for the subset of
  813. the x86 assembly language needed for this chapter. (We use the
  814. AT\&T syntax expected by the GNU assembler inside \key{gcc}.)
  815. \begin{figure}[tbp]
  816. \fbox{
  817. \begin{minipage}{0.96\textwidth}
  818. \[
  819. \begin{array}{lcl}
  820. \Reg &::=& \key{rsp} \mid \key{rbp} \mid \key{rax} \mid \key{rbx} \mid \key{rcx}
  821. \mid \key{rdx} \mid \key{rsi} \mid \key{rdi} \mid \\
  822. && \key{r8} \mid \key{r9} \mid \key{r10}
  823. \mid \key{r11} \mid \key{r12} \mid \key{r13}
  824. \mid \key{r14} \mid \key{r15} \\
  825. \Arg &::=& \key{\$}\Int \mid \key{\%}\Reg \mid \Int(\key{\%}\Reg) \\
  826. \Instr &::=& \key{addq} \; \Arg, \Arg \mid
  827. \key{subq} \; \Arg, \Arg \mid
  828. % \key{imulq} \; \Arg,\Arg \mid
  829. \key{negq} \; \Arg \mid \key{movq} \; \Arg, \Arg \mid \\
  830. && \key{callq} \; \mathit{label} \mid
  831. \key{pushq}\;\Arg \mid \key{popq}\;\Arg \mid \key{retq} \\
  832. \Prog &::= & \key{.globl main}\\
  833. & & \key{main:} \; \Instr^{+}
  834. \end{array}
  835. \]
  836. \end{minipage}
  837. }
  838. \caption{A subset of the x86 assembly language (AT\&T syntax).}
  839. \label{fig:x86-a}
  840. \end{figure}
  841. An immediate value is written using the notation \key{\$}$n$ where $n$
  842. is an integer.
  843. %
  844. A register is written with a \key{\%} followed by the register name,
  845. such as \key{\%rax}.
  846. %
  847. An access to memory is specified using the syntax $n(\key{\%}r)$,
  848. which reads register $r$ and then offsets the address by $n$ bytes
  849. (8 bits). The address is then used to either load or store to memory
  850. depending on whether it occurs as a source or destination argument of
  851. an instruction.
  852. An arithmetic instruction, such as $\key{addq}\,s,\,d$, reads from the
  853. source $s$ and destination $d$, applies the arithmetic operation, then
  854. writes the result in $d$.
  855. %
  856. The move instruction, $\key{movq}\,s\,d$ reads from $s$ and stores the
  857. result in $d$.
  858. %
  859. The $\key{callq}\,\mathit{label}$ instruction executes the procedure
  860. specified by the label.
  861. Figure~\ref{fig:p0-x86} depicts an x86 program that is equivalent
  862. to \code{(+ 10 32)}. The \key{globl} directive says that the
  863. \key{main} procedure is externally visible, which is necessary so
  864. that the operating system can call it. The label \key{main:}
  865. indicates the beginning of the \key{main} procedure which is where
  866. the operating system starts executing this program. The instruction
  867. \lstinline{movq $10, %rax} puts $10$ into register \key{rax}. The
  868. following instruction \lstinline{addq $32, %rax} adds $32$ to the
  869. $10$ in \key{rax} and puts the result, $42$, back into
  870. \key{rax}. The instruction \lstinline{movq %rax, %rdi} moves the value
  871. in \key{rax} into another register, \key{rdi}, and
  872. \lstinline{callq print_int} calls the external function \code{print\_int}, which
  873. prints the value in \key{rdi}.
  874. The instruction \key{retq} finishes the \key{main}
  875. function by returning the integer in \key{rax} to the
  876. operating system.
  877. %\begin{wrapfigure}{r}{2.25in}
  878. \begin{figure}[tbp]
  879. \begin{lstlisting}
  880. .globl main
  881. main:
  882. movq $10, %rax
  883. addq $32, %rax
  884. movq %rax, %rdi
  885. callq print_int
  886. retq
  887. \end{lstlisting}
  888. \caption{An x86 program equivalent to $\BINOP{+}{10}{32}$.}
  889. \label{fig:p0-x86}
  890. %\end{wrapfigure}
  891. \end{figure}
  892. %% \marginpar{Consider using italics for the texts in these figures.
  893. %% It can get confusing to differentiate them from the main text.}
  894. %% It looks pretty ugly in italics.-Jeremy
  895. Unfortunately, x86 varies in a couple ways depending on what
  896. operating system it is assembled in. The code examples shown here are
  897. correct on the Unix platform, but when assembled on Mac OS X, labels
  898. like \key{main} must be prefixed with an underscore. So the correct
  899. output for the above program on Mac would begin with:
  900. \begin{lstlisting}
  901. .globl _main
  902. _main:
  903. ...
  904. \end{lstlisting}
  905. The next example exhibits the use of memory. Figure~\ref{fig:p1-x86}
  906. lists an x86 program that is equivalent to $\BINOP{+}{52}{
  907. \UNIOP{-}{10} }$. To understand how this x86 program works, we
  908. need to explain a region of memory called the \emph{procedure call
  909. stack} (or \emph{stack} for short). The stack consists of a separate
  910. \emph{frame} for each procedure call. The memory layout for an
  911. individual frame is shown in Figure~\ref{fig:frame}. The register
  912. \key{rsp} is called the \emph{stack pointer} and points to the item at
  913. the top of the stack. The stack grows downward in memory, so we
  914. increase the size of the stack by subtracting from the stack
  915. pointer. The frame size is required to be a multiple of 16 bytes. The
  916. register \key{rbp} is the \emph{base pointer} which serves two
  917. purposes: 1) it saves the location of the stack pointer for the
  918. procedure that called the current one and 2) it is used to access
  919. variables associated with the current procedure. We number the
  920. variables from $1$ to $n$. Variable $1$ is stored at address
  921. $-8\key{(\%rbp)}$, variable $2$ at $-16\key{(\%rbp)}$, etc.
  922. %\begin{wrapfigure}{r}{2.1in}
  923. \begin{figure}[tbp]
  924. \begin{lstlisting}
  925. .globl main
  926. main:
  927. pushq %rbp
  928. movq %rsp, %rbp
  929. subq $16, %rsp
  930. movq $10, -8(%rbp)
  931. negq -8(%rbp)
  932. movq $52, %rax
  933. addq -8(%rbp), %rax
  934. movq %rax, %rdi
  935. callq print_int
  936. addq $16, %rsp
  937. popq %rbp
  938. retq
  939. \end{lstlisting}
  940. \caption{An x86 program equivalent to $\BINOP{+}{52}{\UNIOP{-}{10} }$.}
  941. \label{fig:p1-x86}
  942. \end{figure}
  943. %\end{wrapfigure}
  944. \begin{figure}[tbp]
  945. \centering
  946. \begin{tabular}{|r|l|} \hline
  947. Position & Contents \\ \hline
  948. 8(\key{\%rbp}) & return address \\
  949. 0(\key{\%rbp}) & old \key{rbp} \\
  950. -8(\key{\%rbp}) & variable $1$ \\
  951. -16(\key{\%rbp}) & variable $2$ \\
  952. \ldots & \ldots \\
  953. 0(\key{\%rsp}) & variable $n$\\ \hline
  954. \end{tabular}
  955. \caption{Memory layout of a frame.}
  956. \label{fig:frame}
  957. \end{figure}
  958. Getting back to the program in Figure~\ref{fig:p1-x86}, the first
  959. three instructions are the typical \emph{prelude} for a procedure.
  960. The instruction \key{pushq \%rbp} saves the base pointer for the
  961. procedure that called the current one onto the stack and subtracts $8$
  962. from the stack pointer. The second instruction \key{movq \%rsp, \%rbp}
  963. changes the base pointer to the top of the stack. The instruction
  964. \key{subq \$16, \%rsp} moves the stack pointer down to make enough
  965. room for storing variables. This program just needs one variable ($8$
  966. bytes) but because the frame size is required to be a multiple of 16
  967. bytes, it rounds to 16 bytes.
  968. The next four instructions carry out the work of computing
  969. $\BINOP{+}{52}{\UNIOP{-}{10} }$. The first instruction \key{movq \$10,
  970. -8(\%rbp)} stores $10$ in variable $1$. The instruction \key{negq
  971. -8(\%rbp)} changes variable $1$ to $-10$. The \key{movq \$52, \%rax}
  972. places $52$ in the register \key{rax} and \key{addq -8(\%rbp), \%rax}
  973. adds the contents of variable $1$ to \key{rax}, at which point
  974. \key{rax} contains $42$.
  975. The last five instructions are the typical \emph{conclusion} of a
  976. procedure. The first two print the final result of the program. The
  977. latter three are necessary to get the state of the machine back to
  978. where it was before the current procedure was called. The \key{addq
  979. \$16, \%rsp} instruction moves the stack pointer back to point at
  980. the old base pointer. The amount added here needs to match the amount
  981. that was subtracted in the prelude of the procedure. Then \key{popq
  982. \%rbp} returns the old base pointer to \key{rbp} and adds $8$ to the
  983. stack pointer. The \key{retq} instruction jumps back to the procedure
  984. that called this one and subtracts 8 from the stack pointer.
  985. The compiler will need a convenient representation for manipulating
  986. x86 programs, so we define an abstract syntax for x86 in
  987. Figure~\ref{fig:x86-ast-a}. The $\Int$ field of the \key{program} AST
  988. node is number of bytes of stack space needed for variables in the
  989. program. (Some of the intermediate languages will store other
  990. information in that location for the purposes of communicating
  991. auxiliary data from one step of the compiler to the next. )
  992. %% \marginpar{Consider mentioning PseudoX86, since I think that's what
  993. %% you actually are referring to.}
  994. %% Not here. PseudoX86 is the language with variables and
  995. %% instructions that don't obey the x86 rules. -Jeremy
  996. \begin{figure}[tbp]
  997. \fbox{
  998. \begin{minipage}{0.96\textwidth}
  999. \[
  1000. \begin{array}{lcl}
  1001. \Arg &::=& \INT{\Int} \mid \REG{\itm{register}}
  1002. \mid \STACKLOC{\Int} \\
  1003. \Instr &::=& (\key{addq} \; \Arg\; \Arg) \mid
  1004. (\key{subq} \; \Arg\; \Arg) \mid
  1005. % (\key{imulq} \; \Arg\;\Arg) \mid
  1006. (\key{negq} \; \Arg) \mid (\key{movq} \; \Arg\; \Arg) \\
  1007. &\mid& (\key{callq} \; \mathit{label}) \mid
  1008. (\key{pushq}\;\Arg) \mid
  1009. (\key{popq}\;\Arg) \mid
  1010. (\key{retq}) \\
  1011. x86_0 &::= & (\key{program} \;\Int \; \Instr^{+})
  1012. \end{array}
  1013. \]
  1014. \end{minipage}
  1015. }
  1016. \caption{Abstract syntax for x86 assembly.}
  1017. \label{fig:x86-ast-a}
  1018. \end{figure}
  1019. %% \marginpar{I think this is PseudoX86, not x86.}
  1020. \section{Planning the trip from $R_1$ to x86}
  1021. \label{sec:plan-s0-x86}
  1022. To compile one language to another it helps to focus on the
  1023. differences between the two languages. It is these differences that
  1024. the compiler will need to bridge. What are the differences between
  1025. $R_1$ and x86 assembly? Here we list some of the most important the
  1026. differences.
  1027. \begin{enumerate}
  1028. \item x86 arithmetic instructions typically take two arguments and
  1029. update the second argument in place. In contrast, $R_1$ arithmetic
  1030. operations only read their arguments and produce a new value.
  1031. \item An argument to an $R_1$ operator can be any expression, whereas
  1032. x86 instructions restrict their arguments to integers, registers,
  1033. and memory locations.
  1034. \item An $R_1$ program can have any number of variables whereas x86
  1035. has only 16 registers.
  1036. \item Variables in $R_1$ can overshadow other variables with the same
  1037. name. The registers and memory locations of x86 all have unique
  1038. names.
  1039. \end{enumerate}
  1040. We ease the challenge of compiling from $R_1$ to x86 by breaking down
  1041. the problem into several steps, dealing with the above differences one
  1042. at a time. The main question then becomes: in what order do we tackle
  1043. these differences? This is often one of the most challenging questions
  1044. that a compiler writer must answer because some orderings may be much
  1045. more difficult to implement than others. It is difficult to know ahead
  1046. of time which orders will be better so often some trial-and-error is
  1047. involved. However, we can try to plan ahead and choose the orderings
  1048. based on this planning.
  1049. For example, to handle difference \#2 (nested expressions), we shall
  1050. introduce new variables and pull apart the nested expressions into a
  1051. sequence of assignment statements. To deal with difference \#3 we
  1052. will be replacing variables with registers and/or stack
  1053. locations. Thus, it makes sense to deal with \#2 before \#3 so that
  1054. \#3 can replace both the original variables and the new ones. Next,
  1055. consider where \#1 should fit in. Because it has to do with the format
  1056. of x86 instructions, it makes more sense after we have flattened the
  1057. nested expressions (\#2). Finally, when should we deal with \#4
  1058. (variable overshadowing)? We shall solve this problem by renaming
  1059. variables to make sure they have unique names. Recall that our plan
  1060. for \#2 involves moving nested expressions, which could be problematic
  1061. if it changes the shadowing of variables. However, if we deal with \#4
  1062. first, then it will not be an issue. Thus, we arrive at the following
  1063. ordering.
  1064. \[
  1065. \begin{tikzpicture}[baseline=(current bounding box.center)]
  1066. \foreach \i/\p in {4/1,2/2,1/3,3/4}
  1067. {
  1068. \node (\i) at (\p*1.5,0) {$\i$};
  1069. }
  1070. \foreach \x/\y in {4/2,2/1,1/3}
  1071. {
  1072. \draw[->] (\x) to (\y);
  1073. }
  1074. \end{tikzpicture}
  1075. \]
  1076. We further simplify the translation from $R_1$ to x86 by identifying
  1077. an intermediate language named $C_0$, roughly half-way between $R_1$
  1078. and x86, to provide a rest stop along the way. We name the language
  1079. $C_0$ because it is vaguely similar to the $C$
  1080. language~\citep{Kernighan:1988nx}. The differences \#4 and \#1,
  1081. regarding variables and nested expressions, will be handled by two
  1082. steps, \key{uniquify} and \key{flatten}, which bring us to
  1083. $C_0$.
  1084. \[
  1085. \begin{tikzpicture}[baseline=(current bounding box.center)]
  1086. \foreach \i/\p in {R_1/1,R_1/2,C_0/3}
  1087. {
  1088. \node (\p) at (\p*3,0) {\large $\i$};
  1089. }
  1090. \foreach \x/\y/\lbl in {1/2/uniquify,2/3/flatten}
  1091. {
  1092. \path[->,bend left=15] (\x) edge [above] node {\ttfamily\footnotesize \lbl} (\y);
  1093. }
  1094. \end{tikzpicture}
  1095. \]
  1096. Each of these steps in the compiler is implemented by a function,
  1097. typically a structurally recursive function that translates an input
  1098. AST into an output AST. We refer to such a function as a \emph{pass}
  1099. because it makes a pass over, i.e. it traverses the entire AST.
  1100. The syntax for $C_0$ is defined in Figure~\ref{fig:c0-syntax}. The
  1101. $C_0$ language supports the same operators as $R_1$ but the arguments
  1102. of operators are now restricted to just variables and integers. The
  1103. \key{let} construct of $R_1$ is replaced by an assignment statement
  1104. and there is a \key{return} construct to specify the return value of
  1105. the program. A program consists of a sequence of statements that
  1106. include at least one \key{return} statement. Each program is also
  1107. annotated with a list of variables (viz. {\tt (var*)}). At the start
  1108. of the program, these variables are uninitialized (they contain garbage)
  1109. and each variable becomes initialized on its first assignment. All of
  1110. the variables used in the program must be present in this list.
  1111. \begin{figure}[tbp]
  1112. \fbox{
  1113. \begin{minipage}{0.96\textwidth}
  1114. \[
  1115. \begin{array}{lcl}
  1116. \Arg &::=& \Int \mid \Var \\
  1117. \Exp &::=& \Arg \mid (\key{read}) \mid (\key{-}\;\Arg) \mid (\key{+} \; \Arg\;\Arg)\\
  1118. \Stmt &::=& \ASSIGN{\Var}{\Exp} \mid \RETURN{\Arg} \\
  1119. C_0 & ::= & (\key{program}\;(\Var^{*})\;\Stmt^{+})
  1120. \end{array}
  1121. \]
  1122. \end{minipage}
  1123. }
  1124. \caption{The $C_0$ intermediate language.}
  1125. \label{fig:c0-syntax}
  1126. \end{figure}
  1127. To get from $C_0$ to x86 assembly it remains for us to handle
  1128. difference \#1 (the format of instructions) and difference \#3
  1129. (variables versus registers). These two differences are intertwined,
  1130. creating a bit of a Gordian Knot. To handle difference \#3, we need to
  1131. map some variables to registers (there are only 16 registers) and the
  1132. remaining variables to locations on the stack (which is unbounded). To
  1133. make good decisions regarding this mapping, we need the program to be
  1134. close to its final form (in x86 assembly) so we know exactly when
  1135. which variables are used. After all, variables that are used in
  1136. disjoint parts of the program can be assigned to the same register.
  1137. However, our choice of x86 instructions depends on whether the
  1138. variables are mapped to registers or stack locations, so we have a
  1139. circular dependency. We cut this knot by doing an optimistic selection
  1140. of instructions in the \key{select-instructions} pass, followed by the
  1141. \key{assign-homes} pass to map variables to registers or stack
  1142. locations, and conclude by finalizing the instruction selection in the
  1143. \key{patch-instructions} pass.
  1144. \[
  1145. \begin{tikzpicture}[baseline=(current bounding box.center)]
  1146. \node (1) at (0,0) {\large $C_0$};
  1147. \node (2) at (3,0) {\large $\text{x86}^{*}$};
  1148. \node (3) at (6,0) {\large $\text{x86}^{*}$};
  1149. \node (4) at (9,0) {\large $\text{x86}$};
  1150. \path[->,bend left=15] (1) edge [above] node {\ttfamily\footnotesize select-instr.} (2);
  1151. \path[->,bend left=15] (2) edge [above] node {\ttfamily\footnotesize assign-homes} (3);
  1152. \path[->,bend left=15] (3) edge [above] node {\ttfamily\footnotesize patch-instr.} (4);
  1153. \end{tikzpicture}
  1154. \]
  1155. The \key{select-instructions} pass is optimistic in the sense that it
  1156. treats variables as if they were all mapped to registers. The
  1157. \key{select-instructions} pass generates a program that consists of
  1158. x86 instructions but that still uses variables, so it is an
  1159. intermediate language that is technically different than x86, which
  1160. explains the asterisks in the diagram above.
  1161. In this Chapter we shall take the easy road to implementing
  1162. \key{assign-homes} and simply map all variables to stack locations.
  1163. The topic of Chapter~\ref{ch:register-allocation} is implementing a
  1164. smarter approach in which we make a best-effort to map variables to
  1165. registers, resorting to the stack only when necessary.
  1166. %% \marginpar{\scriptsize I'm confused: shouldn't `select instructions' do this?
  1167. %% After all, that selects the x86 instructions. Even if it is separate,
  1168. %% if we perform `patching' before register allocation, we aren't forced to rely on
  1169. %% \key{rax} as much. This can ultimately make a more-performant result. --
  1170. %% Cam}
  1171. Once variables have been assigned to their homes, we can finalize the
  1172. instruction selection by dealing with an idiosyncrasy of x86
  1173. assembly. Many x86 instructions have two arguments but only one of the
  1174. arguments may be a memory reference (and the stack is a part of
  1175. memory). Because some variables may get mapped to stack locations,
  1176. some of our generated instructions may violate this restriction. The
  1177. purpose of the \key{patch-instructions} pass is to fix this problem by
  1178. replacing every violating instruction with a short sequence of
  1179. instructions that use the \key{rax} register. Once we have implemented
  1180. a good register allocator (Chapter~\ref{ch:register-allocation}), the
  1181. need to patch instructions will be relatively rare.
  1182. \section{Uniquify Variables}
  1183. \label{sec:uniquify-s0}
  1184. The purpose of this pass is to make sure that each \key{let} uses a
  1185. unique variable name. For example, the \code{uniquify} pass should
  1186. translate the program on the left into the program on the right. \\
  1187. \begin{tabular}{lll}
  1188. \begin{minipage}{0.4\textwidth}
  1189. \begin{lstlisting}
  1190. (program
  1191. (let ([x 32])
  1192. (+ (let ([x 10]) x) x)))
  1193. \end{lstlisting}
  1194. \end{minipage}
  1195. &
  1196. $\Rightarrow$
  1197. &
  1198. \begin{minipage}{0.4\textwidth}
  1199. \begin{lstlisting}
  1200. (program
  1201. (let ([x.1 32])
  1202. (+ (let ([x.2 10]) x.2) x.1)))
  1203. \end{lstlisting}
  1204. \end{minipage}
  1205. \end{tabular} \\
  1206. %
  1207. The following is another example translation, this time of a program
  1208. with a \key{let} nested inside the initializing expression of another
  1209. \key{let}.\\
  1210. \begin{tabular}{lll}
  1211. \begin{minipage}{0.4\textwidth}
  1212. \begin{lstlisting}
  1213. (program
  1214. (let ([x (let ([x 4])
  1215. (+ x 1))])
  1216. (+ x 2)))
  1217. \end{lstlisting}
  1218. \end{minipage}
  1219. &
  1220. $\Rightarrow$
  1221. &
  1222. \begin{minipage}{0.4\textwidth}
  1223. \begin{lstlisting}
  1224. (program
  1225. (let ([x.2 (let ([x.1 4])
  1226. (+ x.1 1))])
  1227. (+ x.2 2)))
  1228. \end{lstlisting}
  1229. \end{minipage}
  1230. \end{tabular}
  1231. We recommend implementing \code{uniquify} as a structurally recursive
  1232. function that mostly copies the input program. However, when
  1233. encountering a \key{let}, it should generate a unique name for the
  1234. variable (the Racket function \code{gensym} is handy for this) and
  1235. associate the old name with the new unique name in an association
  1236. list. The \code{uniquify} function will need to access this
  1237. association list when it gets to a variable reference, so we add
  1238. another parameter to \code{uniquify} for the association list. It is
  1239. quite common for a compiler pass to need a map to store extra
  1240. information about variables. Such maps are often called \emph{symbol
  1241. tables}.
  1242. The skeleton of the \code{uniquify} function is shown in
  1243. Figure~\ref{fig:uniquify-s0}. The function is curried so that it is
  1244. convenient to partially apply it to an association list and then apply
  1245. it to different expressions, as in the last clause for primitive
  1246. operations in Figure~\ref{fig:uniquify-s0}. In the last \key{match}
  1247. clause for the primitive operators, note the use of the comma-@
  1248. operator to splice a list of S-expressions into an enclosing
  1249. S-expression.
  1250. \begin{exercise}
  1251. \normalfont % I don't like the italics for exercises. -Jeremy
  1252. Complete the \code{uniquify} pass by filling in the blanks, that is,
  1253. implement the clauses for variables and for the \key{let} construct.
  1254. \end{exercise}
  1255. \begin{figure}[tbp]
  1256. \begin{lstlisting}
  1257. (define uniquify
  1258. (lambda (alist)
  1259. (lambda (e)
  1260. (match e
  1261. [(? symbol?) ___]
  1262. [(? integer?) e]
  1263. [`(let ([,x ,e]) ,body) ___]
  1264. [`(program ,e)
  1265. `(program ,((uniquify alist) e))]
  1266. [`(,op ,es ...)
  1267. `(,op ,@(map (uniquify alist) es))]
  1268. ))))
  1269. \end{lstlisting}
  1270. \caption{Skeleton for the \key{uniquify} pass.}
  1271. \label{fig:uniquify-s0}
  1272. \end{figure}
  1273. \begin{exercise}
  1274. \normalfont % I don't like the italics for exercises. -Jeremy
  1275. Test your \key{uniquify} pass by creating five example $R_1$ programs
  1276. and checking whether the output programs produce the same result as
  1277. the input programs. The $R_1$ programs should be designed to test the
  1278. most interesting parts of the \key{uniquify} pass, that is, the
  1279. programs should include \key{let} constructs, variables, and variables
  1280. that overshadow each other. The five programs should be in a
  1281. subdirectory named \key{tests} and they should have the same file name
  1282. except for a different integer at the end of the name, followed by the
  1283. ending \key{.rkt}. Use the \key{interp-tests} function
  1284. (Appendix~\ref{appendix:utilities}) from \key{utilities.rkt} to test
  1285. your \key{uniquify} pass on the example programs.
  1286. \end{exercise}
  1287. \section{Flatten Expressions}
  1288. \label{sec:flatten-r1}
  1289. The \code{flatten} pass will transform $R_1$ programs into $C_0$
  1290. programs. In particular, the purpose of the \code{flatten} pass is to
  1291. get rid of nested expressions, such as the \code{(- 10)} in the program
  1292. below. This can be accomplished by introducing a new variable,
  1293. assigning the nested expression to the new variable, and then using
  1294. the new variable in place of the nested expressions, as shown in the
  1295. output of \code{flatten} on the right.\\
  1296. \begin{tabular}{lll}
  1297. \begin{minipage}{0.4\textwidth}
  1298. \begin{lstlisting}
  1299. (program
  1300. (+ 52 (- 10)))
  1301. \end{lstlisting}
  1302. \end{minipage}
  1303. &
  1304. $\Rightarrow$
  1305. &
  1306. \begin{minipage}{0.4\textwidth}
  1307. \begin{lstlisting}
  1308. (program (tmp.1 tmp.2)
  1309. (assign tmp.1 (- 10))
  1310. (assign tmp.2 (+ 52 tmp.1))
  1311. (return tmp.2))
  1312. \end{lstlisting}
  1313. \end{minipage}
  1314. \end{tabular}
  1315. The clause of \code{flatten} for \key{let} is straightforward to
  1316. implement as it just requires the generation of an assignment
  1317. statement for the \key{let}-bound variable. The following shows the
  1318. result of \code{flatten} for a \key{let}. \\
  1319. \begin{tabular}{lll}
  1320. \begin{minipage}{0.4\textwidth}
  1321. \begin{lstlisting}
  1322. (program
  1323. (let ([x (+ (- 10) 11)])
  1324. (+ x 41)))
  1325. \end{lstlisting}
  1326. \end{minipage}
  1327. &
  1328. $\Rightarrow$
  1329. &
  1330. \begin{minipage}{0.4\textwidth}
  1331. \begin{lstlisting}
  1332. (program (tmp.1 x tmp.2)
  1333. (assign tmp.1 (- 10))
  1334. (assign x (+ tmp.1 11))
  1335. (assign tmp.2 (+ x 41))
  1336. (return tmp.2))
  1337. \end{lstlisting}
  1338. \end{minipage}
  1339. \end{tabular}
  1340. We recommend implementing \key{flatten} as a structurally recursive
  1341. function that returns two things, 1) the newly flattened expression,
  1342. and 2) a list of assignment statements, one for each of the new
  1343. variables introduced during the flattening the expression. The newly
  1344. flattened expression should be an $\Arg$ in the $C_0$ syntax
  1345. (Figure~\ref{fig:c0-syntax}), that is, it should be an integer or a
  1346. variable. You can return multiple things from a function using the
  1347. \key{values} form and you can receive multiple things from a function
  1348. call using the \key{define-values} form. If you are not familiar with
  1349. these constructs, the Racket documentation will be of help. Also, the
  1350. \key{map2} function (Appendix~\ref{appendix:utilities}) is useful for
  1351. applying a function to each element of a list, in the case where the
  1352. function returns two values. The result of \key{map2} is two lists.
  1353. The clause of \key{flatten} for the \key{program} node needs to
  1354. recursively flatten the body of the program and the newly flattened
  1355. expression should be placed in a \key{return} statement. The
  1356. \key{flatten} pass should also compute the list of variables used in
  1357. the program. I recommend traversing the statements in the body of the
  1358. program (after it has been flattened) and collect all variables that
  1359. appear on the left-hand-side of an assignment. Note that each variable
  1360. should only occur once in the list of variables that you place in the
  1361. \key{program} form.
  1362. Take special care for programs such as the following that initialize
  1363. variables with integers or other variables. It should be translated
  1364. to the program on the right \\
  1365. \begin{tabular}{lll}
  1366. \begin{minipage}{0.4\textwidth}
  1367. \begin{lstlisting}
  1368. (let ([a 42])
  1369. (let ([b a])
  1370. b))
  1371. \end{lstlisting}
  1372. \end{minipage}
  1373. &
  1374. $\Rightarrow$
  1375. &
  1376. \begin{minipage}{0.4\textwidth}
  1377. \begin{lstlisting}
  1378. (program (a b)
  1379. (assign a 42)
  1380. (assign b a)
  1381. (return b))
  1382. \end{lstlisting}
  1383. \end{minipage}
  1384. \end{tabular} \\
  1385. and not to the following, which could result from a naive
  1386. implementation of \key{flatten}.
  1387. \begin{lstlisting}
  1388. (program (tmp.1 a tmp.2 b)
  1389. (assign tmp.1 42)
  1390. (assign a tmp.1)
  1391. (assign tmp.2 a)
  1392. (assign b tmp.2)
  1393. (return b))
  1394. \end{lstlisting}
  1395. \begin{exercise}
  1396. \normalfont
  1397. Implement the \key{flatten} pass and test it on all of the example
  1398. programs that you created to test the \key{uniquify} pass and create
  1399. three new example programs that are designed to exercise all of the
  1400. interesting code in the \key{flatten} pass. Use the \key{interp-tests}
  1401. function (Appendix~\ref{appendix:utilities}) from \key{utilities.rkt} to
  1402. test your passes on the example programs.
  1403. \end{exercise}
  1404. \section{Select Instructions}
  1405. \label{sec:select-s0}
  1406. In the \key{select-instructions} pass we begin the work of translating
  1407. from $C_0$ to x86. The target language of this pass is a pseudo-x86
  1408. language that still uses variables, so we add an AST node of the form
  1409. $\VAR{\itm{var}}$ to the x86 abstract syntax. Also, the \key{program}
  1410. form should still list the variables (similar to $C_0$):
  1411. \[
  1412. (\key{program}\;(\Var^{*})\;\Instr^{+})
  1413. \]
  1414. The \key{select-instructions} pass deals with the differing format of
  1415. arithmetic operations. For example, in $C_0$ an addition operation can
  1416. take the form below. To translate to x86, we need to use the
  1417. \key{addq} instruction which does an in-place update. So we must first
  1418. move \code{10} to \code{x}. \\
  1419. \begin{tabular}{lll}
  1420. \begin{minipage}{0.4\textwidth}
  1421. \begin{lstlisting}
  1422. (assign x (+ 10 32))
  1423. \end{lstlisting}
  1424. \end{minipage}
  1425. &
  1426. $\Rightarrow$
  1427. &
  1428. \begin{minipage}{0.4\textwidth}
  1429. \begin{lstlisting}
  1430. (movq (int 10) (var x))
  1431. (addq (int 32) (var x))
  1432. \end{lstlisting}
  1433. \end{minipage}
  1434. \end{tabular} \\
  1435. There are some cases that require special care to avoid generating
  1436. needlessly complicated code. If one of the arguments is the same as
  1437. the left-hand side of the assignment, then there is no need for the
  1438. extra move instruction. For example, the following assignment
  1439. statement can be translated into a single \key{addq} instruction.\\
  1440. \begin{tabular}{lll}
  1441. \begin{minipage}{0.4\textwidth}
  1442. \begin{lstlisting}
  1443. (assign x (+ 10 x))
  1444. \end{lstlisting}
  1445. \end{minipage}
  1446. &
  1447. $\Rightarrow$
  1448. &
  1449. \begin{minipage}{0.4\textwidth}
  1450. \begin{lstlisting}
  1451. (addq (int 10) (var x))
  1452. \end{lstlisting}
  1453. \end{minipage}
  1454. \end{tabular} \\
  1455. The \key{read} operation does not have a direct counterpart in x86
  1456. assembly, so we have instead implemented this functionality in the C
  1457. language, with the function \code{read\_int} in the file
  1458. \code{runtime.c}. In general, we refer to all of the functionality in
  1459. this file as the \emph{runtime system}, or simply the \emph{runtime}
  1460. for short. When compiling your generated x86 assembly code, you
  1461. will need to compile \code{runtime.c} to \code{runtime.o} (an ``object
  1462. file'', using \code{gcc} option \code{-c}) and link it into the final
  1463. executable. For our purposes of code generation, all you need to do is
  1464. translate an assignment of \key{read} to some variable $\itm{lhs}$
  1465. (for left-hand side) into a call to the \code{read\_int} function
  1466. followed by a move from \code{rax} to the left-hand side. The move
  1467. from \code{rax} is needed because the return value from
  1468. \code{read\_int} goes into \code{rax}, as is the case in general. \\
  1469. \begin{tabular}{lll}
  1470. \begin{minipage}{0.4\textwidth}
  1471. \begin{lstlisting}
  1472. (assign |$\itm{lhs}$| (read))
  1473. \end{lstlisting}
  1474. \end{minipage}
  1475. &
  1476. $\Rightarrow$
  1477. &
  1478. \begin{minipage}{0.4\textwidth}
  1479. \begin{lstlisting}
  1480. (callq read_int)
  1481. (movq (reg rax) (var |$\itm{lhs}$|))
  1482. \end{lstlisting}
  1483. \end{minipage}
  1484. \end{tabular} \\
  1485. Regarding the \RETURN{\Arg} statement of $C_0$, we recommend treating it
  1486. as an assignment to the \key{rax} register and let the procedure
  1487. conclusion handle the transfer of control back to the calling
  1488. procedure.
  1489. \begin{exercise}
  1490. \normalfont
  1491. Implement the \key{select-instructions} pass and test it on all of the
  1492. example programs that you created for the previous passes and create
  1493. three new example programs that are designed to exercise all of the
  1494. interesting code in this pass. Use the \key{interp-tests} function
  1495. (Appendix~\ref{appendix:utilities}) from \key{utilities.rkt} to test
  1496. your passes on the example programs.
  1497. \end{exercise}
  1498. \section{Assign Homes}
  1499. \label{sec:assign-s0}
  1500. As discussed in Section~\ref{sec:plan-s0-x86}, the
  1501. \key{assign-homes} pass places all of the variables on the stack.
  1502. Consider again the example $R_1$ program \code{(+ 52 (- 10))},
  1503. which after \key{select-instructions} looks like the following.
  1504. \begin{lstlisting}
  1505. (movq (int 10) (var x))
  1506. (negq (var x))
  1507. (movq (int 52) (reg rax))
  1508. (addq (var x) (reg rax))
  1509. \end{lstlisting}
  1510. The one and only variable \code{x} is assigned to stack location
  1511. \code{-8(\%rbp)}, so the \code{assign-homes} pass translates the
  1512. above to
  1513. \begin{lstlisting}
  1514. (movq (int 10) (stack -8))
  1515. (negq (stack -8))
  1516. (movq (int 52) (reg rax))
  1517. (addq (stack -8) (reg rax))
  1518. \end{lstlisting}
  1519. In the process of assigning stack locations to variables, it is
  1520. convenient to compute and store the size of the frame in the first
  1521. field of the \key{program} node which will be needed later to generate
  1522. the procedure conclusion.
  1523. \[
  1524. (\key{program}\;\Int\;\Instr^{+})
  1525. \]
  1526. Some operating systems place restrictions on
  1527. the frame size. For example, Mac OS X requires the frame size to be a
  1528. multiple of 16 bytes.
  1529. \begin{exercise}
  1530. \normalfont Implement the \key{assign-homes} pass and test it on all
  1531. of the example programs that you created for the previous passes pass.
  1532. I recommend that \key{assign-homes} take an extra parameter that is a
  1533. mapping of variable names to homes (stack locations for now). Use the
  1534. \key{interp-tests} function (Appendix~\ref{appendix:utilities}) from
  1535. \key{utilities.rkt} to test your passes on the example programs.
  1536. \end{exercise}
  1537. \section{Patch Instructions}
  1538. \label{sec:patch-s0}
  1539. The purpose of this pass is to make sure that each instruction adheres
  1540. to the restrictions regarding which arguments can be memory
  1541. references. For most instructions, the rule is that at most one
  1542. argument may be a memory reference.
  1543. Consider again the following example.
  1544. \begin{lstlisting}
  1545. (let ([a 42])
  1546. (let ([b a])
  1547. b))
  1548. \end{lstlisting}
  1549. After \key{assign-homes} pass, the above has been translated to
  1550. \begin{lstlisting}
  1551. (movq (int 42) (stack -8))
  1552. (movq (stack -8) (stack -16))
  1553. (movq (stack -16) (reg rax))
  1554. \end{lstlisting}
  1555. The second \key{movq} instruction is problematic because both arguments
  1556. are stack locations. We suggest fixing this problem by moving from the
  1557. source to \key{rax} and then from \key{rax} to the destination, as
  1558. follows.
  1559. \begin{lstlisting}
  1560. (movq (int 42) (stack -8))
  1561. (movq (stack -8) (reg rax))
  1562. (movq (reg rax) (stack -16))
  1563. (movq (stack -16) (reg rax))
  1564. \end{lstlisting}
  1565. \begin{exercise}
  1566. \normalfont
  1567. Implement the \key{patch-instructions} pass and test it on all of the
  1568. example programs that you created for the previous passes and create
  1569. three new example programs that are designed to exercise all of the
  1570. interesting code in this pass. Use the \key{interp-tests} function
  1571. (Appendix~\ref{appendix:utilities}) from \key{utilities.rkt} to test
  1572. your passes on the example programs.
  1573. \end{exercise}
  1574. \section{Print x86}
  1575. \label{sec:print-x86}
  1576. The last step of the compiler from $R_1$ to x86 is to convert the
  1577. x86 AST (defined in Figure~\ref{fig:x86-ast-a}) to the string
  1578. representation (defined in Figure~\ref{fig:x86-a}). The Racket
  1579. \key{format} and \key{string-append} functions are useful in this
  1580. regard. The main work that this step needs to perform is to create the
  1581. \key{main} function and the standard instructions for its prelude
  1582. and conclusion, as shown in Figure~\ref{fig:p1-x86} of
  1583. Section~\ref{sec:x86}. You need to know the number of
  1584. stack-allocated variables, for which it is suggest that you compute in
  1585. the \key{assign-homes} pass (Section~\ref{sec:assign-s0}) and store in
  1586. the $\itm{info}$ field of the \key{program} node.
  1587. Your compiled code should print the result of the program's execution by using the
  1588. \code{print\_int} function provided in \code{runtime.c}. If your compiler has been implemented correctly so far, this final result should be stored in the \key{rax} register.
  1589. We'll talk more about
  1590. how to perform function calls with arguments in general later on, but
  1591. for now, make sure that your x86 printer includes the following code as part of the conclusion:
  1592. \begin{lstlisting}
  1593. movq %rax, %rdi
  1594. callq print_int
  1595. \end{lstlisting}
  1596. These lines move the value in \key{rax} into the \key{rdi} register, which
  1597. stores the first argument to be passed into \key{print\_int}.
  1598. If you want your program to run on Mac OS X, your code needs to
  1599. determine whether or not it is running on a Mac, and prefix
  1600. underscores to labels like \key{main}. You can determine the platform
  1601. with the Racket call \code{(system-type 'os)}, which returns
  1602. \code{'macosx}, \code{'unix}, or \code{'windows}. In addition to
  1603. placing underscores on \key{main}, you need to put them in front of
  1604. \key{callq} labels (so \code{callq print\_int} becomes \code{callq
  1605. \_print\_int}).
  1606. \begin{exercise}
  1607. \normalfont Implement the \key{print-x86} pass and test it on all of
  1608. the example programs that you created for the previous passes. Use the
  1609. \key{compiler-tests} function (Appendix~\ref{appendix:utilities}) from
  1610. \key{utilities.rkt} to test your complete compiler on the example
  1611. programs.
  1612. % The following is specific to P423/P523. -Jeremy
  1613. %Mac support is optional, but your compiler has to output
  1614. %valid code for Unix machines.
  1615. \end{exercise}
  1616. \begin{figure}[p]
  1617. \begin{tikzpicture}[baseline=(current bounding box.center)]
  1618. \node (R1) at (0,2) {\large $R_1$};
  1619. \node (R1-2) at (3,2) {\large $R_1$};
  1620. \node (C0-1) at (3,0) {\large $C_0$};
  1621. \node (x86-2) at (3,-2) {\large $\text{x86}^{*}$};
  1622. \node (x86-3) at (6,-2) {\large $\text{x86}^{*}$};
  1623. \node (x86-4) at (9,-2) {\large $\text{x86}$};
  1624. \node (x86-5) at (12,-2) {\large $\text{x86}^{\dagger}$};
  1625. \path[->,bend left=15] (R1) edge [above] node {\ttfamily\footnotesize uniquify} (R1-2);
  1626. \path[->,bend left=15] (R1-2) edge [right] node {\ttfamily\footnotesize flatten} (C0-1);
  1627. \path[->,bend right=15] (C0-1) edge [left] node {\ttfamily\footnotesize select-instr.} (x86-2);
  1628. \path[->,bend left=15] (x86-2) edge [above] node {\ttfamily\footnotesize assign-homes} (x86-3);
  1629. \path[->,bend left=15] (x86-3) edge [above] node {\ttfamily\footnotesize patch-instr.} (x86-4);
  1630. \path[->,bend left=15] (x86-4) edge [above] node {\ttfamily\footnotesize print-x86} (x86-5);
  1631. \end{tikzpicture}
  1632. \caption{Overview of the passes for compiling $R_1$. The x86$^{*}$
  1633. language extends x86 with variables and looser rules regarding
  1634. instruction arguments. The x86$^{\dagger}$ language is the concrete
  1635. syntax (string) for x86.}
  1636. \label{fig:R1-passes}
  1637. \end{figure}
  1638. Figure~\ref{fig:R1-passes} provides an overview of all the compiler
  1639. passes described in this Chapter.
  1640. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1641. \chapter{Register Allocation}
  1642. \label{ch:register-allocation}
  1643. In Chapter~\ref{ch:int-exp} we simplified the generation of x86
  1644. assembly by placing all variables on the stack. We can improve the
  1645. performance of the generated code considerably if we instead try to
  1646. place as many variables as possible into registers. The CPU can
  1647. access a register in a single cycle, whereas accessing the stack can
  1648. take from several cycles (to go to cache) to hundreds of cycles (to go
  1649. to main memory). Figure~\ref{fig:reg-eg} shows a program with four
  1650. variables that serves as a running example. We show the source program
  1651. and also the output of instruction selection. At that point the
  1652. program is almost x86 assembly but not quite; it still contains
  1653. variables instead of stack locations or registers.
  1654. \begin{figure}
  1655. \begin{minipage}{0.45\textwidth}
  1656. Source program:
  1657. \begin{lstlisting}
  1658. (program
  1659. (let ([v 1])
  1660. (let ([w 46])
  1661. (let ([x (+ v 7)])
  1662. (let ([y (+ 4 x)])
  1663. (let ([z (+ x w)])
  1664. (+ z (- y))))))))
  1665. \end{lstlisting}
  1666. \end{minipage}
  1667. \begin{minipage}{0.45\textwidth}
  1668. After instruction selection:
  1669. \begin{lstlisting}
  1670. (program (v w x y z t.1 t.2)
  1671. (movq (int 1) (var v))
  1672. (movq (int 46) (var w))
  1673. (movq (var v) (var x))
  1674. (addq (int 7) (var x))
  1675. (movq (var x) (var y))
  1676. (addq (int 4) (var y))
  1677. (movq (var x) (var z))
  1678. (addq (var w) (var z))
  1679. (movq (var y) (var t.1))
  1680. (negq (var t.1))
  1681. (movq (var z) (var t.2))
  1682. (addq (var t.1) (var t.2))
  1683. (movq (var t.2) (reg rax)))
  1684. \end{lstlisting}
  1685. \end{minipage}
  1686. \caption{Running example for this chapter.}
  1687. \label{fig:reg-eg}
  1688. \end{figure}
  1689. The goal of register allocation is to fit as many variables into
  1690. registers as possible. It is often the case that we have more
  1691. variables than registers, so we cannot naively map each variable to a
  1692. register. Fortunately, it is also common for different variables to be
  1693. needed during different periods of time, and in such cases the
  1694. variables can be mapped to the same register. Consider variables
  1695. \code{x} and \code{y} in Figure~\ref{fig:reg-eg}. After the variable
  1696. \code{x} is moved to \code{z} it is no longer needed. Variable
  1697. \code{y}, on the other hand, is used only after this point, so
  1698. \code{x} and \code{y} could share the same register. The topic of the
  1699. next section is how we compute where a variable is needed.
  1700. \section{Liveness Analysis}
  1701. \label{sec:liveness-analysis}
  1702. A variable is \emph{live} if the variable is used at some later point
  1703. in the program and there is not an intervening assignment to the
  1704. variable.
  1705. %
  1706. To understand the latter condition, consider the following code
  1707. fragment in which there are two writes to \code{b}. Are \code{a} and
  1708. \code{b} both live at the same time?
  1709. \begin{lstlisting}[numbers=left,numberstyle=\tiny]
  1710. (movq (int 5) (var a))
  1711. (movq (int 30) (var b))
  1712. (movq (var a) (var c))
  1713. (movq (int 10) (var b))
  1714. (addq (var b) (var c))
  1715. \end{lstlisting}
  1716. The answer is no because the value \code{30} written to \code{b} on
  1717. line 2 is never used. The variable \code{b} is read on line 5 and
  1718. there is an intervening write to \code{b} on line 4, so the read on
  1719. line 5 receives the value written on line 4, not line 2.
  1720. The live variables can be computed by traversing the instruction
  1721. sequence back to front (i.e., backwards in execution order). Let
  1722. $I_1,\ldots, I_n$ be the instruction sequence. We write
  1723. $L_{\mathsf{after}}(k)$ for the set of live variables after
  1724. instruction $I_k$ and $L_{\mathsf{before}}(k)$ for the set of live
  1725. variables before instruction $I_k$. The live variables after an
  1726. instruction are always the same as the live variables before the next
  1727. instruction.
  1728. \begin{equation*}
  1729. L_{\mathsf{after}}(k) = L_{\mathsf{before}}(k+1)
  1730. \end{equation*}
  1731. To start things off, there are no live variables after the last
  1732. instruction, so
  1733. \begin{equation*}
  1734. L_{\mathsf{after}}(n) = \emptyset
  1735. \end{equation*}
  1736. We then apply the following rule repeatedly, traversing the
  1737. instruction sequence back to front.
  1738. \begin{equation*}
  1739. L_{\mathtt{before}}(k) = (L_{\mathtt{after}}(k) - W(k)) \cup R(k),
  1740. \end{equation*}
  1741. where $W(k)$ are the variables written to by instruction $I_k$ and
  1742. $R(k)$ are the variables read by instruction $I_k$.
  1743. Figure~\ref{fig:live-eg} shows the results of live variables analysis
  1744. for the running example, with each instruction aligned with its
  1745. $L_{\mathtt{after}}$ set to make the figure easy to read.
  1746. \begin{figure}[tbp]
  1747. \hspace{20pt}
  1748. \begin{minipage}{0.45\textwidth}
  1749. \begin{lstlisting}[numbers=left]
  1750. (program (v w x y z t.1 t.2)
  1751. (movq (int 1) (var v))
  1752. (movq (int 46) (var w))
  1753. (movq (var v) (var x))
  1754. (addq (int 7) (var x))
  1755. (movq (var x) (var y))
  1756. (addq (int 4) (var y))
  1757. (movq (var x) (var z))
  1758. (addq (var w) (var z))
  1759. (movq (var y) (var t.1))
  1760. (negq (var t.1))
  1761. (movq (var z) (var t.2))
  1762. (addq (var t.1) (var t.2))
  1763. (movq (var t.2) (reg rax)))
  1764. \end{lstlisting}
  1765. \end{minipage}
  1766. \vrule\hspace{10pt}
  1767. \begin{minipage}{0.45\textwidth}
  1768. \begin{lstlisting}
  1769. |$\{ v \}$|
  1770. |$\{ v, w \}$|
  1771. |$\{ w, x \}$|
  1772. |$\{ w, x \}$|
  1773. |$\{ w, x, y\}$|
  1774. |$\{ w, x, y \}$|
  1775. |$\{ w, y, z \}$|
  1776. |$\{ y, z \}$|
  1777. |$\{ t.1, z \}$|
  1778. |$\{ t.1, z \}$|
  1779. |$\{t.1,t.2\}$|
  1780. |$\{t.2\}$|
  1781. |$\{\}$|
  1782. \end{lstlisting}
  1783. \end{minipage}
  1784. \caption{The running example and its live-after sets.}
  1785. \label{fig:live-eg}
  1786. \end{figure}
  1787. \begin{exercise}\normalfont
  1788. Implement the compiler pass named \code{uncover-live} that computes
  1789. the live-after sets. We recommend storing the live-after sets (a list
  1790. of lists of variables) in the $\itm{info}$ field of the \key{program}
  1791. node alongside the list of variables as follows.
  1792. \begin{lstlisting}
  1793. (program (|$\Var^{*}$| |$\itm{live{-}afters}$|) |$\Instr^{+}$|)
  1794. \end{lstlisting}
  1795. I recommend organizing your code to use a helper function that takes a
  1796. list of statements and an initial live-after set (typically empty) and
  1797. returns the list of statements and the list of live-after sets. For
  1798. this chapter, returning the list of statements is unnecessary, as they
  1799. will be unchanged, but in Chapter~\ref{ch:bool-types} we introduce
  1800. \key{if} statements and will need to annotate them with the live-after
  1801. sets of the two branches.
  1802. I recommend creating helper functions to 1) compute the set of
  1803. variables that appear in an argument (of an instruction), 2) compute
  1804. the variables read by an instruction which corresponds to the $R$
  1805. function discussed above, and 3) the variables written by an
  1806. instruction which corresponds to $W$.
  1807. \end{exercise}
  1808. \section{Building the Interference Graph}
  1809. Based on the liveness analysis, we know where each variable is needed.
  1810. However, during register allocation, we need to answer questions of
  1811. the specific form: are variables $u$ and $v$ live at the same time?
  1812. (And therefore cannot be assigned to the same register.) To make this
  1813. question easier to answer, we create an explicit data structure, an
  1814. \emph{interference graph}. An interference graph is an undirected
  1815. graph that has an edge between two variables if they are live at the
  1816. same time, that is, if they interfere with each other.
  1817. The most obvious way to compute the interference graph is to look at
  1818. the set of live variables between each statement in the program, and
  1819. add an edge to the graph for every pair of variables in the same set.
  1820. This approach is less than ideal for two reasons. First, it can be
  1821. rather expensive because it takes $O(n^2)$ time to look at every pair
  1822. in a set of $n$ live variables. Second, there is a special case in
  1823. which two variables that are live at the same time do not actually
  1824. interfere with each other: when they both contain the same value
  1825. because we have assigned one to the other.
  1826. A better way to compute the interference graph is given by the
  1827. following.
  1828. \begin{itemize}
  1829. \item If instruction $I_k$ is a move: (\key{movq} $s$\, $d$), then add
  1830. the edge $(d,v)$ for every $v \in L_{\mathsf{after}}(k)$ unless $v =
  1831. d$ or $v = s$.
  1832. \item If instruction $I_k$ is not a move but some other arithmetic
  1833. instruction such as (\key{addq} $s$\, $d$), then add the edge $(d,v)$
  1834. for every $v \in L_{\mathsf{after}}(k)$ unless $v = d$.
  1835. \item If instruction $I_k$ is of the form (\key{callq}
  1836. $\mathit{label}$), then add an edge $(r,v)$ for every caller-save
  1837. register $r$ and every variable $v \in L_{\mathsf{after}}(k)$.
  1838. \end{itemize}
  1839. Working from the top to bottom of Figure~\ref{fig:live-eg}, we obtain
  1840. the following interference for the instruction at the specified line
  1841. number.
  1842. \begin{quote}
  1843. Line 2: no interference,\\
  1844. Line 3: $w$ interferes with $v$,\\
  1845. Line 4: $x$ interferes with $w$,\\
  1846. Line 5: $x$ interferes with $w$,\\
  1847. Line 6: $y$ interferes with $w$,\\
  1848. Line 7: $y$ interferes with $w$ and $x$,\\
  1849. Line 8: $z$ interferes with $w$ and $y$,\\
  1850. Line 9: $z$ interferes with $y$, \\
  1851. Line 10: $t.1$ interferes with $z$, \\
  1852. Line 11: $t.1$ interferes with $z$, \\
  1853. Line 12: $t.2$ interferes with $t.1$, \\
  1854. Line 13: no interference. \\
  1855. Line 14: no interference.
  1856. \end{quote}
  1857. The resulting interference graph is shown in
  1858. Figure~\ref{fig:interfere}.
  1859. \begin{figure}[tbp]
  1860. \large
  1861. \[
  1862. \begin{tikzpicture}[baseline=(current bounding box.center)]
  1863. \node (v) at (0,0) {$v$};
  1864. \node (w) at (2,0) {$w$};
  1865. \node (x) at (4,0) {$x$};
  1866. \node (t1) at (6,0) {$t.1$};
  1867. \node (y) at (2,-2) {$y$};
  1868. \node (z) at (4,-2) {$z$};
  1869. \node (t2) at (6,-2) {$t.2$};
  1870. \draw (v) to (w);
  1871. \foreach \i in {w,x,y}
  1872. {
  1873. \foreach \j in {w,x,y}
  1874. {
  1875. \draw (\i) to (\j);
  1876. }
  1877. }
  1878. \draw (z) to (w);
  1879. \draw (z) to (y);
  1880. \draw (t1) to (z);
  1881. \draw (t2) to (t1);
  1882. \end{tikzpicture}
  1883. \]
  1884. \caption{Interference graph for the running example.}
  1885. \label{fig:interfere}
  1886. \end{figure}
  1887. Our next concern is to choose a data structure for representing the
  1888. interference graph. There are many standard choices for how to
  1889. represent a graph: \emph{adjacency matrix}, \emph{adjacency list}, and
  1890. \emph{edge set}~\citep{Cormen:2001uq}. The right way to choose a data
  1891. structure is to study the algorithm that uses the data structure,
  1892. determine what operations need to be performed, and then choose the
  1893. data structure that provide the most efficient implementations of
  1894. those operations. Often times the choice of data structure can have an
  1895. affect on the time complexity of the algorithm, as it does here. If
  1896. you skim the next section, you will see that the register allocation
  1897. algorithm needs to ask the graph for all of its vertices and, given a
  1898. vertex, it needs to known all of the adjacent vertices. Thus, the
  1899. correct choice of graph representation is that of an adjacency
  1900. list. There are helper functions in \code{utilities.rkt} for
  1901. representing graphs using the adjacency list representation:
  1902. \code{make-graph}, \code{add-edge}, and \code{adjacent}
  1903. (Appendix~\ref{appendix:utilities}). In particular, those functions
  1904. use a hash table to map each vertex to the set of adjacent vertices,
  1905. and the sets are represented using Racket's \key{set}, which is also a
  1906. hash table.
  1907. \begin{exercise}\normalfont
  1908. Implement the compiler pass named \code{build-interference} according
  1909. to the algorithm suggested above. The output of this pass should
  1910. replace the live-after sets with the interference $\itm{graph}$ as
  1911. follows.
  1912. \begin{lstlisting}
  1913. (program (|$\Var^{*}$| |$\itm{graph}$|) |$\Instr^{+}$|)
  1914. \end{lstlisting}
  1915. \end{exercise}
  1916. \section{Graph Coloring via Sudoku}
  1917. We now come to the main event, mapping variables to registers (or to
  1918. stack locations in the event that we run out of registers). We need
  1919. to make sure not to map two variables to the same register if the two
  1920. variables interfere with each other. In terms of the interference
  1921. graph, this means we cannot map adjacent nodes to the same register.
  1922. If we think of registers as colors, the register allocation problem
  1923. becomes the widely-studied graph coloring
  1924. problem~\citep{Balakrishnan:1996ve,Rosen:2002bh}.
  1925. The reader may be more familiar with the graph coloring problem then he
  1926. or she realizes; the popular game of Sudoku is an instance of the
  1927. graph coloring problem. The following describes how to build a graph
  1928. out of an initial Sudoku board.
  1929. \begin{itemize}
  1930. \item There is one node in the graph for each Sudoku square.
  1931. \item There is an edge between two nodes if the corresponding squares
  1932. are in the same row, in the same column, or if the squares are in
  1933. the same $3\times 3$ region.
  1934. \item Choose nine colors to correspond to the numbers $1$ to $9$.
  1935. \item Based on the initial assignment of numbers to squares in the
  1936. Sudoku board, assign the corresponding colors to the corresponding
  1937. nodes in the graph.
  1938. \end{itemize}
  1939. If you can color the remaining nodes in the graph with the nine
  1940. colors, then you have also solved the corresponding game of Sudoku.
  1941. Figure~\ref{fig:sudoku-graph} shows an initial Sudoku game board and
  1942. the corresponding graph with colored vertices.
  1943. \begin{figure}[tbp]
  1944. \includegraphics[width=0.45\textwidth]{sudoku}
  1945. \includegraphics[width=0.5\textwidth]{sudoku-graph}
  1946. \caption{A Sudoku game board and the corresponding colored graph. We
  1947. map the Sudoku number 1 to blue, 2 to yellow, and 3 to red. We only
  1948. show edges for a sampling of the vertices (those that are colored)
  1949. because showing edges for all of the vertices would make the graph
  1950. unreadable.}
  1951. \label{fig:sudoku-graph}
  1952. \end{figure}
  1953. Given that Sudoku is graph coloring, one can use Sudoku strategies to
  1954. come up with an algorithm for allocating registers. For example, one
  1955. of the basic techniques for Sudoku is called Pencil Marks. The idea is
  1956. that you use a process of elimination to determine what numbers no
  1957. longer make sense for a square, and write down those numbers in the
  1958. square (writing very small). For example, if the number $1$ is
  1959. assigned to a square, then by process of elimination, you can write
  1960. the pencil mark $1$ in all the squares in the same row, column, and
  1961. region. Many Sudoku computer games provide automatic support for
  1962. Pencil Marks. This heuristic also reduces the degree of branching in
  1963. the search tree.
  1964. The Pencil Marks technique corresponds to the notion of color
  1965. \emph{saturation} due to \cite{Brelaz:1979eu}. The saturation of a
  1966. node, in Sudoku terms, is the set of colors that are no longer
  1967. available. In graph terminology, we have the following definition:
  1968. \begin{equation*}
  1969. \mathrm{saturation}(u) = \{ c \;|\; \exists v. v \in \mathrm{adjacent}(u)
  1970. \text{ and } \mathrm{color}(v) = c \}
  1971. \end{equation*}
  1972. where $\mathrm{adjacent}(u)$ is the set of nodes adjacent to $u$.
  1973. Using the Pencil Marks technique leads to a simple strategy for
  1974. filling in numbers: if there is a square with only one possible number
  1975. left, then write down that number! But what if there are no squares
  1976. with only one possibility left? One brute-force approach is to just
  1977. make a guess. If that guess ultimately leads to a solution, great. If
  1978. not, backtrack to the guess and make a different guess. Of course,
  1979. backtracking can be horribly time consuming. One standard way to
  1980. reduce the amount of backtracking is to use the most-constrained-first
  1981. heuristic. That is, when making a guess, always choose a square with
  1982. the fewest possibilities left (the node with the highest saturation).
  1983. The idea is that choosing highly constrained squares earlier rather
  1984. than later is better because later there may not be any possibilities.
  1985. In some sense, register allocation is easier than Sudoku because we
  1986. can always cheat and add more numbers by mapping variables to the
  1987. stack. We say that a variable is \emph{spilled} when we decide to map
  1988. it to a stack location. We would like to minimize the time needed to
  1989. color the graph, and backtracking is expensive. Thus, it makes sense
  1990. to keep the most-constrained-first heuristic but drop the backtracking
  1991. in favor of greedy search (guess and just keep going).
  1992. Figure~\ref{fig:satur-algo} gives the pseudo-code for this simple
  1993. greedy algorithm for register allocation based on saturation and the
  1994. most-constrained-first heuristic, which is roughly equivalent to the
  1995. DSATUR algorithm of \cite{Brelaz:1979eu} (also known as saturation
  1996. degree ordering~\citep{Gebremedhin:1999fk,Omari:2006uq}). Just
  1997. as in Sudoku, the algorithm represents colors with integers, with the
  1998. first $k$ colors corresponding to the $k$ registers in a given machine
  1999. and the rest of the integers corresponding to stack locations.
  2000. \begin{figure}[btp]
  2001. \centering
  2002. \begin{lstlisting}[basicstyle=\rmfamily,deletekeywords={for,from,with,is,not,in,find},morekeywords={while},columns=fullflexible]
  2003. Algorithm: DSATUR
  2004. Input: a graph |$G$|
  2005. Output: an assignment |$\mathrm{color}[v]$| for each node |$v \in G$|
  2006. |$W \gets \mathit{vertices}(G)$|
  2007. while |$W \neq \emptyset$| do
  2008. pick a node |$u$| from |$W$| with the highest saturation,
  2009. breaking ties randomly
  2010. find the lowest color |$c$| that is not in |$\{ \mathrm{color}[v] \;:\; v \in \mathrm{adjacent}(v)\}$|
  2011. |$\mathrm{color}[u] \gets c$|
  2012. |$W \gets W - \{u\}$|
  2013. \end{lstlisting}
  2014. \caption{Saturation-based greedy graph coloring algorithm.}
  2015. \label{fig:satur-algo}
  2016. \end{figure}
  2017. With this algorithm in hand, let us return to the running example and
  2018. consider how to color the interference graph in
  2019. Figure~\ref{fig:interfere}. We shall not use register \key{rax} for
  2020. register allocation because we use it to patch instructions, so we
  2021. remove that vertex from the graph. Initially, all of the nodes are
  2022. not yet colored and they are unsaturated, so we annotate each of them
  2023. with a dash for their color and an empty set for the saturation.
  2024. \[
  2025. \begin{tikzpicture}[baseline=(current bounding box.center)]
  2026. \node (v) at (0,0) {$v:-,\{\}$};
  2027. \node (w) at (3,0) {$w:-,\{\}$};
  2028. \node (x) at (6,0) {$x:-,\{\}$};
  2029. \node (y) at (3,-1.5) {$y:-,\{\}$};
  2030. \node (z) at (6,-1.5) {$z:-,\{\}$};
  2031. \node (t1) at (9,0) {$t.1:-,\{\}$};
  2032. \node (t2) at (9,-1.5) {$t.2:-,\{\}$};
  2033. \draw (v) to (w);
  2034. \foreach \i in {w,x,y}
  2035. {
  2036. \foreach \j in {w,x,y}
  2037. {
  2038. \draw (\i) to (\j);
  2039. }
  2040. }
  2041. \draw (z) to (w);
  2042. \draw (z) to (y);
  2043. \draw (t1) to (z);
  2044. \draw (t2) to (t1);
  2045. \end{tikzpicture}
  2046. \]
  2047. We select a maximally saturated node and color it $0$. In this case we
  2048. have a 7-way tie, so we arbitrarily pick $y$. The then mark color $0$
  2049. as no longer available for $w$, $x$, and $z$ because they interfere
  2050. with $y$.
  2051. \[
  2052. \begin{tikzpicture}[baseline=(current bounding box.center)]
  2053. \node (v) at (0,0) {$v:-,\{\}$};
  2054. \node (w) at (3,0) {$w:-,\{0\}$};
  2055. \node (x) at (6,0) {$x:-,\{0\}$};
  2056. \node (y) at (3,-1.5) {$y:0,\{\}$};
  2057. \node (z) at (6,-1.5) {$z:-,\{0\}$};
  2058. \node (t1) at (9,0) {$t.1:-,\{\}$};
  2059. \node (t2) at (9,-1.5) {$t.2:-,\{\}$};
  2060. \draw (v) to (w);
  2061. \foreach \i in {w,x,y}
  2062. {
  2063. \foreach \j in {w,x,y}
  2064. {
  2065. \draw (\i) to (\j);
  2066. }
  2067. }
  2068. \draw (z) to (w);
  2069. \draw (z) to (y);
  2070. \draw (t1) to (z);
  2071. \draw (t2) to (t1);
  2072. \end{tikzpicture}
  2073. \]
  2074. Now we repeat the process, selecting another maximally saturated node.
  2075. This time there is a three-way tie between $w$, $x$, and $z$. We color
  2076. $w$ with $1$.
  2077. \[
  2078. \begin{tikzpicture}[baseline=(current bounding box.center)]
  2079. \node (v) at (0,0) {$v:-,\{1\}$};
  2080. \node (w) at (3,0) {$w:1,\{0\}$};
  2081. \node (x) at (6,0) {$x:-,\{0,1\}$};
  2082. \node (y) at (3,-1.5) {$y:0,\{1\}$};
  2083. \node (z) at (6,-1.5) {$z:-,\{0,1\}$};
  2084. \node (t1) at (9,0) {$t.1:-,\{\}$};
  2085. \node (t2) at (9,-1.5) {$t.2:-,\{\}$};
  2086. \draw (t1) to (z);
  2087. \draw (t2) to (t1);
  2088. \draw (v) to (w);
  2089. \foreach \i in {w,x,y}
  2090. {
  2091. \foreach \j in {w,x,y}
  2092. {
  2093. \draw (\i) to (\j);
  2094. }
  2095. }
  2096. \draw (z) to (w);
  2097. \draw (z) to (y);
  2098. \end{tikzpicture}
  2099. \]
  2100. The most saturated nodes are now $x$ and $z$. We color $x$ with the
  2101. next available color which is $2$.
  2102. \[
  2103. \begin{tikzpicture}[baseline=(current bounding box.center)]
  2104. \node (v) at (0,0) {$v:-,\{1\}$};
  2105. \node (w) at (3,0) {$w:1,\{0,2\}$};
  2106. \node (x) at (6,0) {$x:2,\{0,1\}$};
  2107. \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
  2108. \node (z) at (6,-1.5) {$z:-,\{0,1\}$};
  2109. \node (t1) at (9,0) {$t.1:-,\{\}$};
  2110. \node (t2) at (9,-1.5) {$t.2:-,\{\}$};
  2111. \draw (t1) to (z);
  2112. \draw (t2) to (t1);
  2113. \draw (v) to (w);
  2114. \foreach \i in {w,x,y}
  2115. {
  2116. \foreach \j in {w,x,y}
  2117. {
  2118. \draw (\i) to (\j);
  2119. }
  2120. }
  2121. \draw (z) to (w);
  2122. \draw (z) to (y);
  2123. \end{tikzpicture}
  2124. \]
  2125. Node $z$ is the next most highly saturated, so we color $z$ with $2$.
  2126. \[
  2127. \begin{tikzpicture}[baseline=(current bounding box.center)]
  2128. \node (v) at (0,0) {$v:-,\{1\}$};
  2129. \node (w) at (3,0) {$w:1,\{0,2\}$};
  2130. \node (x) at (6,0) {$x:2,\{0,1\}$};
  2131. \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
  2132. \node (z) at (6,-1.5) {$z:2,\{0,1\}$};
  2133. \node (t1) at (9,0) {$t.1:-,\{2\}$};
  2134. \node (t2) at (9,-1.5) {$t.2:-,\{\}$};
  2135. \draw (t1) to (z);
  2136. \draw (t2) to (t1);
  2137. \draw (v) to (w);
  2138. \foreach \i in {w,x,y}
  2139. {
  2140. \foreach \j in {w,x,y}
  2141. {
  2142. \draw (\i) to (\j);
  2143. }
  2144. }
  2145. \draw (z) to (w);
  2146. \draw (z) to (y);
  2147. \end{tikzpicture}
  2148. \]
  2149. We have a 2-way tie between $v$ and $t.1$. We choose to color $v$ with
  2150. $0$.
  2151. \[
  2152. \begin{tikzpicture}[baseline=(current bounding box.center)]
  2153. \node (v) at (0,0) {$v:0,\{1\}$};
  2154. \node (w) at (3,0) {$w:1,\{0,2\}$};
  2155. \node (x) at (6,0) {$x:2,\{0,1\}$};
  2156. \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
  2157. \node (z) at (6,-1.5) {$z:2,\{0,1\}$};
  2158. \node (t1) at (9,0) {$t.1:-,\{2\}$};
  2159. \node (t2) at (9,-1.5) {$t.2:-,\{\}$};
  2160. \draw (t1) to (z);
  2161. \draw (t2) to (t1);
  2162. \draw (v) to (w);
  2163. \foreach \i in {w,x,y}
  2164. {
  2165. \foreach \j in {w,x,y}
  2166. {
  2167. \draw (\i) to (\j);
  2168. }
  2169. }
  2170. \draw (z) to (w);
  2171. \draw (z) to (y);
  2172. \end{tikzpicture}
  2173. \]
  2174. In the last two steps of the algorithm, we color $t.1$ with $0$
  2175. then $t.2$ with $1$.
  2176. \[
  2177. \begin{tikzpicture}[baseline=(current bounding box.center)]
  2178. \node (v) at (0,0) {$v:0,\{1\}$};
  2179. \node (w) at (3,0) {$w:1,\{0,2\}$};
  2180. \node (x) at (6,0) {$x:2,\{0,1\}$};
  2181. \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
  2182. \node (z) at (6,-1.5) {$z:2,\{0,1\}$};
  2183. \node (t1) at (9,0) {$t.1:0,\{2,1\}$};
  2184. \node (t2) at (9,-1.5) {$t.2:1,\{0\}$};
  2185. \draw (t1) to (z);
  2186. \draw (t2) to (t1);
  2187. \draw (v) to (w);
  2188. \foreach \i in {w,x,y}
  2189. {
  2190. \foreach \j in {w,x,y}
  2191. {
  2192. \draw (\i) to (\j);
  2193. }
  2194. }
  2195. \draw (z) to (w);
  2196. \draw (z) to (y);
  2197. \end{tikzpicture}
  2198. \]
  2199. With the coloring complete, we can finalize the assignment of
  2200. variables to registers and stack locations. Recall that if we have $k$
  2201. registers, we map the first $k$ colors to registers and the rest to
  2202. stack locations. Suppose for the moment that we just have one extra
  2203. register to use for register allocation, just \key{rbx}. Then the
  2204. following is the mapping of colors to registers and stack allocations.
  2205. \[
  2206. \{ 0 \mapsto \key{\%rbx}, \; 1 \mapsto \key{-8(\%rbp)}, \; 2 \mapsto \key{-16(\%rbp)}, \ldots \}
  2207. \]
  2208. Putting this mapping together with the above coloring of the variables, we
  2209. arrive at the assignment:
  2210. \begin{gather*}
  2211. \{ v \mapsto \key{\%rbx}, \,
  2212. w \mapsto \key{-8(\%rbp)}, \,
  2213. x \mapsto \key{-16(\%rbp)}, \,
  2214. y \mapsto \key{\%rbx}, \,
  2215. z\mapsto \key{-16(\%rbp)}, \\
  2216. t.1\mapsto \key{\%rbx} ,\,
  2217. t.2\mapsto \key{-8(\%rbp)} \}
  2218. \end{gather*}
  2219. Applying this assignment to our running example
  2220. (Figure~\ref{fig:reg-eg}) yields the program on the right.
  2221. % why frame size of 32? -JGS
  2222. \begin{minipage}{0.45\textwidth}
  2223. \begin{lstlisting}
  2224. (program (v w x y z)
  2225. (movq (int 1) (var v))
  2226. (movq (int 46) (var w))
  2227. (movq (var v) (var x))
  2228. (addq (int 7) (var x))
  2229. (movq (var x) (var y))
  2230. (addq (int 4) (var y))
  2231. (movq (var x) (var z))
  2232. (addq (var w) (var z))
  2233. (movq (var y) (var t.1))
  2234. (negq (var t.1))
  2235. (movq (var z) (var t.2))
  2236. (addq (var t.1) (var t.2))
  2237. (movq (var t.2) (reg rax)))
  2238. \end{lstlisting}
  2239. \end{minipage}
  2240. $\Rightarrow$
  2241. \begin{minipage}{0.45\textwidth}
  2242. \begin{lstlisting}
  2243. (program 16
  2244. (movq (int 1) (reg rbx))
  2245. (movq (int 46) (stack -8))
  2246. (movq (reg rbx) (stack -16))
  2247. (addq (int 7) (stack -16))
  2248. (movq (stack 16) (reg rbx))
  2249. (addq (int 4) (reg rbx))
  2250. (movq (stack -16) (stack -16))
  2251. (addq (stack -8) (stack -16))
  2252. (movq (reg rbx) (reg rbx))
  2253. (negq (reg rbx))
  2254. (movq (stack -16) (stack -8))
  2255. (addq (reg rbx) (stack -8))
  2256. (movq (stack -8) (reg rax)))
  2257. \end{lstlisting}
  2258. \end{minipage}
  2259. The resulting program is almost an x86 program. The remaining step
  2260. is to apply the patch instructions pass. In this example, the trivial
  2261. move of \code{-16(\%rbp)} to itself is deleted and the addition of
  2262. \code{-8(\%rbp)} to \key{-16(\%rbp)} is fixed by going through
  2263. \code{rax}. The following shows the portion of the program that
  2264. changed.
  2265. \begin{lstlisting}
  2266. (addq (int 4) (reg rbx))
  2267. (movq (stack -8) (reg rax)
  2268. (addq (reg rax) (stack -16))
  2269. \end{lstlisting}
  2270. An overview of all of the passes involved in register allocation is
  2271. shown in Figure~\ref{fig:reg-alloc-passes}.
  2272. \begin{figure}[p]
  2273. \begin{tikzpicture}[baseline=(current bounding box.center)]
  2274. \node (R1) at (0,2) {\large $R_1$};
  2275. \node (R1-2) at (3,2) {\large $R_1$};
  2276. \node (C0-1) at (3,0) {\large $C_0$};
  2277. \node (x86-2) at (3,-2) {\large $\text{x86}^{*}$};
  2278. \node (x86-3) at (6,-2) {\large $\text{x86}^{*}$};
  2279. \node (x86-4) at (9,-2) {\large $\text{x86}$};
  2280. \node (x86-5) at (12,-2) {\large $\text{x86}^{\dagger}$};
  2281. \node (x86-2-1) at (3,-4) {\large $\text{x86}^{*}$};
  2282. \node (x86-2-2) at (6,-4) {\large $\text{x86}^{*}$};
  2283. \path[->,bend left=15] (R1) edge [above] node {\ttfamily\footnotesize uniquify} (R1-2);
  2284. \path[->,bend left=15] (R1-2) edge [right] node {\ttfamily\footnotesize flatten} (C0-1);
  2285. \path[->,bend right=15] (C0-1) edge [left] node {\ttfamily\footnotesize select-instr.} (x86-2);
  2286. \path[->,bend left=15] (x86-2) edge [right] node {\ttfamily\footnotesize uncover-live} (x86-2-1);
  2287. \path[->,bend right=15] (x86-2-1) edge [below] node {\ttfamily\footnotesize build-inter.} (x86-2-2);
  2288. \path[->,bend right=15] (x86-2-2) edge [right] node {\ttfamily\footnotesize allocate-reg.} (x86-3);
  2289. \path[->,bend left=15] (x86-3) edge [above] node {\ttfamily\footnotesize patch-instr.} (x86-4);
  2290. \path[->,bend left=15] (x86-4) edge [above] node {\ttfamily\footnotesize print-x86} (x86-5);
  2291. \end{tikzpicture}
  2292. \caption{Diagram of the passes for compiling $R_1$, including the
  2293. three new passes for register allocation.}
  2294. \label{fig:reg-alloc-passes}
  2295. \end{figure}
  2296. \begin{exercise}\normalfont
  2297. Implement the pass \code{allocate-registers} and test it by creating
  2298. new example programs that exercise all of the register allocation
  2299. algorithm, such as forcing variables to be spilled to the stack.
  2300. I recommend organizing our code by creating a helper function named
  2301. \code{allocate-homes} that takes an interference graph, a list of all
  2302. the variables in the program, and the list of statements. This
  2303. function should return a mapping of variables to their homes
  2304. (registers or stack locations) and the total size needed for the
  2305. stack. By creating this helper function, we will be able to reuse it
  2306. in Chapter~\ref{ch:functions} when we add support for functions.
  2307. Once you have obtained the mapping from \code{allocate-homes}, you can
  2308. use the \code{assign-homes} function from Section~\ref{sec:assign-s0}
  2309. to replace the variables with their homes.
  2310. \end{exercise}
  2311. \section{Print x86 and Conventions for Registers}
  2312. \label{sec:print-x86-reg-alloc}
  2313. Recall the the \code{print-x86} pass generates the prelude and
  2314. conclusion instructions for the \code{main} function. The prelude
  2315. saved the values in \code{rbp} and \code{rsp} and the conclusion
  2316. returned those values to \code{rbp} and \code{rsp}. The reason for
  2317. this is that there are agreed-upon conventions for how different
  2318. functions share the same fixed set of registers. There is a function
  2319. inside the operating system (OS) that calls our \code{main} function,
  2320. and that OS function uses the same registers that we use in
  2321. \code{main}. The convention for x86 is that the caller is responsible
  2322. for freeing up some registers, the \emph{caller save registers}, prior
  2323. to the function call, and the callee is responsible for saving and
  2324. restoring some other registers, the \emph{callee save registers},
  2325. before and after using them. The caller save registers are
  2326. \begin{lstlisting}
  2327. rax rdx rcx rsi rdi r8 r9 r10 r11
  2328. \end{lstlisting}
  2329. while the callee save registers are
  2330. \begin{lstlisting}
  2331. rsp rbp rbx r12 r13 r14 r15
  2332. \end{lstlisting}
  2333. Another way to think about this caller/callee convention is the
  2334. following. The caller should assume that all the caller save registers
  2335. get overwritten with arbitrary values by the callee. On the other
  2336. hand, the caller can safely assume that all the callee save registers
  2337. contain the same values after the call that they did before the call.
  2338. The callee can freely use any of the caller save registers. However,
  2339. if the callee wants to use a callee save register, the callee must
  2340. arrange to put the original value back in the register prior to
  2341. returning to the caller, which is usually accomplished by saving and
  2342. restoring the value from the stack.
  2343. The upshot of these conventions is that the \code{main} function needs
  2344. to save (in the prelude) and restore (in the conclusion) any callee
  2345. save registers that get used during register allocation. The simplest
  2346. approach is to save and restore all the callee save registers. The
  2347. more efficient approach is to keep track of which callee save
  2348. registers were used and only save and restore them. Either way, make
  2349. sure to take this use of stack space into account when you round up
  2350. the size of the frame to make sure it is a multiple of 16 bytes.
  2351. \section{Challenge: Move Biasing$^{*}$}
  2352. \label{sec:move-biasing}
  2353. This section describes an optional enhancement to register allocation
  2354. for those students who are looking for an extra challenge or who have
  2355. a deeper interest in register allocation.
  2356. We return to the running example, but we remove the supposition that
  2357. we only have one register to use. So we have the following mapping of
  2358. color numbers to registers.
  2359. \[
  2360. \{ 0 \mapsto \key{\%rbx}, \; 1 \mapsto \key{\%rcx}, \; 2 \mapsto \key{\%rdx}, \ldots \}
  2361. \]
  2362. Using the same assignment that was produced by register allocator
  2363. described in the last section, we get the following program.
  2364. \begin{minipage}{0.45\textwidth}
  2365. \begin{lstlisting}
  2366. (program (v w x y z)
  2367. (movq (int 1) (var v))
  2368. (movq (int 46) (var w))
  2369. (movq (var v) (var x))
  2370. (addq (int 7) (var x))
  2371. (movq (var x) (var y))
  2372. (addq (int 4) (var y))
  2373. (movq (var x) (var z))
  2374. (addq (var w) (var z))
  2375. (movq (var y) (var t.1))
  2376. (negq (var t.1))
  2377. (movq (var z) (var t.2))
  2378. (addq (var t.1) (var t.2))
  2379. (movq (var t.2) (reg rax)))
  2380. \end{lstlisting}
  2381. \end{minipage}
  2382. $\Rightarrow$
  2383. \begin{minipage}{0.45\textwidth}
  2384. \begin{lstlisting}
  2385. (program 0
  2386. (movq (int 1) (reg rbx))
  2387. (movq (int 46) (reg rcx))
  2388. (movq (reg rbx) (reg rdx))
  2389. (addq (int 7) (reg rdx))
  2390. (movq (reg rdx) (reg rbx))
  2391. (addq (int 4) (reg rbx))
  2392. (movq (reg rdx) (reg rdx))
  2393. (addq (reg rcx) (reg rdx))
  2394. (movq (reg rbx) (reg rbx))
  2395. (negq (reg rbx))
  2396. (movq (reg rdx) (reg rcx))
  2397. (addq (reg rbx) (reg rcx))
  2398. (movq (reg rcx) (reg rax)))
  2399. \end{lstlisting}
  2400. \end{minipage}
  2401. While this allocation is quite good, we could do better. For example,
  2402. the variables \key{v} and \key{x} ended up in different registers, but
  2403. if they had been placed in the same register, then the move from
  2404. \key{v} to \key{x} could be removed.
  2405. We say that two variables $p$ and $q$ are \emph{move related} if they
  2406. participate together in a \key{movq} instruction, that is, \key{movq
  2407. p, q} or \key{movq q, p}. When the register allocator chooses a
  2408. color for a variable, it should prefer a color that has already been
  2409. used for a move-related variable (assuming that they do not
  2410. interfere). Of course, this preference should not override the
  2411. preference for registers over stack locations, but should only be used
  2412. as a tie breaker when choosing between registers or when choosing
  2413. between stack locations.
  2414. We recommend that you represent the move relationships in a graph,
  2415. similar to how we represented interference. The following is the
  2416. \emph{move graph} for our running example.
  2417. \[
  2418. \begin{tikzpicture}[baseline=(current bounding box.center)]
  2419. \node (v) at (0,0) {$v$};
  2420. \node (w) at (3,0) {$w$};
  2421. \node (x) at (6,0) {$x$};
  2422. \node (y) at (3,-1.5) {$y$};
  2423. \node (z) at (6,-1.5) {$z$};
  2424. \node (t1) at (9,0) {$t.1$};
  2425. \node (t2) at (9,-1.5) {$t.2$};
  2426. \draw (t1) to (y);
  2427. \draw (t2) to (z);
  2428. \draw[bend left=20] (v) to (x);
  2429. \draw (x) to (y);
  2430. \draw (x) to (z);
  2431. \end{tikzpicture}
  2432. \]
  2433. Now we replay the graph coloring, pausing to see the coloring of $z$
  2434. and $v$. So we have the following coloring so far and the most
  2435. saturated vertex is $z$.
  2436. \[
  2437. \begin{tikzpicture}[baseline=(current bounding box.center)]
  2438. \node (v) at (0,0) {$v:-,\{1\}$};
  2439. \node (w) at (3,0) {$w:1,\{0,2\}$};
  2440. \node (x) at (6,0) {$x:2,\{0,1\}$};
  2441. \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
  2442. \node (z) at (6,-1.5) {$z:-,\{0,1\}$};
  2443. \node (t1) at (9,0) {$t.1:-,\{\}$};
  2444. \node (t2) at (9,-1.5) {$t.2:-,\{\}$};
  2445. \draw (t1) to (z);
  2446. \draw (t2) to (t1);
  2447. \draw (v) to (w);
  2448. \foreach \i in {w,x,y}
  2449. {
  2450. \foreach \j in {w,x,y}
  2451. {
  2452. \draw (\i) to (\j);
  2453. }
  2454. }
  2455. \draw (z) to (w);
  2456. \draw (z) to (y);
  2457. \end{tikzpicture}
  2458. \]
  2459. Last time we chose to color $z$ with $2$, which so happens to be the
  2460. color of $x$, and $z$ is move related to $x$. This was rather lucky,
  2461. and if the program had been a little different, and say $x$ had been
  2462. already assigned to $3$, then $z$ would still get $2$ and our luck
  2463. would have run out. With move biasing, we use the fact that $z$ and
  2464. $x$ are move related to influence the choice of color for $z$, in this
  2465. case choosing $2$ because that's the color of $x$.
  2466. \[
  2467. \begin{tikzpicture}[baseline=(current bounding box.center)]
  2468. \node (v) at (0,0) {$v:-,\{1\}$};
  2469. \node (w) at (3,0) {$w:1,\{0,2\}$};
  2470. \node (x) at (6,0) {$x:2,\{0,1\}$};
  2471. \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
  2472. \node (z) at (6,-1.5) {$z:2,\{0,1\}$};
  2473. \node (t1) at (9,0) {$t.1:-,\{2\}$};
  2474. \node (t2) at (9,-1.5) {$t.2:-,\{\}$};
  2475. \draw (t1) to (z);
  2476. \draw (t2) to (t1);
  2477. \draw (v) to (w);
  2478. \foreach \i in {w,x,y}
  2479. {
  2480. \foreach \j in {w,x,y}
  2481. {
  2482. \draw (\i) to (\j);
  2483. }
  2484. }
  2485. \draw (z) to (w);
  2486. \draw (z) to (y);
  2487. \end{tikzpicture}
  2488. \]
  2489. Next we consider coloring the variable $v$, and we just need to avoid
  2490. choosing $1$ because of the interference with $w$. Last time we choose
  2491. the color $0$, simply because it was the lowest, but this time we know
  2492. that $v$ is move related to $x$, so we choose the color $2$.
  2493. \[
  2494. \begin{tikzpicture}[baseline=(current bounding box.center)]
  2495. \node (v) at (0,0) {$v:2,\{1\}$};
  2496. \node (w) at (3,0) {$w:1,\{0,2\}$};
  2497. \node (x) at (6,0) {$x:2,\{0,1\}$};
  2498. \node (y) at (3,-1.5) {$y:0,\{1,2\}$};
  2499. \node (z) at (6,-1.5) {$z:2,\{0,1\}$};
  2500. \node (t1) at (9,0) {$t.1:-,\{2\}$};
  2501. \node (t2) at (9,-1.5) {$t.2:-,\{\}$};
  2502. \draw (t1) to (z);
  2503. \draw (t2) to (t1);
  2504. \draw (v) to (w);
  2505. \foreach \i in {w,x,y}
  2506. {
  2507. \foreach \j in {w,x,y}
  2508. {
  2509. \draw (\i) to (\j);
  2510. }
  2511. }
  2512. \draw (z) to (w);
  2513. \draw (z) to (y);
  2514. \end{tikzpicture}
  2515. \]
  2516. We apply this register assignment to the running example, on the left,
  2517. to obtain the code on right.
  2518. \begin{minipage}{0.45\textwidth}
  2519. \begin{lstlisting}
  2520. (program (v w x y z)
  2521. (movq (int 1) (var v))
  2522. (movq (int 46) (var w))
  2523. (movq (var v) (var x))
  2524. (addq (int 7) (var x))
  2525. (movq (var x) (var y))
  2526. (addq (int 4) (var y))
  2527. (movq (var x) (var z))
  2528. (addq (var w) (var z))
  2529. (movq (var y) (var t.1))
  2530. (negq (var t.1))
  2531. (movq (var z) (var t.2))
  2532. (addq (var t.1) (var t.2))
  2533. (movq (var t.2) (reg rax)))
  2534. \end{lstlisting}
  2535. \end{minipage}
  2536. $\Rightarrow$
  2537. \begin{minipage}{0.45\textwidth}
  2538. \begin{lstlisting}
  2539. (program 0
  2540. (movq (int 1) (reg rdx))
  2541. (movq (int 46) (reg rcx))
  2542. (movq (reg rdx) (reg rdx))
  2543. (addq (int 7) (reg rdx))
  2544. (movq (reg rdx) (reg rbx))
  2545. (addq (int 4) (reg rbx))
  2546. (movq (reg rdx) (reg rdx))
  2547. (addq (reg rcx) (reg rdx))
  2548. (movq (reg rbx) (reg rbx))
  2549. (negq (reg rbx))
  2550. (movq (reg rdx) (reg rcx))
  2551. (addq (reg rbx) (reg rcx))
  2552. (movq (reg rcx) (reg rax)))
  2553. \end{lstlisting}
  2554. \end{minipage}
  2555. The \code{patch-instructions} then removes the trivial moves from
  2556. \key{v} to \key{x}, from \key{x} to \key{z}, and from \key{y} to
  2557. \key{t.1}, to obtain the following result.
  2558. \begin{lstlisting}
  2559. (program 0
  2560. (movq (int 1) (reg rdx))
  2561. (movq (int 46) (reg rcx))
  2562. (addq (int 7) (reg rdx))
  2563. (movq (reg rdx) (reg rbx))
  2564. (addq (int 4) (reg rbx))
  2565. (addq (reg rcx) (reg rdx))
  2566. (negq (reg rbx))
  2567. (movq (reg rdx) (reg rcx))
  2568. (addq (reg rbx) (reg rcx))
  2569. (movq (reg rcx) (reg rax)))
  2570. \end{lstlisting}
  2571. \begin{exercise}\normalfont
  2572. Change your implementation of \code{allocate-registers} to take move
  2573. biasing into account. Make sure that your compiler still passes all of
  2574. the previous tests. Create two new tests that include at least one
  2575. opportunity for move biasing and visually inspect the output x86
  2576. programs to make sure that your move biasing is working properly.
  2577. \end{exercise}
  2578. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2579. \chapter{Booleans, Control Flow, and Type Checking}
  2580. \label{ch:bool-types}
  2581. Up until now the input languages have only included a single kind of
  2582. value, the integers. In this Chapter we add a second kind of value,
  2583. the Booleans (true and false), together with some new operations
  2584. (\key{and}, \key{not}, \key{eq?}) and conditional expressions to create
  2585. the $R_2$ language. With the addition of conditional expressions,
  2586. programs can have non-trivial control flow which has an impact on
  2587. several parts of the compiler. Also, because we now have two kinds of
  2588. values, we need to worry about programs that apply an operation to the
  2589. wrong kind of value, such as \code{(not 1)}.
  2590. There are two language design options for such situations. One option
  2591. is to signal an error and the other is to provide a wider
  2592. interpretation of the operation. The Racket language uses a mixture of
  2593. these two options, depending on the operation and on the kind of
  2594. value. For example, the result of \code{(not 1)} in Racket is
  2595. \code{\#f} (that is, false) because Racket treats non-zero integers as
  2596. true. On the other hand, \code{(car 1)} results in a run-time error in
  2597. Racket, which states that \code{car} expects a pair.
  2598. The Typed Racket language makes similar design choices as Racket,
  2599. except much of the error detection happens at compile time instead of
  2600. run time. Like Racket, Typed Racket accepts and runs \code{(not 1)},
  2601. producing \code{\#f}. But in the case of \code{(car 1)}, Typed Racket
  2602. reports a compile-time error because the type of the argument is
  2603. expected to be of the form \code{(Listof T)} or \code{(Pairof T1 T2)}.
  2604. For the $R_2$ language we choose to be more like Typed Racket in that
  2605. we shall perform type checking during compilation. However, we shall
  2606. take a narrower interpretation of the operations, rejecting
  2607. \code{(not 1)}. Despite this difference in design,
  2608. $R_2$ is literally a subset of Typed Racket. Every $R_2$
  2609. program is a Typed Racket program.
  2610. This chapter is organized as follows. We begin by defining the syntax
  2611. and interpreter for the $R_2$ language (Section~\ref{sec:r2-lang}). We
  2612. then introduce the idea of type checking and build a type checker for
  2613. $R_2$ (Section~\ref{sec:type-check-r2}). To compile $R_2$ we need to
  2614. enlarge the intermediate language $C_0$ into $C_1$, which we do in
  2615. Section~\ref{sec:c1}. The remaining sections of this Chapter discuss
  2616. how our compiler passes need to change to accommodate Booleans and
  2617. conditional control flow.
  2618. \section{The $R_2$ Language}
  2619. \label{sec:r2-lang}
  2620. The syntax of the $R_2$ language is defined in
  2621. Figure~\ref{fig:r2-syntax}. It includes all of $R_1$, so we only show
  2622. the new operators and expressions. We add the Boolean literals
  2623. \code{\#t} and \code{\#f} for true and false and the conditional
  2624. expression. The operators are expanded to include the \key{and} and
  2625. \key{not} operations on Booleans and the \key{eq?} operation for
  2626. comparing two integers and for comparing two Booleans.
  2627. \begin{figure}[tbp]
  2628. \centering
  2629. \fbox{
  2630. \begin{minipage}{0.96\textwidth}
  2631. \[
  2632. \begin{array}{lcl}
  2633. \Exp &::=& \gray{\Int \mid (\key{read}) \mid (\key{-}\;\Exp) \mid (\key{+} \; \Exp\;\Exp)} \\
  2634. &\mid& \gray{\Var \mid \LET{\Var}{\Exp}{\Exp}} \mid \key{\#t} \mid \key{\#f} \mid
  2635. (\key{and}\;\Exp\;\Exp) \mid (\key{not}\;\Exp) \\
  2636. &\mid& (\key{eq?}\;\Exp\;\Exp) \mid \IF{\Exp}{\Exp}{\Exp} \\
  2637. R_2 &::=& (\key{program} \; \Exp)
  2638. \end{array}
  2639. \]
  2640. \end{minipage}
  2641. }
  2642. \caption{The $R_2$ language, an extension of $R_1$
  2643. (Figure~\ref{fig:r1-syntax}).}
  2644. \label{fig:r2-syntax}
  2645. \end{figure}
  2646. Figure~\ref{fig:interp-R2} defines the interpreter for $R_2$, omitting
  2647. the parts that are the same as the interpreter for $R_1$
  2648. (Figure~\ref{fig:interp-R1}). The literals \code{\#t} and \code{\#f}
  2649. simply evaluate to themselves. The conditional expression \code{(if
  2650. cnd thn els)} evaluates the Boolean expression \code{cnd} and then
  2651. either evaluates \code{thn} or \code{els} depending on whether
  2652. \code{cnd} produced \code{\#t} or \code{\#f}. The logical operations
  2653. \code{not} and \code{and} behave as you might expect, but note that
  2654. the \code{and} operation is short-circuiting. That is, the second
  2655. expression \code{e2} is not evaluated if \code{e1} evaluates to
  2656. \code{\#f}.
  2657. \begin{figure}[tbp]
  2658. \begin{lstlisting}
  2659. (define (interp-R2 env e)
  2660. (match e
  2661. ...
  2662. [(? boolean?) e]
  2663. [`(if ,cnd ,thn ,els)
  2664. (match (interp-R2 env cnd)
  2665. [#t (interp-R2 env thn)]
  2666. [#f (interp-R2 env els)])]
  2667. [`(not ,e)
  2668. (match (interp-R2 env e) [#t #f] [#f #t])]
  2669. [`(and ,e1 ,e2)
  2670. (match (interp-R2 env e1)
  2671. [#t (match (interp-R2 env e2) [#t #t] [#f #f])]
  2672. [#f #f])]
  2673. [`(eq? ,e1 ,e2)
  2674. (let ([v1 (interp-R2 env e1)] [v2 (interp-R2 env e2)])
  2675. (cond [(and (fixnum? v1) (fixnum? v2)) (eq? v1 v2)]
  2676. [(and (boolean? v1) (boolean? v2)) (eq? v1 v2)]))]
  2677. ))
  2678. \end{lstlisting}
  2679. \caption{Interpreter for the $R_2$ language.}
  2680. \label{fig:interp-R2}
  2681. \end{figure}
  2682. \section{Type Checking $R_2$ Programs}
  2683. \label{sec:type-check-r2}
  2684. It is helpful to think about type checking into two complementary
  2685. ways. A type checker predicts the \emph{type} of value that will be
  2686. produced by each expression in the program. For $R_2$, we have just
  2687. two types, \key{Integer} and \key{Boolean}. So a type checker should
  2688. predict that
  2689. \begin{lstlisting}
  2690. (+ 10 (- (+ 12 20)))
  2691. \end{lstlisting}
  2692. produces an \key{Integer} while
  2693. \begin{lstlisting}
  2694. (and (not #f) #t)
  2695. \end{lstlisting}
  2696. produces a \key{Boolean}.
  2697. As mentioned at the beginning of this chapter, a type checker also
  2698. rejects programs that apply operators to the wrong type of value. Our
  2699. type checker for $R_2$ will signal an error for the following because,
  2700. as we have seen above, the expression \code{(+ 10 ...)} has type
  2701. \key{Integer}, and we shall require an argument of \code{not} to have
  2702. type \key{Boolean}.
  2703. \begin{lstlisting}
  2704. (not (+ 10 (- (+ 12 20))))
  2705. \end{lstlisting}
  2706. The type checker for $R_2$ is best implemented as a structurally
  2707. recursive function over the AST. Figure~\ref{fig:type-check-R2} shows
  2708. many of the clauses for the \code{typecheck-R2} function. Given an
  2709. input expression \code{e}, the type checker either returns the type
  2710. (\key{Integer} or \key{Boolean}) or it signals an error. Of course,
  2711. the type of an integer literal is \code{Integer} and the type of a
  2712. Boolean literal is \code{Boolean}. To handle variables, the type
  2713. checker, like the interpreter, uses an association list. However, in
  2714. this case the association list maps variables to types instead of
  2715. values. Consider the clause for \key{let}. We type check the
  2716. initializing expression to obtain its type \key{T} and then map the
  2717. variable \code{x} to \code{T}. When the type checker encounters the
  2718. use of a variable, it can lookup its type in the association list.
  2719. \begin{figure}[tbp]
  2720. \begin{lstlisting}
  2721. (define (typecheck-R2 env e)
  2722. (match e
  2723. [(? fixnum?) 'Integer]
  2724. [(? boolean?) 'Boolean]
  2725. [(? symbol?) (lookup e env)]
  2726. [`(let ([,x ,e]) ,body)
  2727. (define T (typecheck-R2 env e))
  2728. (define new-env (cons (cons x T) env))
  2729. (typecheck-R2 new-env body)]
  2730. ...
  2731. [`(not ,e)
  2732. (match (typecheck-R2 env e)
  2733. ['Boolean 'Boolean]
  2734. [else (error 'typecheck-R2 "'not' expects a Boolean" e)])]
  2735. ...
  2736. [`(program ,body)
  2737. (typecheck-R2 '() body)
  2738. `(program ,body)]
  2739. ))
  2740. \end{lstlisting}
  2741. \caption{Skeleton of a type checker for the $R_2$ language.}
  2742. \label{fig:type-check-R2}
  2743. \end{figure}
  2744. \begin{exercise}\normalfont
  2745. Complete the implementation of \code{typecheck-R2} and test it on 10
  2746. new example programs in $R_2$ that you choose based on how thoroughly
  2747. they test the type checking algorithm. Half of the example programs
  2748. should have a type error, to make sure that your type checker properly
  2749. rejects them. The other half of the example programs should not have
  2750. type errors. Your testing should check that the result of the type
  2751. checker agrees with the value returned by the interpreter, that is, if
  2752. the type checker returns \key{Integer}, then the interpreter should
  2753. return an integer. Likewise, if the type checker returns
  2754. \key{Boolean}, then the interpreter should return \code{\#t} or
  2755. \code{\#f}. Note that if your type checker does not signal an error
  2756. for a program, then interpreting that program should not encounter an
  2757. error. If it does, there is something wrong with your type checker.
  2758. \end{exercise}
  2759. \section{The $C_1$ Language}
  2760. \label{sec:c1}
  2761. The $R_2$ language adds Booleans and conditional expressions to $R_1$.
  2762. As with $R_1$, we shall compile to a C-like intermediate language, but
  2763. we need to grow that intermediate language to handle the new features
  2764. in $R_2$. Figure~\ref{fig:c1-syntax} shows the new features of $C_1$;
  2765. we add logic and comparison operators to the $\Exp$ non-terminal, the
  2766. literals \key{\#t} and \key{\#f} to the $\Arg$ non-terminal, and we
  2767. add an \key{if} statement. The \key{if} statement of $C_1$ includes an
  2768. \key{eq?} test, which is needed for improving code generation in
  2769. Section~\ref{sec:opt-if}. We do not include \key{and} in $C_1$
  2770. because it is not needed in the translation of the \key{and} of $R_2$.
  2771. \begin{figure}[tbp]
  2772. \fbox{
  2773. \begin{minipage}{0.96\textwidth}
  2774. \[
  2775. \begin{array}{lcl}
  2776. \Arg &::=& \gray{\Int \mid \Var} \mid \key{\#t} \mid \key{\#f} \\
  2777. \Exp &::= & \gray{\Arg \mid (\key{read}) \mid (\key{-}\;\Arg) \mid (\key{+} \; \Arg\;\Arg)}
  2778. \mid (\key{not}\;\Arg) \mid (\key{eq?}\;\Arg\;\Arg) \\
  2779. \Stmt &::=& \gray{\ASSIGN{\Var}{\Exp} \mid \RETURN{\Arg}} \\
  2780. &\mid& \IF{(\key{eq?}\, \Arg\,\Arg)}{\Stmt^{*}}{\Stmt^{*}} \\
  2781. C_1 & ::= & (\key{program}\;(\Var^{*})\;\Stmt^{+})
  2782. \end{array}
  2783. \]
  2784. \end{minipage}
  2785. }
  2786. \caption{The $C_1$ intermediate language, an extension of $C_0$
  2787. (Figure~\ref{fig:c0-syntax}).}
  2788. \label{fig:c1-syntax}
  2789. \end{figure}
  2790. \section{Flatten Expressions}
  2791. \label{sec:flatten-r2}
  2792. The \code{flatten} pass needs to be expanded to handle the Boolean
  2793. literals \key{\#t} and \key{\#f}, the new logic and comparison
  2794. operations, and \key{if} expressions. We shall start with a simple
  2795. example of translating a \key{if} expression, shown below on the
  2796. left. \\
  2797. \begin{tabular}{lll}
  2798. \begin{minipage}{0.4\textwidth}
  2799. \begin{lstlisting}
  2800. (program (if #f 0 42))
  2801. \end{lstlisting}
  2802. \end{minipage}
  2803. &
  2804. $\Rightarrow$
  2805. &
  2806. \begin{minipage}{0.4\textwidth}
  2807. \begin{lstlisting}
  2808. (program (if.1)
  2809. (if (eq? #t #f)
  2810. ((assign if.1 0))
  2811. ((assign if.1 42)))
  2812. (return if.1))
  2813. \end{lstlisting}
  2814. \end{minipage}
  2815. \end{tabular} \\
  2816. The value of the \key{if} expression is the value of the branch that
  2817. is selected. Recall that in the \code{flatten} pass we need to replace
  2818. arbitrary expressions with $\Arg$'s (variables or literals). In the
  2819. translation above, on the right, we have translated the \key{if}
  2820. expression into a new variable \key{if.1} and we have produced code
  2821. that will assign the appropriate value to \key{if.1}. For $R_1$, the
  2822. \code{flatten} pass returned a list of assignment statements. Here,
  2823. for $R_2$, we return a list of statements that can include both
  2824. \key{if} statements and assignment statements.
  2825. The next example is a bit more involved, showing what happens when
  2826. there are complex expressions (not variables or literals) in the
  2827. condition and branch expressions of an \key{if}, including nested
  2828. \key{if} expressions.
  2829. \begin{tabular}{lll}
  2830. \begin{minipage}{0.4\textwidth}
  2831. \begin{lstlisting}
  2832. (program
  2833. (if (eq? (read) 0)
  2834. 777
  2835. (+ 2 (if (eq? (read) 0)
  2836. 40
  2837. 444))))
  2838. \end{lstlisting}
  2839. \end{minipage}
  2840. &
  2841. $\Rightarrow$
  2842. &
  2843. \begin{minipage}{0.4\textwidth}
  2844. \begin{lstlisting}
  2845. (program (t.1 t.2 if.1 t.3 t.4
  2846. if.2 t.5)
  2847. (assign t.1 (read))
  2848. (assign t.2 (eq? t.1 0))
  2849. (if (eq? #t t.2)
  2850. ((assign if.1 777))
  2851. ((assign t.3 (read))
  2852. (assign t.4 (eq? t.3 0))
  2853. (if (eq? #t t.4)
  2854. ((assign if.2 40))
  2855. ((assign if.2 444)))
  2856. (assign t.5 (+ 2 if.2))
  2857. (assign if.1 t.5)))
  2858. (return if.1))
  2859. \end{lstlisting}
  2860. \end{minipage}
  2861. \end{tabular} \\
  2862. The \code{flatten} clauses for the Boolean literals and the operations
  2863. \key{not} and \key{eq?} are straightforward. However, the
  2864. \code{flatten} clause for \key{and} requires some care to properly
  2865. imitate the order of evaluation of the interpreter for $R_2$
  2866. (Figure~\ref{fig:interp-R2}). We recommend using an \key{if} statement
  2867. in the code you generate for \key{and}.
  2868. The \code{flatten} clause for \key{if} requires some care because the
  2869. condition of the \key{if} can be an arbitrary expression in $R_2$ but
  2870. in $C_1$ the condition must be an equality predicate. We recommend
  2871. flattening the condition into an $\Arg$ and then comparing it with
  2872. \code{\#t}.
  2873. \begin{exercise}\normalfont
  2874. Expand your \code{flatten} pass to handle $R_2$, that is, handle the
  2875. Boolean literals, the new logic and comparison operations, and the
  2876. \key{if} expressions. Create 4 more test cases that expose whether
  2877. your flattening code is correct. Test your \code{flatten} pass by
  2878. running the output programs with \code{interp-C}
  2879. (Appendix~\ref{appendix:interp}).
  2880. \end{exercise}
  2881. \section{More x86}
  2882. \label{sec:x86-1}
  2883. To implement the new logical operations, the comparison \key{eq?}, and
  2884. the \key{if} statement, we need to delve further into the x86
  2885. language. Figure~\ref{fig:x86-ast-b} defines the abstract syntax for a
  2886. larger subset of x86 that includes instructions for logical
  2887. operations, comparisons, and jumps.
  2888. In addition to its arithmetic operations, x86 provides bitwise
  2889. operators that perform an operation on every bit of their
  2890. arguments. For example, the \key{xorq} instruction takes two
  2891. arguments, performs a pairwise exclusive-or (XOR) operation on the
  2892. bits of its arguments, and writes the result into its second argument.
  2893. Recall the truth table for XOR:
  2894. \begin{center}
  2895. \begin{tabular}{l|cc}
  2896. & 0 & 1 \\ \hline
  2897. 0 & 0 & 1 \\
  2898. 1 & 1 & 0
  2899. \end{tabular}
  2900. \end{center}
  2901. So $0011 \mathrel{\mathrm{XOR}} 0101 = 0110$.
  2902. \begin{figure}[tbp]
  2903. \fbox{
  2904. \begin{minipage}{0.96\textwidth}
  2905. \[
  2906. \begin{array}{lcl}
  2907. \Arg &::=& \ldots \mid (\key{byte-reg}\; \itm{register}) \\
  2908. \Instr &::=& \ldots \mid (\key{xorq} \; \Arg\;\Arg) \\
  2909. &\mid& (\key{cmpq} \; \Arg\; \Arg) \mid (\key{sete} \; \Arg)
  2910. \mid (\key{movzbq}\;\Arg\;\Arg) \\
  2911. &\mid& (\key{jmp} \; \itm{label}) \mid (\key{je} \; \itm{label}) \mid
  2912. (\key{label} \; \itm{label}) \\
  2913. x86_1 &::= & (\key{program} \;\itm{info} \; \Instr^{+})
  2914. \end{array}
  2915. \]
  2916. \end{minipage}
  2917. }
  2918. \caption{The x86$_1$ language (extends x86$_0$ of Figure~\ref{fig:x86-ast-a}).}
  2919. \label{fig:x86-ast-b}
  2920. \end{figure}
  2921. The \key{cmpq} instruction is somewhat unusual in that its arguments
  2922. are the two things to be compared and the result (less than, greater
  2923. than, equal, not equal, etc.) is placed in the special EFLAGS
  2924. register. This register cannot be accessed directly but it can be
  2925. queried by a number of instructions, including the \key{sete}
  2926. instruction. The \key{sete} instruction puts a \key{1} or \key{0} into
  2927. its destination depending on whether the comparison came out as equal
  2928. or not, respectively. The \key{sete} instruction has an annoying quirk
  2929. in that its destination argument must be single byte register, such as
  2930. \code{al}, which is part of the \code{rax} register. Thankfully, the
  2931. \key{movzbq} instruction can then be used to move from a single byte
  2932. register to a normal 64-bit register.
  2933. The \key{jmp} instruction jumps to the instruction after the indicated
  2934. label. The \key{je} instruction jumps to the instruction after the
  2935. indicated label if the result in the EFLAGS register is equal, whereas
  2936. the \key{je} instruction falls through to the next instruction if
  2937. EFLAGS is not equal.
  2938. \section{Select Instructions}
  2939. \label{sec:select-r2}
  2940. The \code{select-instructions} pass needs to lower from $C_1$ to an
  2941. intermediate representation suitable for conducting register
  2942. allocation, i.e., close to x86$_1$.
  2943. We can take the usual approach of encoding Booleans as integers, with
  2944. true as 1 and false as 0.
  2945. \[
  2946. \key{\#t} \Rightarrow \key{1}
  2947. \qquad
  2948. \key{\#f} \Rightarrow \key{0}
  2949. \]
  2950. The \code{not} operation can be implemented in terms of \code{xorq}.
  2951. Can you think of a bit pattern that, when XOR'd with the bit
  2952. representation of 0 produces 1, and when XOR'd with the bit
  2953. representation of 1 produces 0?
  2954. Translating the \code{eq?} operation to x86 is slightly involved due
  2955. to the unusual nature of the \key{cmpq} instruction discussed above.
  2956. We recommend translating an assignment from \code{eq?} into the
  2957. following sequence of three instructions. \\
  2958. \begin{tabular}{lll}
  2959. \begin{minipage}{0.4\textwidth}
  2960. \begin{lstlisting}
  2961. (assign |$\itm{lhs}$| (eq? |$\Arg_1$| |$\Arg_2$|))
  2962. \end{lstlisting}
  2963. \end{minipage}
  2964. &
  2965. $\Rightarrow$
  2966. &
  2967. \begin{minipage}{0.4\textwidth}
  2968. \begin{lstlisting}
  2969. (cmpq |$\Arg_1$| |$\Arg_2$|)
  2970. (sete (byte-reg al))
  2971. (movzbq (byte-reg al) |$\itm{lhs}$|)
  2972. \end{lstlisting}
  2973. \end{minipage}
  2974. \end{tabular} \\
  2975. One further caveat is that the second argument of the \key{cmpq} instruction
  2976. cannot be an immediate value. If you are comparing two immediates, you must insert another \key{movq} instruction to put the second argument in
  2977. \key{rax}.
  2978. % The translation of the \code{not} operator is not quite as simple
  2979. % as it seems. Recall that \key{notq} is a bitwise operator, not a boolean
  2980. % one. For example, the following program performs bitwise negation on
  2981. % the integer 1:
  2982. %
  2983. % \begin{tabular}{lll}
  2984. % \begin{minipage}{0.4\textwidth}
  2985. % \begin{lstlisting}
  2986. % (movq (int 1) (reg rax))
  2987. % (notq (reg rax))
  2988. % \end{lstlisting}
  2989. % \end{minipage}
  2990. % \end{tabular}
  2991. %
  2992. % After the program is run, \key{rax} does not contain 0, as you might
  2993. % hope -- it contains the binary value $111\ldots10$, which is the
  2994. % two's complement representation of $-2$. We recommend implementing boolean
  2995. % not by using \key{notq} and then masking the upper bits of the result with
  2996. % the \key{andq} instruction.
  2997. Regarding \key{if} statements, we recommend that you not lower them in
  2998. \code{select-instructions} but instead lower them in
  2999. \code{patch-instructions}. The reason is that for purposes of
  3000. liveness analysis, \key{if} statements are easier to deal with than
  3001. jump instructions.
  3002. \begin{exercise}\normalfont
  3003. Expand your \code{select-instructions} pass to handle the new features
  3004. of the $R_2$ language. Test the pass on all the examples you have
  3005. created and make sure that you have some test programs that use the
  3006. \code{eq?} operator, creating some if necessary. Test the output of
  3007. \code{select-instructions} using the \code{interp-x86} interpreter
  3008. (Appendix~\ref{appendix:interp}).
  3009. \end{exercise}
  3010. \section{Register Allocation}
  3011. \label{sec:register-allocation-r2}
  3012. The changes required for $R_2$ affect the liveness analysis, building
  3013. the interference graph, and assigning homes, but the graph coloring
  3014. algorithm itself should not need to change.
  3015. \subsection{Liveness Analysis}
  3016. \label{sec:liveness-analysis-r2}
  3017. The addition of \key{if} statements brings up an interesting issue in
  3018. liveness analysis. Recall that liveness analysis works backwards
  3019. through the program, for each instruction computing the variables that
  3020. are live before the instruction based on which variables are live
  3021. after the instruction. Now consider the situation for \code{(\key{if}
  3022. (\key{eq?} $e_1$ $e_2$) $\itm{thns}$ $\itm{elss}$)}, where we know the
  3023. $L_{\mathsf{after}}$ set and need to produce the $L_{\mathsf{before}}$
  3024. set. We can recursively perform liveness analysis on the $\itm{thns}$
  3025. and $\itm{elss}$ branches, using $L_{\mathsf{after}}$ as the starting
  3026. point, to obtain $L^{\mathsf{thns}}_{\mathsf{before}}$ and
  3027. $L^{\mathsf{elss}}_{\mathsf{before}}$ respectively. However, we do not
  3028. know, during compilation, which way the branch will go, so we do not
  3029. know whether to use $L^{\mathsf{thns}}_{\mathsf{before}}$ or
  3030. $L^{\mathsf{elss}}_{\mathsf{before}}$ as the $L_{\mathsf{before}}$ for
  3031. the entire \key{if} statement. The solution comes from the observation
  3032. that there is no harm in identifying more variables as live than
  3033. absolutely necessary. Thus, we can take the union of the live
  3034. variables from the two branches to be the live set for the whole
  3035. \key{if}, as shown below. Of course, we also need to include the
  3036. variables that are read in the $\itm{cnd}$ argument.
  3037. \[
  3038. L_{\mathsf{before}} = L^{\mathsf{thns}}_{\mathsf{before}} \cup
  3039. L^{\mathsf{elss}}_{\mathsf{before}} \cup
  3040. \mathit{Vars}(e_1) \cup \mathit{Vars}(e_2)
  3041. \]
  3042. We need the live-after sets for all the instructions in both branches
  3043. of the \key{if} when we build the interference graph, so I recommend
  3044. storing that data in the \key{if} statement AST as follows:
  3045. \begin{lstlisting}
  3046. (if (eq? |$\itm{arg}$| |$\itm{arg}$|) |$\itm{thns}$| |$\itm{thn{-}lives}$| |$\itm{elss}$| |$\itm{els{-}lives}$|)
  3047. \end{lstlisting}
  3048. If you wrote helper functions for computing the variables in an
  3049. argument and the variables read-from ($R$) or written-to ($W$) by an
  3050. instruction, you need to be update them to handle the new kinds of
  3051. arguments and instructions in x86$_1$.
  3052. \subsection{Build Interference}
  3053. \label{sec:build-interference-r2}
  3054. Many of the new instructions, such as the logical operations, can be
  3055. handled in the same way as the arithmetic instructions. Thus, if your
  3056. code was already quite general, it will not need to be changed to
  3057. handle the logical operations. If not, I recommend that you change
  3058. your code to be more general. The \key{movzbq} instruction should be
  3059. handled like the \key{movq} instruction. The \key{if} statement is
  3060. straightforward to handle because we stored the live-after sets for the
  3061. two branches in the AST node as described above. Here we just need to
  3062. recursively process the two branches. The output of this pass can
  3063. discard the live after sets, as they are no longer needed.
  3064. \subsection{Assign Homes}
  3065. \label{sec:assign-homes-r2}
  3066. The \code{assign-homes} function (Section~\ref{sec:assign-s0}) needs
  3067. to be updated to handle the \key{if} statement, simply by recursively
  3068. processing the child nodes. Hopefully your code already handles the
  3069. other new instructions, but if not, you can generalize your code.
  3070. \begin{exercise}\normalfont
  3071. Implement the additions to the \code{register-allocation} pass so that
  3072. it works for $R_2$ and test your compiler using your previously
  3073. created programs on the \code{interp-x86} interpreter
  3074. (Appendix~\ref{appendix:interp}).
  3075. \end{exercise}
  3076. \section{Lower Conditionals (New Pass)}
  3077. \label{sec:lower-conditionals}
  3078. In the \code{select-instructions} pass we decided to procrastinate in
  3079. the lowering of the \key{if} statement (thereby making liveness
  3080. analysis easier). Now we need to make up for that and turn the
  3081. \key{if} statement into the appropriate instruction sequence. The
  3082. following translation gives the general idea. If $e_1$ and $e_2$ are
  3083. equal we need to execute the $\itm{thns}$ branch and otherwise we need
  3084. to execute the $\itm{elss}$ branch. So use \key{cmpq} and do a
  3085. conditional jump to the $\itm{thenlabel}$ (which we can generate with
  3086. \code{gensym}). Otherwise we fall through to the $\itm{elss}$
  3087. branch. At the end of the $\itm{elss}$ branch we need to take care to
  3088. not fall through to the $\itm{thns}$ branch. So we jump to the
  3089. $\itm{endlabel}$ (also generated with \code{gensym}).
  3090. \begin{tabular}{lll}
  3091. \begin{minipage}{0.4\textwidth}
  3092. \begin{lstlisting}
  3093. (if (eq? |$e_1$| |$e_2$|) |$\itm{thns}$| |$\itm{elss}$|)
  3094. \end{lstlisting}
  3095. \end{minipage}
  3096. &
  3097. $\Rightarrow$
  3098. &
  3099. \begin{minipage}{0.4\textwidth}
  3100. \begin{lstlisting}
  3101. (cmpq |$e_1$| |$e_2$|)
  3102. (je |$\itm{thenlabel}$|)
  3103. |$\itm{elss}$|
  3104. (jmp |$\itm{endlabel}$|)
  3105. (label |$\itm{thenlabel}$|)
  3106. |$\itm{thns}$|
  3107. (label |$\itm{endlabel}$|)
  3108. \end{lstlisting}
  3109. \end{minipage}
  3110. \end{tabular}
  3111. \begin{exercise}\normalfont
  3112. Implement the \code{lower-conditionals} pass. Test your compiler using
  3113. your previously created programs on the \code{interp-x86} interpreter
  3114. (Appendix~\ref{appendix:interp}).
  3115. \end{exercise}
  3116. \section{Patch Instructions}
  3117. There are no special restrictions on the instructions \key{je},
  3118. \key{jmp}, and \key{label}, but there is an unusual restriction on
  3119. \key{cmpq}. The second argument is not allowed to be an immediate
  3120. value (such as a literal integer).
  3121. \begin{exercise}\normalfont
  3122. Update \code{patch-instructions} to handle the new x86 instructions.
  3123. Test your compiler using your previously created programs on the
  3124. \code{interp-x86} interpreter (Appendix~\ref{appendix:interp}).
  3125. \end{exercise}
  3126. \section{An Example Translation}
  3127. Figure~\ref{fig:if-example-x86} shows a simple example program in
  3128. $R_2$ translated to x86, showing the results of \code{flatten},
  3129. \code{select-instructions}, and the final x86 assembly.
  3130. \begin{figure}[tbp]
  3131. \begin{tabular}{lll}
  3132. \begin{minipage}{0.5\textwidth}
  3133. \begin{lstlisting}
  3134. (program
  3135. (if (eq? (read) 1) 42 0))
  3136. \end{lstlisting}
  3137. $\Downarrow$
  3138. \begin{lstlisting}
  3139. (program (t.1 t.2 if.1)
  3140. (assign t.1 (read))
  3141. (assign t.2 (eq? t.1 1))
  3142. (if (eq? #t t.2)
  3143. ((assign if.1 42))
  3144. ((assign if.1 0)))
  3145. (return if.1))
  3146. \end{lstlisting}
  3147. $\Downarrow$
  3148. \begin{lstlisting}
  3149. (program (t.1 t.2 if.1)
  3150. (callq read_int)
  3151. (movq (reg rax) (var t.1))
  3152. (cmpq (int 1) (var t.1))
  3153. (sete (byte-reg al))
  3154. (movzbq (byte-reg al) (var t.2))
  3155. (if (eq? (int 1) (var t.2))
  3156. ((movq (int 42) (var if.1)))
  3157. ((movq (int 0) (var if.1))))
  3158. (movq (var if.1) (reg rax)))
  3159. \end{lstlisting}
  3160. \end{minipage}
  3161. &
  3162. $\Rightarrow$
  3163. \begin{minipage}{0.4\textwidth}
  3164. \begin{lstlisting}
  3165. .globl _main
  3166. _main:
  3167. pushq %rbp
  3168. movq %rsp, %rbp
  3169. pushq %r15
  3170. pushq %r14
  3171. pushq %r13
  3172. pushq %r12
  3173. pushq %rbx
  3174. subq $8, %rsp
  3175. callq _read_int
  3176. movq %rax, %rcx
  3177. cmpq $1, %rcx
  3178. sete %al
  3179. movzbq %al, %rcx
  3180. cmpq $1, %rcx
  3181. je then21288
  3182. movq $0, %rbx
  3183. jmp if_end21289
  3184. then21288:
  3185. movq $42, %rbx
  3186. if_end21289:
  3187. movq %rbx, %rax
  3188. movq %rax, %rdi
  3189. callq _print_int
  3190. movq $0, %rax
  3191. addq $8, %rsp
  3192. popq %rbx
  3193. popq %r12
  3194. popq %r13
  3195. popq %r14
  3196. popq %r15
  3197. popq %rbp
  3198. retq
  3199. \end{lstlisting}
  3200. \end{minipage}
  3201. \end{tabular}
  3202. \caption{Example compilation of an \key{if} expression to x86.}
  3203. \label{fig:if-example-x86}
  3204. \end{figure}
  3205. \begin{figure}[p]
  3206. \begin{tikzpicture}[baseline=(current bounding box.center)]
  3207. \node (R1) at (0,2) {\large $R_1$};
  3208. \node (R1-2) at (3,2) {\large $R_1$};
  3209. \node (R1-3) at (6,2) {\large $R_1$};
  3210. \node (C0-1) at (3,0) {\large $C_0$};
  3211. \node (x86-2) at (3,-2) {\large $\text{x86}^{*}$};
  3212. \node (x86-3) at (6,-2) {\large $\text{x86}^{*}$};
  3213. \node (x86-4) at (9,-2) {\large $\text{x86}^{*}$};
  3214. \node (x86-5) at (12,-2) {\large $\text{x86}$};
  3215. \node (x86-6) at (12,-4) {\large $\text{x86}^{\dagger}$};
  3216. \node (x86-2-1) at (3,-4) {\large $\text{x86}^{*}$};
  3217. \node (x86-2-2) at (6,-4) {\large $\text{x86}^{*}$};
  3218. \path[->,bend left=15] (R1) edge [above] node {\ttfamily\footnotesize typecheck} (R1-2);
  3219. \path[->,bend left=15] (R1-2) edge [above] node {\ttfamily\footnotesize uniquify} (R1-3);
  3220. \path[->,bend left=15] (R1-3) edge [right] node {\ttfamily\footnotesize flatten} (C0-1);
  3221. \path[->,bend right=15] (C0-1) edge [left] node {\ttfamily\footnotesize select-instr.} (x86-2);
  3222. \path[->,bend left=15] (x86-2) edge [right] node {\ttfamily\footnotesize uncover-live} (x86-2-1);
  3223. \path[->,bend right=15] (x86-2-1) edge [below] node {\ttfamily\footnotesize build-inter.} (x86-2-2);
  3224. \path[->,bend right=15] (x86-2-2) edge [right] node {\ttfamily\footnotesize allocate-reg.} (x86-3);
  3225. \path[->,bend left=15] (x86-3) edge [above] node {\ttfamily\footnotesize lower-cond.} (x86-4);
  3226. \path[->,bend left=15] (x86-4) edge [above] node {\ttfamily\footnotesize patch-instr.} (x86-5);
  3227. \path[->,bend right=15] (x86-5) edge [left] node {\ttfamily\footnotesize print-x86} (x86-6);
  3228. \end{tikzpicture}
  3229. \caption{Diagram of the passes for compiling $R_2$, including the
  3230. new type checking pass.}
  3231. \label{fig:R2-passes}
  3232. \end{figure}
  3233. Figure~\ref{fig:R2-passes} gives an overview of all the passes needed
  3234. for the compilation of $R_2$.
  3235. \section{Challenge: Optimizing Conditions$^{*}$}
  3236. \label{sec:opt-if}
  3237. A close inspection of the x86 code generated in
  3238. Figure~\ref{fig:if-example-x86} reveals some redundant computation
  3239. regarding the condition of the \key{if}. We compare \key{rcx} to $1$
  3240. twice using \key{cmpq} as follows.
  3241. \begin{lstlisting}
  3242. cmpq $1, %rcx
  3243. sete %al
  3244. movzbq %al, %rcx
  3245. cmpq $1, %rcx
  3246. je then21288
  3247. \end{lstlisting}
  3248. The reason for this non-optimal code has to do with the \code{flatten}
  3249. pass earlier in this Chapter. We recommended flattening the condition
  3250. to an $\Arg$ and then comparing with \code{\#t}. But if the condition
  3251. is already an \code{eq?} test, then we would like to use that
  3252. directly. In fact, for many of the expressions of Boolean type, we can
  3253. generate more optimized code. For example, if the condition is
  3254. \code{\#t} or \code{\#f}, we do not need to generate an \code{if} at
  3255. all. If the condition is a \code{let}, we can optimize based on the
  3256. form of its body. If the condition is a \code{not}, then we can flip
  3257. the two branches.
  3258. %
  3259. \marginpar{\tiny We could do even better by converting to basic
  3260. blocks.\\ --Jeremy}
  3261. %
  3262. On the other hand, if the condition is a \code{and}
  3263. or another \code{if}, we should flatten them into an $\Arg$ to avoid
  3264. code duplication.
  3265. Figure~\ref{fig:opt-if} shows an example program and the result of
  3266. applying the above suggested optimizations.
  3267. \begin{exercise}\normalfont
  3268. Change the \code{flatten} pass to improve the code that gets
  3269. generated for \code{if} expressions. We recommend writing a helper
  3270. function that recursively traverses the condition of the \code{if}.
  3271. \end{exercise}
  3272. \begin{figure}[tbp]
  3273. \begin{tabular}{lll}
  3274. \begin{minipage}{0.5\textwidth}
  3275. \begin{lstlisting}
  3276. (program
  3277. (if (let ([x 1])
  3278. (not (eq? 2 x)))
  3279. 42
  3280. 777))
  3281. \end{lstlisting}
  3282. $\Downarrow$
  3283. \begin{lstlisting}
  3284. (program (x.1 t.1 if.1)
  3285. (assign x.1 1)
  3286. (assign t.1 (read))
  3287. (if (eq? x.1 t.1)
  3288. ((assign if.1 42))
  3289. ((assign if.1 777)))
  3290. (return if.1))
  3291. \end{lstlisting}
  3292. $\Downarrow$
  3293. \begin{lstlisting}
  3294. (program (x.1 t.1 if.1)
  3295. (movq (int 1) (var x.1))
  3296. (callq read_int)
  3297. (movq (reg rax) (var t.1))
  3298. (if (eq? (var x.1) (var t.1))
  3299. ((movq (int 42) (var if.1)))
  3300. ((movq (int 777) (var if.1))))
  3301. (movq (var if.1) (reg rax)))
  3302. \end{lstlisting}
  3303. \end{minipage}
  3304. &
  3305. $\Rightarrow$
  3306. \begin{minipage}{0.4\textwidth}
  3307. \begin{lstlisting}
  3308. .globl _main
  3309. _main:
  3310. pushq %rbp
  3311. movq %rsp, %rbp
  3312. pushq %r15
  3313. pushq %r14
  3314. pushq %r13
  3315. pushq %r12
  3316. pushq %rbx
  3317. subq $8, %rsp
  3318. movq $1, %rbx
  3319. callq _read_int
  3320. movq %rax, %rcx
  3321. cmpq %rbx, %rcx
  3322. je then21288
  3323. movq $777, %r12
  3324. jmp if_end21289
  3325. then21288:
  3326. movq $42, %r12
  3327. if_end21289:
  3328. movq %r12, %rax
  3329. movq %rax, %rdi
  3330. callq _print_int
  3331. movq $0, %rax
  3332. addq $8, %rsp
  3333. popq %rbx
  3334. popq %r12
  3335. popq %r13
  3336. popq %r14
  3337. popq %r15
  3338. popq %rbp
  3339. retq
  3340. \end{lstlisting}
  3341. \end{minipage}
  3342. \end{tabular}
  3343. \caption{Example program with optimized conditionals.}
  3344. \label{fig:opt-if}
  3345. \end{figure}
  3346. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3347. \chapter{Tuples and Garbage Collection}
  3348. \label{ch:tuples}
  3349. In this chapter we study the compilation of mutable tuples (called
  3350. ``vectors'' in Racket). Figure~\ref{fig:r3-syntax} defines the syntax
  3351. for $R_3$, which includes three new forms for creating a tuple,
  3352. reading an element of a tuple, and writing an element into a
  3353. tuple. The following program shows the usage of tuples in Racket. We
  3354. create a 3-tuple \code{t} and a 1-tuple. The 1-tuple is stored at
  3355. index $2$ of the 3-tuple, showing that tuples are first-class values.
  3356. The element at index $1$ of \code{t} is \code{\#t}, so the ``then''
  3357. branch is taken. The element at index $0$ of \code{t} is $40$, to
  3358. which we add the $2$, the element at index $0$ of the 1-tuple.
  3359. \begin{lstlisting}
  3360. (let ([t (vector 40 #t (vector 2))])
  3361. (if (vector-ref t 1)
  3362. (+ (vector-ref t 0)
  3363. (vector-ref (vector-ref t 2) 0))
  3364. 44))
  3365. \end{lstlisting}
  3366. Figure~\ref{fig:interp-R3} shows the interpreter for the $R_3$
  3367. language. With the addition of the vector operations, there are quite
  3368. a few primitive operations and the interpreter code for them is
  3369. somewhat repetative. In Figure~\ref{fig:interp-R3} we factor out the
  3370. different parts into the \code{interp-op} function and the similar
  3371. parts into the one match clause shown in
  3372. Figure~\ref{fig:interp-R3}. It is important for that match clause to
  3373. come last because it matches \emph{any} compound S-expression. We do
  3374. not use \code{interp-op} for the \code{and} operation because of the
  3375. short-circuiting behavior in the order of evaluation of its arguments.
  3376. \begin{figure}[tbp]
  3377. \begin{lstlisting}
  3378. (define (interp-op op)
  3379. (match op
  3380. ['+ fx+]
  3381. ['- (lambda (n) (fx- 0 n))]
  3382. ['read read-fixnum]
  3383. ['not (lambda (v) (match v [#t #f] [#f #t]))]
  3384. ['eq? (lambda (v1 v2)
  3385. (cond [(or (and (fixnum? v1) (fixnum? v2))
  3386. (and (boolean? v1) (boolean? v2))
  3387. (and (vector? v1) (vector? v2)))
  3388. (eq? v1 v2)]))]
  3389. ['vector vector]
  3390. ['vector-ref vector-ref]
  3391. ['vector-set! vector-set!]
  3392. [else (error "in interp-op S0, unmatched" op)]))
  3393. (define (interp-R3 env e)
  3394. (match e
  3395. ...
  3396. [`(,op ,args ...)
  3397. (apply (interp-op op)
  3398. (map (lambda (e) (interp-R3 env e)) args))]
  3399. ))
  3400. \end{lstlisting}
  3401. \caption{Interpreter for the $R_3$ language. We combine the code for
  3402. most of the primitive operations, including the new vector
  3403. operations, into the one match clause and factor their differences
  3404. into the \code{interp-op} function. }
  3405. \label{fig:interp-R3}
  3406. \end{figure}
  3407. \begin{figure}[tbp]
  3408. \centering
  3409. \fbox{
  3410. \begin{minipage}{0.96\textwidth}
  3411. \[
  3412. \begin{array}{lcl}
  3413. \Type &::=& \ldots \mid (\key{Vector}\;\Type^{+}) \\
  3414. \Exp &::=& \ldots \mid (\key{vector}\;\Exp^{+}) \mid
  3415. (\key{vector-ref}\;\Exp\;\Int) \\
  3416. &\mid& (\key{vector-set!}\;\Exp\;\Int\;\Exp)\\
  3417. R_3 &::=& (\key{program} \; \Exp)
  3418. \end{array}
  3419. \]
  3420. \end{minipage}
  3421. }
  3422. \caption{The $R_3$ language, an extension of $R_2$
  3423. (Figure~\ref{fig:r2-syntax}).}
  3424. \label{fig:r3-syntax}
  3425. \end{figure}
  3426. \begin{figure}[tbp]
  3427. \begin{lstlisting}
  3428. (define primitives (set '+ '- 'eq? 'not 'read
  3429. 'vector 'vector-ref 'vector-set!))
  3430. (define (interp-op op)
  3431. (match op
  3432. ['+ fx+]
  3433. ['- (lambda (n) (fx- 0 n))]
  3434. ['eq? (lambda (v1 v2)
  3435. (cond [(or (and (fixnum? v1) (fixnum? v2))
  3436. (and (boolean? v1) (boolean? v2))
  3437. (and (vector? v1) (vector? v2)))
  3438. (eq? v1 v2)]))]
  3439. ['not (lambda (v) (match v [#t #f] [#f #t]))]
  3440. ['read read-fixnum]
  3441. ['vector vector] ['vector-ref vector-ref]
  3442. ['vector-set! vector-set!]
  3443. [else (error 'interp-op "unknown operator")]))
  3444. (define (interp-R3 env)
  3445. (lambda (e)
  3446. (match e
  3447. ...
  3448. [`(,op ,args ...) #:when (set-member? primitives op)
  3449. (apply (interp-op op) (map (interp-R3 env) args))]
  3450. [else (error 'interp-R3 "unrecognized expression")]
  3451. )))
  3452. \end{lstlisting}
  3453. \caption{Interpreter for the $R_3$ language.}
  3454. \label{fig:interp-R3}
  3455. \end{figure}
  3456. Tuples are our first encounter with heap-allocated data, which raises
  3457. several interesting issues. First, variable binding performs a
  3458. shallow-copy when dealing with tuples, which means that different
  3459. variables can refer to the same tuple, i.e., the variables can be
  3460. \emph{aliases} for the same thing. Consider the following example in
  3461. which both \code{t1} and \code{t2} refer to the same tuple. Thus, the
  3462. mutation through \code{t2} is visible when referencing the tuple from
  3463. \code{t1} and the result of the program is therefore \code{42}.
  3464. \begin{lstlisting}
  3465. (let ([t1 (vector 3 7)])
  3466. (let ([t2 t1])
  3467. (let ([_ (vector-set! t2 0 42)])
  3468. (vector-ref t1 0))))
  3469. \end{lstlisting}
  3470. The next issue concerns the lifetime of tuples. Of course, they are
  3471. created by the \code{vector} form, but when does their lifetime end?
  3472. Notice that the grammar in Figure~\ref{fig:r3-syntax} does not include
  3473. an operation for deallocating tuples. Furthermore, the lifetime of a
  3474. tuple is not tied to any notion of static scoping. For example, the
  3475. following program returns \code{3} even though the variable \code{t}
  3476. goes out of scope prior to the reference.
  3477. \begin{lstlisting}
  3478. (vector-ref
  3479. (let ([t (vector 3 7)])
  3480. t)
  3481. 0)
  3482. \end{lstlisting}
  3483. From the perspective of programmer-oberservable behavior, tuples live
  3484. forever. Of course, if they really lived forever, then many programs
  3485. would run out of memory.\footnote{The $R_3$ language does not have
  3486. looping or recursive function, so it is nie impossible to write a
  3487. program in $R_3$ that will run out of memory. However, we add
  3488. recursive functions in the next Chapter!} A Racket implementation
  3489. must therefore perform automatic garbage collection.
  3490. \section{Garbage Collection}
  3491. \label{sec:GC}
  3492. \begin{figure}[tbp]
  3493. \includegraphics[width=0.5\textwidth]{CopyingCollector}
  3494. \includegraphics[width=0.5\textwidth]{CopyCollector2}
  3495. \caption{A copying collector in action.}
  3496. \label{fig:copying-collector}
  3497. \end{figure}
  3498. \cite{Wilson:1992fk}
  3499. Figure~\ref{fig:copying-collector}
  3500. \section{Integrating Garbage Collection}
  3501. \label{sec:integratng-GC}
  3502. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3503. \chapter{Functions}
  3504. \label{ch:functions}
  3505. This chapter studies the compilation of functions (aka. procedures) at
  3506. the level of abstraction of the C language. The syntax for function
  3507. definitions and function application (aka. function call) is shown in
  3508. Figure~\ref{fig:r4-syntax}, where we define the $R_4$ language.
  3509. Programs in $R_4$ start with zero or more function definitions. The
  3510. function names from these definitions are in-scope for the entire
  3511. program, including all other function definitions (so the ordering of
  3512. function definitions does not matter).
  3513. Functions are first-class in the sense that a function pointer is data
  3514. and can be stored in memory or passed as a parameter to another
  3515. function. Thus, we introduce a function type, written
  3516. \begin{lstlisting}
  3517. (|$\Type_1$| |$\cdots$| |$\Type_n$| -> |$\Type_r$|)
  3518. \end{lstlisting}
  3519. for a function whose $n$ parameters have the types $\Type_1$ through
  3520. $\Type_n$ and whose return type is $\Type_r$. The main limitation of
  3521. these functions (with respect to Racket functions) is that they are
  3522. not lexically scoped. That is, the only external entities that can be
  3523. referenced from inside a function body are other globally-defined
  3524. functions. The syntax of $R_4$ prevents functions from being nested
  3525. inside each other; they can only be defined at the top level.
  3526. \begin{figure}[tbp]
  3527. \centering
  3528. \fbox{
  3529. \begin{minipage}{0.96\textwidth}
  3530. \[
  3531. \begin{array}{lcl}
  3532. \Type &::=& \ldots \mid (\Type^{*} \; \key{->}\; \Type) \\
  3533. \Exp &::=& \ldots \mid (\Exp \; \Exp^{*}) \\
  3534. \Def &::=& (\key{define}\; (\Var \; [\Var \key{:} \Type]^{*} \key{:} \Type \; \Exp)) \\
  3535. R_4 &::=& (\key{program} \; \Def^{*} \; \Exp)
  3536. \end{array}
  3537. \]
  3538. \end{minipage}
  3539. }
  3540. \caption{The $R_4$ language, an extension of $R_3$
  3541. (Figure~\ref{fig:r3-syntax}).}
  3542. \label{fig:r4-syntax}
  3543. \end{figure}
  3544. The program in Figure~\ref{fig:r4-function-example} is a
  3545. representative example of defining and using functions in $R_4$. We
  3546. define a function \code{map-vec} that applies some other function
  3547. \code{f} to both elements of a vector (a 2-tuple) and returns a new
  3548. vector containing the results. We also define a function \code{add1}
  3549. that does what its name suggests. The program then applies
  3550. \code{map-vec} to \code{add1} and \code{(vector 0 41)}. The result is
  3551. \code{(vector 1 42)}, from which we return the \code{42}.
  3552. \begin{figure}[tbp]
  3553. \begin{lstlisting}
  3554. (program
  3555. (defines
  3556. (define (map-vec [f : (Integer -> Integer)]
  3557. [v : (Vector Integer Integer)])
  3558. : (Vector Integer Integer)
  3559. (vector (f (vector-ref v 0)) (f (vector-ref v 1))))
  3560. (define (add1 [x : Integer]) : Integer
  3561. (+ x 1))
  3562. (vector-ref (map-vec add1 (vector 0 41)) 1)
  3563. )
  3564. \end{lstlisting}
  3565. \caption{Example of using functions in $R_4$.}
  3566. \label{fig:r4-function-example}
  3567. \end{figure}
  3568. \marginpar{\scriptsize to do: interpreter for $R_4$. \\ --Jeremy}
  3569. \section{Functions in x86}
  3570. \label{sec:fun-x86}
  3571. The x86 architecture provides a few features to support the
  3572. implementation of functions. We have already seen that x86 provides
  3573. labels so that one can refer to the location of an instruction, as is
  3574. needed for jump instructions. Labels can also be used to mark the
  3575. beginning of the instructions for a function. Going further, we can
  3576. obtain the address of a label by using the \key{leaq} instruction and
  3577. \key{rip}-relative addressing. For example, the following puts the
  3578. address of the \code{add1} label into the \code{rbx} register.
  3579. \begin{lstlisting}
  3580. leaq add1(%rip), %rbx
  3581. \end{lstlisting}
  3582. In Sections~\ref{sec:x86} and \ref{sec:select-s0} we saw the use of
  3583. the \code{callq} instruction for jumping to a function as specified by
  3584. a label. The use of the instruction changes slightly if the function
  3585. is specified by an address in a register, that is, an \emph{indirect
  3586. function call}. The x86 syntax is to give the register name prefixed
  3587. with an asterisk.
  3588. \begin{lstlisting}
  3589. callq *%rbx
  3590. \end{lstlisting}
  3591. The x86 architecture does not directly support passing arguments to
  3592. functions; instead we use a combination of registers and stack
  3593. locations for passing arguments, following the conventions used by
  3594. \code{gcc} as described by \cite{Matz:2013aa}. Up to six arguments may
  3595. be passed in registers, using the registers \code{rdi}, \code{rsi},
  3596. \code{rdx}, \code{rcx}, \code{r8}, and \code{r9}, in that order. If
  3597. there are more than six arguments, then the rest must be placed on the
  3598. stack, which we call \emph{stack arguments}, which we discuss in later
  3599. paragraphs. The register \code{rax} is for the return value of the
  3600. function.
  3601. Recall from Section~\ref{sec:x86} that the stack is also used for
  3602. local variables, and that at the beginning of a function we move the
  3603. stack pointer \code{rsp} down to make room for them. To make
  3604. additional room for passing arguments, we shall move the stack pointer
  3605. even further down. We count how many stack arguments are needed for
  3606. each function call that occurs inside the body of the function and
  3607. find their maximum. Adding this number to the number of local variables
  3608. gives us how much the \code{rsp} should be moved at the beginning of
  3609. the function. In preparation for a function call, we offset from
  3610. \code{rsp} to set up the stack arguments. We put the first stack
  3611. argument in \code{0(\%rsp)}, the second in \code{8(\%rsp)}, and so on.
  3612. Upon calling the function, the stack arguments are retrieved by the
  3613. callee using the base pointer \code{rbp}. The address \code{16(\%rbp)}
  3614. is the location of the first stack argument, \code{24(\%rbp)} is the
  3615. address of the second, and so on. Figure~\ref{fig:call-frames} shows
  3616. the layout of the caller and callee frames. Notice how important it is
  3617. that we correctly compute the maximum number of arguments needed for
  3618. function calls; if that number is too small then the arguments and
  3619. local variables will smash into each other!
  3620. As discussed in Section~\ref{sec:print-x86-reg-alloc}, an x86 function
  3621. is responsible for following conventions regarding the use of
  3622. registers: the caller should assume that all the caller save registers
  3623. get overwritten with arbitrary values by the callee. Thus, the caller
  3624. should either 1) not put values that are live across a call in caller
  3625. save registers, or 2) save and restore values that are live across
  3626. calls. We shall recommend option 1). On the flip side, if the callee
  3627. wants to use a callee save register, the callee must arrange to put
  3628. the original value back in the register prior to returning to the
  3629. caller.
  3630. \begin{figure}[tbp]
  3631. \centering
  3632. \begin{tabular}{r|r|l|l} \hline
  3633. Caller View & Callee View & Contents & Frame \\ \hline
  3634. 8(\key{\%rbp}) & & return address & \multirow{5}{*}{Caller}\\
  3635. 0(\key{\%rbp}) & & old \key{rbp} \\
  3636. -8(\key{\%rbp}) & & variable $1$ \\
  3637. \ldots & & \ldots \\
  3638. $-8k$(\key{\%rbp}) & & variable $k$ \\
  3639. & & \\
  3640. $8n-8$\key{(\%rsp)} & $8n+8$(\key{\%rbp})& argument $n$ \\
  3641. & \ldots & \ldots \\
  3642. 0\key{(\%rsp)} & 16(\key{\%rbp}) & argument $1$ & \\ \hline
  3643. & 8(\key{\%rbp}) & return address & \multirow{5}{*}{Callee}\\
  3644. & 0(\key{\%rbp}) & old \key{rbp} \\
  3645. & -8(\key{\%rbp}) & variable $1$ \\
  3646. & \ldots & \ldots \\
  3647. & $-8m$(\key{\%rsp}) & variable $m$\\ \hline
  3648. \end{tabular}
  3649. \caption{Memory layout of caller and callee frames.}
  3650. \label{fig:call-frames}
  3651. \end{figure}
  3652. \section{The compilation of functions}
  3653. Now that we have a good understanding of functions as they appear in
  3654. $R_4$ and the support for functions in x86, we need to plan the
  3655. changes to our compiler, that is, do we need any new passes and/or do
  3656. we need to change any existing passes? Also, do we need to add new
  3657. kinds of AST nodes to any of the intermediate languages?
  3658. To begin with, the syntax of $R_4$ is inconvenient for purposes of
  3659. compilation because it conflates the use of function names and local
  3660. variables and it conflates the application of primitive operations and
  3661. the application of functions. This is a problem because we need to
  3662. compile the use of a function name differently than the use of a local
  3663. variable; we need to use \code{leaq} to move the function name to a
  3664. register. Similarly, the application of a function is going to require
  3665. a complex sequence of instructions, unlike the primitive
  3666. operations. Thus, it is a good idea to create a new pass that changes
  3667. function references from just a symbol $f$ to \code{(function-ref
  3668. $f$)} and that changes function application from \code{($e_0$ $e_1$
  3669. $\ldots$ $e_n$)} to the explicitly tagged AST \code{(app $e_0$ $e_1$
  3670. $\ldots$ $e_n$)}. A good name for this pass is
  3671. \code{reveal-functions}. Placing this pass after \code{uniquify} is a
  3672. good idea, because it will make sure that there are no local variables
  3673. and functions that share the same name. On the other hand,
  3674. \code{reveal-functions} needs to come before the \code{flatten} pass
  3675. because \code{flatten} will help us compiler \code{function-ref}.
  3676. Because each \code{function-ref} needs to eventually become an
  3677. \code{leaq} instruction, it first needs to become an assignment
  3678. statement so there is a left-hand side in which to put the
  3679. result. This can be handled easily in the \code{flatten} pass by
  3680. categorizing \code{function-ref} as a complex expression. Then, in
  3681. the \code{select-instructions} pass, an assignment of
  3682. \code{function-ref} becomes a \code{leaq} instruction as follows: \\
  3683. \begin{tabular}{lll}
  3684. \begin{minipage}{0.45\textwidth}
  3685. \begin{lstlisting}
  3686. (assign |$\itm{lhs}$| (function-ref |$f$|))
  3687. \end{lstlisting}
  3688. \end{minipage}
  3689. &
  3690. $\Rightarrow$
  3691. &
  3692. \begin{minipage}{0.4\textwidth}
  3693. \begin{lstlisting}
  3694. (leaq (function-ref |$f$|) |$\itm{lhs}$|)
  3695. \end{lstlisting}
  3696. \end{minipage}
  3697. \end{tabular}
  3698. Next we consider compiling function definitions. The \code{flatten}
  3699. pass should handle function definitions a lot like a \code{program}
  3700. node; after all, the \code{program} node represents the \code{main}
  3701. function. So the \code{flatten} pass, in addition to flattening the
  3702. body of the function into a sequence of statements, should record the
  3703. local variables in the $\Var^{*}$ field as shown below.
  3704. \begin{lstlisting}
  3705. (define (|$f$| [|\itm{xs}| : |\itm{ts}|]|$^{*}$|) : |\itm{rt}| (|$\Var^{*}$|) |$\Stmt^{+}$|)
  3706. \end{lstlisting}
  3707. In the \code{select-instructions} pass, we need to encode the
  3708. parameter passing in terms of the conventions discussed in
  3709. Section~\ref{sec:fun-x86}. So depending on the length of the parameter
  3710. list \itm{xs}, some of them may be in registers and some of them may
  3711. be on the stack. I recommend generating \code{movq} instructions to
  3712. move the parameters from their registers and stack locations into the
  3713. variables \itm{xs}, then let register allocation handle the assignment
  3714. of those variables to homes. After this pass, the \itm{xs} can be
  3715. added to the list of local variables. As mentioned in
  3716. Section~\ref{sec:fun-x86}, we need to find out how far to move the
  3717. stack pointer to ensure we have enough space for stack arguments in
  3718. all the calls inside the body of this function. This pass is a good
  3719. place to do this and store the result in the \itm{maxStack} field of
  3720. the output \code{define} shown below.
  3721. \begin{lstlisting}
  3722. (define (|$f$|) |\itm{numParams}| (|$\Var^{*}$| |\itm{maxStack}|) |$\Instr^{+}$|)
  3723. \end{lstlisting}
  3724. Next, consider the compilation of function applications, which have
  3725. the following form at the start of \code{select-instructions}.
  3726. \begin{lstlisting}
  3727. (assign |\itm{lhs}| (app |\itm{fun}| |\itm{args}| |$\ldots$|))
  3728. \end{lstlisting}
  3729. In the mirror image of handling the parameters of function
  3730. definitions, some of the arguments \itm{args} need to be moved to the
  3731. argument passing registers and the rest should be moved to the
  3732. appropriate stack locations, as discussed in
  3733. Section~\ref{sec:fun-x86}. You might want to introduce a new kind of
  3734. AST node for stack arguments, \code{(stack-arg $i$)} where $i$ is the
  3735. index of this argument with respect to the other stack arguments. As
  3736. you're generate this code for parameter passing, take note of how many
  3737. stack arguments are needed for purposes of computing the
  3738. \itm{maxStack} discussed above.
  3739. Once the instructions for parameter passing have been generated, the
  3740. function call itself can be performed with an indirect function call,
  3741. for which I recommend creating the new instruction
  3742. \code{indirect-callq}. Of course, the return value from the function
  3743. is stored in \code{rax}, so it needs to be moved into the \itm{lhs}.
  3744. \begin{lstlisting}
  3745. (indirect-callq |\itm{fun}|)
  3746. (movq (reg rax) |\itm{lhs}|)
  3747. \end{lstlisting}
  3748. The rest of the passes need only minor modifications to handle the new
  3749. kinds of AST nodes: \code{function-ref}, \code{indirect-callq}, and
  3750. \code{leaq}. Inside \code{uncover-live}, when computing the $W$ set
  3751. (written variables) for an \code{indirect-callq} instruction, I
  3752. recommend including all the caller save registers, which will have the
  3753. affect of making sure that no caller save register actually need to be
  3754. saved. In \code{patch-instructions}, you should deal with the x86
  3755. idiosyncrasy that the destination argument of \code{leaq} must be a
  3756. register.
  3757. For the \code{print-x86} pass, I recommend the following translations:
  3758. \begin{lstlisting}
  3759. (function-ref |\itm{label}|) |$\Rightarrow$| |\itm{label}|(%rip)
  3760. (indirect-callq |\itm{arg}|) |$\Rightarrow$| callq *|\itm{arg}|
  3761. (stack-arg |$i$|) |$\Rightarrow$| |$i$|(%rsp)
  3762. \end{lstlisting}
  3763. For function definitions, the \code{print-x86} pass should add the
  3764. code for saving and restoring the callee save registers, if you
  3765. haven't already done that.
  3766. \section{An Example Translation}
  3767. Figure~\ref{fig:add-fun} shows an example translation of a simple
  3768. function in $R_4$ to x86. The figure includes the results of the
  3769. \code{flatten} and \code{select-instructions} passes. Can you see any
  3770. obvious ways to improve the translation?
  3771. \begin{figure}[tbp]
  3772. \begin{tabular}{lll}
  3773. \begin{minipage}{0.5\textwidth}
  3774. \begin{lstlisting}
  3775. (program
  3776. (define (add [x : Integer]
  3777. [y : Integer])
  3778. : Integer (+ x y))
  3779. (add 40 2))
  3780. \end{lstlisting}
  3781. $\Downarrow$
  3782. \begin{lstlisting}
  3783. (program (t.1 t.2)
  3784. (defines
  3785. (define (add.1 [x.1 : Integer]
  3786. [y.1 : Integer])
  3787. : Integer (t.3)
  3788. (assign t.3 (+ x.1 y.1))
  3789. (return t.3)))
  3790. (assign t.1 (function-ref add.1))
  3791. (assign t.2 (app t.1 40 2))
  3792. (return t.2))
  3793. \end{lstlisting}
  3794. $\Downarrow$
  3795. \begin{lstlisting}
  3796. (program ((t.1 t.2) 0)
  3797. ((define (add.1) 2 ((x.1 y.1 t.3) 0)
  3798. (movq (reg rdi) (var x.1))
  3799. (movq (reg rsi) (var y.1))
  3800. (movq (var x.1) (var t.3))
  3801. (addq (var y.1) (var t.3))
  3802. (movq (var t.3) (reg rax))))
  3803. (leaq (function-ref add.1) (var t.1))
  3804. (movq (int 40) (reg rdi))
  3805. (movq (int 2) (reg rsi))
  3806. (indirect-callq (var t.1))
  3807. (movq (reg rax) (var t.2))
  3808. (movq (var t.2) (reg rax)))
  3809. \end{lstlisting}
  3810. \end{minipage}
  3811. &
  3812. \begin{minipage}{0.4\textwidth}
  3813. $\Downarrow$
  3814. \begin{lstlisting}
  3815. .globl add_1
  3816. add_1:
  3817. pushq %rbp
  3818. movq %rsp, %rbp
  3819. pushq %r15
  3820. pushq %r14
  3821. pushq %r13
  3822. pushq %r12
  3823. pushq %rbx
  3824. subq $16, %rsp
  3825. movq %rdi, %rbx
  3826. movq %rsi, %rcx
  3827. addq %rcx, %rbx
  3828. movq %rbx, %rax
  3829. addq $16, %rsp
  3830. popq %rbx
  3831. popq %r12
  3832. popq %r13
  3833. popq %r14
  3834. popq %r15
  3835. popq %rbp
  3836. retq
  3837. .globl _main
  3838. _main:
  3839. pushq %rbp
  3840. movq %rsp, %rbp
  3841. subq $16, %rsp
  3842. leaq add_1(%rip), %rbx
  3843. movq $40, %rdi
  3844. movq $2, %rsi
  3845. callq *%rbx
  3846. movq %rax, %rbx
  3847. movq %rbx, %rax
  3848. addq $16, %rsp
  3849. popq %rbp
  3850. retq
  3851. \end{lstlisting}
  3852. \end{minipage}
  3853. \end{tabular}
  3854. \caption{Example compilation of a simple function to x86.}
  3855. \label{fig:add-fun}
  3856. \end{figure}
  3857. \begin{exercise}\normalfont
  3858. Expand your compiler to handle $R_4$ as outlined in this section.
  3859. Create 5 new programs that use functions, including examples that pass
  3860. functions and return functions from other functions, and test your
  3861. compiler on these new programs and all of your previously created test
  3862. programs.
  3863. \end{exercise}
  3864. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3865. \chapter{Lexically Scoped Functions}
  3866. \label{ch:lambdas}
  3867. \begin{figure}[tbp]
  3868. \centering
  3869. \fbox{
  3870. \begin{minipage}{0.96\textwidth}
  3871. \[
  3872. \begin{array}{lcl}
  3873. \Exp &::=& \ldots \mid (\key{lambda:}\; ([\Var \key{:} \Type]^{*} \key{:} \Type \; \Exp)) \\
  3874. R_5 &::=& (\key{program} \; \Def^{*} \; \Exp)
  3875. \end{array}
  3876. \]
  3877. \end{minipage}
  3878. }
  3879. \caption{The $R_5$ language, an extension of $R_4$
  3880. (Figure~\ref{fig:r4-syntax}).}
  3881. \label{fig:r5-syntax}
  3882. \end{figure}
  3883. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3884. %\chapter{Mutable Data}
  3885. %\label{ch:mutable-data}
  3886. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3887. \chapter{Dynamic Typing}
  3888. \label{ch:type-dynamic}
  3889. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3890. \chapter{Parametric Polymorphism}
  3891. \label{ch:parametric-polymorphism}
  3892. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3893. \chapter{High-level Optimization}
  3894. \label{ch:high-level-optimization}
  3895. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3896. \chapter{Appendix}
  3897. \section{Interpreters}
  3898. \label{appendix:interp}
  3899. We provide several interpreters in the \key{interp.rkt} file. The
  3900. \key{interp-scheme} function takes an AST in one of the Racket-like
  3901. languages considered in this book ($R_1, R_2, \ldots$) and interprets
  3902. the program, returning the result value. The \key{interp-C} function
  3903. interprets an AST for a program in one of the C-like languages ($C_0,
  3904. C_1, \ldots$), and the \code{interp-x86} function interprets an AST
  3905. for an x86 program.
  3906. \section{Utility Functions}
  3907. \label{appendix:utilities}
  3908. The utility function described in this section can be found in the
  3909. \key{utilities.rkt} file.
  3910. The \key{read-program} function takes a file path and parses that file
  3911. (it must be a Racket program) into an abstract syntax tree (as an
  3912. S-expression) with a \key{program} AST at the top.
  3913. The \key{assert} function displays the error message \key{msg} if the
  3914. Boolean \key{bool} is false.
  3915. \begin{lstlisting}
  3916. (define (assert msg bool) ...)
  3917. \end{lstlisting}
  3918. The \key{lookup} function ...
  3919. The \key{map2} function ...
  3920. \subsection{Graphs}
  3921. \begin{itemize}
  3922. \item The \code{make-graph} function takes a list of vertices
  3923. (symbols) and returns a graph.
  3924. \item The \code{add-edge} function takes a graph and two vertices and
  3925. adds an edge to the graph that connects the two vertices. The graph
  3926. is updated in-place. There is no return value for this function.
  3927. \item The \code{adjacent} function takes a graph and a vertex and
  3928. returns the set of vertices that are adjacent to the given
  3929. vertex. The return value is a Racket \code{hash-set} so it can be
  3930. used with functions from the \code{racket/set} module.
  3931. \item The \code{vertices} function takes a graph and returns the list
  3932. of vertices in the graph.
  3933. \end{itemize}
  3934. \subsection{Testing}
  3935. The \key{interp-tests} function takes a compiler name (a string), a
  3936. description of the passes, an interpreter for the source language, a
  3937. test family name (a string), and a list of test numbers, and runs the
  3938. compiler passes and the interpreters to check whether the passes
  3939. correct. The description of the passes is a list with one entry per
  3940. pass. An entry is a list with three things: a string giving the name
  3941. of the pass, the function that implements the pass (a translator from
  3942. AST to AST), and a function that implements the interpreter (a
  3943. function from AST to result value) for the language of the output of
  3944. the pass. The interpreters from Appendix~\ref{appendix:interp} make a
  3945. good choice. The \key{interp-tests} function assumes that the
  3946. subdirectory \key{tests} has a bunch of Scheme programs whose names
  3947. all start with the family name, followed by an underscore and then the
  3948. test number, ending in \key{.scm}. Also, for each Scheme program there
  3949. is a file with the same number except that it ends with \key{.in} that
  3950. provides the input for the Scheme program.
  3951. \begin{lstlisting}
  3952. (define (interp-tests name passes test-family test-nums) ...
  3953. \end{lstlisting}
  3954. The compiler-tests function takes a compiler name (a string) a
  3955. description of the passes (see the comment for \key{interp-tests}) a
  3956. test family name (a string), and a list of test numbers (see the
  3957. comment for interp-tests), and runs the compiler to generate x86 (a
  3958. \key{.s} file) and then runs gcc to generate machine code. It runs
  3959. the machine code and checks that the output is 42.
  3960. \begin{lstlisting}
  3961. (define (compiler-tests name passes test-family test-nums) ...)
  3962. \end{lstlisting}
  3963. The compile-file function takes a description of the compiler passes
  3964. (see the comment for \key{interp-tests}) and returns a function that,
  3965. given a program file name (a string ending in \key{.scm}), applies all
  3966. of the passes and writes the output to a file whose name is the same
  3967. as the program file name but with \key{.scm} replaced with \key{.s}.
  3968. \begin{lstlisting}
  3969. (define (compile-file passes)
  3970. (lambda (prog-file-name) ...))
  3971. \end{lstlisting}
  3972. \bibliographystyle{plainnat}
  3973. \bibliography{all}
  3974. \end{document}
  3975. %% LocalWords: Dybvig Waddell Abdulaziz Ghuloum Dipanwita Sussman
  3976. %% LocalWords: Sarkar lcl Matz aa representable Chez Ph Dan's nano
  3977. %% LocalWords: fk bh Siek plt uq Felleisen Bor Yuh ASTs AST Naur eq
  3978. %% LocalWords: BNF fixnum datatype arith prog backquote quasiquote
  3979. %% LocalWords: ast sexp Reynold's reynolds interp cond fx evaluator
  3980. %% LocalWords: quasiquotes pe nullary unary rcl env lookup gcc rax
  3981. %% LocalWords: addq movq callq rsp rbp rbx rcx rdx rsi rdi subq nx
  3982. %% LocalWords: negq pushq popq retq globl Kernighan uniquify lll ve
  3983. %% LocalWords: allocator gensym alist subdirectory scm rkt tmp lhs
  3984. %% LocalWords: runtime Liveness liveness undirected Balakrishnan je
  3985. %% LocalWords: Rosen DSATUR SDO Gebremedhin Omari morekeywords cnd
  3986. %% LocalWords: fullflexible vertices Booleans Listof Pairof thn els
  3987. %% LocalWords: boolean typecheck notq cmpq sete movzbq jmp al
  3988. %% LocalWords: EFLAGS thns elss elselabel endlabel Tuples tuples os
  3989. %% LocalWords: tuple args lexically leaq Polymorphism msg bool nums
  3990. %% LocalWords: macosx unix Cormen vec callee xs maxStack numParams
  3991. %% LocalWords: arg