Download presentation
Presentation is loading. Please wait.
1
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 by Neng-Fa Zhou
2
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
3
JVM Architecture PC Method area OPTOP EP Heap LVARS ….. Registers
Operand stack EP Environment Heap Local vars LVARS ….. Registers Stack by Neng-Fa Zhou
4
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
5
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 LVARS Local variables by Neng-Fa Zhou
6
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
7
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
8
Descriptors Primitive types Arrays Classes Ex: Use [ Lclassname
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
9
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
10
field_info and method_info
Access flags Name Descriptor Code field_info Access flags Name Descriptor Static values by Neng-Fa Zhou
11
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
12
Compiling for JVM Constants, Local Variables, and Control constructs
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
13
Compiling for JVM Receiving Arguments and Invoking Methods
int m1(int a1, int a2) { return m2(2,3,a1,a2); } Method int m1() aload_0 // Push local variable 0 (this) bipush 2 // Push int constant bipush 3 // Push int constant iload_ // Push local var a iload_ // Push local var a2 7 invokevirtual #4 // Method Example.m(IIII)I ireturn // Return int on top of operand stack by Neng-Fa Zhou
14
Compiling for JVM Working 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
15
Compiling for JVM Arrays
Creating arrays Manupulating arrays int x[] = new int[3]; 0 iconst_3 1 newarray int 3 astore_1 x[2] = 0; 4 aload_1 5 iconst_2 6 iconst_0 7 iastore x[0] = x[2]; 8 aload_ iconst_0 10 aload_1 11 iconst_2 12 iaload 13 iastore by Neng-Fa Zhou
16
Compiling for JVM Switches
Method int chooseNear(int) 0 iload_ tableswitch 0 to 2: 0: : : default: iconst_ ireturn iconst_ ireturn iconst_ ireturn iconst_m 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
17
Compiling for JVM Manipulation of the Operand Stack
public long nextIndex(){ return index++; } private long index = 0; Method long nextIndex() 0 aload_ // 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_x // The long on top of the operand stack is // inserted into the operand stack below the // original this 6 lconst_ // 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
18
Compiling for JVM Exception Handling
void catchOne() { try { tryItOut(); } catch (TestExc e) { handleExc(e); } Method void catchOne() 0 aload_ // Beginning of try block 1 invokevirtual #6 // Method Example.tryItOut()V 4 return // End of try block; normal return 5 astore_ // Store thrown value in local var 1 6 aload_ // Push this 7 aload_ // 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 Class TestExc by Neng-Fa Zhou
19
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.