Compiler Designing W07L1304

COOL Semantics II

  • The declared object for a class C would return list of argument with their types and values.
 Class(C)={a1 : T1 ← e1, a2 : T2 ← e2... an : Tn ← en}

If class C is inherited from other class, then the list of the attributes generated would in same order as they are in memory, i.e. first come the superclass’s attributes then the attributes of class.

These are the rules for creating a new object of any class:

gif (1)

See the self object is the allocated object, first three lines are for allocating memory to the class object and remaining for initializing it. Defaults are given to values initially and see that all the attributes are in scope.

  • Discussion about the informal semantics of e0.f(a1,a2…an)
    • Evaluation of the arguments.
    • evaluate target object e0 and let X is the dynamic type of the target object e0
    • Fetch from X the definition of f with n arguments.
    • create n new locations and make environment mapping from attributes to these new locations.
    • set self to target object and evaluate f’s body.
  • impl(A,f)={args, f_body} –lookup in memory of function
  • Example of above @14:00 min, in the example, E have the formal arguments and self attributes. S contains the locations of attributes and formal arguments.

[II]

  • Operational semantics do not cover all the cases.
  • When function impl(A, f) is used, there is not this case of checking that if f exists in A or not that work is already been done by type checker. But there are some error that type checker cannot prevent:
    • Heap overflow
    • string out of range
    • division by zero
    • dispatch on void
  • Operational semantics are good if portability is expected.

Compiler Designing W07L1303

COOL Semantics I

  • Some judgement rules are given:

gif

  • CodeCogsEqn (3)In the case above, store is not change as there is no side-effect of evaluating the expression.
  • if id=self then v=so.
  • Judgement rules where value of store changes due to the side-effect of expressions.

CodeCogsEqn (4)

CodeCogsEqn (5)CodeCogsEqn (6)

  • Example: addition, 8:06 minute: if then else 15 minute: while loop 15:53 minute: let 17:40 minute.
  • Let judgement rule: to change E newloc(S) is used.CodeCogsEqn (7)

Compiler Designing W07L1302

Operational Semantics

  • It has similar rules as type checking:
For type checking, the rules are defined like:
     Context ⊦ e : C
          means the under some conetext e has type C.
Similarly, in evaluation rules 
     Context ⊦ e : v
          means that under some context e evaluates v.

For example,

CodeCogsEqn (2)

  • There are two function to track variables and their values:
    • environment(variable) = memory location of the variable; it stores the variables which are in scope E=[a:loc1, b:loc2].
    • store(memory address) = value at that address. S = [loc1→ 5, loc2 → 7]; to update the value at some location: S’=S[12/loc1].
  • An class object is stored as X(a1=l1, a2=l2 … an=ln) where X is the object and ai and li are attribute and location respectively.
  • Classes not having any attributes like Int(2), Bool(true), void.
  • Evaluations judgement is:
so, E, S ⊦ e:v, S'   //so:self object; E:environment; S:Store; S':new store; e:expression;
                     //v:value of the expression
  • After evaluation so and E do not change but S may change.
  • The judgement is only valid when e terminates.

Compiler Designing W07L1301

Semantics Overview

  • When the programming language was being defined, these tools are used:
    • Tokens for lexical analysis
    • Context free grammar for parsing
    • Type checking for semantic analysis
    • The evaluation rules for code generation and optimization
  • But the code generation is inefficient. Assembly language code generation code contains irrelevant details. So a complete descriptive and less restrictive language is needed.
  • Operational semantic is used to generate code instead of assembly language. It descibes program evaluation via execution rules.
  • Other ways can be:
    • Denotational semantics: Here the program is mapped into a mathematical sense. Although its an elegant way but poses complexity.
    • Axiomatic semantics: It describes program via logical rules. Like if execution of the program begins with satisfying X then it will end with Y.