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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Definition 457 An ASM program VM is called a virtual machine for L(VM), iff VM inputs a<br />

L(VM) program (as data) and runs it on REMA.<br />

Plan: Instead of building a compiler for SML to ASM, build a virtual machine VM for REMA<br />

and a compiler from SML to L(VM). (simpler and more transparent)<br />

c○: Michael Kohlhase 281<br />

The main difference between the register machine REMA and the virtual machine VM construct<br />

is the way it organizes its memory. The REMA gives the assembler language full access to its<br />

internal registers and the data store, which is convenient for direct programming, but not suitable<br />

for a language that is mainly intended as a compilation target for higher-level languages which<br />

have regular (tree-like) structures. The virtual machine VM builds on the realization that tree-like<br />

structures are best supported by stack-like memory organization.<br />

A Virtual Machine for Functional Programming<br />

We will build a stack-based virtual machine; this will have four components<br />

Stack Program Store<br />

Command Interpreter<br />

VPC<br />

The stack is a memory segment operated as a “last-in-first-out” LIFO sequence<br />

The program store is a memory segment interpreted as a sequence of instructions<br />

The command interpreter is a ASM program that interprets commands from the program<br />

store and operates on the stack.<br />

The virtual program counter (VPC) is a register that acts as a the pointer to the current<br />

instruction in the program store.<br />

The virtual machine starts with the empty stack and VPC at the beginning of the program.<br />

A Stack-based Programming Language<br />

c○: Michael Kohlhase 282<br />

Now we are in a situation, where we can introduce a programming language for VM. The main<br />

difference to ASM is that the commands obtain their arguments by popping them from the stack<br />

(as opposed to the accumulator or the ASM instructions) and return them by pushing them to the<br />

stack (as opposed to just leaving them in the registers).<br />

A Stack-Based VM language (Arithmetic Commands)<br />

Definition 458 VM Arithmetic Commands act on the stack<br />

instruction effect VPC<br />

con i pushes i onto stack VPC: = VPC + 2<br />

add pop x, pop y, push x + y VPC: = VPC + 1<br />

sub pop x, pop y, push x − y VPC: = VPC + 1<br />

mul pop x, pop y, push x · y VPC: = VPC + 1<br />

leq pop x, pop y, if x ≤ y push 1, else push 0 VPC: = VPC + 1<br />

Example 459 The L(VM) program “con 4 con 7 add” pushes 7 + 4 = 11 to the stack.<br />

158

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

Saved successfully!

Ooh no, something went wrong!