Bytecode & VM Design

devinsampa 2011

!SLIDE

for dummies?

!SLIDE

@qmx

!SLIDE

blog.qmx.me

!SLIDE

whoami?

!SLIDE

geek

!SLIDE

developer @

intelie !SLIDE

instructor @

caelum !SLIDE

opensource freak

opensource !SLIDE

contributor @

JRuby !SLIDE

yeah, a freak

cello1 !SLIDE

!

!SLIDE

hardcore stuff

warning !SLIDE

VMs

Unit 1

!SLIDE

Back to basics

!SLIDE

1 + 1?

!SLIDE

10

!SLIDE

logic gates

full-adder !SLIDE

law of the instrument

hammer.jpg !SLIDE

more flexibility?

!SLIDE

FPGA

!SLIDE

field-_programmable_ gate array

fpga !SLIDE

VHDL

!SLIDE

unix phylosophy

one thing well

!SLIDE

#fail

!SLIDE

generic processing units

!SLIDE

RISC

!SLIDE

RISC

  • simple instruction set (1 cycle)
  • runs at higher clock
  • power efficient !SLIDE

    CISC

    !SLIDE

    CISC

  • complex instruction set
  • microcode support
  • powerful !SLIDE

    Register-based CPUs

    !SLIDE

    1 + 2 (pseudo asm)

  • mov %eax, 1
  • mov %ebx, 2
  • add %eax, %eax, %ebx !SLIDE

    Register-based CPUs

  • Work juggling register slots
  • Memory efficient !SLIDE

    WHY?

    !SLIDE

    Stack Based CPUs

    !SLIDE

    RPN calculators

    hp !SLIDE

    sum(a,b)

  • iload_1
  • iload_2
  • iadd
  • ireturn !SLIDE

    The Coffee Machine Problem

    !SLIDE

    Free Market

    !SLIDE

    Free Market

    coffee-machines !SLIDE

    Increasing Abstraction

    !SLIDE

    HAL?

    !SLIDE

    HAL!

    hal !SLIDE

    Poor man’s Computer

    !SLIDE

    interpreter loop

    !SLIDE

    tiny VM

    /* simple switch / switch(vm->pProgram->instr[instr_idx]) { case 0x0: break; /* nop / case 0x1: break; / int / case 0x2: *arg0 = *arg1; break; / mov / case 0x3: stack_push(vm->pMemory, arg0); break; / push / case 0x4: stack_pop(vm->pMemory, arg0); break; / pop */ !SLIDE

    JVM

    !SLIDE

    JVM

  • stack-based VM
  • optimizing JIT compiler
  • Award-winning Garbage Collection !SLIDE

    Unit 2

    Bytecode

    !SLIDE

    the tr00 JVM language

    !SLIDE

    limited instruction set

    !SLIDE

    256 opcodes, ~206 used

    !SLIDE

    static types

    !SLIDE

    constant pool

    !SLIDE

    constant pool

    const #6 = Asciz main; const #7 = Asciz ([Ljava/lang/String;)V; const #8 = Asciz Hello, world!; const #9 = String #8; / Hello, world! const #10 = Asciz java/lang/System; const #11 = class #10; / java/lang/System const #12 = Asciz out; const #13 = Asciz Lja../../img/PrintStream;; const #14 = NameAndType #12:#13;/ out:Lja../../img/PrintStream; !SLIDE

    stack-based operation

    !SLIDE

    stack-based operation

    public static void main(java.lang.String[]); Code: Stack=2, Locals=1, Args_size=1 0: ldc #9; /String Hello, world! 2: getstatic #15; /Field java/lang/System.out:Lja../../img/PrintStream; 5: swap 6: invokevirtual #21; /Method ja../../img/PrintStream.println:(Ljava/lang/Object;)V 9: return } !SLIDE

    invocation strategies

    !SLIDE

    static

    invokestatic !SLIDE

    virtual

    invokevirtual !SLIDE

    interface

    invokeinterface1 !SLIDE

    interface

    invokeinterface2 !SLIDE

    interface

    invokeinterface3 !SLIDE

    interface

    invokeinterface5 !SLIDE

    New stuff on JDK7

    !SLIDE

    dynamic invocation

    !SLIDE

    dynamic

    invokedynamic1 !SLIDE

    dynamic

    invokedynamic2 !SLIDE

    dynamic

    invokedynamic3 !SLIDE

    Unit 3

    Optimization

    !SLIDE

    the least effort rule

    !SLIDE

    if it’s unnecessary, skip it

    garfield !SLIDE

    AOT

    !SLIDE

    heuristics based

    !SLIDE

    heuristics?

    crystal ball !SLIDE

    JIT

    !SLIDE

    knowledge based

    !SLIDE

    finding the hot spots

    !SLIDE

    Inlining

    !SLIDE

    inlining

    class Engine def start puts “starting” end end e = Engine.new 1000.times { e.start } !SLIDE

    inlining

    1000.times { puts “starting” } !SLIDE

    de-optimization

    !SLIDE

    Garbage Collection

    !SLIDE

    sorry, iOS

    !SLIDE

    pick yours

    !SLIDE

    Serial GC

    !SLIDE

    Parallel GC

    !SLIDE

    Low Pause

    !SLIDE

    G1 GC

    !SLIDE

    That’s all folks!

    !SLIDE

    ?

    !SLIDE

    Thank you!