Topic 7: Intermediate Representations

Similar documents
Compilation 2013 Translation to IR

7 Translation to Intermediate Code

Intermediate Code Generation

Itree Stmts and Exprs. Back-End Code Generation. Summary: IR -> Machine Code. Side-Effects

Compiler Construction 2009/2010: Intermediate Representation

Administration. Where we are. Canonical form. Canonical form. One SEQ node. CS 412 Introduction to Compilers

COS 320. Compiling Techniques

Announcements. Project 2: released due this sunday! Midterm date is now set: check newsgroup / website. Chapter 7: Translation to IR

Administration. Today

Runtime Support for Algol-Like Languages Comp 412

CPSC 411, Fall 2010 Midterm Examination

Compiler Internals. Reminders. Course infrastructure. Registering for the course

Languages and Compiler Design II IR Code Generation I

Where we are. What makes a good IR? Intermediate Code. CS 4120 Introduction to Compilers

Programming Language Processor Theory

Topic 16: Issues in compiling functional and object-oriented languages

Midterm II CS164, Spring 2006

Qualifying Exam in Programming Languages and Compilers

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Compilation 2013 Basic Blocks and Traces

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

We ve written these as a grammar, but the grammar also stands for an abstract syntax tree representation of the IR.

Lecture Notes on Intermediate Representation

Compiler Optimization Techniques

Topic 12: Register Allocation

Run-time Environments

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Run-time Environments

Project Compiler. CS031 TA Help Session November 28, 2011

Optimizations. Optimization Safety. Optimization Safety CS412/CS413. Introduction to Compilers Tim Teitelbaum

Topic 14: Scheduling COS 320. Compiling Techniques. Princeton University Spring Lennart Beringer

Chapter 9 Subroutines and Control Abstraction. June 22, 2016

Static Program Analysis Part 1 the TIP language

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Lecture Notes on Intermediate Representation

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

CS 406/534 Compiler Construction Putting It All Together

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Lecture 15 CIS 341: COMPILERS

Control Abstraction. Hwansoo Han

Functional programming Primer I

Compilation 2013 Semantic Analysis

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

CS4215 Programming Language Implementation

Lecture 7: Binding Time and Storage

G Programming Languages - Fall 2012

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

(Not Quite) Minijava

The role of semantic analysis in a compiler

Intermediate Code Generation. Intermediate Representations (IR) Case Study : itree. itree Statements and Expressions

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CS415 Compilers. Intermediate Represeation & Code Generation

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Control Flow Analysis

2. Reachability in garbage collection is just an approximation of garbage.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

CS558 Programming Languages

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 5. Exam 1 overview. Type checking basics. HW4 due. HW5 out, due in 2 Tuesdays

Structure of Programming Languages Lecture 10

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

ACM Trivia Bowl. Thursday April 3 rd (two days from now) 7pm OLS 001 Snacks and drinks provided All are welcome! I will be there.

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis

Syntax Analysis, III Comp 412

Implementing Control Flow Constructs Comp 412

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Short Notes of CS201

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

CS558 Programming Languages

CS558 Programming Languages

Project 5 Due 11:59:59pm Wed, Nov 25, 2015 (no late submissions)

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

CSE 504: Compiler Design. Code Generation

CS 231 Data Structures and Algorithms, Fall 2016

CS201 - Introduction to Programming Glossary By

Intermediate Code, Object Representation, Type-Based Optimization

Intermediate Representations & Symbol Tables

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

Class Information ANNOUCEMENTS

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

NOTE: Answer ANY FOUR of the following 6 sections:

CSc 453 Intermediate Code Generation

Connecting Definition and Use? Tiger Semantic Analysis. Symbol Tables. Symbol Tables (cont d)

CS 241 Honors Memory

Virtual Machine. Part I: Stack Arithmetic. Building a Modern Computer From First Principles.

Overview of the Class

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

Program Syntax; Operational Semantics

Lecture Notes on Intermediate Representation

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

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

To figure this out we need a more precise understanding of how ML works

COMPUTER SCIENCE TRIPOS

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Transcription:

Topic 7: Intermediate Representations COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1

2 Intermediate Representations

3 Intermediate Representations

4 Intermediate Representations

5 Properties of a Good IR

IR Representations and Tiger (Explanations on next slides) 6

IR Expression Trees (Explanations on next slides) 7

8 Expressions

9 Expressions

10 Statements

Statements Next: generation of IR code from Absyn heavily interdependent with design of FRAME module in MCIL (abstract interface of activation records, architecture-independent) But first 11

Midterm exam info When? Thursday, March 10 th, 3pm 4:20pm Where? CS 104 (HERE) Closed book / notes, no laptop/smartphone. Honor code applies Material in scope: up to HW 3 (parser), and anything covered in class until this Friday. 12 Preparation: exercises at end of book chapters in MCIL old exams: follow link on course home page

Midterm exam prep QUIZ (also because it s Tuesday) (Spring 2011) e3 13

Translation of Abstract Syntax Goal: function Translate: Absyn.exp => IR Observation: different expression forms in Absyn.exp suggest use of different parts of IR Solution 1: given e:absyn.exp, always generate a Tree.exp term: case A: immediate case B: instead of a Tree.stm s, generate Tree.ESEQ(s, Tree.CONST 0) case C: Tree.ESEQ (s, Tree.TEMP r) : cf expression on slide 16 Resulting code less clean than solution 2 14

Translation of Abstract Syntax Solution 2: define a wrapper datatype with injections (ie constructors) for the 3 cases 15

Translation of Abstract Syntax (Conditionals) Translation of a simple conditional into a Tree.stm: Translation of a more complex conditional into a Tree.stm: Translation of a more conditional into a Tree.exp, to be assigned to a variable: Then, can use conversion function unex: exp => Tree.exp. Convenient to have unnx and uncx, too: 16

Translation of Abstract Syntax (Conditionals) Implementation? 17

Translation of Abstract Syntax unex: exp => Tree.exp: 18 Example: flag := x>y: genstmt = fun (t,f) => CJUMP (GT, Temp x, Temp y, t_label, f_label) Implementation of unnx and uncx similar. Pseudocode Temp r := 1; CJUMP (GT, Temp x, Temp y, t_label, f_label); f_label: Temp r := 0; t_label: flag := r (*program continuation*)

Translation of Abstract Syntax Primary result of sematic analysis: a type environment TENV: collects type declarations, ie maps type names to representations of type a value environment VENV: collects variable declarations, ie maps variable names x to either a type (if x represents a non-function variable) a lists of argument types, and a return type (if x represents a function) But also: generate IR code Tiger: translation functions transexpr, transvar, transdec, and transty based on the syntactic structure of Tiger s Abysn. 19 In particular: TransExp returns record {Tree.exp, ty}.

IR code generation complex for Tiger don t want to be processor specific: abstract notion of frames, with abstract parameter slots ( access ): constructors inframe or inreg, further abstraction layer to separate implementation of translate function from use of these functions in semantic analysis (type check) 20

IR code generation complex for Tiger don t want to be processor specific: abstract notion of frames, with abstract parameter slots ( access ): constructors inframe or inreg, further abstraction layer to separate implementation of translate function from use of these functions in semantic analysis (type check) Root problem: escaped variables Address-taken variables Call by reference variables Nested functions Stack-allocated data structures (records) : 21

IR code generation complex for Tiger don t want to be processor specific: abstract notion of frames, with abstract parameter slots ( access ): constructors inframe or inreg, further abstraction layer to separate implementation of translate function from use of these functions in semantic analysis (type check) Root problem: escaped variables Address-taken variables Call by reference variables Nested functions Stack-allocated data structures (records) : Absence of these features would make frame stack construction and hence IR emission MUCH easier 22

IR code generation complex for Tiger don t want to be processor specific: abstract notion of frames, with abstract parameter slots ( access ): constructors inframe or inreg, further abstraction layer to separate implementation of translate function from use of these functions in semantic analysis (type check) Root problem: escaped variables Address-taken variables Call by reference variables Nested functions Stack-allocated data structures (records) : Compiler design Absence of these features would make frame stack construction and hence IR emission MUCH easier Language design Example of modern language that avoids these features: Java 23

IR code generation: local variable access 24 Choice as to which variables are inframe and which ones are inreg is architecture-specific, so implemented inside FRAME module. FRAME also provides mechanism to construct abstract activation records, containing one inframe/inreg access for each formal parameter.

25 IR code generation: variable access

Simple Variables Thus, IR code generation for a function body can be done using uniform notion of parameter slots ( access ) in an abstract description of Frames: interface of frame says for each parameter whether it s inframe or inreg different implementations of Frame module can follow different policies given any Frame implementation, Translate generates suitable code 26

27 Array Access

Record Access 2 28

Records: allocation and deallocation Records can outlive function invocations, so allocation happens not on stack but on heap, by call to an other runtime function ALLOC to which a call is emitted by the compiler should include code for (type-correct) initialization of components details below 29

Records: allocation and deallocation Records can outlive function invocations, so allocation happens not on stack but on heap, by call to an other runtime function ALLOC to which a call is emitted by the compiler should include code for (type-correct) initialization of components details below deallocation: no explicit instruction in the source language, so either no deallocation (poor memory usage: memory leak), or compiler has an analysis phase that (conservatively) estimates lifetime of records (requires alias analysis) and inserts calls to runtime function FREE at appropriate places, or dynamic garbage collection (future lecture) Similar issues arise for allocation/deallocation of arrays. 30

Conditional Statements if e1 then e2 else e3 Approach 1: use CJUMP ok in principle, but doesn t work well if e1 contains &, 31

Conditional Statements if e1 then e2 else e3 Approach 1: use CJUMP ok in principle, but doesn t work well if e1 contains &, Approach 2: exploit Cx constructor yields good code if e1 contains &, treat e1 as Cx expression apply uncx use fresh labels as entry points for e2 and e3 treat e2, e3 as Ex expressions apply unex 32

Conditional Statements if e1 then e2 else e3 Approach 1: use CJUMP ok in principle, but doesn t work well if e1 contains &, Approach 2: exploit Cx constructor yields good code if e1 contains &, treat e1 as Cx expression apply uncx use fresh labels as entry points for e2 and e3 treat e2, e3 as Ex expressions apply unex 33 if e1 then JUMP t else JUMP f ; t: r := e2 (*code for e2, leaving result in r*) JUMP join f: r := e3 (*code for e3, leaving result in r*) join: (*program continuation, can use r*)

Conditional Statements if e1 then e2 else e3 if e1 then JUMP t else JUMP f ; t: r := e2 (*code for e2, leaving result in r*) JUMP join f: r := e3 (*code for e3, leaving result in r*) join: (*program continuation, can use r*) 34 Optimizations possible, e.g. if e2/e3 are themselves Cx expressions see MCIL

Strings Can think of as additional function definitions, for which the compiler silently generates code, too 35

Strings length 36

37 Strings

38 Array Creation

Record Creation (3*w) ] ) ), 39

40 While Loops

41 For Loops

For Loops (Approx.) solution hinted to in MCIL: in if lo <= hi then lab: body if i < limit then i++; JUMP lab else JUMP done else JUMP done done: 42

43 Function Calls

Declarations basic idea: 44

Declarations basic idea: Complications: need auxiliary info level, break inside translation of body need to insert code for variable initialization. 45

Declarations basic idea:, inits = e} = ESEQ (e, ) 46 Complications: need auxiliary info level, break inside translation of body need to insert code for variable initialization. Thus, transdecs is modified to additionally return an expression list e of assignment expressions that s inserted HERE (and empty for function and type declarations)

47 Function Declarations

48 Function Prolog

49 Function Epilog

Fragments inframe, inreg etc 50

51 Problem with IR Trees

52 Canonicalizer: overview

Elimination of (E)SEQs Goal: Move ESEQ and SEQ nodes towards the top of a Tree.stm by repeatedly applying local rewrite rules SEQ ESEQ ESEQ SEQ (selected rewrite rules on next slides) 53

Rewrite rules 1 54

Rewrite rules 1 2 (also for MEM, JUMP, CJUMP in place of BINOP) 55

Rewrite rules 1 2 (also for MEM, JUMP, CJUMP in place of BINOP) What about this: 56

Rewrite rules 1 2 (also for MEM, JUMP, CJUMP in place of BINOP) What about this: Incorrect if s contains assignment to a variable read by e1! 57

Rewrite rules 1 2 (also for MEM, JUMP, CJUMP in place of BINOP) What about this: General Solution: 3 Incorrect if s contains assignment to a variable read by e1! (also for CJUMP in place of BINOP) 58

Rewrite Rules Specific solution: 4 In fact correct if s and e1 commute! When do s and e commute? variables and memory locations accessed by s are disjoint from those accessed by e no disjointness but all accesses to such a shared resource are READ (Similarly for CJUMP) 59

Rewrite Rules Specific solution: 4 In fact correct if s and e1 commute! When do s and e commute? variables and memory locations accessed by s are disjoint from those accessed by e no disjointness but all accesses to such a shared resource are READ (Similarly for CJUMP) But: deciding whether MEM(x) and MEM(z) represent the same location requires deciding whether x and z may be equal Undecidable in general! 60

Rewrite Rules Specific solution: 4 In fact correct if s and e1 commute! When do s and e commute? variables and memory locations accessed by s are disjoint from those accessed by e no disjointness but all accesses to such a shared resource are READ (Similarly for CJUMP) But: deciding whether MEM(x) and MEM(z) represent the same location requires deciding whether x and z may be equal Undecidable in general! Solution: Compiler conservatively approximates disjointness / commutability, ie performs the rewrite cautiously example: e1 == CONST(i) 61

Rewrite Rules Goal 2: ensure that parent of a CALL is EXP ( ) or MOVE(TEMP t, ) Motivation: calls leave their result in dedicated register rv. Now consider tree T. What could go wrong? T: 62

Rewrite Rules Goal 2: ensure that parent of a CALL is EXP ( ) or MOVE(TEMP t, ) Motivation: calls leave their result in dedicated register rv. Now consider tree T. T: What could go wrong? Call to g will overwrite result of f (held in rv) before BINOP is executed! Solution? 63

Rewrite Rules Goal 2: ensure that parent of a CALL is EXP ( ) or MOVE(TEMP t, ) Motivation: calls leave their result in dedicated register rv. Now consider tree T. T: 5 What could go wrong? Call to g will overwrite result of f (held in rv) before BINOP is executed! Solution: save result of call to f before calling g, to avoid overwriting rv! 64 (where t is a fresh temp; don t apply recursively under the pattern MOVE(TEMP(..), CALL(..) ))

Rewrite Rules Goal 2: ensure that parent of a CALL is EXP ( ) or MOVE(TEMP t, ) Motivation: calls leave their result in dedicated register rv. Now consider tree T. T: What could go wrong? Call to g will overwrite result of f (held in rv) before BINOP is executed! Solution: save result of call to f before calling g, to avoid overwriting rv! 5 Homework 1: apply rules 1-5 to rewrite T until no more rules can be applied. Is your solution optimal? 65 (where t is a fresh temp; don t apply recursively under the pattern MOVE(TEMP(..), CALL(..) )) Homework 2: can you think of additional rules, for nested calls?

Elimination of SEQs Associativity of SEQ: 6 Final step: once all SEQ s are at top of tree, collect list of statements left-to-right 66

Elimination of SEQs Associativity of SEQ: SEQ ESEQ ESEQ s3 s1 SEQ s1 e s2 s3 Final steps: once all SEQ s are at top of tree, extract list of statements left-to-right 67

Midterm exam End of lecture material that s relevant for the midterm. Thus, MCIL material up to (and including) Section 8.1 Canonical Trees is fair game. 68

Normalization of branches Remember conditional branch instruction of TREE: true branch CJUMP (relop, exp, exp, label, label) false branch Assembly languages: conditional branches typically have only one label So need to analyze control flow of program: what s the order in which an execution might walk through program, ie execute instructions? 69

Normalization of branches Remember conditional branch instruction of TREE: true branch CJUMP (relop, exp, exp, label, label) false branch Assembly languages: conditional branches typically have only one label So need to analyze control flow of program: what s the order in which an execution might walk through program, ie execute instructions? sequence of non-branching instructions: trivial, in sequential order unconditional jumps: obvious follow the goto CJMUP: cannot predict outcome, so need to assume either branch may be taken 70 For analysis of control flow, can consider sequences of non-branching instructions as single node ( basic block )

Basic blocks A basic block is a sequence of statements such that the first statement is a LABEL instruction the last statement is a JUMP or CJUMP there are no other LABELSs, JUMPs, or CJUMPs 71

Basic blocks A basic block is a sequence of statements such that the first statement is a LABEL instruction the last statement is a JUMP or CJUMP there are no other LABELSs, JUMPs, or CJUMPs Task: partition a sequence of statements (Ln: LABEL n; si = straight-line stmt) L1 s1 s2 s3 CJUMP.. L2 s4 JUMP L1 L3 s5 s6 s7 JUMP L2 (L1, L3) into a sequence of basic blocks L1 s1 s2 s3 CJUMP.. L2 s4 JUMP L1 L3 s5 s6 s7 JUMP L2 (L1, L3) 72

Partitioning into basic blocks Naïve algorithm: traverse left-to-right whenever a LABEL is found, start a new BB (and end current BB) whenever a JUMP or CJUMP is found, end current BB (and start new BB) insert fresh LABELs at beginnings of BBs that don t start with a LABEL insert JUMPs at ends of BBs that don t end with a JUMP or CJUMP s1 L1 s2 s3 CJUMP.. s4 L2 L3 s5 s6 JUMP L1 s7 JUMP L2 (L1, L3) 73

Partitioning into basic blocks Naïve algorithm: traverse left-to-right whenever a LABEL is found, start a new BB (and end current BB) whenever a JUMP or CJUMP is found, end current BB (and start new BB) insert fresh LABELs at beginnings of BBs that don t start with a LABEL insert JUMPs at ends of BBs that don t end with a JUMP or CJUMP s1 L1 s2 s3 CJUMP.. s4 L2 L3 s5 s6 JUMP L1 s7 JUMP L2 (L1, L3) s1 L1 s2 s3 CJUMP.. s4 L2 L3 s5 s6 JUMP L1 s7 JUMP L2 (L1, L3) 74

Partitioning into basic blocks Better algorithm: traverse left-to-right whenever a LABEL is found, start a new BB (and end current BB) whenever a JUMP or CJUMP is found, end current BB (and start new BB) insert fresh LABELs at beginnings of BBs that don t start with a LABEL insert JUMPs at ends of BBs that don t end with a JUMP or CJUMP s1 L1 s2 s3 CJUMP.. (L1, L3) s4 L2 L3 s5 s6 JUMP L1 s7 JUMP L2 JUMP L1 JUMP L2 JUMP L3 75 s1 L1 s2 s3 CJUMP.. s4 L2 L3 s5 s6 JUMP L1 s7 JUMP L2 (L1, L3) L0 L4 L5

Partitioning into basic blocks Better algorithm: traverse left-to-right whenever a LABEL is found, start a new BB (and end current BB) whenever a JUMP or CJUMP is found, end current BB (and start new BB) insert fresh LABELs at beginnings of BBs that don t start with a LABEL insert JUMPs at ends of BBs that don t end with a JUMP or CJUMP convenient to also add a special LABEL D for epilogue and add JUMP D s1 L1 s2 s3 CJUMP.. s4 L2 L3 s5 s6 JUMP L1 s7 JUMP L2 (L1, L3) JUMP L1 JUMP L2 JUMP L3 76 s1 L1 s2 s3 CJUMP.. s4 L2 L3 s5 s6 JUMP L1 s7 JUMP L2 (L1, L3) L0 L4 L5

Ordering basic blocks Given that basic blocks have entry labels and jumps at end relative order of basic blocks irrelevant so reorder to ensure (if possible) that a block ending in CJUMP is followed by the block labeled with the FALSE label JUMP is followed by its target label More precisely: cover the collection of basic blocks by a set of traces: sequences of stmts (maybe including jumps) that are potentially executed sequentially aims: have each basic block covered by only one trace use low number of traces in order to reduce number of JUMPS 77