hosting

dynamic

languages & apps

on the

JVM

whoami

@qmx
dynjs

disclaimer

once upon a

time

computers?

languages?

movl   %eax, -35(%rbp)

ETOOHIGHLEVEL

abstractions

spoiled

our generation

so...

you want to implement a

JVM-based

language?

why

JVM?

WORA?

awesomesauce?

abstractions

reality check

wait, WAAT?

bytes

abstractions

are

awesome

abstractions

considered

harmful?

we need to go

deeper

JVM?

Java?

I've got 99 problems

Java ain't one

deconstructing

the

JVM

CPU

movl   %eax, -36(%rbp)

register-based

JVM

stack-based

Virtual Machine

1+2 = ?

(+ 1 2)

ZOMG, lisp???

le stack

PUSH 1
  • 1
PUSH 2
  • 1
  • 2
ADD
  • 3

le stack - bytecode

iconst_1
  • 1
iconst_2
  • 1
  • 2
iadd
  • 3

bytecode?

iadd?

integer add

statically
typed!

what

statically
typed

means?

going

dynamic

1 + 2

is 1 an integer?

NO

					> 1.class
=> Fixnum
					

OO

taken to the next level

1+2

						1.+(2)
					

1+2

						1.send(:+, 2)
					

writing this in Java

						
IRubyObject +(IRubyObject o){}
						
					

syntax error

Java

Y U NO

OPERATOR OVERLOADING

we <3 Java

but

Java (language) is

limited

(and that's ok)

writing this in Java

						
IRubyObject op_plus(IRubyObject o){}
						
					

verbose

1 + 2

						
IRubyObject x  = // 1
IRubyObject y = // 2
x.op_plus(y);
					
					

IRubyObject?

everything is an object

Java

Virtual Machine

requires

ALL THE TYPES

during compilation

JVM

AOT vs JIT

speaker notes remind me to troll the CLR

optimize

GUESS?

Here be Dragons

IR

flowGRAPH

we know this!?

S tatic S ingle

A ssignment

Maths?

E scape A nalysis

Java

Y U NO

UNSIGNED

Case Study:


Ruby Numb3rs

Ruby Numb3rs


1.9.3-p362 :010 > (2**2**5).class
=> Fixnum
1.9.3-p362 :011 > (4**2**5).class
=> Bignum
					

Java

Y U NO

V-FLAG

D ead

C ode

E limination

LICM

ERMAHGERD

back to sanity?

do you know

where

methods live?

Object.methods()

classes!

so

{:+ => #<Object:0x007f95329a4510>, ...}

map!

back to Java

let's call it

						IRubyObject x = //;
x.+(); // fails
x.callMethod(context, "+"); // works

					

what's

happening

here?

steps

  1. get the object
  2. hash lookup
  3. context lookup
  4. actual invocation
  5. profit?

what if...?

we could teach

the JVM

on how to reach the method instance we want?

what if...?

we could hint

the JVM

about this very codepath

invokedynamic

bytecode

method handles

bootstrap method

target method

MethodHandles

int (*foo)(int)

good ol'

pointers

bytecode

bytecode - standard invoke

						INVOKEVIRTUAL #39, #42
					

#39 is method name, #42 is the signature with types

bytecode - invokedynamic

						INVOKEDYNAMIC name, #42, BSM, BSM_SIG
					

Bootstrap Method

CallSite

a CallSite has a

Method Handle

which points to the

TargetMethod

magic?

invokedynamic's

"friends"

aka APIs

SwitchPoint

case study:
Ruby inconstants

Ruby inconstants


1.9.3-p362 :001 > THING="hello"
 => "hello"
1.9.3-p362 :002 > THING="hellow"
(irb):2: warning: already initialized constant THING
 => "hellow"
					

invokedynamic

partial solution

a new approach rises

METACIRCULAR

YO DAWG

Maxine VM

peeking on hotspot guts

Graal VM

language runtime framework

Truffle

partial

conclusion

what about the

apps?

uncle Ben's

with great

productivity

comes great

responsibility

optimizing dynlangs

is hard

but doable

@headius guaranteed

having

structure

is a must

check

async polyglot framework

we love

dynamic

languages

but there's

NO

silver language

take your daily

abstraction

fix

just don't be

spoiled

by it

that's all, folks!

thanks!

blog.qmx.me