"Code Generation 001.ps.gz" - читать интересную книгу автора



Copyright K.J.Gough 199516JAN96 DRAFT

Contents 1 Introduction 1

1.1 Assumed background : : : : : : : : : : : : : : : : : : : : : : : : : 1 1.2 The reference architecture : : : : : : : : : : : : : : : : : : : : : : 2 1.3 Intermediate Forms : : : : : : : : : : : : : : : : : : : : : : : : : : 2

1.3.1 Abstract syntax tree : : : : : : : : : : : : : : : : : : : : : 2 1.3.2 Abstract stack intermediate representation : : : : : : : : 4 1.3.3 Virtual assembly language : : : : : : : : : : : : : : : : : : 4 1.3.4 A note on assembly language syntax : : : : : : : : : : : : 5 1.4 How to avoid code generation entirely : : : : : : : : : : : : : : : 6 1.5 A little bit of duality : : : : : : : : : : : : : : : : : : : : : : : : : 6 1.6 Compiler Bootstrapping : : : : : : : : : : : : : : : : : : : : : : : 8

1.6.1 Introduction to T-diagrams : : : : : : : : : : : : : : : : : 8 1.6.2 The hand-translation bootstrap : : : : : : : : : : : : : : : 11 1.6.3 The cross-bootstrap : : : : : : : : : : : : : : : : : : : : : 12 1.6.4 Why write a compiler in its own language : : : : : : : : : 12 1.7 Code Improvement : : : : : : : : : : : : : : : : : : : : : : : : : : 14

1.7.1 Safety and profitability : : : : : : : : : : : : : : : : : : : 14 1.7.2 Space and time : : : : : : : : : : : : : : : : : : : : : : : : 15

2 Attribute Evaluation 17

2.1 Abstract syntax trees : : : : : : : : : : : : : : : : : : : : : : : : : 17

2.1.1 Defining abstract syntax : : : : : : : : : : : : : : : : : : : 18 2.2 Representing declared entities : : : : : : : : : : : : : : : : : : : : 20

2.2.1 Representing types : : : : : : : : : : : : : : : : : : : : : : 21 2.2.2 Representing scopes : : : : : : : : : : : : : : : : : : : : : 23 2.3 Static semantic checking : : : : : : : : : : : : : : : : : : : : : : : 24 2.4 Attribute evaluation : : : : : : : : : : : : : : : : : : : : : : : : : 25

2.4.1 Attribute grammars : : : : : : : : : : : : : : : : : : : : : 25 2.4.2 Implementing attribute evaluation : : : : : : : : : : : : : 27 2.4.3 Attribute evaluation from specifications : : : : : : : : : : 29 2.5 Operator identification : : : : : : : : : : : : : : : : : : : : : : : : 29

2.5.1 Automatic coercions : : : : : : : : : : : : : : : : : : : : : 30 2.6 Rewriting the tree : : : : : : : : : : : : : : : : : : : : : : : : : : 34 2.7 Finding out more : : : : : : : : : : : : : : : : : : : : : : : : : : : 36 2.8 Exercises : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 36

i

Copyright K.J.Gough 199516JAN96 DRAFT

ii CONTENTS 3 Runtime Organization 37

3.1 The runtime environment : : : : : : : : : : : : : : : : : : : : : : 37

3.1.1 Memory organization : : : : : : : : : : : : : : : : : : : : 38 3.2 Procedure call and return : : : : : : : : : : : : : : : : : : : : : : 42

3.2.1 Register use conventions : : : : : : : : : : : : : : : : : : : 43 3.2.2 Activation records : : : : : : : : : : : : : : : : : : : : : : 44 3.2.3 Stack organization : : : : : : : : : : : : : : : : : : : : : : 45 3.2.4 Parameter passing : : : : : : : : : : : : : : : : : : : : : : 52 3.2.5 Addressing data from non-local scopes : : : : : : : : : : : 62 3.2.6 Architectures with global data pointers : : : : : : : : : : 68 3.2.7 Procedure parameters and variables : : : : : : : : : : : : 72 3.2.8 Dynamic linking : : : : : : : : : : : : : : : : : : : : : : : 75 3.3 Object oriented languages, and method dispatch : : : : : : : : : 77

3.3.1 Runtime polymorphism : : : : : : : : : : : : : : : : : : : 77 3.3.2 Languages with single inheritance : : : : : : : : : : : : : 78 3.3.3 Languages with multiple inheritance : : : : : : : : : : : : 79 3.4 Error handling and exceptions : : : : : : : : : : : : : : : : : : : : 80