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.

P instruction comment P instruction comment<br />

0 LOAD 1 ACC : = a 10 MOVE ACC IN1 IN1: = IN1 +1<br />

1 MOVE ACC IN1 IN1: = a 11 MOVE IN2 ACC<br />

2 LOAD 2 ACC : = b 12 ADDI 1<br />

3 MOVE ACC IN2 IN2: = b 13 MOVE ACC IN2 IN2: = IN2 +1<br />

4 LOAD 0 ACC : = n 14 LOAD 0<br />

5 JUMP= 13 if n = 0 then stop 15 SUBI 1<br />

6 LOADIN 1 0 ACC : = P (IN1) 16 STORE 0 P (0): = P (0) − 1<br />

7 STOREIN 2 0 P (IN2): = ACC 17 JUMP − 12 goto step 5<br />

8 MOVE IN1 ACC 18 STOP 0 Stop<br />

9 ADDI 1<br />

Lemma 454 We have P (0) = n − (i − 1), IN1 = a + i − 1, and IN2 = b + i − 1 for all<br />

(1 ≤ i ≤ n + 1). (the program does what we want)<br />

proof by induction on n.<br />

Definition 455 The induction hypotheses are called loop invariants.<br />

c○: Michael Kohlhase 280<br />

3.4.2 A Stack-based Virtual Machine<br />

We have seen that our register machine runs programs written in assembler, a simple machine<br />

language expressed in two-word instructions. Machine languages should be designed such that on<br />

the processors that can be built machine language programs can execute efficiently. On the other<br />

hand machine languages should be built, so that programs in a variety of high-level programming<br />

languages can be transformed automatically (i.e. compiled) into efficient machine programs. We<br />

have seen that our assembler language ASM is a serviceable, if frugal approximation of the first<br />

goal for very simple processors. We will (eventually) show that it also satisfies the second goal by<br />

exhibiting a compiler for a simple SML-like language.<br />

In the last 20 years, the machine languages for state-of-the art processors have hardly changed.<br />

This stability was a precondition for the enormous increase of computing power we have witnessed<br />

during this time. At the same time, high-level programming languages have developed considerably,<br />

and with them, their needs for features in machine-languages. This leads to a significant<br />

mismatch, which has been bridged by the concept of a virtual machine.<br />

Definition 456 A virtual machine is a simple machine-language program that interprets a slightly<br />

higher-level program — the “byte code” — and simulates it on the existing processor.<br />

Byte code is still considered a machine language, just that it is realized via software on a real<br />

computer, instead of running directly on the machine. This allows to keep the compilers simple<br />

while only paying a small price in efficiency.<br />

In our compiler, we will take this approach, we will first build a simple virtual machine (an ASM<br />

program) and then build a compiler that translates functional programs into byte code.<br />

Virtual Machines<br />

Question: How to run high-level programming languages (like SML) on REMA?<br />

Answer: By providing a compiler, i.e. an ASM program that reads SML programs (as data)<br />

and transforms them into ASM programs.<br />

But: ASM is optimized for building simple, efficient processors, not as a translation target!<br />

Idea: Build an ASM program VM that interprets a better translation target language<br />

(interpret REMA+VM as a “virtual machine”)<br />

157

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

Saved successfully!

Ooh no, something went wrong!