Motivation. Compiler. Our ultimate goal: Hack code. Jack code (example) Translate high-level programs into executable code. return; } } return

Similar documents
Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles.

Virtual Machine. Part I: Stack Arithmetic. Building a Modern Computer From First Principles.

Virtual Machine Where we are at: Part I: Stack Arithmetic. Motivation. Compilation models. direct compilation:... 2-tier compilation:

Virtual Machine (Part II)

Chapter 8: Virtual Machine II: Program Control

Virtual Machine (Part II)

Assembler Human Thought

Compiler II: Code Generation Human Thought

Introduction: From Nand to Tetris

Introduction: Hello, World Below

Assembler. Building a Modern Computer From First Principles.

Compiler I: Syntax Analysis

7. The Virtual Machine II: Flow Control 1

Compiler I: Sytnax Analysis

Chapter 10: Compiler I: Syntax Analysis

Chapter 11: Compiler II: Code Generation

7. The Virtual Machine

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (

Functions in MIPS. Functions in MIPS 1

Lecture 5. Announcements: Today: Finish up functions in MIPS

CSC 2400: Computer Systems. Using the Stack for Function Calls

Implementing Procedure Calls

Today. Putting it all together

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Machine (Assembly) Language

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Compilers and computer architecture: A realistic compiler to MIPS

Digital Forensics Lecture 3 - Reverse Engineering

Boolean Arithmetic. From Nand to Tetris Building a Modern Computer from First Principles. Chapter 2

IWKS 3300: NAND to Tetris Spring John K. Bennett. Assembler

Machine (Assembly) Language Human Thought

Chapter 6: Assembler

Chapter 9: High Level Language

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

10. The Compiler II: Code Generation 1

Problem with Scanning an Infix Expression

Implementing Functions at the Machine Level

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

SOURCE LANGUAGE DESCRIPTION

Compiler Construction

CS61C : Machine Structures

CSE Lecture In Class Example Handout

EP1200 Introduktion till datorsystemteknik Tentamen tisdagen den 3 juni 2014, till 18.00

Compiler Construction

CS 2210 Programming Project (Part IV)

Module 27 Switch-case statements and Run-time storage management

CS61C : Machine Structures

Compiler Construction I

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Code Generation. Lecture 12

CSCE 5610: Computer Architecture

Problem with Scanning an Infix Expression

Today's Topics. CISC 458 Winter J.R. Cordy

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

CSC 8400: Computer Systems. Using the Stack for Function Calls

Topic 7: Activation Records

Compiler Construction D7011E

Subprograms, Subroutines, and Functions

x86 assembly CS449 Fall 2017

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Computer Architecture

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

1/30/2018. Conventions Provide Implicit Information. ECE 220: Computer Systems & Programming. Arithmetic with Trees is Unambiguous

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Implementing Subprograms

ECS 142 Project: Code generation hints

Course Administration

Compiling Techniques

Arithmetic Logic Unit (ALU)

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

Run-Time Data Structures

Compiling Code, Procedures and Stacks

CS-201 Introduction to Programming with Java

CSC 2400: Computer Systems. Using the Stack for Function Calls

Chapter 2A Instructions: Language of the Computer

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Chapter 6: The Assembler The Assembler Hack Assembly-to-Binary Translation Specification

CPSC 213. Introduction to Computer Systems. Reading. Control Flow. Loops (S5-loop) Static Control Flow. Unit 1d. Companion.

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

CPSC 213. Introduction to Computer Systems. Static Control Flow. Unit 1d

CPSC 213. Introduction to Computer Systems. Readings for Next 2 Lectures. Loops (S5-loop) Control Flow. Static Control Flow. Unit 1d.

A Unix process s address space appears to be three regions of memory: a read-only text region (containing executable code); a read-write region

Project 3 Due October 21, 2015, 11:59:59pm

MIPS Procedure Calls. Lecture 6 CS301

RECURSIVE FUNCTIONS ON STACK

Lecture 7: Procedures

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

CS558 Programming Languages

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

Type Checking Binary Operators

Anne Bracy CS 3410 Computer Science Cornell University

Transcription:

Motivation Jack code (example) class class Main Main { { static static int int x; x; function function void void main() main() { { Inputs Inputs and and multiplies multiplies two two numbers numbers var var int int a, a, b, b, x; x; a a Keyboard.readInt( Enter Keyboard.readInt( Enter a a number ); number ); b b Keyboard.readInt( Enter Keyboard.readInt( Enter a a number ); number ); x x mult(a,b); mult(a,b); return; return; Multiplies Multiplies two two numbers. numbers. function function int int mult(int mult(int x, x, int int y) y) { { var var int int result, result, j; j; result result 0; 0; j j y; y; while while ~(j ~(j 0) 0) { { result result result result + + x; x; j j j j 1; 1; return return result; result; Our ultimate goal: Translate high-level programs into executable code. Compiler Hack code 1110111111001000 1110111111001000 0000000000010001 0000000000010001 1110101010001000 1110101010001000 1111110000010000 1111110000010000 0000000000000000 0000000000000000 1111010011010000 1111010011010000 0000000000010010 0000000000010010 1110001100000001 1110001100000001 1111110000010000 1111110000010000 0000000000010001 0000000000010001 1110111111001000 1110111111001000 0000000000010001 0000000000010001 1110101010001000 1110101010001000 1111110000010000 1111110000010000 0000000000000000 0000000000000000 1111010011010000 1111010011010000 0000000000010010 0000000000010010 1110001100000001 1110001100000001 1111110000010000 1111110000010000 0000000000010001 0000000000010001...... Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 7: Virutal Machine, Part I slide 3

Compilation models direct compilation:... language 1 language 2 language n 2-tier compilation:... language 1 language 2 language n intermediate language hardware platform 1 hardware platform 2.... requires n m translators hardware platform m hardware platform 1 hardware platform 2... requires n + m translators hardware platform m Two-tier compilation: First compilation stage: depends only on the details of the source language Second compilation 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 4

The big picture VM implementation over CISC platforms The intermediate code: Some...... language Some compiler VM imp. over RISC platforms Some Other language Some Other compiler Intermediate code VM emulator Jack language Jack compiler VM imp. over the Hack platform The interface between the 2 compilation stages Must be sufficiently general to support many <high-level language, machine-language> pairs Can be modeled as the language of an abstract virtual machine (VM) Can be implemented in several different ways. CISC machine language RISC machine language... written in a high-level language Hack machine language...... CISC machine RISC machine other digital platforms, each equipped with its own VM implementation Any computer Hack computer Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 7: Virutal Machine, Part I slide 5

Our VM model is stack-oriented All operations are done on a stack Data is saved in several separate memory segments All the memory segments behave the same One of the memory segments m is called static, and we will use it (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 10

Data types Our VM model features a single 16-bit data type that can be used as: an integer value (16-bit 2 s complement: -32768,..., 32767) a Boolean value (0 and -1, standing for true and false) a pointer (memory address) Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 7: Virutal Machine, Part I slide 11

Memory access operations static 2 (before) (after) pop static 0 The stack: A classical LIFO data structure Elegant and powerful Several hardware / software implementation options. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 7: Virutal Machine, Part I slide 12

Evaluation of arithmetic expressions VM code (example) z(2-x)-(y+5) z(2-x)-(y+5) 2 2 x x sub sub y y 5 5 add add sub sub pop pop z z (suppose that x refers to static 0, y refers to static 1, and z refers to static 2) Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 7: Virutal Machine, Part I slide 13

Evaluation of Boolean expressions VM code (example) (x<7) (x<7) or or (y8) (y8) x x 7 7 lt lt y y 8 8 eq eq or or (suppose that x refers to static 0, and y refers to static 1) (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 14

Arithmetic and Boolean commands in the VM language (wrap-up) Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 7: Virutal Machine, Part I slide 15

The VM s Memory segments A VM program is designed to provide an interim abstraction of a program written in some high-level language Modern OO high-level languages normally feature the following variable kinds: Class level: Static variables (class-level variables) Private variables (aka object variables / fields / properties ) Method level: Local variables Argument variables When translated into the VM language, The static, private, local and argument variables are mapped by the compiler on the four memory segments static, this, local, argument In addition, there are four additional memory segments, whose role will be presented later: that, constant, pointer, temp. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 7: Virutal Machine, Part I slide 16

Memory segments and memory access commands The VM abstraction includes 8 separate memory segments named: static, this, local, argument, that, constant, pointer, temp As far as VM programming commands go, all memory segments look and behave the same To access a particular segment entry, use the following generic syntax: Memory access VM VM commands: pop popmemorysegment index index memorysegment index index Where memorysegment is is static, this, this, local, argument, that, that, constant, pointer, or ortemp And And index indexis is a non-negative integer Notes: (In all our code examples thus far, memorysegment was static) The different roles of the eight memory segments will become relevant when we ll talk about the compiler 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 17

VM programming VM programs are normally written by compilers, not by humans However, compilers are written by humans... In order to write or optimize a compiler, it helps to first understand the spirit of the compiler s target language the VM language So, we ll now see an example of a VM program The example includes three new VM commands: function functionsymbol function function declaration declaration label labelsymbol label label declaration declaration if-goto labelsymbol pop pop x if if xtrue, xtrue, jump jump to to execute execute the the command command after after labelsymbol else else proceed proceed to to execute execute the the next next command command in in the the program program For For example, to to effect if if (x (x > n) n) goto gotoloop, we we can can use use the the following VM VM commands: x n gt gt if-goto if-goto loop loop Note Note that that x, x, n, n, and and the the truth truth value value were were removed removed from from the the stack. stack. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 7: Virutal Machine, Part I slide 18

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

VM programming: multiple functions Compilation: A Jack application is a set of 1 or more class files (just like.java files). 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 method in the Jack app is translated into a VM function written in the VM language Thus, a VM file consists of one or more VM functions. Execution: At any given point of time, only one VM function is executing (the current function ), while 0 or more functions are waiting for it to terminate (the functions up the calling hierarchy ) For example, a main function starts running; at some point we may reach the command call factorial, at which point the factorial function starts running; then we may reach the command call mult, at which point the mult 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 20

Program flow commands in the VM language VM code example: function function mult mult 1 constant constant 0 pop pop local local 0 label label loop loop argument argument 0 constant constant 0 eq eq if-goto if-goto end end argument argument 0 1 sub sub pop pop argument argument 0 argument argument 1 local local 0 add add pop pop local local 0 goto goto loop loop label label end end local local 0 return return In the VM language, the program flow abstraction is delivered using three commands: label c label label declaration goto goto c unconditional jump jump to to the the VM VMcommand following the the label label c if-goto c pops pops the the topmost stack stack element; if if it s it s not not zero, zero, jumps to to the the VM VMcommand following the the label label c How to translate these three abstractions into assembly? Simple: label declarations and goto directives can be effected directly by assembly commands More to the point: given any one of these three VM commands, the VM Translator must emit one or more assembly commands that effects the same semantics on the Hack platfrom 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 9

Subroutines Compute Compute x (-b (-b + sqrt(b^2 sqrt(b^2-4*a*c)) -4*a*c))/ 2*a 2*a if if (~(a (~(a 0)) 0)) x (-b (-b + sqrt(b sqrt(b * b 4 * a * c)) c)) / (2 (2* a) a) else 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 A well-designed system consists of a collection of black box modules, each executing its effect like magic (Steven Pinker, How The Mind Works) Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 8: Virtual Machine, Part II slide 11

Subroutines in the VM language Calling code (example)...... computes computes (7 (7 + 2) 2) * 3-5 constant constant 7 constant constant 2 add add constant constant 3 call call mult mult constant constant 5 sub sub...... VM subroutine call-and-return commands The invocation of the VM s primitive commands and subroutines follow exactly the same rules: The caller es the necessary argument(s) and calls the command / function for its effect The called command / function is responsible for removing the argument(s) from the stack, and for popping onto the stack the result of its execution. Called code, aka callee (example) function function mult mult 1 constant constant 0 pop pop local local 0 result result (local (local 0) 0) 0 label label loop loop argument argument 0 constant constant 0 eq eq if-goto if-goto end end if if arg0 arg0 0, 0, jump jump to to end end argument argument 0 1 sub sub pop pop argument argument 0 arg0-- arg0-- argument argument 1 local local 0 add add pop pop local local 0 result result + + arg1 arg1 goto goto loop loop label label end end local local 0 result result return return Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 8: Virtual Machine, Part II slide 12

Function commands in the VM language function g nvars here here starts a function called g, g, which has has nvars local local variables call call g nargs invoke function g for for its its effect; nargs arguments have have already been been ed onto onto the the stack stack return terminate execution and and return control to to the 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 13

Function call-and-return conventions Calling function function function demo demo 3 3...... constant constant 7 7 constant constant 2 2 add add constant constant 3 3 call call mult mult...... called function aka callee (example) function function mult mult 1 1 constant constant 0 0 pop pop local local 0 0 result result (local (local 0) 0) 0 0 label label loop loop...... rest rest of of code code ommitted ommitted label label end end local local 0 0 result result return 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. Call-and-return programming convention The caller must the necessary argument(s), call the callee, and wait for it to return Before the callee terminates (returns), it must 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, 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 14

The function-call-and-return protocol The caller s view: Before Before calling calling a a function function g, g, I must must onto onto the the stack stack as as many many arguments arguments as as needed needed by by g Next, Next, I invoke invoke the the function function using using the the command command call callg nargs nargs After After g returns: returns: The The arguments arguments that that I ed ed before before the the call call have have disappeared disappeared from from the the stack, stack, and and a a return return value value (that (that always always exists) exists) appears appears at at the the top top of of the the stack stack All All my my memory memory segments segments (local, (local, argument, argument, this, this, that, that, pointer) pointer) are are the the same same as as before before the the call. call. function function g nvars nvars call call g nargs nargs return 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. The callee s (g s) view: When When I start start executing, executing, my my argument argumentsegment has has been been initialized initialized with with actual actual argument argument values values passed passed by by the the caller caller My My local localvariables segment segment has has been been allocated allocated and and initialized initialized to to zero zero The The static staticsegment segment that that I see see has has been been set set to to the the static staticsegment segment of of the the VM VM file file to to which which I belong, belong, and and the the working working stack stack that that I see see is is empty empty Before Before exiting, exiting, I must must a a value value onto onto the the stack stack and and then then use use the the command command return. return. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 8: Virtual Machine, Part II slide 15

The function-call-and-return protocol: the VM implementation view When function f calls function g, the VM implementation must: Save the return address within f s code: the address of the command just after the call 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. function function g nvars nvars call call g nargs nargs return return 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 16

Perspective Benefits of the VM approach Code transportability: compiling for different platforms requires replacing only the VM implementation Language inter-operability: code of multiple languages can be shared using the same VM Common software libraries Code mobility: Internet VM implementation over CISC platforms Some... language Some compiler... VM language VM imp. over RISC platforms Some Other language Some Other compiler CISC RISC machine machine... language language... VM emulator... Jack compiler Translator written in a high-level language Hack Some virtues of the modularity implied by the VM approach to program translation: Improvements in the VM implementation are shared by all compilers above it Every new digital device with a VM implementation gains immediate access to an existing software base New programming languages can be implemented easily using simple compilers Benefits of managed code: Security 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 24