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