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

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

Operational Semantics of Cool

Operational Semantics. One-Slide Summary. Lecture Outline

Operational Semantics of Cool

Compilers. Cool Semantics II. Alex Aiken

Operational Semantics

Operational Semantics of Cool

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

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

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

Type Checking in COOL (II) Lecture 10

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

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

CoolAid: The Cool Reference Manual

1 Lexical Considerations

Lexical Considerations

The code generator must statically assign a location in the AR for each temporary add $a0 $t1 $a0 ; $a0 = e 1 + e 2 addiu $sp $sp 4 ; adjust $sp (!

Semantic Analysis. Lecture 9. February 7, 2018

Lexical Considerations

The Cool Reference Manual

More Static Semantics

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

Overview of Semantic Analysis. Lecture 9

Passing Out Review Forms

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

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Functional Programming. Pure Functional Programming

CS558 Programming Languages

CS558 Programming Languages. Winter 2013 Lecture 3

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

Semantic Analysis and Type Checking

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

Programming Languages Third Edition

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

Principles of Programming Languages

CSCE 314 Programming Languages. Type System

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

COS 320. Compiling Techniques

The role of semantic analysis in a compiler

type environment updated subtype sound

Syntax-Directed Translation. Lecture 14

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

Compilers and computer architecture: A realistic compiler to MIPS

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

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

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

More Static Semantics

Written Assignment 5 Due??

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

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

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

Lecture 15 CIS 341: COMPILERS

IC Language Specification

Mutable References. Chapter 1

(Not Quite) Minijava

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

CMSC 330: Organization of Programming Languages

Syntax and Grammars 1 / 21

Simply-Typed Lambda Calculus

Lectures 20, 21: Axiomatic Semantics

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

1 Introduction. 3 Syntax

G Programming Languages - Fall 2012

CMSC 330: Organization of Programming Languages. Operational Semantics

Application: Programming Language Semantics

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Programming Language Concepts, cs2104 Lecture 04 ( )

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

Code Generation & Parameter Passing

Programmiersprachen (Programming Languages)

The SPL Programming Language Reference Manual

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Formal Semantics of Programming Languages

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

Solutions to Written Assignment 4

Contents. Chapter 1 SPECIFYING SYNTAX 1

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

Lecture 3 Notes Arrays

( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

Formal Semantics of Programming Languages

CSE 341: Programming Languages

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

CMSC 330: Organization of Programming Languages

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Midterm II CS164, Spring 2006

Chapter 1 Getting Started

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

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

CSE 431S Final Review. Washington University Spring 2013

Final CSE 131B Spring 2004

Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.

A Tour of the Cool Support Code

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Derived and abstract data types. TDT4205 Lecture 15

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER COMPILERS ANSWERS. Time allowed TWO hours

The Structure of a Syntax-Directed Compiler

Transcription:

Lecture Outline Operational Semantics of Cool COOL operational semantics Motivation Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) Notation The rules CS781(Prasad) L24CG 1 CS781(Prasad) L24CG 2 Motivation We must specify for every Cool expression what happens when it 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 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? CS781(Prasad) L24CG 3 CS781(Prasad) L24CG 4 Assembly Language Description of Semantics Assembly-language descriptions of language implementation have too much irrelevant details Whether to use a stack machine or not Which way the stack grows How integers are represented on a particular machine The particular instruction set of the architecture We need a complete but not overly restrictive specification 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 CS781(Prasad) L24CG 5 CS781(Prasad) L24CG 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 terminates with a sorted array The foundation of many program verification systems Introduction 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) CS781(Prasad) L24CG 7 CS781(Prasad) L24CG 8 Example of Inference Rule for Operational Semantics Example: Context e 1 : 5 Context e 2 : 7 Context e 1 + e 2 : 12 In general, the result of evaluating an expression depends on the result of evaluating its sub-expressions The logical rules specify everything that is needed to evaluate an expression CS781(Prasad) L24CG 9 What Contexts Are Needed? 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 CS781(Prasad) L24CG 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 : l 1, b : l 2 ] To lookup a variable a in environment E we write E(a) CS781(Prasad) L24CG 11 Stores A store maps memory locations to values Example: S = [l 1 5, l 2 7] To lookup the contents of a location l 1 in store S we write S(l 1 ) To perform an assignment of 12 to location l 1 we write S[12/l 1 ] This denotes a store S such that S (l 1 ) = 12 and S (l) = S(l) if l l 1 CS781(Prasad) L24CG 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(a 1 = l 1,, a n = l n ) 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 Cool Values (Cont.) 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 of type Object No operations can be performed on it Except for the test isvoid Concrete implementations might use NULL here CS781(Prasad) L24CG 13 CS781(Prasad) L24CG 14 Operational Rules of Cool The evaluation judgment is so, E, S e : v, S read: Given so the current value of self And E the current variable environment And S the current store If the evaluation of e terminates then The return value is v And the new store is S CS781(Prasad) L24CG 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 CS781(Prasad) L24CG 16 Operational Semantics for Base Values Operational Semantics of Variable References 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 No side effects in these cases (the store does not change) s is a string literal n is the length of s so, E, S s : String(n,s), S E(id) = l id S(l id ) = v 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 CS781(Prasad) L24CG 17 CS781(Prasad) L24CG 18

Operational Semantics of Assignment Example so, E, S e : v, S 1 E(id) = l id S 2 = S 1 [v/l id ] so, E, S id e : v, S 2 A three step process Evaluate the right hand side a value and a new store S 1 Fetch the location of the assigned variable The result is the value v and an updated store The environment does not change CS781(Prasad) L24CG 19 class Main { Int p <- 6; Int q <- p; ENV: p 111000X q 111004X STORE: 111000X Int(6) 111004X Int(6) Copy semantics implicit in this value copying. CS781(Prasad) L24CG 20 Example class C { Int j <- 6; class Main { C p <- new C; C q <- p; ENV: p 111000X q 111004X object mini-env : C:j 111008X STORE: 111000X C(j:111008X) 111004X C(j:111008X) 111008X Int(6) Reference semantics; Stack vs Heap CS781(Prasad) L24CG 21 Operational Semantics of Conditionals so, E, S e 1 : Bool(true), S 1 so, E, S 1 e 2 : v, S 2 so, E, S if e 1 then e 2 else e 3 : v, S 2 The threading of the store enforces an evaluation sequence e 1 must be evaluated first to produce S 1 Then e 2 can be evaluated The result of evaluating e 1 is a boolean object The typing rules ensure this CS781(Prasad) L24CG 22 Operational Semantics of Sequences Operational Semantics of while (I) so, E, S 1 e 2 : v 2, S 2 so, E, S n-1 e n : v n, S n so, E, S { e 1 ; ; e n ; : v n, S n Again the threading of the store expresses the intended evaluation sequence Only the last value is used But all the side-effects are collected CS781(Prasad) L24CG 23 so, E, S e 1 : Bool(false), S 1 so, E, S while e 1 loop e 2 pool : void, S 1 If e 1 evaluates to Bool(false) then the loop terminates immediately With the side-effects from the evaluation of e 1 And with result value void The typing rules ensure that e 1 evaluates to a boolean object CS781(Prasad) L24CG 24

Operational Semantics of while (II) Operational Semantics of let Expressions (I) so, E, S e 1 : Bool(true), S 1 so, E, S 1 e 2 : v, S 2 so, E, S 2 while e 1 loop e 2 pool : void, S 3 so, E, S while e 1 loop e 2 pool : void, S 3 Note the sequencing (S S 1 S 2 S 3 ) Note how looping is expressed Evaluation of while is expressed in terms of the evaluation of itself in another state The result of evaluating e 2 is discarded Only the side-effect is preserved CS781(Prasad) L24CG 25 so,?,? e 2 : v, S 2 so, E, S let id : T e 1 in e 2 : v, S 2 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 l new Store like S 1 but with l new mapped to v 1 CS781(Prasad) L24CG 26 Operational Semantics of let Expressions (II) We write l new = newloc(s) to say that l new is a location that is not already used in S Think of newloc as the dynamic memory allocation function The operational rule for let: l new = newloc(s 1 ) so, E[l new /id], S 1 [v 1 /l new ] e 2 : v 2, S 2 so, E, S let id : T e 1 in e 2 : v 2, S 2 Operational Semantics of new Consider the expression new T Informal semantics Allocate new locations to hold the values for all attributes of an object of class T Essentially, allocate a new object Initialize those locations with the default values of attributes Evaluate the initializers and set the resulting attribute values Return the newly allocated object CS781(Prasad) L24CG 27 CS781(Prasad) L24CG 28 Default Values Operational Semantics of new For each class A, there is a default value denoted by D A D int = Int(0) D bool = Bool(false) D string = String(0, ) D A = void (for another 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 their declared types e i are the initializers Observation: new SELF_TYPE allocates an object with the same dynamic type as self 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 ) E = [a 1 : l 1,, a n : l n ] S 1 = S[D T1 /l 1,,D Tn /l n ] v, E, S 1 { a 1 e 1 ; ; a n e n ; : v n, S 2 so, E, S new T : v, S 2 CS781(Prasad) L24CG 29 CS781(Prasad) L24CG 30

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 Operational Semantics of Method Dispatch Consider the expression e 0.f(e 1,,e n ) Informal semantics: 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 CS781(Prasad) L24CG 31 CS781(Prasad) L24CG 32 More Notation Operational Semantics of Dispatch For a class A and a method f of A (possibly inherited) we write: 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 1 e 2 : v 2, S 2 so, E, S n-1 e n : v n, S n so, E, S n 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 = [x 1 : l x1,, x n : l xn, a 1 : l 1,,a m : l m ] S n+2 = S n+1 [v 1 /l x1,,v n /l xn ] v 0, E, S n+2 e body : v, S n+3 so, E, S e 0.f(e 1,,e n ) : v, S n+3 CS781(Prasad) L24CG 33 CS781(Prasad) L24CG 34 Operational Semantics of Dispatch. Notes. Expression Evaluation Ordering 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 class A { int f(a x) {1 class B extends A { int f(a x) {2 class Main { A a <- new A; a <- f (a); // 1 a <- f (a<-new B); //2 CS781(Prasad) L24CG 35 CS781(Prasad) L24CG 36

Runtime Errors Operational rules do not cover all cases Consider the dispatch example: so, E, S n 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 ) so, E, S e 0.f(e 1,,e n ) : v, S n+3 What happens if impl(x, f) is not defined? Cannot happen in a well-typed program (Type safety theorem) 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 must abort gracefully With an error message, not with segmentation fault CS781(Prasad) L24CG 38 Conclusions Operational rules are very precise Nothing is left unspecified Operational rules contain a lot of details Read them carefully Most languages do not have a well specified operational semantics When portability is important, an operational semantics becomes essential But not always using the notation we used for Cool CS781(Prasad) L24CG 39