Hack のバイトコード命令を確認する
Hack のプログラムがどのようなバイトコードにコンパイルされるのか、以下の手順で確認できます。
サンプルとして hello.hh を作成します。
$ cat hello.hh <?hh echo "Hello, world!\n";
hhvm に --hphp オプションを指定すると、HipHop コンパイラとして動作します。他のオプションや引数を何も与えなければ usage が表示されます。
$ hhvm --hphp HipHop Compiler for PHP Usage: hphp <options> <inputs> Options: --help display this message --version display version number -t [ --target ] arg (=run) lint | analyze | php | hhbc | filecache | run (default) ...
Usage に表示されているように、-t オプションの指定によってコンパイラのターゲットを変更できます。試したところでは、バイトコードコンパイラとして動かすには -t hhbc を指定すればよさそうです。デフォルトではバイナリファイルとして出力されるのですが、-f text オプションを指定すると、バイトコード命令列をテキストファイルで出力してくれます。また、-o output オプションを指定すると、コンパイルによって生成されるファイル群が output/ ディレクトリ以下に出力されます。-o オプションを指定しないと /tmp の下に出力されるようです。これらをまとめると、以下のように実行すればよいことになります。
$ hhvm --hphp -t hhbc -f text -o output hello.hh running hphp... parsing inputs... analyzeProgram... analyzeProgram took 0'00" (61859 us) wall time parsing inputs took 0'00" (64336 us) wall time pre-optimizing... pre-optimizing took 0'00" (160237 us) wall time analyze includes... analyze includes took 0'00" (162 us) wall time inferring types... inferring types took 0'00" (77364 us) wall time post-optimizing... post-optimizing took 0'00" (235406 us) wall time creating text HHBC files... creating text HHBC files took 0'00" (336390 us) wall time saving code errors... all files saved in output ... running hphp took 0'01" (1534312 us) wall time
バイトコード命令列は、output/php/hello.hh.hhbc.txt というファイルで確認できます。
$ cat output/php/hello.hh.hhbc.txt Hash: d0a26e8a84df8ca12d0dbd66cf23122c Pseudo-main at 0 (ID 0) // line 2 0: String "Hello, world!\n" 5: Print 6: PopC 7: Int 1 16: RetC Pseudo-main at 0 (ID 0)
なお、バイトコード命令の仕様は、ドキュメントとしては以下に書かれています。
プログラムソースコード上は、以下のファイルに実装されているようです。ExecutionContext::iopString 等が各バイトコード命令の実装になっています。