at builderscon tokyo 2019
English Title: How to implement JVM by the PHP, and until output HelloWorld.
/ / builderscon TokyoPHP JVM Hello World© - BASE, Inc.
View Slide
© - BASE, Inc.Who am I?(@m m r )BASEPHP, React
© - BASE, Inc.PHPJVM
© - BASE, Inc.Hello WorldJVMPHP JVM
© - BASE, Inc.PHPJVM SpecificationJava
© - BASE, Inc.PHP JVMPHPerKaigihttps://speakerdeck.com/memory /phperkaigi-
© - BASE, Inc.PHP JVM OSS ( )https://github.com/php-java/php-java
1. JDKJDK
2.
JVM PHP
© - BASE, Inc.PHP
© - BASE, Inc.
© - BASE, Inc.20142015 1 30nbproject Netbeans C:\\ Windows1 (Hello World ) ( )
© - BASE, Inc.Wikipedia JVM PHP( ) https://en.wikipedia.org/wiki/List_of_Java_virtual_machinesJPHP Java PHP( ) https://github.com/jphp-group/jphp
© - BASE, Inc.PHPPHPPHPLaravel PHP JVM
JVM
© - BASE, Inc.JVMJVM class (= )
© - BASE, Inc.JVMclassjavac javaJAR (Java ARchive) classkotlinc -include-runtime class JARScala sbt-assembly JAR
© - BASE, Inc.JVMJVMC CPUOracle PHPRust ( )invoke_*
© - BASE, Inc.JVM3PHPPHP class
© - BASE, Inc.JVMPHPPHP class
© - BASE, Inc.JVMPHP classPHPerKaigi https://speakerdeck.com/memory /phperkaigi-
PHP
© - BASE, Inc.PHPPHP Objectjava.lang.Object?String VoidInteger, Long OK
© - BASE, Inc.PHPSwoole parallel
© - BASE, Inc.PHPPHP ExceptionJava java.lang.Object
OpCode
© - BASE, Inc.1JVM 算
© - BASE, Inc.CPUmov rax, rbx( 0x )1, 2
© - BASE, Inc.JVMldc #( 0x )#3 Constant Pool 3
Constant Pool
© - BASE, Inc.Constant PoolConstant Pool The Java Virtual Machine maintains a per-type constant pool, a run-time data structure that serves many of the purposes of the symboltable 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
© - BASE, Inc.Constant PoolConstant 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
© - BASE, Inc.Constant PoolConstant Pool
© - BASE, Inc.Constant PoolConstant PoolConstant Pool
Hello World
© - BASE, Inc.PHPJDK
© - BASE, Inc.javajavapConstant Pool javacJavaphp
Java
© - BASE, Inc.Java
© - BASE, Inc.Hello World Hello World ClassFile Structure Constant PoolMethodref Constant PoolCode AttributeCode Attribute Constant Pool: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
© - BASE, Inc.ClassFile StructureCode Attribute1Constant Pool
ClassFile Structure
© - BASE, Inc.ClassFile StructureClassFile Structure JavaConstant Pooletc: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
© - BASE, Inc.ClassFile Structure: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .JVM Specification
© - BASE, Inc.ClassFile Structurejavac : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .javac -encoding UTF- HelloWorld.java
© - BASE, Inc.ClassFile StructurePHP unpackfopenfreadunpackhttps://gist.github.com/memory-agape/d dce af ece e afb e caa e
ClassFile StructureConstant Pool
© - BASE, Inc.Constant Pool: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .Constant Poolu unsigned short
© - BASE, Inc.Constant Pool: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .Constant Pool 16bit Constant Pool JVM Specification
© - BASE, Inc.Constant Pool: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .tag Constant Pooltagprefix CONSTANT_छྨ छྨ Class 0x07 Fieldref 0x09Methodref 0x0A InterfaceMethodref 0x0BString 0x08 Integer 0x03Float 0x04 Long 0x05Double 0x06 NameAndType 0x0CUtf8 0x01 MethodHandle 0x0FMethodType 0x10 InvokeDynamic 0x12
© - 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 0x09Methodref 0x0A InterfaceMethodref 0x0BString 0x08 Integer 0x03Float 0x04 Long 0x05Double 0x06 NameAndType 0x0CUtf8 0x01 MethodHandle 0x0FMethodType 0x10 InvokeDynamic 0x12
© - BASE, Inc.Constant Pool: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
© - BASE, Inc.: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .ClassFile Structure
© - BASE, Inc. CodeAttribute
© - BASE, Inc.: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .
© - BASE, Inc.: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .CodeAttributeConstant Pool ( CONSTANT_Utf )
Code Attribute
© - BASE, Inc.Code AttributeCode Attribute
© - BASE, Inc.Code Attribute
1
javap
© - BASE, Inc.javap
© - BASE, Inc.javap mainmain#
© - BASE, Inc.(java.lang.Object)mainSystem.out.println Hello World!
© - BASE, Inc.: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- .χʔϞχοΫ Φϖίʔυ ׂgetstatic 0xB2 Get static field from classldc 0x12 Push item from run-time constant poolinvokevirtual 0xB6 Invoke instance method; dispatch based on classreturn 0xB1 Return void from method
© - 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 Λฦ༷͢Ͱ͢
© - BASE, Inc.(Operand Stack)JVMJVMPHPer array_push array_pop
© - BASE, Inc.public static main (String[])main ( )ldcinvokevirtualreturngetstatic
© - BASE, Inc.public static main (String[])main ( )getstaticldcinvokevirtualreturnSystem.out
© - BASE, Inc.public static main (String[])main ( )ldcinvokevirtualreturngetstaticjava.lang.String (Hello World)
© - BASE, Inc.public static main (String[])main ( )getstaticldcinvokevirtualreturnSystem.out Hello World
© - BASE, Inc.public static main (String[])main ( )getstaticldcinvokevirtualreturnvoid PHP void
© - BASE, Inc.Code Attributemain 16B B B
© - BASE, Inc.Code Attributemain 16B B Bgetstaticgetstatic (Constant Pool 2 )
© - BASE, Inc.Code Attributemain 16B B Bldcldc (Constant Pool 3 )
© - BASE, Inc.Code Attributemain 16B B Binvokevirtualinvokevirtual (Constant Pool 4 )
© - BASE, Inc.Code Attributemain 16B B Breturn
getstatic
© - BASE, Inc.getstaticgetstatic2 (operand[ ] << ) | operand[ ]unsigned shortConstant PoolConstant 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
© - BASE, Inc.getstatic: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
© - BASE, Inc.getstatic: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic2 unsigned short
© - BASE, Inc.getstatic: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstaticjava.lang.System Constant Pool
© - BASE, Inc.getstatic: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstaticout Constant Pool
© - BASE, Inc.getstatic: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstaticout Constant PoolPrintStream out
© - BASE, Inc.getstatic: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstaticjava/lang/System PHP java\lang\System
ldc
© - BASE, Inc.ldcldc1Constant Poolint, float, ,ldc ldc_w ldc _w: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
© - BASE, Inc.ldc: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic
© - BASE, Inc.ldc: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic1 Constant Pool
© - BASE, Inc.ldc: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstaticConstant Pool Constant Pool Constant Pool Hello World
© - BASE, Inc.ldc: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstaticHello World
invokevirtual
© - BASE, Inc.invokevirtualinvokevirtual invokevirtual invoke php-java invokevirtual2 Constant Pooldescriptor_index 範 : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .invokevirtual
© - BASE, Inc.invokevirtualinvokevirtual Hello World
© - BASE, Inc.invokevirtualinvokevirtual Hello World 2
© - BASE, Inc.invokevirtual: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstaticinvokevirtual Hello World NameAndType Structure
© - BASE, Inc.invokevirtualinvokevirtual Hello World ;
© - BASE, Inc.invokevirtualinvokevirtual Hello World 1 範
© - BASE, Inc.invokevirtualinvokevirtual Hello World java/lang/System.out.println
java/lang/System.out.println
java\lang\System.out
© - BASE, Inc.java\lang\System.out: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstatic$out PrintStream PHP
java\io\PrintStream.println
© - BASE, Inc.java\io\PrintStream.println: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstaticjava\lang\System.out PrintStream PrintStream
php
© - BASE, Inc.phpDEMONSTRATION
return
© - BASE, Inc.returnreturn void PHP voidreturn ireturn, freturn, areturn dreturnmonitorexit : https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .return
© - BASE, Inc.return: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstaticreturn
© - BASE, Inc.return: https://docs.oracle.com/javase/specs/jvms/se /html/jvms- .html#jvms- . .getstaticreturnwhile break PHP break continue
THANK YOU FOR YOUR LISTENING