Operational Semantics of Cool

Similar documents
Operational Semantics of Cool

Operational Semantics of Cool

Operational Semantics. One-Slide Summary. Lecture Outline

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

Operational Semantics

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

Compilers. Cool Semantics II. Alex Aiken

Type Checking in COOL (II) Lecture 10

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

CoolAid: The Cool Reference Manual

Semantic Analysis. Lecture 9. February 7, 2018

1 Lexical Considerations

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

The Cool Reference Manual

Lexical Considerations

Lexical Considerations

CS558 Programming Languages

CSE 341: Programming Languages

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

Overview of Semantic Analysis. Lecture 9

More Static Semantics

Principles of Programming Languages

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

Passing Out Review Forms

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

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

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

IC Language Specification

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

Programming Languages Third Edition

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CS171:Introduction to Computer Science II

COS 320. Compiling Techniques

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

Written Assignment 5 Due??

Semantic Analysis and Type Checking

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

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

Winter Compiler Construction Who. Mailing list and forum

Midterm II CS164, Spring 2006

Java and C I. CSE 351 Spring Instructor: Ruth Anderson

CS558 Programming Languages. Winter 2013 Lecture 3

CMSC 330: Organization of Programming Languages. Operational Semantics

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

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

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

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

Solutions to Written Assignment 4

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Project 5 Due 11:59:59pm Tuesday, April 25, 2017

Compilers and computer architecture: A realistic compiler to MIPS

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

(Not Quite) Minijava

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

The SPL Programming Language Reference Manual

CS4215 Programming Language Implementation

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

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

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

type environment updated subtype sound

EI326 ENGINEERING PRACTICE & TECHNICAL INNOVATION (III-G) Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

Programming Languages Third Edition. Chapter 7 Basic Semantics

Final CSE 131B Spring 2004

Functional Programming. Pure Functional Programming

Lecture 3 Notes Arrays

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

CS558 Programming Languages

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

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

Last name:... First name:... Department (if not D-INFK):...

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CS1 Lecture 3 Jan. 18, 2019

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

CoolAid: The Cool 2016 Reference Manual

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

CSCE 314 Programming Languages. Type System

The role of semantic analysis in a compiler

CS 415 Midterm Exam Spring SOLUTION

Project Compiler. CS031 TA Help Session November 28, 2011

Project 5 Due 11:59:59pm Tue, Dec 11, 2012

Application: Programming Language Semantics

Derived and abstract data types. TDT4205 Lecture 15

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Mutable References. Chapter 1

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

A Short Summary of Javali

Java and C. CSE 351 Autumn 2018

Wizard is about to die.

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

1 Introduction. 3 Syntax

G Programming Languages - Fall 2012

CS143 Final Spring 2016

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

The Decaf Language. 1 Lexical considerations

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

Announcements. Written Assignment 2 due today at 5:00PM. Programming Project 2 due Friday at 11:59PM. Please contact us with questions!

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

Transcription:

Operational Semantics of Cool Lecture 22 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 are Wed at 4pm and 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

Motivation We must specify for every Cool expression what happens when is evaluated. This is the meaning of an expression. The definition of a programming language: The tokens lexical analysis The grammar syntactic analysis The typing rules semantic analysis The evaluation rules code generation and optimization 3

Evaluation Rules So Far So far, we specified the evaluation rules indirectly. We specified the compilation of Cool to a stack machine We specified the evaluation rules of the stack machine This is a complete description Why isn t it good enough? 4

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. 5

Programming Language Semantics There are many ways to specify programming language semantics. They are all equivalent but some are more suitable to various tasks than others Operational semantics Describes the evaluation of programs on an abstract machine Most useful for specifying implementations This is what we will use for Cool (sec 13 of Cool manual) 6

Other Kinds of Semantics Denotational semantics The meaning of a program is expressed as a mathematical object Elegant but quite complicated Axiomatic semantics Useful for checking that programs satisfy certain correctness properties e.g., that the quick sort function sorts an array The foundation of many program verification systems 7

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) 8

Example of Inference Rule for Operational Semantics Example: Context e1 : 5 Context e2 : 7 Context e1 + e2 : 12 In general the result of evaluating an expression depends on the result of evaluating its subexpressions The logical rules specify everything that is needed to evaluate an expression 9

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 10

Variable Environments A variable environment is a map from variable names to locations Tells in what memory location the value of a variable is stored Keeps track of which variables are in scope Example: E = [a : l1, b : l2] To lookup a variable a in environment E, we write E(a) 11

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 12

Cool Values All values in Cool are objects All objects are instances of some class (the dynamic type of the object) To denote a Cool object we use the notation X(a1 = l1,, an = ln) where X is the dynamic type of the object a i are the attributes (including those inherited) l i are the locations where the values of attributes are stored 13

Cool Values Special cases (classes without attributes) Int(5) the integer 5 Bool(true) the boolean true String(4, Cool ) the string Cool of length 4 There is a special value void that is a member of all types No operations can be performed on it Except for the test isvoid (e.g., isvoid expr) Concrete implementations might use NULL here 14

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 15

Notes The result of evaluating an expression is a value and a new store The store changes model the side-effects The variable environment does not change Nor does the value of self The operational semantics allows for nonterminating evaluations We define one rule for each kind of expression 16

Operational Semantics for Base Values No side effects in these cases (the store does not change) so, E, S true : Bool(true), S so, E, S false : Bool(false), S i is an integer literal so, E, S i : Int(i), S s is a string literal n is the length of s so, E, S s : String(n,s), S 17

Operational Semantics of Variable References E(id) = lid S(lid) = v so, E, so, E, S id : v, S Note the double lookup of variables First from name to location Then from location to value The store does not change A special case: so, E, S self : so, S 18

Operational Semantics of Assignment so, E, S e : v, S1 E(id) = lid S2 = S1[v/lid] so, E, S id e : v, S2 A three step process Evaluate the right hand side a value and a new store S1 Fetch the location of the assigned variable The result is the value v and an updated store The environment does not change 19

Operational Semantics of Conditionals so, E, S e1: Bool(true), S1 so, E, S1 e2: v, S2 so, E, S if e1 then e2 else e3 : v, S2 The threading of the store enforces an evaluation sequence e 1 must be evaluated first to produce S1 Then e 2 can be evaluated The result of evaluating e 1 is a boolean object The typing rules ensure this 20

Operational Semantics of Sequences so, E, S e1: v1, S1 so, E, S1 e2: v2, S2 so, E, Sn-1 en: vn, Sn so, E, S { e1; ; en; } : vn, Sn Again the threading of the store expresses the intended evaluation sequence Only the last value is used But all the side-effects (updated stores) are collected 21

Operational Semantics of while(i) so, E, S e1 : Bool(false), S1 so, E, S while e1 loop e2 pool : void, S1 If e1 evaluates to Bool(false) then the loop terminates immediately With the side-effects from the evaluation of e1 And with result value void The typing rules ensure that e1 evaluates to a boolean object 22

Operational Semantics of while(ii) so, E, S e1 : Bool(true), S1 so, E, S1 e2: v, S2 so, E, S2 while e1 loop e2 pool : void, S3 so, E, S while e1 loop e2 pool : void, S3 Note the sequencing (S S1 S2 S3) Note how looping is expressed Evaluation of while is expressed in terms of the evaluation of itself in another state The result of evaluating e2 is discarded Only the side-effect (updated store) is preserved 23

Operational Semantics of let Expressions (I) so, E, S e1: v1, S1 so,?,? e2: v2, S2 so, E, S let id : T e1 in e2: v2, S2 What is the context in which e 2 must be evaluated? Environment like E but with a new binding of id to a fresh location lnew Store like S 1 but with lnew mapped to v1 24

Operational Semantics of let Expressions (II) We write l new = newloc(s) to say that lnew is a location that is not already used in S Think of newloc as the dynamic memory allocation function malloc in C. The operational rule for let: so, E, S e1: v1, S1 lnew = newloc(s1) so, E[lnew/id], S1[v1/lnew] e2: v2, S2 so, E, S let id : T e1 in e2: v2, S2 25

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 26

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) 27

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 28

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 29

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 30

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 31

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 32

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 33

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 34

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) 35

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 36

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 37

Finish Project 3! 38