26
Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Embed Size (px)

Citation preview

Page 1: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Spring 2012 61102 CompilersSoftware Eng. Dept. – Ort Braude

Compilers

Lecturer: Esti Stein

brd4.braude.ac.il/~esti2

Page 2: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Spring 2012 61102 CompilersSoftware Eng. Dept. – Ort Braude

Bibliography• A. Aho, R. Sethi, J. Ullman, Compilers - Principles, Techniques and

Tools, Addison-Wesley, '86 - '88• R. Wilhelm, D. Maurer, Compiler Design, Addison-Wesley, '95• Kenneth C. Louden, Compiler Construction Principles and Practice,

PWS Publishing Company, 1997• Steven S. Muchnik , Advanced Compiler Design and Implementation,

Morgan-Kaufman, 1997• Tony Mason, John Levine, Doug Brown, lex & yacc, 2nd Edition

October 1992 • Andrew W. Appel, Maia Ginsburg, Modern Compiler Implementation

in C, Cambridge University Press

Page 3: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Spring 2012 61102 CompilersSoftware Eng. Dept. – Ort Braude

Language & Communication

[jonathan Schaffer© - Ualberta]Language: “Any system of formalized symbols,

signs, etc., used or conceived as a means of communication.” [Random House]

Communicate: “to transmit or exchange thought or knowledge.” [Funk & Wagnall’s]

person

Computing problem

Programming

Language

Communicate

Machine

Solve

Page 4: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Spring 2012 61102 CompilersSoftware Eng. Dept. – Ort Braude

Language is an intermediary

Human expresses his way of thought in a rigid manner.

Machine interprets the human specification & interprets it as instructions to follow.

Goal – to automate the amount of work required to transmit ideas to a machine.

Page 5: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Spring 2012 61102 CompilersSoftware Eng. Dept. – Ort Braude

Hierarchy of languages

Machine

language

Assembly

Language

High-level

Problem

oriented

Natural

language

Page 6: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Spring 2012 61102 CompilersSoftware Eng. Dept. – Ort Braude

Translator

Translate the language into machine language

Compiler – translate the source language into the host’s machine language.

Interpreter – processes the source program and data at the same time.

Page 7: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Spring 2012 61102 CompilersSoftware Eng. Dept. – Ort Braude

If IL(PL, e) = a then IM( PM, e) = a , where PM

is the object program corresponding to PL.

Compiler

Compiler

CL

Program

PL

Object program

PM

Compile time

Object program

PM

Input

eOutput

a

Run timeInterprete

r

Machine

IM

Page 8: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Spring 2012 61102 CompilersSoftware Eng. Dept. – Ort Braude

For a language L – IL: L D* D* {error}

IL( PL, e) = a

It does not use any information independent from the input data.

Interpreter

Interpreter

IL

Program

PL

Input

e

Output

a

Page 9: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

Language implementations

• Some newer languages use a combination of compiler and interpreter to get many of the benefits of each.

• Examples are Java and Microsoft’s .NET, which compile into a virtual assembly language (while being optimized), which can then be interpreted on any computer.

• Other languages (such as Basic or Lisp) have both compilers and interpreters written for them.

• Recently, “Just-in-Time” compilers are becoming more common - compile code only when its used!

Page 10: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

How does a compiler work?

“similar” to how a human approaches the same problem.

Consider the following sequence:

W r I t e a C o m p i l e r

Do you understand what it means?

Can you describe the process ( how did you arrive at that conclusion)?

Page 11: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

Compilation Phases1. Recognize patterns that have some meaning (use

alphabet, math. Signs, punctuation etc.)

2. Group character into logical entities (words).

3. Associate a meaning with each word.

4. Check that the words form a structurally correct sentence. (what about – “compiler a write”).

5. Check that the combination of words make sense. (what about – “fly a compiler”).

6. Plan what you have to do to accomplish the task.

Page 12: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

General Termssource to source

virtual machine

Pre-processors

C ++ program C programtranslator

Java bytecodeJava program compiler

“pure” programprogram with

embedded

pre-pocessing statements

(e.g., #include, macros)

preprocessor

Page 13: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Using compilers in..

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

input• Programming languages: C,

Pascal, Assembler,... • Text processing languages:

PostScript, TeX, html, RTF,… • Scripting languages: C-shell,

emacs, perl, Hypercard…• Databases query languages:

SQL• Hardware desc. Languages:

VHDL, Verilog

target• Machine code: SPARC,

P690, IA32• Assembly language:• Code for the Interpreter:

Java Virtual Machine, P-Code, …

• Text processing languages: PostScript, TeX, html, RTF, …

• equipment languages• Electronic circuits

Page 14: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

Language processing tools

Skeletal source program

Preprocessor

Source program

Target assembly program

Assembler

Relocateable machine code

Loader/Link-editor

•Absolute machine code

compiler

Page 15: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

An example to modern structure - Java

Bytecode is standard,

machine-independent

Virtual machine – machine depended ( best for the target machine)

Page 16: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

History of compiler development

1953 IBM develops the 701 EDPM (Electronic Data Processing Machine), the first general purpose computer, built as a “defense calculator” in the Korean War

No high-level languages were available, so all programming was done in assembly

Page 17: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

History of compilers (cont’d)

As expensive as these early computers were, most of the money companies spent was for software development, due to the complexities of assembly.

John Backus

In 1953, John Backus came up with the idea of “speed coding”, and developed the first interpreter. Unfortunately, this was 10-20 times slower than programs written in assembly.

He was sure he could do better.

Page 18: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

History of compilers (cont’d)

In 1954, Backus and his team released a research paper titled “Preliminary Report, Specifications for the IBM Mathematical FORmula TRANslating System, FORTRAN.”

The initial release of FORTRAN I was in 1956, totaling 25,000 lines of assembly code. Compiled programs ran almost as fast as handwritten assembly!

Projects that had taken two weeks to write now took only 2 hours. By 1958 more than half of all software was written in FORTRAN.

Page 19: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

Structure of the compiler

Front-end Back-end

Machine Indepen

dent

Machine

Dependent

Page 20: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

Compilation Phases[2]Source program

Tokens

Parse tree

Abstract syntax tree w. attr.

Lexical analyzer

Syntax analyzer

Semantic analyzer

Intermediate code

Optimized Intermediate code

Target machine code

Intermediate-code generator

Intermediate-code optimizer

Target-code generator

Page 21: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

Front-endSource program

Tokens

Parse tree

Abstract syntax tree w. attr.

Lexical analyzer

Syntax analyzer

Semantic analyzer

Symbol Table

Error handler

Page 22: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

Back-end

Intermediate code

Optimized Intermediate code

Target machine code

Intermediate-code generator

Intermediate-code optimizer

Target-code generator

Symbol Table

Error handler

Page 23: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

position := initial + rate * 60

Th

e P

has

es o

f a

Com

pil

er

lexical analyzer

id1 := id2 + id3 * 60

syntax analyzer

:=

id1 +

id2 *

id3 60

semantic analyzer

:=

id1 +

id2 *

id3 inttoreal

60

intermediate code generator

temp1 := inttoreal (60)temp2 := id3 * temp1temp3 := id2 + temp2id1 := temp3

code optimizer

temp1 := id3 * 60.0id1 := id2 + temp1

code generator

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

Page 24: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

Passes• Each component in a compiler can be

viewed as a separate pass.• Usually several passes run concurrently.

Examples:

Pascal – designed to be 1-pass compiler.

PL/1 – At least 50 passes!

C/C++ - typically 2 to 3 passes:

- pre-processor, analysis phase, synthesis phase.

Page 25: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

5 languages on 4 machines

eq. 20 compilers ??

No!! If you do it right:

5 front-ends

4 back-ends

Porting a language to a new machine

Source language

Target Machine

Pascal IBM

C SUN

C++ SGI

Fortran HP

PL/1

Page 26: Spring 2012 61102 Compilers Software Eng. Dept. – Ort Braude Compilers Lecturer: Esti Stein brd4.braude.ac.il/~esti2

Winter 2010 61102 CompilersSoftware Eng. Dept. – Ort Braude

Summery

An automation of the lexical and syntactic analysis phases:

• lex, flex, lexagen – lexical analysis generators.• yacc, bison – parses generators.• suif – framework for an optimizer compiler.