+ All Categories
Home > Documents > 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to...

6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to...

Date post: 20-Jun-2020
Category:
Upload: others
View: 5 times
Download: 0 times
Share this document with a friend
77
Spring 2010 Lecture 1: Introduction Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info Course Administration info. Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/ The Saylor Foundation 1
Transcript
Page 1: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Spring 2010

Lecture 1: IntroductionLecture 1: Introduction

Intro. to Computer Language Engineering Course Administration infoCourse Administration info.

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 1

Page 2: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

OutlineOutline

• Course Administration Information • Course Administration Information • Introduction to computer language engineering

– Why do we need a compiler? – What are compilers? – Anatomy of a compiler

Saman Amarasinghe 2 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 2

Page 3: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Course AdministrationCourse Administration

• Staff• Staff • Optional Text • Course Outline • The Projectj• Project Groups • GradingGrading

Saman Amarasinghe 3 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 3

Page 4: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Reference TextbooksReference Textbooks • Modern Compiler Implementation in Java (Tiger book)

A.W. Appel Cambridge University Press, 1998

A textbook tutorial on compiler implementation, including techniques for many language Cambridge University Press, 1998

ISBN 0-52158-388-8

• Advanced Compiler Design and Implementation (Whale book) Steven Muchnick

techniques for many language features

Essentially a recipe book of ti i ti l t d Steven Muchnick

Morgan Kaufman Publishers, 1997 ISBN 1-55860-320-4

• Compilers: Principles Techniques and Tools (Dragon book)

optimizations; very complete and suited for industrial practitioners and researchers.

Th l i il t tb k • Compilers: Principles, Techniques and Tools (Dragon book) Aho, Lam, Sethi and Ullman Addison-Wesley, 2006 ISBN 0321486811

The classic compilers textbook, although its front-end emphasis reflects its age. New edition has more optimization material.

• Engineering a Compiler (Ark book) Keith D. Cooper, Linda Torczon Morgan Kaufman Publishers, 2003 ISBN 1 55860 698 X

A modern classroom textbook, with increased emphasis on the back-end and implementation techniques ISBN 1 -55860 -698 -X

• Optimizing Compilers for Modern Architectures Randy Allen and Ken Kennedy

techniques.

A modern textbook that focuses on optimizations including

Mor g an Kaufman Publishers, 2001 ISBN 1-55860-286-0

parallelization and memory hierarchy optimization

Saman Amarasinghe 5 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 4

Page 5: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

The Project: The Five SegmentsThe Project: The Five Segments

Lexical and Syntax Analysis Lexical and Syntax Analysis Semantic Analysis

Code Generation

Data-flow Analysis Data flow Analysis

Optimizations

Saman Amarasinghe 6 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 5

Page 6: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Each Segment...Each Segment...

• Segment Start – Project Description

• Lectures – 2 to 5 lectures

• Project Time – (Design Document)(Design Document) – (Project Checkpoint)

• Project Due• Project Due

Saman Amarasinghe 7 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 6

Page 7: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

t t t

Project GroupsProject Groups

• 1st project is an individual project 1 project is an individual project • Projects 2 to 5 are group projects consists of

3 to 4 students

• Grading All b ( l ) h d– All group members (mostly) get the same grade

Saman Amarasinghe 8 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 7

Page 8: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

GradesGrades

• Compiler project 70%• Compiler project 70%

• In-class Quizzes 30% (10% each)

• In-class mini-quizzes 10% (0.5% each)

Saman Amarasinghe 10 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 8

Page 9: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Grades for the ProjectGrades for the Project

– Scanner/Parser 5% S ti Ch ki 7 5% – Semantic Checking 7.5%

– Code Generation 10% D t fl A l i 7 5% – Data-flow Analysis 7.5%

– Optimizations 30% 60%

Saman Amarasinghe 11 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 9

Page 10: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Optimization SegmentOptimization Segment

• Making programs run fastMaking programs run fast – We provide a test set of applications – Figure-out what will make them run fast – Prioritize and implement the optimizationsPrioritize and implement the optimizations – Compiler derby at the end

• A “similar” application to the test set is provided the day before • The compiler that produced the fastest code is the winnerThe compiler that produced the fastest code is the winner

• Do any optimizations you choose – Including parallelization for multicores

• Grade is divided into: – Documentation 6%

• Justify your optimizations and the selection processy y p p – Optimization Implementation 12%

• Producing correct code – Derby performance 12%

Saman Amarasinghe 12 6.035 ©MIT Fall 1998

Derby performance 12%

30% Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 10

Page 11: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

The QuizThe Quiz

• Three Quizzes • Three Quizzes

• In-Class Quiz – 50 Minutes (be on time!) – Open book, open notes

Saman Amarasinghe 13 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 11

Page 12: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Mini QuizzesMini Quizzes

• You already got one • You already got one. • Given at the beginning of the class; Collected at

th dthe end • Collaboration is OK

• This is in lieu of time consuming problem setsThis is in lieu of time consuming problem sets

Saman Amarasinghe 14 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 12

Page 13: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

OutlineOutline

• Course Administration Information • Course Administration Information • Introduction to computer language engineering

– What are compilers? – Why should we learn about them? – Anatomy of a compiler

Saman Amarasinghe 15 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 13

Page 14: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Why Study Compilers?Why Study Compilers?

• Compilers enable programming at a high level • Compilers enable programming at a high level language instead of machine instructions.

Malleability Portability Modularity Simplicity – Malleability, Portability, Modularity, Simplicity, Programmer Productivity

– Also Efficiency and PerformanceAlso Efficiency and Performance

Saman Amarasinghe 16 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 14

Page 15: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

t t t t

Compilers Construction touches t i i C t S imany topics in Computer Science

• Theory Fi it St A G d P i d fl– Finite State Automata, Grammars and Parsing, data-flow

• Algorithms – Graph manipulation, dynamic programming

• Data structures – Symbol tables, abstract syntax trees

• SystemsSystems – Allocation and naming, multi-pass systems, compiler construction

• Computer Architecture M hi h i t ti l ti i t l k d l t i ll li – Memory hierarchy, instruction selection, interlocks and latencies, parallelism

• Security – Detection of and Protection against vulnerabilities

• Software Engineering – Software development environments, debugging

• Artificial Intelligence

Saman Amarasinghe 17 6.035 ©MIT Fall 1998

Artificial Intelligence – Heuristic based search for best optimizations

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 15

Page 16: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Power of a LanguagePower of a Language

• Can use to describe any action • Can use to describe any action – Not tied to a “context”

M t d ib th ti • Many ways to describe the same action – Flexible

Saman Amarasinghe 18 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 16

Page 17: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

How to instruct a computerHow to instruct a computer

• How about natural languages?How about natural languages? – English?? – “Open the pod bay doors, Hal.”

“I D I f id I t d th t”– “I am sorry Dave, I am afraid I cannot do that” – We are not there yet!!

• Natural Languages: – Powerful, but… – Ambiguous

• Same expression describes many possible actions

Saman Amarasinghe 19 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 17

Page 18: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Programming LanguagesProgramming Languages

• Properties – need to be precise – need to be concise – need to be expressive – need to be at a high-level (lot of abstractions)

Saman Amarasinghe 20 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 18

Page 19: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

t tHigh-level Abstract Description

t L l l I l i D t il to Low-level Implementation Details

President President

General

Sergeant

Saman Amarasinghe 21 6.035 ©MIT Fall 1998

Foot Soldier Figure by MIT OpenCourseWare.

My poll ratings are low, My poll ratings are low, lets invade a small nation

Cross the river and take defensive positions defensive positions

Forward march turn left Forward march, turn left Stop!, Shoot

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 19

Page 20: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

1. How to instruct the computer1. How to instruct the computer

• Write a program using a programming language• Write a program using a programming language– High-level Abstract Description

• Microprocessors talk in assembly language– Low-level Implementation Details

Program

CompilerAssemblyLanguage

Programwritten

in a pTranslationProgramming

Languages

Saman Amarasinghe 22 6.035 ©MIT Fall 1998Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 20

Page 21: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

1. How to instruct the computer1. How to instruct the computer

• Input: High level programming language • Input: High-level programming language • Output: Low-level assembly instructions

• Compiler does the translation:p– Read and understand the program – Precisely determine what actions it requirey q – Figure-out how to faithfully carry-out those actions – Instruct the computer to carry out those actionsInstruct the computer to carry out those actions

Saman Amarasinghe 23 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 21

Page 22: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Input to the CompilerInput to the Compiler

• Standard imperative language (Java C C++)• Standard imperative language (Java, C, C++) – State

• Variables• Variables, • Structures, • Arraysy

– Computation • Expressions (arithmetic, logical, etc.) • Assignment statements • Control flow (conditionals, loops)

P d• Procedures

Saman Amarasinghe 24 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 22

Page 23: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

pace

Output of the CompilerOutput of the Compiler

• State• State – Registers

Memory with Flat Address S– Memory with Flat Address Space

• Machine code – load/store architecture – Load, store instructions – Arithmetic, logical operations on registers – Branch instructions

Saman Amarasinghe 25 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 23

Page 24: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

=

Example (input program)Example (input program)

int sumcalc(int a, int b, int N){

int i, x, y;int i, x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {for(i 0; i <= N; i++) {

x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*y;

}} return x;

}

Saman Amarasinghe 26 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 24

Page 25: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

-

u %ed 0 e

Example (Output assembly code) sumcalc:

pushq %rbpmovq %rsp, %rbpmovl %edi, -4(%rbp)

.size sumcalc, .-sumcalc.section

.Lframe1: .long .LECIE1-.LSCIE1

movl %esi, -8(%rbp)movl %edx, -12(%rbp)movl $0, -20(%rbp)movl $0, -24(%rbp)movl $0, -16(%rbp)

.L2: movl -16(%rbp), %eaxl 12(% b ) %

.LSCIE1:.long 0x0 .byte 0x1 .string "" .uleb128 0x1 .sleb128 -8 .byte 0x10 b t 0cmpl -12(%rbp), %eax

jg .L3 movl -4(%rbp), %eaxleal 0(,%rax,4), %edxleaq -8(%rbp), %raxmovq %rax, -40(%rbp)movl %edx %eax

.byte 0xc

.uleb128 0x7

.uleb128 0x8

.byte 0x90

.uleb128 0x1

.align 8 LECIE1: long LEFDE1- LASFDE1movl %edx, %eax

movq -40(%rbp), %rcxcltd idivl (%rcx)movl %eax, -28(%rbp)movl -28(%rbp), %edximull -16(%rbp), %edx

.LECIE1:.long .LEFDE1 .LASFDE1 .long .LASFDE1-.Lframe1 .quad .LFB2 .quad .LFE2-.LFB2 .byte 0x4 .long .LCFI0-.LFB2 .byte 0xe6(% bp),

movl -16(%rbp), %eaxincl %eax imull %eax, %eaxaddl %eax, %edxleaq -20(%rbp), %raxaddl %edx, (%rax)

.byte

.uleb128 0x10

.byte 0x86

.uleb128 0x2

.byte 0x4

.long .LCFI1-.LCFI0

.byte 0xd movl -8(%rbp), %eaxmovl %eax, %edximull -24(%rbp), %edxleaq -20(%rbp), %raxaddl %edx, (%rax)leaq -16(%rbp), %raxi l (% )

.uleb128 0x6

.align 8

Saman Amarasinghe 27 6.035 ©MIT Fall 1998

incl (%rax) jmp .L2

.L3: movl -20(%rbp), %eaxleave ret

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 25

Page 26: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

t t t t tMapping Time Continuum

C il i I iCompilation to Interpretation • Compile time • Compile time

– Ex: C compiler

Li k ti • Link time – Ex: Binary layout optimizer

• Load time – Ex: JIT compiler

• Run time – Ex: Java InterpreterEx: Java Interpreter

Saman Amarasinghe 28 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 26

Page 27: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Anatomy of a ComputerAnatomy of a Computer

ProgramProgramwritten

in a AssemblyLanguageCompiler

ProgrammingLanguages

Translationp

Saman Amarasinghe 29 6.035 ©MIT Fall 1998Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 27

Page 28: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

L i l A l (S )Lexical Analyzer (Scanner)

Anatomy of a Computer

Program (character stream)L i l A l (S )

Program (character stream)

Token Stream Lexical Analyzer (Scanner)

Token Stream

Saman Amarasinghe 30 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 28

Page 29: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Lexical Analyzer (Scanner)Lexical Analyzer (Scanner)

2 3 4 * ( 1 1 + - 2 2 )

Num(234) mul_op lpar_op Num(11) add_op rpar_opNum(-22)

Saman Amarasinghe 31 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 29

Page 30: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Lexical Analyzer (Scanner)Lexical Analyzer (Scanner)

2 3 4 * ( 1 1 + - 2 2 )

Num(234) mul_op lpar_op Num(11) add_op rpar_opNum(-22)

18..23 + val#ue

V i bl t h ‘#’ h t Not a number

Variable names cannot have ‘#’ character

Saman Amarasinghe 32 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 30

Page 31: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Anatomy of a Computer

L i l A l (S )Program (character stream)

Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)Token Stream

Syntax Analyzer (Parser)Token Stream

Syntax Analyzer (Parser)Parse Tree

Syntax Analyzer (Parser)Parse Tree

Saman Amarasinghe 33 6.035 ©MIT Fall 1998Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 31

Page 32: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Syntax Analyzer (Parser)Syntax Analyzer (Parser)

‘*’ ‘(‘ ‘ ’ ‘)’num ‘*’ ‘(‘ num ‘+’ num ‘)’

<expr>

<expr><expr> <op>

( )*num <expr> ( )

<expr> <expr> <op>

num num+

Saman Amarasinghe 34 6.035 ©MIT Fall 1998

num num+

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 32

Page 33: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Syntax Analyzer (Parser)Syntax Analyzer (Parser)

int * foo(i, j, k))int i;int i; int j; Extra parentheses

{ for(i=0; i j) {fi(i>j)

return j; Missing increment

Not an expression }

Not an expression

Not a keyword

Saman Amarasinghe 35 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 33

Page 34: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Anatomy of a Computer

L i l A l (S )Program (character stream)

Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)Token Stream

Semantic Analyzer

Syntax Analyzer (Parser)Parse Tree

Semantic AnalyzerParse Tree

Intermediate RepresentationSemantic Analyzer

Intermediate RepresentationSemantic Analyzer

Saman Amarasinghe 36 6.035 ©MIT Fall 1998Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 34

Page 35: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Semantic AnalyzerSemantic Analyzer

int * foo(i, j, k)int i;int i; int j; Type not declared

{ int x;

Mismatched return type x = x + j + N;return j;

Mismatched return type

Uninitialized variable used } Undeclared variable

Saman Amarasinghe 37 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 35

Page 36: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Anatomy of a Computer

L i l A l (S )Program (character stream)

Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)Token Stream

Semantic Analyzer

Syntax Analyzer (Parser)Parse Tree

Code OptimizerIntermediate Representation

Semantic Analyzer

Code OptimizerIntermediate Representation

Code OptimizerOptimized Intermediate Representation

Code OptimizerOptimized Intermediate Representation

Saman Amarasinghe 38 6.035 ©MIT Fall 1998Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 36

Page 37: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

OptimizerOptimizer

int sumcalc(int a, int b, int N){

int sumcalc(int a, int b, int N)

{ int i;int x, t, u, v;x = 0;u = ((a<<2)/b);

{

int i;

int x, y;

v = 0;for(i = 0; i <= N; i++) {

t = i+1;x = x + v + t*t;

x = 0;

y = 0;

for(i = 0; i <= N; i++) { v = v + u;

}return x;

}

x = x+4*a/b*i+(i+1)*(i+1);

x = x + b*y;

}}

return x;

}

Saman Amarasinghe 39 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 37

Page 38: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Anatomy of a Computer

L i l A l (S )Program (character stream)

Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)Token Stream

Semantic Analyzer

Syntax Analyzer (Parser)Parse Tree

Code OptimizerIntermediate Representation

Semantic Analyzer

Code Optimizer

Code GeneratorOptimized Intermediate Representation

Code GeneratorOptimized Intermediate Representation

Code GeneratorAssembly code

Code GeneratorAssembly code

Saman Amarasinghe 40 6.035 ©MIT Fall 1998Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 38

Page 39: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Code GeneratorCode Generator

sumcalc: xorl %r8d, %r8dxorl %ecx %ecx

int sumcalc(int a, int b, int N){ xorl %ecx, %ecx

movl %edx, %r9dcmpl %edx, %r8djg .L7 sall $2, %edi

.L5: movl %edi, %eax

{ int i;int x, t, u, v;x = 0;

(( <<2)/b) , cltd idivl %esi leal 1(%rcx), %edxmovl %eax, %r10dimull %ecx, %r10d

u = ((a<<2)/b);v = 0;for(i = 0; i <= N; i++) {

t = i+1; movl %edx, %ecximull %edx, %ecxleal (%r10,%rcx), %eaxmovl %edx, %ecxaddl %eax, %r8d

% 9 %

;x = x + v + t*t;v = v + u;

}t cmpl %r9d, %edx

jle .L5 .L7: movl %r8d, %eax

ret

return x;}

Saman Amarasinghe 41 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 39

Page 40: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Program TranslationProgram Translation

• Correct• Correct – The actions requested by the program has to be

faithfully executedfaithfully executed

• Efficient• Efficient – Intelligently and efficiently use the available resources to

carry out the requestscarry out the requests – (the word optimization is used loosely in the compiler

community – Optimizing compilers are never optimal) community Optimizing compilers are never optimal)

Saman Amarasinghe 42 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 40

Page 41: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Efficient ExecutionEfficient Execution

Cross the river and take

General

Cross the river and take defensive positions

SergeantSergeant

Foot Soldier

Saman Amarasinghe 43 6.035 ©MIT Fall 1998

Figure by MIT OpenCourseWare.

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 41

Page 42: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Efficient ExecutionEfficient Execution

Saman Amarasinghe 44 6.035 ©MIT Fall 1998 Figure by MIT OpenCourseWare.

General

Sergeant Sergeant

Foot Soldier

Cross the river and take Cross the river and take defensive positions

Where to cross the river? Use the bridge upstream or surprise the enemy bridge upstream or surprise the enemy by crossing downstream? How do I minimize the casualties??

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 42

Page 43: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Efficient ExecutionEfficient Execution

President President

General

Saman Amarasinghe 45 6.035 ©MIT Fall 1998

Figure by MIT OpenCourseWare.

My poll ratings are low, My poll ratings are low, lets invade a small nation

Russia or Bermuda? Or just stall for his pollOr just stall for his poll numbers to go up?

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 43

Page 44: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Efficient ExecutionEfficient Execution

• Mapping from High to Low • Mapping from High to Low – Simple mapping of a program to assembly language

produces inefficient executionproduces inefficient execution – Higher the level of abstraction more inefficiency

• If not efficient • If not efficient – High-level abstractions are useless

d• Need to: – provide a high level abstraction – with performance of giving low-level instructions

Saman Amarasinghe 46 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 44

Page 45: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

t t tEfficient Execution help

i h l l f b iincrease the level of abstraction • Programming languages • Programming languages

– From C to OO-languages with garbage collectionwith garbage collection

– Even more abstract definitionsdefinitions

• Microprocessor From simple CISC to RISC to – From simple CISC to RISC to VLIW to ….

Saman Amarasinghe 47 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 45

Page 46: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

The Multicore DilemmaThe Multicore Dilemma

• Superscalars • Multicores• Superscalars • Multicores

High Level Language High Level Languageg g g

m er

High Level Language

Simple von Neumann Machine

Co

pil

e

om

pe

r??

Simple von Neumann Machine

Co

ile

Ha

rdw

are

Multiple exposed cores

rd reSaman Amarasinghe 48 6.035 ©MIT Fall 1998

H w

Ha

rw

ar

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 46

Page 47: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

The Multicore DilemmaThe Multicore Dilemma

• Superscalars • Multicores• Superscalars • Multicores

High Level Languageg g g

m er

Parallel Language

Simple von Neumann Machine

Co

pil

e Parallel Language

m rSimple von Neumann Machine

Co

mp

ile

r

Ha

rdw

are

Multiple exposed cores

rd reSaman Amarasinghe 49 6.035 ©MIT Fall 1998

H w

Ha

rw

ar

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 47

Page 48: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

=

Optimization ExampleOptimization Example

int sumcalc(int a, int b, int N){

int i;int x, y;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {for(i 0; i <= N; i++) {

x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*y;

}} return x;

}

Saman Amarasinghe 50 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 48

Page 49: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

45pushq %rbpmovq %rsp, %rbpmovl %edi, -4(%rbp)movl %esi, -8(%rbp)

l % d 12(% b ) movl %edx, -12(%rbp) movl $0, -20(%rbp)movl $0, -24(%rbp)movl $0, -16(%rbp)

.L2: movl -16(%rbp), %eaxcmpl 12(%rbp) %eaxcmpl

-

12(%rbp), %eax jg .L3 movl -4(%rbp), %eaxleal 0(,%rax,4), %edxleaq -8(%rbp), %raxmovq %rax -40(%rbp)movq %rax, 40(%rbp) movl %edx, %eax movq -40(%rbp), %rcxcltd idivl (%rcx)movl %eax, -28(%rbp), ( p) movl -28(%rbp), %edximull -16(%rbp), %edxmovl -16(%rbp), %eaxincl %eax imull %eax, %eaxaddl %eax, %edxleaq -20(%rbp), %raxaddl %edx, (%rax)movl -8(%rbp), %eaxmovl %eax, %edxi ll 24(% b ) % dimull

-24(%rbp), %edx

leaq -20(%rbp), %raxaddl %edx, (%rax)leaq -16(%rbp), %raxincl (%rax)jmp L2

Saman Amarasinghe 51 6.035 ©MIT Fall 1998

jmp

.

L2 .L3: movl -20(%rbp), %eax

leave ret

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 49

Page 50: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

0

Lets Optimize...Lets Optimize...

int sumcalc(int a, int b, int N){

int i, x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {o ( ; ; ) {

x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*y;

}} return x;

}

Saman Amarasinghe 52 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 50

Page 51: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Constant PropagationConstant Propagation

int i, x, y;x = 0; y = 0;for(i = 0; i <= N; i++) {

x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*y;

}return x;

Saman Amarasinghe 53 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 51

Page 52: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Constant PropagationConstant Propagation

int i, x, y;

x = 0; y = 0; for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*y;

}return x;

Saman Amarasinghe 54 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 52

Page 53: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Constant PropagationConstant Propagation

i t iint i, x, y;x = 0; y = 0; for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*0;

}return x;

Saman Amarasinghe 55 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 53

Page 54: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Algebraic SimplificationAlgebraic Simplification

i t iint i, x, y;x = 0; y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*0;

}return x;

Saman Amarasinghe 56 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 54

Page 55: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Algebraic SimplificationAlgebraic Simplification

i t iint i, x, y;x = 0; y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*0;

}return x;

Saman Amarasinghe 57 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 55

Page 56: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Algebraic SimplificationAlgebraic Simplification

i t iint i, x, y;x = 0; y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1); x = x;

}return x;

Saman Amarasinghe 58 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 56

Page 57: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Copy PropagationCopy Propagation

i t iint i, x, y;x = 0; y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x;

}return x;

Saman Amarasinghe 59 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 57

Page 58: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Copy PropagationCopy Propagation

i t iint i, x, y;x = 0; y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x;

}return x;

Saman Amarasinghe 60 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 58

Page 59: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Copy PropagationCopy Propagation

i t iint i, x, y;x = 0; y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);

}return x;

Saman Amarasinghe 61 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 59

Page 60: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

=

Common Subexpression EliminationCommon Subexpression Elimination

int i x y;int i, x, y; x = 0; y = 0;y 0; for(i = 0; i <= N; i++) {

x = x + (4*a/b)*i + (i+1)*(i+1);}} return x;

Saman Amarasinghe 62 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 60

Page 61: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

=

Common Subexpression EliminationCommon Subexpression Elimination

int i x y;int i, x, y; x = 0; y = 0;y 0; for(i = 0; i <= N; i++) {

x = x + (4*a/b)*i + (i+1)*(i+1); }} return x;

Saman Amarasinghe 63 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 61

Page 62: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Common Subexpression EliminationCommon Subexpression Elimination

i t i tint i, x, y, t; x = 0; y = 0;for(i = 0; i <= N; i++) {

t = i+1; x = x + (4*a/b)*i + t*t;

}return x;

Saman Amarasinghe 64 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 62

Page 63: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Dead Code EliminationDead Code Elimination

i t i tint i, x, y, t; x = 0; y = 0;for(i = 0; i <= N; i++) {

t = i+1; x = x + (4*a/b)*i + t*t;

}return x;

Saman Amarasinghe 65 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 63

Page 64: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Dead Code EliminationDead Code Elimination

i t i tint i, x, y, t; x = 0; y = 0;for(i = 0; i <= N; i++) {

t = i+1; x = x + (4*a/b)*i + t*t;

}return x;

Saman Amarasinghe 66 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 64

Page 65: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Dead Code EliminationDead Code Elimination

i t i tint i, x, t; x = 0;

for(i = 0; i <= N; i++) {t = i+1; x = x + (4*a/b)*i + t*t;

}return x;

Saman Amarasinghe 67 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 65

Page 66: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Loop Invariant RemovalLoop Invariant Removal

i t i tint i, x, t; x = 0;

for(i = 0; i <= N; i++) {t = i+1; x = x + (4*a/b)*i + t*t;

}return x;

Saman Amarasinghe 68 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 66

Page 67: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Loop Invariant RemovalLoop Invariant Removal

i t i tint i, x, t; x = 0;

for(i = 0; i <= N; i++) {t = i+1; x = x + (4*a/b)*i + t*t;

}return x;

Saman Amarasinghe 69 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 67

Page 68: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Loop Invariant RemovalLoop Invariant Removal

i t i tint i, x, t, u; x = 0;

/u = (4*a/b);for(i = 0; i <= N; i++) {

t = i+1; x = x + u*i + t*t;

}return x;

Saman Amarasinghe 70 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 68

Page 69: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Strength ReductionStrength Reduction

i t i tint i, x, t, u; x = 0;

/u = (4*a/b);

for(i = 0; i <= N; i++) {t = i+1; x = x + u*i + t*t;

}return x;

Saman Amarasinghe 71 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 69

Page 70: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Strength ReductionStrength Reduction

i t i tint i, x, t, u; x = 0;

/u = (4*a/b);

for(i = 0; i <= N; i++) {t = i+1; x = x + u*i + t*t;

}return x;

Saman Amarasinghe 72 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 70

Page 71: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

=

Strength ReductionStrength Reduction

int i x t u v;int i, x, t, u, v; x = 0; u = ((a<<2)/b);u ((a<<2)/b); v = 0; for(i = 0; i <= N; i++) { for(i 0; i < N; i++) {

t = i+1; x = x + v + t*t; v = v + u;

}return x;

Saman Amarasinghe 73 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 71

Page 72: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Register AllocationRegister Allocation

fpfp Local variable X Local variable Y Local variable I

Saman Amarasinghe 74 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 72

Page 73: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Register AllocationRegister Allocation

fpfp Local variable X Local variable Y Local variable I

$r8d = X $r9d = t $r10d = u $ebx = v $ecx = i

Saman Amarasinghe 75 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 73

Page 74: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

=

Optimized ExampleOptimized Example

int sumcalc(int a, int b, int N){

int i, x, t, u, v; x = 0;x 0;u = ((a<<2)/b);v = 0;for(i = 0; i <= N; i++) {for(i 0; i <= N; i++) {

t = i+1;x = x + v + t*t;

+v = v + u;}return x;

}

Saman Amarasinghe 76 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 74

Page 75: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

xorl %r8d, %r8dxorl %ecx, %ecxmovl %edx, %r9d

pushq %rbpmovq %rsp, %rbpmovl %edi, -4(%rbp)

Unoptimized Code Optimized Code movl %edx, %r9d cmpl %edx, %r8djg .L7 sall $2, %edi

.L5:

movl

%edi, %eaxcltd idivl %esi leal 1(%rcx), %edx movl %eax %r10d

movl %edi, 4(%rbp) movl %esi, -8(%rbp) movl %edx, -12(%rbp) movl $0, -20(%rbp) movl $0, -24(%rbp) movl $0, -16(%rbp)

.L2:

movl

-16(%rbp), %eax cmpl -12(%rbp), %eax jg L3 movl %eax, %r10d

imull %ecx, %r10dmovl %edx, %ecximull %edx, %ecxleal (%r10,%rcx), %eax movl %edx, %ecxaddl %eax, %r8dcmpl %r9d, %edxjl L5

jg .L3 movl -4(%rbp), %eax leal 0(,%rax,4), %edx leaq -8(%rbp), %rax movq %rax, -40(%rbp) movl %edx, %eax movq -40(%rbp), %rcx cltd idi l (% ) jle .L5

.L7:

movl

%r8d, %eaxret

idivl (%rcx) movl %eax, -28(%rbp) movl -28(%rbp), %edx imull -16(%rbp), %edx movl -16(%rbp), %eax incl %eax imull %eax, %eaxaddl %eax, %edxleaq -20(%rbp), %rax addl %edx, (%rax) movl -8(%rbp), %eax movl %eax, %edximull -24(%rbp), %edx leaq -20(%rbp), %rax addl %edx, (%rax) leaq -16(%rbp), %raxq ( p), incl (%rax)jmp .L2

.L3:

movl

-20(%rbp), %eax leave ret

Inner Loop:Inner Loop: 10*mov + 5*lea + 5*add/inc + 4*div/mul + 5*cmp/br/jmp

29 i i

4*mov + 2*lea + 1*add/inc+ 3*div/mul + 2*cmp/br/jmp

12 i i

Saman Amarasinghe 77 6.035 ©MIT Fall 1998

= 29 instructions = 12 instructions Execution time = 17 sec Execution time = 43 sec

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 75

Page 76: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

Compilers Optimize Programs for…Compilers Optimize Programs for…

• Performance/Speed• Performance/Speed • Code Size • Power Consumption • Fast/Efficient Compilation/ p • Security/Reliability • DebuggingDebugging

Saman Amarasinghe 78 6.035 ©MIT Fall 1998 Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 76

Page 77: 6.035 Lecture 1, Introduction · 2018-02-22 · Lecture 1: Inntroduction troduction Intro. to Computer Language Engineering Course Administration info. ... • Introduction to computer

MIT OpenCourseWarehttp://ocw.mit.edu

6.035 Computer Language Engineering Spring 2010

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/

The Saylor Foundation 77


Recommended