Every Java developer should have a good working knowledge of JVM bytecode. Its fun, it can help you diagnose problems, improve performance, and even empowers you to build your own languages. No matter what kind of Java application you work on, youll get something out of this talk. Well start with bytecode fundamentals. Youll learn how the most common operations work and see visual representations of how the JVM executes that code. The second part of the talk will introduce Jitescript, a Java library for generating bytecode. You learn how to use Jitescript with some plain old Java code to create your own JVM languages.
17. Exception in thread "main" java.lang.NullPointerException
at Printer.printString(Printer.java:13)
at Printer.print(Printer.java:9)
at Printer.main(Printer.java:19)
Stack Frame
Operand
Stack
Local
Variable
Array
Constant Pool
#1 = Methodref
#2 = Class
#3 = Methodref
#4 = String
#5 = Methodref
...
18. Stack Frame
Operand StackLocal Vars Constant Pool
#1 = Methodref
#2 = Class
#3 = Methodref
#4 = String
#5 = Methodref
...
0: new #2
3: dup
4: invokespecial #3
7: ldc #4
9: invokevirtual #5
12: pop
13: return
19. Stack Frame
Operand StackLocal Vars Constant Pool
<ArrayList>
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = String
#5 = Methodref
...
0: new #2
3: dup
4: invokespecial #3
7: ldc #4
9: invokevirtual #5
12: pop
13: return
20. Stack Frame
Operand StackLocal Vars Constant Pool
<ArrayList>
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = String
#5 = Methodref
...
0: new #2
3: dup
4: invokespecial #3
7: ldc #4
9: invokevirtual #5
12: pop
13: return
<ArrayList>
21. Stack Frame
Operand StackLocal Vars Constant Pool
<ArrayList>
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = String
#5 = Methodref
...
0: new #2
3: dup
4: invokespecial #3
7: ldc #4
9: invokevirtual #5
12: pop
13: return
<ArrayList>
22. invokespecial
Used to invoke an initializer, private method, or superclass method
invokeinterface
Used to invoke an interface method
invokestatic
Used to invoke a class-level method (i.e. static methods)
invokevirtual
Used to invoke an instance method
invokedynamic
Used to invoke methods dynamically (new shiny)
23. Stack Frame
Operand StackLocal Vars Constant Pool
<ArrayList>
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = String
#5 = Methodref
...
0: new #2
3: dup
4: invokespecial #3
7: ldc #4
9: invokevirtual #5
12: pop
13: return
<ArrayList>
56. p
>
<
: print a variable
: push a 0 onto the stack
: pop the top value off of the stack
+
-
[
: increment a variable
: decrement a variable
: start a loop
] : end a loop
s : swap the top two values of the stack
80. Stack<LabelNode[]> loopStack = new Stack<LabelNode[]>();
else if (“[“.equals(cmd)) {
LabelNode begin = new LabelNode();
LabelNode end = new LabelNode();
label(begin);
dup();
ifeq(end);
loopStack.push(new LabelNode[] {begin, end});
}
81. Stack<LabelNode[]> loopStack = new Stack<LabelNode[]>();
else if (“[“.equals(cmd)) {
LabelNode begin = new LabelNode();
LabelNode end = new LabelNode();
label(begin);
dup();
ifeq(end);
loopStack.push(new LabelNode[] {begin, end});
}
82. Stack<LabelNode[]> loopStack = new Stack<LabelNode[]>();
else if (“[“.equals(cmd)) {
LabelNode begin = new LabelNode();
LabelNode end = new LabelNode();
label(begin);
dup();
ifeq(end);
loopStack.push(new LabelNode[] {begin, end});
}
83. Stack<LabelNode[]> loopStack = new Stack<LabelNode[]>();
else if (“[“.equals(cmd)) {
LabelNode begin = new LabelNode();
LabelNode end = new LabelNode();
label(begin);
dup();
ifeq(end);
loopStack.push(new LabelNode[] {begin, end});
}
84. else if (“]“.equals(cmd)) {
LabelNode[] labelNodes = loopStack.pop();
LabelNode begin = labelNodes[0];
LabelNode end = labelNodes[1];
go_to(begin);
label(end);
}