Modern Compiler Design
Dick Grune • Kees van Reeuwijk • Henri E. BalModern Compiler DesignSecond EditionCeriel J.H. Jacobs • Koen Langendoen
Dick Grune
Vrije Universiteit
Amsterdam, The Netherlands
Kees van Reeuwijk
Vrije Universiteit
Amsterdam, The Netherlands
Henri E. Bal
Vrije Universiteit
Amsterdam, The Netherlands
Ceriel J.H. Jacobs
Vrije Universiteit
Amsterdam, The Netherlands
Koen Langendoen
Delft University of Technology
Delft, The Netherlands
Additional material to this book can be downloaded from http://extras.springer.com.
-9
er:
2012941168
ISBN 978-1-4614-4699-
6 (eBook)
-
1 4614
4698
1 4614-4699-6
ISBN 978- -
DOI 10.1007/978- -
Springer New York Heidelberg Dordrecht London
Library of Congress Control Numb
© Springer Science+Business Media New York 2012
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of
the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or
information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar
methodology now known or hereafter developed. Exempted from this legal reservation are brief
excerpts in connection with reviews or scholarly analysis or material supplied specifically for the
purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the
work. Duplication of this publication or parts thereof is permitted only unde
r the provisions of the
Copyright Law of the Publisher’s location, in its current version, and permission for use must always
be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright
Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
The use of general descriptive names, registered names, trademarks, service marks, etc. in thi
does not imply, even in the absence of a specific statement, that such names are exemp
protective laws and regulations and therefore free for general use.
While the advice and information in this book are believed to be tru
e and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for
any errors or omissions that may be made. The publisher makes no warranty, express or implied, with
respect to the material contained herein.
Printed on acid-free paper
Springer is part of Springer Science+Business Media (www.springer.com)
s publication
t from the relevant
PrefaceTwelveyearshavepassedsincethefirsteditionofModernCompilerDesign.Formanycomputersciencesubjectsthiswouldbemorethanalifetime,butsincecom-pilerdesignisprobablythemostmaturecomputersciencesubject,itisdifferent.Anadultpersondevelopsmoreslowlyanddifferentlythanatoddlerorateenager,andsodoescompilerdesign.Thepresentbookreflectsthat.Improvementstothebookfallintotwogroups:presentationandcontent.The‘lookandfeel’ofthebookhasbeenmodernized,butmoreimportantlywehaverearrangedsignificantpartsofthebooktopresenttheminamorestructuredmanner:largechaptershavebeensplitandtheoptimizingcodegenerationtechniqueshavebeencollectedinaseparatechapter.Basedonreaderfeedbackandexperiencesinteachingfromthisbook,bothbyourselvesandothers,materialhasbeenexpanded,clarified,modified,ordeletedinalargenumberofplaces.Wehopethatasaresultofthisthereaderfeelsthatthebookdoesabetterjobofmakingcompilerdesignandconstructionaccessible.Thebookaddsnewmaterialtocoverthedevelopmentsincompilerdesignandconstructionoverthelasttwelveyears.Overallthestandardcompilingtechniquesandparadigmshavestoodthetestoftime,butstillnewandoftensurprisingopti-mizationtechniqueshavebeeninvented;existingoneshavebeenimproved;andoldoneshavegainedprominence.Examplesofthefirstare:proceduralabstraction,inwhichroutinesarerecognizedinthecodeandreplacedbyroutinecallstoreducesize;binaryrewriting,inwhichoptimizationsareappliedtothebinarycode;andjust-in-timecompilation,inwhichpartsofthecompilationaredelayedtoimprovetheperceivedspeedoftheprogram.Anexampleofthesecondisatechniquewhichextendsoptimalcodegenerationthroughexhaustivesearch,previouslyavailablefortinyblocksonly,tomoderate-sizebasicblocks.Andanexampleofthethirdistailrecursionremoval,indispensableforthecompilationoffunctionallanguages.ThesedevelopmentsaremainlydescribedinChapter9.Althoughsyntaxanalysisistheonebutoldestbranchofcompilerconstruction(lexicalanalysisbeingtheoldest),eveninthatareainnovationhastakenplace.Generalized(non-deterministic)LRparsing,developedbetween1984and1994,isnowusedincompilers.ItiscoveredinSection3.5.8.Newhardwarerequirementshavenecessitatednewcompilerdevelopments.Themainexamplesaretheneedforsizereductionoftheobjectcode,bothtofitthecodeintosmallembeddedsystemsandtoreducetransmissiontimes;andforlowerpowerv
viPrefaceconsumption,toextendbatterylifeandtoreduceelectricitybills.Dynamicmemoryallocationinembeddedsystemsrequiresabalancebetweenspeedandthrift,andthequestionishowcompilerdesigncanhelp.ThesesubjectsarecoveredinSections9.2,9.3,and10.2.8,respectively.Withagecomeslegacy.Thereismuchlegacycodearound,codewhichissooldthatitcannolongerbemodifiedandrecompiledwithreasonableeffort.Ifthesourcecodeisstillavailablebutthereisnocompileranymore,recompilationmuststartwithagrammarofthesourcecode.Forfiftyyearsprogrammersandcompilerdesignershaveusedgrammarstoproduceandanalyzeprograms;nowlargelegacyprogramsareusedtoproducegrammarsforthem.TherecoveryofthegrammarfromlegacysourcecodeisdiscussedinSection3.6.Ifjustthebinaryexecutableprogramisleft,itmustbedisassembledorevendecompiled.Forfiftyyearscom-pilerdesignershavebeencalledupontodesigncompilersandassemblerstoconvertsourceprogramstobinarycode;nowtheyarecalledupontodesigndisassemblersanddecompilers,torollbacktheassemblyandcompilationprocess.TherequiredtechniquesaretreatedinSections8.4and8.5.ThebibliographyTheliteraturelisthasbeenupdated,butitsusefulnessismorelimitedthanbefore,fortworeasons.Thefirstisthatbythetimeitappearsinprint,theInternetcanpro-videmoreup-to-dateandmoreto-the-pointinformation,inlargerquantities,thanaprintedtextcanhopetoachieve.Itisourcontentionthatanybodywhohasunder-stoodalargerpartoftheideasexplainedinthisbookisabletoevaluateInternetinformationoncompilerdesign.Thesecondisthatmanyofthepaperswerefertoareavailableonlytothosefortunateenoughtohaveloginfacilitiesataninstitutewithsufficientbudgettoobtainsubscriptionstothelargerpublishers;theyarenolongeravailabletojustanyonewhowalksintoauniversitylibrary.Bothphenomenapointtoparadigmshiftswithwhichreaders,authors,publishersandlibrarianswillhavetocope.ThestructureofthebookThisbookisconceptuallydividedintotwoparts.Thefirst,comprisingChapters1through10,isconcernedwithtechniquesforprogramprocessingingeneral;itin-cludesachapteronmemorymanagement,bothinthecompilerandinthegeneratedcode.Thesecondpart,Chapters11through14,coversthespecifictechniquesre-quiredbythevariousprogrammingparadigms.Theinteractionsbetweenthepartsofthebookareoutlinedintheadjacenttable.Theleftmostcolumnshowsthefourphasesofcompilerconstruction:analysis,contexthandling,synthesis,andrun-timesystems.Chaptersinthiscolumncoverboththemanualandtheautomaticcreation
Prefaceviiofthepertinentsoftwarebuttendtoemphasizeautomaticgeneration.Theothercolumnsshowthefourparadigmscoveredinthisbook;foreachparadigmanex-ampleofasubjecttreatedbyeachofthephasesisshown.Thesechapterstendtocontainmanualtechniquesonly,allautomatictechniqueshavingbeendelegatedtoChapters2through9.inimperativeandobject-orientedprograms(Chapter11)infunctionalprograms(Chapter12)inlogicprograms(Chapter13)inparallel/distributedprograms(Chapter14)Howtodo:analysis(Chapters2&3)−−−−−−−−contexthandling(Chapters4&5)identifieridentificationpolymorphictypecheckingstaticrulematchingLindastaticanalysissynthesis(Chapters6–9)codeforwhile-statementcodeforlistcomprehensionstructureunificationmarshalingrun-timesystems(nochapter)stackreductionmachineWarrenAbstractMachinereplicationThescientificmindwouldlikethetabletobeniceandsquare,withallboxesfilled—inshort“orthogonal”—butweseethatthetoprightentriesaremissingandthatthereisnochapterfor“run-timesystems”intheleftmostcolumn.Thetoprightentrieswouldcoversuchthingsasthespecialsubjectsintheprogramtextanalysisoflogiclanguages,butpresenttextanalysistechniquesarepowerfulandflexibleenoughandlanguagessimilarenoughtohandlealllanguageparadigms:thereisnothingtobesaidthere,forlackofproblems.Thechaptermissingfromtheleftmostcolumnwoulddiscussmanualandautomatictechniquesforcreatingrun-timesystems.Unfortunatelythereislittleornotheoryonthissubject:run-timesystemsarestillcraftedbyhandbyprogrammersonanintuitivebasis;thereisnothingtobesaidthere,forlackofsolutions.Chapter1introducesthereadertocompilerdesignbyexaminingasimpletradi-tionalmodularcompiler/interpreterindetail.Severalhigh-levelaspectsofcompilerconstructionarediscussed,followedbyashorthistoryofcompilerconstructionandintroductionstoformalgrammarsandclosurealgorithms.Chapters2and3treattheprogramtextanalysisphaseofacompiler:theconver-sionoftheprogramtexttoanabstractsyntaxtree.Techniquesforlexicalanalysis,lexicalidentificationoftokens,andsyntaxanalysisarediscussed.Chapters4and5coverthesecondphaseofacompiler:contexthandling.Sev-eralmethodsofcontexthandlingarediscussed:automatedonesusingattributegrammars,manualonesusingL-attributedandS-attributedgrammars,andsemi-automatedonesusingsymbolicinterpretationanddata-flowanalysis.