1 Code Generation. 2 Position of a Code Generator in the Compiler Model Front-End Code Optimizer Source program Symbol Table Lexical error Syntax error.

Slides:



Advertisements
Similar presentations
Compiler Construction Sohail Aslam Lecture Code Generation  The code generation problem is the task of mapping intermediate code to machine code.
Advertisements

Target Code Generation
1 CS 201 Compiler Construction Machine Code Generation.
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:-
1 Registers and MAL - Part I. Motivation So far there are some details that we have ignored instructions can have different formats most computers have.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Implementation of a Stored Program Computer
ARM C Language & Assembler. Using C instead of Java (or Python, or your other favorite language)? C is the de facto standard for embedded systems because.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
410/510 1 of 20 Week 1 – Lecture 1 Introduction The Textbook Assessment Programming & Tools A v. small compiler Compiler Construction.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Execution of an instruction
Computer Organization and Architecture Instructions: Language of the Machine Hennessy Patterson 2/E chapter 3. Notes are available with photocopier 24.
What Do I Represent?. Translators – Module Knowledge Areas Revisiting object code When we disassemble code we can view the opcodes used This is just a.
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.
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter# 6 Code generation.  The final phase in our compiler model is the code generator.  It takes as input the intermediate representation(IR) produced.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Chapter 1: Introduction 1 Compiler Designs and Constructions Chapter 1: Introduction Objectives: Course Objectives Introduction Dr. Mohsen Chitsaz.
Compilers I CNS History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language.
Compilers Modern Compiler Design
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
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.
Operand Addressing And Instruction Representation Tutorial 3.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Lecture 2: Instruction Set Architecture part 1 (Introduction) Mehran Rezaei.
Computer Organization Instructions Language of The Computer (MIPS) 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Topic #9: Target Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
Code Generation Part I Chapter 8 (1st ed. Ch.9)
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Advanced Computer Systems
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
Language Translation Compilation vs. interpretation.
The University of Adelaide, School of Computer Science
Introduction to Compiler Construction
Code Generation Part I Chapter 9
Introduction to System Programming
Code Generation.
Computer Programming Machine and Assembly.
Unit IV Code Generation
CSc 453 Interpreters & Interpretation
Code Generation Part I Chapter 8 (1st ed. Ch.9)
Lesson Objectives Aims Key Words Compiler, interpreter, assembler
The University of Adelaide, School of Computer Science
Code Generation Part I Chapter 9
Under Address Modes Source: under
Under Address Modes Source: under
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Compiler Construction
Code Generation Part II
Target Code Generation
TARGET CODE GENERATION
CSc 453 Interpreters & Interpretation
Presentation transcript:

1 Code Generation

2 Position of a Code Generator in the Compiler Model Front-End Code Optimizer Source program Symbol Table Lexical error Syntax error Semantic error Intermediate code Code Generator Intermediate code Target program

3 Code Generation Code produced by compiler must be correct –Source-to-target program transformation should be semantics preserving Code produced by compiler should be of high quality –Effective use of target machine resources –Heuristic techniques should be used to generate good but suboptimal code, because generating optimal code is undecidable

4 Target Program Code The back-end code generator of a compiler may generate different forms of code, depending on the requirements: –Absolute machine code (executable code) –Relocatable machine code (object files for linker) –Assembly language (facilitates debugging) –Byte code forms for interpreters (e.g. JVM)

5 The Target Machine Implementing code generation requires thorough understanding of the target machine architecture and its instruction set Our (hypothetical) machine: –Byte-addressable (word = 4 bytes) –Has n general purpose registers R0, R1, …, R n-1 –Two-address instructions of the form op source, destination

6 The Target Machine: Op-codes and Address Modes Op-codes (op), for example MOV (move content of source to destination) ADD (add content of source to destination) SUB (subtract content of source from dest.) Address modes ModeFormAddressAdded Cost Absolute MM 1 Register RR 0 Indexed c(R)c(R)c+contents( R ) 1 Indirect register *R contents( R ) 0 Indirect indexed *c(R)*c(R)contents(c+contents( R )) 1 Literal #c#c N/A1

7 Instruction Costs Machine is a simple, non-super-scalar processor with fixed instruction costs Realistic machines have deep pipelines, I-cache, D-cache, etc. Define the cost of instruction = 1 + cost(source-mode) + cost(destination-mode)

8 InstructionOperationCost MOV R0,R1 Store content( R0 ) into register R1 1 MOV R0,M Store content( R0 ) into memory location M 2 MOV M,R0 Store content( M ) into register R0 2 MOV 4(R0),M Store contents(4+contents( R0 )) into M 3 MOV *4(R0),M Store contents(contents(4+contents( R0 ))) into M 3 MOV #1,R0 Store 1 into R0 2 ADD 4(R0),*12(R1) Add contents(4+contents( R0 )) to value at location contents(12+contents( R1 ))3 Examples

9 Instruction Selection Instruction selection is important to obtain efficient code Suppose we translate three-address code x := y + z to: MOV y,R0 ADD z,R0 MOV R0, x a:=a+1 MOV a,R0 ADD #1,R0 MOV R0,a ADD #1,aINC a Cost = 6 Cost = 3Cost = 2 BetterBest

10 Instruction Selection: Utilizing Addressing Modes Suppose we translate a:=b+c into MOV b,R0 ADD c,R0 MOV R0,a Assuming addresses of a, b, and c are stored in R0, R1, and R2 MOV *R1,*R0 ADD *R2,*R0 Assuming R1 and R2 contain values of b and c ADD R2,R1 MOV R1,a

11 Need for Global Machine- Specific Code Optimizations Suppose we translate three-address code x := y + z to: MOV y,R0 ADD z,R0 MOV R0, x Then, we translate a:=b+c d:=a+e to: MOV a,R0 ADD b,R0 MOV R0,a MOV a,R0 ADD e,R0 MOV R0,d Redundant

12 Register Allocation and Assignment Efficient utilization of the limited set of registers is important to generate good code Registers are assigned by –Register allocation to select the set of variables that will reside in registers at a point in the code –Register assignment to pick the specific register that a variable will reside in Finding an optimal register assignment in general is NP-complete

13 Example t:=a*b t:=t+a t:=t/d MOV a,R1 MUL b,R1 ADD a,R1 DIV d,R1 MOV R1,t t:=a*b t:=t+a t:=t/d MOV a,R0 MOV R0,R1 MUL b,R1 ADD R0,R1 DIV d,R1 MOV R1,t { R1 = t } { R0 = a, R1 = t }

14 Choice of Evaluation Order When instructions are independent, their evaluation order can be changed t1:=a+b t2:=c+d t3:=e*t2 t4:=t1-t3 a+b-(c+d)*e MOV a,R0 ADD b,R0 MOV R0,t1 MOV c,R1 ADD d,R1 MOV e,R0 MUL R1,R0 MOV t1,R1 SUB R0,R1 MOV R1,t4 t2:=c+d t3:=e*t2 t1:=a+b t4:=t1-t3 MOV c,R0 ADD d,R0 MOV e,R1 MUL R0,R1 MOV a,R0 ADD b,R0 SUB R1,R0 MOV R0,t4 reorder