[Mono-list] Making a ruby.net compiler

Miguel de Icaza miguel@ximian.com
10 May 2003 14:05:22 -0400


Hello!

    This morning (which is absolutely beautiful here in Boston) I woke
up with an idea to implement some of Fergus' comments:

> Sure, but no CIL implementations thus far are capable of making more
> significant changes, such as changing data representations to replace a
> class hierarchy with the use of tag bits for representing discriminated
> unions.  Nor does it look like we'll see this in the near future.

    And I figured: there is really nothing that prevents the runtime to
pick a few bits as tags on pointers to implement "isinst class".

    We could have a call that informs the runtime that isinst will be
used massively on a set of classes.  So for example, say your lisp
implementation will be using a lot of classes of type "Atom" and
"List", and that you want the following test to be as fast as possible:

	Type t = typeof (o);
	if (t == typeof (List)){
		List l = (List) o;
	} else if (t == typeof (Atom)){
		Atom a = (Atom) o; 
	} else {
		Its some other kind.
	}

    You would inform the runtime about this:

	System.Runtime.FastCast (typeof (List));
	System.Runtime.FastCast (typeof (Atom));

    The runtime could reserve a few bits in the pointer.  The lower two
bits are ideal, the combination "00" means `normal object', and the
other three combinations 01, 10 and 11 map to special classes.

    So then the assembly language code for "o is List" instead of
involving a call to an expensive routine, becomes:

	mov $eax, o
	test $eax, 1
	jne 1f
	mov $eax, type_of_list_constant
	jmp 3f
1:
	test $eax, 2
	jne 2f
	mov $eax, type_of_atom_constant
	jmp 3f
2:
	call runtime_type_get_type_of_object
3:

   The GC probably has to be modified to cope with this.

Miguel.