of a Compiler Different Phases of a Compiler Example ... - CS 434

7 downloads 39 Views 25KB Size Report
Abstract Syntax Tree. Error Reports. Note: Not all compilers construct an explicit representation of an AST. (e.g. on a “single pass compiler” generally no need.
Course Overview

Chapter 3 Compilation So far we have treated language processors (including compilers) as “black boxes”

PART I: overview material 1 2

Introduction Language processors (tombstone diagrams, bootstrapping)

3

Architecture of a compiler

GOAL this lecture: – A first look "inside the box": how to build compilers. – Different “phases” and their relationships.

PART II: inside a compiler 4 5

Syntax analysis Contextual analysis

6 7

Runtime organization Code generation

PART III: conclusion 8 9

Interpretation Review

Compilation (Chapter 3)

1

The Major “Phases” of a Compiler

The different phases can be seen as different transformation steps to transform source code into object code. The different phases correspond roughly to the different parts of the language specification: • Syntax analysis Syntax • Contextual analysis Contextual constraints • Code generation Semantics

Error Reports

Abstract Syntax Tree Contextual Analysis

2

Different Phases of a Compiler

Source Program Syntax Analysis

Compilation (Chapter 3)

Error Reports

Decorated Abstract Syntax Tree Code Generation Object Code Compilation (Chapter 3)

3

Compilation (Chapter 3)

Example Program

4

1) Syntax Analysis

We now look at each of the three different phases in a little more detail. We look at each of the steps in transforming an example Triangle program into TAM code.

Source Program Syntax Analysis

!! This This program program is is useless useless except except for for !! illustration illustration let let var var n: n: integer; integer; var var c: c: char char in in begin begin cc := := ‘&’; ‘&’; nn := := n+1 n+1 end end

Compilation (Chapter 3)

Abstract Syntax Tree

5

Compilation (Chapter 3)

Error Reports

Note: Note: Not Notall allcompilers compilersconstruct constructan an explicit explicit representation representation of of an an AST. AST. (e.g. (e.g. on on aa “single “single pass pass compiler” compiler” generally generally no no need need to toconstruct constructan anAST) AST)

6

1

1) Syntax Analysis --> AST

2) Contextual Analysis --> Decorated AST

Program

Abstract Syntax Tree

LetCommand Contextual Analysis SequentialCommand SequentialDeclaration

Error Reports

Decorated Abstract Syntax Tree

AssignCommand

Contextual analysis: AssignCommand VarDecl

VarDecl

SimpleT Ident

n

Ident

Integer

BinaryExpr

Char.Expr

SimpleT SimpleV Ident

Ident

c

Char

SimpleV

Ident Char.Lit Ident

c

• Scope checking: verify that all applied occurrences of identifiers are declared • Type checking: verify that all operations in the program are used according to their type rules. Annotate AST: • Applied identifier occurrences => declaration • Expressions => Type

VNameExp Int.Expr

‘&’

Ident Op Int.Lit

n

n

+

1

Compilation (Chapter 3)

7

Compilation (Chapter 3)

8

Contextual Analysis

2) Contextual Analysis --> Decorated AST

Finds scope and type errors.

Program LetCommand

Example 1: AssignCommand SequentialCommand

***TYPE ERROR (incompatible types in AssignCommand)

:char :int

SequentialDeclaration

AssignCommand AssignCommand SimpleV BinaryExpr :int

Example 2:

:int VarDecl

VarDecl

Char.Expr

VNameExp Int.Expr

:char

SimpleT Ident

Ident

n Integer

:int

SimpleT SimpleV

:char

Ident

Ident

c Char

‘&’

SimpleV

:int

Ident Char.Lit Ident

c

foo not found

:int

SimpleV

n

Ident Op Int.Lit

n

Ident

+ 1

Compilation (Chapter 3)

foo 9

Compilation (Chapter 3)

3) Code Generation

10

3) Code Generation

Decorated Abstract Syntax Tree

let var n: integer; var c: char in begin c := ‘&’; n := n+1 end

Code Generation Object Code

• Assumes that program has been thoroughly checked and is well formed (scope & type rules) • Takes into account semantics of the source language as well as the target language. • Transforms source program into target code. Compilation (Chapter 3)

***SCOPE ERROR (undeclared variable foo)

VarDecl address = 0[SB]

PUSH 2 LOADL 38 STORE 1[SB] LOAD 0[SB] LOADL 1 CALL add STORE 0[SB] POP 2 HALT

SimpleT Ident

Ident

n Integer 11

Compilation (Chapter 3)

12

2

Compiler Passes

Single Pass Compiler

• A “pass” is a complete traversal of the source program, or a complete traversal of some internal representation of the source program (such as an AST). • A pass can correspond to a “phase” but it does not have to! • Sometimes a single pass corresponds to several phases that are interleaved in time. • What and how many passes a compiler does over the source program is an important design decision.

A single pass compiler makes a single pass over the source text, parsing, analyzing, and generating code all at once.

Dependency diagram of a typical Single Pass Compiler: Compiler Driver calls Syntactic Analyzer calls Contextual Analyzer

Compilation (Chapter 3)

13

let var n: integer; var c: char in begin c := ‘&’; n := n+1 end

Dependency diagram of a typical Multi Pass Compiler: Compiler Driver calls

Syntactic Analyzer input

Contextual Analyzer Code Generator output input output input output

Source Text

AST

14

Example: Single Pass Compilation of ...

A multi pass compiler makes several passes over the program. The output of a preceding phase is stored in a data structure and used by subsequent phases.

calls

Decorated AST

Ident n c

Object Code

Compilation (Chapter 3)

15

Type int char

Address 0[SB] 1[SB]

better

worse

Memory Modularity

better for large programs worse

(potentially) better for small programs better

Flexibility

worse

better

“Global” optimization

impossible

possible

Source Language

single pass compilers are not possible for many programming languages

Compilation (Chapter 3)

16

Language Issues Example Pascal: Pascal was explicitly designed to be easy to implement with a single pass compiler:

Multi Pass

Speed

PUSH 2 LOADL 38 STORE 1[SB] LOAD 0[SB] LOADL 1 CALL add STORE 0[SB] POP 2 HALT

Compilation (Chapter 3)

Compiler Design Issues Single Pass

Code Generator

Compilation (Chapter 3)

Multi Pass Compiler

calls

calls

– Every identifier must be declared before its first use. ? var n:integer; procedure inc; begin n:=n+1 end

17

Compilation (Chapter 3)

procedure inc; begin n:=n+1 end; Undeclared Variable! var n:integer;

18

3

Language Issues

Language Issues

Example Pascal:

Example Pascal:

– Every identifier must be declared before it is used.

– Every identifier must be declared before it is used.

– How to handle mutual recursion then?

– How to handle mutual recursion then?

procedure ping(x:integer) begin ... pong(x-1); ... end;

forward procedure pong(x:integer) procedure ping(x:integer) begin ... pong(x-1); ... end; OK! procedure pong(x:integer) begin ... ping(x–1); ... end;

procedure pong(x:integer) begin ... ping(x–1); ... end;

Compilation (Chapter 3)

19

Compilation (Chapter 3)

20

Example: The Triangle Compiler Driver public public class classCompiler Compiler {{ public public static static void void compileProgram(...) compileProgram(...) {{ Parser Parser parser parser = = new new Parser(...); Parser(...); Checker Checker checker checker = = new newChecker(...); Checker(...); Encoder Encoder generator generator = = new new Encoder(...); Encoder(...); Program // Program theAST theAST = = parser.parse( parser.parse( );); // first first pass pass checker.check(theAST // checker.check(theAST);); // second second pass pass generator.encode(theAST // generator.encode(theAST);); // third third pass pass

}}

}} public public static staticvoid void main(String[ main(String[ ]] args args)) {{ ... ... compileProgram(...); compileProgram(...); ... ... }}

Compilation (Chapter 3)

21

4