Ruby-1.9.1を使うと、 以下のようにして簡単にRubyVMのInstaractionSequenceを見る事が出来ます。

   1  >> puts RubyVM::InstructionSequence.new("puts 'Hello, world!'").disasm #=> nil
   2  == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
   3  0000 trace            1                       (   1)
   4  0002 putnil           
   5  0003 putstring        "Hello, world!"
   6  0005 send             :puts, 1, nil, 8, <ic>
   7  0011 leave            

生成したInstractionSequence(aka iseq)は、evalメソッドを呼び出して実行する事もできます。

   1  >> RubyVM::InstructionSequence.new("puts 'Hello, world!'").eval #=> nil
   2  Hello, world!

さらに、to_aメソッドで個々のインストラクションに分解する事も出来ます。

   1  >> RubyVM::InstructionSequence.new("puts 'Hello, world!'").to_a
   2  ["YARVInstructionSequence/SimpleDataFormat", 1, 1, 1, {:arg_size=>0, :local_size=>1, :stack_max=>2}, "<compiled>", "<compiled>", :top, [], 0, [], [1, [:trace, 1], [:putnil], [:putstring, "Hello, world!"], [:send, :puts, 1, nil, 8, nil], [:leave]]]

posted by Png genki on Fri 20 Feb 2009 at 01:49

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry1314
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ