Lecture Outline: Prof. Aiken CS 143 Lecture 13 1 Prof. Aiken CS 143 Lecture 13 2
Lecture Outline: Prof. Aiken CS 143 Lecture 13 1 Prof. Aiken CS 143 Lecture 13 2
Lecture Outline: Prof. Aiken CS 143 Lecture 13 1 Prof. Aiken CS 143 Lecture 13 2
Lecture 13 • Notation
• The rules
• We must specify for every Cool expression • We have specified evaluation rules indirectly
what happens when it is evaluated – The compilation of Cool to a stack machine
– This is the “meaning” of an expression – The evaluation rules of the stack machine
1
Other Kinds of Semantics Introduction to Operational Semantics
• Denotational semantics
– Program’s meaning is a mathematical function • Once again we introduce a formal notation
– Elegant, but introduces complications
• Need to define a suitable space of functions
• Logical rules of inference, as in type checking
• Axiomatic semantics
– Program behavior described via logical formulae
• If execution begins in state satisfying X, then it ends in
state satisfying Y
• X, Y formulas
– Foundation of many program verification systems
2
Stores Cool Values
s is a string literal
• Some things don’t change i is an integer literal n is the length of s
– The variable environment so, E, S i : Int(i), S so, E, S s : String(n,s), S
– The value of self
– The operational semantics allows for non- • No side effects in these cases
terminating evaluations (the store does not change)
3
Operational Semantics of Variable References Operational Semantics for Self
E(id) = lid
S(lid) = v
so, E, S id : v, S • A special case:
4
Operational Semantics of while (II) Operational Semantics of let Expressions (I)
5
Operational Semantics of new Notes on Operational Semantics of new.
• new SELF_TYPE allocates an object with the • The first three steps allocate the object
same dynamic type as self
• The remaining steps initialize it
T0 = if (T == SELF_TYPE and so = X(…)) then X else T
class(T0) = (a1 : T1 e1,…, an : Tn en) – By evaluating a sequence of assignments
li = newloc(S) for i = 1,…,n
v = T0(a1= l1,…,an= ln)
S1 = S[DT1/l 1,…,DTn/ln] • State in which the initializers are evaluated
E’ = [a1 : l1, …, an : ln] – Self is the current object
v, E’, S1 { a1 e1; …; an en; } : vn, S2
– Only the attributes are in scope (same as in typing)
so, E, S new T : v, S2 – Initial values of attributes are the defaults
6
Runtime Errors Runtime Errors (Cont.)
Operational rules do not cover all cases • There are some runtime errors that the type
Consider the dispatch example: checker does not prevent
… – A dispatch on void
so, E, Sn e0 : v0,Sn+1
– Division by zero
v0 = X(a1 = l 1,…, am = lm)
impl(X, f) = (x1,…, xn, ebody) – Substring out of range
… – Heap overflow
so, E, S e0.f(e1,…,en) : v, Sn+3
What happens if impl(X, f) is not defined? • In such cases execution must abort gracefully
Cannot happen in a well-typed program – With an error message, not with segfault
Conclusions