Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP で JVM を実装して Hello World を出力するまで

memory
August 31, 2019

PHP で JVM を実装して Hello World を出力するまで

at builderscon tokyo 2019

English Title:
How to implement JVM by the PHP, and until output HelloWorld.

memory

August 31, 2019
Tweet

More Decks by memory

Other Decks in Programming

Transcript

  1. / / builderscon Tokyo
    PHP JVM Hello World
    © - BASE, Inc.

    View full-size slide

  2. © - BASE, Inc.
    Who am I?
    (@m m r )
    BASE
    PHP, React

    View full-size slide

  3. © - BASE, Inc.
    PHP
    JVM

    View full-size slide

  4. © - BASE, Inc.
    Hello World
    JVM
    PHP JVM

    View full-size slide

  5. © - BASE, Inc.
    PHP
    JVM Specification
    Java

    View full-size slide

  6. © - BASE, Inc.
    PHP JVM
    PHPerKaigi
    https://speakerdeck.com/memory /phperkaigi-

    View full-size slide

  7. © - BASE, Inc.
    PHP JVM OSS ( )
    https://github.com/php-java/php-java

    View full-size slide

  8. © - BASE, Inc.
    PHP
    JVM

    View full-size slide

  9. © - BASE, Inc.
    PHP

    View full-size slide

  10. © - BASE, Inc.

    View full-size slide

  11. © - BASE, Inc.
    2014
    2015 1 30
    nbproject Netbeans C:\\ 

    Windows
    1 

    (Hello World )

    ( )

    View full-size slide

  12. © - BASE, Inc.

    View full-size slide

  13. © - BASE, Inc.

    View full-size slide

  14. © - BASE, Inc.

    View full-size slide

  15. © - BASE, Inc.

    View full-size slide

  16. © - BASE, Inc.

    View full-size slide

  17. © - BASE, Inc.

    View full-size slide

  18. © - BASE, Inc.

    View full-size slide

  19. © - BASE, Inc.
    Wikipedia JVM 

    PHP
    ( ) https://en.wikipedia.org/wiki/List_of_Java_virtual_machines
    JPHP Java PHP
    ( ) https://github.com/jphp-group/jphp

    View full-size slide

  20. © - BASE, Inc.
    PHP

    View full-size slide

  21. © - BASE, Inc.
    PHP
    PHP
    PHP
    Laravel 

    PHP 

    JVM

    View full-size slide

  22. © - BASE, Inc.
    PHP
    JVM

    View full-size slide

  23. © - BASE, Inc.
    JVM
    JVM class 

    (= )

    View full-size slide

  24. © - BASE, Inc.
    JVM
    class
    javac java
    JAR (Java ARchive) class
    kotlinc -include-runtime class 

    JAR
    Scala sbt-assembly JAR

    View full-size slide

  25. © - BASE, Inc.
    JVM
    JVM
    C CPU
    Oracle 

    PHP
    Rust ( )
    invoke_*

    View full-size slide

  26. © - BASE, Inc.
    JVM
    3
    PHP
    PHP class

    View full-size slide

  27. © - BASE, Inc.
    JVM
    PHP
    PHP class

    View full-size slide

  28. © - BASE, Inc.
    JVM
    PHP class
    PHPerKaigi 

    https://speakerdeck.com/memory /phperkaigi-

    View full-size slide

  29. © - BASE, Inc.
    PHP
    JVM

    View full-size slide

  30. © - BASE, Inc.
    PHP
    PHP Object
    java.lang.Object?
    String Void
    Integer, Long OK

    View full-size slide

  31. © - BASE, Inc.
    PHP
    Swoole parallel


    View full-size slide

  32. © - BASE, Inc.
    PHP
    PHP Exception
    Java java.lang.Object

    View full-size slide

  33. © - BASE, Inc.
    Hello World
    JVM
    PHP JVM

    View full-size slide

  34. © - BASE, Inc.
    1
    JVM 算

    View full-size slide

  35. © - BASE, Inc.
    CPU
    mov rax, rbx
    ( 0x )
    1, 2

    View full-size slide

  36. © - BASE, Inc.
    JVM
    ldc #
    ( 0x )
    #3 Constant Pool 3

    View full-size slide

  37. Constant Pool

    View full-size slide

  38. © - BASE, Inc.
    Constant Pool
    Constant Pool 

    The Java Virtual Machine maintains a per-type constant pool, a run-
    time data structure that serves many of the purposes of the symbol
    table of a conventional programming language implementation.
    Java Virtual Machine Specification :
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
    Constant Pool Hello World 

    JVM Constant Pool

    View full-size slide

  39. © - BASE, Inc.
    Constant Pool
    Constant Pool 

    Java Virtual Machine Constant Pool ( )
    Java Virtual Machine Specification :
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
    Constant Pool Hello World 

    JVM Constant Pool

    View full-size slide

  40. © - BASE, Inc.
    Constant Pool
    Constant Pool

    View full-size slide

  41. © - BASE, Inc.
    Constant Pool
    Constant Pool
    Constant Pool

    View full-size slide

  42. © - BASE, Inc.
    Hello World
    JVM
    PHP JVM

    View full-size slide

  43. © - BASE, Inc.
    PHP
    JDK

    View full-size slide

  44. © - BASE, Inc.
    java
    javap
    Constant Pool 

    javac
    Java
    php

    View full-size slide

  45. © - BASE, Inc.
    Java

    View full-size slide

  46. © - BASE, Inc.
    Hello World
    Hello World ClassFile Structure 

    Constant Pool
    Methodref

    Constant Pool
    Code Attribute
    Code Attribute Constant Pool
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .

    View full-size slide

  47. © - BASE, Inc.
    ClassFile Structure
    Code Attribute
    1
    Constant Pool

    View full-size slide

  48. © - BASE, Inc.
    ClassFile Structure
    Code Attribute
    1
    Constant Pool

    View full-size slide

  49. ClassFile Structure

    View full-size slide

  50. © - BASE, Inc.
    ClassFile Structure
    ClassFile Structure 

    Java
    Constant Pool
    etc
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .

    View full-size slide

  51. © - BASE, Inc.
    ClassFile Structure
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
    JVM Specification

    View full-size slide

  52. © - BASE, Inc.
    ClassFile Structure
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
    JVM Specification

    View full-size slide

  53. © - BASE, Inc.
    ClassFile Structure
    javac 

    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
    javac -encoding UTF- HelloWorld.java

    View full-size slide

  54. © - BASE, Inc.
    ClassFile Structure
    PHP 

    unpack
    fopen
    fread
    unpack
    https://gist.github.com/memory-agape/
    d dce af ece e afb e caa e

    View full-size slide

  55. ClassFile Structure
    Constant Pool

    View full-size slide

  56. © - BASE, Inc.
    ClassFile Structure
    Code Attribute
    1
    Constant Pool

    View full-size slide

  57. © - BASE, Inc.
    Constant Pool
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
    Constant Pool
    u unsigned short

    View full-size slide

  58. © - BASE, Inc.
    Constant Pool
    Constant Pool

    View full-size slide

  59. © - BASE, Inc.
    Constant Pool
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
    Constant Pool 16bit 

    Constant Pool 

    JVM Specification

    View full-size slide

  60. © - BASE, Inc.
    Constant Pool
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
    tag Constant Pool
    tag
    prefix CONSTANT_
    छྨ ஋ छྨ ஋
    Class 0x07 Fieldref 0x09
    Methodref 0x0A InterfaceMethodref 0x0B
    String 0x08 Integer 0x03
    Float 0x04 Long 0x05
    Double 0x06 NameAndType 0x0C
    Utf8 0x01 MethodHandle 0x0F
    MethodType 0x10 InvokeDynamic 0x12

    View full-size slide

  61. © - BASE, Inc.
    Constant Pool
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
    Hello World Class, Methodref, 

    String, Utf , NameAndType 5
    छྨ ஋ छྨ ஋
    Class 0x07 Fieldref 0x09
    Methodref 0x0A InterfaceMethodref 0x0B
    String 0x08 Integer 0x03
    Float 0x04 Long 0x05
    Double 0x06 NameAndType 0x0C
    Utf8 0x01 MethodHandle 0x0F
    MethodType 0x10 InvokeDynamic 0x12

    View full-size slide

  62. © - BASE, Inc.
    Constant Pool
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .

    View full-size slide

  63. ClassFile Structure

    View full-size slide

  64. © - BASE, Inc.
    ClassFile Structure
    Code Attribute
    1
    Constant Pool

    View full-size slide

  65. © - BASE, Inc.
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
    ClassFile Structure

    View full-size slide

  66. © - BASE, Inc.

    CodeAttribute

    View full-size slide

  67. © - BASE, Inc.

    CodeAttribute

    View full-size slide

  68. © - BASE, Inc.
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .

    View full-size slide

  69. © - BASE, Inc.
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
    CodeAttribute
    Constant Pool 

    ( CONSTANT_Utf )

    View full-size slide

  70. © - BASE, Inc.
    ClassFile Structure
    Code Attribute
    1
    Constant Pool

    View full-size slide

  71. Code Attribute

    View full-size slide

  72. © - BASE, Inc.
    Code Attribute
    Code Attribute 


    View full-size slide

  73. © - BASE, Inc.
    Code Attribute
    Code Attribute 


    View full-size slide

  74. © - BASE, Inc.
    Code Attribute

    View full-size slide

  75. © - BASE, Inc.
    Code Attribute

    View full-size slide

  76. © - BASE, Inc.
    ClassFile Structure
    Code Attribute
    1
    Constant Pool

    View full-size slide

  77. © - BASE, Inc.
    javap

    View full-size slide

  78. © - BASE, Inc.
    javap

    main
    main
    #

    View full-size slide

  79. © - BASE, Inc.
    (java.lang.Object)
    main
    System.out.println Hello World!

    View full-size slide

  80. © - BASE, Inc.
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
    χʔϞχοΫ Φϖίʔυ ໾ׂ
    getstatic 0xB2 Get static field from class
    ldc 0x12 Push item from run-time constant pool
    invokevirtual 0xB6 Invoke instance method; dispatch based on class
    return 0xB1 Return void from method

    View full-size slide

  81. © - BASE, Inc.
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .

    χʔϞχοΫ Φϖίʔυ ໾ׂ
    getstatic 0xB2 ੩తϑΟʔϧυΛऔಘͯ͠ɺΦϖϥϯυελοΫʹੵΈ·͢ɻ
    ldc 0x12 Constant Pool ͔Β஋Λऔಘͯ͠ɺΦϖϥϯυελοΫʹੵΈ·͢ɻ
    invokevirtual 0xB6 ΦϖϥϯυελοΫͷ஋ΑΓɺΫϥε্ʹ͓͚ΔϝιουΛ࣮ߦ͠·͢
    return 0xB1 void Λฦ͠·͢ɻPHP ͸੍໿͕͋Γɺ return; ͱͯ͠΋ null Λฦ͢࢓༷Ͱ͢

    View full-size slide

  82. © - BASE, Inc.
    (Operand Stack)
    JVM
    JVM
    PHPer array_push 

    array_pop

    View full-size slide

  83. © - BASE, Inc.
    public static main (String[])
    main 

    ( )
    ldc
    invokevirtual
    return
    getstatic

    View full-size slide

  84. © - BASE, Inc.
    public static main (String[])
    main 

    ( )
    getstatic
    ldc
    invokevirtual
    return
    System.out 


    View full-size slide

  85. © - BASE, Inc.
    public static main (String[])
    main 

    ( )
    ldc
    invokevirtual
    return
    getstatic
    java.lang.String (Hello World)

    View full-size slide

  86. © - BASE, Inc.
    public static main (String[])
    main 

    ( )
    getstatic
    ldc
    invokevirtual
    return
    System.out 

    Hello World

    View full-size slide

  87. © - BASE, Inc.
    public static main (String[])
    main 

    ( )
    getstatic
    ldc
    invokevirtual
    return
    void 

    PHP void

    View full-size slide

  88. Code Attribute

    View full-size slide

  89. © - BASE, Inc.
    Code Attribute
    main 16
    B B B

    View full-size slide

  90. © - BASE, Inc.
    Code Attribute
    main 16
    B B B
    getstatic
    getstatic (Constant Pool 2 )

    View full-size slide

  91. © - BASE, Inc.
    Code Attribute
    main 16
    B B B
    ldc
    ldc (Constant Pool 3 )

    View full-size slide

  92. © - BASE, Inc.
    Code Attribute
    main 16
    B B B
    invokevirtual
    invokevirtual (Constant Pool 4 )

    View full-size slide

  93. © - BASE, Inc.
    Code Attribute
    main 16
    B B B
    return

    View full-size slide

  94. © - BASE, Inc.
    PHP

    View full-size slide

  95. © - BASE, Inc.
    getstatic
    getstatic
    2 (operand[ ] << ) | operand[ ]
    unsigned short
    Constant Pool
    Constant Pool Operand Stack 

    PHP unsigned short unpack 

    PHP 

    PHP unsigned short v n 

    n
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic

    View full-size slide

  96. © - BASE, Inc.
    getstatic
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic

    View full-size slide

  97. © - BASE, Inc.
    getstatic
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    2 unsigned short

    View full-size slide

  98. © - BASE, Inc.
    getstatic
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    java.lang.System Constant Pool

    View full-size slide

  99. © - BASE, Inc.
    getstatic
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    out Constant Pool

    View full-size slide

  100. © - BASE, Inc.
    getstatic
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    out Constant Pool
    PrintStream out

    View full-size slide

  101. © - BASE, Inc.
    getstatic
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    java/lang/System PHP 

    java\lang\System

    View full-size slide

  102. © - BASE, Inc.
    getstatic
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic

    View full-size slide

  103. © - BASE, Inc.
    ldc
    ldc
    1
    Constant Pool
    int, float, ,
    ldc ldc_w ldc _w
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic

    View full-size slide

  104. © - BASE, Inc.
    ldc
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic

    View full-size slide

  105. © - BASE, Inc.
    ldc
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    1 

    Constant Pool

    View full-size slide

  106. © - BASE, Inc.
    ldc
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    Constant Pool 

    Constant Pool 

    Constant Pool Hello World

    View full-size slide

  107. © - BASE, Inc.
    ldc
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    Hello World

    View full-size slide

  108. invokevirtual

    View full-size slide

  109. © - BASE, Inc.
    invokevirtual
    invokevirtual invokevirtual 

    invoke php-java 

    invokevirtual
    2 Constant Pool
    descriptor_index 範 

    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .invokevirtual

    View full-size slide

  110. © - BASE, Inc.
    invokevirtual
    invokevirtual Hello World 


    View full-size slide

  111. © - BASE, Inc.
    invokevirtual
    invokevirtual Hello World 

    2

    View full-size slide

  112. © - BASE, Inc.
    invokevirtual
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    invokevirtual Hello World 


    NameAndType Structure

    View full-size slide

  113. © - BASE, Inc.
    invokevirtual
    invokevirtual Hello World 


    ;

    View full-size slide

  114. © - BASE, Inc.
    invokevirtual
    invokevirtual Hello World 


    1 範

    View full-size slide

  115. © - BASE, Inc.
    invokevirtual
    invokevirtual Hello World 


    View full-size slide

  116. © - BASE, Inc.
    invokevirtual
    invokevirtual Hello World 

    java/lang/System.out.println

    View full-size slide

  117. java/lang/System.out.println

    View full-size slide

  118. java\lang\System.out

    View full-size slide

  119. © - BASE, Inc.
    java\lang\System.out
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    $out PrintStream 

    PHP 


    View full-size slide

  120. java\io\PrintStream.println

    View full-size slide

  121. © - BASE, Inc.
    java\io\PrintStream.println
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    java\lang\System.out PrintStream 

    PrintStream

    View full-size slide

  122. © - BASE, Inc.
    php
    DEMONSTRATION

    View full-size slide

  123. © - BASE, Inc.
    return
    return void PHP void
    return ireturn, freturn, areturn dreturn
    monitorexit 

    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .return

    View full-size slide

  124. © - BASE, Inc.
    return
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    return

    View full-size slide

  125. © - BASE, Inc.
    return
    : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
    return
    while break 

    PHP break continue 


    View full-size slide

  126. THANK YOU FOR YOUR LISTENING

    View full-size slide