SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 161
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16
Insert Picture Here
2
Towards JVM Dynamic
Languages Toolchain
!Attila Szegedi

Principal Member of Technical Staff
Insert Picture Here
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.3
The following is intended to outline our general product
direction. It is intended for information purposes only, and
may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality,
and should not be relied upon in making purchasing
decisions. The development, release, and timing of any
features or functionality described for Oracle’s products
remains at the sole discretion of Oracle.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.4
Why?
Source
code
Interpreter
Compiler
Runtime
JVM
Assumption: you talk to JVM in bytecode
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.5
Reuse
!V8 has 972k lines of code
!Nashorn has 213k lines of code
!Because underlying Java platform provides lots of services
!Imagine your language runtime tapping some of Nashorn’s 213k LOC.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.6
What can Nashorn do today?
!Parameter type specialized compilation
!Gradual deoptimization with on-stack code replacement
!a.k.a. optimistic typing
!Splitting large methods and array initializers
!Static code analysis
!Compiler optimizations
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.7
Parameter type specialized compilation
function square(x) {
return x*x;
}
print(square(500));
print(square(500.1));
public static square(Object;I)I
0 iload 1
1 iload 1
2 invokedynamic imul(II)I
7 ireturn
!
public static square(Object;D)D
0 dload 1
1 dload 1
2 dmul
3 dreturn
!Here’s code versions for f generated when invoked with int and double:
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.8
Parameter type specialized compilation
function square(x) {
return x*x;
}
!
var a = {
valueOf: function() {
return 500;
}
};
!
print(square(a));
public static square(Object;Object;)D
0 aload 1
1 invokestatic JSType.toNumber(Object;)D
4 aload 1
5 invokestatic JSType.toNumber(Object;)D
8 dmul
9 dreturn
!Here’s code version for f generated when invoked with object:
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.9
Parameter type specialized compilation
function square(x) {
return x*x;
}
var i = 500;
var a = {
valueOf: function() {
return i++;
}
}
!
print(square(a))
public static square(Object;Object;)D
0 aload 1
1 invokestatic JSType.toNumber(Object;)D
4 aload 1
5 invokestatic JSType.toNumber(Object;)D
8 dmul
9 dreturn
!toNumber is invoked twice: object-to-number can have side effects!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.10
Deoptimizing compilation: arithmetic overflow
function square(x) {
return x*x;
}
print(square(500));
public static square(Object;I)I
0 iload 1
1 iload 1
2 invokedynamic imul(II)I
7 ireturn
!can’t just use IMUL.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.11
Deoptimizing compilation: arithmetic overflow
public static square(Object;I)I
try L0 L1 L2 UnwarrantedOptimismException
!
0 iload 1
1 iload 1
L0
2 invokedynamic imul(II)I [static ‘mathBootstrap']
L1
7 ireturn
L2
8 iconst_2
9 anewarray Object
12 aload 0
13 iload 1
14 invokedynamic populateArray([Object;Object;I)[Object;[‘populateArrayBootstrp']
23 invokestatic RewriteException.create(UnwarrantedOptimismException;
[Object;)RewriteException;
26 athrow
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.12
Deoptimizing compilation: arithmetic overflow
public static int mulExact(final int x, final int y, final int programPoint)
throws UnwarrantedOptimismException
{
try {
return Math.multiplyExact(x, y);
} catch (final ArithmeticException e) {
throw new UnwarrantedOptimismException((long)x * (long)y, programPoint);
}
}
!Math.multiplyExact() is intrinsified by HotSpot
!We must perform the operation and return the result in the exception
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.13
Deoptimizing compilation: property type
function f(a) {
return a.foo * 2;
}
f({foo: 5.5})
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.14
Deoptimizing compilation: property type
public static f(Object;Object;)I
try L0 L1 L2 UnwarrantedOptimismException
try L3 L4 L2 UnwarrantedOptimismException
!
0 aload 1
L0
1 invokedynamic dyn:getProp|getElem|getMethod:foo(Object;)I [static “bootstrap” pp=2]
L1
6 iconst_2
L3
7 invokedynamic imul(II)I [static “mathBootstrap” pp=3]
L4
12 ireturn
L2
13 iconst_2
14 anewarray Object
17 aload 0
18 aload 1
19 invokedynamic populateArray([Object;Object;Object;)[Object;
...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.15
Deoptimizing compilation: property type
public static f(Object;Object;)D
0 aload 1
1 invokedynamic dyn:getProp|getElem|getMethod:foo(Object;)D [static 'bootstrap']
6 ldc 2.0
9 dmul
10 dreturn
!Since first argument is now double, second is also widened to double.
!Static analysis FTW!
!Multiplication can no longer overflow, so implicitly it also becomes non-
optimistic in a single deoptimizing recompilation pass.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.16
Deoptimizing compilation: rest-of method
public static f(RewriteException;)D
0 goto L0
3 nop
...
8 athrow
L2
9 ldc 2.0
12 dmul
13 dreturn
L0
14 aload 0
15 astore 2
16 aload 0
17 invokevirtual RewriteException.getByteCodeSlots()[Object;
20 dup
21 iconst_0
22 aaload
23 astore 0
25 iconst_1
26 aaload
27 astore 1
32 invokevirtual RewriteException.getReturnValueDestructive()Object;
35 invokestatic JSType.toNumber(Object;)D
38 goto L2
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.17
Sometimes we don’t want to be optimistic
!Static analysis can help us avoid optimism when it’s not needed.
!E.g. overflow semantics is sufficient because an operator would coerce
anyway: (i*i)|0 (logical or coerces to 32-bit int in JS)
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.18
Type inference
!Static analysis in a dynamic language is great when you’re targeting a
statically typed runtime.
!Nashorn calculates types of expressions and local variables. Local
variable types are propagated from def to use sites.
!Handles tricky control flow situations. Examples:
!Control transfer into catch blocks
!break from a finally
!Operates on AST
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.19
Handling undefined
function f(x) {
var i;
print(i);
var sum = 0;
for(i = 0; i < x; ++i) {
sum += i;
}
}
print(f(5))
!“undefined” is a type in the type
calculator with a single value.
!When known undefined is loaded,
we just load it as a constant.
!Otherwise, it’s an object. But that’s
rare.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.20
Variables with multiple types
function f(x) {
var i;
var y;
if(x) {
i = 1;
y = i * 2; // i int here
}
return i; // i object here
}
print(f(5))
!Even then, we preserve it as int
within the branch range and add an
implicit boxing before the join point.
!This preservation of narrower types
in ranges is true for all types.
!Variables can be stored in slots of
different types at different times.
!Here, we emit a synthetic “else”
block that initializes i to
Undefined.INSTANCE.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.21
Parameters with multiple types
function f(x, y) {
x = x * 2.1;
return x * y;
}
print(f(5, 6))
!Currently, storage for locals of
multiple types is contiguous.
!Method prologue is emitted to shift
incoming parameters to the right as
necessary.
public static f(Object;II)D
iload 2
istore 4
!
iload 1
i2d
ldc 2.1
dmul
!
dstore 2
dload 2
iload 4
i2d
dmul
dreturn
!
local x L0 L2 1 I
local :this L0 L4 0 Object;
local x L2 L4 2 D
local y L0 L4 4 I
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.22
Dead code elimination
!Due to type specialization functions are compiled only on first invocation.
!Dead stores are eliminated, as well as some side-effect free parts of their
right sides.
!Since we don’t have full liveness analysis on AST, we resorted to
weaker “type liveness” analysis for variables to avoid unnecessary
conversions at joins. Almost as a side effect, partial dead store
elimination came out of it.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.23
Nashorn compiler pipeline
Source
code
Parser AST Atf1 Atfn… AST CodeGen Bytecode
!After parser, there are lots of AST transformation steps (lowering,
splitting, symbol assignment, type calculation)
!Code generator translate AST to JVM bytecode. It’s heavy machinery.
!Emits code for optimistic operations, continuation handling, loads vs.
stores, self-assignment stores (++, += operations), split functions
control flow handover, …
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.24
Separate AST linearization
Atfn… AST DynCodeGen Bytecode
!Code generator heavy lifting doesn’t need to produce JVM bytecode.
!It could produce “dynamically typed” bytecode.
!Separate optimization steps could work on this bytecode.
!Some optimizations work better on a basic-block representation.
!Finally, a light straightforward new codegen could emit JVM bytecode
from dynamic bytecode.
Dynamic
bytecode
Dtfn … CodeGen
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.25
Benefits of new intermediate representation
!Some calculations are impossible or near impossible on AST, but easy
and well understood on basic blocks.
!E.g. liveness analysis needs backwards control flow traversal.
!AST is language dependent, bytecode isn’t. Or less so.
!The proposed dynamic bytecode is still very close to JVM bytecode, so
can be targeted by many languages.
!This is the reusability/toolchain idea.
!Similar in idea to LLVM’s IR.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.26
Arithmetic example:
!Source code:
function f(x, y) {
return x * y;
}
!Dynamic bytecode:
f(INT x, INT y)
LOAD x
LOAD y
MUL
RETURN
!JVM bytecode:
public static f(Object;II)I
iload 1
iload 2
invokedynamic imul(II)I ['mathBootstrap']
ireturn
!
catch UnwarrantedOptimismException
iconst_3
anewarray Object
aload 0
iload 1
iload 2
invokedynamic populateArray([Object;Object;II)[Object;
invokestatic RewriteException.create(...)RewriteException;
athrow
!
local :this L3 L2 0 Object;
local x L3 L2 1 I
local y L3 L2 2 I
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.27
Benefits of new intermediate representation
!You can emit symbolic identifiers for variables; final codegen will take
care of mapping to JVM local variable slots.
!It’ll infer types for variables and other values (with pluggable language
specific rules for operations).
!It’ll emit optimistic operations when needed, set up exception handlers,
and emit continuation restart methods.
!It’ll infer JVM return types for functions.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.28
Property getter example:
!Source code:
function f(x, y) {
return x.foo * y;
}
!Dynamic bytecode:
f(OBJECT x, INT y)
LOAD x
GETPROP foo
LOAD y
MUL
RETURN
!JVM bytecode:
public static f(Object;II)I
aload 1
invokedynamic dyn:getProp|getElem|getMethod:foo(Object;)I
iload 2
invokedynamic imul(II)I ['mathBootstrap']
ireturn
!
catch UnwarrantedOptimismException
iconst_3
anewarray Object
aload 0
aload 1
iload 2
invokedynamic populateArray([Object;Object;Object;I)[Object;
invokestatic RewriteException.create(...)RewriteException;
athrow
!
local :this L3 L2 0 Object;
local x L3 L2 1 Object;
local y L3 L2 2 I
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.29
Code regeneration reasons
!foo might turn out not to be an int (but a double, or even an object)
!multiplication can overflow.
!If needed, function type during regeneration will also evolve from int to
long or double.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.30
Lexical scope getter example:
!Source code:
function f(x, y) {
function g(z) {
return x.foo * z;
}
return g(z);
}
!Dynamic bytecode:
g(INT y)
LOAD x
GETPROP foo
LOAD y
MUL
RETURN
!JVM bytecode:
public static f$g(ScriptFunction;Object;I)I
aload 0
invokevirtual ScriptFunction.getScope()ScriptObject;
astore 3
aload 3
invokedynamic dyn:getProp|getElem|getMethod:x(Object;)Object;
invokedynamic dyn:getProp|getElem|getMethod:foo(Object;)I
iload 2
invokedynamic imul(II)I
ireturn
!
catch UnwarrantedOptimismException
…
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.31
Lexical scope
!Most dynamic languages have the concept of accessing variables from
outer lexical scope.
!JVM bytecode doesn’t.
!We can bridge that.
!In dynamic bytecode, you can symbolically reference those variables as
if they were locals.
!JVM code generator will emit necessary “load scope, get variable as
property from it” sequence, complete with optimism if needed.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.32
Dynamic bytecode to JVM bytecode
LOAD x ! ILOAD 3 or
DLOAD 3 etc.
!
LOAD x ! ALOAD 2 // scope
INVOKEDYNAMIC getprop:x
!
MUL ! IMUL or
INVOKEDYNAMIC imul
!
ALOAD x ALOAD 5
GETPROP y ! INVOKEDYNAMIC getprop:y
!Types are inferred; statically non-provable types are optimistically
presumed; some operations are emitted as indy invocations
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.33
Other things we can do automatically
!Splitting of large methods into less than 64k JVM bytecode chunks.
!Current Nashorn AST splitter makes conservative size estimates.
!Dynamic bytecode size much better approximates JVM bytecode size.
!Passing local variables used by split chunks needs artificial lexical scope
objects.
!Split functions are also subject to deoptimizing recompilation.
!Trickier as possibly multiple stack frames need to be saved/restored.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.34
Things you don’t even think about
!Remember how we capture local variables in the optimism exception
handler?
!Know what we can’t capture?
!Stack state.
!We’ll add artificial stores into temporary local variable slots for anything
that needs to remain on stack, e.g.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.35
Things you don’t even think about
function f(x, y) {
return (x * x) + (y * y);
}
!If squaring y overflows, we already have square of x on stack, and it must
be captured and restored.
!If a value on stack isn’t already a load of a local variable, we store it into
a new temporary and load it back on stack, thus stashing them away in a
place accessible to the exception handler.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.36
Things you don’t even think about
public static f(Object;II)I
try L0 L1 L2 UnwarrantedOptimismException
try L3 L4 L5 UnwarrantedOptimismException
try L4 L6 L2 UnwarrantedOptimismException
!
0 iload 1
1 iload 1
L0 [bci=2]
2 invokedynamic imul(II)I
L1 [bci=7]
7 istore 3
8 iload 3
9 iload 2
10 iload 2
L3 [bci=11]
11 invokedynamic imul(II)I
L4 [bci=16]
16 invokedynamic iadd(II)I
L6 [bci=21]
21 ireturn
L5 [bci=22]
22 iconst_4
23 anewarray Object
26 iload 3
27 invokedynamic populateArray([Object;I)[Object;
32 goto L8
L2 [bci=35]
35 iconst_3
36 anewarray Object
L8 [bci=39]
39 aload 0
40 iload 1
41 iload 2
42 invokedynamic populateArray([Object;Object;II)
47 iconst_0
51 invokestatic RewriteException.create(…);
54 athrow
!
local :this L7 L5 0 Object;
local x L7 L5 1 I
local y L7 L5 2 I
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.37
Could be improved
function f(x,y) {
function g() {}
return g(x*x, x, y*y);
}
iload 1
iload 1
invokedynamic imul(II)I
!
iload 1
pop
istore 5
iload 5
iload 1
!
iload 2
iload 2
invokedynamic imul(II)I
!Generates:
iload 1
iload 1
invokedynamic imul(II)I
!
istore 5
iload 1
!
iload 2
iload 2
invokedynamic imul(II)I
!Could be:
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.38
Could be improved
!We detect the need to store the value too late (after load of the next one
was emitted).
!Hard to solve with AST, as it doesn't have the stack-machine model in it.
Easier with a CFG of basic blocks in an already stack-machine language.
!We could also avoid synthetic stores for all values that are constants or
pure functions of local variables. (Currently: locals only).
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.39
Linking aspect
!There is also a linking aspect involving MethodHandles.catchException
combinator that jumps into deoptimizing recompilation and runs a rest-of
method when a function exits with RewriteException.
!Won’t go into details here.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.40
Summary
!Some things are hard:
!static analysis, type-specialized on-demand compilation, optimistic
types with on-stack-replacement deoptimizing recompilation, etc.
!We have solved a bunch of those with Nashorn, and would like to offer
them as a reusable library.
!The library needs an input code representation to work on.
!AST is too high level, JVM bytecode is too statically typed.
!We think something “almost bytecode”, but with optional types.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.41
Summary
!We want to give you a framework that:
!takes dynamic bytecode as its input with given parameter types
!proves JVM types of expressions statically
!where they can’t be proven or could overflow, inserts optimistic
operations
!allows for running various optimizing transforms on it
!emits the final JVM bytecode, with all the smarts presented earlier.
!helps you with method handle combinators for linking call sites to
functions that can get deoptimized.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1642
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1643

Weitere ähnliche Inhalte

Was ist angesagt?

Graal Tutorial at CGO 2015 by Christian Wimmer
Graal Tutorial at CGO 2015 by Christian WimmerGraal Tutorial at CGO 2015 by Christian Wimmer
Graal Tutorial at CGO 2015 by Christian Wimmer
Thomas Wuerthinger
 
JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013
Vladimir Ivanov
 
Java compilation
Java compilationJava compilation
Java compilation
Mike Kucera
 
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...
Thomas Wuerthinger
 
"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine
"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine
"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine
Vladimir Ivanov
 

Was ist angesagt? (20)

JVM++: The Graal VM
JVM++: The Graal VMJVM++: The Graal VM
JVM++: The Graal VM
 
Intrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VMIntrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VM
 
Jvm Architecture
Jvm ArchitectureJvm Architecture
Jvm Architecture
 
Java byte code & virtual machine
Java byte code & virtual machineJava byte code & virtual machine
Java byte code & virtual machine
 
Graal Tutorial at CGO 2015 by Christian Wimmer
Graal Tutorial at CGO 2015 by Christian WimmerGraal Tutorial at CGO 2015 by Christian Wimmer
Graal Tutorial at CGO 2015 by Christian Wimmer
 
Just-in-time compiler (March, 2017)
Just-in-time compiler (March, 2017)Just-in-time compiler (March, 2017)
Just-in-time compiler (March, 2017)
 
An Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeAn Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and Runtime
 
Apache Harmony: An Open Innovation
Apache Harmony: An Open InnovationApache Harmony: An Open Innovation
Apache Harmony: An Open Innovation
 
CS Lesson: Introduction to the Java virtual Machine
CS Lesson: Introduction to the Java virtual MachineCS Lesson: Introduction to the Java virtual Machine
CS Lesson: Introduction to the Java virtual Machine
 
JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013
 
FOSDEM2016 - Ruby and OMR
FOSDEM2016 - Ruby and OMRFOSDEM2016 - Ruby and OMR
FOSDEM2016 - Ruby and OMR
 
Graal and Truffle: One VM to Rule Them All
Graal and Truffle: One VM to Rule Them AllGraal and Truffle: One VM to Rule Them All
Graal and Truffle: One VM to Rule Them All
 
Java compilation
Java compilationJava compilation
Java compilation
 
JVM: A Platform for Multiple Languages
JVM: A Platform for Multiple LanguagesJVM: A Platform for Multiple Languages
JVM: A Platform for Multiple Languages
 
Micro-Benchmarking Considered Harmful
Micro-Benchmarking Considered HarmfulMicro-Benchmarking Considered Harmful
Micro-Benchmarking Considered Harmful
 
What is-java
What is-javaWhat is-java
What is-java
 
Graal VM: Multi-Language Execution Platform
Graal VM: Multi-Language Execution PlatformGraal VM: Multi-Language Execution Platform
Graal VM: Multi-Language Execution Platform
 
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...
 
"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine
"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine
"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine
 
Dvm
DvmDvm
Dvm
 

Andere mochten auch

JavaOne 2014: Java Debugging
JavaOne 2014: Java DebuggingJavaOne 2014: Java Debugging
JavaOne 2014: Java Debugging
Chris Bailey
 
An Introduction To Java Profiling
An Introduction To Java ProfilingAn Introduction To Java Profiling
An Introduction To Java Profiling
schlebu
 

Andere mochten auch (19)

Everything I Ever Learned About JVM Performance Tuning @Twitter
Everything I Ever Learned About JVM Performance Tuning @TwitterEverything I Ever Learned About JVM Performance Tuning @Twitter
Everything I Ever Learned About JVM Performance Tuning @Twitter
 
Java performance tuning
Java performance tuningJava performance tuning
Java performance tuning
 
Java Performance Monitoring & Tuning
Java Performance Monitoring & TuningJava Performance Monitoring & Tuning
Java Performance Monitoring & Tuning
 
STM in Haskell
STM in HaskellSTM in Haskell
STM in Haskell
 
Understanding Java Garbage Collection and What You Can Do About It: Gil Tene
Understanding Java Garbage Collection and What You Can Do About It: Gil TeneUnderstanding Java Garbage Collection and What You Can Do About It: Gil Tene
Understanding Java Garbage Collection and What You Can Do About It: Gil Tene
 
Hands-on Performance Tuning Lab - Devoxx Poland
Hands-on Performance Tuning Lab - Devoxx PolandHands-on Performance Tuning Lab - Devoxx Poland
Hands-on Performance Tuning Lab - Devoxx Poland
 
Diagnosing Your Application on the JVM
Diagnosing Your Application on the JVMDiagnosing Your Application on the JVM
Diagnosing Your Application on the JVM
 
Java profiling Do It Yourself
Java profiling Do It YourselfJava profiling Do It Yourself
Java profiling Do It Yourself
 
JavaOne 2014: Java Debugging
JavaOne 2014: Java DebuggingJavaOne 2014: Java Debugging
JavaOne 2014: Java Debugging
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuning
 
Debugging Your Production JVM
Debugging Your Production JVMDebugging Your Production JVM
Debugging Your Production JVM
 
Hotspot Garbage Collection - The Useful Parts
Hotspot Garbage Collection - The Useful PartsHotspot Garbage Collection - The Useful Parts
Hotspot Garbage Collection - The Useful Parts
 
Seven Ineffective Coding Habits of Many Programmers
Seven Ineffective Coding Habits of Many ProgrammersSeven Ineffective Coding Habits of Many Programmers
Seven Ineffective Coding Habits of Many Programmers
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java Applications
 
Real Life Java EE Performance Tuning
Real Life Java EE Performance TuningReal Life Java EE Performance Tuning
Real Life Java EE Performance Tuning
 
An Introduction To Java Profiling
An Introduction To Java ProfilingAn Introduction To Java Profiling
An Introduction To Java Profiling
 
Distributed Consensus A.K.A. "What do we eat for lunch?"
Distributed Consensus A.K.A. "What do we eat for lunch?"Distributed Consensus A.K.A. "What do we eat for lunch?"
Distributed Consensus A.K.A. "What do we eat for lunch?"
 
BDD - Writing better scenario
BDD - Writing better scenarioBDD - Writing better scenario
BDD - Writing better scenario
 
[D2]java 성능에 대한 오해와 편견
[D2]java 성능에 대한 오해와 편견[D2]java 성능에 대한 오해와 편견
[D2]java 성능에 대한 오해와 편견
 

Ähnlich wie Towards JVM Dynamic Languages Toolchain

Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
JAXLondon2014
 
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbersDefcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Alexandre Moneger
 
14.jun.2012
14.jun.201214.jun.2012
14.jun.2012
Tech_MX
 

Ähnlich wie Towards JVM Dynamic Languages Toolchain (20)

invokedynamic for Mere Mortals [Code One 2019]
invokedynamic for Mere Mortals [Code One 2019]invokedynamic for Mere Mortals [Code One 2019]
invokedynamic for Mere Mortals [Code One 2019]
 
Lambdas : Beyond The Basics
Lambdas : Beyond The BasicsLambdas : Beyond The Basics
Lambdas : Beyond The Basics
 
What's new for JavaFX in JDK8 - Weaver
What's new for JavaFX in JDK8 - WeaverWhat's new for JavaFX in JDK8 - Weaver
What's new for JavaFX in JDK8 - Weaver
 
Autonomous Drone Development with Java and IoT
Autonomous Drone Development with Java and IoTAutonomous Drone Development with Java and IoT
Autonomous Drone Development with Java and IoT
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2
 
Xtext Webinar
Xtext WebinarXtext Webinar
Xtext Webinar
 
Intel open mp
Intel open mpIntel open mp
Intel open mp
 
Java
JavaJava
Java
 
Xtext Webinar
Xtext WebinarXtext Webinar
Xtext Webinar
 
02 - Introduction to the cdecl ABI and the x86 stack
02 - Introduction to the cdecl ABI and the x86 stack02 - Introduction to the cdecl ABI and the x86 stack
02 - Introduction to the cdecl ABI and the x86 stack
 
05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR matters05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR matters
 
Java Magazine : The JAVA Virtual Machine alternative languages
Java Magazine : The JAVA Virtual Machine alternative languagesJava Magazine : The JAVA Virtual Machine alternative languages
Java Magazine : The JAVA Virtual Machine alternative languages
 
掀起 Swift 的面紗
掀起 Swift 的面紗掀起 Swift 的面紗
掀起 Swift 的面紗
 
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
 
Lambdas And Streams in JDK8
Lambdas And Streams in JDK8Lambdas And Streams in JDK8
Lambdas And Streams in JDK8
 
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbersDefcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbers
 
14.jun.2012
14.jun.201214.jun.2012
14.jun.2012
 
JavaOne 2015: From Java Code to Machine Code
JavaOne 2015: From Java Code to Machine CodeJavaOne 2015: From Java Code to Machine Code
JavaOne 2015: From Java Code to Machine Code
 
Manage software dependencies with ioc and aop
Manage software dependencies with ioc and aopManage software dependencies with ioc and aop
Manage software dependencies with ioc and aop
 

Kürzlich hochgeladen

Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 

Kürzlich hochgeladen (20)

WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 

Towards JVM Dynamic Languages Toolchain

  • 1. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 161
  • 2. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16 Insert Picture Here 2 Towards JVM Dynamic Languages Toolchain !Attila Szegedi
 Principal Member of Technical Staff Insert Picture Here
  • 3. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.3 The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
  • 4. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.4 Why? Source code Interpreter Compiler Runtime JVM Assumption: you talk to JVM in bytecode
  • 5. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.5 Reuse !V8 has 972k lines of code !Nashorn has 213k lines of code !Because underlying Java platform provides lots of services !Imagine your language runtime tapping some of Nashorn’s 213k LOC.
  • 6. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.6 What can Nashorn do today? !Parameter type specialized compilation !Gradual deoptimization with on-stack code replacement !a.k.a. optimistic typing !Splitting large methods and array initializers !Static code analysis !Compiler optimizations
  • 7. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.7 Parameter type specialized compilation function square(x) { return x*x; } print(square(500)); print(square(500.1)); public static square(Object;I)I 0 iload 1 1 iload 1 2 invokedynamic imul(II)I 7 ireturn ! public static square(Object;D)D 0 dload 1 1 dload 1 2 dmul 3 dreturn !Here’s code versions for f generated when invoked with int and double:
  • 8. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.8 Parameter type specialized compilation function square(x) { return x*x; } ! var a = { valueOf: function() { return 500; } }; ! print(square(a)); public static square(Object;Object;)D 0 aload 1 1 invokestatic JSType.toNumber(Object;)D 4 aload 1 5 invokestatic JSType.toNumber(Object;)D 8 dmul 9 dreturn !Here’s code version for f generated when invoked with object:
  • 9. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.9 Parameter type specialized compilation function square(x) { return x*x; } var i = 500; var a = { valueOf: function() { return i++; } } ! print(square(a)) public static square(Object;Object;)D 0 aload 1 1 invokestatic JSType.toNumber(Object;)D 4 aload 1 5 invokestatic JSType.toNumber(Object;)D 8 dmul 9 dreturn !toNumber is invoked twice: object-to-number can have side effects!
  • 10. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.10 Deoptimizing compilation: arithmetic overflow function square(x) { return x*x; } print(square(500)); public static square(Object;I)I 0 iload 1 1 iload 1 2 invokedynamic imul(II)I 7 ireturn !can’t just use IMUL.
  • 11. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.11 Deoptimizing compilation: arithmetic overflow public static square(Object;I)I try L0 L1 L2 UnwarrantedOptimismException ! 0 iload 1 1 iload 1 L0 2 invokedynamic imul(II)I [static ‘mathBootstrap'] L1 7 ireturn L2 8 iconst_2 9 anewarray Object 12 aload 0 13 iload 1 14 invokedynamic populateArray([Object;Object;I)[Object;[‘populateArrayBootstrp'] 23 invokestatic RewriteException.create(UnwarrantedOptimismException; [Object;)RewriteException; 26 athrow
  • 12. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.12 Deoptimizing compilation: arithmetic overflow public static int mulExact(final int x, final int y, final int programPoint) throws UnwarrantedOptimismException { try { return Math.multiplyExact(x, y); } catch (final ArithmeticException e) { throw new UnwarrantedOptimismException((long)x * (long)y, programPoint); } } !Math.multiplyExact() is intrinsified by HotSpot !We must perform the operation and return the result in the exception
  • 13. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.13 Deoptimizing compilation: property type function f(a) { return a.foo * 2; } f({foo: 5.5})
  • 14. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.14 Deoptimizing compilation: property type public static f(Object;Object;)I try L0 L1 L2 UnwarrantedOptimismException try L3 L4 L2 UnwarrantedOptimismException ! 0 aload 1 L0 1 invokedynamic dyn:getProp|getElem|getMethod:foo(Object;)I [static “bootstrap” pp=2] L1 6 iconst_2 L3 7 invokedynamic imul(II)I [static “mathBootstrap” pp=3] L4 12 ireturn L2 13 iconst_2 14 anewarray Object 17 aload 0 18 aload 1 19 invokedynamic populateArray([Object;Object;Object;)[Object; ...
  • 15. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.15 Deoptimizing compilation: property type public static f(Object;Object;)D 0 aload 1 1 invokedynamic dyn:getProp|getElem|getMethod:foo(Object;)D [static 'bootstrap'] 6 ldc 2.0 9 dmul 10 dreturn !Since first argument is now double, second is also widened to double. !Static analysis FTW! !Multiplication can no longer overflow, so implicitly it also becomes non- optimistic in a single deoptimizing recompilation pass.
  • 16. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.16 Deoptimizing compilation: rest-of method public static f(RewriteException;)D 0 goto L0 3 nop ... 8 athrow L2 9 ldc 2.0 12 dmul 13 dreturn L0 14 aload 0 15 astore 2 16 aload 0 17 invokevirtual RewriteException.getByteCodeSlots()[Object; 20 dup 21 iconst_0 22 aaload 23 astore 0 25 iconst_1 26 aaload 27 astore 1 32 invokevirtual RewriteException.getReturnValueDestructive()Object; 35 invokestatic JSType.toNumber(Object;)D 38 goto L2
  • 17. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.17 Sometimes we don’t want to be optimistic !Static analysis can help us avoid optimism when it’s not needed. !E.g. overflow semantics is sufficient because an operator would coerce anyway: (i*i)|0 (logical or coerces to 32-bit int in JS)
  • 18. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.18 Type inference !Static analysis in a dynamic language is great when you’re targeting a statically typed runtime. !Nashorn calculates types of expressions and local variables. Local variable types are propagated from def to use sites. !Handles tricky control flow situations. Examples: !Control transfer into catch blocks !break from a finally !Operates on AST
  • 19. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.19 Handling undefined function f(x) { var i; print(i); var sum = 0; for(i = 0; i < x; ++i) { sum += i; } } print(f(5)) !“undefined” is a type in the type calculator with a single value. !When known undefined is loaded, we just load it as a constant. !Otherwise, it’s an object. But that’s rare.
  • 20. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.20 Variables with multiple types function f(x) { var i; var y; if(x) { i = 1; y = i * 2; // i int here } return i; // i object here } print(f(5)) !Even then, we preserve it as int within the branch range and add an implicit boxing before the join point. !This preservation of narrower types in ranges is true for all types. !Variables can be stored in slots of different types at different times. !Here, we emit a synthetic “else” block that initializes i to Undefined.INSTANCE.
  • 21. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.21 Parameters with multiple types function f(x, y) { x = x * 2.1; return x * y; } print(f(5, 6)) !Currently, storage for locals of multiple types is contiguous. !Method prologue is emitted to shift incoming parameters to the right as necessary. public static f(Object;II)D iload 2 istore 4 ! iload 1 i2d ldc 2.1 dmul ! dstore 2 dload 2 iload 4 i2d dmul dreturn ! local x L0 L2 1 I local :this L0 L4 0 Object; local x L2 L4 2 D local y L0 L4 4 I
  • 22. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.22 Dead code elimination !Due to type specialization functions are compiled only on first invocation. !Dead stores are eliminated, as well as some side-effect free parts of their right sides. !Since we don’t have full liveness analysis on AST, we resorted to weaker “type liveness” analysis for variables to avoid unnecessary conversions at joins. Almost as a side effect, partial dead store elimination came out of it.
  • 23. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.23 Nashorn compiler pipeline Source code Parser AST Atf1 Atfn… AST CodeGen Bytecode !After parser, there are lots of AST transformation steps (lowering, splitting, symbol assignment, type calculation) !Code generator translate AST to JVM bytecode. It’s heavy machinery. !Emits code for optimistic operations, continuation handling, loads vs. stores, self-assignment stores (++, += operations), split functions control flow handover, …
  • 24. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.24 Separate AST linearization Atfn… AST DynCodeGen Bytecode !Code generator heavy lifting doesn’t need to produce JVM bytecode. !It could produce “dynamically typed” bytecode. !Separate optimization steps could work on this bytecode. !Some optimizations work better on a basic-block representation. !Finally, a light straightforward new codegen could emit JVM bytecode from dynamic bytecode. Dynamic bytecode Dtfn … CodeGen
  • 25. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.25 Benefits of new intermediate representation !Some calculations are impossible or near impossible on AST, but easy and well understood on basic blocks. !E.g. liveness analysis needs backwards control flow traversal. !AST is language dependent, bytecode isn’t. Or less so. !The proposed dynamic bytecode is still very close to JVM bytecode, so can be targeted by many languages. !This is the reusability/toolchain idea. !Similar in idea to LLVM’s IR.
  • 26. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.26 Arithmetic example: !Source code: function f(x, y) { return x * y; } !Dynamic bytecode: f(INT x, INT y) LOAD x LOAD y MUL RETURN !JVM bytecode: public static f(Object;II)I iload 1 iload 2 invokedynamic imul(II)I ['mathBootstrap'] ireturn ! catch UnwarrantedOptimismException iconst_3 anewarray Object aload 0 iload 1 iload 2 invokedynamic populateArray([Object;Object;II)[Object; invokestatic RewriteException.create(...)RewriteException; athrow ! local :this L3 L2 0 Object; local x L3 L2 1 I local y L3 L2 2 I
  • 27. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.27 Benefits of new intermediate representation !You can emit symbolic identifiers for variables; final codegen will take care of mapping to JVM local variable slots. !It’ll infer types for variables and other values (with pluggable language specific rules for operations). !It’ll emit optimistic operations when needed, set up exception handlers, and emit continuation restart methods. !It’ll infer JVM return types for functions.
  • 28. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.28 Property getter example: !Source code: function f(x, y) { return x.foo * y; } !Dynamic bytecode: f(OBJECT x, INT y) LOAD x GETPROP foo LOAD y MUL RETURN !JVM bytecode: public static f(Object;II)I aload 1 invokedynamic dyn:getProp|getElem|getMethod:foo(Object;)I iload 2 invokedynamic imul(II)I ['mathBootstrap'] ireturn ! catch UnwarrantedOptimismException iconst_3 anewarray Object aload 0 aload 1 iload 2 invokedynamic populateArray([Object;Object;Object;I)[Object; invokestatic RewriteException.create(...)RewriteException; athrow ! local :this L3 L2 0 Object; local x L3 L2 1 Object; local y L3 L2 2 I
  • 29. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.29 Code regeneration reasons !foo might turn out not to be an int (but a double, or even an object) !multiplication can overflow. !If needed, function type during regeneration will also evolve from int to long or double.
  • 30. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.30 Lexical scope getter example: !Source code: function f(x, y) { function g(z) { return x.foo * z; } return g(z); } !Dynamic bytecode: g(INT y) LOAD x GETPROP foo LOAD y MUL RETURN !JVM bytecode: public static f$g(ScriptFunction;Object;I)I aload 0 invokevirtual ScriptFunction.getScope()ScriptObject; astore 3 aload 3 invokedynamic dyn:getProp|getElem|getMethod:x(Object;)Object; invokedynamic dyn:getProp|getElem|getMethod:foo(Object;)I iload 2 invokedynamic imul(II)I ireturn ! catch UnwarrantedOptimismException …
  • 31. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.31 Lexical scope !Most dynamic languages have the concept of accessing variables from outer lexical scope. !JVM bytecode doesn’t. !We can bridge that. !In dynamic bytecode, you can symbolically reference those variables as if they were locals. !JVM code generator will emit necessary “load scope, get variable as property from it” sequence, complete with optimism if needed.
  • 32. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.32 Dynamic bytecode to JVM bytecode LOAD x ! ILOAD 3 or DLOAD 3 etc. ! LOAD x ! ALOAD 2 // scope INVOKEDYNAMIC getprop:x ! MUL ! IMUL or INVOKEDYNAMIC imul ! ALOAD x ALOAD 5 GETPROP y ! INVOKEDYNAMIC getprop:y !Types are inferred; statically non-provable types are optimistically presumed; some operations are emitted as indy invocations
  • 33. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.33 Other things we can do automatically !Splitting of large methods into less than 64k JVM bytecode chunks. !Current Nashorn AST splitter makes conservative size estimates. !Dynamic bytecode size much better approximates JVM bytecode size. !Passing local variables used by split chunks needs artificial lexical scope objects. !Split functions are also subject to deoptimizing recompilation. !Trickier as possibly multiple stack frames need to be saved/restored.
  • 34. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.34 Things you don’t even think about !Remember how we capture local variables in the optimism exception handler? !Know what we can’t capture? !Stack state. !We’ll add artificial stores into temporary local variable slots for anything that needs to remain on stack, e.g.
  • 35. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.35 Things you don’t even think about function f(x, y) { return (x * x) + (y * y); } !If squaring y overflows, we already have square of x on stack, and it must be captured and restored. !If a value on stack isn’t already a load of a local variable, we store it into a new temporary and load it back on stack, thus stashing them away in a place accessible to the exception handler.
  • 36. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.36 Things you don’t even think about public static f(Object;II)I try L0 L1 L2 UnwarrantedOptimismException try L3 L4 L5 UnwarrantedOptimismException try L4 L6 L2 UnwarrantedOptimismException ! 0 iload 1 1 iload 1 L0 [bci=2] 2 invokedynamic imul(II)I L1 [bci=7] 7 istore 3 8 iload 3 9 iload 2 10 iload 2 L3 [bci=11] 11 invokedynamic imul(II)I L4 [bci=16] 16 invokedynamic iadd(II)I L6 [bci=21] 21 ireturn L5 [bci=22] 22 iconst_4 23 anewarray Object 26 iload 3 27 invokedynamic populateArray([Object;I)[Object; 32 goto L8 L2 [bci=35] 35 iconst_3 36 anewarray Object L8 [bci=39] 39 aload 0 40 iload 1 41 iload 2 42 invokedynamic populateArray([Object;Object;II) 47 iconst_0 51 invokestatic RewriteException.create(…); 54 athrow ! local :this L7 L5 0 Object; local x L7 L5 1 I local y L7 L5 2 I
  • 37. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.37 Could be improved function f(x,y) { function g() {} return g(x*x, x, y*y); } iload 1 iload 1 invokedynamic imul(II)I ! iload 1 pop istore 5 iload 5 iload 1 ! iload 2 iload 2 invokedynamic imul(II)I !Generates: iload 1 iload 1 invokedynamic imul(II)I ! istore 5 iload 1 ! iload 2 iload 2 invokedynamic imul(II)I !Could be:
  • 38. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.38 Could be improved !We detect the need to store the value too late (after load of the next one was emitted). !Hard to solve with AST, as it doesn't have the stack-machine model in it. Easier with a CFG of basic blocks in an already stack-machine language. !We could also avoid synthetic stores for all values that are constants or pure functions of local variables. (Currently: locals only).
  • 39. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.39 Linking aspect !There is also a linking aspect involving MethodHandles.catchException combinator that jumps into deoptimizing recompilation and runs a rest-of method when a function exits with RewriteException. !Won’t go into details here.
  • 40. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.40 Summary !Some things are hard: !static analysis, type-specialized on-demand compilation, optimistic types with on-stack-replacement deoptimizing recompilation, etc. !We have solved a bunch of those with Nashorn, and would like to offer them as a reusable library. !The library needs an input code representation to work on. !AST is too high level, JVM bytecode is too statically typed. !We think something “almost bytecode”, but with optional types.
  • 41. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.41 Summary !We want to give you a framework that: !takes dynamic bytecode as its input with given parameter types !proves JVM types of expressions statically !where they can’t be proven or could overflow, inserts optimistic operations !allows for running various optimizing transforms on it !emits the final JVM bytecode, with all the smarts presented earlier. !helps you with method handle combinators for linking call sites to functions that can get deoptimized.
  • 42. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1642
  • 43. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1643