Compilers. Cool Semantics II. Alex Aiken

Similar documents
Operational Semantics of Cool

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

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

Operational Semantics. One-Slide Summary. Lecture Outline

Operational Semantics of Cool

Operational Semantics of Cool

Operational Semantics

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

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

Type Checking in COOL (II) Lecture 10

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

1 Lexical Considerations

Lexical Considerations

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

Lexical Considerations

UVa ID: (yes, again!)

More Static Semantics

CoolAid: The Cool Reference Manual

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

COMP520 - GoLite Type Checking Specification

IC Language Specification

CS143 Final Spring 2016

Passing Out Review Forms

More Static Semantics

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

G Programming Languages - Fall 2012

t=t + "g"; On the other hand, arrays are mutable. The assignment a[i]=e/// where e element is equal 6

The Cool Reference Manual

a data type is Types

COMP520 - GoLite Type Checking Specification

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

type environment updated subtype sound

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

Lecture08: Scope and Lexical Address

CS2141 Software Development using C/C++ C++ Basics

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CMSC330 Fall 2013 Practice Problems 6 Solutions

Semantic Analysis and Type Checking

Chapter 4 Defining Classes I

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

Understand Execution of a Program

Code Generation II. Code generation for OO languages. Object layout Dynamic dispatch. Parameter-passing mechanisms Allocating temporaries in the AR

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

(Not Quite) Minijava

Written Assignment 5 Due??

Run-time Environments

CS453 CLASSES, VARIABLES, ASSIGNMENTS

Run-time Environments

The role of semantic analysis in a compiler

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Final CSE 131B Spring 2004

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

PES INSTITUTE OF TECHNOLOGY

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

CPSC 213, Winter 2009, Term 2 Midterm Exam Date: March 12, 2010; Instructor: Mike Feeley

Java Primer 1: Types, Classes and Operators

The Decaf Language. 1 Lexical considerations

Programming Project 4: COOL Code Generation

COMPILER DESIGN - RUN-TIME ENVIRONMENT

CSE 504: Compiler Design. Runtime Environments

Late Binding; OOP as a Racket Pattern

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

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

Run-Time Data Structures

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

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

Code Generation & Parameter Passing

Lecture 3: C Programm

Winter Compiler Construction Who. Mailing list and forum

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

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Supporting Class / C++ Lecture Notes

Semantic Analysis. Lecture 9. February 7, 2018

内存管理. Memory management

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Type Checking Binary Operators

Decaf Language Reference Manual

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

University of Palestine. Mid Exam Total Grade: 100

CAAM 420 Daily Note. Scriber: Qijia Jiang. Date: Oct.16. Project 3 Due Wed 23.Oct. Two parts: debug code and library exercise.

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Array. Prepared By - Rifat Shahriyar

Weeks 6&7: Procedures and Parameter Passing

Computer Programming: C++

Exception Safe Coding

The compilation process is driven by the syntactic structure of the program as discovered by the parser

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

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

Run-Time Organization

CSE351 Winter 2016, Final Examination March 16, 2016

Making New instances of Classes

Implementing Subprograms

CSE 307: Principles of Programming Languages

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Run-Time Environments

CS 2630 Computer Organization. Meeting 10/11: data structures in MIPS Brandon Myers University of Iowa

The Decaf language 1

Local Optimizations #1

Transcription:

Compilers

Informal semantics of new T Allocate locations to hold all attributes of an object of class T Essentially, allocate a new object Set attributes with their default values Evaluate the initializers and set the resulting attribute values Return the newly allocated object

For each class A there is a default value D A D int = Int(0) D bool = Bool(false) D string = String(0, ) D A = void (for any other class A)

For a class A we write class(a) = (a 1 : T 1 e 1,, a n : T n e n ) where a i are the attributes (including the inherited ones) T i are the attributes declared types e i are the initializers

T 0 = if (T == SELF_TYPE and so = X( )) then X else T class(t 0 ) = (a 1 : T 1 e 1,, a n : T n e n ) l i = newloc(s) for i = 1,,n v = T 0 (a 1 = l 1,,a n = l n ) S 1 = S[D T1 /l 1,,D Tn /l n ] E = [a 1 : l 1,, a n : l n ] v, E, S 1 d { a 1 e 1 ; ; a n e n ; } : v n, S 2 so, E, S d new T : v, S 2

The first three steps allocate the object The remaining steps 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) Initial values of attributes are the defaults

Informal semantics of e 0.f(e 1,,e n ) Evaluate the arguments in order e 1,,e n 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

For a class A and a method f of A (possibly inherited): impl(a, f) = (x 1,, x n, e body ) where x i are the names of the formal arguments e body is the body of the method

so, E, S d e 1 : v 1, S 1 so, E, S 1 d e 2 : v 2, S 2 so, E, S n-1 d e n : v n, S n so, E, S n d e 0 : v 0, S n+1 v 0 = X(a 1 = l 1,, a m = l m ) impl(x, f) = (x 1,, x n, e body ) l xi = newloc(s n+1 ) for i = 1,,n E = [a 1 : l 1,,a m : l m ][x 1 /l x1,, x n /l xn ] S n+2 = S n+1 [v 1 /l x1,,v n /l xn ] v 0, E, S n+2 d e body : v, S n+3 so, E, S d e 0.f(e 1,,e n ) : v, S n+3

What is the final value of S 5 in the dispatch of obj.foo(i) below? so, [i:l i ], S 1 d i : 3, S 2 so, [i:l i ], S 2 d obj : C(a = l obj_a ), S 3 impl(c, foo) = (x, x + a) l x = newloc(s 3 ) S 4 = S 3 [3/l x ] C(a = l obj_a ), [a:l obj_a ][x/l x ], S 4 d x + a : 4, S 5 so, [i:l i ], [l obj_a 1, l i 3] d obj.foo(i) : 4, S 5 Class C { a: Int <- 0; foo(x: Int) : Int { x + a }; }; [l i 3] [l obj_a 1, l i 3] [l obj_a 1, l i 3, l x 3] It cannot be determined from the information given.

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 frame is implicit New locations are allocated for actual arguments The semantics of static dispatch is similar

Runtime Errors Operational rules do not cover all cases Consider the dispatch example: so, E, S n d e 0 : v 0,S n+1 v 0 = X(a 1 = l 1,, a m = l m ) impl(x, f) = (x 1,, x n, e body )

There are some runtime errors that the type checker does not prevent A dispatch on void Division by zero Substring out of range Heap overflow In such cases execution must abort gracefully With an error message, not with a segfault

Operational rules are very precise & detailed Nothing is left unspecified Read them carefully Most languages do not have a well specified operational semantics When portability is important an operational semantics becomes essential