The Java programming environment Introduction to JVM Based on material produced by Bill Venners 1 2 The Java platform The role of the virtual machime • byte code generated by the Java front-end is an intermediate representation (IR) – clean and compact – platform-independent Local or Local or Remote Remote 3 4 1
Java compilation model Java bytecode format • void spin () { int i; for (i = 0; i < 100; i++) {;} } 0 iconst_0 // push int constant 0 1 istore_1 // store into var 1 "i=0" 2 goto 8 // first time: don't incr 5 iinc 1 1 // incr var 1 by 1 "i++" 8 iload_1 // push local var 1 "i" 9 bipush 100 // push byte const "100" 11 if_icmplt 5 // loop if less "i < 100" 14 return // return void when done • branch instructions use relative displacement, i.e., add/subtract from PC => easy combination • disassembled code uses absolute pseudo-labels 5 6 Java bytecode basics Back-end transformation and execution • no run-time tag checking (but objects have metadata) (1) simple JVM • "untyped" local variables, reused for different types – byte code interpretation, including resolution of • type tags are carried along the instructions symbolic references: • the verification phase of class loading ensures • finding the entity identified by a text symbol validity: types are OK, no operand stack overflow.. and replacing it with a direct reference (2) JIT (Just-In-Time) compiler • bytecode categories – method byte codes are compiled into native – arithmetic: add, sub, mul, rem, div ( typed versions) machine code the first time they are invoked – operand stack management: load/store, dup, swap – the machine code is cached for subsequent – control transfer: goto <offset>, if_icmpeq, ifeq, .. invocation – type conversions: i2l, i2f, l2f, f2i, d2i, int2byte, etc. – compilation overhead & requires more memory – method invocation and return: invokevirtual, invokestatic, ireturn, lreturn, return, etc. (3) adaptive optimization: the interpreter monitors the program, compiling only heavily used parts.. – throwing exceptions, monitors, etc.. 7 8 2
Shared data areas The Java Virtual Machine • each JVM has one of each: – method area : byte code and class (static) data storage – heap : object storage 9 10 Example representation of objects in heap: Thread data areas - or can use handle ptrs pointing to an inderect Frame in Frame in handle pool Execution Execution • every thread has its own stack of call frames • the heap is garbage collected • in a frame, a fixed-sized stack for expr evaluation • each JVM has its own heap - supports isolation 11 12 3
Recommend
More recommend