The best kittens, technology, and video games blog in the world.

Monday, April 16, 2007

Compiler for RLisp

some cats love sinks by Kevin Steele from flickr (CC-NC)RLisp was a wonderful idea, unfortunately its first implementation was a very slow pure-interpreter, and it was too difficult to retrofit good performance into it. The first version of RLisp even evaluated macros each time they were encountered, making them in practice "FEXPR"s instead of macros. I was able to improve RLisp performance a bit. The biggest gain was making sure macros expand just once. Other optimizations resulted in only minor improvement, and the code was quickly turning into a unreadable mess. The solution was a major rewrite. In the new implementation RLisp code is compiled to something that looks a lot like Lua 5.0's virtual machine code, and then to pure Ruby. Some of Lua virtual machine's ideas like flat closures are simply awesome and greatly simplify the implementation. The code is much cleaner, and much faster. In "x times slower than Ruby" benchmarks:

Early interpreterx96.1x37.8x1027.0
Optimized interpreterx35.5x58.2x53.1

Compiled code

RLisp compiler changed a lot since the teaser. Now to create functions it uses Object#function_* and instead of Function objects. Function objects were more elegant, but they could only be used for free-standing functions, not for methods - inside such function self was a closure. The new form makes it possible to use RLisp (fn ...) as methods, instance_eval them and so on. Unfortunately as{|*args,&blk| ... } is a syntax error in Ruby 1.8, it's impossible to create RLisp functions which take Ruby-compatible block argument. Fortunately you can still call such functions from RLisp code, and RLisp functions can take Proc objects as normal arguments. A few examples.
(fn (x)
(fn (y) (+ x y))
compiles to:
class ::Object; def function_29(globals, x) do |*args|
      y, = *args
      t0 = x.send(:+, y)
end; end
class ::Object; def function_28(globals) do |*args|
      x, = *args
      t1 = function_29(globals, x)
end; end
t2 = function_28(globals)
t2 takes |*args| instead of |x| to work around magical behaviour of Ruby blocks called with one Array argument, which is autoexpanded:{|first_arg, *other_args| first_arg}.call("foo") # => "foo"{|first_arg, *other_args| first_arg}.call("foo", "bar") # => "foo"{|first_arg, *other_args| first_arg}.call([1, 2, 3]) # => 1{|first_arg, *other_args| first_arg}.call([1, 2, 3], "bar") # => [1, 2, 3]
Fibonacci function:
(let fib (fn (x)
(if (< x 2)
  (+ (fib (- x 1)) (fib (- x 2)))
compiles to:
class ::Object; def function_28(globals) do |*args|
      x, = *args
      t0 = globals[:<]
      t1 =, 2)
      if t1
          t2 = 1
          t3 = globals[:fib]
          t4 = x.send(:-, 1)
          t5 =
          t6 = globals[:fib]
          t7 = x.send(:-, 2)
          t8 =
          t9 = t5.send(:+, t8)
          t2 = t9
end; end
t10 = function_28(globals)
globals[:fib] = t10
If RLisp can determine that a variable is constant or isn't used within nested function, it doesn't create Variable objects for it, and performs various optimizations based on such information. Of course Variable objects are created when needed, like in the following code which creates a counter function. A counter function called with an argument increases the internal counter and returns the old value.
(let make-counter (fn (val)
(fn (incr)
   (let old val)
   (set! val (+ val incr))
class ::Object; def function_29(globals, val) do |*args|
      incr, = *args
      t0 = val.get
      old = t0
      t1 = val.get
      t2 = t1.send(:+, incr)
      val.set t2
      t3 = old
end; end
class ::Object; def function_28(globals) do |*args|
      a_val, = *args
      val =
      val.set a_val
      t4 = function_29(globals, val)
end; end
t5 = function_28(globals)
globals[:"make-counter"] = t5

Access to Ruby

RLisp can access Ruby with (ruby-eval "Ruby code") function. All unknown uppercase variables are also assumed to be Ruby constants (nested constants like WEBrick::HTTPServer not supported yet). This makes using most Ruby libraries from RLisp pretty straightforward.
rlisp> (ruby-eval "require 'complex'")
rlisp> (let a [Complex new 1.0 2.0])
rlisp> (let b [Complex new -2.5 3.0])
rlisp> (let c [a * b])
Like before, RLisp supports syntactic sugar [receiver method args] for method calls, which expands to (send receiver 'method args). Now it's also possible to specify block argument with &.
rlisp> ['(1 2 3) map & (fn (x) (* x 2))]
(2 4 6)
RLisp can be run in two modes. To execute a standalone program use rlisp.rb program.rl. To run interactive REPL use rlisp.rb -i. It uses readline for input and prints out each expression as it is evaluated. Like every decent REPL, it captures exceptions without exiting.
rlisp> (hello "world")
./rlisp.rb:102:in `initialize': No such global variable: hello
rlisp> (defun hello (obj) (print "Hello, " obj "!"))
rlisp> (hello "world")
Hello, world!
It also supports multiline expressions (without a special prompt yet):
rlisp> (defun hello ()
rlisp>   (print "Hello, world!")
rlisp> )
rlisp> (hello)
Hello, world!
There's also a wrapper script which makes it possible to run RLisp programs with #!, but as it needs to know path to RLisp it's not enabled by default.

Object-orientation and macros

It's possible to create object-oriented code with a bunch of standard macros like.
rlisp> (class String (method welcome () (+ "Hello, " self " !")))
rlisp> ["world" welcome]
Hello, world !
(class ...), (method ...) and so on are just macros, and they expand to:
rlisp> (macroexpand '(class String do_stuff))
(send String (quote instance_eval) & (fn () do_stuff))
rlisp> (macroexpand '(method foo (arg) do_stuff))
(send self (quote define_method) (quote foo) & (fn (arg) do_stuff))
You can explore macroexpansions with (macroexpand 'code) and (macroexpand-1 'code), which are just normal functions. You can look at final generated code with -d command line switch:
$ ./rlisp.rb -d -i
rlisp> (+ 1 2 3)
t0 = 2.send(:+, 3)
t1 = 1.send(:+, t0)
rlisp> ((fn (x) (+ x 2)) 5)
class ::Object; def function_30(globals) do |*args|
      x, = *args
      t2 = x.send(:+, 2)
end; end
t3 = function_30(globals)
t4 =
Other options are -n (don't use RLisp standard library), -r (force recompilation of standard library), and -h (display help message).

The future

You can download RLisp from RLisp website. It's licenced under BSD/MIT-like licence, has reasonable performance (CPU-wise 5x slower than Ruby - perfectly reasonable for database-bound or network-bound programs), and a lot of libraries (that is - it can use most Ruby libraries out of the box). The code is rather clean, and reasonably tested (test code to code ratio 0.96:1). No major program was written in RLisp so far, so a few misdesigns and major bugs are certainly present. What I'd like to do is create a new backend for RLisp. For now Lua-like VM opcodes are compiled to Ruby, but they're so simple that they could be compiled to C, x86 assembly, or LLVM and still stay fully compatible with Ruby runtime. That would probably make RLisp somewhat faster than Ruby, as Ruby's slowest part is eval. Ruby backend could probably be made faster too without too much work. It would be awesome to do add at least limited tail-recursion optimization to RLisp. Have fun.

1 comment:

Brad Ediger said...

Good stuff. I like it.

I hacked together a Rails plugin to compile RLisp files on the fly within a Rails application: