<<

Part II: Program Control

Building a Modern Computer From First Principles

www.nand2tetris.org

Elements of Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 1 The big picture

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

Chapters Some Jack Some Other 9-13 compiler compiler

Implemented in VM language Projects 7-8

VM implementation VM imp. VM imp. VM over the Hack Chapters over CISC over RISC emulator platforms platforms platform 7-8

A Java-based emulator CISC RISC is included in the course written in Hack suite machine machine . . . a high-level machine language language language language

Chapters ...... 1-6

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

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 2 The VM language

Goal: Complete the specification and implementation of the VM model and language

Arithmetic / Boolean commands Program flow commands add (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 (model’s constructs and commands) (b) propose how to implement it over the Hack platform.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 3 The compilation challenge

Source code (high-level language) Target code class Main { 0000000000010000 static int x; 1110111111001000 Our ultimate goal: 0000000000010001 function void main() { 1110101010001000 // Inputs and multiplies two numbers Translate high-level 0000000000010000 var int a, b, c; programs into 1111110000010000 0000000000000000 let a = Keyboard.readInt(“Enter a number”); code. 1111010011010000 let b = Keyboard.readInt(“Enter a number”); 0000000000010010 let c = Keyboard.readInt(“Enter a number”); 1110001100000001 let x = solve(a,b,c); 0000000000010000 return; Compiler 1111110000010000 } 0000000000010001 0000000000010000 } 1110111111001000 0000000000010001 // Solves a quadratic equation (sort of) 1110101010001000 function int solve(int a, int b, int c) { 0000000000010000 var int x; 1111110000010000 if (~(a = 0)) 0000000000000000 x=(‐b+sqrt(b*b–4*a*c))/(2*a); 1111010011010000 0000000000010010 else 1110001100000001 x=‐c/b; 0000000000010000 return x; 1111110000010000 } 0000000000010001 } ...

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 4 The compilation challenge / two-tier setting

Jack VM (pseudo) code push a 0000000000010000 if (~(a = 0)) push 0 1110111111001000 x = (‐b+sqrt(b*b–4*a*c))/(2*a) eq 0000000000010001 else if‐goto elseLabel 1110101010001000 x = ‐c/b Compiler push b 0000000000010000 neg 1111110000010000 push b 0000000000000000 push b 1111010011010000 call mult 0000000000010010  We’ll develop the compiler push 4 1110001100000001 push a VM translator 0000000000010000 later in the course call mult 1111110000010000 push c 0000000000010001 call mult 0000000000010000  We now turn to describe call sqrt 1110111111001000 how to complete the add 0000000000010001 push 2 1110101010001000 implementation of the VM push a 0000000000010000 call mult 1111110000010000 language div 0000000000000000 pop x 1111010011010000 goto contLable 0000000000010010  That is -- how to elseLabel: 1110001100000001 translate each VM push c 0000000000010000 neg 1111110000010000 into assembly push b 0000000000010001 call div 0000000000010010 commands that perform pop x 1110001100000001 the desired semantics. contLable: ...

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 5 The compilation challenge Typical compiler’s source code input:

// Computes x = (‐b + sqrt(b^2 ‐4*a*c)) / 2*a if (~(a = 0)) x = (‐b + sqrt(b * b –4 * a * c)) / (2 * a) else x = ‐ c / b

program flow logic Boolean function call and arithmetic (branching) expressions return logic expressions

(this lecture)(previous lecture) (this lecture) (previous lecture) How to translate such high-level code into machine language?  In a two-tier compilation model, the overall translation challenge is broken between a front-end compilation stage and a subsequent back- end translation stage  In our Hack-Jack platform, all the above sub-tasks (handling arithmetic / Boolean expressions and program flow / function calling commands) are done by the back-end, i.e. by the VM translator.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 6 Lecture plan

Arithmetic / Boolean commands Program flow commands add label (declaration) sub goto (label) neg eq if‐goto (label) gt Chapter 7 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)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 7 Program flow commands in the VM language

VM code example: In the VM language, the program flow abstraction function mult 1 is delivered using three commands: push constant 0 pop local 0 label c // label declaration label loop push argument 0 goto c // unconditional jump to the push constant 0 // VM command following the label c eq if‐goto c // pops the topmost stack element; if‐goto end // if it’s not zero, jumps to the push argument 0 // VM command following the label c push 1 sub How to translate these abstractions into assembly? pop argument 0 push argument 1  Simple: label declarations and goto directives can push local 0 be effected directly by assembly commands add  to the point: given any one of these three VM pop local 0 commands, the VM Translator must emit one or goto loop more assembly commands that effects the same label end semantics on the Hack platform push local 0 return  How to do it? see project 8.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 8 Flow of control

pseudo code VM code if (cond) ~cond statement1 if‐goto elseLabel else statement1 statement2 goto contLabel label elseLabel statement2 label contLabel

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 9 Flow of control

pseudo code VM code while (cond) label contLabel statement ~(cond) ... if‐goto exitLabel statement goto contLabel label exitLabel ...

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 10 Lecture plan

Arithmetic / Boolean commands Program flow commands add label (declaration) sub goto (label) neg eq if‐goto (label) gt previous lt lecture 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)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 11 Subroutines

// Compute x = (‐b + sqrt(b^2 ‐4*a*c)) / 2*a if (~(a = 0)) x = (‐b + sqrt(b * b –4 * a * c)) / (2 * a) else x = ‐ c / b

Subroutines = a major programming artifact  Basic idea: the given language can be extended will by user- defined commands ( aka subroutines / functions / methods ...)  Important: the language’s primitive commands and the user-defined commands have the same look-and-feel  This transparent extensibility is the most important abstraction delivered by high-level programming languages  The challenge: implement this abstraction, i.e. allow the program control to flow effortlessly between one subroutine to the other

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 12 Subroutines in the VM language Calling code, aka “caller” (example) Called code, aka “callee” (example) ... function mult 1 // computes (7 + 2) * 3 ‐ 5 push constant 0 push constant 7 pop local 0 // result (local 0) = 0 push constant 2 label loop add push argument 0 push constant 3 VM subroutine push constant 0 call mult call-and-return eq push constant 5 commands if‐goto end // if arg0==0, jump to end sub push argument 0 ... push 1 sub pop argument 0 // arg0‐‐ push argument 1 push local 0 add pop local 0 // result += arg1 goto loop label end push local 0 // push result return

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 13 Subroutines in the VM language

The invocation of the VM’s primitive commands and subroutines follow exactly the same rules:  The caller pushes the necessary argument(s) and calls the command / function for its effect  The callee is responsible for removing the argument(s) from the stack, and for popping onto the stack the result of its .

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 14 What behind subroutines

The following scenario happens  The caller pushes the necessary arguments and call callee  The state of the caller is saved  The space of callee’s local variables is allocated  The callee executes what it is supposed to do  The callee removes all arguments and pushes the result to the stack  The space of the callee is recycled  The caller’s state is reinstalled  Jump back to where is called

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 15 Stack as the facility for subroutines code flow stack function a a call b start b call c start c ... start d end d function b end c call c start d call d end d ... end b start c function c start d call d end d ... end c end a function d ...

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 16 Function commands in the VM language

function g nVars // here starts a function called g, // which has nVars local variables

call g nArgs // invoke function g for its effect; // nArgs arguments have already been pushed // onto the stack

return // terminate execution and return control // to the caller

Q: Why this particular syntax?

A: Because it simplifies the VM implementation (later).

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 17 Function call-and-return conventions Calling function called function aka “callee” (example) function demo 3 function mult 1 ... push constant 0 push constant 7 pop local 0 // result (local 0) = 0 push constant 2 label loop add ... // rest of code omitted push constant 3 label end call mult push local 0 // push result ... return

Although not obvious in this example, every VM function has a private set of 5 memory segments (local, argument, this, that, pointer) These resources exist as long as the function is running.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 18 Function call-and-return conventions Calling function called function aka “callee” (example) function demo 3 function mult 1 ... push constant 0 push constant 7 pop local 0 // result (local 0) = 0 push constant 2 label loop add ... // rest of code omitted push constant 3 label end call mult push local 0 // push result ... return

Call-and-return programming convention  The caller must push the necessary argument(s), call the callee, and wait for it to return  Before the callee terminates (returns), it must push a return value  At the point of return, the callee’s resources are recycled, the caller’s state is re-instated, execution continues from the command just after the call  Caller’s effect: the arguments were replaced by the return value (just like with primitive commands)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 19 Function call-and-return conventions Calling function called function aka “callee” (example) function demo 3 function mult 1 ... push constant 0 push constant 7 pop local 0 // result (local 0) = 0 push constant 2 label loop add ... // rest of code omitted push constant 3 label end call mult push local 0 // push result ... return

Behind the scene  Recycling and re-instating subroutine resources and states is a major headache  Some agent (either the VM or the compiler) should manage it behind the scene “like magic”  In our implementation, the magic is VM / stack-based, and is considered a great CS gem.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 20 The function-call-and-return protocol function g nVars call g nArgs return The caller’s view:

 Before calling a function g, I must push onto the stack as many arguments as needed by g  Next, I invoke the function using the command call g nArgs  After g returns:  The arguments that I pushed before the call have disappeared from the stack, and a return value (that always exists) appears at the top of the stack  All my memory segments (local, argument, this, that, pointer) are the same as before the call.

Blue = VM function writer’s responsibility

Black = black box magic, delivered by the VM implementation

Thus, the VM implementation writer must worry about the “black operations” only.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 21 The function-call-and-return protocol function g nVars call g nArgs return The callee’s (g ‘s) view:

 When I start executing, my argument segment has been initialized with actual argument values passed by the caller  My local variables segment has been allocated and initialized to zero  The static segment that I see has been set to the static segment of the VM file to which I belong, and the working stack that I see is empty  Before exiting, I must push a value onto the stack and then use the command return.

Blue = VM function writer’s responsibility

Black = black box magic, delivered by the VM implementation

Thus, the VM implementation writer must worry about the “black operations” only.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 22 The function-call-and-return protocol: the VM implementation view When function f calls function g, the VM implementation must: function g nVars  Save the return address within f ‘s code: call g nArgs the address of the command just after the call return  Save the virtual segments of f  Allocate, and initialize to 0, as many local variables as needed by g  Set the local and argument segment pointers of g  Transfer control to g.

When g terminates and control should return to f, the VM implementation must:  Clear g ’s arguments and other junk from the stack  Restore the virtual segments of f  Transfer control back to f (jump to the saved return address).

Q: How should we make all this work “like magic”? A: We’ll use the stack cleverly.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 23 The implementation of the VM’s stack on the host Hack RAM Global stack: the entire RAM area dedicated for holding the stack Working stack: The stack that the current function sees

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 24 The implementation of the VM’s stack on the host Hack RAM

 At any point of , only one function (the current function) is executing; other functions may be waiting up the calling chain  Shaded areas: irrelevant to the current function  The current function sees only the working stack, and has access only to its memory segments  The rest of the stack holds the frozen states of all the functions up the calling hierarchy.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 25 Implementing the call g nArgs command None of this code is executed yet ... At this point we are just generating code (or simulating the VM code on call g nArgs some platform) // In the course of implementing the code of f // (the caller), we arrive to the command call g nArgs. // we assume that nArgs arguments have been pushed // onto the stack. What do we do next? // We generate a symbol, let’s call it returnAddress; // Next, we effect the following logic: push returnAddress // saves the return address push LCL // saves the LCL of f push ARG // saves the ARG of f push THIS // saves the THIS of f push THAT // saves the THAT of f ARG = SP‐nArgs‐5 // repositions SP for g LCL = SP // repositions LCL for g goto g // transfers control to g returnAddress: // the generated symbol Implementation: If the VM is implemented as a program that translates VM code into assembly code, the translator must emit the above logic in assembly. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 26 Implementing the function g nVars command

function g nVars frames of all the functions up the calling chain // to implement the command function g nVars, // we effect the following logic: ARG argument 0 argument 1 g: repeat nVars times: . . . argument nArgs-1 push 0 saved returnAddress saved LCL

saved ARG

saved THIS

saved THAT

LCL local 0

local 1 . . . local nVars-1 SP Implementation: If the VM is implemented as a program that translates VM code into assembly code, the translator must emit the above logic in assembly.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 27 Implementing the return command

return

// In the course of implementing the code of g, // we arrive to the command return. // We assume that a return value has been pushed // onto the stack. // We effect the following logic: frame = LCL // frame is a temp. variable retAddr = *(frame‐5) // retAddr is a temp. variable *ARG = pop // repositions the return value // for the caller SP=ARG+1 // restores the caller’s SP THAT = *(frame‐1) // restores the caller’s THAT THIS = *(frame‐2) // restores the caller’s THIS ARG = *(frame‐3) // restores the caller’s ARG LCL = *(frame‐4) // restores the caller’s LCL goto retAddr // goto returnAddress Implementation: If the VM is implemented as a program that translates VM code into assembly code, the translator must emit the above logic in assembly. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 28 Example: factorial VM code (first approx.) VM code function fact(n) function fact 2 High-level code push 0 push constant 0 function fact (n) { pop result pop local 0 int result, j; push 1 push constant 1 result = 1; pop j pop local 1 j = 1; label loop label loop while ((j=j+1) <= n) { push 1 push constant 1 result = result * j; push j push local 1 } add add return result; pop j pop local 1 } push n push argument 0 gt gt if‐goto end Pseudo code if‐goto end push result push local 0 ... push j push local 1 loop: mult call mult 2 if ((j=j+1) > n) goto end pop result pop local 0 result=result*j goto loop goto loop goto loop label end label end end: push result push local 0 ... return return function p ... push constant 4 call fact 1 ...

Example: factorial VM code (first approx.) High-level code function fact(n) push n function fact (n) { push 1 int r; eq if (n!=1) if‐goto else r = n * fact(n‐1); push n else push 1 r = 1; sub return r; fact } push n mult pop r goto cont label else push 1 pop r label cont push r return Example: factorial VM code (first approx.) VM code High-level code function fact(n) function fact 1 push n push argument 0 function fact (n) { push 1 push constant 1 int r; eq eq if (n!=1) if‐goto else if‐goto else r = n * fact(n‐1); push n push argument 0 else push 1 push constant 1 r = 1; sub sub return r; fact call fact 1 } push n push argument 0 mult call mult 2

goto cont goto cont label else label else push 1 push constant 1

label cont label cont

return return Calling stack for fact(4)

High-level code stack

function fact (n) { frame fact(4) int r; frame fact(3) if (n!=1) r = n * fact(n‐1); frame fact(2) else frame fact(1) r = 1; return r; }

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 34 Calling stack for fact(4)

High-level code stack

function fact (n) { frame fact(4) int r; frame fact(3) if (n!=1) r = n * fact(n‐1); frame fact(2) else frame mult(2,1) r = 1; return r; }

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 35 Calling stack for fact(4)

High-level code stack

function fact (n) { frame fact(4) int r; frame fact(3) if (n!=1) r = n * fact(n‐1); frame mult(3,2) else r = 1; return r; }

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 36 Calling stack for fact(4)

High-level code stack

function fact (n) { frame fact(4) int r; frame mult(4,6) if (n!=1) r = n * fact(n‐1); else r = 1; return r; }

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 37 Bootstrapping A high-level jack program (aka application) is a set of class files. By a Jack convention, one class must be called Main, and this class must have at least one function, called main.

The contract: when we tell the computer to execute a Jack program, the function Main.main starts running

Implementation:  After the program is compiled, each class file is translated into a .vm file  The is also implemented as a set of .vm files (aka “libraries”) that co-exist alongside the program’s .vm files  One of the OS libraries, called Sys.vm, includes a method called init. The Sys.init function starts with some OS initialization code (we’ll deal with this later, when we discuss the OS), then it does call Main.main  Thus, to bootstrap, the VM implementation has to effect (e.g. in assembly), the following operations:

SP = 256 // initialize the stack pointer to 0x0100 call Sys.init // call the function that calls Main.main

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 38 Perspective Some . . . Some Other . . . language language Jack

Benefits of the VM approach Some compiler Some Other compiler compiler  Code transportability: compiling VM language for different platforms requires VM implementation VM imp. VM over CISC over RISC replacing only the VM emulator Translator platforms platforms implementation

CISC RISC written in machine machine . . . a high-level Hack  Language inter-operability: code of language language language multiple languages can be shared ...... using the same VM Benefits of managed code:  Common software libraries  Security  Code mobility: Internet, cloud  Array bounds, index checking, …  Add-on code  Etc. VM Cons  Performance.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 39 Perspective Some . . . Some Other . . . language language Jack  Some virtues of the modularity Some compiler Some Other compiler implied by the VM approach to compiler

program translation: VM language

VM implementation VM imp. VM over CISC over RISC  Improvements in the VM emulator Translator platforms platforms implementation are shared by

CISC RISC written in all above it machine machine . . . a high-level Hack language language language  Every new digital device with a ...... VM implementation gains immediate access to an existing Benefits of managed code: software base  Security  New programming languages can  Array bounds, index be implemented easily using checking, … simple compilers  Add-on code  Etc. VM Cons  Performance.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 40