Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Names and Bindings.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
ISBN Chapter 3 Describing Syntax and Semantics.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
CS 355 – Programming Languages
Compiler Construction
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Foundations of Programming Languages: Introduction to Lambda Calculus
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CS 536 Spring Run-time organization Lecture 19.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Describing Syntax and Semantics
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Prof. Necula CS 164 Lectures Semantic Analysis Typechecking in COOL Lecture 7.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Names Variables Type Checking Strong Typing Type Compatibility 1.
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
Compiler Construction
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CS 363 Comparative Programming Languages Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Looping and Counting Lecture 3 Hartmut Kaiser
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
CMSC 330: Organization of Programming Languages Operational Semantics.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Operational Semantics of Scheme
Compiler Construction (CS-636)
CS 326 Programming Languages, Concepts and Implementation
Run-time organization
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
Presentation transcript:

Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10

Prof. Necula CS 164 Lecture 172 Lecture Outline COOL operational semantics Motivation Notation The rules

Prof. Necula CS 164 Lecture 173 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

Prof. Necula CS 164 Lecture 174 Evaluation Rules So Far So far, we specified the evaluation rules indirectly –We specified the compilation of Cool to a stack machine –And we specified the evaluation rules of the stack machine This is a complete description Why isn’t it good enough?

Prof. Necula CS 164 Lecture 175 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 –The particular instruction set of the architecture We need a complete but not overly restrictive specification

Prof. Necula CS 164 Lecture 176 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

Prof. Necula CS 164 Lecture 177 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

Prof. Necula CS 164 Lecture 178 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)

Prof. Necula CS 164 Lecture 179 Example of Inference Rule for Operational Semantics Example: 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 Context ` e 1 : 5 Context ` e 2 : 7 Context ` e 1 + e 2 : 12

Prof. Necula CS 164 Lecture 1710 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

Prof. Necula CS 164 Lecture 1711 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)

Prof. Necula CS 164 Lecture 1712 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 1 2 to location l 1 we write S[1 2 /l 1 ] –This denotes a store S’ such that S’(l 1 ) = 1 2 and S’(l) = S(l) if l  l 1

Prof. Necula CS 164 Lecture 1713 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

Prof. Necula CS 164 Lecture 1714 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 that is a member of all types –No operations can be performed on it –Except for the test isvoid –Concrete implementations might use NULL here

Prof. Necula CS 164 Lecture 1715 Operational Rules of Cool The evaluation judgment is so, E, S ` e : v, S’ read: –Given so the current value of the self object –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’

Prof. Necula CS 164 Lecture 1716 Notes The “result” of evaluating an expression is a value and a new store Changes to the store model the side-effects The variable environment does not change Nor does the value of “self” The operational semantics allows for non- terminating evaluations We define one rule for each kind of expression

Prof. Necula CS 164 Lecture 1717 Operational Semantics for Base Values No side effects in these cases (the store does not change) i is an integer literal so, E, S ` i : Int(i), S so, E, S ` true : Bool(true), Sso, E, S ` false : Bool(false), S s is a string literal n is the length of s so, E, S ` s : String(n,s), S

Prof. Necula CS 164 Lecture 1718 Operational Semantics of Variable References Note the double lookup of variables –First from name to location –Then from location to value The store does not change A special case: E(id) = l id S(l id ) = v so, E, S ` id : v, S so, E, S ` self : so, S

Prof. Necula CS 164 Lecture 1719 Operational Semantics of Assignment 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 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

Prof. Necula CS 164 Lecture 1720 Operational Semantics of Conditionals 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 –There is another, similar, rule for Bool(false) 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

Prof. Necula CS 164 Lecture 1721 Operational Semantics of Sequences Again the threading of the store expresses the intended evaluation sequence Only the last value is used But all the side-effects are collected so, E, S ` e 1 : v 1, S 1 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

Prof. Necula CS 164 Lecture 1722 Operational Semantics of while (I) 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 so, E, S ` e 1 : Bool(false), S 1 so, E, S ` while e 1 loop e 2 pool : void, S 1

Prof. Necula CS 164 Lecture 1723 Operational Semantics of while (II) 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 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

Prof. Necula CS 164 Lecture 1724 Operational Semantics of let Expressions (I) 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 so, E, S ` e 1 : v 1, S 1 so, ?, ? ` e 2 : v, S 2 so, E, S ` let id : T  e 1 in e 2 : v 2, S 2

Prof. Necula CS 164 Lecture 1725 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: so, E, S ` e 1 : v 1, S 1 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

Prof. Necula CS 164 Lecture 1726 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

Prof. Necula CS 164 Lecture 1727 Default Values 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)

Prof. Necula CS 164 Lecture 1728 More Notation 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

Prof. Necula CS 164 Lecture 1729 Operational Semantics of new 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

Prof. Necula CS 164 Lecture 1730 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

Prof. Necula CS 164 Lecture 1731 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

Prof. Necula CS 164 Lecture 1732 More Notation 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

Prof. Necula CS 164 Lecture 1733 Operational Semantics of Dispatch so, E, S ` e 1 : v 1, S 1 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

Prof. Necula CS 164 Lecture 1734 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

Prof. Necula CS 164 Lecture 1735 Runtime Errors Operational rules do not cover all cases Consider for example the rule for dispatch: What happens if impl(X, f) is not defined? Cannot happen in a well-typed program (Type safety theorem) … 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

Prof. Necula CS 164 Lecture 1736 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 segfault

Prof. Necula CS 164 Lecture 1737 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