compiler design - gbv
TRANSCRIPT
COMPILER DESIGN
Reinhard Wilhelm University ofthe Saarland, Saarbrücken
Dieter Maurer Saarbrücken Zeitung
Translated by Stephen S. Wilson
ADDISON-WESLEY PUBLISHING COMPANY
HARLOW, ENGLAND «READING, MASSACHUSETTS «MENLO PARK, CALIFORNIA «NEW YORK
DON MILLS, ONTARIO «AMSTERDAM «BONN «SYDNEY «SINGAPORE
TOKYO «MADRID «SAN JUAN «MILAN «MEXICO CITY «SEOUL «TAIPEI
Contents
Preface vü
1 Introduction 1
1.1 High-level programming languages 2
1.2 The implementation of programming languages 3
2 Compilation of imperative programming languages 7
2.1 Language constructs and their compilation 8
2.2 The architecture of the P machine 9
2.3 Assignments and expressions 10
2.4 Conditional and iterative Statements, sequences of Statements 13
2.5 Memory allocation for variables of simple type 18
2.6 Memory allocation for arrays 19
2.7 Memory allocation for records 27
2.8 Pointers and dynamic memory allocation 28
2.9 Procedures 32
2.10 Main program 56
2.11 Exercises 57
2.12 Literature 61
3 Compilation of functional programming languages 63
3.1 Language type and introductory example 64
3.2 LaMa, a simple functional programming language 72
3.3 Introduction to the compilation of LaMa 77
3.4 Environments and bindings 81
3.5 The MaMa architecture 83
xi
xii Contents
3.6 Stack management and addressing
3.7 Instruction set and compilation
3.8 Implementation of lists
3.9 Exercises
3.10 Literature
Compilation of logic programming languages
4.1 Logic programming languages
4.2 Logical foundations
4.3 Unification
4.4 Execution of logic programs
4.5 Prolog
4.6 Abstract Prolog machine
4.7 Compilation of Prolog
4.8 Efficiency improvements
4.9 Exercises
4.10 Literature
Compilation of object-oriented languages
5.1 Concepts of object-oriented languages
5.2 The compilation of methods
5.3 Schemes for compilation of inheritance
5.4 Genericity
5.5 Exercises
5.6 Literature
The structure of Compilers
6.1 Compiler subtasks
6.2 Lexical analysis
6.3 Screening
6.4 Syntax analysis
6.5 Semantic analysis
87
90
104
109
111
113
114
118
120
124
132
138
142
161
167
169
171
172
180
182
207
214
219
221
222
223
225
225
226
Contents xiii
6.6 Machine-independent optimization 228
6.7 Address assignment 229
6.8 Generation of the target program 230
6.9 Machine-dependent code improvement 231
6.10 Real Compiler structures 231
6.11 Formal specification and generation of Compiler modules 232
6.12 Literature 233
Lexical analysis 235
7.1 The task of lexical analysis 236
7.2 Theoretical foundations 236
7.3 A language for specifying the lexical analysis 248
7.4 The generation of a Scanner 251
7.5 The screener 256
7.6 Flex, a Scanner generator under UNIX 259
7.7 Exercises 261
7.8 Literature 263
Syntax analysis 265
8.1 The task of syntax analysis 266
8.2 Theoretical foundations 269
8.3 Top-down syntax analysis 300
8.4 Bottom-up syntax analysis 339
8.5 Bison, an LALR(l)-parser generator 375
8.6 Exercises 377
8.7 Literature 382
Semantic analysis 385
9.1 Task of the semantic analysis 386
9.2 Attribute grammars 405
9.3 Examples of attribute grammars 410
9.4 The generation of attribute evaluators 417
xiv Contents
9.5 Exercises 453
9.6 Literature 456
10 Abstract interpretation 457
10.1 Introduction 458
10.2 Abstract interpretation based on denotational semantics 462
10.3 Abstract interpretation based on operational semantics 484
10.4 Exercises 506
10.5 Literature 510
11 Trees: pattern matching and parsing 513
11.1 Program transformations 514
11.2 Code selection 519
11.3 The pattern-matching problem 523
11.4 The tree-parsing problem 526
11.5 Finite tree automata 529
11.6 Generation of pattern matchers 531
11.7 Generation of tree parsers 535
11.8 Tree automata with costs 537
11.9 Implementation 542
11.10 Exercises 544
11.11 Literature 545
12 Code generation 547
12.1 Abstract and real machines 548
12.2 Classification of architectures 554
12.3 Program representations 558
12.4 Code generation, integrated methods 561
12.5 Register allocation by graph colouring 568
12.6 Instruction scheduling 570
12.7 Exercises 588
12.8 Literature 590
Contents xv
Bibliography 591
Index 599