To sketch an operational semantic definition for a simple imperative language as a virtual machine interpreter.
This is the same as the language introduced in class. Its abstract syntax is:
P ::= S
S ::= I = E | S1;S2 | if B then S1 else S2 end | while B do S end
E ::= N | I | E1 + E2 | E1 – E2
B ::= true | false | I == E | I > E | I < E | B1 or B2 | B1 and B2 | not B
N ::= … -2 | -1 | 0 | 1 | 2 …
I ::= [any identifier]
All of the operators used are intended to have their usual meaning.
The necessary components are a virtual machine (which really means defining the “machine code” of the machine, and the structures they operate on), and the interpreter which runs on that machine. For this assignment we will target a very simple machine with the following instruction set.
M is the (single) memory, organized as a sequence of locations, indexed (like an array) by an integer starting at zero, where each location can contain an integer. n is any constant integer; i, j, k are taken from the range 0..max where max is some large but finite positive integer; l, m are labels taken from the same range of positive integers. Every instruction has a label where the next instruction has a label one higher than the previous one. The first label is 1.
l: M[i] = n // put a constant in location i
l: M[i] = M[j] // copy contents of location j into location i
l: M[i] = M[M[j]] // indirect copy where contents of M[j] is used as index on RHS
l: M[M[j]] = M[i] // indirect copy with index on LHS
l: M[i] = M[j] + M[k] // addition
l: M[i] = M[j] – M[k] // subtraction
l: if M[i] > 0 goto m // test greater than zero and branch
l: if M[i] = 0 goto m // test equal to zero and branch
l: goto m // unconditional branch
l: gosub m // branch to label m and store label l+1 for return
l: return // branch to stored label
l: read M[i] // copy value from input stream to location
l: write M[i] // copy value from location to output stream
l: halt // stop the machine
Even for a simple language, given the nature of the VM’s instruction set, the interpreter would be very large. However, several issues arise when defining the interpreter. Give as much detail as you think is necessary to answer these questions for the following program:
p = 0
n = x
while n > 0 do
p = p + y
n = n – 1
end
This assignment is worth 50 points. Questions 1, 2 and 4 are worth 10 points each, and Questions 3 is worth 20 points each.
This assignment must be submitted through WebCT, so it must be prepared electronically. Read the drop box information page before submitting it. Pay close attention to the availability “window” of the assignment. When the window closes, you will not be able to submit an answer.