cs6270 virtual machines - java virtual machine architecture and apis

29
Java Virtual Java Virtual Machine Machine Architecture Architecture and APIs and APIs 22 Oct 2007 22 Oct 2007 National University of Singapore National University of Singapore School of Computing School of Computing OH KWANG SHIN OH KWANG SHIN

Upload: kwangshin-oh

Post on 22-Apr-2015

1.857 views

Category:

Technology


0 download

DESCRIPTION

CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

TRANSCRIPT

Page 1: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

Java Virtual MachineJava Virtual Machine ArchitectureArchitecture

and APIsand APIs

22 Oct 200722 Oct 2007

National University of SingaporeNational University of SingaporeSchool of ComputingSchool of Computing

OH KWANG SHINOH KWANG SHIN

Page 2: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 3: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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 VirtualMachine

Java APIs

Object.class

Page 4: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 5: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 6: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 7: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 8: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

The JVM Architecture and APIs 22 Oct 2007 8

JVM – Data StorageJVM – Data Storage• Memory Hierarchy

Page 9: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 10: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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 disca

rds it•swap: swaps the positions of the top two stack ele

ments

Page 11: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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 ini

tialized. 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

Page 12: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 13: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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 a

nd 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

Page 14: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 15: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

The JVM Architecture and APIs 22 Oct 2007 15

JVM – Java Instruction SetJVM – Java Instruction Set• Example Program

– javap: The Java Class File Disassemblerclass 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

Page 16: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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 stackPushes 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 0Load element 0 from the array sidesPushes local variable 0 onto the stack (argument reference to the rectangle object)Pushes the reference to the sides arrayPushes index number 1 onto the stackLoad element 1 from the array sides

Rectangle Object

sides Array

Index Number 1

sides[1]

Pushes addition of the top two stack elementsPushes multiplication of top two stack elementsReturns with the integer result on top of stack

sides[0]+sides[1]

2*(sides[0]+sides[1])

Page 17: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 18: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 19: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 20: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 21: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

The JVM Architecture and APIs 22 Oct 2007 21

JVM – Binary Class FormatJVM – Binary Class FormatMagic 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 superinterfacesNumber of references to the superinterfaces(Valid index into the Constant Pool table)Number of field_info structures in the Field Informationfield_info structures giving a complete description of a field in this class or interfaceNumber of method_info structures in the Methods method_info structure giving a complete description of a method in this class or interfaceNumber of attributes in the Attributes

Detailed information regarding the other components listed earlier

Page 22: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 23: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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.

Page 24: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

The JVM Architecture and APIs 22 Oct 2007 24

Java PlatformJava Platform• J2EE, J2SE and J2ME

Page 25: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

The JVM Architecture and APIs 22 Oct 2007 25

Java APIs - SerializationJava APIs - Serialization• Process of converting an object into

an implementation-independent form

ObjectPlatform A-Dependent

Representation

Serialization Deserialization

ObjectPlatform B-Dependent

Representation

Platform-Independent

Representation

Persistent Storage

Network

Page 26: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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

Page 27: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

The JVM Architecture and APIs 22 Oct 2007 27

210

Java APIs - ThreadJava APIs - Threadpublic 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)

Page 28: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

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 DevelopersAvailable: 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.

Page 29: CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs