220 likes | 335 Views
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/. JVM Types and Words.
E N D
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,1999http://java.sun.com/docs/books/vmspec/ by Neng-Fa Zhou
JVM Types and Words • byte, char, short, int, float, reference, and returnAddress 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
JVM Architecture PC Method area OPTOP Operand stack EP Environment Heap Local vars LVARS ….. Registers Stack by Neng-Fa Zhou
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
Stack Frame Structure Operand stack: Evaluate expressions Pass method arguments and receive results Local variables: Addressed as word offsets Constant pool: Dynamic linking Operand stack Environment Constant pool PC EP LVARSLocal variables by Neng-Fa Zhou
The class File Format ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } by Neng-Fa Zhou
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
Descriptors • Primitive types • B, C, D, F, I,J(long), S, Z(boolean), V(void) • Arrays • Use [ • Classes • Lclassname • Ex: • Type:String valueOf(char[] , int offset, int count) • Descriptor:([CII)Ljava/lang/String; by Neng-Fa Zhou
Constants • CONSTANT_Class • CONSTANT_Fieldref • CONSTANT_Methodref • CONSTANT_InterfaceMthodref • CONSTANT_String, • CONSTANT_Integer • CONSTANT_Float • CONSTANT_Long • CONSTANT_Double • CONSTANT_NameAndType • CONSTANT_Utf8 by Neng-Fa Zhou
field_info Access flags Name Descriptor Static values method_info Access flags Name Descriptor Code field_info and method_info by Neng-Fa Zhou
Instruction Set • Load and store (e.g., iload, istore ldc, iconst_<i>) • Arithmetic (e.g., iadd, isub, imul, idiv, irem) • Type conversion (e.g., i2b, i2f, i2d) • Object creation and manipulation(e.g, new, newarray, iaload, iastore , getfield, putfield) • Operand stack manipulation (e.g., pop, dup, dup_x1, 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
Compiling for JVMConstants, Local Variables, and Controlconstructs void spin() { int i; for (i = 0;i<100;i++) ; } Method void spin() 0 iconst_0 // Push int constant 0 1 istore_1 // Store into local variable 1 (i=0) 2 goto 8 // First time through don't increment 5 iinc 1 1 // Increment local variable 1 by 1 (i++) 8 iload_1 // Push local variable 1 (i) 9 bipush 100 // Push int constant 100 11 if_icmplt 5 // Compare and loop if less than (i < 100) 14 return // Return void when done by Neng-Fa Zhou
Compiling for JVMReceiving Arguments and Invoking Methods int m1(int a1, int a2) { return m2(2,3,a1,a2);} Method int m1() 0 aload_0 // Push local variable 0 (this) 1 bipush 2 // Push int constant 2 3 bipush 3 // Push int constant 3 5 iload_1 // Push local var a16 iload_2 // Push local var a27 invokevirtual #4 // Method Example.m(IIII)I 10 ireturn // Return int on top of operand stack by Neng-Fa Zhou
Compiling for JVMWorking with Class Instances MyObj example() { MyObj o = new MyObj(); return silly(o); } Method MyObj example() 0 new #2 // Class MyObj 3 dup 4 invokespecial #5 // Method MyObj.<init>()V 7 astore_1 8 aload_0 9 aload_1 10 invokevirtual #4 // Method Example.silly(LMyObj;)LMyObj; 13 areturn by Neng-Fa Zhou
Compiling for JVMArrays Creating arrays Manupulating arrays int x[] = new int[3];0 iconst_31 newarray int3 astore_1 x[2] = 0;4 aload_1 5 iconst_2 6 iconst_0 7 iastore x[0] = x[2]; 8 aload_1 9 iconst_010 aload_111 iconst_2 12 iaload13 iastore by Neng-Fa Zhou
Compiling for JVMSwitches Method int chooseNear(int) 0 iload_1 1 tableswitch 0 to 2: 0: 28 1: 30 2: 32 default:34 28 iconst_0 29 ireturn 30 iconst_1 31 ireturn 32 iconst_2 33 ireturn 34 iconst_m1 35 ireturn int chooseNear(int i) { switch (i) { case 0: return 0; case 1: return 1; case 2: return 2; default: return -1; } } by Neng-Fa Zhou
Compiling for JVMManipulation of the Operand Stack public long nextIndex(){ return index++; } private long index = 0; Method long nextIndex() 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 dup2_x1 // 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
Compiling for JVMException Handling void catchOne() { try { tryItOut(); } catch (TestExc e) { handleExc(e); } } Method void catchOne() 0 aload_0 // Beginning of try block 1 invokevirtual #6 // Method Example.tryItOut()V 4 return // End of try block; normal return 5 astore_1 // Store thrown value in local var 1 6 aload_0 // Push this 7 aload_1 // Push thrown value 8 invokevirtual #5 // Invoke handler method: // Example.handleExc(LTestExc;)V 11 return // Return after handling TestExc Exception table: From To Target Type 0 4 5 Class TestExc by Neng-Fa Zhou
Review Questions • Does the efficiency of a program change after an int variable is changed to byte? • 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? by Neng-Fa Zhou