chapter 16 java virtual machine. to compile a java program in simple.java, enter javac simple.java...

53
Chapter 16 Java Virtual Machine

Upload: nyasia-chisman

Post on 02-Apr-2015

240 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Chapter 16

Java Virtual Machine

Page 2: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

To compile a java program in Simple.java, enter

javac Simple.java

javac outputs Simple.class, a file that contains bytecode (machine language for the Java Virtual Machine (JVM).

To run Simple.class, enter

java Simple

programdata

Page 3: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

java (the Java interpreter) makes your computer act like the JVM so it can execute bytecode.

Page 4: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Why is Java slow?

• Interpretation of bytecode can involve a lot of overhead.• JVM dynamically links classes versus C++ where linking

is done at compile time. Slow start-up.• JVM performs checks during loading, linking, and

executing bytecode (array bounds, invalid casts, etc).

Page 5: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Why is Java good for the Web?

• Bytecode is space efficient (transport).• Bytecode is portable to any system with a java

interpreter.• Java applets are safe to run. For example, you can’t

access the file system from an applet.

Page 6: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Four parts of the JVM

• Execution engine (contains pc register)• Method area (contains information on each class:

bytecode, static variables, information needed for verification and linking).

• Java stack (the run time stack). Each frame of the Java stack contains a local variable array and an operand stack.

• heap (contains data associated with objects). Periodically, garbage collection deallocates objects in the heap that are no longer referenced.

Page 7: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Comparison of JVM and Traditional Program Memory Allocation

• Execution engine: not part of comparison; acts like CPU

• Method area: Code and Globals• Java Stack (the run time stack). The Stack• Heap (contains data associated with objects). The Heap

Page 8: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

There are two types of stacks in the JVM

• The Java stack• The Java stack consists of frames, one frame for each

method invocation. Each frame contains an operand stack and a local variable array.

Page 9: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Local variable array

Contains local variables indexed starting from 0. For example, the first slot of the local variable array is called local variable 0. This array also contains parameters.

All primitives and references fit in this array.

byte and short are sign-extended

char is zero-extended

double and long use two slots in the array

Page 10: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Operand stack

Used to hold operands and results during the execution of instructions.

Example: iadd: pops two operands of the Operand Stack, adds them and pushes the result back onto the Operand Stack

Page 11: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Operand stack

The JVM is said to have a Stack Architecture because of the Operand Stack, not the Java Stack.

Page 12: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

JVM is a Stack Archtitecture

32 bit

store

loadput

stat

ic

getst

atic

(indexed)

Page 13: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Most instructions consist of an opcode only and are 1 byte long. For example,

iconst_0, iconst_1, iconst_2, iconst_3, iconst_4, iconst_5

which push 0, 1, 2, 3, 4, and 5, respectively, onto the operand stack.

The more common operations are performed by such single-byte opcode-only instructions.

The first letter of the opcode represents the argument data type - i for integer,a for reference

Page 14: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Some instructions require an operand. For example,

bipush 6

which pushes 6 and uses 2 bytes – one for opcode and one for operand. This instruction consists of the opcode for bipush (byte integer push) followed by a byte containing 6. A 4-byte, sign-extended version is actually what gets pushed.

To push a number greater than 127, use sipush (short int push). For example,

sipush 130

The range of values that can be operands for sipush is [-32768.32767]

Page 15: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Symbolic bytecode that adds three numbers

Page 16: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

The initial letter of some mnemonics indicates the data type. For example, iadd, dadd, fadd, ladd.

a: reference

d: double

f: float

i: integer

ia: integer array

l: long

The actual operation takes place inthe Execution Engine.

Page 17: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

iload_0

pushes the value in local variable 0 (i.e., it pushes the value from the first slot of the local variable array onto the operand stack; first 4 slots only)

iload 4

pushes the value in local variable 4.

Load instructions on the JVM

one byte

two bytes

Page 18: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

istore_0

pops and stores the value on top of the operand stack into local variable 0.

istore 4

pops and stores the value on top of the operand stack into local variable 4.

Store instructions on the JVM

Page 19: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

A static variable in Java is a variable associated with a class rather than an object. It is shared by all objects of its class.

A static method in Java is a method that can be called via its class.

Page 20: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

The getstatic and putstatic instructions transfer values between the top of the operand stack and static variables.

The operand that appears in getstatic and putstatic instructions is an index into the constant pool. For example,

getstatic 2

pushes a value at location 2 in the constant pool onto the operand stack.

Page 21: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Invoking a static method with invokestatic instruction

• Calling environment pushes args onto its stack beginning with receiver (invokevirtual) or constant pool ref to class (invokestatic).

• Creates frame for the called method and pushes it onto the Java stack.

• Pops the arguments from the caller’s operand stack and places them in the called method’s local variable array starting from local variable1. Local variable 0 is used to hold a reference to the object or the class as appropriate.

• Transfers control to the called method.

Page 22: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

invokestatic

Argument is index of constant pool ptr to actual method

this

pushed first

Page 23: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Returning a value to the calling method with the ireturn instruction

The value returned is pushed onto the calling method’s operand stack.

s

Page 24: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

iconst_3

return

Page 25: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Implementation of the execution engine

Page 26: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

sp = operandtop-of-stack*sp == sp[0]

ap = localvariable array

trivial to “decode”

Page 27: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

The wisdom of using a stack architecture for the JVM

• A stack architecture on a simulated machine is no slower than a register architecture (on a simulated machine).

• Register architecture is faster only if the registers are• real registers.• On a simulated machine even registers are in memory.• Bytecode is very compact which is important for a web

programs. Few extra operands for operations.

stack == memory == slow!!

Page 28: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

A simple Java program follows, along with its bytecode

Page 29: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

notice: return address handled by JVM; not on our stack

Page 30: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode
Page 31: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

A formatted display of the constant pool for our simple program follows.

Page 32: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

type of entry what class; what thing

the actual class (reference)

method name; param list

default constructor

Page 33: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Information in the constant pool for index 3

Page 34: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

An attribute in a class file

The first entry is the constant pool index of the attribute name. The second entry is the length of what follows.

Page 35: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

A hex display of the complete class file for our simple program follows.

Page 36: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode
Page 37: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode
Page 38: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

max operandstack sz

max lvaarray sz

lv1 = 11;

Page 39: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode
Page 40: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Sizes of comparable programs

Page 41: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Some comparison and control instructions

• goto unconditional jump• if_cmplt compares top two stack items• if_icmpge compares top two stack items• iflt compares top of stack with 0• if_acmpeq compares references (not values)• if_acmpne compares references (not values)

See the illustrative program on the next slide.

Page 42: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

jump absolute to

jmp relativeaddressing

java puts test at bottomof loop

Page 43: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode
Page 44: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Instructions that jump use pc-relative addressing

A70006 (the machine code for goto 6) jumps to the location whose address is 6 + the contents of the pc register (before incrementation).

Range limitation: ±32k within method.

Page 45: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Unassembling the Simple class file

javap –c Simple

Page 46: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

cinit()

init()

Page 47: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Unassemble this program to see its bytecode

Page 48: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

pushes left-to-right

Page 49: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

public class O { public static void f() { // ... } int x;}

Page 50: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Arrays and objects

Page 51: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

(o)

needed to pass to constructor

even though f() is staticwe invoke it with an object receiver

Page 52: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode

Now that you know the basics of the JVM, you can enjoy (and understand) some more advanced discussions of the JVM.

Page 53: Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode