1 contents. 2 run-time storage organization 3 static allocation in many early languages, notably...

33
1 Contents Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice Grammars and Parsing Grammars and Parsing LL(1) Parsing LL(1) Parsing Lex and yacc Lex and yacc LR Parsing LR Parsing Semantic Processing Semantic Processing Symbol Tables Symbol Tables Run-time Storage Organization Run-time Storage Organization Code Generation and Local Code Optimization Code Generation and Local Code Optimization Global Optimization Global Optimization

Post on 22-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

1

ContentsContents

IntroductionIntroduction A Simple CompilerA Simple Compiler Scanning – Theory and PracticeScanning – Theory and Practice Grammars and ParsingGrammars and Parsing LL(1) ParsingLL(1) Parsing Lex and yaccLex and yacc LR ParsingLR Parsing Semantic ProcessingSemantic Processing Symbol TablesSymbol Tables Run-time Storage OrganizationRun-time Storage Organization Code Generation and Local Code OptimizationCode Generation and Local Code Optimization Global OptimizationGlobal Optimization

2

Run-Time Storage Run-Time Storage OrganizationOrganization

3

Static AllocationStatic Allocation

In many early languages, notably assembly In many early languages, notably assembly and FORTRAN, all storage allocation is and FORTRAN, all storage allocation is static.static.

Space for data objects is allocated in a fixed Space for data objects is allocated in a fixed location for the lifetime of a program.location for the lifetime of a program.

The number and size of all objects to be The number and size of all objects to be allocated is known at compile-time.allocated is known at compile-time.

Static allocation is used both for global Static allocation is used both for global variables and constants.variables and constants.

Static allocation is also used for local Static allocation is also used for local variables, e.g., static and extern in C variables, e.g., static and extern in C

DisadvantageDisadvantage Wasteful of spaceWasteful of space

4

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

Recursive procedures, a feature that requires Recursive procedures, a feature that requires dynamic allocation.dynamic allocation.

Each recursive call requires the allocation of Each recursive call requires the allocation of a new copy of a procedure’s local variables.a new copy of a procedure’s local variables. The number of data objects required during The number of data objects required during

program execution is not known at compile-time.program execution is not known at compile-time. Activation record (AR)Activation record (AR)

An AR is used to store all the data space required An AR is used to store all the data space required for a procedure or function.for a procedure or function.

An AR is pushed onto a run-time stack when the An AR is pushed onto a run-time stack when the procedure or function is calledprocedure or function is called

When is subporgram returns, the AR is popped.When is subporgram returns, the AR is popped.

5

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

6

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

7

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

Dynamic arraysDynamic arrays The bounds of dynamic arrays are determined at The bounds of dynamic arrays are determined at

run-time rather than compile-time, hence these run-time rather than compile-time, hence these arrays cannot be allocated within an AR.arrays cannot be allocated within an AR.

Dynamic arrays can be allocated as soon as their Dynamic arrays can be allocated as soon as their associated declarations are elaborated.associated declarations are elaborated.

8

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

9

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

10

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

11

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

12

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

13

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

14

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

15

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

16

Stack Allocation Stack Allocation (Cont’d.)(Cont’d.)

17

Heap AllocationHeap Allocation

18

Heap Allocation Heap Allocation (Cont’d.)(Cont’d.)

Three methods for deallocationThree methods for deallocation No deallocationNo deallocation Explicit deallocationExplicit deallocation

19

Heap Allocation Heap Allocation (Cont’d.)(Cont’d.)

20

Program Layout in MemoryProgram Layout in Memory

21

Program Layout in Memory Program Layout in Memory (Cont’d.)(Cont’d.)

22

Static and Dynamic ChainsStatic and Dynamic Chains

23

Static and Dynamic Chains Static and Dynamic Chains (Cont’d.)(Cont’d.)

24

Formal ProceduresFormal Procedures

25

Formal Procedures Formal Procedures (Cont’d.)(Cont’d.)

26

Formal Procedures Formal Procedures (Cont’d.)(Cont’d.)

27

Formal Procedures Formal Procedures (Cont’d.)(Cont’d.)

28

Formal Procedures Formal Procedures (Cont’d.)(Cont’d.)

29

Formal Procedures Formal Procedures (Cont’d.)(Cont’d.)

30

Chapter 10 Processing DeclarationsChapter 10 Processing Declarations

Declaration Processing FundamentalsDeclaration Processing Fundamentals Action Routines for Simple DeclarationsAction Routines for Simple Declarations Action Routines for Advanced FeaturesAction Routines for Advanced Features

31

Chapter 11 Processing Expressions and Chapter 11 Processing Expressions and Data Structure ReferencesData Structure References Action Routines for Simple Names, Action Routines for Simple Names,

Expressions, and StructuresExpressions, and Structures Array References and RecordsArray References and Records

Action Routines for Advantage FeaturesAction Routines for Advantage Features

32

Chapter 12 Translating Control StructuresChapter 12 Translating Control Structures

if Statementsif Statements loopsloops

while loopswhile loops for loopsfor loops

The case StatementThe case Statement Short-circuit Boolean ExpressionsShort-circuit Boolean Expressions

33

Chapter 13 Translating Procedures and Chapter 13 Translating Procedures and FunctionsFunctions

Simple SubprogramsSimple Subprograms Passing Parameters to SubprogramsPassing Parameters to Subprograms Processing Subprogram Calls and Parameter Processing Subprogram Calls and Parameter

ListsLists Subprogram InvocationSubprogram Invocation