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

218473ff7abad0fcd72865da2b23bb5b?s=47 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.

218473ff7abad0fcd72865da2b23bb5b?s=128

memory

August 31, 2019
Tweet

Transcript

  1. / / builderscon Tokyo PHP JVM Hello World © -

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

    ) BASE PHP, React
  3. None
  4. © - BASE, Inc. PHP JVM

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

  6. None
  7. © - BASE, Inc. PHP JVM Specification Java

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

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

  11. None
  12. 1. JDK JDK

  13. 2. 


  14. JVM PHP

  15. © - BASE, Inc. PHP JVM

  16. None
  17. © - BASE, Inc. PHP

  18. © - BASE, Inc.

  19. © - BASE, Inc. 2014 2015 1 30 nbproject Netbeans

    C:\\ 
 Windows 1 
 (Hello World ) 
 ( )
  20. © - BASE, Inc.

  21. © - BASE, Inc.

  22. © - BASE, Inc.

  23. © - BASE, Inc.

  24. © - BASE, Inc.

  25. © - BASE, Inc.

  26. © - BASE, Inc.

  27. © - BASE, Inc. Wikipedia JVM 
 PHP ( )

    https://en.wikipedia.org/wiki/List_of_Java_virtual_machines JPHP Java PHP ( ) https://github.com/jphp-group/jphp
  28. © - BASE, Inc. PHP

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

    
 JVM
  30. © - BASE, Inc. PHP JVM

  31. JVM

  32. © - BASE, Inc. JVM JVM class 
 (= )

  33. © - BASE, Inc. JVM class javac java JAR (Java

    ARchive) class kotlinc -include-runtime class 
 JAR Scala sbt-assembly JAR
  34. © - BASE, Inc. JVM JVM C CPU Oracle 


    PHP Rust ( ) invoke_*
  35. © - BASE, Inc. JVM 3 PHP PHP class

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

  37. © - BASE, Inc. JVM PHP class PHPerKaigi 
 https://speakerdeck.com/memory

    /phperkaigi-
  38. © - BASE, Inc. PHP JVM

  39. PHP

  40. PHP

  41. © - BASE, Inc. PHP PHP Object java.lang.Object? String Void

    Integer, Long OK
  42. © - BASE, Inc. PHP Swoole parallel 
 


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

  44. None
  45. None
  46. © - BASE, Inc. Hello World JVM PHP JVM

  47. JVM

  48. OpCode

  49. OpCode

  50. None
  51. © - BASE, Inc. 1 JVM 算

  52. © - BASE, Inc. CPU mov rax, rbx ( 0x

    ) 1, 2
  53. © - BASE, Inc. JVM ldc # ( 0x )

    #3 Constant Pool 3
  54. Constant Pool

  55. © - 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
  56. © - 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
  57. © - BASE, Inc. Constant Pool Constant Pool

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

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

  60. Hello World

  61. None
  62. © - BASE, Inc. PHP JDK

  63. None
  64. © - BASE, Inc. java javap Constant Pool 
 javac

    Java php
  65. Java

  66. None
  67. © - BASE, Inc. Java

  68. Hello World

  69. © - 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- .
  70. None
  71. © - BASE, Inc. ClassFile Structure Code Attribute 1 Constant

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

    Pool
  73. ClassFile Structure

  74. © - BASE, Inc. ClassFile Structure ClassFile Structure 
 Java

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

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

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

    /html/jvms- .html#jvms- . javac -encoding UTF- HelloWorld.java
  78. © - BASE, Inc. ClassFile Structure PHP 
 unpack fopen

    fread unpack https://gist.github.com/memory-agape/ d dce af ece e afb e caa e
  79. ClassFile Structure Constant Pool

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

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

    . Constant Pool u unsigned short
  82. © - BASE, Inc. Constant Pool Constant Pool

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

    . Constant Pool 16bit 
 Constant Pool 
 JVM Specification
  84. © - 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
  85. © - 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
  86. © - BASE, Inc. Constant Pool : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms-

    .
  87. ClassFile Structure

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

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

    Structure
  90. © - BASE, Inc. 
 CodeAttribute

  91. © - BASE, Inc. 
 CodeAttribute

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

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

    Constant Pool 
 ( CONSTANT_Utf )
  94. © - BASE, Inc. ClassFile Structure Code Attribute 1 Constant

    Pool
  95. Code Attribute

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


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


  98. © - BASE, Inc. Code Attribute

  99. © - BASE, Inc. Code Attribute

  100. None
  101. None
  102. © - BASE, Inc. ClassFile Structure Code Attribute 1 Constant

    Pool
  103. 1

  104. javap

  105. © - BASE, Inc. javap <javap -v > 


  106. © - BASE, Inc. javap <javap -v > 
 main

    main #<Number>
  107. None
  108. © - BASE, Inc. (java.lang.Object) main System.out.println Hello World!

  109. None
  110. © - 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
  111. © - 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 Λฦ͢࢓༷Ͱ͢
  112. © - BASE, Inc. (Operand Stack) JVM JVM PHPer array_push

    
 array_pop
  113. None
  114. © - BASE, Inc. public static main (String[]) main 


    ( ) ldc invokevirtual return getstatic
  115. © - BASE, Inc. public static main (String[]) main 


    ( ) getstatic ldc invokevirtual return System.out 

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


    ( ) ldc invokevirtual return getstatic java.lang.String (Hello World)
  117. © - BASE, Inc. public static main (String[]) main 


    ( ) getstatic ldc invokevirtual return System.out 
 Hello World
  118. © - BASE, Inc. public static main (String[]) main 


    ( ) getstatic ldc invokevirtual return void 
 PHP void
  119. Code Attribute

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

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

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

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

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

    B return
  125. PHP

  126. None
  127. © - BASE, Inc. PHP

  128. getstatic

  129. © - 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
  130. © - BASE, Inc. getstatic : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .

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

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

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

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

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

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

    .getstatic
  137. ldc

  138. © - 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
  139. © - BASE, Inc. ldc : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .

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

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

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

    .getstatic Hello World
  143. invokevirtual

  144. © - 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
  145. © - BASE, Inc. invokevirtual invokevirtual Hello World 


  146. © - BASE, Inc. invokevirtual invokevirtual Hello World 
 2

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

    .getstatic invokevirtual Hello World 
 
 NameAndType Structure
  148. © - BASE, Inc. invokevirtual invokevirtual Hello World 
 


    ;
  149. © - BASE, Inc. invokevirtual invokevirtual Hello World 
 


    1 範
  150. © - BASE, Inc. invokevirtual invokevirtual Hello World 


  151. © - BASE, Inc. invokevirtual invokevirtual Hello World 
 java/lang/System.out.println

  152. java/lang/System.out.println

  153. None
  154. java\lang\System.out

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

    .getstatic $out PrintStream 
 PHP 

  156. java\io\PrintStream.println

  157. © - 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
  158. php

  159. © - BASE, Inc. php DEMONSTRATION

  160. Hello World

  161. return

  162. © - 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
  163. © - BASE, Inc. return : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .

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

    .getstatic return while break 
 PHP break continue 

  165. Hello World

  166. THANK YOU FOR YOUR LISTENING