CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

2,861 views

Published on

CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,861
On SlideShare
0
From Embeds
0
Number of Embeds
445
Actions
Shares
0
Downloads
86
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

  1. 1. Java Virtual MachineJava Virtual Machine ArchitectureArchitecture and APIsand APIs 22 Oct 200722 Oct 2007 National University of SingaporeNational University of Singapore School of ComputingSchool of Computing OH KWANG SHINOH KWANG SHIN
  2. 2. The JVM Architecture and APIs 22 Oct 2007 2 AgendaAgenda • Big Picture of Java • The Java Virtual Machine Architecture – Data Types & Storage – Java Instruction Set – Exceptions and Errors – Binary Classes – The Java Native Interface • Completing the Platform: APIs – Java Platforms – Java APIs: Serializability
  3. 3. The JVM Architecture and APIs 22 Oct 2007 3 Big Picture of JavaBig Picture of Java Java Source ( *.java ) Java Compiler ( javac ) Java Class ( *.class ) Java Platform Java Virtual Machine Java APIs Object.class
  4. 4. The JVM Architecture and APIs 22 Oct 2007 4 JVM – Data TypesJVM – Data Types • Primitive Data Types – int, char, byte, short, float, double – Implementation-dependent fashion • Defined according to the values they can take, not the number of bits of storage • Integer in the range -231 to +231 -1 – 32-bit words + two’s complement – Could use more storage bits • References – Can hold reference values • Reference value points to an object stored in memory
  5. 5. The JVM Architecture and APIs 22 Oct 2007 5 JVM – Data TypesJVM – Data Types • Objects and Arrays – Object • Composed of primitive data types and references that may point to other object – Array • Has a fixed number of elements • Elements of an array – Must all be of the same primitive type – Must all be references which point to objects of the same type
  6. 6. The JVM Architecture and APIs 22 Oct 2007 6 JVM – Data StorageJVM – Data Storage • Global Storage – Main memory, where globally declared variables reside • Local Storage – Temporary storage for variables that are local to a method • Operand Storage – Holds variables while they are being operated on by the functional instructions
  7. 7. The JVM Architecture and APIs 22 Oct 2007 7 JVM – Data StorageJVM – Data Storage • The Stack – Local and operand storage are allocated on the stack – Not arrays and objects, but only references and individual array elements on the stack – As each method is called, a stack frame is allocated Locals Operands Arguments Locals Operands Arguments Locals Operands Java Stack Structure
  8. 8. The JVM Architecture and APIs 22 Oct 2007 8 JVM – Data StorageJVM – Data Storage • Memory Hierarchy
  9. 9. The JVM Architecture and APIs 22 Oct 2007 9 JVM – Java Instruction SetJVM – Java Instruction Set • Instruction Formats (a) opcode (b) opcode index (c) opcode index1 index2 (d) opcode data (e) opcode data1 data2 Typical Bytecode Instruction Formats
  10. 10. The JVM Architecture and APIs 22 Oct 2007 10 JVM – Java Instruction SetJVM – Java Instruction Set • Data-Movement Instructions – Push constant values onto the stack •iconst1: single-byte instruction that pushes the integer constant 1 onto the stack •bipush data, sipush data1 data2, ldc index, ldc_w index1 index2, etc – Manipulate the stack entries •pop: pops the top element from the stack and discards it •swap: swaps the positions of the top two stack elements
  11. 11. The JVM Architecture and APIs 22 Oct 2007 11 JVM – Java Instruction SetJVM – Java Instruction Set • Data-Movement Instructions – Moves values : local storage ↔ operand stack • iload_1: takes the integer from local storage slot 1 and pushes it onto the stack • istore_1: moves data from the stack to local storage in the current stack frame – Deal with global memory data (objects, arrays) • new: new instance of the object is created on the heap and initialized. A reference to the object is pushed onto the stack • newarray: creates an array containing elements of a specified primitive type • getfield, putfield: accesses data held in objects
  12. 12. The JVM Architecture and APIs 22 Oct 2007 12 JVM – Java Instruction SetJVM – Java Instruction Set • Type Conversion – Convert one type of data item on the stack to another •i2f: pops an integer from the stack, converts it to a float, and pushes the float back onto the stack
  13. 13. The JVM Architecture and APIs 22 Oct 2007 13 JVM – Java Instruction SetJVM – Java Instruction Set • Functional Instructions – Take input operands, perform operations on them, and produce a result – Single byte: operands are always taken from the stack and results are placed onto the stack – Example • iadd: pops two integers from the stack  adds them  pushes the sum onto the stack • iand: pops two integers from the stack  performs a logical AND on them  pushes the result onto the stack • ishfl: pops two integers from the stack  shifts the top element left by an amount specified by the second element  pushes the result onto the stack
  14. 14. The JVM Architecture and APIs 22 Oct 2007 14 JVM – Java Instruction SetJVM – Java Instruction Set • Control Flow Instructions – ifeq data1 data2 • pops an integer from the stack  compares it with zero – True: PC relative branch to an offset found by concatenating the two data bytes – if_icmpeq data1 data2 • pops two integer values from the stack  compares the first with the second – True: PC relative branch to an offset found by concatenating the two data bytes – ifnull data1 data2 • pops a reference from the stack  check it for null – True: PC relative branch to an offset found by concatenating the two data bytes
  15. 15. The JVM Architecture and APIs 22 Oct 2007 15 JVM – Java Instruction SetJVM – Java Instruction Set • Example Program – javap: The Java Class File Disassembler class Rectangle { protected int sides []; ……………… ……………… public int perimeter () { return 2*(sides[0] + sides[1]); } public int area () { return (sides[0] * sides[1]); } } Java Source Rectangle.java public int perimeter(); Code: 0: iconst_2 1: aload_0 2: getfield #2; //Field sides:[I 5: iconst_0 6: iaload 7: aload_0 8: getfield #2; //Field sides:[I 11: iconst_1 12: iaload 13: iadd 14: imul 15: ireturn Disassembled Code
  16. 16. The JVM Architecture and APIs 22 Oct 2007 16 Rectangle Objectsides[0] JVM – Java Instruction SetJVM – Java Instruction Set • Example Program Scenario public int perimeter () { return 2*(sides[0] + sides[1]); } public int perimeter(); Code: 0: iconst_2 1: aload_0 2: getfield #2; 5: iconst_0 6: iaload 7: aload_0 8: getfield #2; 11: iconst_1 12: iaload 13: iadd 14: imul 15: ireturn Constant 2 Operand Stack Pushes a constant 2 onto the operand stack Pushes local variable 0 onto the stack (argument  reference to the rectangle object) Pushes the reference to the sides array sides Array Pushes index number 0 onto the stack Index Number 0 Load element 0 from the array sides Pushes local variable 0 onto the stack (argument  reference to the rectangle object) Pushes the reference to the sides array Pushes index number 1 onto the stack Load element 1 from the array sides Rectangle Object sides Array Index Number 1 sides[1] Pushes addition of the top two stack elements Pushes multiplication of top two stack elements Returns with the integer result on top of stack sides[0]+sides[1] 2*( sides[0]+sides[1])
  17. 17. The JVM Architecture and APIs 22 Oct 2007 17 JVM – Exceptions & ErrorsJVM – Exceptions & Errors • All exceptions must be handled somewhere – No global way to turn them off – If there is no handler, then calling method takes over – Overall program robustness consideration • Errors – Caused by limitation of the VM implementation or VM bugs • Exceptions – Caused by program behavior that occurs dynamically – as the program executes
  18. 18. The JVM Architecture and APIs 22 Oct 2007 18 JVM – Exceptions & ErrorsJVM – Exceptions & Errors • Exception table with each method – Makes it possible to specify an exception handler, depending on where an exception occurs From To Target Type 8 12 96 Arithmetic Exception Exception Table
  19. 19. The JVM Architecture and APIs 22 Oct 2007 19 JVM – Exceptions & ErrorsJVM – Exceptions & Errors • Example of exception table class Rectangle { protected int sides []; ……………… public int perimeter () { try { return 2*(sides[0] + sides[1]); } catch(ArithmeticException e) { return -1; } } public int area () { return (sides[0] * sides[1]); } } public int perimeter(); Code: 0: iconst_2 1: aload_0 2: getfield #2; //Field sides:[I 5: iconst_0 6: iaload 7: aload_0 8: getfield #2; //Field sides:[I 11: iconst_1 12: iaload 13: iadd 14: imul 15: ireturn 16: astore_1 17: iconst_m1 18: ireturn Exception table: from to target type 0 15 16 Class java/lang/ArithmeticException
  20. 20. The JVM Architecture and APIs 22 Oct 2007 20 JVM – Binary ClassesJVM – Binary Classes • Binary class – Typically included in a class file – Code + Metadata • Metadata is a detailed specification of the data structures and their relationships – Can be loaded on demand • At the time they are needed by the program • Saves bandwidth for loading binary classes that are never used • Allows a Java program to start up quickly
  21. 21. The JVM Architecture and APIs 22 Oct 2007 21 JVM – Binary Class FormatJVM – Binary Class Format Magic Number Version Information Constant Pool Size Constant Pool Access Flags This Class Super Class Interface Count Interfaces Field Count Field Information Method Count Methods Attribute Count Attributes • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • Identifier of java binary class Minor and major version numbers of this class file Number of entries in the constant pool + 1 All the constant values and references Provide access information Name of this class (Valid index of Constant Pool) Name of super class (Valid index of Constant Pool or 0) Number of direct superinterfaces Number of references to the superinterfaces (Valid index into the Constant Pool table) Number of field_info structures in the Field Information field_info structures giving a complete description of a field in this class or interface Number of method_info structures in the Methods method_info structure giving a complete description of a method in this class or interface Number of attributes in the Attributes Detailed information regarding the other components listed earlier
  22. 22. The JVM Architecture and APIs 22 Oct 2007 22 JVM – Java Native InterfaceJVM – Java Native Interface • JNI (Java Native Interface) – Allows java code and native compiled code to interoperate
  23. 23. The JVM Architecture and APIs 22 Oct 2007 23 Java PlatformJava Platform • Java Platform = JVM + Java APIs • Java APIs – A set of standard libraries – Provide most of the features that are visible to users and software developers • Support for secure network computing, component-based software, graphical user interfaces (GUIs), etc.
  24. 24. The JVM Architecture and APIs 22 Oct 2007 24 Java PlatformJava Platform • J2EE, J2SE and J2ME
  25. 25. The JVM Architecture and APIs 22 Oct 2007 25 Java APIs - SerializationJava APIs - Serialization • Process of converting an object into an implementation-independent form Object Platform A- Dependent Representation Serialization Deserialization Object Platform B- Dependent Representation Platform- Independent Representation Persistent Storage Network
  26. 26. The JVM Architecture and APIs 22 Oct 2007 26 Java APIs - ThreadJava APIs - Thread • Multithreading support is provided by Java libraries that are part of java.lang • Monitors – Support the synchronization among threads – Lock and two Java bytecode instructions • Lock – Associated with each object and each class – Operated as a counter, rather than flag • Two Java bytecode instructions – Monitorenter – monitorexit
  27. 27. The JVM Architecture and APIs 22 Oct 2007 27 210 Java APIs - ThreadJava APIs - Thread public int perimeter () { synchronized (sides) { synchronized (sides) { return 2*(sides[0] + sides[1]); } } } public int perimeter(); Code: ……… 6: monitorenter ……… 13: monitorenter 14: iconst_2 ……… 35: aload_2 36: monitorexit ……… 41: aload_1 42: monitorexit ……… Lock of sides Acquires the lock for the object, the lock is incremented Acquiring thread may already hold the lock , the lock is incremented Decrements the lock for the object Decrements the lock for the object If the lock becomes zero, then it is released and can be acquired by a waiting thread (if there is one)
  28. 28. The JVM Architecture and APIs 22 Oct 2007 28 ReferencesReferences • James E. Smith and Ravi Nair, Virtual Machines: Versatile Platform for Systems and Processes. Morgan Kaufmann Publishers, 2005. • Java Technology – The Source for Java Developers Available: http://java.sun.com • Bill Venners, Inside the Java 2 Virtual Machine, McGraw- Hill, 1999. • Tim Lindholm and Frank Yellin, The JavaTM Virtual Machine Specification – Second Edition, Addison-Wesley Longman Publishing Co., Inc., Boston, MA, 1999.

×