tokens parser 1. postfix notation, 2. graphical representation (such as syntax tree or dag), 3. three address code

Similar documents
Figure 28.1 Position of the Code generator

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z)

A Simple Syntax-Directed Translator

Principles of Compiler Design

CSE 504: Compiler Design. Code Generation

A simple syntax-directed

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

VIVA QUESTIONS WITH ANSWERS

Intermediate Representations

COMPILER CONSTRUCTION (Intermediate Code: three address, control flow)

Front End. Hwansoo Han

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Group B Assignment 9. Code generation using DAG. Title of Assignment: Problem Definition: Code generation using DAG / labeled tree.

Intermediate Representations

Intermediate Representations

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

CS 4201 Compilers 2014/2015 Handout: Lab 1

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

CA Compiler Construction

Principles of Compiler Design

2.2 Syntax Definition

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

Intermediate Code Generation

Computer Architecture Lecture No.4 5-Instruction Set Architecture and Design 5-1 Memory locations and Operations. word

Introduction to Compiler

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Variables vs. Registers/Memory. Simple Approach. Register Allocation. Interference Graph. Register Allocation Algorithm CS412/CS413

COMPUTER ORGANIZATION & ARCHITECTURE

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18


Compiler Code Generation COMP360

PRINCIPLES OF COMPILER DESIGN

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

CSE 401/M501 Compilers

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

CSc 453. Compilers and Systems Software. 21 : Code Generation II. Department of Computer Science University of Arizona

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Alternatives for semantic processing

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona

High-level View of a Compiler

Register allocation. TDT4205 Lecture 31

Compiler Design. Lecture 1

Formal Languages and Compilers Lecture I: Introduction to Compilers

Optimization. ASU Textbook Chapter 9. Tsan-sheng Hsu.

Compiling Techniques

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

UNIT IV INTERMEDIATE CODE GENERATION

Overview. EE 4504 Computer Organization. Much of the computer s architecture / organization is hidden from a HLL programmer

Group A Assignment 3(2)

6. Intermediate Representation

Code generation and local optimization

Compiler Design (40-414)

Code generation and local optimization

CS415 Compilers. Intermediate Represeation & Code Generation

LANGUAGE PROCESSORS. Introduction to Language processor:

Question Bank. 10CS63:Compiler Design

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

Summary: Direct Code Generation

Compiler Structure. Lexical. Scanning/ Screening. Analysis. Syntax. Parsing. Analysis. Semantic. Context Analysis. Analysis.

UNIT 3 OPERATORS. [Marks- 12]

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Code Generation: Integrated Instruction Selection and Register Allocation Algorithms

CS 360 Programming Languages Interpreters

Principles of Programming Languages COMP251: Syntax and Grammars

A programming language requires two major definitions A simple one pass compiler

6. Intermediate Representation!

Intermediate Representations & Symbol Tables

CS 132 Compiler Construction

CSC488S/CSC2107S - Compilers and Interpreters. CSC 488S/CSC 2107S Lecture Notes

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

EECS 6083 Intro to Parsing Context Free Grammars

CS606- compiler instruction Solved MCQS From Midterm Papers

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

COMPILER DESIGN. Intermediate representations Introduction to code generation. COMP 442/6421 Compiler Design

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

CSc 453 Interpreters & Interpretation

COMPILERS BASIC COMPILER FUNCTIONS

COMPILER DESIGN. For COMPUTER SCIENCE

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Overview of Compiler. A. Introduction

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Dixita Kagathara Page 1

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Last time. What are compilers? Phases of a compiler. Scanner. Parser. Semantic Routines. Optimizer. Code Generation. Sunday, August 29, 2010

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Compiler Architecture

Compiler Optimization

CSE 230 Intermediate Programming in C and C++

SYLLABUS UNIT - I UNIT - II UNIT - III UNIT - IV CHAPTER - 1 : INTRODUCTION CHAPTER - 4 : SYNTAX AX-DIRECTED TRANSLATION TION CHAPTER - 7 : STORA

Transcription:

Intermediate generation source program lexical analyzer tokens parser parse tree generation intermediate language The intermediate language can be one of the following: 1. postfix notation, 2. graphical representation (such as syntax tree or dag), 3. three address Example: consider the following assignment statement a := b * -c + b * -c This assignment statement can has a corresponding intermediate representation as one of the following: 1. the postfix notation: a b c - * b c - * + := 2. the graphical representation: (a) syntax tree: := a + b - * * b - c c

(b) dag: := a + * b - c 3. the three address : (a) the three address for the above syntax tree is: t 1 := - c t 2 := b * t 1 t 3 := - c t 4 := b * t 3 t 5 := t 2 + t 4 a := t 5 (b) the three address for the above dag is t 1 := - c t 2 := b * t 1 t 5 := t 2 + t 2 a := t 5 2

Dag: a dag gives the same information as the syntax tree but in a more compact way because common expressions are identified. Three address Three address is a sequence of statements of the general form: x := y op z where x, y, and z are variables, constants, or compiler-generated temporary variables; op stands for any operator. Example: a source language expression like x+y*z might be translated into a sequence t 1 := y * z t 2 := x + t 1 because there is only one operator on the right side of a statement. Here t 1 and t 2 are compiler-generated temporary variables. Three address vs. graphical representation Three address is a linearized representation of a syntax tree or a dag in which explicit names correspond to the interior nodes of the graph. 3

Code generation Code generation is the last phase of the compiler. It takes an (optimized) intermediate as its input and produce the equivalent target. The following figure shows this process: generator optimizer optimized generator target program The generator depends on 1. the target language and 2. the operating system We also need to know issues such as 1. memory management 2. instruction selection 3. register allocation 4. evaluation order 4

1. Memory management Memory management is the process of mapping names in the source program to addresses of data objects in run-time memory. This process is done cooperatively by the front end and the generator. 2. Instruction selection The selected set of instructions depends on the nature of the target machine. If we don t care about the efficiency of the target program, instruction selection is straightforward. Example: The three-address statement of the form x := y + z can be translated into the sequence MOV y, R0 /* load y into register R0 */ ADD z, R0 /* add z to R0 */ MOV R0, x /* store R0 into x */ Unfortunately, this kind of statement-by-statement generation often produces poor. For example, would be translated into MOV b, R0 ADD c, R0 MOV R0, a MOV a, R0 ADD e, R0 MOV R0, d a := b + c d := a + e Here the 4th and 3rd statements are redundant. 5

3. Register allocation Using registers yields a shorter and a faster instructions than using memory locations. The use of registers is divided into: 1. register allocation: selecting variables that will reside in registers 2. register assignment: picking up a specific register 4.Evaluation order The order of performing computations can affect the efficiency of the target The target machine Our target machine is described as follows: 1. it is a byte-addressable machine with 4 bytes to a word and n general-purpose registers, R0, R1,..., Rn 1. 2. it has two-address instructions of the form op source, destination where op is an operator (op-), and source and destination are data fields. Examples of op- are: MOV (move source to destination) ADD (add source to destination) SUB (subtract source from destination) MUL (multiply source to destination) DIV (divide source by destination) 6

Storing values Examples of storing the contents of registers into memory locations can be as follows: 1. MOV R0, M stores the contents of register R0 into memory location M 2. MOV 4(R0), M stores the value contents(4 + contents(r0)) into memory location M 3. MOV *4(R0), M stores the value contents(contents(4 + contents(r0))) into memory location M 4. MOV #n, R0 loads the constant number n into register R0 Example: consider the three-address instruction a := b + c where b and c are simple variables denoting distinct memory locations. The corresponding generated could be: MOV b, R0 ADD c, R0 MOV R0, a 7