CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

Code Generation for Control Flow Mooly Sagiv Ohad Shacham Chapter 6.4.
Intermediate Code Generation
1 CS 201 Compiler Construction Machine Code Generation.
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
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.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Program Representations. Representing programs Goals.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 14: Building a Runnable Program Chapter 14: Building a runnable program 14.1 Back-End Compiler Structure 14.2 Intermediate Forms 14.3 Code.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
CS 536 Spring Code generation I Lecture 20.
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Intermediate Code. Local Optimizations
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Precision Going back to constant prop, in what cases would we lose precision?
Abstract Interpretation (Cousot, Cousot 1977) also known as Data-Flow Analysis.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
Compilation /15a Lecture 7 Getting into the back-end Noam Rinetzky 1.
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.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
CS412/413 Introduction to Compilers and Translators May 3, 1999 Lecture 34: Compiler-like Systems JIT bytecode interpreter src-to-src translator bytecode.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
Compiler Chapter# 5 Intermediate code generation.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Introduction to Code Generation and Intermediate Representations
Intermediate Code Representations
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Intermediate code generation Simplifying initial assumptions a 3-address code will be used for instructions  choice of instruction mnemonics and layouts.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Code Generation How to produce intermediate or target code.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Compiler Principles Fall Compiler Principles Lecture 8: Intermediate Representation Roman Manevich Ben-Gurion University.
Three Address Code Generation of Control Statements continued..
Intermediate Code Generation CS 671 February 14, 2008.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
CS 404 Introduction to Compiler Design
Computer Science 210 Computer Organization
Intermediate Code Generation
Data Types, Identifiers, and Expressions
Lecture 30 (based on slides by R. Bodik)
Computer Science 210 Computer Organization
Intermediate Code Generation
Compiler Construction
Presentation transcript:

CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02

CS 412/413 Spring 2002 Introduction to Compilers2 Intermediate Representation High IR: captures high-level language constructs –Has a tree structure very similar to AST –Has expression nodes (ADD, SUB, etc) and statement nodes (if-then-else, while, etc) Low IR: captures low-level machine features –Is a instruction set describing an abstract machine –Has arithmetic/logic instructions, data movement instructions, branch instructions, function calls

CS 412/413 Spring 2002 Introduction to Compilers3 IR Lowering Use temporary variables for the translation Temporary variables in the Low IR store intermediate values corresponding to the nodes in the High IR t1 = a t2 = b t3 = t1 – t2 t4 = b t5 = c t5 = t4 + t5 t = t3 * t5 a b SUB c d ADD MUL lowering High IR Low IR

CS 412/413 Spring 2002 Introduction to Compilers4 Lowering Methodology Define simple translation rules for each High IR node –Arithmetic: e1 + e2, e1 – e2, etc. –Logic: e1 AND e2, e1 OR e2, etc. –Array access expressions: e1[e2] –Statements: if (e) then s1 else s2, while (e) s1, etc. –Function calls f(e1, …, eN) Recursively traverse the High IR trees and apply the translation rules Can handle nested expressions and statements

CS 412/413 Spring 2002 Introduction to Compilers5 IR Lowering Efficiency t1 = c fjump t1 Lend1 t2 = d fjump t2 Lend2 t3 = b a = t3 label Lend2 label Lend1 fjump c Lend fjump d Lend a = b Label Lend a b = d c if-then

CS 412/413 Spring 2002 Introduction to Compilers6 Efficient Lowering Techniques How to generate efficient Low IR: 1.Reduce number of temporaries 1.Don’t use temporaries that duplicate variables 2.Use “accumulator” temporaries 3.Reuse temporaries in Low IR 2.Don’t generate multiple adjacent label instructions 3.Encode conditional expressions in control flow

CS 412/413 Spring 2002 Introduction to Compilers7 No Duplicated Variables Basic algorithm: –Translation rules recursively traverse expressions until they reach terminals (variables and numbers) –Then translate t =  v  into t = v for variables –And translate t =  n  into t = n for constants Better: –terminate recursion one level before terminals –Need to check at each step if expressions are terminals –Recursively generate code for children only if they are non-terminal expressions

CS 412/413 Spring 2002 Introduction to Compilers8 No Duplicated Variables t =  e1 OP e2  t1 =  e1 , i f e1 is not terminal t2 =  e2 , i f e2 is not terminal t = x1 OP x2 where: x1 = t1, if e1 is not terminal x1 = e1, if e1 is terminal x2 = t2, if e2 is not terminal x2 = e2, if e2 is terminal Similar translation for statements with conditional expressions: if, while, switch

CS 412/413 Spring 2002 Introduction to Compilers9 Example t =  (a+b)*c  Operand e1 = a+b, is not terminal Operand e2 = c, is terminal Translation:t1 =  e1  t = t1 * c Recursively generate code for t1 =  e1  For e1 = a+b, both operands are terminals Code for t1 =  e1  is t1 = b+c Final result:t1 = b + c t = t1 * c

CS 412/413 Spring 2002 Introduction to Compilers10 Accumulator Temporaries Use the same temporary variables for operands and result Translate t =  e1 OP e2  as: t =  e1  t1 =  e2  t = t OP t1 Example: t =  (a+b)*c  t = b + c t = t * c

CS 412/413 Spring 2002 Introduction to Compilers11 Reuse Temporaries Idea: in the translation of t =  e1 OP e2  as: t =  e1 , t’ =  e2 , t = t OP t’ temporary variables from the translation of e1 can be reused in the translation of e2 Observation: temporary variables compute intermediate values, so they have limited lifetime Algorithm: –Use a stack of temporaries –This corresponds to the stack of the recursive invocations of the translation functions t =  e  –All the temporaries on the stack are alive

CS 412/413 Spring 2002 Introduction to Compilers12 Reuse Temporaries Implementation: use counter c to implement the stack –Temporaries t(0), …,t(c) are alive –Temporaries t(c+1), t(c+2), … can be reused –Push means increment c, pop means decrement c In the translation of t(c) =  e1 OP e2  t(c) =  e1  c = c+1 t(c) =  e2  c = c-1 t(c) = t(c) OP t(c+1)

CS 412/413 Spring 2002 Introduction to Compilers13 Example t0 =  (a*b) + ((c*d) – (e*f))  t0 = a * b c = c+1 t1 =  e2  c = c-1 t0 = t0+t1 t1 = c*d c = c+1 t2 = e*f c = c-1 t1 = t1 – t2

CS 412/413 Spring 2002 Introduction to Compilers14 Trade-offs Benefits of fewer temporaries: –Smaller symbol tables –Smaller analysis information propagated during dataflow analysis Drawbacks: –Same temporaries store multiple values –Some analysis results may be less precise –Also harder to reconstruct expression trees (which may be convenient for instruction selection) Possible compromise: –Use different temporaries for intermediate expression values in each statement –Use same temporaries in different statements

CS 412/413 Spring 2002 Introduction to Compilers15 No Adjacent Labels Translation of control flow constructs (if, while, switch) and short-circuit conditionals generates label instructions Nested if/while/switch statements and nested short- circuit AND/OR expressions may generate adjacent labels Simple solution: have a second pass that merges adjacent labels –And a third pass to adjust the branch instructions More efficient: backpatching –Directly generate code without adjacent label instructions –Code has placeholders for jump labels, fill in labels later

CS 412/413 Spring 2002 Introduction to Compilers16 Backpatching Keep track of the return label (if any) of translation of each High IR node: t =  e, L  No end label for a translation: L =  Translate t =  e1 SC-OR e2, L  as: t1 =  e1, L1  tjump t1 L t1 =  e2, L2  If L2 =  : L is new label; add ‘label L’ to code If L2   : L = L2; don’t add label instruction Then fill placeholder L in jump instruction and set L = end label of the SC-OR construct

CS 412/413 Spring 2002 Introduction to Compilers17 Example t =  (a < b) OR (c<d OR d<e), L  t = a < b tjump t1 L t =  c<d OR d<e, L’  Backpatch t =  c<d OR d<e, L’  : L’ = Lend Backpatch t =  (a<b) OR (c<d OR d<e), L  : L = L’ = Lend t = c < d fjump t L’ t = d<e label Lend

CS 412/413 Spring 2002 Introduction to Compilers18 Backpatching Similar rules for end labels of short-circuit OR, and for control- flow statements: if-then-else, if-then, while, switch Keep track of end labels for each of these constructs Translations may begin with a label: while statements start with a label instruction for the test condition For a statement sequence s1;s2 : should merge end label of s1 with start label of s2 Need to pass in the end label of s1 to the recursive translation of s2 Translation of each statement: receives end label of previous statement, returns end label of current statement

CS 412/413 Spring 2002 Introduction to Compilers19 Encode Booleans in Control-Flow Consider  if ( a<b AND c<d ) x = y;  t = a<b fjump t L1 t = c<d label L1 fjump t L2 x = y label L2 … can we do better? Condition: t = a<b AND c<d Control flow: if (t) x = y

CS 412/413 Spring 2002 Introduction to Compilers20 Encode Booleans in Control-Flow Consider  if ( a<b AND c<d ) x = y;  t = a<b fjump t L1 t = c<d label L1 fjump t L2 x = y label L2 If t = a<b is false, program branches to label L2 Encode (a<b) == false to branch directly to the end label t = a<b fjump t L2 t = c<d fjump t L2 x = y label L2 Condition and control flow

CS 412/413 Spring 2002 Introduction to Compilers21 How It Works For each boolean expression e:  e, L1, L2  is the code that computes e and branches to L1 if e evaluates to true, and to L2 if e evaluates to false New translation:  if(e) then s   e, L1, L2  label L1  s  label L2 Also remove sequences ‘jump L, label L’

CS 412/413 Spring 2002 Introduction to Compilers22 Define New Translations Must define:  s  for if, while statements  e, L1, L2  for boolean expressions e  if(e) then s1 else s2   e, L1, L2  label L1  s1  jump Lend label L2  s2  label Lend

CS 412/413 Spring 2002 Introduction to Compilers23 While Statement  while (e) s  label Ltest  e, L1, L2  label L1  s  jump Ltest label L2 Code branches directly to end label when e evaluates to false

CS 412/413 Spring 2002 Introduction to Compilers24 Boolean Expression Translations  true, L1, L2  : jump L1  false, L1, L2  : jump L2  e1 SC-OR e2, L1, L2   e1, L1, Lnext  label lnext  e2, L1, L2   e1 SC-AND e2, L1, L2   e1, Lnext, L2  label lnext  e2, L1, L2 