Lecture 2 Java Virtual Machine
Lecture 2 Java Virtual Machine
Technology
CIC-212
Java Programming
Lecture 2
Also visit this site for more complete information about the JVM:
http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html
2
Abstract
Machines
Abstract machine implements an intermediate language in between the
high-level language (e.g. Java) and the low-level hardware (e.g.
Pentium) Implemented in Java:
High level Java Java Machine independent
Java compiler
3
Abstract
Machines
An abstract machine is intended specifically as a runtime
system for a particular (kind of) programming language.
• JVM is a virtual machine for Java programs.
• It directly supports object-oriented concepts such as
classes, objects, methods, method invocation etc.
• Easy to compile Java to JVM
1. easy to implement compiler
2. fast compilation
• Another advantage: portability
4
Class Files and Class File
External representation Format
(platform independent) JVM
Internal representation
.class files
(implementation dependent)
load
classes primitive types
arrays
objects strings
methods
5
Data Types
JVM (and Java) distinguishes between two kinds:
Primitive types:
• boolean: boolean
• numeric integral: byte, short, int, long, char
• numeric floating point: float, double
• internal, for exception handling: returnAddress
Reference types:
• class types
• array types
• interface types
6
Java JVM Run-time Data
Areas
Types of Java JVM Run-time Memory
Areas,
4. Method Area
In general, method area is a logical part of heap area. But that is left to the
JVM implementers to decide. Method area has per class structures and
fields. Nothing but static fields and structures. It also includes the method
data, method and constructor code, run-time constant pool. Method area is
created at JVM startup and shared among all the threads. JVM will throw
OutOfMemoryError if the allocated memory area is not sufficient during the
run-time.
Java JVM Runtime Data Areas
5. Run-time Constant Pool
Run-time constant pool is created out of the method area and it is
created by JVM when a class or interface is created. Run-time
constant pool contains the constant_pool table which is
applicable for per class or interface. It contains the literals. JVM
will throw OutOfMemoryError when it cannot find memory to
allocate to run-time constant pool.
6. Native Method Stacks
JVM that supports native methods will have native method
stacks. It is used for native methods, and created per thread. If
the native methods cannot be loaded by a JVM then it need not
have native method stacks. Memory size is managed similar to
general JVM stacks like fixed or dynamic. JVM will throw
StackOverflowError or OutOfMemoryError accordingly.
Java Stacks
JVM is a stack based machine.
JVM instructions
• implicitly take arguments from the stack top
• put their result on the top of the stack
The stack is used to
• pass arguments to methods
• return a result from a method
• store intermediate results while evaluating expressions
• store local variables
This works similarly to (but not exactly the same as) what we
previously discussed about stack-based storage allocation and
routines.
12
Java Frame
The Java stack consists of frames. The JVM specification does not say
exactly how the stack and frames should be implemented.
The JVM specification specifies that a stack frame has areas for:
13
Java Frame
The role/purpose of each of the areas in a stack frame:
14
Java Frame
An implementation using registers such as SB, ST, and LB and a
dynamic link is one possible implementation.
SB to previous frame on the stack
LB
dynamic link
to runtime constant pool
args JVM instructions store and load
+ (for accessing args and locals) use
local vars addresses which are numbers
from 0 to #args + #locals - 1
operand stack
ST
15
JVM Interpreter
The core of a JVM interpreter is basically this:
do {
byte opcode = fetch an opcode;
switch (opcode) {
case opCode1 :
fetch operands for opCode1;
execute action for opCode1;
break;
case opCode2 :
fetch operands for opCode2;
execute action for opCode2;
break;
case ...
} while (more to do)
16
Instruction-set: typed
instructions!
JVM instructions are explicitly typed: different opCodes for instructions
for integers, floats, arrays, reference types, etc.
17
Instruction-set: typed
instructions!
JVM instructions have three kinds of operands:
- from the top of the operand stack
- from the bytes following the opCode
- part of the opCode itself
Each instruction may have different “forms” supporting different
kinds of operands.
Example: different forms of “iload”
Assembly code Binary instruction code layout
iload_0 26
iload_1 27
iload_2 28
iload_3 29
iload n 21 n
wide iload n 196 21 n
18
Instruction-set: accessing
arguments and locals
arguments and locals area inside a stack frame
0:
1: args: indexes 0 .. #args - 1
2:
3: locals: indexes #args .. #args + #locals - 1
19
Instruction-set: non-local
memory access
In the JVM, the contents of different “kinds” of memory can be
accessed by different kinds of instructions.
accessing locals and arguments: load and store instructions
accessing fields in objects: getfield, putfield
accessing static fields: getstatic, putstatic
Note: Static fields are a lot like global variables. They are allocated
in the “method area” where also code for methods and
representations for classes (including method tables) are stored.
20
Instruction-set: operations on
numbers
Arithmetic
add: iadd, ladd, fadd, dadd
subtract: isub, lsub, fsub, dsub
multiply: imul, lmul, fmul, dmul
etc.
Conversion
i2l, i2f, i2d,
l2f, l2d, f2d,
f2i, d2i, …
21
Instruction-set …
Control transfer
Unconditional: goto, jsr, ret, …
Conditional: ifeq, iflt, ifgt, if_icmpeq, …
22
Instruction-set …
Method invocation:
invokevirtual: usual instruction for calling a method on an
object.
invokeinterface: same as invokevirtual, but used
when the called method is declared in an interface (requires a
different kind of method lookup)
invokespecial: for calling things such as constructors,
which are not dynamically dispatched (this instruction is also
known as invokenonvirtual).
invokestatic: for calling methods that have the “static”
modifier (these methods are sent to a class, not to an object).
Returning from methods:
return, ireturn, lreturn, areturn, freturn,
…
23
Instruction-set: Heap
Memory Allocation
Create new class instance (object):
new
24
Instructions and the
“Constant Pool”
Many JVM instructions have operands which are indexes pointing to
an entry in the so-called constant pool.
The constant pool contains all kinds of entries that represent
“symbolic” references for “linking”. This is the way that instructions
refer to things such as classes, interfaces, fields, methods, and
constants such as string literals and numbers.
These are the kinds of constant pool entries that exist:
• Class_info • Integer
• Fieldref_info • Float
• Methodref_info • Long
• InterfaceMethodref_info • Double
• String • Name_and_Type_info
• Utf8_info (Unicode characters)
25
Instructions and the
“Constant Pool”
That previous picture is rather complicated, let’s simplify it a little:
Format: 180 indexbyte1 indexbyte2
Fieldref
Class Name_and_Type
26
Instructions and the “Constant Pool”
Java Virtual Machine instructions do not rely on the
Table 4.3. Constant pool tags
run-time layout of classes, interfaces, class
instances, or arrays. Instead, instructions refer to Constant Type Value
vary with the value of tag. The valid tags and their CONSTANT_Utf8 1
values are listed in Table 4.3. Each tag byte must be CONSTANT_MethodHandle 15
followed by two or more bytes giving information CONSTANT_MethodType 16
about the specific constant. The format of the
CONSTANT_InvokeDynamic 18
additional information varies with the tag value.
27
Instructions and the “Constant Pool”
The CONSTANT_Class_info Structure
The CONSTANT_Class_info structure is used to represent a class or an interface:
CONSTANT_Class_info
{
u1 tag;
u2 name_index;
}
name_index: The value of the name_index item must be a valid index into
the constant_pool table. The constant_pool entry at that index must be
a CONSTANT_Utf8_info structure representing a valid binary class or interface name
encoded in internal form .
28
Instructions and the “Constant Pool”
The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and
CONSTANT_InterfaceMethodref_info Structures
Fields, methods, and interface methods are represented by similar structures:
Tag:
The tag item of a CONSTANT_Fieldref_info structure has the
CONSTANT_Fieldref_info
value CONSTANT_Fieldref (9).
{ The tag item of a CONSTANT_Methodref_info structure has the
u1 tag; value CONSTANT_Methodref (10).
The tag item of a CONSTANT_InterfaceMethodref_info structure has the
u2 class_index;
value CONSTANT_InterfaceMethodref (11).
u2 name_and_type_index;
} class_index:
The value of the class_index item must be a valid index into the constant_pool table.
The constant_pool entry at that index must be a CONSTANT_Class_info structure
CONSTANT_Methodref_info representing a class or interface type that has the field or method as a member.
{ The class_index item of a CONSTANT_Methodref_info structure must be a class
u1 tag; type, not an interface type.
The class_index item of a CONSTANT_InterfaceMethodref_info structure must
u2 class_index; be an interface type.
u2 name_and_type_index; The class_index item of a CONSTANT_Fieldref_info structure may be either a class
type or an interface type.
}
name_and_type_index:
CONSTANT_InterfaceMethodref_infoThe value of the name_and_type_index item must be a valid index into
the constant_pool table. The constant_pool entry at that index must be
{
a CONSTANT_NameAndType_info structure. Thisconstant_pool entry indicates
u1 tag; the name and descriptor of the field or method.
u2 class_index; In a CONSTANT_Fieldref_info, the indicated descriptor must be a field descriptor.
Otherwise, the indicated descriptor must be a method descriptor.
u2 name_and_type_index; If the name of the method of a CONSTANT_Methodref_info structure begins with a
} `` '<' ('\u003c'), then the name must be the special name <init>, representing an instance
initialization method. The return type of such a method must be void.
29
Instructions and the “Constant Pool”
The constant entries format is part of the Java class file format.
This assembler takes care of creating the constant pool entries for us.
When an instruction operand expects a constant pool entry the
assembler allows you to enter the entry “in place” in an easy syntax.
Example:
getfield mypackage/Queue i I
30
Instructions and the “Constant Pool”
Fully qualified class names and descriptors in constant pool UTF8
entries.
1. Fully qualified class name: a package + class name string. Note
this uses “/” instead of “.” to separate each level along the path.
31
Linking
In general, linking is the process of resolving symbolic references in
binary files.
=> The binary file is not yet executable, because it has some kind of
“symbolic links” in it that point to things (classes, methods, functions,
variables, etc.) in other files/modules.
32
Loading and Linking in
In JVM, loading and linking of class files happens at runtime, while
the program is running!
JVM
Classes are loaded as needed.
33
Closing Example
As a closing example on the JVM, we will take a look at the
compiled code of the following simple Java class declaration.
class Factorial {
int fac(int n) {
int result = 1;
for (int i=2; i<n; i++) {
result = result * i;
}
return result;
}
}
34
Closing Example
Compiling and Disassembling
% javac Factorial.java
% javap -c -verbose Factorial
Compiled from Factorial.java
class Factorial extends java.lang.Object {
Factorial();
/* Stack=1, Locals=1, Args_size=1 */
int fac(int);
/* Stack=2, Locals=4, Args_size=2 */
}
Method Factorial()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 return
35
Compiling and
Method int Disassembling
// address: 0
fac(int)
1 2
...i
// stack: this n result
3
36