lecture 2 phases of compiler. preprocessors, compilers, assemblers, and linkers preprocessor...

11
Lecture 2 Phases of Compiler

Upload: kevin-mosley

Post on 23-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source

Lecture 2

Phases of Compiler

Page 2: Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source

Preprocessors, Compilers, Assemblers, and Linkers

Preprocessor

Compiler

Assembler

Linker

Skeletal Source Program

Source Program

Target Assembly Program

Relocatable Object Code

Absolute Machine Code

Libraries andRelocatable Object Files

Page 3: Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source

Compiler structure

sourcecode

targetcodeFront End Back End

IR

Page 4: Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source

Phases of compiler

Page 5: Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source

The Phases of a CompilerPhase Output Sample

Programmer (source code producer) Source string Position=initial+ rate*60

Scanner (performs lexical analysis) Token string <id 1><=><id 2><+> <id 3><*><60>

Parser (performs syntax analysis based on the grammar of the programming language)

Parse tree or abstract syntax tree = / \<id 1> + / \ <id 2> * / \ <id 3> 60

Semantic analyzer (type checking, etc) Annotated parse tree or abstract syntax tree

= / \<id 1> + / \ <id 2> * / \ <id 3> inttofloat | 60

5

Page 6: Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source

Cont’d

Phase Output Sample

Intermediate code generator Three-address code t1 = intofloat(60)t2 = id3 * t1t3 = id2 + t2id1 = t3

Code Optimizer • To improve the intermediate code so that better target code will result• Three-address code,

t1 = id3* 60.0id1 = id2 + t1

Code generator Assembly code LDF R2, id2MULF R2, R3, #60.0LDF R1, id2ADDF R1, R1, R2STF id1, R1

Page 7: Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source

The Grouping of Phases into passesCompiler front and back ends:

◦Front end: analysis (machine independent)

◦Back end: synthesis (machine dependent)

Compiler passes:◦A collection of phases is done only

once (single pass) or multiple times (multi pass) Single pass: usually requires

everything to be defined before being used in source program

Multi pass: compiler may have to keep entire program representation in memory

7

Page 8: Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source

Compiler-Construction ToolsSoftware development tools are

available to implement one or more compiler phases

1. Parser generators 2. Scanner generators 3. Syntax-directed translation engines 4. Code-generator generators that 5. Data-flow analysis engines6. Compiler-construction toolkits that

Page 9: Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source

Cont’d………………..

1. Scanner generators- that produce lexical analyzers from a

regular-expression description of the tokens of a language

2. Parser generators-that automatically produce syntax

analyzers from a grammatical description of a programming language

9

Page 10: Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source

3. Syntax-directed translation-Engines- that produce collections of

routines for walking a parse tree and generating intermediate code.4. Code-generators- that produce a code generator from

a collection of rules for translating each operation of the intermediate language into the machine language for a target machine.

Page 11: Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source

Cont’d…………….

5. Data-flow analysis engines- that facilitate the gathering of

information about how values are transmitted from one part of a program to each other part.

Data-flow analysis is a key part of code optimization.

6. Compiler-construction toolkits –that provide an integrated set of

routine for constructing various phases of a compiler.