Three Address Code Generation Control Statement

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Control Flow Statements: Repetition/Looping
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Chapter 13 Control Structures in C. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Control Structures if Statement if-else Statement switch.
Code Generation for Control Flow Mooly Sagiv Ohad Shacham Chapter 6.4.
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
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
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.
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.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
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.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
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. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
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.
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
CIS 3301 C# Lesson 3 Control Statements - Selection.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Three Address Code Generation of Control Statements continued..
Conditional statements and boolean expressions Arithmetic, relational and logical operators.
Algorithm & Flow Charts Decision Making and Looping
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 6: Loops.
Algorithm & Flow Charts Week 1
Loops in Java.
Compiler Construction
Intermediate Code Generation
Control Structures.
Flow of Control.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Control Structures – Selection
11/10/2018.
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Flow of Control.
Three Address Code Generations for Boolean Functions
Data Types, Identifiers, and Expressions
Three Address Code Generation - Control Statements
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
7.4 Boolean Expression and Control Flow Statements
Logical Operations In Matlab.
CS139 October 11, 2004.
Compiler Design 21. Intermediate Code Generation
Statement-Level Control Structures
Three Address Code Generation – Backpatching
THREE ADDRESS CODE GENERATION
Whitebox Testing.
General Condition Loop
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Compiler Construction
Compiler Design 21. Intermediate Code Generation
CSC215 Lecture Control Flow.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Review: What is an activation record?
The return Statement © 2018 Kris Jordan.
Presentation transcript:

Three Address Code Generation Control Statement Robin Anil (04CS3005)

Basic Idea 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. This is done by skipping to different parts of the code (label) to mimic the different flow of control branches.

Special Function 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.

Attributes 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.

FOR Loop a=3; b=4; for(i=0;i<n;i++){ a=b+1; a=a*a; } c=a;

FOR Loop a=3; b=4; i=0; L1: VAR1=i<n; if(VAR1) goto L2; goto L3; L4: i++; goto L1; L2: VAR2=b+1; a=VAR2; VAR3=a*a; a=VAR3; goto L4 L3: c=a;

WHILE Loop a=3; b=4; i=0; while(i<n){ a=b+1; a=a*a; i++; } c=a;

WHILE Loop a=3; b=4; i=0; L1: VAR1=i<n; if(VAR1) goto L2; goto L3; L2: VAR2=b+1; a=VAR2; VAR3=a*a; a=VAR3; i++; goto L1 L3: c=a;

DO WHILE LOOP a=3; b=4; i=0; do{ a=b+1; a=a*a; i++; }while(i<n); c=a;

DO WHILE LOOP a=3; b=4; i=0; L1: VAR2=b+1; a=VAR2; VAR3=a*a; a=VAR3; VAR1=i<n; if(VAR1) goto L1; goto L2; L2: c=a;

Example -2 while a < b do If c < d then x = y + z else x = y - z

Here the productions used are: 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)}

Using the above rules and assignment statements we get the 3 address code as: L1: if a < b goto L2 goto Lnext L2: if c < d goto L3 goto L4 L3: t1 = y +z x = t1 goto L1 L4: t2 = y – z x = t2 Lnext: