y_uti のブログ

統計、機械学習、自然言語処理などに興味を持つエンジニアの技術ブログです

各言語でのバイトコード命令表示方法

普段あまり使うことのない言語も含めていくつか調べてみたので、備忘録として。

Perl

$ perl -MO=Concise hello.pl
6  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 3 hello.pl:6) v:*,&,{,$ ->3
5     <@> print vK ->6
3        <0> pushmark s ->4
4        <$> const[PV "Hello, world!\n"] s ->5
hello.pl syntax OK

B::Concise - search.cpan.org

PHP

PECL::vld を使います。この記事を書いている時点では beta 版なのでインストール時には vld-beta と指定する必要があります。

# pecl install vld-beta

実行方法

$ php -dvld.action=0 -dvld.execute=0 hello.php
Finding entry points
Branch analysis from position: 0
Return found
filename:       /home/uchiyama/work/test/hello/hello.php
function name:  (null)
number of ops:  3
compiled vars:  none
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   3     0  >   PRINT                                            ~0      'Hello%2C+world%21%0A'
         1      FREE                                                     ~0
   4     2    > RETURN                                                   1

branch: #  0; line:     3-    4; sop:     0; eop:     2
path #1: 0,

PECL :: Package :: vld

Python

$ python -m dis hello.py
  3           0 LOAD_CONST               0 ('Hello, world!')
              3 PRINT_ITEM
              4 PRINT_NEWLINE
              5 LOAD_CONST               1 (None)
              8 RETURN_VALUE

31.12. dis — Disassembler for Python bytecode — Python v2.7.6 documentation

C/C++ (gcc)

バイトコードではないですが。

$ gcc -S hello.c
$ cat hello.s
        .file   "hello.c"
        .section        .rodata
.LC0:
        .string "Hello, world!"
        .text
.globl main
        .type   main, @function
main:
        pushl   %ebp
        movl    %esp, %ebp
        andl    $-16, %esp
        subl    $16, %esp
        movl    $.LC0, (%esp)
        call    puts
        movl    $0, %eax
        leave
        ret
        .size   main, .-main
        .ident  "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)"
        .section        .note.GNU-stack,"",@progbits

Overall Options - Using the GNU Compiler Collection (GCC)

Java

$ javac Hello.java
$ javap -c Hello
Compiled from "Hello.java"
public class Hello {
  public Hello();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #3                  // String Hello, world!
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: iconst_0
       9: invokestatic  #5                  // Method java/lang/System.exit:(I)V
      12: return
}

javap - The Java Class File Disassembler