[MacRuby-devel] How Roxor compiles Ruby via LLVM Re: Looking for info on MacRuby's JIT

Ernest N. Prabhakar, Ph.D. prabhaka at apple.com
Tue Dec 8 10:46:37 PST 2009


Great summary -- can someone validate and post on the MacRuby site, for future reference?

Also, might be helpful to include links to the appropriates files so it is easier to cross-check.

-- Ernie P.

On Dec 8, 2009, at 5:49 AM, ravenex wrote:
Just to make sure I got it right:

MacRuby's VM is called Roxor. Currently in the code in trunk doesn't use
any interpreter mode; Ruby code is always compiled before is can be run.
Compilation can be done in either JIT or AOT mode.
(I thought there was an experimental branch that actually used LLVM's
interpreter to speed up eval, what happened to that?)

JIT mode is the default mode. A translation unit is a Ruby source file.
As a Ruby source file gets loaded, it's first parsed by a modified parser
taken from MRI 1.9, and turned into NODE*, pretty much the same as the
original Ruby AST. Then, RoxorCompiler will compile AST into LLVM IR,
which is kept and JITted later on demand.


Because in MacRuby all Ruby classes inherit from NSObject, MacRuby uses
mechanism from the Objective-C runtime to drive the lazy compilation
behavior: original implementations of NSObject's resolveClassMethod: and
resolveInstanceMethod: are replaced with MacRuby's own ones, which act as
a hook and call the JIT when method implementations are being resolved.


For normal Ruby methods, resolveClassMethod_imp() and
resolveInstanceMethod_imp() both call RoxorCore::resolve_method(), which
calls RoxorCore::resolve_methods(), which calls RoxorCore::resolve_method(),
and that'll call RoxorCore::compile() which compiles LLVM IR into native
code.

In AOT mode, Ruby code gets parsed in Ruby AST, compiled into LLVM IR by
RoxorAOTCompiler, and then the LLVM bitcode is dumped to a temporary file.
The bitcode is then translated into assembly by llc, then assembled by
invoking gcc, and then optionally linked with other stuff.

I'll continue reading the source for now...



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20091208/086a0278/attachment-0001.html>


More information about the MacRuby-devel mailing list