Virtual Machine Part II: Program Control
Total Page:16
File Type:pdf, Size:1020Kb
Virtual Machine Part II: Program Control Building a Modern Computer From First Principles www.nand2tetris.org Elements of Computing 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 compiler 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 software 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 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 (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”); executable 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 source code VM (pseudo) code Machine 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 command 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 More 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 at 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 execution. 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 start 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 ..