Where we are at:

Human Abstract design Software Virtual Machine abstract interface Thought Chapters 9, 12 hierarchy Part I: Stack Arithmetic H.L. Language & abstract interface Chapters 10 - 11 Operating Sys. Virtual VM abstract interface Machine Chapters 7 - 8

Assembler

Chapter 6

abstract interface Building a Modern Computer From First Principles Machine Architecture abstract interface Language Chapters 4 - 5 www.nand2tetris.org Hardware Gate Logic abstract interface Platform Chapters 1 - 3 Electrical Chips & Engineering Hardware Physics hierarchy Logic Gates

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 1 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 2

Motivation Compilation models

Jack code (example) Hack code direct compilation: 2-tier compilation: 0000000000010000 class Main { 1110111111001000 language 1 language 2 . . . language n language 1 language 2 . . . language n static int x; Our ultimate goal: 0000000000010001 1110101010001000 function void main() { Translate high-level 0000000000010000 // Inputs and multiplies two numbers programs into 1111110000010000 0000000000000000 var int a, b, x; code. intermediate language let a = Keyboard.readInt(“Enter a number”); 1111010011010000 0000000000010010 let b = Keyboard.readInt(“Enter a number”); 1110001100000001 let x = mult(a,b); 0000000000010000 return; Compiler 1111110000010000 } 0000000000010001 hardware hardware hardware 0000000000010000 . . . hardware hardware hardware } platform 1 platform 2 platform m . . . 1110111111001000 platform 1 platform 2 platform m // Multiplies two numbers. 0000000000010001 1110101010001000 requires n. m translators requires n + m translators function int mult(int x, int y) { 0000000000010000 var int result, j; 1111110000010000 let result = 0; let j = y; 0000000000000000 while ~(j = 0) { 1111010011010000 Two-tier compilation: let result = result + x; 0000000000010010 let j = j – 1; 1110001100000001 } 0000000000010000  First stage: depends only on the details of the source language return result; 1111110000010000 } 0000000000010001  Second stage: depends only on the details of the target language. } ...

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 3 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 4 The big picture Focus of this lecture (yellow): The intermediate code: Some . . . Some Other . . . Jack Some . . . Some Other . . . Jack Book chapters and language language language language language  The interface between language Course projects: the 2 compilation stages

Some Jack  Must be sufficiently 9, 10, 11, 12 Some Other Some Jack compiler compiler compiler Some Other compiler general to support many compiler compiler Intermediate code pairs VM language  Can be modeled as the VM language of an abstract VM implementation VM imp. VM imp. VM imp. VM implementation VM imp. VM (this and the over CISC over RISC over the Hack virtual machine (VM) over the Hack 7, 8 over CISC over RISC emulator platforms platforms platform platforms platforms platform next lecture)  Can be implemented in several different ways.

CISC RISC written in Hack CISC RISC written in Hack machine machine . . . a high-level machine machine machine . . . a high-level machine language language language language language language language language

1, 2, 3, 4, 5, 6 ......

CISC RISC other digital platforms, each equipped Any Hack CISC RISC other digital platforms, each equipped Any Hack machine machine with its own VM implementation computer computer machine machine with its VM implementation computer computer

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 5 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 6

Virtual machines Virtual machines  A virtual machine (VM) is an emulation of a particular (real or  A virtual machine (VM) is an emulation of a particular (real or hypothetical) computer system. hypothetical) computer system.

(full VMs): a complete  System virtual machine ( VMs): a complete substitute for the targeted real machine and a level of substitute for the targeted real machine and a level of functionality required for the of a complete operating functionality required for the execution of a complete , e.g., VirtualBox. system, e.g., VirtualBox.

 Process virtual machine: designed to execute a single by providing an abstracted and platform-independent program execution environment, e.g., (JVM).

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 7 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 8 The VM model and language The VM model and language

Perspective: Several different ways to think about the notion of a virtual machine: From here till the end of the next lecture we describe the VM model  Abstract software engineering view: used in the Hack-Jack platform the VM is an interesting abstraction that makes sense in its own right (a hypothetical machine closer to high-level language, but could still be Other VM models (like Java’s JVM/JRE and .NET’s IL/CLR) are built easily. Sometimes, no need to worry about how to implement it in similar in spirit, but differ in scope and details. hardware.)

 Practical software engineering view: the VM code layer enables “managed code” (e.g. enhanced security)

 Pragmatic compiler writing view: a VM architecture makes writing a compiler much easier (as we’ll see later in the course)

 Opportunistic empire builder view: a VM architecture allows writing high-level code once and have it run on many target platforms with little or no modification.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 9 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 10

Hack virtual machine The stack

Goal: Specify and implement a VM model and language: The stack:  Arithmetic / Boolean commands Program flow commands A classical LIFO data structure add  Elegant and powerful label (declaration) sub  Several hardware / software implementation options. goto (label) neg eq if‐goto (label) gt Chapter 7 Chapter 8 lt Function calling commands and or function (declaration) not call (a function) Memory access commands pop x (pop into x, which is a variable) return (from a function) push y (y being a variable or a constant)

Our game plan: (a) describe the VM abstraction (3 types of instructions) (b) propose how to implement it over the Hack platform.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 11 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 12 The stack What is the stack good for?

The stack:  Stack can be used for evaluating arithmetic expressions  A classical LIFO data structure  Expression: 5 * (6+2) – 12/4  Elegant and powerful  Infix  Several hardware / software implementation options.  Several flavors: next empty/available, upward/downward  Prefix  Postfix push(x) stack[top]=x; top++;

pop() top‐‐; return stack[top];

peek(), empty() Stack is also good for implementing function call structures, such as subroutines, local variables and recursive calls. Will discuss it later.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 13 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 14

Our VM model is stack-oriented Data types

 All operations are done on a stack Our VM model features a single 16-bit data type that can be used as:

 Data is saved in several separate memory segments  an integer value (16-bit 2’s complement: ‐32768, ... , 32767)

 All the memory segments behave the same  a Boolean value (0 and ‐1, standing for true and false)

 One of the memory segments m is called static, and we will use it  a pointer (memory address) (as an arbitrary example) in the following examples:

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 15 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 16 Memory access operations Evaluation of arithmetic expressions

VM code (example) // z=(2‐x)‐(y+5) (suppose that push 2 push x refers to static 0, push x static 2 y refers to static 1, sub z refers to static 2) push y push 5 add sub pop z

(before) (after)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 17 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 18

Evaluation of Boolean expressions Arithmetic and Boolean commands in the VM language (wrap-up)

VM code (example) // (x<7) or (y=8) (suppose that push x x refers to static 0, push 7 y refers to static 1) lt push y push 8 eq or

(actually true and false are stored as 0 and ‐1, respectively)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 19 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 20 The VM’s Memory segments Memory segments and memory access commands A VM program is designed to provide an interim abstraction of a program The VM abstraction includes 8 separate memory segments named: written in some high-level language. static, this, local, argument, that, constant, pointer, temp As far as VM programming commands go, all memory segments look and Modern OO languages normally feature the following variable kinds: behave the same Class level: To access a particular segment entry, use the following generic syntax:  Static variables (class-level variables) Memory access VM commands:  Private variables (aka “object variables” / “fields” / “properties”)  pop memorySegment index Method level:  push memorySegment index  Local variables Where memorySegment is static, this, local, argument, that,  Argument variables constant, pointer, or temp When translated into the VM language, And index is a non-negative integer

The static, private, local and argument variables are mapped by the (In all our code examples thus far, memorySegment was static) compiler on the four memory segments static, this, local, argument The different roles of the eight memory segments will become relevant when we’ll talk In addition, there are four additional memory segments, whose role will about the compiler be presented later: that, constant, pointer, temp. At the VM abstraction level, all memory segments are treated the same way. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 21 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 22

VM programming VM programming

VM programs are normally written by , not by humans The example includes three new VM commands:

 function functionSymbol However, compilers are written by humans ... // function declaration

 label labelSymbol // label declaration In order to write or optimize a compiler, it helps to first understand the spirit of the compiler’s target language – the VM  if‐goto labelSymbol // pop x language // if x=true, jump to execute the // command after labelSymbol So, we’ll now see an example of a VM program // else proceed to execute the next // command in the program

For example, to effect if (x > n) goto loop, we can use the following VM commands:

push x push n gt if‐goto loop // Note that x, n, and the truth value // were removed from the stack.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 23 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 24 High-level code VM code (first approx.) VM code High-level code VM code (first approx.) VM code function mult (x,y) { function mult(x,y) function mult 2 function mult (x,y) { function mult(x,y) function mult 2 int result, j; push 0 push constant 0 int result, j; push 0 push constant 0 result = 0; pop result pop local 0 result = 0; pop result pop local 0 j = y; push y push argument 1 j = y; push y push argument 1 while ~(j = 0) { pop j pop local 1 while ~(j = 0) { pop j pop local 1 result = result + x; label loop label loop result = result + x; label loop label loop j = j ‐ 1; push j push local 1 j = j ‐ 1; push j push local 1 } push 0 push constant 0 } push 0 push constant 0 return result; eq eq return result; eq eq } if‐goto end if‐goto end } if‐goto end if‐goto end push result push local 0 push result push local 0 Pseudo code push x push argument 0 push x push argument 0 add add add add ... pop result pop local 0 pop result pop local 0 loop: push j push local 1 push j push local 1 if (j=0) goto end push 1 push constant 1 push 1 push constant 1 result=result+x sub sub sub sub j=j‐1 pop j pop local 1 pop j pop local 1 goto loop goto loop goto loop goto loop goto loop end: label end label end label end label end ... push result push local 0 push result push local 0 return return return return

VM programming: multiple functions VM programming: multiple functions

Compilation: Execution:

 A Jack application is a set of 1 or more class files (just like .java  At any given point of time, only one VM function is executing (the files). “current function”), while 0 or more functions are waiting for it to terminate (the functions up the “calling hierarchy”)  When we apply the Jack compiler to these files, the compiler creates a set of 1 or more .vm files (just like .class files). Each  For example, a main function starts running; at some point we may method in the Jack app is translated into a VM function written in reach the command call factorial, at which point the factorial the VM language function starts running; then we may reach the command call mult, at which point the mult  Thus, a VM file consists of one or more VM functions. function starts running, while both main and factorial are waiting for it to terminate

The stack: a global data structure, used to save and restore the resources (memory segments) of all the VM functions up the calling hierarchy (e.g. main and factorial). The tip of this stack if the working stack of the current function (e.g. mult).

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 27 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 28 VM programming: multiple functions (files) VM programming: multiple functions (memory)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 29 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 30

Handling array Handling array

int foo() {// some language, not Jack int bar[10]; ... Alternative bar[2] = 19; push local 0 } pop pointer 1 push constant 19 pop that 2

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 31 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 32 Handling objects Handling objects

Class Ball {// some language, not Jack int x, y, radius, color; int SetR(int r) {radius = r;} } Ball b; b.SetR(10);

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 33 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 34

Lecture plan Implementation of VM on Hack Summary: Hack VM has the following instructions and eight  Each VM instruction must be translated into a set of Hack assembly memory segments. code Arithmetic / Boolean commands Program flow commands  VM segments need to be realized on the host memory add label (declaration) sub goto (label) neg eq if‐goto (label) gt Chapter 7 Chapter 8 lt Function calling commands and or function (declaration) not call (a function) Memory access commands pop x (pop into x, which is a variable) return (from a function) push y (y being a variable or a constant)

Method: (a) specify the abstraction (stack, memory segments, commands) (b) how to implement the abstraction over the Hack platform.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 35 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 36 Implementation Software implementation: VM emulator (part of the course software suite) VM implementation options:

 Emulator-based (e.g. emulate the VM model using Java)

 Translator-based (e. g. translate VM programs into the Hack machine language)

 Hardware-based (realize the VM model using dedicated memory and registers)

Two well-known translator-based implementations:

JVM: Javac translates Java programs into ; The JVM translates the bytecode into the machine language of the host computer CLR: # compiler translates C# programs into IL code; The CLR translated the IL code into the machine language of the host computer.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 37 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 38

VM implementation on the Hack platform (memory) VM implementation on the Hack platform (memory)

The stack: a global data structure, used to save SP 0 Basic idea: the mapping of the stack and the

and restore the resources of all the VM LCL 1 global segments on the RAM is easy (fixed); functions up the calling hierarchy. the mapping of the function-level segments is ARG 2 dynamic, using pointers The tip of this stack if the working stack of the THIS 3

current function THAT 4 The stack: mapped on RAM[256 ... 2047]; Host Host The stack pointer is kept in RAM address SP static, constant, temp, pointer: 5 RAM Global memory segments, all functions TEMP . . . RAM static: mapped on RAM[16 ... 255]; static i see the same four segments 12 each segment reference appearing in a VM file named f is compiled to the assembly 13 local,argument,this,that: language symbol f.i (recall that the assembler further General 14 these segments are local at the function level; purpose maps such symbols to the RAM, from address 16 onward) each function sees its own, private copy of 15 local,argument,this,that: these method-level each one of these four segments 16 segments are mapped somewhere from address . . . The challenge: Statics 2048 onward, in an area called “heap”. The base represent all these logical constructs on the 255 addresses of these segments are kept in RAM same single physical address space -- the host 256 addresses LCL, ARG, THIS, and THAT. Access to RAM. . . . Stack the i-th entry of any of these segments is 2047 implemented by accessing RAM[segmentBase + i] 2048 constant: a truly a virtual segment: . . . Heap access to constant i is implemented by supplying the constant i. pointer: discussed later.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 39 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 40 VM implementation on the Hack platform (memory) VM implementation on the Hack platform (translator)

SP 0 Practice exercises push constant 1 add pop local 2 LCL 1 Now that we know how the memory segments are @1 @SP @LCL ARG 2 mapped on the host RAM, we can write Hack THIS 3 commands that realize the various VM commands. D=A AM=M‐1 D=M for example, let us write the Hack code that THAT 4 Host 5 implements the following VM commands: @SP D+M @2 TEMP . . . RAM  push constant 1 A=M A=A‐1 D=D+A 12  pop static 7 (suppose it appears in a VM file named f) 13  push constant 5 M=D M=M+D @R15 General 14 purpose  add 15 @SP M=D  pop local 2 16 M=M+1 @SP . . . Statics  eq 255 Tips: AM=M‐1 256 1. The implementation of any one of these VM . . . Stack D=M 2047 commands requires several Hack assembly commands involving pointer arithmetic 2048 @R15 (using commands like A=M) . . . Heap 2. If you run out of registers (you have only two ...), A=M you may use R13, R14, and R15. M=D Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 41 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 42

Some . . . Some Other . . . language language Jack

Some Perspective Some Other The big picture compiler compiler compiler  In this lecture we began the process of VM language VM implementation VM imp. VM over CISC over RISC emulator Translator building a compiler platforms platforms

CISC RISC written in machine machine . . . a high-level Hack  Modern compiler architecture: language language language ......  Front-end (translates from a high-level language to a VM language)  JVM  CLR  VM  7, 8  Back-end (translates from the VM language to the machine language of some target hardware platform)  Java  C#  Jack  9

 Brief history of virtual machines:  Java compiler  C# compiler  Jack compiler  10, 11  1970’s: p-Code  JRE  .NET base  Mini OS  12  1990’s: Java’s JVM class (Book chapters and  2000’s: Microsoft .NET Course projects)  A full blown VM implementation typically also includes a common software library (can be viewed as a mini, portable OS).  We will build such a mini OS later in the course. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 43 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 44