Code Generation for Control Flow Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc12-13.html Chapter 6.4 1.

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
Code Generation for Control Flow Mooly Sagiv Ohad Shacham Chapter 6.4.
Intermediate Code Generation
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
Comp Sci Control structures 1 Ch. 5 Control Structures.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
8 Intermediate code generation
1 Compiler Construction Intermediate Code Generation.
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.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Basic Blocks Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Chapter 8.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Code Generation Simple Register Allocation Mooly Sagiv html:// Chapter
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
Chapter 8 . Sequence Control
Code Generation Mooly Sagiv html:// Chapter 4.
Routines and Control Flows Code Generation Professor Yihjia Tsai Tamkang University.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Code Generation Mooly Sagiv html:// Chapter 4.
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
Compiler construction in4020 – lecture 11 Koen Langendoen Delft University of Technology The Netherlands.
Sequence Control Chapter 6. 2 l Control structures: the basic framework within which operations and data are combined into programs. Sequence control.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
C Chuen-Liang Chen, NTUCS&IE / 279 CONTROL STRUCTURES Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
sequence of execution of high-level statements
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
An Attribute Grammar for Tiny Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 18.
Intermediate code generation Simplifying initial assumptions a 3-address code will be used for instructions  choice of instruction mnemonics and layouts.
Control Flow Statements
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Computer Science 210 Computer Organization Machine Language Instructions: Control.
CS 153: Concepts of Compiler Design September 14 Class Meeting
Intermediate code generation Jakub Yaghob
Optimization Code Optimization ©SoftMoore Consulting.
11/10/2018.
Three Address Code Generations for Boolean Functions
Computer Science 210 Computer Organization
Chapter 6 Intermediate-Code Generation
Code Optimization Overview and Examples Control Flow Graph
Chapter 8: More on the Repetition Structure
Chapter 4: Control Structures I (Selection)
Languages and Compilers (SProg og Oversættere)
Flow of Control.
Intermediate Code Generation
Procedure Linkages Standard procedure linkage Procedure has
Presentation transcript:

Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4 1

Outline Local flow of control Conditionals Switch Loops 2

Machine Code Assumptions InstructionMeaning GOTO LabelJump to Label GOTO label registerIndirect jump IF condition register then GOTO LabelConditional Jump IF not condition register then GOTO Label 3

Boolean Expressions In principle behave like arithmetic expressions But are treated specially –Different machine instructions –Used in control flow instructions –Shortcut computations –Negations can be performed at compile-time Code for a < b yielding a condition value Conversion condition value into Boolean Conversion from Boolean in condition value Jump to l on condition value if (a < b) goto l 4

Shortcut computations Languages such as C define shortcut computation rules for Boolean Incorrect translation of e1 && e2 Code to compute e1 in loc1 Code to compute e2 in loc2 Code for && operator on loc1 and loc2 5

Location Computation The result of a Boolean expression is pair of locations in the generated code –The true location corresponds to the target instruction when the condition holds –The false location corresponds to the target instruction when the condition does not hold 6

Code for e1 && e2 7 if e1 then goto L11 goto L12 Code for e1 if e2 then goto L21 goto L22 Code for e2 L11: Code for &&

Code for Booleans (Location Computation) Top-Down tree traversal Generate code sequences instructions Jump to a designated ‘true’ label when the Boolean expression evaluates to 1 Jump to a designated ‘false’ label when the Boolean expression evaluates to 0 The true and the false labels are passed as parameters 8

Example if ((a==0) && (b > 5)) x = ((7 * a) + b) if &&:= x+ *b 7a == a0 > b5 9

if &&:= x+ *b 7a == a0 > b5 No label Lf Lt Lf Load_Local ‘a’, R0 Cmp_Constant R0, 0 IF NOT EQ THEN GOTO Lf Lt Lf Load_Local ‘b’, R0 Cmp_Constant R0, 5 IF GT THEN GOTO Lt GOTO Lf Lt: Code for := Lf: 10

Location Computation for Booleans Procedure Generate Code for Boolean control expression ( Node, True label, False label) : SELECT Node.type: CASE Comparison type: //, ==, etc. in C Generate code for comparison expression (Node.expr); // The comparison result is now in condition register IF True label =/ No label: Emit(“If condition register THEN GOTO True label); IF False label /= No label: Emit(“GOTO” False label); ELSE True label == No label: IF False label /= No label: Emit(“IF NOT” condition register THEN GOTO”, False label); CASE Lazy and type: // && in C Generate code for Boolean control expression Node.left, No Label, False label); Generate code for Boolean control expression Node.right, True label, False label); CASE Negation type: // ! in C Generate code for Boolean control expression Node.arg, False label, True label); … 11

Code generation for IF if Boolean expressiontrue sequencefalse sequence Allocate two new labels Lf, Lend Generate code for Boolean(left, 0, Lf) Code for Boolean with jumps to Lf Code for true sequence Code for false sequence GOTO Lend Lf:: Lend: 12

Code generation for IF (no-else) if Boolean expressiontrue sequence Allocate new label Lend Generate code for Boolean(left, 0, Lend) Code for Boolean with jumps to Lend Code for true sequence Lend: 13

Coercions into value computations := xa > b Generate new label Lf Load_Constant R0, 0; Generate code for Boolean(right, 0, Lf) Load_Local ‘a’, R1; CMP R1, ‘b’ ; IF <= GOTO Lf; Load_Constant R0, 1 Lf: Store_Local R0, ‘x’ 14

Effects on performance Number of executed instructions Unconditional vs. conditional branches Instruction cache Branch prediction Target look-ahead 15

Code for case statements Three possibilities –Sequence of IFs O(n) comparisons –Jump table O(1) comparisons –Balanced binary tree O(log n) comparisons Performance depends on n Need to handle runtime errors 16

Simple Translation tmp_case_value := case expression; IF tmp_case_value = l 1 THEN GOTO label_1; IF tmp_case_value = l 2 THEN GOTO label_2; … IF tmp_case_value = l n THEN GOTO label_n; GOTO label_else; // or insert the code at label else label 1: Code for statement sequence 1 GOTO label_next; label 2: Code for statement sequence 2 GOTO label_next; … label n: Code for statement sequence n GOTO label_next; label else: Code for else-statement sequence 17

Simple Translation 18

Jump Table Generate a table of L high -L low +1 entries –Filled at ?time Each entry contains the start location of the corresponding case or a special label Generated code tmp_case_value:= case expression; if tmp_case_value L high GOTO label_else; GOTO table[tmp_case_value –L low ]; 19

Balanced trees The jump table may be inefficient –Space consumption –Cache performance Organize the case labels in a balanced tree –Left subtrees smaller labels –Right subtrees larger labels Code generated for node_k label_k: IF tmp_case_value l k THEN GOTO label of right branch; code for statement sequence; GOTO label_next; 20

Repetition Statements (loops) Similar to IFs Preserve language semantics Performance can be affected by different instruction orderings Some work can be shifted to compile-time –Loop invariant –Strength reduction –Loop unrolling 21

while statements Boolean expression while statement Sequence test_label: Generate new labels test_label, L end Generate code for Boolean(left, 0, L end ) Code for Boolean with jumps to L end Code for statement sequence GOTO test_label; L end : 22

while statements(2) Boolean expression while statement Sequence GOTO test_label: Ls: Generate labels test_label, Ls Generate code for Boolean(left, Ls, 0) Code for Boolean with jumps to L S Code for statement sequence test_label: 23

For-Statements Special case of while Tricky semantics –Number of executions –Effect on induction variables –Overflow 24

Simple-minded translation FOR i in lower bound.. upper bound DO statement sequence END for  i := lower_bound; tmp_ub := upper_bound; WHILE i <= tmp_ub DO code for statement sequence i := i + 1; END WHILE 25

Correct Translation FOR i in lower bound.. upper bound DO statement sequence END for  i := lower_bound; tmp_ub := upper_bound; IF i >tmp_ub THEN GOTO end_label; loop_label: code for statement sequence if (i==tmp_ub) GOTO end_label; i := i + 1; GOTO loop_label; end_label: 26

Tricky question for (exp1; exp2; exp3) { body; } exp1; while (exp2) { body; exp3; } 27

Summary Handling control flow statements is usually simple Complicated aspects –Routine invocation –Non local gotos Runtime profiling can help 28