ebook img

Modern Compiler Implementation in C PDF

557 Pages·1998·5.172 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Modern Compiler Implementation in C

Modern Compiler Implementation in C Modern Compiler Implementation in C ANDREW W. APPEL PrincetonUniversity with MAIA GINSBURG PUBLISHED BY THE PRESS SYNDICATE OF THE UNIVERSITY OF CAMBRIDGE The Pitt Building, Trumpington Street, Cambridge, United Kingdom CAMBRIDGE UNIVERSITY PRESS The Edinburgh Building, Cambridge CB2 2RU, UK 40 West 20th Street, New York NY 10011–4211, USA 477 Williamstown Road, Port Melbourne, VIC 3207, Australia Ruiz de Alarcón 13, 28014 Madrid, Spain Dock House, The Waterfront, Cape Town 8001, South Africa http://www.cambridge.org Informationonthistitle:www.cambridge.org/9780521583909 © Andrew W. Appel and Maia Ginsburg 1998 This book is in copyright. Subject to statutory exception and to the provisions of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press. First published 1998 Revised and expanded edition of Modern Compiler Implementation in C: Basic Techniques Reprinted with corrections, 1999 First paperback edition 2004 Typeset in Times, Courier, and Optima A catalogue record for this book is available from the British Library Library of Congress Cataloguing-in-Publication data Appel, Andrew W., 1960– Modern compiler implementation in C / Andrew W. Appel with Maia Ginsburg. – Rev. and expanded ed. x, 544 p. : ill. ; 24 cm. Includes bibliographical references (p. 528–536) and index. ISBN 0 521 58390 X (hardback) 1. C (Computer program language) 2. Compilers (Computer programs) I. Ginsburg, Maia. II. Title. QA76.73.C15A63 1998 005.4´53—dc21 97-031089 CIP ISBN 0 521 58390 X hardback ISBN 0 521 60765 5 paperback Contents Preface ix PartI Fundamentals ofCompilation 1 Introduction 3 1.1 Modulesandinterfaces 4 1.2 Toolsandsoftware 5 1.3 Datastructures fortreelanguages 7 2 LexicalAnalysis 16 2.1 Lexicaltokens 17 2.2 Regularexpressions 18 2.3 Finiteautomata 21 2.4 Nondeterministic finiteautomata 24 2.5 Lex:alexicalanalyzer generator 30 3 Parsing 39 3.1 Context-free grammars 41 3.2 Predictiveparsing 46 3.3 LRparsing 56 3.4 Usingparsergenerators 69 3.5 Errorrecovery 76 4 AbstractSyntax 88 4.1 Semanticactions 88 4.2 Abstractparsetrees 92 5 SemanticAnalysis 103 5.1 Symboltables 103 5.2 BindingsfortheTigercompiler 112 v CONTENTS 5.3 Type-checking expressions 115 5.4 Type-checking declarations 118 6 ActivationRecords 125 6.1 Stackframes 127 6.2 FramesintheTigercompiler 135 7 TranslationtoIntermediateCode 150 7.1 Intermediate representation trees 151 7.2 Translation intotrees 154 7.3 Declarations 170 8 BasicBlocksandTraces 176 8.1 Canonical trees 177 8.2 Tamingconditional branches 185 9 InstructionSelection 191 9.1 Algorithmsforinstruction selection 194 9.2 CISCmachines 202 9.3 Instruction selection fortheTigercompiler 205 10 LivenessAnalysis 218 10.1 Solutionofdataflowequations 220 10.2 LivenessintheTigercompiler 229 11 RegisterAllocation 235 11.1 Coloringbysimplification 236 11.2 Coalescing 239 11.3 Precolored nodes 243 11.4 Graphcoloring implementation 248 11.5 Registerallocation fortrees 257 12 PuttingItAllTogether 265 PartII Advanced Topics 13 GarbageCollection 273 13.1 Mark-and-sweep collection 273 13.2 Referencecounts 278 vi CONTENTS 13.3 Copyingcollection 280 13.4 Generational collection 285 13.5 Incremental collection 287 13.6 Baker’salgorithm 290 13.7 Interface tothecompiler 291 14 Object-OrientedLanguages 299 14.1 Classes 299 14.2 Singleinheritance ofdatafields 302 14.3 Multipleinheritance 304 14.4 Testingclassmembership 306 14.5 Privatefieldsandmethods 310 14.6 Classlesslanguages 310 14.7 Optimizingobject-oriented programs 311 15 FunctionalProgrammingLanguages 315 15.1 Asimplefunctional language 316 15.2 Closures 318 15.3 Immutablevariables 319 15.4 Inlineexpansion 326 15.5 Closureconversion 332 15.6 Efficienttailrecursion 335 15.7 Lazyevaluation 337 16 PolymorphicTypes 350 16.1 Parametricpolymorphism 351 16.2 Typeinference 359 16.3 Representation ofpolymorphic variables 369 16.4 Resolution ofstaticoverloading 378 17 DataflowAnalysis 383 17.1 Intermediate representation forflowanalysis 384 17.2 Variousdataflowanalyses 387 17.3 Transformations usingdataflowanalysis 392 17.4 Speedingupdataflowanalysis 393 17.5 Aliasanalysis 402 18 LoopOptimizations 410 18.1 Dominators 413 vii CONTENTS 18.2 Loop-invariant computations 418 18.3 Induction variables 419 18.4 Array-bounds checks 425 18.5 Loopunrolling 429 19 StaticSingle-AssignmentForm 433 19.1 Converting toSSAform 436 19.2 Efficientcomputation ofthedominator tree 444 19.3 Optimization algorithmsusingSSA 451 19.4 Arrays,pointers, andmemory 457 19.5 Thecontrol-dependence graph 459 19.6 Converting backfromSSAform 462 19.7 Afunctional intermediate form 464 20 PipeliningandScheduling 474 20.1 Loopscheduling withoutresource bounds 478 20.2 Resource-bounded looppipelining 482 20.3 Branchprediction 490 21 TheMemoryHierarchy 498 21.1 Cacheorganization 499 21.2 Cache-block alignment 502 21.3 Prefetching 504 21.4 Loopinterchange 510 21.5 Blocking 511 21.6 Garbagecollection andthememoryhierarchy 514 Appendix:TigerLanguageReference Manual 518 A.1 Lexicalissues 518 A.2 Declarations 518 A.3 Variablesandexpressions 521 A.4 Standardlibrary 525 A.5 SampleTigerprograms 526 Bibliography 528 Index 537 viii Preface Overthepastdecade, therehavebeenseveralshiftsinthewaycompilersare built. New kinds of programming languages are being used: object-oriented languages with dynamic methods, functional languages with nested scope andfirst-classfunctionclosures;andmanyoftheselanguagesrequiregarbage collection.Newmachineshavelargeregistersetsandahighpenaltyformem- oryaccess,andcanoftenrunmuchfasterwithcompilerassistanceinschedul- inginstructions andmanaginginstructions anddataforcachelocality. This book is intended as a textbook for a one- or two-semester course in compilers. Students will see the theory behind different components of a compiler, the programming techniques used to put the theory into practice, and the interfaces used to modularize the compiler. To make the interfaces and programming examples clear and concrete, Ihave written them in the C programming language. Othereditions ofthisbookareavailable thatusethe JavaandMLlanguages. Implementationproject. The“studentprojectcompiler”thatIhaveoutlined is reasonably simple, but is organized to demonstrate some important tech- niques that are now in common use: abstract syntax trees to avoid tangling syntaxandsemantics,separationofinstructionselectionfromregisteralloca- tion,copypropagationtogiveflexibilitytoearlierphasesofthecompiler,and containment of target-machine dependencies. Unlike many“student compil- ers” found in textbooks, this one has a simple but sophisticated back end, allowinggoodregisterallocation tobedoneafterinstruction selection. Each chapter in Part I has a programming exercise corresponding to one moduleofacompiler.Softwareusefulfortheexercisescanbefoundat http://www.cs.princeton.edu/˜appel/modern/c ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.