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

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-
8 Intermediate code generation
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
CPSC Compiler Tutorial 8 Code Generator (unoptimized)
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Code Generation Mooly Sagiv html:// Chapter 4.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
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://
Chapter 8 . Sequence Control
Code Generation Mooly Sagiv html:// Chapter 4.
Activation Records (Introduction) Mooly Sagiv html:// Chapter 6.3.
Code Generation Example Mooly Sagiv html://
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.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
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.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
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.
Statement Level Flow of Control GOTOs and Other Weirdness Copyright © by Curt Hill.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
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.
Code Generation for Control Flow Mooly Sagiv html:// Chapter
Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics.
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.
Code Optimization.
Intermediate code generation Jakub Yaghob
Review: Chapter 5: Syntax directed translation
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)
Flow of Control.
Intermediate Code Generation
The Selection Structure
Procedure Linkages Standard procedure linkage Procedure has
Presentation transcript:

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

Outline Local flow of control Conditionals Switch Loops

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

Boolean Expressions In principle behave like arithmetic expressions But are treated specially –Different machine 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

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

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

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

if &&:= x+ *b 7a == a0 > b5 No label Lf Lt Lf Load_Local -8(FP), R0 Cmp_Constant R0, 0 IF NOT EQ THEN GOTO Lf Lt Lf Load_Local -12(FP), R0 Cmp_Constant R0, 5 IF GT THEN GOTO Lt GOTO Lf Lt: Code for := Lf:

Location Computation for Booleans

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:

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:

Coercions into value computations := xa > b Generate new label Lf Load_Constant R0, 0; Generate code for Boolean(right, 0, Lf) Load_Local -8(FP), R1; CMP R1, -12(FP) ; IF <= GOTO Lf; Load_Constant R0, 1 Lf: Store_Local R0, -20(FP)

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

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

Simple Translation

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

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;

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

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 :

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:

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

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

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:

Tricky question

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