Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * 60 1. Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *

Slides:



Advertisements
Similar presentations
CSC 4181 Compiler Construction Code Generation & Optimization.
Advertisements

CPSC 388 – Compiler Design and Construction
Symbol Table.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
Intermediate Code Generation
1 Chapter 8: Code Generation. 2 Generating Instructions from Three-address Code Example: D = (A*B)+C =* A B T1 =+ T1 C T2 = T2 D.
8. Code Generation. Generate executable code for a target machine that is a faithful representation of the semantics of the source code Depends not only.
CS 31003: Compilers Introduction to Phases of Compiler.
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Intermediate Code. Local Optimizations
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
COP4020 Programming Languages
CSC 338: Compiler design and implementation
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.
The TINY sample language and it’s compiler
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
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”
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Introduction to Code Generation and Intermediate Representations
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Compilers Computer Symbol Table Output Scanner (lexical analysis)
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
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.
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.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Compiler Summary Lexical analysis—scanner – String of characters  token – Finite automata. Syntactical analysis – parser – Sequence of tokens –> language.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
Component 1.6.
CS 3304 Comparative Languages
A Simple Syntax-Directed Translator
Compiler Construction (CS-636)
CS 363 – Chapter 1 What is a programming language? Kinds of languages
Optimization Code Optimization ©SoftMoore Consulting.
Chapter 1: Introduction to Compiling (Cont.)
Compiler Construction
CSE401 Introduction to Compiler Construction
Compilers B V Sai Aravind (11CS10008).
Compiler design.
Intermediate Code Generation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Structures 1. Overview Objective
Presentation transcript:

Dr. Hussien Sharaf Dr Emad Nabil

Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 * Semantic analyzer := id 1 + id 2 * id 3 60 := id 1 + id 2 * id 3 60 inttoreal 4. Intermediate code generator temp 1 := inttoreal(60) temp 2 := id 3 * temp 1 temp 3 := id 2 + temp 2 id 1 := temp3 5. Code optimizer temp 1 := id 3 * 60.0 id 1 := id 2 + temp 1 6. Code generator MOVF id 3, R 2 MULF #60.0, R 2 MOVF id 2, R 1 ADDF R 2, R 1 MOVF R 1, id 1

 The semantics of a program are its meaning as opposed to syntax or structure.  The semantics consist of:  Runtime semantics: behavior of program at run time.  Static semantics: checked by the compiler. Dr. Hussien M. Sharaf 3

 Static semantics include:  Declaration of variables and constants before use. i.e. int x; x = 3 ;  Calling functions that exist (predefined in a library or defined by the user) i.e. n = Max(4,7); int Max(int x, int y) { int z; if(x > y) z = x; else z = y; return z; } Dr. Hussien M. Sharaf 4

 Passing parameters properly.  Type checking, i.e. int x, y; x = 3; y = 2.5;  performs an error, cause 2.5 is not int it is float data type.  Static semantics can not be checked by the parser. Dr. Hussien M. Sharaf 5

 The semantic analyzer does the following:  Checks the static semantics of the language.  Annotates the syntax tree with type information, as shown in example.  $ Dr. Hussien M. Sharaf 6 Real a, x; Int y; a := x + y * 2.5; := real id a real+ real id x real * real inttorealliteral 2.5 real id y integer Annotated syntax tree

 Intermediate language called “Three-address code”.  Should have two important properties:  Should be easy to produce.  Should be easy to translate to the target language.  A TAC instruction have at most one instruction per line and can have at most three operands. Dr. Hussien M. Sharaf 7

 Example:  A TAC instruction have at most one instruction per line and can have at most three operands. Dr. Hussien M. Sharaf 8 Three-address code temp1 := inttoreal(y) temp2 := temp1 * 2.5 temp3 := x + temp2 a := temp3 := real id a real+ real id x real * real inttorealliteral 2.5 real id y integer a := x + y * 2.5;

 Code optimization can be applied to:  Intermediate code – independent of the target machine.  Target code – dependent on the target machine. Dr. Hussien M. Sharaf 9

 Intermediate code optimization include: A. Constant folding. B. Elimination of common sub- expressions. C. Identification and elimination of unreachable code (called dead code). D. Improving loops. E. Improving function calls. Dr. Hussien M. Sharaf 10

 Simplifying constant expressions at compile time.  Example: i = 320 * 200 * 32 In this example modern compilers identify constructs, and substitute the computed values at compile time (in this case – 2,048,000) Dr. Hussien M. Sharaf 11

 Replace the common expressions with a single variable holding the computed value.  Example: Dr. Hussien M. Sharaf 12 a = b * c + g; d = b * c * e; tmp = b * c; a = tmp + g; d = tmp * e;

 Dead code is the code in a program which is executed but whose result is never used in any other computation.  Dead code wastes computation time.  Example: int f (int x, int y) { int z = x + y; should be eliminated. return x * y; } Dr. Hussien M. Sharaf 13

 There are a lot of strategies for improving loops.  A simple one is: Move loop invariants out of the loop  Example:  This link : illustrates all the strategies for interested readers. Dr. Hussien M. Sharaf 14 For y = 0 to Height-1 For x = 0 to Width-1 ' //y*Width is invariant i = y*Width + x Process i Next x Next y For y = 0 to Height-1 temp = y*Width For x = 0 to Width-1 i = temp + x Process i Next x Next y

 One strategy is by Removing recursion (function that calls itself).  Example of recursive function: unsigned int factorial(unsigned int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } }  After removing recursion and using loops instead it will be: unsigned int factorial(unsigned int n) { int result = 1; if (n == 0) { return 1; } else { for (i = n; i>=1; i--) { result = result * i; } return result; } Dr. Hussien M. Sharaf 15

 Target Code optimization is done by improving the intermediate code, and removing the redundant code.  Example: temp 1 := id 3 * 60.0 id 1 := id 2 + temp 1 Dr. Hussien M. Sharaf 16 Temp1 := int-to-real(60) Temp2 := id3 * temp1 Temp3 := id2 + temp2 Id1 := temp3

 Target code optimization include: a. Allocation and use of registers. b. Selection of better (safer) instructions and addressing modes. Dr. Hussien M. Sharaf 17

 Generates code for the target machine.  Selects appropriate machine instructions.  Allocates memory locations for variables.  Allocates registers for intermediate computations. Dr. Hussien M. Sharaf 18 temp1 := int2real(y) temp2 := temp1 * 2.5 temp3 := x + temp2 a : = temp3 LOADIR1, y;; R1= y MOVFF1, R1;; F1= int2real(R1) MULFF2, F1, 2.5;; F2= F1 * 2.5 LOADFF3, x;; F3= x ADDFF4, F3, F2 ;; F4= F3 + F2 STORFa, F4;; a= F4 Three-address codeAssembly code

 Generation target code example: temp := id3 * 60.0 id1 := id2 + temp Dr. Hussien M. Sharaf 19 MOVF R2, id3;; R2= id3 MULF R2, #60.0 ;; R2= R2 * 60.0 MOVF R1, id2 ;; R1= id2 ADDFR1, R2 ;; R1= R2 + R1 MOVF id1, R1;; id1= R1

Dr. Hussien M. Sharaf