CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

ANALYSIS OF PROG. LANG. PROGRAM ANALYSIS Instructors: Crista Lopes Copyright © Instructors. 1.
Intermediate Code Generation
1 CS 201 Compiler Construction Machine Code Generation.
8. Code Generation. Generate executable code for a target machine that is a faithful representation of the semantics of the source code Depends not only.
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.
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
8 Intermediate code generation
1 Compiler Construction Intermediate Code Generation.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Program Representations. Representing programs Goals.
Compiler Principles Fall Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.
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.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
1 CS 201 Compiler Construction Lecture 1 Introduction.
Compiler Construction Intermediate Representation I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Precision Going back to constant prop, in what cases would we lose precision?
Semantic Analysis III + Intermediate Representation I.
What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.
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.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
CS412/413 Introduction to Compilers and Translators May 3, 1999 Lecture 34: Compiler-like Systems JIT bytecode interpreter src-to-src translator bytecode.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 8 Intermediate Code Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
Compiler Chapter# 5 Intermediate code generation.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
sequence of execution of high-level statements
Control Structures sequence of execution of high-level statements.
Intermediate Representation I High-Level to Low-Level IR Translation.
Introduction to Code Generation and Intermediate Representations
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
COMPILERS Intermediate Code hussein suleman uct csc3003s 2007.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Semantic Analysis III + Intermediate Representation I.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
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.
Code Generation CPSC 388 Ellen Walker Hiram College.
Intermediate Language  Compiler Model Front-End− language dependant part Back-End− machine dependant part [1/34]
Code Generation How to produce intermediate or target code.
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
CPS120: Introduction to Computer Science Decision Making in Programs.
Intermediate Code Generation CS 671 February 14, 2008.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
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.
INTERMEDIATE LANGUAGES SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Compiler Chapter 9. Intermediate Languages Sung-Dong Kim Dept. of Computer Engineering, Hansung University.
CS 404 Introduction to Compiler Design
Intermediate code Jakub Yaghob
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Chapter 9. Intermediate Languages
Subject Name:COMPILER DESIGN Subject Code:10CS63
Chapter 6 Intermediate-Code Generation
Compiler Design 21. Intermediate Code Generation
Compiler Design 21. Intermediate Code Generation
Review: What is an activation record?
Intermediate Code Generating machine-independent intermediate form.
Presentation transcript:

CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02

CS 412/413 Spring 2002 Introduction to Compilers2 Intermediate Representation Intermediate representation = internal representation –Is language-independent and machine-independent High IR: captures high-level language constructs Low IR: captures low-level machine features Pentium Java bytecode Alpha HIRLIR C Fortran Pascal

CS 412/413 Spring 2002 Introduction to Compilers3 High-level IR Tree node structure very similar to the AST Contains high-level constructs common to many languages –Expression nodes –Statement nodes Expression nodes for: –Integers and program variables –Binary operations: e1 OP e2 Arithmetic operations Logic operations Comparisons –Unary operations: OP e –Array accesses: e1[e2]

CS 412/413 Spring 2002 Introduction to Compilers4 High-level IR Statement nodes: –Block statements (statement sequences): (s1, …, sN) –Variable assignments: v = e –Array assignments: e1[e2] = e3 –If-then-else statements: if c then s1 else s2 –If-then statements: if c then s –While loops: while (c) s –Function call statements: f(e1, …, eN) –Return statements: return or return e May also contain: –For loop statements: for(v = e1 to e2) s –Break and continue statements –Switch statements: switch(e) { v1: s1, …, vN: sN }

CS 412/413 Spring 2002 Introduction to Compilers5 High-level IR Statements may be expressions Statement expression nodes: –Block statements: (s1, …, sN) –Variable assignments: v = e –Array assignments: e1[e2] = e3 –If-then-else statements: if c then s1 else s2 –Function calls: f(e1, …, eN) There is a high IR node for each of the above. –All AST nodes are translated into the above IR nodes

CS 412/413 Spring 2002 Introduction to Compilers6 Low-level IR Represents a set of instructions which emulates an abstract machine Arithmetic and logic instructions: –Binary : a = b OP c Arithmetic operations Logic operations Comparisons –Unary operations: a = OP b Data movement instructions: –Copy : a = b –Load : a = [b] (load in a the value at address b) –Store: [a] = b (store at address a the value b)

CS 412/413 Spring 2002 Introduction to Compilers7 Low-level IR Function call instructions: –Call instruction: call f(a1, …, aN) –Call assignment: a = call f(a1, …, aN) –Return instruction: return –Value return: return a Branch instructions: –Unconditional jump: jump L –Conditional jump: cjump c L These instructions are also called quadruples or three- address instructions

CS 412/413 Spring 2002 Introduction to Compilers8 Translating High IR to Low IR We need to translate each high-level IR node to a low- level IR sequence of instructions –Expressions nodes: arithmetic, logic, comparison, unary, etc. –Statements nodes: blocks, if-then-else, if-then, while, function calls, etc. –Expression statements nodes: if-then-else, calls, etc.

CS 412/413 Spring 2002 Introduction to Compilers9 Notation Use the following notation:  e  = the low-level IR representation of high-level IR construct e  e  is a sequence of Low-level IR instructions If e is an expression (or a statement expression), it represents a value Denote by t =  e  the low-level IR representation of e, whose result value is stored in t For variable v: t =  v  is the copy instruction t = v

CS 412/413 Spring 2002 Introduction to Compilers10 Translating Expressions Binary operations: t =  e1 OP e2  (arithmetic operations and comparisons) t1 =  e1  t2 =  e2  t = t1 OP t2 Unary operations: t =  OP e  t1 =  e  t = OP t1 OP e1 e2 OP e

CS 412/413 Spring 2002 Introduction to Compilers11 Translating Boolean Expressions t =  e1 OR e2  t1 =  e1  t2 =  e2  t = t1 OR t2 … how about short-circuit OR? Should compute e2 only if e1 evaluates to false OR e1 e2

CS 412/413 Spring 2002 Introduction to Compilers12 Translating Short-Circuit OR Short-circuit OR: t =  e1 SC-OR e2  t =  e1  cjump t Lend t =  e2  label Lend … how about short-circuit AND? SC-OR e1 e2

CS 412/413 Spring 2002 Introduction to Compilers13 Translating Short-Circuit AND Short-circuit AND: t =  e1 SC-AND e2  t =  e1  cjump t Lnext jump Lend label Lnext t =  e2  label Lend SC-AND e1 e2

CS 412/413 Spring 2002 Introduction to Compilers14 Another Translation Short-circuit AND: t =  e1 SC-AND e2  t1 =  e1  t2 = not t1 cjump t2 Lend t =  e2  label Lend SC-AND e1 e2

CS 412/413 Spring 2002 Introduction to Compilers15 Yet Another Translation Use another low-level IR: abstract machine with two kinds of conditional jumps –tjump c L : jump to L if c is true –fjump c L : jump to L if c is false Short-circuit AND: t =  e1 SC-AND e2  t =  e1  fjump t2 Lend t =  e2  label Lend SC-AND e1 e2

CS 412/413 Spring 2002 Introduction to Compilers16 Translating Array Accesses Array access: t =  v[e]  (type of e1 is array[T] and S = size of T) t1 = addr v t2 =  e  t3 = t2 * S t4 = t1 + t3 t = [t4] array v e

CS 412/413 Spring 2002 Introduction to Compilers17 Translating Statements Statement sequence:  s1; s2; …; sN   s1   s2  …  sN  IR instructions of a statement sequence = concatenation of IR instructions of statements seq s1 s2 sN …

CS 412/413 Spring 2002 Introduction to Compilers18 Assignment Statements Variable assignment:  v = e  v =  e  Array assignment:  v[e1] = e2  t1 = addr v t2 =  e1  t3 = t2 * S t4 = t1 + t3 t5 =  e2  [t4] = t5 var-assign v e array-assign v e1e2

CS 412/413 Spring 2002 Introduction to Compilers19 Translating If-Then-Else  if (e) then s1 else s2  t1 =  e  fjump t1 Lfalse  s1  jump Lend label Lfalse  s2  label Lend if-then-else e s1 s2

CS 412/413 Spring 2002 Introduction to Compilers20 Translating If-Then  if (e) then s  t1 =  e  fjump t1 Lend  s  label Lend if-then e s

CS 412/413 Spring 2002 Introduction to Compilers21 While Statements  while (e) { s }  label Ltest t1 =  e  fjump t1 Lend  s  jump Ltest label Lend while e s1

CS 412/413 Spring 2002 Introduction to Compilers22 Switch Statements  switch (e) { case v1: s1, …, case vN: sN }  t =  e  c = t != v1 tjump c L2  s1  jump Lend label L2 c = t != v2 tjump c L3  s2  jump Lend … label LN c = t != vN tjump c Lend  sN  label Lend switch e s1v1sNvN …

CS 412/413 Spring 2002 Introduction to Compilers23 Call and Return Statements  call f(e1, e2, …, eN)  t1 =  e1  t2 =  e2  … tN =  eN  call f(t1, t2, …, tN)  return e  t =  e  return t call f e2eN … e1 return e

CS 412/413 Spring 2002 Introduction to Compilers24 Statement Expressions So far: statements which do not return values Easy extensions for statement expressions: –Block statements –If-then-else –Assignment statements t =  s  is the sequence of low IR code for statement s, whose result is stored in t

CS 412/413 Spring 2002 Introduction to Compilers25 Statement Expressions t =  if (e) then s1 else s2  t1 =  e  cjump t1 Ltrue t =  s2  jump Lend label Ltrue t =  s1  label Lend if-then-else e s1 s2 assign t

CS 412/413 Spring 2002 Introduction to Compilers26 Block Statements t =  s1; s2; …; sN   s1   s2  … t =  sN  Result value of a block statement = value of last statement in the sequence seq s1 s2 sN …

CS 412/413 Spring 2002 Introduction to Compilers27 Assignment Statements t =  v = e  v =  e  t = v Result value of an assignment statement = value of the assigned expression assign v e

CS 412/413 Spring 2002 Introduction to Compilers28 Nested Expressions In these translations, expressions may be nested; Translation recurses on the expression structure Example: t =  (a - b) * (c + d)  t1 = a t2 = b t3 = t1 – t2 t4 = b t5 = c t5 = t4 + t5 t = t3 * t5  (a - b)   (c + d)   (a - b) * (c + d) 

CS 412/413 Spring 2002 Introduction to Compilers29 Nested Statements Same for statements: recursive translation Example:  if c then if d then a = b  t1 = c fjump t1 Lend1 t2 = d fjump t2 Lend2 t3 = b a = t3 label Lend2 label Lend1  if c then …   if d …   a = b 

CS 412/413 Spring 2002 Introduction to Compilers30 Issues These translations are straightforward … and inefficient: –May generate many temporary variables –May generate many labels Can optimize translation process: –Don’t create temporaries for variables –Reuse temporary variables –Merge adjacent labels

CS 412/413 Spring 2002 Introduction to Compilers31 Optimize Translation Example: t = 〚 (a - b) * (c + d) 〛 t1 = a t2 = b t3 = t1 – t2 t4 = b t5 = c t5 = t4 + t5 t = t3 * t5 t = a - b t1 = b + c t = t * t1