Title: Java and the JVM
1Java and the JVM
2Overview
- History and Java features
- Java technology
- The Java language
- A first look into the JVM
- Disassembling of .class files
3History of a Young Java
- 1992 Oak for a PDA on a SPARC (7)
- 1995 Official release as Java Internet
- 1997 picoJava Suns Java processor
- 1998 RTSJ specification start as JSR-01
- 1999 split into J2SE and J2EE
- 2000 J2ME
- 2002 RTSJ final release
4Java features
- Simple and object oriented
- Look and feel of C
- Simplified object model with single inheritance
- Portability
- Java compiler generates bytecodes
- Runtime systems for various platforms
- Size and behavior of basic data types defined
- Write once, run/debug anywhere
5Java features cont.
- Availability
- Windows, Linux, Solaris,
- Embedded systems
- Compiler and runtime are free
- Free IDEs Eclipse, Netbeans
- Library
- Rich class library
- Part of the definition
- Standard GUI toolkit
6Java features cont.
- Built-in model for concurrency
- Threads at the language level
- Synchronization
- Libraries are thread-safe
- Safety
- No Pointer!
- Extensive compile-time checking
- Runtime checking
- Automatic memory management GC
7Java system overview
8Java Technology
- The Java programming language
- The library (JDK)
- The Java virtual machine (JVM)
- Instruction set
- Binary format
- Verification
9Java Primitive Data Types
10Objects
- Everything belongs to an object (or a class)
- No global variables
- Namespace for objects
- Single inheritance
- Interfaces
- Allocated on the heap
- Shared among threads
- No free() garbage collector
11What is a Virtual Machine?
- A virtual machine (VM) is an abstract computer
architecture - Software on top of a real hardware
- Can run the same application on different
machines where the VM is available
12The Java Virtual Machine
- An abstract computing machine that executes
bytecode programs - An instruction set and the meaning of those
instructions the bytecodes - A binary format the class file format
- An algorithm to verify the class file
13JVM cont.
- Runtime environment for Java
- Implementation NOT defined
- Runs Java .class files
- Has to conform to Suns specification
14Implementations of the JVM
- Interpreter
- Simple, compact
- Slow
- Just-in-time compilation
- State-of-the-art for desktop/server
- Too resource consuming in embedded systems
- Batch compilation
- Hardware implementation
- Our topic!
15JVM Data Types
- No boolean, char, byte, and short types
- Stack contains only 32-bit and 64-bit data
- Conversion instructions
16Memory Areas for the JVM
- Method area
- Class description
- Code
- Constant pool
- Heap
- Objects and Arrays
- Shared by all threads
- Garbage collected
17Memory Areas for the JVM
- Stack
- Thread private
- Logical stack that contains
- Invocation frame
- Local variable area
- Operand stack
- Not necessary a single stack
- Local variables and operand stack are accessed
frequently
18JVM Instruction Set
- 32 (64) bit stack machine
- Variable length instruction set
- Simple to very complex instructions
- Symbolic references
- Only relative branches
19JVM Instruction Set
- The Bytecodes
- Operations on the operand stack
- Variable length
- Simple, e.g. iadd
- Complex, e.g. new
- Symbolic references
- 201 different instructions
20Instruction Types
- Arithmetic
- Load and store
- Type conversion
- Object creation and manipulation
- Operand stack manipulation
- Control transfer
- Method invocation and return
21Arithmetic Instructions
- Operate on the values from the stack
- Push the result back onto the stack
- Instructions for int, long, float and double
- No direct support for byte, short or char types
- Handled by int operations and type conversion
22iadd
- Operation Add int
- Format iadd
- Forms iadd 96 (0x60)
- Operand Stack ..., value1, value2 gt ..., result
- Both value1 and value2 must be of type int. The
values are popped from the operand stack. The int
result is value1 value2. The result is pushed
onto the operand stack. - The result is the 32 low-order bits of the true
mathematical result in a sufficiently wide
two's-complement format, represented as a value
of type int. If overflow occurs, then the sign of
the result may not be the same as the sign of the
mathematical sum of the two values. - Despite the fact that overflow may occur,
execution of an iadd instruction never throws a
runtime exception.
23fadd
- Operation Add float
- Format fadd
- Forms fadd 98 (0x62)
- Operand Stack ..., value1, value2 gt ..., result
- Both value1 and value2 must be of type float. The
values are popped from the operand stack and
undergo value set conversion, resulting in
value1' and value2'. The float result is value1'
value2'. The result is pushed onto the operand
stack. - The result of an fadd instruction is governed by
the rules of IEEE arithmetic. - The Java virtual machine requires support of
gradual underflow as defined by IEEE 754. Despite
the fact that overflow, underflow, or loss of
precision may occur, execution of an fadd
instruction never throws a runtime exception.
24ladd
- Operation Add long
- Format ladd
- Forms ladd 97 (0x61)
- Operand Stack ..., value1, value2 ..., result
- Both value1 and value2 must be of type long. The
values are popped from the operand stack. The
long result is value1 value2. The result is
pushed onto the operand stack. - The result is the 64 low-order bits of the true
mathematical result in a sufficiently wide
two's-complement format, represented as a value
of type long. If overflow occurs, the sign of the
result may not be the same as the sign of the
mathematical sum of the two values. - Despite the fact that overflow may occur,
execution of an ladd instruction never throws a
runtime exception.
25Arithmetic Instructions
- Add iadd, ladd, fadd, dadd
- Subtract isub, lsub, fsub, dsub
- Multiply imul, lmul, fmul, dmul
- Divide idiv, ldiv, fdiv, ddiv
- Remainder irem, lrem, frem, drem
- Negate ineg, lneg, fneg, dneg
- Shift ishl, ishr, iushr, lshl, lshr, lushr
- Bitwise OR ior, lor
- Bitwise AND iand, land
- Bitwise exclusive OR ixor, lxor
- Local variable increment iinc
- Comparison dcmpg, dcmpl, fcmpg, fcmpl, lcmp
26Load and Store Instructions
- Load
- Push value from local variable onto stack
- Push a constant onto the stack
- Store
- Transfer value from the stack to a local variable
- Typed instructions
- Short versions
27iload
- Operation Load int from local variable
- Format iload
- index
- Forms iload 21 (0x15)
- Operand Stack ... gt ..., value
- The index is an unsigned byte that must be an
index into the local variable array of the
current frame. The local variable at index must
contain an int. The value of the local variable
at index is pushed onto the operand stack. - The iload opcode can be used in conjunction with
the wide instruction to access a local variable
using a two-byte unsigned index.
28iload_ltngt
- Operation Load int from local variable
- Format iload_ltngt
- Forms iload_0 26 (0x1a)
- iload_1 27 (0x1b)
- iload_2 28 (0x1c)
- iload_3 29 (0x1d)
- Operand Stack ... gt ..., value
- The ltngt must be an index into the local variable
array of the current frame. The local variable at
ltngt must contain an int. The value of the local
variable at ltngt is pushed onto the operand stack. - Each of the iload_ltngt instructions is the same as
iload with an index of ltngt, except that the
operand ltngt is implicit.
29istore
- Operation Store int into local variable
- Format istore
- index
- Forms istore 54 (0x36)
- Operand Stack ..., value gt ...
- The index is an unsigned byte that must be an
index into the local variable array of the
current frame. The value on the top of the
operand stack must be of type int. It is popped
from the operand stack, and the value of the
local variable at index is set to value. - The istore opcode can be used in conjunction with
the wide instruction to access a local variable
using a two-byte unsigned index.
30bipush
- Operation Push byte
- Format bipush
- byte
- Forms bipush 16 (0x10)
- Operand Stack ... gt ..., value
- The immediate byte is sign-extended to an int
value. That value is pushed onto the operand
stack.
31sipush
- Operation Push short
- Format sipush
- byte1
- byte2
- Forms sipush 17 (0x11)
- Operand Stack ... gt ..., value
- The immediate unsigned byte1 and byte2 values are
assembled into an intermediate short where the
value of the short is (byte1 ltlt 8) byte2. The
intermediate value is then sign-extended to an
int value. That value is pushed onto the operand
stack.
32iconst_ltigt
- Operation Push int constant
- Format iconst_ltigt
- Forms iconst_m1 2 (0x2)
- iconst_0 3 (0x3)
- iconst_1 4 (0x4)
-
- iconst_5 8 (0x8)
- Operand Stack ... gt ..., ltigt
- Push the int constant ltigt (-1, 0, 1, 2, 3, 4 or
5) onto the operand stack. - Each of this family of instructions is equivalent
to bipush ltigt for the respective value of ltigt,
except that the operand ltigt is implicit.
33ldc
- Operation Push item from runtime constant pool
- Format ldc
- index
- Forms ldc 18 (0x12)
- Operand Stack ... gt ..., value
- The index is an unsigned byte that must be a
valid index into the runtime constant pool of the
current class. The runtime constant pool entry at
index either must be a runtime constant of type
int or float, or must be a symbolic reference to
a string literal. - If the runtime constant pool entry is a runtime
constant of type int or float, the numeric value
of that runtime constant is pushed onto the
operand stack as an int or float, respectively. - Otherwise, the runtime constant pool entry must
be a reference to an instance of class String
representing a string literal. A reference to
that instance, value, is pushed onto the operand
stack.
34Load and Store Instructions
- Load a local variable
- iload, iload_ltngt, lload, lload_ltngt, fload,
fload_ltngt, dload, dload_ltngt, aload, aload_ltngt - Store a local variable
- istore, istore_ltngt, lstore, lstore_ltngt, fstore,
fstore_ltngt, dstore, dstore_ltngt, astore,
astore_ltngt - Load a constant
- bipush, sipush, ldc, ldc_w, ldc2_w, aconst_null,
iconst_m1, iconst_ltigt, lconst_ltlgt, fconst_ltfgt,
dconst_ltdgt - Wider index, or larger immediate operand
- wide
35Load/Add/Store Example
- int a, b, c
- a 1
- b 123
- c ab
- 0 iconst_1
- 1 istore_0 // a
- 2 bipush 123
- 4 istore_1 // b
- 5 iload_0 // a
- 6 iload_1 // b
- 7 iadd
- 8 istore_2 // c
36Type Conversion
- Widening numeric conversions
- int to long, float, or double
- long to float or double
- float to double
- i2l, i2f, i2d, l2f, l2d, and f2d
- Narrowing numeric conversions
- int to byte, short, or char
- long to int
- float to int or long
- double to int, long, or float
- i2b, i2c, i2s, l2i, f2i, f2l, d2i, d2l, and d2f
37Conversion Example
- 0 iconst_1
- 1 istore_0
- 2 iload_0
- 3 iconst_1
- 4 iadd
- 5 i2s // truncate
- 6 istore_0
38Object Instructions
- Create a new class instance or array
- new, newarray, anewarray, multianewarray
- Field access
- getfield, putfield, getstatic, putstatic
- Array load, store
- baload, caload, saload, iaload, laload, faload,
daload, aaload - bastore, castore, sastore, iastore, lastore,
fastore, dastore, aastore - Length of an array
- arraylength
- Check properties
- instanceof, checkcast
39Object Creation
- Object create()
- return new Object()
-
- 0 new 2 //class Object
- 3 dup
- 4 invokespecial 1 //Method
java/lang/Object."ltinitgt"()V - 7 areturn
40getfield
- Operation Fetch field from object
- Format getfield
- indexbyte1
- indexbyte2
- Forms getfield 180 (0xb4)
- Operand Stack ..., objectref gt ..., value
- The objectref, which must be of type reference,
is popped from the operand stack. The unsigned
indexbyte1 and indexbyte2 are used to construct
an index into the runtime constant pool of the
current class, where the value of the index is
(indexbyte1 ltlt 8) indexbyte2. The runtime
constant pool item at that index must be a
symbolic reference to a field, which gives the
name and descriptor of the field as well as a
symbolic reference to the class in which the
field is to be found. The referenced field is
resolved. The value of the referenced field in
objectref is fetched and pushed onto the operand
stack.
41putfield
- Operation Set field in object
- Format putfield
- indexbyte1
- indexbyte2
- Forms putfield 181 (0xb5)
- Operand Stack ..., objectref, value gt ...
- The unsigned indexbyte1 and indexbyte2 are used
to construct an index into the runtime constant
pool of the current class - The value and objectref are popped from the
operand stack. The objectref must be of type
reference. The referenced field in objectref is
set to value.
42Field Access
- static int statVal
- private int privVal
- void foo()
-
- int i statVal privVal
-
- statVal i
-
- privVal i
- 0 getstatic 3 //Field statValI
- 3 aload_0
- 4 getfield 4 //Field privValI
- 7 iadd
- 8 istore_1
- 9 iload_1
- 10 putstatic 3 //Field statValI
- 13 aload_0
- 14 iload_1
- 15 putfield 4 //Field privValI
- 18 return
43Operand Stack Manipulation
- Direct manipulation of the operand stack
- pop, pop2
- dup, dup2, dup_x1, dup2_x1, dup_x2, dup2_x2
- swap
44swap
- Operation Swap the top two operand stack values
- Format swap
- Forms swap 95 (0x5f)
- Operand Stack ..., value2, value1 gt ..., value1,
value2 - Swap the top two values on the operand stack.
45Control Transfer
- Conditional branch
- ifeq, iflt, ifle, ifne, ifgt, ifge, ifnull,
ifnonnull, if_icmpeq, if_icmpne, if_icmplt,
if_icmpgt, if_icmple, if_icmpge, if_acmpeq,
if_acmpne. - Switch
- tableswitch, lookupswitch.
- Unconditional branch
- goto, goto_w, jsr, jsr_w, ret.
46ifltcondgt
- Operation Branch if int comparison with zero
succeeds - Format ifltcondgt
- branchbyte1
- branchbyte2
- Forms ifeq 153 (0x99)
-
- ifle 158 (0x9e)
- Operand Stack ..., value gt ...
- The value is popped from the operand stack and
compared against zero. All comparisons are
signed - eq succeeds if and only if value 0
-
- le succeeds if and only if value 0
- If the comparison succeeds, branchbyte1 and
branchbyte2 are used to construct a signed 16-bit
offset. Execution then proceeds at that offset
from the address of the opcode of this ifltcondgt
instruction. Otherwise, execution proceeds at the
address of the instruction following this
ifltcondgt instruction.
47Method Invocation, Return
- invokevirtual
- Invokes an instance method of an object,
dispatching on the (virtual) type of the object. - This is the normal method dispatch in the Java
programming language - invokeinterface
- Invokes a method that is implemented by an
interface - invokespecial
- Invokes an instance method requiring special
handling - Instance initialization method, a private method,
or a superclass method - invokestatic
- Invokes a class (static) method
48invokevirtual
- Operation Invoke instance method dispatch based
on class - Format invokevirtual
- indexbyte1
- indexbyte2
- Forms invokevirtual 182 (0xb6)
- Operand Stack ..., objectref, arg1, arg2 ...
gt ... - The unsigned indexbyte1 and indexbyte2 are used
to construct an index into the runtime constant
pool - The objectref must be followed on the operand
stack by nargs argument values, where the number,
type, and order of the values must be consistent
with the descriptor of the selected instance
method. - If the method is synchronized, the monitor
associated with objectref is acquired or
reentered.
49Class Information
- Instance size
- Static (class) variables
- Virtual method table
- Interface table
- Constant pool
- Reference to super class
50(No Transcript)
51Method Structure
- Information about a method
- Address
- Length (for the cache)
- Pointer to the constant pool of the class
- Number of arguments and local variables
52Object Format
- Direct pointer
- Handle possible
- Return pointer to the class information
53Array Format
- Direct pointer
- Handle possible
- Length is needed
54Constant Pool
- Contains
- Simple constants (e.g. 123, 0.345)
- String constants
- Class references
- Field references
- Method references
- All references are symbolic in the class file
- References can and should be converted to direct
pointers
55Runtime Data Structures
- PC program counter
- Operand stack
- SP stack pointer
- VP variable pointer
- MP method pointer
- Reference to the method structure
- CP constant pool
- Current constant pool
56Parameter passing
- int val foo(1, 2)
- ...
- public int foo(int a, int b)
- int c 1
- return abc
-
- The invocation sequence
- aload_0 // Push the object
reference - iconst_1 // and the parameter onto
the - iconst_2 // operand stack.
- invokevirtual 2 // Invoke method
foo(II)I. - istore_1 // Store the result in
val. - public int foo(int,int)
- iconst_1 // The constant is stored
in a method - istore_3 // local variable (at
position 3). - iload_1 // Arguments are accessed
as locals - iload_2 // and pushed onto the
operand stack.
57Stack on Method Invocation
58Dissassembling Java
- Compile
- javac Hello.java
- Run
- java Hello
- Dissassemble
- javap -c Hello
59A Bytecode Example
- public class X
- public static void
- main(String args)
- add(1, 2)
-
- public static int
- add(int a, int b)
- return ab
-
- public static void main(java.lang.String)
- Code
- 0 iconst_1
- 1 iconst_2
- //Method add(II)I
- 2 invokestatic 2 5 pop
- 6 return
- public static int add(int,int)
- Code
- 0 iload_0
- 1 iload_1
- 2 iadd
- 3 ireturn
60Coding Avoiding garbage
- System.out.println("Result "i)
- getstatic 3 // Field System.outLjava/io/PrintSt
ream - new 4 // class StringBuffer
- dup
- invokespecial 5 // StringBuffer."ltinitgt"()V
- ldc 6 // String Result
- invokevirtual 7 // StringBuffer.append(LString
)LStringBuffer - iload_1
- invokevirtual 8 // StringBuffer.append(I)LStrin
gBuffer - invokevirtual 9 // StringBuffer.toString()LStri
ng - invokevirtual 10// PrintStream.println(LString
)V
61Coding Avoiding garbage
- System.out.print("Result ")
- System.out.println(i)
- getstatic 3 //Field System.outLjava/io/Pr
intStream - ldc 4 //String Result
- invokevirtual 5 //Method PrintStream.print(LS
tring)V - getstatic 3 //Field System.outLPrintStrea
m - iload_1
- invokevirtual 6 //Method PrintStream.println(
I)V
62Java for Embedded Systems?
- Simpler than C/C
- Safer than C/C
- Threads are part of the language
- - Interpreting JVM is slow
- - JIT needs a lot of memory
- - GC and real-time?
63Summary Java Features
- Safe OO Language
- No pointers
- Type-safety
- Garbage Collection
- Built in model for concurrency
- Platform independent
- Very rich standard library
64Summary Java/JVM
- Java language definition
- Class library
- The Java virtual machine (JVM)
- An instruction set the bytecodes
- A binary format the class file
- An algorithm to verify the class file
65Summary
- The JVM defines an instruction set Bytecodes
- Simple, typed stack instructions
- Complex, such as object creation
- Implementation details are not defined
- Method invocation suggests a common stack
66More Information
- Java
- James Gosling, Bill Joy, Guy Steele, and Gilad
Bracha. The Java Language Specification,
Addison-Wesley, 2000, JavaSpec. - JVM
- Tim Lindholm and Frank Yellin. The Java Virtual
Machine Specification. Addison-Wesley, 1999,
JVMSpec.
67More Information
- Virtual Machine Design, Antero Taivalsaari,
seminar notes
68JOP A Java Optimized Processor for Embedded
Real-Time Systems
69Research Objectives
- Primary objectives
- Time-predictable Java platform
- Small design
- A working processor
- Secondary objectives
- Acceptable performance
- A flexible architecture
- Real-time profile for Java
70Memory Areas for the JVM
- Stack
- Most often accessed
- On-chip memory as cache
- Code
- Novel instruction cache
- Class description and constant pool
- Heap
71Implementations of the JVM
- Interpreter
- Just-in-time compilation
- Batch compilation
- Hardware implementation
72Related Work
- picoJava
- SUN, never released
- aJile JEMCore
- Available, RTSJ, two versions
- Komodo
- Multithreaded Java processor
- FemtoJava
- Application specific processor
73Research Objectives
74JOP Architecture
- Overview
- Microcode
- Processor pipeline
- An efficient stack machine
- Instruction cache
75JOP Block Diagram
76JVM Bytecode Issue
- Simple and complex instruction mix
- No bytecodes for native functions
- Common solution (e.g. in picoJava)
- Implement a subset of the bytecodes
- SW trap on complex instructions
- Overhead for the trap 16 to 926 cycles
- Additional instructions (115!)
77JOP Solution
- Translation to microcode in hardware
- Additional pipeline stage
- No overhead for complex bytecodes
- 1 to 1 mapping results in single cycle execution
- Microcode sequence for more complex bytecodes
- Bytecodes can be implemented in Java
78Microcode
- Stack-oriented
- Compact
- Constant length
- Single cycle
- Low-level HW access
- An example
- dup dup nxt // 1 to 1 mapping
- // a and b are scratch variables
- // for the JVM code.
- dup_x1 stm a // save TOS
- stm b // and TOS-1
- ldm a // duplicate TOS
- ldm b // restore TOS-1
- ldm a nxt // restore TOS
- // and fetch next bytecode
79Processor Pipeline
80Interrupts
- Interrupt logic at bytecode translation
- Special bytecode
- Transparent to the core pipeline
- Interrupts under scheduler control
- Priority for device drivers
- No additional blocking time
- Integration in schedulability analysis
- Jitter free timer events
- Bound to a thread
81An Efficient Stack Machine
- JVM stack is a logical stack
- Frame for return information
- Local variable area
- Operand stack
- Argument-passing regulates the layout
- Operand stack and local variables need caching
82Stack access
- Stack operation
- Read TOS and TOS-1
- Execute
- Write back TOS
- Variable load
- Read from deeper stack location
- Write into TOS
- Variable store
- Read TOS
- Write into deeper stack location
83Two-Level Stack Cache
- Dual read only from TOS and TOS-1
- Two register (A/B)
- Dual-port memory
- Simpler Pipeline
- No forwarding logic
- Instruction fetch
- Instruction decode
- Execute, load or store
84JVM Properties
- Short methods
- Maximum method size is restricted
- No branches out of or into a method
- Only relative branches
85Proposed Cache Solution
- Full method cached
- Cache fill on call and return
- Cache misses only at these bytecodes
- Relative addressing
- No address translation necessary
- No fast tag memory
86Architecture Summary
- Microcode
- 13 stage pipeline
- Two-level stack cache
- Method cache
The JVM is a CISC stack architecture,whereas JOP
is a RISC stack architecture.
87Results
- Size
- Compared to soft-core processors
- General performance
- Application benchmark (KFL UDP/IP)
- Various Java systems
- Real-time performance
- 100MHz JOP 266MHz Pentium MMX
- Simple RT profile RTSJ/RT-Linux
88Size of FPGA processors
89Application Benchmark
90Periodic Thread Jitter
91Context Switch
- Low priority thread records current time
- High priority periodic/event thread measures
elapsed time after unblocking - Time in cycles
92Contributions
- Real-time Java processor
- Exactly known execution time of the BCs
- No mutual dependency between BCs
- Time-predictable method cache
- Resource-constrained processor
- RISC stack architecture
- Efficient stack cache
- Flexible architecture
93More Information
- JOP Thesis and source
- http//www.jopdesign.com/thesis/index.jsp
- http//www.jopdesign.com/download.jsp
- Various papers
- http//www.jopdesign.com/docu.jsp