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

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

Module 25 Control Flow statements and Boolean Expressions

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

Formal Languages and Compilers Lecture X Intermediate Code Generation

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

CMPSC 160 Translation of Programming Languages. Three-Address Code

Syntax-Directed Translation

Flow of Control Execution Sequence

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

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7

CS2210: Compiler Construction. Code Generation

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

CSCI Compiler Design

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

Intermediate Code Generation

CSc 520 Principles of Programming Languages

Languages and Compiler Design II IR Code Generation I

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

SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

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

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

THEORY OF COMPILATION

CSc 520 Principles of Programming Languages

UNIT IV INTERMEDIATE CODE GENERATION

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. 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 520 Principles of Programming Languages

Dixita Kagathara Page 1

CSc 453 Intermediate Code Generation

ECS 142 Project: Code generation hints (2)

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

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

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

CSc 520 Principles of Programming Languages

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

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

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

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

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

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

Intermediate Code Generation

Intermediate Code Generation

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

VIVA QUESTIONS WITH ANSWERS

CSCE 531, Spring 2015 Final Exam Answer Key

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

Syntax-Directed Translation Part II

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

PRINCIPLES OF COMPILER DESIGN

Intermediate Code Generation

Lecture 9: Control Flow

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

Intermediate Code Generation

Homework 1 Answers. CS 322 Compiler Construction Winter Quarter 2006

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

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

intermediate-code Generation

CS 432 Fall Mike Lam, Professor. Code Generation

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

! 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

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

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

CSE P 501 Exam 8/5/04

Intermediate Code Generation

Control Flow. Stephen A. Edwards. Fall Columbia University

Intermediate Code Generation

CSc 520. Principles of Programming Languages 25: Types Introduction

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

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

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

CSCI Compiler Design

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

CSCE 531 Spring 2009 Final Exam

Syntax Directed Translation

COP5621 Exam 3 - Spring 2005

Basic Structure of Denotational Definitions

Compilers. Compiler Construction Tutorial The Front-end

Intermediate Code Generation

2068 (I) Attempt all questions.

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

G Programming Languages - Fall 2012

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

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

CSc 453. OO Languages. Object-Oriented Languages... Object-Oriented Languages. Compiling OO Languages. Compilers and Systems Software

Transcription:

CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg

Control Structures

Control Structures IF E THEN S ENDIF IF E THEN S ELSE S ENDIF; WHILE E DO S ENDDO; IF E 1 THEN S 1 ELSIF E 2 THEN S 2 ELSE S 3 ENDIF FOR i:int := E 1 TO E 2 BY E 3 DO S ENDFOR; REPEAT S UNTIL E; LOOP S ENDLOOP; <Name>: LOOP S 1 ; EXIT <Name> WHEN E; S 2 ; ENDLOOP; FOR i IN [E 1, E 2, ] DO S ENDFOR;

Boolean Expressions

Short Circuit Evaluation 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 true 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.

Boolean Expressions E ::= E OR E E AND E NOT E ( E ) E relop E true 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 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;

Numerical Representation... 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:t 1 := 0 106:GOTO 108 107:t 1 := 1 108:IF t 1 = 0 GOTO 111 109:X := X - 1; 110:GOTO 104 111:

Flow-of-Control Representation

Flow-of-Control 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 Evaluation

Short-Circuit Code 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 IF (X > 5) AND f(34) THEN DEC (X); ENDIF Short Circuit 100:IF X <= 5 GOTO 103 101:IF NOT f(34) GOTO 103 102:X := X - 1; 103: Full Evaluation 100:t 1 := X > 5; 101:t 2 := f(34); 102:t 3 := t 1 AND t 2 ; 103:IF NOT t 3 GOTO 105 104:X := X - 1; 105:

Short-Circuit Code OR IF (X > 5) OR f(34) THEN DEC (X); ENDIF Short Circuit 100:IF X > 5 GOTO 103 101:IF f(34) GOTO 103 102:GOTO 104 103:X := X - 1; 104: Full Evaluation 100:t 1 := X > 5; 101:t 2 := f(34); 102:t 3 := t 1 OR t 2 ; 103:IF NOT t 3 GOTO 105 104:X := X - 1; 105:

Implementing Control Structures

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 (true & false) which are filled in with the location to which we will jump if the expression is true or false respectively.

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.

Implementing Control Structures 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: true (false) Where to jump to when the expression is true (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; ENDIF ENDDO L1: if x >= b goto L2 if a <> 1 goto L4 X := 1 L4: goto L1 L2:

Semantic Attributes true An inherited attribute passed into boolean expressions. Holds the label to which the expression should jump if it evaluates to true. 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.

IF Statements We first generate code for the expression, then the body of the loop. We put a label (E.true) 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 true (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 S ::= IF E THEN SS 1 ENDIF E.true: E.false: E.code S.code Example: to E.true to E.false IF (X > 5) THEN DEC (X); ENDIF 100: IF X > 5 GOTO 102 (=E.true) 101: GOTO 103 (=E.false) 102: X := X - 1; 103:

Control Structures IF The implementation is encoded into these attribute evaluation rules. We start by creating the new label E.true. 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... The next rule (S 1.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.true, 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.

Control Structures IF... S ::= IF E THEN SS 1 ENDIF S ::= E S 1 { E.true := newlabel(); E.false := S.next; S 1.next := S.next; S.code := E.code E.true ":" S 1.code; } E.true: E.false: E.code S.code to E.true to E.false

Control Structures IF... X:=1; IF a<b THEN X:=3 ENDIF; X:=2; L1: L3: IF X := 1 X := 2 Expr next Stat L2 true < L3 Lop Rop false L2: X := 3 a b L3 IF a < b GOTO L2 GOTO L3

Control Structures IF... L1: X := 1 if a < b then goto L2 goto L3 L2: X := 3; L3: X := 2; S ::= E S 1 { E.true := newlabel(); E.false := S 1.next := S.next; S.code := E.code E.true ":" S 1.code; }

Control Structures IF-ELSE S ::= IF E THEN SS 1 ELSE SS 2 ENDIF E.true: E.false: S.next: E.code S1.code goto S.next S2.code to E.true to E.false

Control Structures IF-ELSE... Example: IF (X > 5) THEN DEC (X); ELSE INC (X); ENDIF 100: IF X > 5 GOTO 102 (=E.true) 101: GOTO 104 (=E.false) 102: X := X - 1; 103: GOTO 105 (=S.next) 104: X := X - 1;

Control Structures IF-ELSE 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 S 1.next and S 2.next to S.next

Control Structures IF-ELSE... S ::= IF E THEN SS 1 ELSE SS 2 ENDIF S ::= E S 1 S 2 { E.true := newlabel(); E.false := newlabel(); S 1.next := S.next; S 2.next := S.next; S.code := E.code E.true ":" S 1.code goto S.next E.false ":" S 2.code; } E.true: E.false: S.next: E.code S1.code goto S.next S2.code to E.true to E.false

Control Structures WHILE S ::= WHILE E DO SS 1 ENDDO S.begin: E.code to E.true to E.false E.true: E.false: S1.code goto S.begin Example: WHILE (X>5) DO DEC(X); ENDDO 100: IF X > 5 GOTO 102 (=E.true) 101: GOTO 104 (=E.false) 102: X := X - 1; 103: GOTO 100 (=S.begin) 104:

Control Structures WHILE 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.true 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... S ::= WHILE E DO SS 1 ENDDO S ::= E S 1 { S.begin := newlabel(); E.true := newlabel(); E.false := S.next; S 1.next := S.begin; S.code := S.begin ":" E.code E.true ":" S 1.code goto S.begin } S.begin: E.code to E.true to E.false E.true: E.false: S1.code goto S.begin

Control Structures RelOp/NOT/AND/NOT 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 true and false attributes. All we have to do for the NOT operator is to switch the true and false attributes! In other words, NOT means that we jump to the true label when the expression evaluates to false and to the false label when the expression evaluates to true.

Control Structures RelOp/NOT E ::= E 1 < E 2 Relational Operators: E 0 ::= E 1 < E 2 {E.code := t 1 := E 1.code t 2 := E 2.code IF t 1 < t 2 GOTO E 0.true GOTO E 0.false ; } E ::= NOT E 1 NOT: E 0 ::= NOT E 1 { E 1.true := E 0.false; E 1.false := E 0.true; }

Control Structures OR 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 true then the complete expression is true also. Therefore we set E 1.true and E 2.true to the same label as E 0.true.

Control Structures OR E ::= E 1 OR E 2 E 0 ::= E 1 OR E 2 { E 1.true := E 0.true; E 1.false := newlabel(); E 2.true := E 0.true; E 2.false := E 0.false; E.code := E 1.code E 1.false ":" E 2.code } E 1.false: E 1.code E 2.code to E 1.true=E 0.true to E 1.false to E 2.true=E 0.true to E 2.false=E 0.false

Example OR IF (a < b) OR c = d THEN X := 1 ENDIF IF Expr next Stat L1 L3 E OR X := 1 true Lop Rop false E1 E2 < = true Lop Rop false true Lop Rop false a b L2 c d IF a < b GOTO L1 IF c = d GOTO L1 GOTO L2 GOTO L3 IF a < b GOTO L1 GOTO L2 L2: IF c = d GOTO L1 GOTO L3 L1: X := 1; L3:

Control Structures AND E ::= E 1 AND E 2 E 0 ::= E 1 AND E 2 { E 1.true := newlabel(); E 1.false := E 0.false; E 2.true := E 0.true; E 2.false := E 0.false; E.code := E 1.code E 1.true ":" E 2.code } E 1.true: E 1.code E 2.code to E 1.true to E 1.false=E 0.false to E 2.true=E 0.true to E 2.false=E 0.false

Example AND IF (a < b) AND c = d THEN X := 1 ENDIF IF Expr next Stat L1 true L3 E AND Lop Rop false X := 1 L2 E1 < true Lop Rop false a b IF a < b GOTO L2 GOTO L3 E2 = true Lop Rop false c d IF c = d GOTO L1 GOTO L3 IF a < b GOTO L2 GOTO L3 L2: IF c = d GOTO L1 GOTO L3 L1: X := 1; L3:

Examples

Example WHILE WHILE NOT (a > b) DO IF c<d THEN X:=1 ENDIF ENDDO Expr next WHILE begin Stat L1 L3 L2 NOT true Rop false IF Expr next Stat > true Lop Rop false a b L4 IF a > b GOTO L3 GOTO L1 true Lop Rop false c < d X := 1 IF c < d GOTO L4 GOTO L2 IF a > b GOTO L3 GOTO L1 L1: IF c = d GOTO L4 GOTO L2 L4: X := 1; GOTO L2 L3:

Example IF-ELSE IF a < b THEN X := 1 ELSE WHILE c<d DO X:=2; ENDDO ENDIF IF ELSE Expr next Stat1 Stat2 L1 true a GOTO L2 < Lop Rop false b IF a < b GOTO L1 L5 L2 L3 true X := 1 Expr next < WHILE Lop Rop false c d begin L4 Stat X := 2 IF c < d GOTO L5 GOTO L4 IF a < b GOTO L1 GOTO L2 L1: X := 1 GOTO L3 L2: L4: IF c < d GOTO L5 GOTO L3 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.

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