course overview

21
1 Compilation (Chapter 3) Course Overview PART I: overview material 1 Introduction 2 Language processors (tombstone diagrams, bootstrapping) 3 Architecture of a compiler PART II: inside a compiler 4 Syntax analysis 5 Contextual analysis 6 Runtime organization 7 Code generation PART III: conclusion 8 Interpretation 9 Review

Upload: akiko

Post on 07-Jan-2016

21 views

Category:

Documents


0 download

DESCRIPTION

Course Overview. PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II: inside a compiler 4Syntax analysis 5Contextual analysis 6Runtime organization 7Code generation PART III: conclusion - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Course Overview

1Compilation (Chapter 3)

Course Overview

PART I: overview material1 Introduction

2 Language processors (tombstone diagrams, bootstrapping)

3 Architecture of a compiler

PART II: inside a compiler4 Syntax analysis

5 Contextual analysis

6 Runtime organization

7 Code generation

PART III: conclusion8 Interpretation

9 Review

Page 2: Course Overview

2Compilation (Chapter 3)

Chapter 3 Compilation

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

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

Page 3: Course Overview

3Compilation (Chapter 3)

The Major “Phases” of a Compiler

Syntax Analysis

Contextual Analysis

Code Generation

Source Program

Abstract Syntax Tree

Decorated Abstract Syntax Tree

Object Code

Error Reports

Error Reports

Page 4: Course Overview

4Compilation (Chapter 3)

Different 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

Page 5: Course Overview

5Compilation (Chapter 3)

Example Program

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.

! This program is useless except for! illustrationlet var n: integer; var c: charin begin c := ‘&’; n := n+1end

! This program is useless except for! illustrationlet var n: integer; var c: charin begin c := ‘&’; n := n+1end

Page 6: Course Overview

6Compilation (Chapter 3)

1) Syntax Analysis

Syntax Analysis

Source Program

Abstract Syntax Tree

Error Reports

Note: Not all compilers construct anexplicit representation of an AST. (e.g. on a “single pass compiler” generally no need to construct an AST)

Note: Not all compilers construct anexplicit representation of an AST. (e.g. on a “single pass compiler” generally no need to construct an AST)

Page 7: Course Overview

7Compilation (Chapter 3)

1) Syntax Analysis --> AST

Program

LetCommand

SequentialDeclaration

n Integer c Char c ‘&’ n n + 1

Ident Ident Ident Ident Ident Ident Ident OpChar.Lit Int.Lit

SimpleT

VarDecl

SimpleT

VarDecl

SimpleV

Char.Expr

SimpleV

VNameExp Int.Expr

AssignCommand BinaryExpr

SequentialCommand

AssignCommand

Page 8: Course Overview

8Compilation (Chapter 3)

2) Contextual Analysis --> Decorated AST

Contextual Analysis

Decorated Abstract Syntax Tree

Error Reports

Abstract Syntax Tree

Contextual analysis:

• 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

Page 9: Course Overview

9Compilation (Chapter 3)

2) Contextual Analysis --> Decorated AST

Program

LetCommand

SequentialDeclaration

n

Ident Ident Ident Ident

SimpleT

VarDecl

SimpleT

VarDecl

Integer c Char c ‘&’ n n + 1

Ident Ident Ident OpChar.Lit Int.Lit

SimpleV

Char.Expr

SimpleV

VNameExp Int.Expr

AssignCommand BinaryExpr

SequentialCommand

AssignCommand

:char

:char

:int

:int

:int :int

SimpleV:int

Page 10: Course Overview

10Compilation (Chapter 3)

Contextual Analysis

Finds scope and type errors.

AssignCommand

:int

Example 1:

:char

***TYPE ERROR(incompatible types in AssignCommand)

Example 2:

foo

Ident

SimpleV

foo not found

***SCOPE ERROR(undeclared variable foo)

Page 11: Course Overview

11Compilation (Chapter 3)

3) Code Generation

• 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.

Code Generation

Decorated Abstract Syntax Tree

Object Code

Page 12: Course Overview

12Compilation (Chapter 3)

3) Code Generation

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

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

n

Ident Ident

SimpleT

VarDecl

Integer

address = 0[SB]

Page 13: Course Overview

13Compilation (Chapter 3)

Compiler Passes

• 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.

Page 14: Course Overview

14Compilation (Chapter 3)

Single Pass Compiler

Compiler Driver

Syntactic Analyzer

calls

calls

Contextual Analyzer Code Generator

calls

Dependency diagram of a typical Single Pass Compiler:

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

Page 15: Course Overview

15Compilation (Chapter 3)

Multi Pass Compiler

Compiler Driver

Syntactic Analyzer

callscalls

Contextual Analyzer Code Generator

calls

Dependency diagram of a typical Multi Pass Compiler:

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.

input

Source Text

output

AST

input output

Decorated AST

input output

Object Code

Page 16: Course Overview

16Compilation (Chapter 3)

Example: Single Pass Compilation of ...

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

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

Identnc

Typeintchar

Address0[SB]1[SB]

Page 17: Course Overview

17Compilation (Chapter 3)

Compiler Design Issues

Single Pass Multi Pass

Speed

Memory

Modularity

Flexibility

“Global” optimization

Source Language

better worse

better for large programs

(potentially) better for small programs

worse better

betterworse

impossible possible

single pass compilers are not possible for many programming languages

Page 18: Course Overview

18Compilation (Chapter 3)

Language Issues

Example Pascal:

Pascal was explicitly designed to be easy to implement with a single pass compiler:– Every identifier must be declared before its first use.

var n:integer;

procedure inc;begin n:=n+1end

Undeclared Variable!

procedure inc;begin n:=n+1end;

var n:integer;

?

Page 19: Course Overview

19Compilation (Chapter 3)

Language Issues

Example Pascal:– Every identifier must be declared before it is used.

– How to handle mutual recursion then?

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

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

Page 20: Course Overview

20Compilation (Chapter 3)

Language Issues

Example Pascal:– Every identifier must be declared before it is used.

– How to handle mutual recursion then?

forward procedure pong(x:integer)

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

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

OK!

Page 21: Course Overview

21Compilation (Chapter 3)

Example: The Triangle Compiler Driver

public class Compiler { public static void compileProgram(...) {

Parser parser = new Parser(...);Checker checker = new Checker(...);Encoder generator = new Encoder(...);

Program theAST = parser.parse( ); // first pass checker.check(theAST); // second pass

generator.encode(theAST); // third pass } public static void main(String[ ] args) {

... compileProgram(...); ... }}

public class Compiler { public static void compileProgram(...) {

Parser parser = new Parser(...);Checker checker = new Checker(...);Encoder generator = new Encoder(...);

Program theAST = parser.parse( ); // first pass checker.check(theAST); // second pass

generator.encode(theAST); // third pass } public static void main(String[ ] args) {

... compileProgram(...); ... }}