John Lam 1997 — 2026

Refactoring CIL

I had a great day today. I had one of those a-ha experiences where you get to benefit from an earlier decision in ways that you did not expect. I started down the path of generating constructor shims using RbDynamicMethod. I completed my first spike yesterday, when I successfully constructed ArrayList objects using RbDynamicMethod.

Today, I wanted to make a slightly more generic constructor shim that would let me call both a default and a single argument constructor: ArrayList() and ArrayList(Int32). While I was writing this code, I began to feel some CIL pain (it is, after all assembly language). So I did what came naturally: I began to refactor the code.

This reminds me of the talk that I saw this year at OOPSLA where Ward Cunningham was refactoring assembly language using IntelliJ. In my case, I refactored CIL using emacs :) Here’s the end result:

def initialize(*params)
  create_ruby_instance_method(self.class, 'initialize') do
    include     'System.Collections'
    declare     "#{symbol}", :obj
    ldarg_0
    switch      [:zero_param, :one_param]
    br_s        :end_switch
    label       :zero_param
    newobj      '#{symbol}()'
    stloc_s     :obj
    br_s        :end_switch
    label       :one_param
    ld_rb_param 0, 'Int32'
    newobj      '#{symbol}(Int32)'
    stloc_s     :obj
    label       :end_switch
    ret_objref  :obj
  end
  initialize(*params)
end

While this is mostly CIL, it also contains a number of macros and fun Ruby string stuff. The macros that I used are: ld_rb_param, ret_objref. As it turns out, these will be very common code fragments that will be used throughout the bridge. Writing those macros was even easier. Here’s a sample:

module RbDynamicMethodHelpers
  def ld_self
    ldarg_2
  end

  def ld_rb_param(index, type)
    ldarg_1
    case index
    when 1:
      ldc_i4_4
      add
    when 2:
      ldc_i4_8
      add
    else
      ldc_i4    index << 2
      add
    end
    ldind_i4
    call      "static Marshal::To#{type}(VALUE)"
  end

  def ret_objref(local_variable_name)
    ld_self
    ldloc_s  local_variable_name
    call     'static Marshal::AssignToClassInstance(VALUE, Object)'
    ret
  end
end

It turned out it was a good thing that I left the method declarations as strings, since I could do some simple string manipulation to inject implement syntactic sugar methods such as ld_rb_param (which is actually a very efficient CIL implementation that is optimized for the common cases).

Oh yeah, how much extra code did I have to add to RbDynamicMethod to enable macros? I changed:

object.extend RbDynamicMethod

to:

object.extend RbDynamicMethod, RbDynamicMethodHelpers

Did I mention how much I love Ruby? :)

3 archived reader comments

SM (2005-12-16)

Hi John: Love the blog and you adventures in Ruby/CLR land. I’m from the .NET world and considering getting into Ruby. ROR looks very enticing. What environment/editor do you use for Ruby? Know of any similar to VS for Ruby/ROR?

John Lam (2005-12-16)

There are a number of IDEs available for Ruby. There’s “ArachnoRuby”:http://www.ruby-ide.com/ruby/rubyideandrubyeditor.php and “Komodo”:http://www.activestate.com/Products/Komodo/.

Personally, I use emacs on Windows and TextMate on the Mac.

John Lam (2005/12/15)

There are a number of IDEs available for Ruby. There’s "ArachnoRuby":http://www.ruby-ide.com/ruby/ruby_ide_and_ruby_editor.php and "Komodo":http://www.activestate.com/Products/Komodo/. Personally, I use emacs on Windows and TextMate on the Mac.