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. © - BASE, Inc. 2014 2015 1 30 nbproject Netbeans

    C:\\ 
 Windows 1 
 (Hello World ) 
 ( )
  2. © - BASE, Inc. Wikipedia JVM 
 PHP ( )

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

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

    ARchive) class kotlinc -include-runtime class 
 JAR Scala sbt-assembly JAR
  5. PHP

  6. PHP

  7. JVM

  8. © - 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
  9. © - 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
  10. © - 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- .
  11. © - BASE, Inc. ClassFile Structure ClassFile Structure 
 Java

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

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

    fread unpack https://gist.github.com/memory-agape/ d dce af ece e afb e caa e
  14. © - 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
  15. © - 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
  16. 1

  17. © - 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
  18. © - 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 Λฦ͢࢓༷Ͱ͢
  19. © - BASE, Inc. public static main (String[]) main 


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


    ( ) getstatic ldc invokevirtual return System.out 

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


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


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


    ( ) getstatic ldc invokevirtual return void 
 PHP void
  24. © - BASE, Inc. Code Attribute main 16 B B

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

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

    B invokevirtual invokevirtual (Constant Pool 4 )
  27. PHP

  28. © - 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
  29. ldc

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

    .getstatic Constant Pool 
 Constant Pool 
 Constant Pool Hello World
  32. © - 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
  33. © - BASE, Inc. invokevirtual : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .

    .getstatic invokevirtual Hello World 
 
 NameAndType Structure
  34. php

  35. © - 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