Modern Compiler Implementation in C
Modern Compiler
Implementation
in C
ANDREW W. APPEL
Princeton University
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
Information on this title: 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–
and expanded ed.
Modern compiler implementation in C / Andrew W. Appel with Maia Ginsburg. – Rev.
x, 544 p. : ill. ; 24 cm.
Includes bibliographical references (p. 528–536) and index.
ISBN 0 521 58390 X
1. C (Computer program language) 2. Compilers (Computer programs)
(hardback)
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
Part I Fundamentals of Compilation
1 Introduction
1.1 Modules and interfaces
1.2 Tools and software
1.3 Data structures for tree languages
2 Lexical Analysis
2.1 Lexical tokens
2.2 Regular expressions
2.3 Finite automata
2.4 Nondeterministic finite automata
2.5 Lex: a lexical analyzer generator
3 Parsing
3.1 Context-free grammars
3.2 Predictive parsing
3.3 LR parsing
3.4 Using parser generators
3.5 Error recovery
4 Abstract Syntax
4.1 Semantic actions
4.2 Abstract parse trees
5 Semantic Analysis
5.1 Symbol tables
5.2 Bindings for the Tiger compiler
ix
3
4
5
7
16
17
18
21
24
30
39
41
46
56
69
76
88
88
92
103
103
112
v
CONTENTS
5.3 Type-checking expressions
5.4 Type-checking declarations
6 Activation Records
6.1 Stack frames
6.2 Frames in the Tiger compiler
7 Translation to Intermediate Code
7.1 Intermediate representation trees
7.2 Translation into trees
7.3 Declarations
8 Basic Blocks and Traces
8.1 Canonical trees
8.2 Taming conditional branches
9 Instruction Selection
9.1 Algorithms for instruction selection
9.2 CISC machines
9.3 Instruction selection for the Tiger compiler
10 Liveness Analysis
10.1 Solution of dataflow equations
10.2 Liveness in the Tiger compiler
11 Register Allocation
11.1 Coloring by simplification
11.2 Coalescing
11.3 Precolored nodes
11.4 Graph coloring implementation
11.5 Register allocation for trees
12 Putting It All Together
Part II Advanced Topics
13 Garbage Collection
13.1 Mark-and-sweep collection
13.2 Reference counts
115
118
125
127
135
150
151
154
170
176
177
185
191
194
202
205
218
220
229
235
236
239
243
248
257
265
273
273
278
vi
CONTENTS
13.3 Copying collection
13.4 Generational collection
13.5
Incremental collection
13.6 Baker’s algorithm
13.7
Interface to the compiler
14 Object-Oriented Languages
14.1 Classes
14.2 Single inheritance of data fields
14.3 Multiple inheritance
14.4 Testing class membership
14.5 Private fields and methods
14.6 Classless languages
14.7 Optimizing object-oriented programs
15 Functional Programming Languages
Immutable variables
Inline expansion
15.1 A simple functional language
15.2 Closures
15.3
15.4
15.5 Closure conversion
15.6 Efficient tail recursion
15.7 Lazy evaluation
16 Polymorphic Types
16.1 Parametric polymorphism
16.2 Type inference
16.3 Representation of polymorphic variables
16.4 Resolution of static overloading
17 Dataflow Analysis
Intermediate representation for flow analysis
17.1
17.2 Various dataflow analyses
17.3 Transformations using dataflow analysis
17.4 Speeding up dataflow analysis
17.5 Alias analysis
18 Loop Optimizations
18.1 Dominators
280
285
287
290
291
299
299
302
304
306
310
310
311
315
316
318
319
326
332
335
337
350
351
359
369
378
383
384
387
392
393
402
410
413
vii