Virtual Machine (Part II)

Similar documents
Virtual Machine (Part II)

Chapter 8: Virtual Machine II: Program Control

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

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

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:

7. The Virtual Machine II: Flow Control 1

Introduction: Hello, World Below

7. The Virtual Machine

Chapter 11: Compiler II: Code Generation

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

Chapter 10: Compiler I: Syntax Analysis

Compiler II: Code Generation Human Thought

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

Compiler I: Sytnax Analysis

Course Administration

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Introduction: From Nand to Tetris

Assembler Human Thought

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

Assembler. Building a Modern Computer From First Principles.

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

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 (

CSCE 5610: Computer Architecture

Chapter 6: Assembler

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CS61C : Machine Structures

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

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

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

Chapter 9: High Level Language

10. The Compiler II: Code Generation 1

ECE232: Hardware Organization and Design

Assembly Language: Function Calls

Wednesday, October 15, 14. Functions

CSE Lecture In Class Example Handout

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

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

SRC Assembly Language Programming - III

Digital Forensics Lecture 3 - Reverse Engineering

Lecture 5: Procedure Calls

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

Compiler I: Syntax Analysis

CS61C : Machine Structures

Implementing Subroutines. Outline [1]

Subroutines and Stack Usage on the MicroBlaze. ECE 3534 Microprocessor System Design

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

CS 61c: Great Ideas in Computer Architecture

CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays. Kevin McDonnell Stony Brook University CSE 220

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

Machine (Assembly) Language

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

Computer Architecture

Instruction Set Architecture

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

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

Implementing Procedure Calls

CS64 Week 5 Lecture 1. Kyle Dewey

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

System Software Assignment 1 Runtime Support for Procedures

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

Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to

Run-Time Data Structures

6.004 Tutorial Problems L3 Procedures and Stacks

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Lecture 7: Procedures

Functions in MIPS. Functions in MIPS 1

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

COE608: Computer Organization and Architecture

Compiler Construction

MIPS Procedure Calls. Lecture 6 CS301

EE 361 University of Hawaii Fall

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

CS 316: Procedure Calls/Pipelining

Assembly Language: Function Calls

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.

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

ECS 142 Project: Code generation hints

Systems I. Machine-Level Programming V: Procedures

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

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

Assembly Language: Function Calls" Goals of this Lecture"

CS153: Compilers Lecture 8: Compiling Calls

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

CS240: Programming in C

Lecture 6: Assembly Programs

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

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Topic 7: Activation Records

Assembly Language: Function Calls" Goals of this Lecture"

G Programming Languages - Fall 2012

Transcription:

Harvard University CS 101 Fall 2005, Shimon Schocken Virtual Machine (Part II) Elements of Computing Systems 1 Virtual Machine II (Ch. 8) Where we are at: Human Thought Abstract design Chapters 9, 12 H.L. Language & Operating Sys. Compiler Chapters 10-11 Virtual Machine Software hierarchy VM Translator Chapters 7-8 Assembly Language Assembler Chapter 6 Machine Language Computer Architecture Chapters 4-5 Hardware hierarchy Hardware Platform Gate Logic Chapters 1-3 Chips & Logic Gates Electrical Engineering Physics Elements of Computing Systems 2 Virtual Machine II (Ch. 8)

Program structure class Main { class Main { static int x; static int x; function void main() { function void main() { // Input and multiply 2 numbers // Input and multiply 2 numbers var int a, b; var int a, b; let a = Keyboard.readInt( Enter a number ); let a = Keyboard.readInt( Enter a number ); let b = Keyboard.readInt( Enter a number ); let b = Keyboard.readInt( Enter a number ); x = mult(a,b); x = mult(a,b); return; return; // Multiplies two numbers. // Multiplies two numbers. function int mult(int x, int y) { function int mult(int x, int y) { var int result, j; var int result, j; let result = 0; let j = y; let result = 0; let j = y; while not(j = 0) { while not(j = 0) { let result = result + x; let result = result + x; let j = j 1; let j = j 1; return result; return result; Elements of Computing Systems 3 Virtual Machine II (Ch. 8) Lecture plan 2 x = ( b + b 4 a c) / 2a if if ~(a ~(a = 0) 0) x = (-b (-b + sqrt(power(b,2) 4 * a * c)) c)) / (2 (2 * a) a) else else x = - c / b In order to implement this abstraction we have to know how to handle: Arithmetic operations (last lecture) Boolean operations (last lecture) Program flow (this lecture, easy) Subroutines (this lecture, medium/rare) In our system: all these abstractions can delivered at the VM level. Elements of Computing Systems 4 Virtual Machine II (Ch. 8)

Program flow commands label c goto goto c if-goto c Example: function function mult mult 2 push push constant constant 0 pop pop local push push argument argument 1 pop pop local label label loop loop push push local push push constant constant 0 eq eq if-goto if-goto end end push push local push push argument argument 0 add add pop pop local push push local push push constant constant 1 sub sub pop pop local goto goto loop loop label label end end push push local return return Elements of Computing Systems 5 Virtual Machine II (Ch. 8) Subroutine calling: high level if if ~(a ~(a = 0) 0) x = (-b (-b + sqrt(power(b,2) 4 * a * c)) c)) / (2 (2 * a) a) else else x = - c / b The most important abstraction delivered by high level s: The given can be extended at will by user-defined commands ( = subroutines / functions / methods...) The primitive commands and the user-defined commands have the same look-and-feel A well-deigned 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 6 Virtual Machine II (Ch. 8)

Example Call-and-return convention The caller pushes the arguments, calls the callee, then waits for it to return Before the callee terminates (returns), it must push a return value After the callee returns, its resources must be recycled, and the caller s state must be re-instated Net effect The arguments are replaced by the return value (as with primitive operations) The overall magic is delivered by the VM implementation The VM implementation manages everything away from the programmer s view Stack implementation a great CS gem. Elements of Computing Systems 7 Virtual Machine II (Ch. 8) Program structure and memory segments Elements of Computing Systems 8 Virtual Machine II (Ch. 8)

The calling protocol (blue = unfinished magic business) The caller function view: function function f nvars nvars call call f nargs nargs return return Before Before calling calling the the function, function, I must must push push as as many many arguments arguments as as necessary necessary onto onto the the stack stack Next, Next, I invoke invoke the the function function using using the the call callcommand command After After the the called called function function returns: returns: The The arguments arguments that that I pushed pushed 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 (argument, (argument, local, local, static, static, ) ) are are the the same same as as before before the the call. call. The called function 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 returning, returning, I must must push push a a value value onto onto the the stack. stack. Elements of Computing Systems 9 Virtual Machine II (Ch. 8) Implementation Elements of Computing Systems 10 Virtual Machine II (Ch. 8)

Implementation view of the calling protocol When function f calls function g, I must: function function f nvars nvars call call f nargs nargs return return Save the return address and the segment pointers 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, I must: Clear the arguments and other junk from the stack Restore the segments of f Transfer control back to f (jump to the saved return address). Elements of Computing Systems 11 Virtual Machine II (Ch. 8) The global stack the VM implementation s housekeeping memory frames of all the functions up the calling chain argument 0 At any point of time, only one function is running argument 1 argument n-1 return address saved saved saved THIS saved THAT local k-1 arguments pushed for the current function saved state of the calling function, used to return to and restore the segments of, the calling function upon returning from the current function local variables of the current function Shaded areas: not seen by the current function The current function sees only the tip of the stack The rest of the stack holds the frozen states of all the functions up the calling hierarchy Physical storage details depend on the VM implementation. working stack of the current function Elements of Computing Systems 12 Virtual Machine II (Ch. 8)

Example: a typical calling scenario function p(...) {...... fact(4)... function fact(n) { vars result,j; result=1; j=1; while j<=n { result=mult(result,j); return result; function mult(x,y) { vars sum,j; sum=0; j=y; while j>0 { sum=sum+x; return sum; p call fact(4) waiting time call mult(1,2) call mult(2,3) call mult(6,4) 24 fact waiting waiting waiting return 2 6 24 mult return mult return mult return Elements of Computing Systems 13 Virtual Machine II (Ch. 8) Behind the scene: just before "call mult" just after mult is entered just after mult returns argument 0 argument 0 argument 0 return addr return addr return addr function p(...) {...... fact(4)... THIS THAT THIS THAT THIS THAT function fact(n) { vars result,j; result=1; j=1; while j<=n { result=mult(result,j); return result; working stack argument 0 argument 1 (mult) (mult) working stack argument 0 (mult) argument 1 (mult) return addr working stack return value function mult(x,y) { vars sum,j; sum=0; j=y; while j>0 { sum=sum+x; return sum; THIS THAT (mult) (mult) Elements of Computing Systems 14 Virtual Machine II (Ch. 8)

Implementing the call f n command frames of all the functions up the calling chain argument 0 argument 1 argument n-1 return address saved saved saved THIS saved THAT local k-1 Elements of Computing Systems 15 Virtual Machine II (Ch. 8) Implementing the function f k command frames of all the functions up the calling chain argument 0 argument 1 argument n-1 return address saved saved saved THIS saved THAT local k-1 Elements of Computing Systems 16 Virtual Machine II (Ch. 8)

Implementing the return command frames of all the functions up the calling chain argument 0 argument 1 argument n-1 return address saved saved saved THIS saved THAT local k-1 Elements of Computing Systems 17 Virtual Machine II (Ch. 8) Bootstrap code = 256 Call Sys.init // // initialize the the stack pointer to to 0x0100 // // an an initialization function Sys.init should call f and then enter an infinite loop In the Jack platform: Sys.init is part of the OS f is Main.main (must exist in any Jack application). Elements of Computing Systems 18 Virtual Machine II (Ch. 8)

VM implementation over Hack: a monolithic assembly program with lots of weird symbols: Elements of Computing Systems 19 Virtual Machine II (Ch. 8) Proposed API Elements of Computing Systems 20 Virtual Machine II (Ch. 8)

Perspective Some Some Other Jack VM Pros Code transportability: compiling for different platforms require replacing only the VM implementation Language inter-operability: code of multiple s can be shared using the same VM Common software libraries Modularity: 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 Simple compilers CISC machine CISC machine VM implementation over CISC platforms Elements of Computing Systems 21 Virtual Machine II (Ch. 8) RISC machine RISC machine Some compiler VM imp. over RISC platforms Some Other compiler Intermediate code VM emulator other digital platforms, each equipped with its VM implementation VM Cons Performance History P-code Java.Net Jack compiler VM imp. over the Hack platform written in a high-level Any computer Hack machine Hack computer