Lecture 013

Semantics

Semantics: what is the actual desired behavior of the code

You can specify the semantics of a language by saying whatever is executed in the byte level is the expected behavior of the code. However, it does not enable code optimization since optimization might sometimes change small details in the exact behavior. So we need to distinguish defined behavior (we guarantee) with undefined behavior (might be optimized out)

If we were to use stack machine to define code behavior: - we might care whether to use stack machine - we might care which way stack grows - we might care how integers are represented - we might care particular instruction set of the architecture

These are too irelevant for an ordinary programmer.

Operational Semantics: describe program evaluation via execution rules on an abstract machine. It is useful for specifying implementations.

Denotational Semantics: program's text is mapped to a function in mathematical sense.

Axiomatic Semantics: define the behavior as @require and @ensure. Foundation of many program verification systems.

Operational Semantics

In operational semantics

We also define a notation on how we update the store: we write S' = S[12/l_1] to update the value at location l_1 to 12. So we guarantee S'(l_1) = 12 \land (\forall l \neq l_1)(S'(l) = S(l))

We also need a way to represent objects: X(a_1 = l_1, ..., a_n = l_n):

Special objects without attributes:

self, E, S \vdash e : v, S'

The above is an evaluation judgement: if evaluation of e terminates then the value of e is v and the new store is S'

Notice that the self object and E is not modified. The only side effect of evaluation is the value and updated S'.

Also the expression only guarantee to hold when the evaluation actually terminates.

Evaluation Rules

Example Evaluation Rules for Simple Objects

Example Evaluation Rules for Simple Objects

Evaluation of Identifier

Evaluation of Identifier

Evaluation of Self

Evaluation of Self

Note that in above example, we only evaluate by look up, we are not changing any value.

Evaluation of Assignment Expression

Evaluation of Assignment Expression

Evaluation of Addition Expression

Evaluation of Addition Expression

Evaluation of Expression Block: note that only the last value appear

Evaluation of Expression Block: note that only the last value appear

Note that the order of evaluation matters.

Evaluation of If-Else

Evaluation of If-Else

Evaluation of While Loop (false)

Evaluation of While Loop (false)

Evaluation of While Loop (true)

Evaluation of While Loop (true)

When we wan to allocate new variables to our environment and store, we need to define a new instruction newloc() that allocate an return a new location.

Evaluation of Let Expression

Evaluation of Let Expression

Allocation Evaluations

Default Value: for each class A there is a default value D_A

For class A, we write: class(A) = (a_1 : T_1 \leftarrow e_1, ..., a_n : T_n \leftarrow e_n)

Allocation Evaluation

Allocation Evaluation

Note that when initializers are evaluated (and typing), only attributes are in scope.

Method Call Evaluation

Method Call Evaluation

Some error are not caught by type checker can happen run-time:

Most language does not have a well specified operational semantics. When portability (heterogeneous device) is important, operational semantics is essential.

Table of Content