virtual machine part 2 program control
play

Virtual Machine Part 2: Program Control Foundations of Global - PowerPoint PPT Presentation

IWKS 3300: NAND to Tetris Spring 2019 John K. Bennett Virtual Machine Part 2: Program Control Foundations of Global Networked Computing: Building a Modern Computer From First Principles This course is based upon the work of Noam Nisan and


  1. IWKS 3300: NAND to Tetris Spring 2019 John K. Bennett Virtual Machine Part 2: Program Control Foundations of Global Networked Computing: Building a Modern Computer From First Principles This course is based upon the work of Noam Nisan and Shimon Schocken. More information can be found at (www.nand2tetris.org).

  2. Where We Are Software Hierarchy Abstract design Human We are (still) here abstract interface Thought Chapters 9, 12 Compiler H.L. Language & abstract interface Chapters 10 - 11 Operating Sys. VM Translator Virtual abstract interface Machine Chapters 7 - 8 Assembly Language Assembler Chapter 6 abstract interface Computer Architecture Machine abstract interface Code Chapters 4 - 5 Gate Logic Hardware abstract interface Platform Chapters 1 - 3 Electrical Engineering Chips & Physics Logic Gates Hardware Hierarchy

  3. The Big Picture . . . . . . Jack Some Some Other language language language Chapters 9- Jack Some Some Other 13 compiler compiler compiler Implemented in Projects 7-8 VM language VM VM imp. implementation VM imp. Chapters VM over the Hack over CISC over RISC emulator 7-8 platform platforms platforms A Java-based emulator is included in the course CISC RISC written in Hack . . . software suite machine machine a high-level machine language language language language Chapters . . . . . . 1-6 Hack CISC RISC other digital platforms, each equipped Any computer machine machine with its VM implementation computer

  4. Our Game Plan Goal: Specify and implement a VM model and language: Arithmetic / Boolean commands Program flow commands add label (declaration) sub goto (label) neg if-goto (label) eq gt Today Last Week lt Function calling commands and or function (declaration) not call (a function) Memory access commands return (from a function) pop x (pop into x , a variable) push y (push y, a variable or constant) Our game plan today: (a) describe the VM abstraction (above) (b) suggest how to implement it over the Hack platform.

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

  6. The Compilation Challenge / Two-tier Approach VM (pseudo) code Machine code Jack source code push a 0000000000010000 if (~(a = 0)) push 0 1110111111001000 x = (-b+sqrt(b*b – 4*a*c))/(2*a) eq //!cond on stack 0000000000010001 else if-goto elseLabel 1110101010001000 x = -c/b Compiler push b 0000000000010000 neg 1111110000010000 push b 0000000000000000 push b 1111010011010000 call mult 0000000000010010 push 4 1110001100000001  We will develop the Jack language VM translator push a 0000000000010000 compiler later in the course call mult 1111110000010000 push c 0000000000010001 call mult 0000000000010000  We now turn to describe how to call sqrt 1110111111001000 complete the implementation of add 0000000000010001 the VM language push 2 1110101010001000 push a 0000000000010000 call mult 1111110000010000  That is -- how to translate each div 0000000000000000 VM command into assembly pop x 1111010011010000 commands that perform the goto contLabel 0000000000010010 elseLabel: 1110001100000001 desired semantics. push c 0000000000010000 neg 1111110000010000 push b 0000000000010001 call div 0000000000010010 pop x 1110001100000001 contLabel: ...

  7. 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 (today) (last week) (today) (last week) How to translate this high-level code into assembly 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 of 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.

  8. Program Flow Commands in the VM Language In the VM language, the program flow abstraction is VM code example: delivered using three commands: function mult 1 push constant 0 label c // label declaration pop local 0 label loop goto c // unconditional jump to the push argument 0 // VM command following the label c push constant 0 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 pop argument 0 How to translate these 3 abstractions into assembly? push argument 1  label declarations and goto directives can be push local 0 effected directly by assembly commands add  The VM Translator must emit one or more assembly pop local 0 goto loop commands that performs these semantics label end on the Hack platform push local 0  Today’s lecture will describe how return

  9. Subroutines in the VM Language // 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 are a programming artifact of most modern languages  Basic idea: the given language can be extended at will by user- defined commands ( aka subroutines / functions / methods ...)  Important: the language’s built -in primitive commands and the user- defined commands have the same look-and-feel  This transparent extensibility is the one of the most important abstractions provided by high-level programming languages  The challenge: how to implement this abstraction, allowing program control to flow seamlessly from one subroutine to another and back

  10. Subroutines in the VM language Calling code (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 push constant 0 VM subroutine call mult 2 eq call-and-return push constant 5 if-goto end // if arg0 == 0, jump to end commands sub push argument 0 ... push constant 1 sub The invocation of the VM ’s primitive pop argument 0 // arg0-- push argument 1 commands and subroutines push local 0 follow the same rules: add  The caller pushes the necessary pop local 0 // result += arg1 argument(s) onto the stack and calls the goto loop command / function for its effect label end  The called command / function is push local 0 // push result responsible for removing the argument(s) return from the stack, and for popping onto the stack the result of its execution.

  11. Function-related 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 (as we will see in a moment).

  12. Function Call-and-return Conventions called function aka “callee” (example) Calling function Although not obvious in this function demo 3 function mult 1 example, every VM function ... push constant 0 push constant 7 pop local 0 // result (local 0) = 0 has a private set of 5 memory segments ( local,argument, push constant 2 label loop add ... // rest of code omitted this,that, pointer) push constant 3 label end These resources exist only as call mult 2 push local 0 // push result long as the function is running. ... 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 net effect: the arguments were replaced by the return value (just like with primitive commands) 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.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend