15.08.2013 Views

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

eturn takes the current frame from the stack, jumps to previous program address.<br />

(which is cached in the frame)<br />

call and return jointly have the effect of replacing the arguments by the result of the<br />

procedure.<br />

c○: Michael Kohlhase 311<br />

We will now extend our implementation of the virtual machine by the new instructions. The<br />

central idea is that we have to realize call frames on the stack, so that they can be used to store<br />

the data for managing the recursion.<br />

Realizing Call Frames on the Stack<br />

Problem: How do we know<br />

what the current frame is?<br />

(after all, return has to pop it)<br />

Idea: Maintain another register: the<br />

frame pointer (FP), and cache information<br />

about the previous frame<br />

and the number of arguments in the<br />

frame.<br />

frame pointer<br />

return address 0<br />

previous frame<br />

argument number<br />

first argument -1<br />

last argument -n<br />

Add two internal cells to the frame, that are hidden to the outside. The upper one is called<br />

the anchor cell.<br />

In the anchor cell we store the stack address of the anchor cell of the previous frame.<br />

The frame pointer points to the anchor cell of the uppermost frame.<br />

c○: Michael Kohlhase 312<br />

With this memory architecture realizing the four new commands is relatively straightforward.<br />

Realizing proc<br />

proc a l jumps over the procedure with the help of the length l of the procedure.<br />

label instruction effect comment<br />

〈proc〉 MOVE IN1 ACC ACC : = VPC<br />

STORE 0 P (0): = ACC cache VPC<br />

LOADIN 1 2 ACC : = P (VPC + 2) load length<br />

ADD 0 ACC : = ACC +P (0) compute new VPC value<br />

MOVE ACC IN1 IN1: = ACC update VPC<br />

JUMP 〈jt〉 jump back<br />

c○: Michael Kohlhase 313<br />

178

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!