Saman Amarasinghe 16.035 ©MIT Fall 1998 Outline Course Administration Information Introduction to computer language engineering –What are compilers? –

Slides:



Advertisements
Similar presentations
Programming Languages Third Edition Chapter 6 Syntax.
Advertisements

Course Outline Traditional Static Program Analysis Software Testing
Compilers and Language Translation
Introduction to Advanced Topics Chapter 1 Mooly Sagiv Schrierber
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Cpeg421-08S/final-review1 Course Review Tom St. John.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)
Overview of Compiler Design CIS 631, CSE 691, CIS400, CSE 400 Compiler Design Dr. Nancy McCracken January 15, 2008.
1 Lecture 7: Computer Arithmetic Today’s topics:  Chapter 2 wrap-up  Numerical representations  Addition and subtraction Reminder: Assignment 3 will.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic0: Introduction José Nelson Amaral
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
Invitation to Computer Science 5th Edition
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
CSC 338: Compiler design and implementation
1 Introduction to Compilation Aaron Bloomfield CS 415 Fall 2005.
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
Project: ACM Compiler 2014 Shanghai Jiao Tong University Class ACM 2011 Dong Xie.
Introduction: Compiler Design CSC532. Outline Course related info What are compilers? Why learning? Introductory Anatomy of Compiler.
Joey Paquet, 2000, 2002, 2007, Concordia University Department of Computer Science COMP 442/6421 Compiler Design.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
1 Compiler 3.2 A Level computing. 2 So, Sir what is a compiler?
CS 671 Compilers Prof. Kim Hazelwood Spring 2008.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 1, 08/28/03 Prof. Roy Levow.
CPS 506 Comparative Programming Languages Syntax Specification.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Introduction to Compiling
Introduction CPSC 388 Ellen Walker Hiram College.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Compiler Construction Composed By, Muhammad Bilal Qureshi.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Compilers Computer Symbol Table Output Scanner (lexical analysis)
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
What Do Compilers Do 1 A compiler acts as a translator, transforming human-oriented programming languages into computer-oriented machine languages. Ignore.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
CHAPTER 1 INTRODUCTION TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
System Software Theory (5KS03).
Compiler principles Compiler Jakub Yaghob.
CS510 Compiler Lecture 1.
Introduction to Advanced Topics Chapter 1 Text Book: Advanced compiler Design implementation By Steven S Muchnick (Elsevier)
CMPUT Compiler Design and Optimization
课程名 编译原理 Compiling Techniques
Compiler Lecture 1 CS510.
CS416 Compiler Design lec00-outline September 19, 2018
An Overview to Compiler Design
Course supervisor: Lubna Siddiqui
Introduction CI612 Compiler Design CI612 Compiler Design.
Subject: Language Processor
CS416 Compiler Design lec00-outline February 23, 2019
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Presentation transcript:

Saman Amarasinghe ©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

Saman Amarasinghe ©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 Muchnick Morgan Kaufman Publishers, 1997 Compilers -- Principles, Techniques and Tools by Aho, Sethi and Ullman Addison-Wesley, 1988 Tiger Book Whale Book Dragon Book

Saman Amarasinghe ©MIT Fall 1998 The Five Segments  Lexical and Syntax Analysis  Semantic Analysis  Code Generation  Data-flow Optimizations  Instruction Optimizations

Saman Amarasinghe ©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

Saman Amarasinghe ©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

Saman Amarasinghe ©MIT Fall 1998 How to instruct a computer How about natural languages? –E–English?? –“–“Open the pod bay doors, Hal.” –“–“I am sorry Dave, I am afraid I cannot do that” –W–We are not there yet!! Natural Languages: –S–Same expression describes many possible actions –A–Ambiguous

Saman Amarasinghe ©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

Saman Amarasinghe ©MIT Fall 1998 Programming Languages Properties –need to be unambiguous –need to be precise –need to be concise –need to be expressive –need to be at a high-level (lot of abstractions)

Saman Amarasinghe ©MIT Fall 1998 High-level Abstract Description to Low-level Implementation Details Foot Soldier President My poll ratings are low, lets invade a small nation General Cross the river and take defensive positions Sergeant Forward march, turn left Stop!, Shoot

Saman Amarasinghe ©MIT Fall How to instruct the computer Compiler Assembly Language Translation Microprocessors talk in assembly language –Low-level Implementation Details Program written in a Programming Languages Write a program using a programming language – High-level Abstract Description

Saman Amarasinghe ©MIT Fall 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

Saman Amarasinghe ©MIT Fall 1998 Example (input program) int expr(int n) { int d; d = 4 * n * n * (n + 1) * (n + 1); return d; }

Saman Amarasinghe ©MIT Fall 1998 Example (Output assembly code) lda $30,-32($30) stq $26,0($30) stq $15,8($30) bis $30,$30,$15 bis $16,$16,$1 stl $1,16($15) lds $f1,16($15) sts $f1,24($15) ldl $5,24($15) bis $5,$5,$2 s4addq $2,0,$3 ldl $4,16($15) mull $4,$3,$2 ldl $3,16($15) addq $3,1,$4 mull $2,$4,$2 ldl $3,16($15) addq $3,1,$4 mull $2,$4,$2 stl $2,20($15) ldl $0,20($15) br $31,$33 $33: bis $15,$15,$30 ldq $26,0($30) ldq $15,8($30) addq $30,32,$30 ret $31,($26),1

Saman Amarasinghe ©MIT Fall 1998 Efficient Execution of the Actions Foot Soldier General Cross the river and take defensive positions Sergeant

Saman Amarasinghe ©MIT Fall 1998 Efficient Execution of the Actions Foot Soldier General Cross the river and take defensive positions Sergeant Where to cross the river? Use the bridge upstream or surprise the enemy by crossing downstream? How do I minimize the casualties??

Saman Amarasinghe ©MIT Fall 1998 Efficient Execution of the Actions President My poll ratings are low, lets invade a small nation General Russia or Bermuda? Or just stall for his poll numbers to go up?

Saman Amarasinghe ©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

Saman Amarasinghe ©MIT Fall 1998 Example 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; }

Saman Amarasinghe ©MIT Fall 1998 test: subu $fp, 16 sw zero, 0($fp)# x = 0 sw zero, 4($fp)# y = 0 sw zero, 8($fp)# i = 0 lab1:# for(i=0;i<N; i++) mul $t0, $a0, 4# a*4 div $t1, $t0, $a1# a*4/b lw $t2, 8($fp)# i mul $t3, $t1, $t2# a*4/b*i lw $t4, 8($fp)# i addui$t4, $t4, 1# i+1 lw $t5, 8($fp)# i addui$t5, $t5, 1# i+1 mul $t6, $t4, $t5# (i+1)*(i+1) addu $t7, $t3, $t6# a*4/b*i + (i+1)*(i+1) lw $t8, 0($fp)# x add $t8, $t7, $t8# x = x + a*4/b*i + (i+1)*(i+1) sw $t8, 0($fp) lw $t0, 4($fp)# y mul $t1, $t0, a1# b*y lw $t2, 0($fp)# x add $t2, $t2, $t1# x = x + b*y sw $t2, 0($fp) lw $t0, 8($fp)# i addui$t0, $t0, 1# i+1 sw $t0, 8($fp) ble $t0, $a3, lab1 lw $v0, 0($fp) addu $fp, 16 b $ra 45

Saman Amarasinghe ©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; }

Saman Amarasinghe ©MIT Fall 1998 Constant Propagation 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; }

Saman Amarasinghe ©MIT Fall 1998 Constant Propagation 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; }

Saman Amarasinghe ©MIT Fall 1998 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; } Constant Propagation

Saman Amarasinghe ©MIT Fall 1998 Constant Propagation 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; }

Saman Amarasinghe ©MIT Fall 1998 Constant Propagation 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*0; } return x; }

Saman Amarasinghe ©MIT Fall 1998 Algebraic Simplification 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*0; } return x; }

Saman Amarasinghe ©MIT Fall 1998 Algebraic Simplification 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*0; } return x; }

Saman Amarasinghe ©MIT Fall 1998 Algebraic Simplification 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 + 0; } return x; }

Saman Amarasinghe ©MIT Fall 1998 Algebraic Simplification 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 + 0; } return x; }

Saman Amarasinghe ©MIT Fall 1998 Algebraic Simplification 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 + 0; } return x; }

Saman Amarasinghe ©MIT Fall 1998 Algebraic Simplification 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; } return x; }

Saman Amarasinghe ©MIT Fall 1998 Copy Propagation 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; } return x; }

Saman Amarasinghe ©MIT Fall 1998 Copy Propagation 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); } return x; }

Saman Amarasinghe ©MIT Fall 1998 Common Subexpression Elimination 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); } return x; }

Saman Amarasinghe ©MIT Fall 1998 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); } return x; } Common Subexpression Elimination

Saman Amarasinghe ©MIT Fall 1998 int 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; } Common Subexpression Elimination

Saman Amarasinghe ©MIT Fall 1998 int 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; } Common Subexpression Elimination

Saman Amarasinghe ©MIT Fall 1998 Dead Code Elimination int 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; }

Saman Amarasinghe ©MIT Fall 1998 Dead Code Elimination int 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; }

Saman Amarasinghe ©MIT Fall 1998 Dead Code Elimination int 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; }

Saman Amarasinghe ©MIT Fall 1998 Dead Code Elimination int 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; }

Saman Amarasinghe ©MIT Fall 1998 Loop Invariant Removal int 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; }

Saman Amarasinghe ©MIT Fall 1998 Loop Invariant Removal int 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; }

Saman Amarasinghe ©MIT Fall 1998 Loop Invariant Removal int 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; }

Saman Amarasinghe ©MIT Fall 1998 Strength Reduction int 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; }

Saman Amarasinghe ©MIT Fall 1998 Strength Reduction int 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, u*2, u*3, u*4,... v=0, v=v+u,...

Saman Amarasinghe ©MIT Fall 1998 Strength Reduction int 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; }

Saman Amarasinghe ©MIT Fall 1998 Strength Reduction int 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; }

Saman Amarasinghe ©MIT Fall 1998 Strength Reduction int 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; }

Saman Amarasinghe ©MIT Fall 1998 Strength Reduction int 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; }

Saman Amarasinghe ©MIT Fall 1998 Strength Reduction 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; }

Saman Amarasinghe ©MIT Fall 1998 Register Allocation fp Local variable X Local variable Y Local variable I

Saman Amarasinghe ©MIT Fall 1998 Register Allocation $t9 = X $t8 = t $t7 = u $t6 = v $t5 = i fp Local variable X Local variable Y Local variable I

Saman Amarasinghe ©MIT Fall 1998 Optimized Example 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; }

Saman Amarasinghe ©MIT Fall 1998 test: subu $fp, 16 add $t9, zero, zero# x = 0 sll $t0, $a0, 2# a<<2 div $t7, $t0, $a1# u = (a<<2)/b add $t6, zero, zero# v = 0 add $t5, zero, zero# i = 0 lab1:# for(i=0;i<N; i++) addui$t8, $t5, 1# t = i+1 mul $t0, $t8, $t8# t*t addu $t1, $t0, $t6# v + t*t addu $t9, t9, $t1# x = x + v + t*t addu $6, $6, $7# v = v + u addui$t5, $t5, 1# i = i+1 ble $t5, $a3, lab1 addu $v0, $t9, zero addu $fp, 16 b $ra

Saman Amarasinghe ©MIT Fall 1998 test: subu $fp, 16 add $t9, zero, zero sll $t0, $a0, 2 div $t7, $t0, $a1 add $t6, zero, zero add $t5, zero, zero lab1: addui $t8, $t5, 1 mul $t0, $t8, $t8 addu $t1, $t0, $t6 addu $t9, t9, $t1 addu $6, $6, $7 addui $t5, $t5, 1 ble $t5, $a3, lab1 addu $v0, $t9, zero addu $fp, 16 b $ra test: subu $fp, 16 sw zero, 0($fp) sw zero, 4($fp) sw zero, 8($fp) lab1: mul $t0, $a0, 4 div $t1, $t0, $a1 lw $t2, 8($fp) mul $t3, $t1, $t2 lw $t4, 8($fp) addui $t4, $t4, 1 lw $t5, 8($fp) addui $t5, $t5, 1 mul $t6, $t4, $t5 addu $t7, $t3, $t6 lw $t8, 0($fp) add $t8, $t7, $t8 sw $t8, 0($fp) lw $t0, 4($fp) mul $t1, $t0, a1 lw $t2, 0($fp) add $t2, $t2, $t1 sw $t2, 0($fp) lw $t0, 8($fp) addui $t0, $t0, 1 sw $t0, 8($fp) ble $t0, $a3, lab1 lw $v0, 0($fp) addu $fp, 16 b $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 Unoptimized CodeOptimized Code Execution time = 17 secExecution time = 43 sec

Saman Amarasinghe ©MIT Fall 1998 Compilers Optimize Programs for… Performance/Speed Code Size Power Consumption Fast/Efficient Compilation Security/Reliability Debugging

Saman Amarasinghe ©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 ….

Saman Amarasinghe ©MIT Fall 1998 Anatomy of a Computer Program written in a Programming Languages Assembly Language Translation Compiler

Saman Amarasinghe ©MIT Fall 1998 Anatomy of a Computer Lexical Analyzer (Scanner) Token Stream Program (character stream) Lexical Analyzer (Scanner) Token Stream Program (character stream)

Saman Amarasinghe ©MIT Fall 1998 Lexical Analyzer (Scanner) val#ue Num(234)mul_oplpar_opNum(11)add_oprpar_op 2 34 *( ) Num(-22)

Saman Amarasinghe ©MIT Fall 1998 Lexical Analyzer (Scanner) val#ue Num(234)mul_oplpar_opNum(11)add_oprpar_op 2 34 *( ) Num(-22) Not a number Variable names cannot have ‘#’ character

Saman Amarasinghe ©MIT Fall 1998 Anatomy of a Computer Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Token Stream Parse Tree Program (character stream) Syntax Analyzer (Parser) Token StreamParse Tree

Saman Amarasinghe ©MIT Fall 1998 Syntax Analyzer (Parser) num + ()* num ‘*’ ‘(‘ num ‘+’ num ‘)’

Saman Amarasinghe ©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 expressionNot a keyword

Saman Amarasinghe ©MIT Fall 1998 Anatomy of a Computer Intermediate Representation Semantic Analyzer Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Token Stream Parse Tree Program (character stream) Intermediate Representation Semantic Analyzer Parse Tree

Saman Amarasinghe ©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

Saman Amarasinghe ©MIT Fall 1998 Anatomy of a Computer Code Optimizer Optimized Intermediate Representation Intermediate Representation Semantic Analyzer Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Token Stream Parse Tree Program (character stream) Code Optimizer Optimized Intermediate RepresentationIntermediate Representation

Saman Amarasinghe ©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; }

Saman Amarasinghe ©MIT Fall 1998 Anatomy of a Computer Code Optimizer Code Generator Optimized Intermediate Representation Assembly code Intermediate Representation Semantic Analyzer Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Token Stream Parse Tree Program (character stream) Code Generator Optimized Intermediate RepresentationAssembly code

Saman Amarasinghe ©MIT Fall 1998 Code Generator test: subu $fp, 16 add $t9, zero, zero sll $t0, $a0, 2 div $t7, $t0, $a1 add $t6, zero, zero add $t5, zero, zero lab1: addui $t8, $t5, 1 mul $t0, $t8, $t8 addu $t1, $t0, $t6 addu $t9, t9, $t1 addu $6, $6, $7 addui $t5, $t5, 1 ble $t5, $a3, lab1 addu $v0, $t9, zero addu $fp, 16 b $ra 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; }