82
Fall 2005 Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info.

Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Fall 2005

Lecture 1: Introduction

Intro. to Computer Language EngineeringCourse Administration info.

Page 2: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 2 6.035 ©MIT Fall 1998

Outline

• Course Administration Information

• Introduction to computer language engineering– What are compilers? – Why should we learn about them?– Anatomy of a compiler

Page 3: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 3 6.035 ©MIT Fall 1998

Course Administration

• Staff• Text• Course Outline• The Project• Project Groups• Grading

Page 4: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 4 6.035 ©MIT Fall 1998

Staff• Lecturers

– Prof. Saman Amarasinghe– Prof. Martin Rinard

Page 5: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 5 6.035 ©MIT Fall 1998

Optional Textbooks

• Modern Compiler Implementation in Java by A.W. Appel Cambridge University Press, 1998

• Advanced Compiler Design and Implementation by Steven MuchnickMorgan Kaufman Publishers, 1997

• Compilers -- Principles, Techniques and Tools by Aho, Sethi and Ullman Addison-Wesley, 1988

TigerBook

WhaleBook

DragonBook

Page 6: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 6 6.035 ©MIT Fall 1998

The Five Segments

Lexical and Syntax AnalysisSemantic Analysis Code GenerationData-flow OptimizationsInstruction Optimizations

Page 7: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 7 6.035 ©MIT Fall 1998

Each Segment...

• Segment Start– Project Description

• Lectures– 2 to 4 lectures

• Project Time• (Project checkpoint)• Project Time• Project Due

Page 8: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 8 6.035 ©MIT Fall 1998

Project Groups• Each project group consists of 3 to 4 students

• Grading– All group members (mostly) get the same grade

Page 9: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 9 6.035 ©MIT Fall 1998

Weekly Group Meeting with the TA

• TA will schedule a weekly 30 minute slot• The group will get to:

– Ask questions about the project– Discuss design decisions – Describe what each person is doing– Answer questions the TA has about the project

• TA will use this to measure individual contribution to the project be there!

Page 10: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 10 6.035 ©MIT Fall 1998

Grades

• Compiler project 58%

• Paper Discussion 12% (3% each)

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

Page 11: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 11 6.035 ©MIT Fall 1998

Grades for the Project

– Scanner/Parser 10%– Semantic Checking 10%– Code Generation 14%– Data-flow Opt. 12%– Instruction Opt. 12%

58%

Page 12: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 12 6.035 ©MIT Fall 1998

The Quiz

• Three Quizzes

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

Page 13: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 13 6.035 ©MIT Fall 1998

Paper Discussions• Will be giving out 3 papers• Read the paper, think about…

– Do you like the research? – What is the context of the research? – Did anything surprise you about the paper? – Do the results allow you to evaluate the proposed technique? – Do the authors understand the wider ramifications of their research? – How have things changed since the paper was written? – How important will the research be in the future?

• Write a 150 to 200 word summary of the paper. • Will schedule a one-on-one (15 or 20 minute) meeting with a

professor or a TA to talk about the paper.

Page 14: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 14 6.035 ©MIT Fall 1998

Outline

• Course Administration Information

• Introduction to computer language engineering– What are compilers? – Why should we learn about them?– Anatomy of a compiler

Page 15: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 15 6.035 ©MIT Fall 1998

What is Computer Language Engineering

1. How to give instructions to a computer

2. How to make the computer carryout the instructions efficiently

Page 16: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 16 6.035 ©MIT Fall 1998

Power of a Language

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

• Many ways to describe the same action– Flexible

Page 17: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 17 6.035 ©MIT Fall 1998

How to instruct a computer

• How about natural languages?– English??– “Open the pod bay doors, Hal.”– “I am sorry Dave, I am afraid I cannot do that”– We are not there yet!!

• Natural Languages: – Same expression describes many possible actions– Ambiguous

Page 18: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 18 6.035 ©MIT Fall 1998

How to instruct a computer

• How about natural languages?– English??– “Open the pod bay doors, Hal.”– “I am sorry Dave, I am afraid I cannot do that”– We are not there yet!!

• Use a programming language– Examples: Java, C, C++, Pascal, BASIC, Scheme

Page 19: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 19 6.035 ©MIT Fall 1998

Programming Languages

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

Page 20: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 20 6.035 ©MIT Fall 1998

High-level Abstract Description to Low-level Implementation Details

President

General

Sergeant

Foot Soldier

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

Cross the river and takedefensive positionsCross the river and takedefensive positions

Forward march, turn leftStop!, ShootForward march, turn leftStop!, Shoot

Figures by MIT OCW.

Page 21: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 21 6.035 ©MIT Fall 1998

1. How to instruct the computer

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

CompilerAssembly Language

Translation

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

Program written

in a Programming

Languages

Page 22: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 22 6.035 ©MIT Fall 1998

1. How to instruct the computer

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

• Compiler has to:– Read and understand the program – Precisely determine what actions it require– Figure-out how to carry-out those actions– Instruct the computer to carry out those actions

Page 23: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 23 6.035 ©MIT Fall 1998

Example (input program)

int expr(int n){

int d;d = 4 * n * n * (n + 1) * (n + 1);return d;

}

Page 24: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 24 6.035 ©MIT Fall 1998

Example (Output assembly code)

lda $30,-32($30)stq $26,0($30)stq $15,8($30)bis $30,$30,$15bis $16,$16,$1stl $1,16($15)lds $f1,16($15)sts $f1,24($15)ldl $5,24($15)bis $5,$5,$2s4addq $2,0,$3ldl $4,16($15)mull $4,$3,$2ldl $3,16($15)addq $3,1,$4mull $2,$4,$2ldl $3,16($15)addq $3,1,$4mull $2,$4,$2stl $2,20($15)ldl $0,20($15)br $31,$33

$33:bis $15,$15,$30ldq $26,0($30)ldq $15,8($30)addq $30,32,$30ret $31,($26),1

Page 25: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 25 6.035 ©MIT Fall 1998

Efficient Execution of the Actions

General

Sergeant

Foot Soldier

Cross the river and takedefensive positionsCross the river and takedefensive positions

Figures by MIT OCW.

Page 26: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 26 6.035 ©MIT Fall 1998

Efficient Execution of the Actions

General

Sergeant

Foot Soldier

Cross the river and takedefensive positionsCross the river and takedefensive positions

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

Figures by MIT OCW.

Page 27: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 27 6.035 ©MIT Fall 1998

Efficient Execution of the Actions

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

General

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

President

Figures by MIT OCW.

Page 28: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 28 6.035 ©MIT Fall 1998

Efficient Execution of the Actions

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

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

• If not efficient– High-level abstractions are useless

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

Page 29: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 29 6.035 ©MIT Fall 1998

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

int i;int 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;

}

Page 30: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 30 6.035 ©MIT Fall 1998

45test:subu $fp, 16sw zero, 0($fp) # x = 0sw zero, 4($fp) # y = 0sw zero, 8($fp) # i = 0

lab1: # for(i=0;i<N; i++)mul $t0, $a0, 4 # a*4div $t1, $t0, $a1 # a*4/blw $t2, 8($fp) # imul $t3, $t1, $t2 # a*4/b*ilw $t4, 8($fp) # iaddui$t4, $t4, 1 # i+1lw $t5, 8($fp) # iaddui$t5, $t5, 1 # i+1mul $t6, $t4, $t5 # (i+1)*(i+1)addu $t7, $t3, $t6 # a*4/b*i + (i+1)*(i+1)lw $t8, 0($fp) # xadd $t8, $t7, $t8 # x = x + a*4/b*i + (i+1)*(i+1)sw $t8, 0($fp)lw $t0, 4($fp) # ymul $t1, $t0, a1 # b*ylw $t2, 0($fp) # xadd $t2, $t2, $t1 # x = x + b*ysw $t2, 0($fp)lw $t0, 8($fp) # iaddui $t0, $t0, 1 # i+1sw $t0, 8($fp)ble $t0, $a3, lab1lw $v0, 0($fp)addu $fp, 16b $ra

Page 31: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 31 6.035 ©MIT Fall 1998

Lets Optimize...int sumcalc(int a, int b, int N){

int i;int 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;

}

Page 32: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 32 6.035 ©MIT Fall 1998

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

int i;int 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;

}

Page 33: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 33 6.035 ©MIT Fall 1998

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

int i;int 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;

}

Page 34: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 34 6.035 ©MIT Fall 1998

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

int i;int 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;

}

Page 35: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 35 6.035 ©MIT Fall 1998

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

int i;int 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;

}

Page 36: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 36 6.035 ©MIT Fall 1998

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

int i;int 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;

}

Page 37: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 37 6.035 ©MIT Fall 1998

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

int i;int 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;

}

Page 38: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 38 6.035 ©MIT Fall 1998

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

int i;int 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;

}

Page 39: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 39 6.035 ©MIT Fall 1998

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

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

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

}return x;

}

Page 40: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 40 6.035 ©MIT Fall 1998

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

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

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

}return x;

}

Page 41: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 41 6.035 ©MIT Fall 1998

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

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

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

}return x;

}

Page 42: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 42 6.035 ©MIT Fall 1998

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

int i;int 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;

}

Page 43: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 43 6.035 ©MIT Fall 1998

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

int i;int 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;

}

Page 44: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 44 6.035 ©MIT Fall 1998

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

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

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

}

Page 45: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 45 6.035 ©MIT Fall 1998

Common Subexpression Eliminationint sumcalc(int a, int b, int N){

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

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

}

Page 46: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 46 6.035 ©MIT Fall 1998

Common Subexpression Eliminationint sumcalc(int a, int b, int N){

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

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

}

Page 47: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 47 6.035 ©MIT Fall 1998

Common Subexpression Eliminationint sumcalc(int a, int b, int N){

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

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

}return x;

}

Page 48: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 48 6.035 ©MIT Fall 1998

Common Subexpression Eliminationint sumcalc(int a, int b, int N){

int i;int 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;

}

Page 49: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 49 6.035 ©MIT Fall 1998

Dead Code Eliminationint sumcalc(int a, int b, int N){

int i;int 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;

}

Page 50: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 50 6.035 ©MIT Fall 1998

Dead Code Eliminationint sumcalc(int a, int b, int N){

int i;int 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;

}

Page 51: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 51 6.035 ©MIT Fall 1998

Dead Code Eliminationint sumcalc(int a, int b, int N){

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

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

}return x;

}

Page 52: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 52 6.035 ©MIT Fall 1998

Dead Code Eliminationint sumcalc(int a, int b, int N){

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

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

}return x;

}

Page 53: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 53 6.035 ©MIT Fall 1998

Loop Invariant Removalint sumcalc(int a, int b, int N){

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

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

}return x;

}

Page 54: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 54 6.035 ©MIT Fall 1998

Loop Invariant Removalint sumcalc(int a, int b, int N){

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

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

}return x;

}

Page 55: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 55 6.035 ©MIT Fall 1998

Loop Invariant Removalint sumcalc(int a, int b, int N){

int i;int 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;

}

Page 56: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 56 6.035 ©MIT Fall 1998

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

int i;int 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;

}

Page 57: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 57 6.035 ©MIT Fall 1998

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

int i;int 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;

}

u*0, u*1, v=v+u,u*2, v=v+u,u*3, u*4,...

v=0,

v=v+u,v=v+u,...

Page 58: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 58 6.035 ©MIT Fall 1998

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

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

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

}return x;

}

Page 59: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 59 6.035 ©MIT Fall 1998

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

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

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

}return x;

}

Page 60: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 60 6.035 ©MIT Fall 1998

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

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

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

}return x;

}

Page 61: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 61 6.035 ©MIT Fall 1998

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

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

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

}return x;

}

Page 62: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 62 6.035 ©MIT Fall 1998

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

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

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

}return x;

}

Page 63: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 63 6.035 ©MIT Fall 1998

Register Allocationfp

Local variable XLocal variable YLocal variable I

Page 64: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 64 6.035 ©MIT Fall 1998

Register Allocationfp

Local variable XLocal variable YLocal variable I

$t9 = X$t8 = t$t7 = u$t6 = v$t5 = i

Page 65: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 65 6.035 ©MIT Fall 1998

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

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

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

}return x;

}

Page 66: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 66 6.035 ©MIT Fall 1998

test:subu $fp, 16add $t9, zero, zero # x = 0sll $t0, $a0, 2 # a<<2div $t7, $t0, $a1 # u = (a<<2)/badd $t6, zero, zero # v = 0add $t5, zero, zero # i = 0

lab1: # for(i=0;i<N; i++)addui$t8, $t5, 1 # t = i+1mul $t0, $t8, $t8 # t*taddu $t1, $t0, $t6 # v + t*taddu $t9, t9, $t1 # x = x + v + t*t

addu $6, $6, $7 # v = v + u

addui$t5, $t5, 1 # i = i+1ble $t5, $a3, lab1

addu $v0, $t9, zeroaddu $fp, 16b $ra

Page 67: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 67 6.035 ©MIT Fall 1998

Unoptimized Code Optimized Codetest:

subu $fp, 16add $t9, zero, zerosll $t0, $a0, 2div $t7, $t0, $a1add $t6, zero, zeroadd $t5, zero, zero

lab1:addui $t8, $t5, 1mul $t0, $t8, $t8addu $t1, $t0, $t6addu $t9, t9, $t1addu $6, $6, $7addui $t5, $t5, 1ble $t5, $a3, lab1

addu $v0, $t9, zeroaddu $fp, 16b $ra

test:subu $fp, 16sw zero, 0($fp)sw zero, 4($fp)sw zero, 8($fp)

lab1:mul $t0, $a0, 4div $t1, $t0, $a1lw $t2, 8($fp)mul $t3, $t1, $t2lw $t4, 8($fp)addui $t4, $t4, 1lw $t5, 8($fp)addui $t5, $t5, 1mul $t6, $t4, $t5addu $t7, $t3, $t6lw $t8, 0($fp)add $t8, $t7, $t8sw $t8, 0($fp)lw $t0, 4($fp)mul $t1, $t0, a1lw $t2, 0($fp)add $t2, $t2, $t1sw $t2, 0($fp)lw $t0, 8($fp)addui $t0, $t0, 1sw $t0, 8($fp)ble $t0, $a3, lab1

lw $v0, 0($fp)addu $fp, 16b $ra

4*ld/st + 2*add/sub + br +N*(9*ld/st + 6*add/sub + 4* mul + div + br)= 7 + N*21

6*add/sub + shift + div + br +N*(5*add/sub + mul + br)= 9 + N*7

Execution time = 17 secExecution time = 43 sec

Page 68: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 68 6.035 ©MIT Fall 1998

Compilers Optimize Programs for…

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

Page 69: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 69 6.035 ©MIT Fall 1998

Compilers help increase the level of abstraction

• Programming languages– From C to OO-languages

with garbage collection– Even more abstract

definitions

• Microprocessor– From simple CISC to RISC

to VLIW to ….

Page 70: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 70 6.035 ©MIT Fall 1998

Anatomy of a Computer

Program written

in a Programming

Languages

Assembly Language

TranslationCompiler

Page 71: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 71 6.035 ©MIT Fall 1998

Anatomy of a Computer

Lexical Analyzer (Scanner)Token Stream

Program (character stream)Lexical Analyzer (Scanner)

Token Stream

Program (character stream)

Page 72: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 72 6.035 ©MIT Fall 1998

Lexical Analyzer (Scanner)

18..23 + val#ue

Num(234) mul_op lpar_op Num(11) add_op rpar_op

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

Num(-22)

Page 73: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 73 6.035 ©MIT Fall 1998

Lexical Analyzer (Scanner)

18..23 + val#ue

Num(234) mul_op lpar_op Num(11) add_op rpar_op

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

Num(-22)

Not a number Variable names cannot have ‘#’ character

Page 74: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 74 6.035 ©MIT Fall 1998

Anatomy of a Computer

Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)Token Stream

Parse Tree

Program (character stream)

Syntax Analyzer (Parser)Token Stream

Parse Tree

Page 75: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 75 6.035 ©MIT Fall 1998

Syntax Analyzer (Parser)

num num+

( )*num

<expr>

<expr><expr>

<expr>

<expr>

<expr>

<op>

<op>

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

Page 76: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 76 6.035 ©MIT Fall 1998

Syntax Analyzer (Parser)

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

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

return j;}

Extra parentheses

Missing increment

Not an expression

Not a keyword

Page 77: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 77 6.035 ©MIT Fall 1998

Anatomy of a Computer

Intermediate RepresentationSemantic Analyzer

Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)Token Stream

Parse Tree

Program (character stream)

Intermediate RepresentationSemantic Analyzer

Parse Tree

Page 78: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 78 6.035 ©MIT Fall 1998

Semantic Analyzer

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

{int x;x = x + j + N;return j;

}

Type not declared

Mismatched return type

Uninitialized variable used

Undeclared variable

Page 79: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 79 6.035 ©MIT Fall 1998

Anatomy of a Computer

Code OptimizerOptimized Intermediate Representation

Intermediate RepresentationSemantic Analyzer

Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)Token Stream

Parse Tree

Program (character stream)

Code OptimizerOptimized Intermediate Representation

Intermediate Representation

Page 80: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 80 6.035 ©MIT Fall 1998

Optimizer

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

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

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

}return x;

}

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

int i;int 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;

}

Page 81: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 81 6.035 ©MIT Fall 1998

Anatomy of a Computer

Code Optimizer

Code GeneratorOptimized Intermediate Representation

Assembly code

Intermediate RepresentationSemantic Analyzer

Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)Token Stream

Parse Tree

Program (character stream)

Code GeneratorOptimized Intermediate Representation

Assembly code

Page 82: Lecture 1: Introduction - MIT OpenCourseWare · Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info. ... – Answer questions the TA has about

Saman Amarasinghe 82 6.035 ©MIT Fall 1998

Code Generator

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

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

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

}return x;

}

test:subu $fp, 16add $t9, zero, zerosll $t0, $a0, 2div $t7, $t0, $a1add $t6, zero, zeroadd $t5, zero, zero

lab1:addui $t8, $t5, 1mul $t0, $t8, $t8addu $t1, $t0, $t6addu $t9, t9, $t1addu $6, $6, $7addui $t5, $t5, 1ble $t5, $a3, lab1

addu $v0, $t9, zeroaddu $fp, 16b $ra