YARV (The Official VM for Ruby 1.9) Instruction Set

April 18, 2008 – 11:33 pm

YARV is Yet Another Ruby VM. But it is the official Ruby VM since Ruby 1.9. It is a stack-based VM which runs the YARV bytecode, or intcode because each element of the instruction sequence is in fact stored as an int.

There is an instruction table for YARV on the original YARV homepage, but it is out-dated now. So I build a new one and make it publicly available in case some one would be interested in.

Koichi said that the instructions are not stable currently. Something might going to change later. Some instructions that are not used frequently might be modified or even removed. But I guess at least those two instructions will be kept: ;)

  • bitblt: returns "a bit of bacon, lettuce and tomato".
  • answer: the answer to life, the universe, and everything. It returns 42.

Yes, there they are, both in the joke category of the instruction set. :D

I’ll try to keep the table up-to-date when modification has made to the instruction set. In fact, writing a script to generate this is not very hard. You can find scripts (insns2vm.rb and instruction.rb) in the tool directory of Ruby 1.9 source tree to parse the instruction definition (insns.def).

A RubyVM::InstructionsLoader can be used to parse the definition file. Then an ERB template can be used to generate whatever you want from the instructions easily. A bunch of templates can be found in the template directory of Ruby 1.9 source tree.

I’m also trying to make those information available from Ruby:

irb(main):002:0> VM::InstructionSet.select { |insn|
irb(main):003:1*   insn.operands.length == 2
irb(main):004:1> }.each { |insn|
irb(main):005:1*   puts "%4d %s" % [insn.opcode, insn.name]
irb(main):006:1> }
   3 getspecial
   5 getdynamic
   6 setdynamic
  21 toregexp
  24 expandarray
  47 invokeblock
  54 getinlinecache
  55 onceinlinecache
  57 opt_case_dispatch
  65 opt_neq

If everything goes well, I’ll create a gem to manipulate the YARV instruction sequence. Just like the InstructionSequence::Encoder in Rubinius.

Post a Comment