Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

Similar documents
CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

Module 25 Control Flow statements and Boolean Expressions

Formal Languages and Compilers Lecture X Intermediate Code Generation

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

Module 26 Backpatching and Procedures

Syntax-Directed Translation

CMPSC 160 Translation of Programming Languages. Three-Address Code

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7

Flow of Control Execution Sequence

Module 27 Switch-case statements and Run-time storage management

CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona

CS2210: Compiler Construction. Code Generation

CSCI Compiler Design

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSc 520 Principles of Programming Languages

Intermediate Code Generation

University of Arizona, Department of Computer Science. CSc 453 Assignment 5 Due 23:59, Dec points. Christian Collberg November 19, 2002

CSc 520 Principles of Programming Languages

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 7, vrijdag 2 november 2018

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

Intermediate Representations

THEORY OF COMPILATION

CSc 520 Principles of Programming Languages

UNIT IV INTERMEDIATE CODE GENERATION

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Languages and Compiler Design II IR Code Generation I

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

CSc 520 Principles of Programming Languages

Dixita Kagathara Page 1

CSc 453 Intermediate Code Generation

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Intermediate Code Generation

Introduction. Inline Expansion. CSc 553. Principles of Compilation. 29 : Optimization IV. Department of Computer Science University of Arizona

University of Arizona, Department of Computer Science. CSc 520 Assignment 4 Due noon, Wed, Apr 6 15% Christian Collberg March 23, 2005

CSc 453. Semantic Analysis III. Basic Types... Basic Types. Structured Types: Arrays. Compilers and Systems Software. Christian Collberg

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages. ameter Passing Reference Parameter. Call-by-Value Parameters. 28 : Control Structures Parameters

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Intermediate Code Generation

Intermediate Code Generation

CSCE 531, Spring 2015 Final Exam Answer Key

intermediate-code Generation

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES

Syntax-Directed Translation Part II

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

Intermediate Code Generation

Lecture 9: Control Flow

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

Intermediate Code Generation

Homework 1 Answers. CS 322 Compiler Construction Winter Quarter 2006

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Architectures. Code Generation Issues III. Code Generation Issues II. Machine Architectures I

CSc 553. Principles of Compilation. 16 : OO Languages Polymorphism. Department of Computer Science University of Arizona

PRINCIPLES OF COMPILER DESIGN

Introduction. Data-flow Analysis by Iteration. CSc 553. Principles of Compilation. 28 : Optimization III

CS 432 Fall Mike Lam, Professor. Code Generation

CSc 553. Principles of Compilation. 23 : Register Allocation. Department of Computer Science University of Arizona

ECS 142 Project: Code generation hints (2)

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

int foo() { x += 5; return x; } int a = foo() + x + foo(); Side-effects GCC sets a=25. int x = 0; int foo() { x += 5; return x; }

Basic Structure of Denotational Definitions

! Non-determinacy (unspecified order) Maria Hybinette, UGA 2. 1 Landin adding sugar to a language to make it easier to read (for humans)

LECTURE 17. Expressions and Assignment

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

LECTURE 18. Control Flow

More Examples. Lecture 24: More Scala. Higher-Order Functions. Control Structures

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

Expressions vs statements

CSc 453 Compilers and Systems Software

VIVA QUESTIONS WITH ANSWERS

Intermediate Code Generation

CSE P 501 Exam 8/5/04

CSc 520. Principles of Programming Languages 25: Types Introduction

Control Flow. Stephen A. Edwards. Fall Columbia University

Intermediate Code Generation

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Object-Oriented Languages. CSc 453. Compilers and Systems Software. 23 : OO Languages. Department of Computer Science University of Arizona

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

CSCI Compiler Design

CSCE 531 Spring 2009 Final Exam

Syntax Directed Translation

Compiler. --- Intermediate Code Generation. Zhang Zhizheng.

CSc 520. Principles of Programming Languages 46: OO Languages Polymorphism

COP5621 Exam 3 - Spring 2005

Writing Evaluators MIF08. Laure Gonnord

Compilers. Compiler Construction Tutorial The Front-end

2068 (I) Attempt all questions.

Theory of Compilation Erez Petrank. Lecture 7: Intermediate Representation Part 2: Backpatching

G Programming Languages - Fall 2012

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

Transcription:

CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Control Structures Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures IF E THEN S IF E THEN S ELSE S ; WHILE E DO S ENDDO; IF E1 THEN S1 ELSIF E2 THEN S2 ELSE S3 FOR i:int := E1 TO E2 BY E3 DO S ENDFOR; REPEAT S UNTIL E; LOOP S ENDLOOP; <Name>: LOOP S1; EXIT <Name> WHEN E; S2; ENDLOOP; FOR i IN [E1, E2, ] DO S ENDFOR; Boolean Expressions

Short Circuit Evaluation Boolean Expressions With short circuit evaluation of boolean expressions we only evaulate as much of the expression as is necessary to determine if the expression evaulates to or false. Pascal does not have short-circuit evaluation. Many Pascal programmers have been burnt by this type of code: if p <> nil and p^.data = 32 then... On the other hand, Modula-2 (which only supports short-circuit evaluation) sometimes get burnt when a function with side-effects doesn t get executed: if a < t and (f(45) < 10) then... Some languages (Ada, Algol) let the programmer decide when to use short-circuit evaluation. E ::= E OR E E AND E NOT E ( E ) E relop E false relop ::= < <= = <> >= > Language Design Space: Short-circuit evaluation of AND & OR? if p <> nil and p^.data = 32 then... if a < t or (f(45) < 23) then... Compiler Design Space: Numerical or flow-of-control representation? Numerical Representation Numerical Representation The main advantage of implementing boolean expressions using a numerical representation is that it is very easy to implement. We simply extend our arithmetic expressions with new operators for AND, OR, NOT, and the relational operators.

Numerical Representation Numerical Representation... Boolean expressions are evaluated similarly to arithmetic expressions. Operators: FALSE 0 TRUE any value > 0 x AND y x y x OR y x + y NOT x IF x = 0 THEN t := 1 ELSE t := 0; x < y IF x < y THEN t := 1 ELSE t := 0; B := X < 10; WHILE X > 5 DO DEC (X); ENDDO 100:IF X < 10 GOTO 103 101:B := 0 102:GOTO 104 103:B := 1 104:IF X > 5 GOTO 107 105:t1 := 0 106:GOTO 108 107:t1 := 1 108:IF t1 = 0 GOTO 111 109:X := X - 1; 110:GOTO 104 111: Flow-of-Control Flow-of-Control Representation The value of a boolean expression is given by our position in the program. The value of X<10 is given by position 103 (if X<10=TRUE) or 101 (if X<10=TRUE). B := X < 10; WHILE X > 5 DO DEC (X); ENDDO 100:IF X < 10 GOTO 103 101:B := 0 102:GOTO 104 103:B := 1 104:IF X <= 5 GOTO 107 105:X := X - 1; 106:GOTO 104 107:

Short-Circuit Code Short-Circuit Evaluation What happens if the function f has side-effects (e.g. if it changes the value of a global variable)? Well, in such cases short-circuit code will have different semantics from the non-short-circuit code. In this example we use flow-of-control for the short-circuit evaluation, and numerical representation for the full evaluation. We could have given both examples using flow-of-control. Short-Circuit Code AND Short-Circuit Code OR IF (X > 5) AND f(34) THEN DEC (X); Short Circuit Full Evaluation 100:IF X <= 5 GOTO 103 100:t1 := X > 5; 101:IF NOT f(34) GOTO 103 101:t2 := f(34); 102:X := X - 1; 102:t3 := t1 AND t2; 103: 103:IF NOT t3 GOTO 105 104:X := X - 1; 105: IF (X > 5) OR f(34) THEN DEC (X); Short Circuit Full Evaluation 100:IF X > 5 GOTO 103 100:t1 := X > 5; 101:IF f(34) GOTO 103 101:t2 := f(34); 102:GOTO 104 102:t3 := t1 OR t2; 103:X := X - 1; 103:IF NOT t3 GOTO 105 104: 104:X := X - 1; 105:

Control Structures Implementing Control Structures One problem we re faced with when generating code for control structures, is that we generate code for the boolean expression before we generate code for the statements. Hence, when we generate jumps from out of (a complex) boolean expression we don t know where to jump to. Each expression is given two slots ( & false) which are filled in with the location to which we will jump if the expression is or false respectively. Control Structures... Implementing Control Structures Each statement also has a slot next which is the location of the instruction following the statement. This is used when we want to jump out of the body of a control statement. Using the next label avoids some jumps-to-jumps. Consider the example in the slide. When we jump out of the inner IF-statement (when the expression evaluates to false) we jump to the instruction following the IF. That happens to be a jump back to the top of the WHILE-statement. Using the next-slot fixes this. We generate code for the expression before the body of the control structure. How can we know where to jump? We give each expression two slots which get filled in when the appropriate label is known: (false) Where to jump to when the expression is (false). We give each statement one slot next which gets filled with the label of the next statement when it becomes known. WHILE x<b DO IF a = 1 THEN X := 1; ENDDO L1: if x >= b goto L2 if a <> 1 goto L4 X := 1 L4: goto L1 L2:

Semantic Attributes IF Statements An inherited attribute passed into boolean expressions. Holds the label to which the expression should jump if it evaluates to. false Where to jump to if the expression evaluates to false. next An inherited attribute passed into statements. Holds the label of the statement following the current one. begin A synthesized attribute that holds the label of the beginning of a WHILE-statement. We first generate code for the expression, then the body of the loop. We put a label (E.) at the beginning of the loop body. We jump to this label from every place within the expression where we can determine that it is evaluated to (there may be several such places). Similarly, we add a label after the end of the statement (E.false) to which we jump when the statement evaluates to false. Control Structures IF Control Structures IF S ::= IF E THEN SS1 E.: E.false: E.code S.code Example: IF (X > 5) THEN DEC (X); 100: IF X > 5 GOTO 102 (=E.) 101: GOTO 103 (=E.false) 102: X := X - 1; 103: to E. to E.false The implementation is encoded into these attribute evaluation rules. We start by creating the new label E.. Then we set E.false to be the same label as S.next. This means that if the expression evaluates to false, then we will jump to the statement immediately following the IF-statement, which is exactly what we want to do.

Control Structures IF... Control Structures IF... The next rule (S1.next := S.next;) says that: if we should need to jump out of the body of the IF-statement, then we should jump to the statement immediately following the current statment. Finally, we have the code generation rule which says that the code generated from an IF-statement consists of the code for the expression, the label E., and the code for the statement body. Normally we don t generate code as part of the attribute grammar, but it is certainly possible to do so. S ::= IF E THEN SS1 S ::= E S1 { E. := newlabel(); E.false := S.next; S1.next := S.next; S.code := E.code E. ":" S1.code; } E.: E.false: E.code S.code to E. to E.false Control Structures IF... Control Structures IF... X:=1; IF a<b THEN X:=3 ; X:=2; L2 L1: L3: IF X := 1 X := 2 Expr next Stat a < b L3 L3 L2: X := 3 L1: X := 1 if a < b then goto L2 goto L3 L2: X := 3; L3: X := 2; S ::= E S1 { E. := newlabel(); E.false := S1.next := S.next; S.code := E.code E. ":" S1.code; } IF a < b

Control Structures IF-ELSE Control Structures IF-ELSE... Example: S ::= IF E THEN SS1 ELSE SS2 to E. E.code to E.false E.: S1.code goto S.next E.false: S2.code S.next: IF (X > 5) THEN DEC (X); ELSE INC (X); 100: IF X > 5 GOTO 102 (=E.) 101: GOTO 104 (=E.false) 102: X := X - 1; 103: GOTO 105 (=S.next) 104: X := X - 1; Control Structures IF-ELSE Control Structures IF-ELSE... S ::= IF E THEN SS1 ELSE SS2 We have to generate two new labels, one attatched to the beginning of the THEN-part the other to the beginning of the ELSE-part. These are then passed into the expression to make sure we jump to the right places. If we need to jump out of the THEN-part or the ELSE-part we should land at the statement immediately following the current statment, hence we set S1.next and S2.next to S.next S ::= E S1 S2 { E. := newlabel(); E.false := newlabel(); S1.next := S.next; S2.next := S.next; S.code := E.code E. ":" S1.code goto S.next E.false ":" S2.code; } E.: E.code S1.code to E. to E.false goto S.next E.false: S2.code S.next:

Control Structures WHILE Control Structures WHILE S ::= WHILE E DO SS1 ENDDO S.begin: E.code to E. to E.false E.: E.false: S1.code goto S.begin Example: WHILE (X>5) DO DEC(X); ENDDO 100: IF X > 5 GOTO 102 (=E.) 101: GOTO 104 (=E.false) 102: X := X - 1; 103: GOTO 100 (=S.begin) 104: S.begin is a label we create and attatch to the expression itself. Later we will complete the loop by generating a jump back to this label. E. is a label we attatch to the loop body. E.false is set to the instruction following the loop; this is where we jump if the loop condition evaluates to false. Control Structures WHILE... Control Structures RelOp/NOT/AND/NOT S ::= WHILE E DO SS1 ENDDO S ::= E S1 { S.begin := newlabel(); E. := newlabel(); E.false := S.next; S1.next := S.begin; S.code := S.begin ":" E.code E. ":" S1.code goto S.begin } S.begin: E.code to E. to E.false The boolean connectives AND, OR, and NOT, are interesting since they don t require us to generate any new code; all we have to do is to assign the correct labels to the and false attributes. All we have to do for the NOT operator is to switch the and false attributes! In other words, NOT means that we jump to the label when the expression evaluates to false and to the false label when the expression evaluates to. E.: S1.code goto S.begin E.false:

Control Structures RelOp/NOT Control Structures OR Relational Operators: E ::= E1 < E2 E0 ::= E1 < E2 {E.code := t1 := E1.code t2 := E2.code IF t1 < t2 GOTO E0. GOTO E0.false ; } NOT: Since we re assuming short-circuit evaluation when the left-hand-side expression evaluates to false, we have to jump to the right-hand-side expression and evaluate that one too. If either one of the expressions evaluates to then the complete expression is also. Therefore we set E1. and E2. to the same label as E0.. E ::= NOT E1 E0 ::= NOT E1 { E1. := E0.false; E1.false := E0.; } Control Structures OR Example OR E ::= E1 OR E2 E0 ::= E1 OR E2 { E1. := E0.; E1.false := newlabel(); E2. := E0.; E2.false := E0.false; E.code := E1.code E1.false ":" E2.code } E1.false: E1.code E2.code to E1.=E0. to E1.false to E2.=E0. to E2.false=E0.false IF (a < b) OR c = d THEN X := 1 L1 a < b IF a < b GOTO L1 IF Expr next Stat OR X := 1 L2 E1 L3 E = c d IF c = d GOTO L1 E2 IF a < b GOTO L1 L2: IF c = d GOTO L1 L1: X := 1; L3:

Control Structures AND Example AND E ::= E1 AND E2 E0 ::= E1 AND E2 { E1. := newlabel(); E1.false := E0.false; E2. := E0.; E2.false := E0.false; E.code := E1.code E1. ":" E2.code } E1.: E1.code E2.code to E1. to E1.false=E0.false to E2.=E0. to E2.false=E0.false IF (a < b) AND c = d THEN X := 1 L2 L1 a < E1 b IF a < b Expr next AND IF L3 Stat E = X := 1 c d IF c = d GOTO L1 E2 IF a < b L2: IF c = d GOTO L1 L1: X := 1; L3: Example WHILE Examples WHILE NOT (a > b) DO IF c<d THEN X:=1 ENDDO Expr next WHILE begin Stat L1 L3 L2 NOT Rop false > a b L4 IF a > b GOTO L1 Expr next c < IF d Stat X := 1 IF c < d GOTO L4 IF a > b GOTO L1 L1: IF c = d GOTO L4 L4: X := 1; L3:

Example IF-ELSE IF a < b THEN X := 1 ELSE WHILE c<d DO X:=2; ENDDO Expr next IF ELSE Stat1 Stat2 Summary L3 X := 1 < WHILE Expr next begin Stat L1 a b L2 L4 IF a < b GOTO L1 < X := 2 L5 IF c < d GOTO L5 c d GOTO L4 IF a < b GOTO L1 L1: X := 1 L2: L4: IF c < d GOTO L5 L5: X := 2 Summary Summary... Some languages (Pascal) only support full evaluation of boolean expressions, some (Modula-2) only support short-circuit evaluation, others (Simula, Ada) allow the programmer to chose. Numeric representation is easier to implement, flow-of-control representation can be more efficient. We can often generate more efficient code by reversing tests (< ; >; ) to make the evaluation fall through. Predicting the outcome of tests (e.g. by feeding profiling information back into the compiler) is an important optimization technique. It is possible (but not always advisable) to use attribute grammars for (intermediate) code generation. Read the Dragon-book: 488 497, 468 469, 500-506