JVM Bytecodes
~ Anvay Patil
We spend 90% of our time in developing/maintaining
10% of code.
From the book:
Screenshot from Chapter 5
JVM=J+VM
Let’s get little curious here
What is VM?
A Virtual Machine is a high level abstraction on top of native operating system
that emulates a physical machine.
Two types of Virtual Machines:
1. Process Virtual Machine.
2. System Virtual Machine.
JVM -> Process Virtual Machine
A Process Virtual machine enables the same platform/environment for execution on
multiple operating systems and different hardware architectures.
They could be of two type:
1. Stack Based Process Virtual machine (HotSpot Virtual machine/ Primary JVM)
2. Register Based Process Virtual machine (Android’s Dalvik Virtual machine)
Stack Based
vs
Register based
Virtual Machine
Types
Now few questions?
1. Stack based vs register based Virtual machine, does it affect bytecodes which is
generated by Java Compiler ?
2. What makes Java achieve platform independency ?
3. Is platform independency, for only Java language or it is true for other JVM languages
too ?
Bytecodes
So what is the Magic behind the bytecodes?
What exactly are bytecodes?
Why should i be aware about it?
Bytecode?
JVM
➔ one byte instruction set
➔ 256 possible opcodes (unsigned bytecode)
➔ 200 in use on current
Instruction sets designed for efficient
execution by software interpreter which can
do suitable further compilation into
machine code may be at runtime.
Microsoft CLR
➔ 2 bytes instruction sets
➔ Stack based VM
Why know JVM Bytecodes?
1. Can give deep understanding about the
platform
2. Can read the byte code
3. Can modify the bytecode
4. Can build own language?
5. Use libraries which can generate bytecodes.
Java -VM
Android’s
Dalvik -VM
Jython VM
CPython
x=7,y=8
print x+y
Jython
Compiler
CPython
Compiler
Cpython
Interpreter
Jython
Interpreter15
CPython VM Java VM
CPython Bytecode Java Bytecode
Python source code
JVM Languages: < OMG :-O >
Jython
Mirah
JRuby
MicroFocus Visual COBOL
JScheme
Javascal/Pascal4J
Javascal
JTclJawk
JLua
Erjang
Jaskell
JPerl
JPerl Groovy
Clojure
Xtend Ceylon Kotlin
More than 800 implementations of JVM and more than 250 JVM languages
Scala
Fhatom
Gosu
Processing
Drools
Frink Open-XION
Redline SmallTalk
Ioke Nashorn JS
JudoScript
LolCode
NestVM JPC
PizzaNoop
jvm.go
JVM=Polyglot-VM
Simple Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
> javac HelloWorld.java
> javap -c HelloWorld.class
> javap -verbose HelloWorld.class
javap
● Java Class File disassembler
● Basic operations and class structure
○ javap HelloWorld.class
● -c flag include bytecodes
○ javap -c HelloWorld.class
● -public -private -protected option
○ javap -private HelloWorld.class
○ javap -public HelloWorld.class
○ javap -protected HelloWorld.class
● -verbose for stack size, locals, args etc.
○ javap -verbose HelloWorld.class
Instruction set:
● Stack instruction, since stack based machine
● Basic operations
● Flow control
● Class structures
● Exception Handling
Stack Manipulation:
Typed Opcodes:
b byte
s short
c char
i int
l long
f float
d double
a reference
<type> <operation>
Constant Values
Local vars (load, store)
Array Operations (aload, astore)
Math ops (add, sub, mul, div)
Boolean bitwise
Comparisons
Conversions
Type
Operations
There is no instruction for boolean, and can play with 0 or 1
Constant Values:
Local variables:
Arrays:
Math Operations:
Boolean and Bitwise Operations:
Conversions:
Compare operations:
Flow Control:
More Flow Control:
Instructions for Classes
Instructions for Classes
Let’s execute HelloWorld
stack=2, locals=1, args_size=1
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String Hello Java User Group
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
Q & A :)
References
● Java Virtual Machine Specification
● Inside JVM
● JVM Programming Languages
Thank you :)

JVM Bytecodes

  • 1.
  • 2.
    We spend 90%of our time in developing/maintaining 10% of code.
  • 3.
  • 4.
  • 5.
    What is VM? AVirtual Machine is a high level abstraction on top of native operating system that emulates a physical machine. Two types of Virtual Machines: 1. Process Virtual Machine. 2. System Virtual Machine.
  • 6.
    JVM -> ProcessVirtual Machine A Process Virtual machine enables the same platform/environment for execution on multiple operating systems and different hardware architectures. They could be of two type: 1. Stack Based Process Virtual machine (HotSpot Virtual machine/ Primary JVM) 2. Register Based Process Virtual machine (Android’s Dalvik Virtual machine)
  • 7.
  • 8.
    Now few questions? 1.Stack based vs register based Virtual machine, does it affect bytecodes which is generated by Java Compiler ? 2. What makes Java achieve platform independency ? 3. Is platform independency, for only Java language or it is true for other JVM languages too ?
  • 9.
    Bytecodes So what isthe Magic behind the bytecodes? What exactly are bytecodes? Why should i be aware about it?
  • 10.
    Bytecode? JVM ➔ one byteinstruction set ➔ 256 possible opcodes (unsigned bytecode) ➔ 200 in use on current Instruction sets designed for efficient execution by software interpreter which can do suitable further compilation into machine code may be at runtime. Microsoft CLR ➔ 2 bytes instruction sets ➔ Stack based VM
  • 11.
    Why know JVMBytecodes? 1. Can give deep understanding about the platform 2. Can read the byte code 3. Can modify the bytecode 4. Can build own language? 5. Use libraries which can generate bytecodes.
  • 12.
  • 13.
  • 14.
    JVM Languages: <OMG :-O > Jython Mirah JRuby MicroFocus Visual COBOL JScheme Javascal/Pascal4J Javascal JTclJawk JLua Erjang Jaskell JPerl JPerl Groovy Clojure Xtend Ceylon Kotlin More than 800 implementations of JVM and more than 250 JVM languages Scala Fhatom Gosu Processing Drools Frink Open-XION Redline SmallTalk Ioke Nashorn JS JudoScript LolCode NestVM JPC PizzaNoop jvm.go
  • 15.
  • 16.
    Simple Program public classHelloWorld { public static void main(String[] args) { System.out.println("Hello, world"); } } > javac HelloWorld.java > javap -c HelloWorld.class > javap -verbose HelloWorld.class
  • 17.
    javap ● Java ClassFile disassembler ● Basic operations and class structure ○ javap HelloWorld.class ● -c flag include bytecodes ○ javap -c HelloWorld.class ● -public -private -protected option ○ javap -private HelloWorld.class ○ javap -public HelloWorld.class ○ javap -protected HelloWorld.class ● -verbose for stack size, locals, args etc. ○ javap -verbose HelloWorld.class
  • 18.
    Instruction set: ● Stackinstruction, since stack based machine ● Basic operations ● Flow control ● Class structures ● Exception Handling
  • 19.
  • 20.
    Typed Opcodes: b byte sshort c char i int l long f float d double a reference <type> <operation> Constant Values Local vars (load, store) Array Operations (aload, astore) Math ops (add, sub, mul, div) Boolean bitwise Comparisons Conversions Type Operations There is no instruction for boolean, and can play with 0 or 1
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
    Let’s execute HelloWorld stack=2,locals=1, args_size=1 0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3 // String Hello Java User Group 5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return
  • 33.
  • 34.
    References ● Java VirtualMachine Specification ● Inside JVM ● JVM Programming Languages
  • 35.