7.4 Boolean Expression and Control Flow Statements

Slides:



Advertisements
Similar presentations
Logic Gates.
Advertisements

Intermediate Code Generation. 2 Intermediate languages Runtime environments Declarations Expressions Statements.
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.
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.
Compiler Designs and Constructions
Chapter 8 Intermediate code generation Section 0 Overview 1.Position of Intermediate code generator parser Token stream static checker Syntax tree Intermedi.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Theory of Compilation Erez Petrank Lecture 7: Intermediate Representation Part 2: Backpatching 1.
Theory of Compilation Erez Petrank Lecture 6: Intermediate Representation 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.
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.
Copyright 2013, 2010, 2007, Pearson, Education, Inc. Section 3.7 Switching Circuits.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Chapter 8: Intermediate Code Generation
國立台灣大學 資訊工程學系 薛智文 98 Spring Intermediate Code Generation (textbook ch# 6.1–6.4, 6.5.1–6.5.3,
Intermediate Code Generation
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,
Topic #7: Intermediate Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Intermediate Code Generation CS308 Compiler Theory1.
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.
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.
Translation Scheme for Addressing Array Elements
Logic Gates.
Logic Gates Benchmark Companies Inc PO Box Aurora CO
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 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
Logic Gates.
Intermediate Code Generation Part II
Intermediate code generation
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
CSc 453 Intermediate Code Generation
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generation Part I
Intermediate Code Generation
THREE ADDRESS CODE GENERATION
Section 3.7 Switching Circuits
Intermediate Code Generation Part II
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:

7.4 Boolean Expression and Control Flow Statements Two goals of Boolean expression Calculate logic value c = (a > b) && a > 1 Conditional expression in control flow if (a > b && a > 1) then … 1/25

7.4 Boolean Expression and Control Flow Statements Two ways of calculation: if ( 1 or i++ > 3) then Non short-circuit E1 or E2 Short-circuit E1 or E2 => if E1 then true else E2 E1 and E2 => if E1 then E2 else false

7.4 Boolean Expression and Control Flow Statements 7.4.1 Translation for Boolean Expressions E  E or E | E and E | not E | ( E ) | id relop id | true | false a < b 100: if a < b goto 103 101: t := 0 102: goto 104 103: t := 1 104: >, >=, <, <=, <>, == 对于a<b的翻译而言,由于三地址代码中不包括形如t=a<b的格式,故此使用if x relop y goto L 来实现。

7.4 Boolean Expression and Control Flow Statements E  E1 or E2 {E.place := newtemp; emit (E.place, ‘:=’, E1.place, ‘or’ E2.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’ ) } a < b的翻译 100: if a < b goto 103 101: t := 0 102: goto 104 103: t := 1 104:

7.4 Boolean Expression and Control Flow Statements 7.4.2 Translation of control flow statements S  if E then S1 | if E then S1 else S2 | while E do S1 | S1; S2 5/25

7.4 Boolean Expression and Control Flow Statements To E.true To E.true E.code To E.false E.code To E.false E.true: E.true: S1.code S1.code . . . S1.next: goto S.next (S1.next) E.false: S2.code E s1 (a) if--then . . . E s1 s2 (b) if-then-else E.code S1.code E.true: . . . To E.true To E.false goto S.begin S.begin: (c) while-do S1.code S2.code S1.next: . . . (d) S1; S2 E s1

7.4 Boolean Expression and Control Flow Statements E.code S1.code E.true: . . . To E.true To E.false (a) if-then 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 }

7.4 Boolean Expression and Control Flow Statements E.code S1.code E.true: . . . To E.true To E.false E.false: goto S.next S2.code (b) if-then-else 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}

7.4 Boolean Expression and Control Flow Statements 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) } E.code S1.code E.true: . . . To E.true To E.false goto S.begin S.begin: (c) while-do

7.4 Boolean Expression and Control Flow Statements S  S1; S2 {S.code := S1.code || gen(S1.next, ‘:’) || S2.code } S1.code S2.code S1.next: . . . (d) S1; S2 10/25

7.4 Boolean Expression and Control Flow Statements 7.4.3 Translation for Boolean Expression based Control Flow If E of a < b format, The corresponding code is: if a < b goto E.true goto E.false

7.4 Boolean Expression and Control Flow Statements The code for a < b or c < d and e < f is: if a < b goto Ltrue goto L1 L1: if c < d goto L2 goto Lfalse L2: if e < f goto Ltrue

7.4 Boolean Expression and Control Flow Statements To E1.true E1.code To E1.false E1.true: 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 } E2.code . . . E1 and E2

7.4 Boolean Expression and Control Flow Statements 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  (E1 ) {E1.true := E.true; E.code := E1.code }

7.4 Boolean Expression and Control Flow Statements 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 } 15/25

7.4 Boolean Expression and Control Flow Statements 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  not E1 {E1.true := E.false; E1.false := E.true; E.code := E1.code }

7.4 Boolean Expression and Control Flow Statements E:a < b, The code is: if a < b goto E.true goto E.false E  id1 relop id2 {E.code := gen(‘if’, id1.place, relop.op, id2.place, ‘goto’, E.true) || gen(‘goto’, E.false) } E  true {E.code := gen(‘goto’, E.true)} E  false {E.code := gen(‘goto’, E.false)}

7.4 Boolean Expression and Control Flow Statements 7.4.4 Translation for Switch Statement switch E begin case V1: S1 case V2: S2 . . . case Vn - 1: Sn – 1 default: Sn end

7.4 Boolean Expression and Control Flow Statements Small number of cases: t := E’s code if t  V1 goto L1 S1’s code goto next L1: if t  V2 goto L2 S2’s code L2: . . . . . . Ln-2: if t  Vn-1 goto Ln-1 Sn -1’s code | goto next Ln-1: Sn’s code next:

7.4 Boolean Expression and Control Flow Statements Large number of cases, put the test code for the cases together t := E's code | Ln: Sn's code goto test | goto next L1: S1's code |test: if t = V1 goto L1 goto next | if t = V2 goto L2 L2: S2's code | . . . goto next | if t = Vn-1 goto Ln-1 . . . | goto Ln Ln-1: Sn -1's code | next: goto next 20/25

7.4 Boolean Expression and Control Flow Statements In IR, add a special case, to facilitate the special processing for it test: case V1 L1 case V2 L2 . . . case Vn-1 Ln-1 case t Ln next:

7.4 Boolean Expression and Control Flow Statements 7.4.5 Translation for procedure calls S  call id (Elist) Elist  Elist, E Elist  E

7.4 Boolean Expression and Control Flow Statements The IR structure of calling id(E1, E2, …, En) E1.place := E1's code E2.place := E2's code . . . En.place := En's code param E1.place param E2.place param En.place call id.place, n

7.4 Boolean Expression and Control Flow Statements S  call id (Elist) {For each E.place in the queue, emit(‘param’, E.place); emit(‘call’, id.plase, n) } Elist  Elist, E {Push E.place to the end of queue} Elist  E {Initialize the queue, make it contain only E.place} E1.place | E2.place | E3.place | E4.place …..

Revision Types of IRs, and the transformation between them Structure of the symbol table, and the storage of the scope information Two ways of realization for Boolean expressions IR structures for assignment/control flow statements, and their translation schemes IR structures for procedure call, and the translation schemes 25/25