Skip to content

ruby-syntax-tree/yarv

Repository files navigation

YARV

Build Status

This project's aim is to provide an educational window into how CRuby's virtual machine works under the hood.

CRuby uses a stack-based virtual machine named YARV. You can see the bytecode that will be executed by running RubyVM::InstructionSequence.compile(source).disasm or by running ruby --dump=insns -e source. At last count, there were 202 instructions, though that number gets significantly smaller if you factor out tracepoint and specialized instructions. Most instructions look similar to their machine code counterparts.

There isn't really a canonical source for documentation for these instructions beyond the source code itself in insns.def or the original YARV design document. Because of this, this project aims to provide comprehensive documentation to make it easier to contribute to CRuby's development. The documentation is currently published at https://ruby-syntax-tree.github.io/yarv/. It is generated by running bin/doc.

As a part of the documentation effort, this project also aims to provide an emulator of YARV behavior. This will ensure the documentation doesn't get out of date as it will need to reflect actual Ruby semantics. To run the emulator, after cloning the repository, run exe/yarv <path_to_file> from the command line.

Getting started

To use this project as a CLI, you can execute the exe/yarv executable. That functions similarly to the ruby executable. You pass it the path to a Ruby file, and it will execute that file.

To use this project as a library, you start with the YARV.compile method. This method is similar to the RubyVM::InstructionSequence.compile method, in that it accepts a string that represents Ruby source code and compiles it into instruction sequences. Once the source is compiled, you can call eval on the result to get it to evaluate your code.

Related content

To learn more about this kind of content, there are a number of articles and blog posts you can read. Most of them are in Japanese, so a translation tool may be necessary. Some of them are also quite old since a lot of them were written around the time YARV was created. They are listed below:

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ruby-syntax-tree/yarv. In this project's current form there are lots of opportunities to contribute. To get started, please check the issues on the project board.

CRuby contains tests that help with generating specific instructions, these are a good starting point when writing tests for our reimplemented instructions: https://github.com/ruby/ruby/blob/master/bootstraptest/test_insns.rb.

License

The gem is available as open source under the terms of the MIT License.