Skip to content

Instantly share code, notes, and snippets.

@rohitarondekar
Created February 20, 2012 05:01
Show Gist options
  • Save rohitarondekar/1867939 to your computer and use it in GitHub Desktop.
Save rohitarondekar/1867939 to your computer and use it in GitHub Desktop.

simple.rb

code = <<END
class Adder
  def add_two(x)
    x+2
  end
end

puts Adder.new.add_two(3)
END

bytecode = RubyVM::InstructionSequence.compile(code)
puts bytecode.disasm
९ ruby simple.rb 
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
0000 trace            1                                               (   1)
0002 putspecialobject 3
0004 putnil           
0005 defineclass      :Adder, <class:Adder>, 3
0009 pop              
0010 trace            1                                               (   7)
0012 putnil           
0013 getinlinecache   20, <ic:0>
0016 getconstant      :Adder
0018 setinlinecache   <ic:0>
0020 send             :new, 0, nil, 0, <ic:1>
0026 putobject        3
0028 send             :add_two, 1, nil, 0, <ic:2>
0034 send             :puts, 1, nil, 8, <ic:3>
0040 leave            
== disasm: <RubyVM::InstructionSequence:<class:Adder>@<compiled>>=======
0000 trace            2                                               (   1)
0002 trace            1                                               (   2)
0004 putspecialobject 1
0006 putspecialobject 2
0008 putobject        :add_two
0010 putiseq          add_two
0012 send             :"core#define_method", 3, nil, 0, <ic:0>
0018 trace            4                                               (   5)
0020 leave                                                            (   2)
== disasm: <RubyVM::InstructionSequence:add_two@<compiled>>=============
local table (size: 2, argc: 1 [opts: 0, rest: -1, post: 0, block: -1] s1)
[ 2] x<Arg>     
0000 trace            8                                               (   2)
0002 trace            1                                               (   3)
0004 getlocal         x
0006 putobject        2
0008 opt_plus         <ic:1>
0010 trace            16                                              (   4)
0012 leave
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment