other kinds of semantics introduction to operational
play

Other Kinds of Semantics Introduction to Operational Semantics - PDF document

Lecture Outline COOL operational semantics Operational Semantics of Cool Motivation Notation Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) The rules CS781(Prasad) L24CG 2 CS781(Prasad) L24CG 1


  1. Lecture Outline • COOL operational semantics Operational Semantics of Cool – Motivation – Notation Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) – The rules CS781(Prasad) L24CG 2 CS781(Prasad) L24CG 1 Motivation Evaluation Rules So Far • We must specify for every Cool expression • So far, we specified the evaluation rules what happens when it is evaluated indirectly – This is the “meaning” of an expression – We specified the compilation of Cool to a stack machine • The definition of a programming language: – We specified the evaluation rules of the stack – The tokens ⇒ lexical analysis machine – The grammar ⇒ syntactic analysis – The typing rules ⇒ semantic analysis • This is a complete description – The evaluation rules • Why isn’t it good enough? ⇒ code generation and optimization CS781(Prasad) L24CG 3 CS781(Prasad) L24CG 4 Assembly Language Description of Semantics Programming Language Semantics • Assembly-language descriptions of language • There are many ways to specify programming implementation have too much “irrelevant” language semantics details – They are all equivalent but some are more suitable – Whether to use a stack machine or not to various tasks than others – Which way the stack grows • Operational semantics – How integers are represented on a particular – Describes the evaluation of programs on an machine abstract machine – The particular instruction set of the architecture – Most useful for specifying implementations • We need a complete but not overly restrictive – This is what we will use for Cool specification CS781(Prasad) L24CG 5 CS781(Prasad) L24CG 6 �

  2. Other Kinds of Semantics Introduction to Operational Semantics • Denotational semantics • Once, again we introduce a formal notation – The meaning of a program is expressed as a – Using logical rules of inference, just like for typing mathematical object • Recall the typing judgment – Elegant but quite complicated Context ฀ e : C • Axiomatic semantics (in the given context, expression e has type C) – Useful for checking that programs satisfy certain correctness properties • We try something similar for evaluation • e.g., that the quick sort function terminates with a sorted Context ฀ e : v array (in the given context, expression e evaluates to – The foundation of many program verification value v) systems CS781(Prasad) L24CG 7 CS781(Prasad) L24CG 8 Example of Inference Rule for Operational What Contexts Are Needed? Semantics • Example: • Contexts are needed to handle variables Context ฀ e 1 : 5 • Consider the evaluation of y ← x + 1 Context ฀ e 2 : 7 – We need to keep track of values of variables Context ฀ e 1 + e 2 : 12 – We need to allow variables to change their values • In general, the result of evaluating an during the evaluation expression depends on the result of evaluating • We track variables and their values with: its sub-expressions – An environment : tells us at what address in • The logical rules specify everything that is memory is the value of a variable stored needed to evaluate an expression – A store : tells us what is the contents of a memory location CS781(Prasad) L24CG 9 CS781(Prasad) L24CG 10 Variable Environments Stores • A variable environment is a map from variable • A store maps memory locations to values names to locations • Example: – Tells in what memory location the value of a S = [l 1 → 5, l 2 → 7] variable is stored • To lookup the contents of a location l 1 in store – Keeps track of which variables are in scope S we write S(l 1 ) • Example: • To perform an assignment of 12 to location l 1 E = [a : l 1 , b : l 2 ] we write S[12/l 1 ] • To lookup a variable a in environment E we – This denotes a store S’ such that write E(a) S’(l 1 ) = 12 and S’(l) = S(l) if l ≠ l 1 CS781(Prasad) L24CG 11 CS781(Prasad) L24CG 12 �

  3. Cool Values Cool Values (Cont.) • All values in Cool are objects • Special cases (classes without attributes) – All objects are instances of some class (the Int(5) the integer 5 dynamic type of the object) Bool(true) the boolean true • To denote a Cool object, we use the notation String(4, “Cool”) the string “Cool” of length 4 X(a 1 = l 1 , …, a n = l n ) where • There is a special value void of type Object – X is the dynamic type of the object – No operations can be performed on it – a i are the attributes (including those inherited) – Except for the test isvoid – l i are the locations where the values of attributes – Concrete implementations might use NULL here are stored CS781(Prasad) L24CG 13 CS781(Prasad) L24CG 14 Operational Rules of Cool Notes • The evaluation judgment is • The “result” of evaluating an expression is a value and a new store so, E, S ฀ e : v, S’ • The store changes model the side-effects read: • The variable environment does not change – Given so the current value of self – And E the current variable environment • Nor does the value of “self” – And S the current store • The operational semantics allows for non- – If the evaluation of e terminates then terminating evaluations – The return value is v • We define one rule for each kind of – And the new store is S’ expression CS781(Prasad) L24CG 15 CS781(Prasad) L24CG 16 Operational Semantics for Base Values Operational Semantics of Variable References E(id) = l id S(l id ) = v so, E, S ฀ true : Bool(true), S so, E, S ฀ false : Bool(false), S so, E, S ฀ id : v, S s is a string literal • Note the double lookup of variables i is an integer literal n is the length of s – First from name to location so, E, S ฀ i : Int(i), S so, E, S ฀ s : String(n,s), S – Then from location to value • No side effects in these cases • The store does not change (the store does not change) • A special case: so, E, S ฀ self : so, S CS781(Prasad) L24CG 17 CS781(Prasad) L24CG 18 �

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend