Virtual Machine Abstract Interface Thought Chapters 9, 12 Hierarchy Part I: Stack Arithmetic H.L

Virtual Machine Abstract Interface Thought Chapters 9, 12 Hierarchy Part I: Stack Arithmetic H.L

Where we are at: Human Abstract design Software Virtual Machine abstract interface Thought Chapters 9, 12 hierarchy Part I: Stack Arithmetic H.L. Language Compiler & abstract interface Chapters 10 - 11 Operating Sys. Virtual VM Translator abstract interface Machine Chapters 7 - 8 Assembly Language Assembler Chapter 6 abstract interface Computer 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; executable 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 <high-level language, machine-language> 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 emulator 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. System virtual machine (full virtualization VMs): a complete System virtual machine (full virtualization 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 execution of a complete operating functionality required for the execution of a complete operating system, e.g., VirtualBox. system, e.g., VirtualBox. Process virtual machine: designed to execute a single computer program by providing an abstracted and platform-independent program execution environment, e.g., Java virtual machine (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)

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us