THREE ADDRESS CODE GENERATION

Slides:



Advertisements
Similar presentations
SYNTAX DIRECTED TRANSLATION 11CS Types of Attributes There are two types of attributes for non- terminals :- Synthesized Attributes : For a non-terminal.
Advertisements

Intermediate Code Generation
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
8 Intermediate code generation
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
Compiler Designs and Constructions
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules 17 and 18 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction.
CH4.1 CSE244 Intermediate Code Generation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax Directed Definitions Synthesized Attributes
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Chapter 8: Intermediate Code Generation
Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program.
Intermediate Code Generation
1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Topic #7: Intermediate Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Intermediate Code Generation Abstraction at the source level identifiers, operators, expressions, statements, conditionals, iteration, functions (user.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Chap. 4, Intermediate Code Generation
1 February 28, February 28, 2016February 28, 2016February 28, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Three Address Code Generation of Control Statements continued..
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Lecture 12 Intermediate Code Generation Translating Expressions
Translation Scheme for Addressing Array Elements
CS 404 Introduction to Compiler Design
Chapter 3 – Describing Syntax
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Intermediate code generation Jakub Yaghob
Review: Chapter 5: Syntax directed translation
Compiler Construction
Compiler Construction
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Intermediate Code Generation Part I
Intermediate Code Generation
Intermediate Code Generation Part II
Three Address Code Generation Control Statement
Intermediate Code Generation
Chapter 6 Intermediate-Code Generation
Intermediate Code Generation Part II
Three Address Code Generation - Control Statements
Intermediate Code Generation Part I
Intermediate Code Generation Part II
Intermediate code generation
Compiler Construction
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
7.4 Boolean Expression and Control Flow Statements
CSc 453 Intermediate Code Generation
Intermediate Code Generation Part I
Three Address Code Generation – Backpatching
SYNTAX DIRECTED DEFINITION
Intermediate Code Generation Part II
Directed Acyclic Graphs (DAG)
Intermediate Code Generation Part II
Review: For array a[2,5], how would the memory for this array looks like using row major layout? What about column major layout? How to compute the address.
Intermediate Code Generation Part I
Compiler Construction
Code generation and data types
Presentation transcript:

THREE ADDRESS CODE GENERATION Consider the following grammar with its translation scheme for producing 3-address statements S  id:= E S.Code := E.Code || gen(id.place ‘=’ E.place) E  E + E E.place := newtemp( ) , E.Code =E1.Code || E2.Code || gen(E.place ‘=’ E1.place + E2.place) E  E * E E.place = newtemp( ) , E.Code := E1.Code || E2.Code || gen(E.place ‘=’ E1.place * E2.place) E  -E E.place = newtemp( ), E.Code := E1.Code || gen (E.place ‘=’ ‘uni –‘ ‘E1.place) E  (E1) E.place = E1.place , E.Code = E1.Code E  id E.place = id.place , E.Code := ‘ ’ Consider the expression x := ( y + z ) * ( -w + v) 1

Consider the following grammar with its translation scheme for producing 3-address statements S  id:= E S.Code := E.Code || gen(id.place ‘=’ E.place) E  E + E E.place := newtemp( ) , E.Code =E1.Code || E2.Code || gen(E.place ‘=’ E1.place + E2.place)

E  E. E E. place = newtemp( ) , E. Code := E1. Code || E2 E  E * E E.place = newtemp( ) , E.Code := E1.Code || E2.Code || gen(E.place ‘=’ E1.place * E2.place) E  -E E.place = newtemp( ), E.Code := E1.Code || gen (E.place ‘=’ ‘uni –‘‘E1.place)

E  (E1) E.place = E1.place , E.Code = E1.Code E  id E.place = id.place , E.Code := ‘ ’

Consider the expression x := ( y + z ) * ( -w + v) Annotated parse tree for the above expression will be :

Three-address code statements for the above expression will be as follows: t1 = y +z t2 = -w t3 = t2 +v t4 = t1 * t3 x = t4

Syntax directed translation for the above expression: Eid | E.place := y Eid | E.place := z EE + E | E.place := t1 E.Code := gen( t1 = y + z ) E(E) | E.place := t1 E.Code := gen( t1 = y + z )

Eid | E.place := w E -E | E.place := t2 E.Code := gen( t2 = -w) Eid | E.place := v EE + E | E.place := t3 E.Code := gen( t2 = -w , t3 = t2 +v )

E (E) | E.place := t3 E.Code := gen( t2 = -w , t3 = t2 + v ) EE * E | E.place = t4 E.Code := gen( t1= y + z , t2 = -w , t3 = t2 + v , t4 = t1 * t3) Sid:=E | S.Code := gen(t1= y + z , t2 = -w , t3 = t2 + v , t4 = t1 * t3 , x=t4)

NOTES S.code represents the three address code for assignment S Non terminal E has 2 attributes E.place, the name that will hold the value of E E.code, the sequence of three-address statements evaluating E The function newtemp returns a sequence of distinct names t1,t2 … in successive calls

ARRAYS: 1 Dimensional array X[i] = A A = X[i] where X is the starting point and i is the offset from X. Address of A[i] in terms of base address , low and high (generally low = 0)

=Base + ( i – low)*w (w= size of each address block in bytes) = ( Base – low*w ) + i * w.

2 Dimensional array A[ i , j ] Address of A[i] in terms of base n1 , n2 , l1 , l2 ( l1 , l2 correspond to low of the two dimensions n1 , n2 correspond to the size of each array)

=Base + n2 * w * ( i – l1 ) + ( j – l2 ) * w = ( Base –( n2*l1 + l2 )*w ) + w*(n2*i + j)

-Prepared By -ARINDAM BARAL(04CS1034).