241-437 compilers: overview/1 1 compiler structures objective – –what are the main features...

22
241-437 Compilers: Overview/1 Compiler Structures Objective what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012 1. Overview

Upload: joel-bell

Post on 14-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 1

Compiler Structures

• Objective– what are the main features (structures) in a

compiler?

241-437, Semester 1, 2011-2012

1. Overview

Page 2: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 2

Overview

1. What is a Compiler?

2. Structure of a Compiler

3. Example Compilation

4. Compiler Design Issues

5. Compilers and Interpreters

Page 3: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 3

1. What is a Compiler?

• A compiler reads a program written in one language and translates it into another language.

• The source language is high-level (e.g. C), and the target language is low-level (e.g. machine code).

compilerprogram

in a sourcelanguage

programin a targetlanguage

Page 4: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 4

• The target program is run later.

Compiler

Compile-timeError messages

SourceProgram

TargetProgram

Input

Output

TargetProgram

RuntimeError messages

Page 5: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 5

2. Structure of a Compiler

Source Program

?

Target Lang. Prog.

Page 6: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 6

Structure of a Compiler

Source Program

Target Lang. Prog.

Front End

Back End

Intermediate Code

Page 7: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 7

Structure of a Compiler

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

FrontEnd

Back End

Int. Code Generator

Intermediate Code

Page 8: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 8

Structure of a Compiler

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

FrontEnd

Code Optimizer

Target Code Generator

BackEnd

Int. Code Generator

Intermediate Code

This course willconcentrate on the"front end" tasksperformed by acompiler.

Page 9: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 9

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

Code Optimizer

Target Code Generator

Int. Code Generator

Intermediate Code

Source Code:cur_time = start_time + cycles * 60

3. Example Compilation

Page 10: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 10

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

Code Optimizer

Target Code Generator

Int. Code Generator

Intermediate Code

Source Code:cur_time = start_time + cycles * 60

Lexical Analysis:ID(1) ASSIGN ID(2) ADD ID(3) MULT INT(60)

Example Compilation

Page 11: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 11

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

Code Optimizer

Target Code Generator

Int. Code Generator

Intermediate Code

Source Code:cur_time = start_time + cycles * 60

Lexical Analysis:ID(1) ASSIGN ID(2) ADD ID(3) MULT INT(60)

Syntax Analysis: ASSIGN

ID(1) ADD

ID(2) MULT

ID(3) INT(60)

Example Compilation

Page 12: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 12

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

Code Optimizer

Target Code Generator

Int. Code Generator

Intermediate Code

Syntax Analysis: ASSIGN

ID(1) ADD

ID(2) MULT

ID(3) INT(60)Sematic Analysis: ASSIGN

ID(1) ADD

ID(2) MULT

ID(3) int2float

INT(60)

Example Compilation

Page 13: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 13

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

Code Optimizer

Target Code Generator

Int. Code Generator

Intermediate Code

Sematic Analysis: ASSIGN

ID(1) ADD

ID(2) MULT

ID(3) int2float

INT(60)

Intermediate Code:temp1 = int2float(60)temp2 = id3 * temp1temp3 = id2 + temp2id1 = temp3

Example Compilation

Page 14: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 14

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

Code Optimizer

Target Code Generator

Int. Code Generator

Intermediate Code

Intermediate Code:temp1 = int2float(60)temp2 = id3 * temp1temp3 = id2 + temp2id1 = temp3

Optimized Code (step 0):temp1 = int2float(60)temp2 = id3 * temp1temp3 = id2 + temp2id1 = temp3

Example Compilation

Page 15: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 15

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

Code Optimizer

Target Code Generator

Int. Code Generator

Intermediate Code

Intermediate Code:temp1 = int2float(60)temp2 = id3 * temp1temp3 = id2 + temp2id1 = temp3

Optimized Code (step 1):temp1 = 60.0temp2 = id3 * temp1temp3 = id2 + temp2id1 = temp3

Example Compilation

Page 16: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 16

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

Code Optimizer

Target Code Generator

Int. Code Generator

Intermediate Code

Intermediate Code:temp1 = int2float(60)temp2 = id3 * temp1temp3 = id2 + temp2id1 = temp3

Optimized Code (step 2):

temp2 = id3 * 60.0temp3 = id2 + temp2id1 = temp3

Example Compilation

Page 17: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 17

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

Code Optimizer

Target Code Generator

Int. Code Generator

Intermediate Code

Intermediate Code:temp1 = int2float(60)temp2 = id3 * temp1temp3 = id2 + temp2id1 = temp3

Optimized Code (step 3):

temp2 = id3 * 60.0id1 = id2 + temp2

Example Compilation

Page 18: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 18

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

Code Optimizer

Target Code Generator

Int. Code Generator

Intermediate Code

Intermediate Code:temp1 = int2float(60)temp2 = id3 * temp1temp3 = id2 + temp2id1 = temp3

Optimized Code:

temp1 = id3 * 60.0id1 = id2 + temp1

Example Compilation

Page 19: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 19

Source Program

Target Lang. Prog.

Semantic Analyzer

Syntax Analyzer

Lexical Analyzer

Code Optimizer

Target Code Generator

Int. Code Generator

Intermediate Code

Intermediate Code:temp1 = int2float(60)temp2 = id3 * temp1temp3 = id2 + temp2id1 = temp3

Optimized Code:

temp1 = id3 * 60.0id1 = id2 + temp1

Target Code:MOVF id3, R2MULF #60.0, R2MOVF id2, R1ADDF R2, R1MOVF R1, id1

Example Compilation

Page 20: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 20

4. Compiler Design Issues

• Correctness– does the target do the 'same' as the source?

• Speed (compile time and runtime)– code optimizations

– multiple passes over code

• Memory space required• Good feedback to user

– error reporting

Page 21: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 21

5. Compilers and Interpreters

• A compiler generates a target program that can be run later.

Compiler

Compile-timeError messages

SourceProgram

TargetProgram

Input

Output

TargetProgram

RuntimeError messages

Page 22: 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012

241-437 Compilers: Overview/1 22

interpreter

SourceProgram

Input

Output

Error messages

• An interpreter 'compiles' and runs the source program all at once– it usually executes the intermediate code