Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

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.
Control Structures.
Chapter 6 Intermediate Code Generation
Intermediate Code Generation
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
Comp Sci Control structures 1 Ch. 5 Control Structures.
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.
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 Construction Sohail Aslam Lecture Boolean Expressions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist.
Compiler Designs and Constructions
Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Lecture 15 Control Flow Topics Review Positional Encoding of Booleans Short Circuit Evaluation Control Flow Statements Readings: 8.4, 8.6 March 13, 2006.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Intermediate Code. Local Optimizations
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.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
C Chuen-Liang Chen, NTUCS&IE / 279 CONTROL STRUCTURES Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
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.
Machine Independent Macro Processor Features Concatenation of Macro Parameters Generation of Unique Labels Conditional Macro Expansion Keyword Macro.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Code Generation How to produce intermediate or target code.
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..
Conditional statements and boolean expressions Arithmetic, relational and logical operators.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
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.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
Loops in Java.
Compiler Construction
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Flow of Control.
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
Flow of Control.
Program Flow Control Selection & repetition
Intermediate Code Generation Part II
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
Three Address Code Generation – Backpatching
Flow of Control.
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.
Compiler Construction
Compiler Design 21. Intermediate Code Generation
Controlling Program 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.
Presentation transcript:

Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements, the effect of such boolean expression can be represented by the position of the program after the expression is evaluated. Jump code can be directly generated without evaluating the expressions explicitly. The value of a boolean expression can sometimes be determined without evaluating the whole expression. Example: if (p && (p->next)) { p = p->next;}

Code generation for flow of control statements: S->if E then S S->if E then S else S S->while E do S –S -> if E then S1 Evaluate E into t If (t = true) goto ETRUE goto EFALSE ETRUE: code for S1 EFLASE:

Code generation for flow of control statements: –S -> if E then S1 else S2 ??? –S -> While E DO S1 ??? –How about these two? S -> Repeat S until E S -> For id := E1 to E2 do S1 endfor

Short circuit code for the control flow statements If E then S1 If E then S1 else S2

Short circuit code for the control flow statements

Code generation for flow of control statements: S->if E then S S->if E then S else S S->while E do S Attributes: E.true: the label to which control flows if E is true. E.false: the label to while control flows if E is false E/S.code: three-address code for E/S S.next: the next three-address code following the three address code of S. Functions to be used: || concatenate three address code

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); }

–Control flow translation of boolean expressions: Basic idea: generate the jumping code without evaluating the whole boolean expression. Example: Let E = a < b, we will generate the code as (1) If a < b then goto E.true (2) Goto T.false Grammar: E->E or E | E and E | not E | (E) | id relop id | true | false.

E -> E1 or E2 { E1.true = E.true; E1.false = newlabel; E2.true = E.true; E2.false = E.false; E.code = E1.code || gen(E1.false ‘:’) || E2.code} E->E1 and E2 {E1.true = newlabel; E1.false = E.false; E2.true = E.true; E2.false = E.false; E.code = E1.code || gen(E1.true ‘:’) || E2.code} E->not E {E1.true = E.false; E1.false = E.true; E.code = E1.code} E->(E1) {E1.true = E.true; E1.false = E.false; E.code = E1.code;} E->id1 relop id2 {E.code = gen(‘if’ id1.place relop.op id2.place ‘goto’ E.true); gen (‘goto’ E.false);} E->true {gen(‘goto’ E.true);} E->false{gen(‘goto’ E.false);}

Example: a < b or (c < d and e < f) Example: while a< b do if c < d then x := y + z; else x: = y – z;