Java Virtual Machine JVM Reasons for learning JVM

  • Slides: 19
Download presentation
Java Virtual Machine (JVM) • Reasons for learning JVM • Resource – The Java

Java Virtual Machine (JVM) • Reasons for learning JVM • Resource – The Java TM Virtual Machine Specification (2 nd Ed), by Tim Lindholm & Frank Yellin, Addison-Wesley, 1999 http: //java. sun. com/docs/books/vmspec/ by Neng-Fa Zhou 1

JVM Types and Words • byte, char, short, int, float, reference, and return. Address

JVM Types and Words • byte, char, short, int, float, reference, and return. Address in one word – boolean, byte, char, and short are converted to int – Special representations for byte, char, and short arrays are possible • long and double in two words by Neng-Fa Zhou 2

JVM Architecture PC Method area OPTOP Operand stack EP Environment LVARS Registers Local vars

JVM Architecture PC Method area OPTOP Operand stack EP Environment LVARS Registers Local vars Heap …. . Stack by Neng-Fa Zhou 3

Data Areas of JVM • Method area (shared by all threads) – One record

Data Areas of JVM • Method area (shared by all threads) – One record for each loaded class that contains: • Constant pool • Code for methods and constructors • Heap (shared by all threads) – One record for each class instance or array • Stack (one per thread) – Hold frames associated with method invocations by Neng-Fa Zhou 4

Stack Frame Structure Operand stack Environment Constant pool PC EP LVARS Local variables Operand

Stack Frame Structure Operand stack Environment Constant pool PC EP LVARS Local variables Operand stack: Evaluate expressions Pass method arguments and receive results Local variables: Addressed as word offsets Constant pool: Dynamic linking by Neng-Fa Zhou 5

The class File Format Class. File { u 4 magic; u 2 minor_version; u

The class File Format Class. File { u 4 magic; u 2 minor_version; u 2 major_version; u 2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u 2 access_flags; u 2 this_class; u 2 super_class; u 2 interfaces_count; u 2 interfaces[interfaces_count]; u 2 fields_count; field_info fields[fields_count]; u 2 methods_count; method_info methods[methods_count]; u 2 attributes_count; attribute_info attributes[attributes_count]; } by Neng-Fa Zhou 6

Names and Descriptors • Class, method, and field names are all stored symbolically as

Names and Descriptors • Class, method, and field names are all stored symbolically as strings • A descriptor is a string representing the type of a field or method • Class names – Class names are always fully qualified – Use forward slash rather than dot as the delimeter • Ex: Java. util. Vector => java/util/Vector by Neng-Fa Zhou 7

Descriptors • Primitive types – B, C, D, F, I, J(long), S, Z(boolean), V(void)

Descriptors • Primitive types – B, C, D, F, I, J(long), S, Z(boolean), V(void) • Arrays – Use [ • Classes – Lclassname • Ex: – Type: String value. Of(char[] , int offset, int count) – Descriptor: ([CII)Ljava/lang/String; by Neng-Fa Zhou 8

Constants • • • CONSTANT_Class CONSTANT_Fieldref CONSTANT_Methodref CONSTANT_Interface. Mthodref CONSTANT_String, CONSTANT_Integer CONSTANT_Float CONSTANT_Long CONSTANT_Double

Constants • • • CONSTANT_Class CONSTANT_Fieldref CONSTANT_Methodref CONSTANT_Interface. Mthodref CONSTANT_String, CONSTANT_Integer CONSTANT_Float CONSTANT_Long CONSTANT_Double CONSTANT_Name. And. Type CONSTANT_Utf 8 by Neng-Fa Zhou 9

field_info and method_info • field_info – – Access flags Name Descriptor Static values •

field_info and method_info • field_info – – Access flags Name Descriptor Static values • method_info – – by Neng-Fa Zhou Access flags Name Descriptor Code 10

Instruction Set • • Load and store (e. g. , iload, istore ldc, iconst_<i>)

Instruction Set • • Load and store (e. g. , iload, istore ldc, iconst_<i>) Arithmetic (e. g. , iadd, isub, imul, idiv, irem) Type conversion (e. g. , i 2 b, i 2 f, i 2 d) Object creation and manipulation (e. g, newarray, iaload, iastore , getfield, putfield) • Operand stack manipulation (e. g. , pop, dup_x 1, swap) • Control transfer (e. g. , goto, ifeq, tableswitch) • Method invocation and return (invokevirtual, invokeinterface, invokespecial, invokestatic, ireturn) • Exception handling and synchronization (e. g. , athrow) by Neng-Fa Zhou 11

Compiling for JVM Constants, Local Variables, and Control constructs void spin() { int i;

Compiling for JVM Constants, Local Variables, and Control constructs void spin() { int i; for (i = 0; i<100; i++) ; } Method 0 1 2 5 8 9 11 14 void spin() iconst_0 istore_1 goto 8 iinc 1 1 iload_1 bipush 100 if_icmplt 5 return // // Push int constant 0 Store into local variable 1 (i=0) First time through don't increment Increment local variable 1 by 1 (i++) Push local variable 1 (i) Push int constant 100 Compare and loop if less than (i < 100) Return void when done by Neng-Fa Zhou 12

Compiling for JVM Receiving Arguments and Invoking Methods int m 1(int a 1, int

Compiling for JVM Receiving Arguments and Invoking Methods int m 1(int a 1, int a 2) { return m 2(2, 3, a 1, a 2); } Method 0 1 3 5 6 7 10 int m 1() aload_0 bipush 2 bipush 3 iload_1 iload_2 invokevirtual #4 ireturn // Push local variable 0 (this) // Push int constant 2 // Push int constant 3 // Push local var a 1 // Push local var a 2 // Method Example. m(IIII)I // Return int on top of operand stack by Neng-Fa Zhou 13

Compiling for JVM Working with Class Instances My. Obj example() { My. Obj o

Compiling for JVM Working with Class Instances My. Obj example() { My. Obj o = new My. Obj(); return silly(o); } Method My. Obj example() 0 new #2 // Class My. Obj 3 dup 4 invokespecial #5 // Method My. Obj. <init>()V 7 astore_1 8 aload_0 9 aload_1 10 invokevirtual #4 // Method Example. silly(LMy. Obj; )LMy. Obj; 13 areturn by Neng-Fa Zhou 14

Compiling for JVM Arrays Creating arrays Manupulating arrays int x[] = new int[3]; x[2]

Compiling for JVM Arrays Creating arrays Manupulating arrays int x[] = new int[3]; x[2] = 0; x[0] = x[2]; 0 iconst_3 1 newarray int 3 astore_1 4 5 6 7 8 9 10 11 12 13 aload_1 iconst_2 iconst_0 iastore by Neng-Fa Zhou aload_1 iconst_0 aload_1 iconst_2 iaload iastore 15

Compiling for JVM Switches Method int choose. Near(int) 0 int choose. Near(int i) {

Compiling for JVM Switches Method int choose. Near(int) 0 int choose. Near(int i) { switch (i) { case 0: return case 1: return case 2: return default: return } } 0; 1; 2; -1; 28 29 30 31 32 33 34 35 by Neng-Fa Zhou iload_1 tableswitch 0 to 2: 0: 28 1: 30 2: 32 default: 34 iconst_0 ireturn iconst_1 ireturn iconst_2 ireturn iconst_m 1 ireturn 16

Compiling for JVM Manipulation of the Operand Stack public long next. Index(){ return index++;

Compiling for JVM Manipulation of the Operand Stack public long next. Index(){ return index++; } private long index = 0; Method long next. Index() 0 aload_0 // Push this 1 dup // Make a copy of it 2 getfield #4 // One of the copies of this is consumed // pushing long field index, // above the original this 5 dup 2_x 1 // The long on top of the operand stack is // inserted into the operand stack below the // original this 6 lconst_1 // Push long constant 1 7 ladd // The index value is incremented. . . 8 putfield #4 //. . . and the result stored back in the field 11 lreturn // The original value of index is left on // top of the operand stack, ready to be returned by Neng-Fa Zhou 17

Compiling for JVM Exception Handling void catch. One() { try. It. Out(); } catch

Compiling for JVM Exception Handling void catch. One() { try. It. Out(); } catch (Test. Exc e) { handle. Exc(e); } } Method void catch. One() 0 aload_0 1 invokevirtual #6 4 return 5 astore_1 6 aload_0 7 aload_1 8 invokevirtual #5 11 return Exception table: From To Target 0 4 5 // // // Beginning of try block Method Example. try. It. Out()V End of try block; normal return Store thrown value in local var 1 Push this Push thrown value Invoke handler method: Example. handle. Exc(LTest. Exc; )V Return after handling Test. Exc Type Class Test. Exc by Neng-Fa Zhou 18

Review Questions 1. Does the efficiency of a program change after an int variable

Review Questions 1. Does the efficiency of a program change after an int variable is changed to byte? 2. 3. 4. When does dynamic loading take place? When does dynamic linking take place? Why are run-time byte-code verification and type checking necessary? How are exceptions handled in JVM? How different is the JVM from a stack machine for Pascal? 5. 6. by Neng-Fa Zhou 19