The Department of Computer Science & Engineering |
STUART C. SHAPIRO: CSE
305
|
Operational Semantics describes the meaning of some programming language construct by translating it into a low-level language that is the "machine language" for some virtual machine. We will use operational semantics in this course.
Here, I will describe the virtual machine we will use and its programming language, though there may be modifications later in the course.
The Fetch Execute Cycle (the basic machine operation):
When the program is loaded, the address of the first instruction is placed in the PC. Repeat { Fetch instruction at PC; Increment PC; Execute the fetched instruction;} until the executed instructed is Stop;
We will assume that:<program> -> {[<label>:] <statement>} <statement> -> goto <label> | if <boolean expression> goto <label> | <variable> := <expression> | <foreign non-control statement>
<label>
is a symbolic label that unambiguously
stands for the address of the word in RAM where the
<label>
ed <statement>
begins.
goto <label>
is to replace the
contents of the PC by the address <label>
.
if <boolean expression> goto
<label>
is: if <boolean expression>
evaluates to true
, replace the PC by the address
<label>
; else leave the PC unchanged.
<variable>
unambiguously refers to the address
of a word in RAM or the stack;
<expression>
is any valid expression of Java,
with the semantics Java would assign to it.
<
, <=
, =
,
!=
, >=
, and >
; the
boolean operators are and
, or
, and
not
; and the boolean values are true
and
false
.
Example:
Some programming languages have a for loop like,
wherefor i in min .. max {statements}
min
and max
can be any integers such
that min
<= max
,and
whose semantics is
Those programming languages also have a while loop like,i := min loop: statements if i = max goto out i := i + 1 goto loop out:
whose semantics iswhile <boolean expression> {statements}
Question: Can every for loop in this language be replaced by a while loop?loop: if not <boolean expression> goto out statements goto loop out:
What I mean by replacing one control structure by another:
- without duplicating code as in:
i := min; while (i < max) { statements i := i +1; } statements- without using an additional control structure as in:
i := min; done := false; while (i <= max and not done) { statements if (i = max) {done := true} else {i := i +1;} }
<program> -> {[<label>:] <instruction>}
Instruction | Semantics |
---|---|
<variable> |
push the address, <variable> on the stack |
<literal value> |
push the <literal value> on the stack |
fetch |
replace the top of the stack with the value stored in that location |
store |
store the value on the top of the stack into the location on the 2nd element of the stack, and replace both with the value stored |
swap |
switch the positions of the top 2 elements of the stack |
go |
pop the top element of the stack into the PC |
condgo |
if the 2nd element of the stack is true, pop the top element of the stack into the PC; in either case, pop off the top and 2nd elements |
pop | pop the stack |
stop | terminate the program |
<operator> |
replace the top 2 elements of the stack with the value of (2nd
<operator> top) |
Example:
The semantics of C's
x = y + (z = 3);
is
x y fetch z 3 store + store pop
Trace:
Data: | x: 4 | y: 5 | z: 6 | |||||||
---|---|---|---|---|---|---|---|---|---|---|
Program: | x | y | fetch | z | 3 | store | + | store | pop | |
PC: | ^ | |||||||||
Stack Top: | ||||||||||
------------------------------------------------------ | ||||||||||
Data: | x: 4 | y: 5 | z: 6 | |||||||
Program: | x | y | fetch | z | 3 | store | + | store | pop | |
PC: | ^ | |||||||||
Stack Top: | x | |||||||||
------------------------------------------------------ | ||||||||||
Data: | x: 4 | y: 5 | z: 6 | |||||||
Program: | x | y | fetch | z | 3 | store | + | store | pop | |
PC: | ^ | |||||||||
Stack Top: | y | x | ||||||||
------------------------------------------------------ | ||||||||||
Data: | x: 4 | y: 5 | z: 6 | |||||||
Program: | x | y | fetch | z | 3 | store | + | store | pop | |
PC: | ^ | |||||||||
Stack Top: | 5 | x | ||||||||
------------------------------------------------------ | ||||||||||
Data: | x: 4 | y: 5 | z: 6 | |||||||
Program: | x | y | fetch | z | 3 | store | + | store | pop | |
PC: | ^ | |||||||||
Stack Top: | z | 5 | x | |||||||
------------------------------------------------------ | ||||||||||
Data: | x: 4 | y: 5 | z: 6 | |||||||
Program: | x | y | fetch | z | 3 | store | + | store | pop | |
PC: | ^ | |||||||||
Stack Top: | 3 | z | 5 | x | ||||||
------------------------------------------------------ | ||||||||||
Data: | x: 4 | y: 5 | z: 3 | |||||||
Program: | x | y | fetch | z | 3 | store | + | store | pop | |
PC: | ^ | |||||||||
Stack Top: | 3 | 5 | x | |||||||
------------------------------------------------------ | ||||||||||
Data: | x: 4 | y: 5 | z: 3 | |||||||
Program: | x | y | fetch | z | 3 | store | + | store | pop | |
PC: | ^ | |||||||||
Stack Top: | 8 | x | ||||||||
------------------------------------------------------ | ||||||||||
Data: | x: 8 | y: 5 | z: 3 | |||||||
Program: | x | y | fetch | z | 3 | store | + | store | pop | |
PC: | ^ | |||||||||
Stack Top: | 8 | |||||||||
------------------------------------------------------ | ||||||||||
Data: | x: 8 | y: 5 | z: 3 | |||||||
Program: | x | y | fetch | z | 3 | store | + | store | pop | |
PC: | ^ | |||||||||
Stack Top: |