Three Address Code Generation of Control Statements continued..

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

Intermediate Code Generation
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
Backpatching: The syntax directed definition we discussed before can be implemented in two or more passes (we have both synthesized attributes and inheritent.
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
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.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Compiler Designs and Constructions
Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Lecture 09 – IR (Backpatching) Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6
Theory of Compilation Erez Petrank Lecture 7: Intermediate Representation Part 2: Backpatching 1.
Lecture 15 Control Flow Topics Review Positional Encoding of Booleans Short Circuit Evaluation Control Flow Statements Readings: 8.4, 8.6 March 13, 2006.
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.
CH4.1 CSE244 Intermediate Code Generation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f.
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.
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.
C Chuen-Liang Chen, NTUCS&IE / 279 CONTROL STRUCTURES Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
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.
Boolean expressions 1 productionsemantic action E  E1 or E2E1.trueLabel = E.trueLabel; E1.falseLabel = freshLabel(); E2.trueLabel = E.trueLabel; E2.falseLabel.
Intermediate code generation Simplifying initial assumptions a 3-address code will be used for instructions  choice of instruction mnemonics and layouts.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Intermediate code generation Jakub Yaghob
Compiler Construction
Compiler Construction
Intermediate Code Generation
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 Generations for Boolean Functions
Three Address Code Generation Control Statement
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
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generation Part I
Three Address Code Generation – Backpatching
THREE ADDRESS CODE GENERATION
Intermediate Code Generation Part II
Intermediate Code Generation
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
Compiler Design 21. Intermediate Code Generation
Presentation transcript:

Three Address Code Generation of Control Statements continued..

PRESENTED BY : Anchal Nema Roll No:04CS3004

Boolean Expressions: Boolean expressions are composed of the Boolean operators (and, or, and not) applied to the elements that are Boolean variables or relational expressions. E  E or E | E and E | not E | id1 relop id2 | true | false | (E)

Methods of implementing Boolean expressions There are two principal methods of representing the value of a Boolean expression. The first method is to encode true and false numerically and to evaluate a Boolean expression analogously to an arithmetic expression. The second principle method is by flow of control, that is, representing the value of a Boolean expression by a position reached in a program. Here we have adopted the first method

Semantic Actions for producing Three Address Codes for Boolean Expressions: E  E1 or E2 E.place := newtemp(); emit (E.place ‘:=’ E1.place ‘or’ E2.place); E  E1 and E2 E.place := newtemp(); emit (E.place ‘:=’ E1.place ‘and’ E2.place); E  not E E.place := newtemp(); emit ( E.place ‘:=’ ‘not’ E.place);

E  ( E1 ) E.place := E1.place; E  id1 relop id2 E.place := newtemp; emit (‘if’ id1.place relop.op id2.place ‘goto’ nextstat+3); emit (E.place ‘:=’ ‘0’); emit (‘goto’ nextstat+2); emit (E.place ‘:=’ ‘1’); E  true E.place := newtemp; emit (E.place ‘:=’ ‘1’); E  false E.place = newtemp; emit (E.place ‘:=’ ‘0’);

Note: E.place stands for the variable name. Here we assume that emit places three Address statements into an output file in the right format,that nextstat gives the index of the three address statement in the output sequence, and that emit increments nextstat after producing each three address statement. newtemp is the function creating a new temporary variable for 3 address code.

Short-Circuit Code We can translate a Boolean expression into three address code without generating code for any of the Boolean operators and without having the code necessarily evaluate the entire expression. This is called Short-Circuit or Jumping code.

Parse Tree of a<b or c<d and e<f E E id (a) relop (<) id (b) orE E id (c) relop (<) id (d) andE id (e) relop (<) id (f)

Translation of a<b or c<d and e<f(Short Circuit code) 100: if a<b goto : t1 := 0 102: goto : t1 := 1 104: ifc<d goto : t2 := 0 106: goto : t2 := 1 108: if e<f goto : t3 := 0 110: goto : t3 := 1 112: t4 := t2 and t3 113: t5 := t1 or t4

Functions and Attributes used in the translation of control Statements :- Flow of control statements may be converted to three address code by use of the following functions:- newlabel – returns a new symbolic label each time it is called. gen ( ) – “generates” the code (string) passed as a parameter to it.

The following attributes are associated with the non-terminals for the code generation:- code – contains the generated three address code. true – contains the label to which a jump takes place if the Boolean expression associated (if any) evaluates to “true”. false – contains the label to which a jump takes place if the Boolean expression (if any) associated evaluates to “false”. begin – contains the label / address pointing to the beginning of the code chunk for the statement “generated” (if any) by the non- terminal. next - contains the label / address pointing to the end of the code chunk for the statement “generated” (if any) by the non- terminal

BASIC CONCEPT INVLOVED:- The basic idea of converting any flow of control statement to a three address code is to simulate the “branching” of the flow of control using the goto statement. This is done by skipping to different parts of the code (label) to mimic the different flow of control branches.

EXAMPLES:- Suppose we have the following grammar:- S  if E then S1 S  if E then S1 else S2 S  while E do S1

The simulation of the flow of control branching for each statement is depicted pictorially as follows E.true: E.false: … S 1.code E.code … S 1.code E.code S 2.code E.true: E.false: S.next: goto S.next to E.true to E.false if - thenif – then - else

… S 1.code E.code S.begin: E.true: E.false: goto S.begin to E.true to E.false while - do

SEMANTIC RULES:- S  if E then S1 E.true := newlabel ; E.false := S.next ; S1.next := S.next ; S.code := E.code || gen(E.true ‘:’) || S1.code

S  if E then S1 else S2 E.true := newlabel ; E.false := newlabel ; S1.next := S.next ; S2.next := S.next ; S.code := E.code || gen(E.true ‘:’) || S1.code || gen(‘goto’ S.next) || gen(E.false ‘:’) || S2.code

S  while E do S1 S.begin := newlabel ; E.true := newlabel ; E.false := S.next ; S1.next := S.begin ; S.code := gen(S.begin ‘:’) || E.code || gen(E.true ‘:’) || S1.code || gen(‘goto’ S.begin)

THANK YOU