Operational Semantics of Cool

Similar documents
Operational Semantics of Cool

Operational Semantics of Cool

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations

Intermediate Code & Local Optimizations. Lecture 20

Operational Semantics. One-Slide Summary. Lecture Outline

Operational Semantics

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Other Forms of Intermediate Code. Local Optimizations. Lecture 34

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages?

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

Compilers. Cool Semantics II. Alex Aiken

Intermediate Code & Local Optimizations

Multi-dimensional Arrays

Local Optimizations #1

Local Optimizations #1

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

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

Midterm II CS164, Spring 2006

Type Checking in COOL (II) Lecture 10

More Static Semantics. Static. One-Slide Summary. Lecture Outline. Typing Rules. Dispatch Rules SELF_TYPE

Solutions to Written Assignment 4

CS143 - Written Assignment 4 Reference Solutions

Languages and Compiler Design II IR Code Optimization

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

Lecture Outline. Type systems and their expressiveness. Type Checking in COOL (II) Type checking with SELF_TYPE in COOL

CODE GENERATION Monday, May 31, 2010

Intermediate Code Generation

CS 406/534 Compiler Construction Putting It All Together

Project Compiler. CS031 TA Help Session November 28, 2011

Code Optimization. Code Optimization

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

USC 227 Office hours: 3-4 Monday and Wednesday CS553 Lecture 1 Introduction 4

G Programming Languages - Fall 2012

Tour of common optimizations

CS143 Compilers - Written Assignment 4

Scoping rules match identifier uses with identifier definitions. A type is a set of values coupled with a set of operations on those values.

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

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Compilers and computer architecture: A realistic compiler to MIPS

Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code

CS143 Final Spring 2016

Three-Address Code IR

Passing Out Review Forms

More Static Semantics

CS 164, Midterm #2, Spring O, M, C - e1 : Bool O, M, C - e2 : t2 O, M, C - e2 : T3 O, M, C - if e1 then e2 else e3 fi : T2 _ T3

Programming Project 4: COOL Code Generation

IR Generation. May 13, Monday, May 13, 13

Building a Compiler with. JoeQ. Outline of this lecture. Building a compiler: what pieces we need? AKA, how to solve Homework 2

CS 4110 Programming Languages & Logics. Lecture 35 Typed Assembly Language

Semantic Analysis. Lecture 9. February 7, 2018

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

CS 4110 Programming Languages & Logics

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

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Principles of Compiler Design

Weeks 6&7: Procedures and Parameter Passing

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Running class Timing on Java HotSpot VM, 1

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

CS143 Final Fall 2009

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Topic 6: Types COS 320. Compiling Techniques. Princeton University Spring Prof. David August. Adapted from slides by Aarne Ranta

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

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

Lecture Outline. Type systems and their expressiveness. Type Checking in COOL (II) Type checking with SELF_TYPE in COOL

CS4215 Programming Language Implementation

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Structure of a Compiler. We looked at each stage in turn. Exceptions. Language Design and Implementation Issues

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Run-time Environments

Run-time Environments

ECE331: Hardware Organization and Design

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Undergraduate Compilers in a Day

CSCI565 Compiler Design

Programming Assignment IV Due Thursday, November 18th, 2010 at 11:59 PM

Mechanized Operational Semantics

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

COS 320. Compiling Techniques

Computing Inside The Parser Syntax-Directed Translation. Comp 412

Exercise 7 Bytecode Verification self-study exercise sheet

Compiler construction 2009

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

We will focus on data dependencies: when an operand is written at some point and read at a later point. Example:!

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Digital Forensics Lecture 3 - Reverse Engineering

Final Examination CS 4501 Spring 2012

CSE 141 Computer Architecture Spring Lecture 3 Instruction Set Architecute. Course Schedule. Announcements

CS558 Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Code Generation. Lecture 12

Field Analysis. Last time Exploit encapsulation to improve memory system performance

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Concepts Introduced in Chapter 6

IR Optimization. May 15th, Tuesday, May 14, 13

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

Transcription:

Operational Semantics of Cool Lecture 23 Dr. Sean Peisert ECS 142 Spring 2009 1

Status Project 3 due on Friday Project 4 assigned on Friday. Due June 5, 11:55pm Office Hours this week Fri at 11am No Class (Memorial Day) on Monday, May 25 Read Sec. 8.1 through 8.8 by Wednesday, May 27 Final Exam Review 9a-11a on June 5 in Olson 105 2

Assembly Language Description of Semantics Assembly-language descriptions of language implementation have too many irrelevant details Whether to use a stack machine or not Which way the stack grows How integers are represented on a particular machine We need a complete specification that is architecture and OS independent and doesn t overly restrict implementation choices. 3

Intro to Operational Semantics Once again we introduce a formal notation Using logical rules of inference, just like for typing Recall the typing judgment Context e : C (in the given context, expression e has type C) We try something similar for evaluation Context e : v (in the given context, expression e evaluates to value v) 4

What Contexts Are Needed? Obs.: Contexts are needed to handle variables Consider the evaluation of y x + 1 We need to keep track of values of variables We need to allow variables to change their values during the evaluation We track variables and their values with: An environment tells us at what address in memory is the value of a variable stored A store tells us what is the contents of a memory location 5

Stores A store maps memory locations to values Example: S = [l1 5, l2 7] To lookup the contents of a location l1 in store S we write S(l1) To perform an assignment of 12 to location l1 we write S[12/l1] This denotes a new store S such that for some location l S (l1) = 12 and S (l) = S(l) if l l1 6

Operational Rules of Cool The evaluation judgment is so, E, S e : v, S read: Given the object so with the current value of self And E the current variable environment And S the current store If the evaluation of e terminates then The returned value is v And the new store is S 7

Operational Semantics of new Consider the expression new T Informal semantics (we ll do formal ones in a moment) Essentially, allocate a new object Allocate new locations to hold the values for all attributes of an object of class T Initialize those locations with the default values of attributes Evaluate the initializers and set the resulting attribute values Return the newly allocated object 8

Default Values For each class A there is a default value denoted by DA Dint = Int(0) Dbool = Bool(false) Dstring = String(0, ) DA= void (for another class A) 9

More Notation For a class A we write the following mapping: class(a) = (a1: T1 e1,, an: Tn en) where ai are the attributes (including the inherited ones) Ti are their declared types ei are the initializers 10

Operational Semantics of new Obs. : new SELF_TYPE allocates an object with the same dynamic type as self if T == SELF_TYPE and so = X( ) then T0 = X else T0 = T class(t0) = (a1: T1 e1,, an: Tn en) li = newloc(s) foreach i = 1,,n v = T0(a1= l1,,an = ln) E = [a1: l1,, an: ln] S1= S[DT1/l1,,DTn/ln] v, E, S1 { a1 e1; ; an en; } : vn, S2 so, E, S new T : v, S2 11

Operational Semantics of new. Notes. The first three lines allocate the object The rest of the lines initialize it By evaluating a sequence of assignments State in which the initializers are evaluated Self is the current object Only the attributes are in scope (same as in typing) Starting value of attributes are the default ones The side-effect of initialization is preserved 12

Operational Semantics of Method Dispatch Consider the expression e 0.f(e1,,en) Informal semantics: Evaluate the arguments in order e 1,,en Evaluate e 0 to the target object Let X be the dynamic type of the target object Fetch from X the definition of f(with n args.) Create n new locations and an environment that maps f s formal arguments to those locations Initialize the locations with the actual arguments Set self to the target object and evaluate f s body 13

More Notation For a class A and a method f of A (possibly inherited) we write the following mapping: implementation(a, f) = (x1,, xn, ebody) where xi are the names of the formal arguments ebody is the body of the method 14

Operational Semantics of Dispatch so, E, S e1 : v1, S1 so, E, S1 e2 : v2, S2 so, E, Sn-1 en : vn, Sn so, E, Sn e0 : v0, Sn+1 v0 = X(a1= l1,, am = lm) implementation(x, f) = (x1,, xn,ebody) lxi = newloc(sn+1) for i = 1,,n E = [x1: lx1,, xn: lxn, a1: l1,,am: lm] Sn+2 = Sn+1[v1/lx1,,vn/lxn] v0, E, Sn+2 ebody : v, Sn+3 so, E, S e0.f(e1,,en) : v, Sn+3 15

Operational Semantics of Dispatch. Notes. The body of the method is invoked with E mapping formal arguments and self s attributes S like the caller s except with actual arguments bound to the locations allocated for formals The notion of the activation frame is implicit New locations are allocated for actual arguments The semantics of static dispatch is similar except the implementation of f is taken from the specified class 16

Runtime Errors Operational rules do not cover all cases. Consider for example the rule for dispatch: so, E, Sn e0 : v0,sn+1 v0 = X(a1= l1,, am = lm) implementation(x, f) = (x1,, xn,ebody) so, E, S e0.f(e1,,en) : v, Sn+3 What happens if the mapping function implementation(x, f) is not defined? Cannot happen in a well-typed program (type safety theorem) 17

Runtime Errors (Cont.) There are some runtime errors that the type checker does not try to prevent A dispatch on void Division by zero Substring out of range Heap overflow In such case the execution should abort gracefully With an error message not with segfault 18

Conclusions Operational rules are very precise Nothing (that can be specified) is left unspecified Operational rules contain a lot of details Read them carefully Most languages do not have a well specified operational semantics (but Cool does) When portability is important an operational semantics becomes essential (hence portability problems) But not always using the notation we used for Cool 19

Local Optimizations 20

Code Generation Summary We have discussed Runtime organization Simple stack machine code generation Improvements to stack machine code generation Our compiler goes directly from AST to assembly language And does not perform optimizations Most real compilers use intermediate languages 21

Why Intermediate Languages? When to perform optimizations On AST Pro: Machine independent Con: Too high level On assembly language Pro: Exposes optimization opportunities Con: Machine dependent Con: Must re-implement optimizations when re-targeting On an intermediate language Pro: Machine independent Pro: Exposes optimization opportunities Con: One more language to worry about 22

Intermediate Languages Each compiler uses its own intermediate language IL design is still an active area of research Intermediate language = high-level assembly language Uses register names, but has an unlimited number Uses control structures like assembly language Uses opcodes but some are higher level 23

Three-Address Intermediate Code Each instruction is of the form x := y op z y and z can be only registers or constants Just like assembly Common form of intermediate code The AST expression x + y * z is translated as t1:= y * z t2:= x + t1 Each sub expression has a name 24

Generating Intermediate Code Similar to assembly code generation Major difference Use any number of IL registers to hold intermediate results 25

Generating Intermediate Code igen(e, t)function generates code to compute the value of e in register t Example: igen(e 1 + e2, t) = igen(e 1, t1) (t1 is a fresh register) igen(e 2, t2) (t2 is a fresh register) t := t 1 + t2 Unlimited number of registers simple code generation 26

Intermediate Code Notes You should be able to use intermediate code At the level discussed in lecture You are not expected to know how to generate intermediate code Because we won t discuss it But really just a variation on code generation... 27

Notes Dragon book has very good coverage on Also good coverage on Intermediate code (Ch. 8) read this Code generation (Ch. 9) optional Local and global optimizations (Ch. 10) optional 28

An Intermediate Language P S P ε id s are register names S id := id op id Constants can replace id s id := op id Typical operators: +, -, * id := id push id id := pop if id relop id goto L L: jump L 29

Two Useful Concepts Basic blocks (BB) Split code into basic atomic units Control-flow graphs (CFG) Connect the BBs together as a directed graph Useful for representing intermediate code Use a graphical representation Make control-flow explicit 30

Definition. Basic Blocks A basic block is a maximal sequence of instructions with: no labels (except at the first instruction), and no jumps (except in the last instruction) Idea: Cannot jump in a basic block (except at beginning) Cannot jump out of a basic block (except at end) Each instruction in a basic block is executed after all the preceding instructions have been executed 31

Basic Block Example Consider the basic block 1. L: 2. t := 2 * x 3. w := t + x 4. if w > 0 goto L No way for (3) to be executed without (2) having been executed right before We can change (3) to w := 3 * x Can we eliminate (2) as well? 32

33 Definition. Control-Flow Graphs A control-flow graph is a directed graph with Basic blocks as nodes An edge from block A to block B if the execution can flow from the last instruction in A to the first instruction in B E.g., the last instruction in A is jump LB E.g., the execution can fall-through from block A to block B Frequently abbreviated as CFG

Control-Flow Graphs. Example. The body of a method (or procedure) can be represented as a control-flow graph There is one initial node All return nodes are terminal 34

Optimization Overview Optimization seeks to improve a program s utilization of some resource Execution time (most often) Code size Network messages sent Battery power used, etc. Optimization should not alter what the program computes correctness The answer must still be the same 35

A Classification of Optimizations For languages like C and Cool there are three granularities of optimizations 1. Local optimizations Apply to a basic block in isolation 2. Global optimizations(a.k.a. intra-procedural) Apply to a control-flow graph (method body) in isolation 3. Inter-procedural optimizations Apply across method boundaries Most compilers do (1), many do (2) and very few do (3) 36

Cost of Optimizations In practice, a conscious decision is made not to implement the fanciest optimization known Why? Some optimizations are hard to implement Some optimizations are costly in terms of compilation time The fancy optimizations are both hard and costly The goal Maximum improvement with minimum cost 37

Local Optimizations The simplest form of optimizations No need to analyze the whole procedure body Just the basic block in question Example: algebraic simplification 38

Algebraic Simplification Some statements can be deleted x := x + 0 x := x * 1 Some statements can be simplified x := x * 0 x := 0 y := y ** 2 y := y * y x := x * 8 x := x << 3 x := x * 15 t := x << 4; x := t - x (on some machines << is faster than *; but not on all!) 39

Constant Folding Operations on constants can be computed at compile time In general, if there is a statement x := y op z And y and z are constants Then y op z can be computed at compile time Example: x := 2 + 2 x := 4 Example: if 2 < 0 jump L can be deleted When might constant folding be dangerous? 40

Flow of Control Optimizations Eliminating unreachable code: Code that is unreachable in the control-flow graph Basic blocks that are not the target of any jump or fall through from a conditional Such basic blocks can be eliminated Why would such basic blocks occur? Removing unreachable code makes the program smaller And sometimes also faster Due to memory cache effects (increased spatial locality) 41

Single Assignment Form Some optimizations are simplified if each register occurs only once on the left-hand side of an assignment Intermediate code can be rewritten to be in single assignment form x := z + y b := z + y a := x a := b x := 2 * x x := 2 * b (b is a fresh register) More complicated in general, due to loops 42

Common Subexpression Elimination Assume Basic block is in single assignment form A definition x := is the first use of x in a block All assignments with same rhs compute the same value Example: x := y + z x := y + z w := y + z w := x (the values of x, y, and z do not change in the code) 43

Copy Propagation If w := x appears in a block, all subsequent uses of w can be replaced with uses of x Example: b := z + y b := z + y a := b a := b x := 2 * a x := 2 * b This does not make the program smaller or faster but might enable other optimizations Constant folding Dead code elimination 44

Copy Propagation and Constant Folding Example: a := 5 a := 5 x := 2 * a x := 10 y := x + 6 y := 16 t := x * y t := x << 4 45

Finish Project 3! 46