Archives For author

I believe that a Klein-like VM has the potential to be more object-oriented and more interactive than a Squeak-like VM. But one of the costs of the Klein approach is that we can’t reuse existing compilers the way Squeak does.

One of the cool things about the Squeak VM is that by writing it in a subset of Smalltalk, they were able to use the standard Smalltalk environment to write and debug the VM, while still taking advantage of all the work that’s gone into implementing C compilers. Writing in restricted Smalltalk is still nicer than writing in C, and debugging a simulated interpreter using the Smalltalk debugger is nicer than using a C debugger. (And while GCC isn’t written in Smalltalk, the Squeak guys don’t have to maintain it, so who cares?)

In contrast, one of Klein’s goals is to write the entire VM in a normal, high-level, object-oriented style. This includes the compiler and the assembler and the garbage collector and the interpreter (if we someday decide that we want an interpreter) and the object-format subsystem and everything else. So far, I believe we’ve only had to “cheat” (dip down into a lower-level style) in two small places: we had to write the message-sending routine without doing any message sends, and the object-cloning routine without doing any cloning (which meant we couldn’t use blocks). But I believe that even those compromises are just temporary – now that we’ve got a compiler that can inline away message-sends and block-clones, we should be able to rewrite even those two routines in a normal OO Self style.

So we’ve had to write our own compiler ourselves, and we’ll have to maintain it and port it ourselves. But the payoff is that the whole VM can be written in full OO Self, and be a live part of the running Klein image.

Which brings me to the other big difference between Klein and Squeak: in Squeak, the running VM isn’t part of the image. If you use the Squeak environment to make a change to the source code of the Squeak interpreter, the currently-running Squeak system doesn’t immediately change – you have to re-run the Slang-to-C translator and rebuild the VM and restart the image. (You can use Squeak to run a simulated Squeak interpreter, and make changes to the source code of the interpreter and see the changes immediately take effect in the simulation. But you don’t get that kind of interactiveness in the real base-level Squeak.) And if you want to debug the Squeak VM, you have to either use C tools to debug the real VM, or you have to recreate the bug in the simulator and use an existing Squeak to debug the simulated Squeak.

(Someone please correct me if I’m wrong. I haven’t spent a lot of time playing with the Squeak VM. This is just how it seems to me, given my understanding of Squeak’s architecture.)

In contrast, the Klein VM code is all just a regular part of the running Klein image. The project isn’t far enough along yet for us to be able to hack on Klein while it’s running, but once we get there, we should be able to make changes to the running VM and see those changes take effect immediately. (I expect this to be both very useful and very dangerous – a tradeoff Smalltalkers are used to. :)) So when we debug the Klein VM, we’re looking at the real, running, compiled code, using the standard Self environment.

A third difference is that in Klein we intend to – and for performance reasons we pretty much have to – implement Self’s dynamic optimization tricks, whereas I don’t think the current Squeak VM does much of that kind of thing. But I don’t think that’s a fundamental limitation of the Squeak VM architecture. (I’m excited about Cog!)

I never actually wanted to be a VM guy.

I fell in love with Smalltalk because it was simple and powerful and it fit the way I thought. (Later I fell in love with Self because it was even more so. ;)) The languages that I like best are the ones that are designed for human beings (rather than for computers or drones or mathematicians).

But every so often I have a language idea I’d like to try out – and to do that, it helps to be able to tinker with VMs. Unfortunately, VMs these days tend to be written in languages like C – and thanks to all the cool optimization technologies needed to make a language like Self run fast, they tend to be fairly hefty chunks of C, too. The Self VM is over 100,000 lines of C++, and I’m kinda scared of it.

So I dream of having a VM that’s simple and malleable and debuggable and written in a high-level language, but also includes enough Self-style optimization technology that it can make a really pure OO language run well. That’s one of the reasons why I was so excited about Klein – one of our goals was to produce a VM like that, written almost entirely in full object-oriented Self, and a VM-development environment that’s as comfortable and interactive as a regular Self or Smalltalk application-development environment. (So far, it seems to me like the “fully OO” part has been working out pretty well, but the interactiveness part has proven to be a major challenge – the project is still plagued by long compile times. I think it’s solvable, though.)

There hasn’t been any serious work done on Klein since Sun cancelled the project back in late 2005, but I pulled out the code a few months ago and started spending my spare time working on it. There’s nothing in there really worth seeing yet – I’m still working out the bugs in the inlining compiler that I just wrote, so everything’s kinda broken right now. (I’m hoping to have the inlining compiler working dynamically soon, though, at which point it’ll be interesting to see how much of a difference it makes, since practically everything in Klein, right down to the innermost loops of the GC, is written in an OO style with lots of message sends.) But one of the reasons I’m writing this post is because I’m wondering whether there’s anybody out there who feels like doing some hacking on Klein – there’s lots of fun stuff to be done, and I’d welcome the help.

I’m also wondering whether there’s another VM out there that’s further along towards fulfilling Klein’s goals. (I haven’t been keeping up with the VM world over the past few years.) Maybe Exupery?

And my third wondering is whether there’d be interest in Klein outside the Self community. If Klein ever does become a usable Self VM, it ought to be usable for other OO languages too, especially Smalltalk. If there was interest in the Smalltalk community in having a Klein-style VM, I’d make it a higher priority to teach Klein to handle non-LIFO blocks, which was one of the main obstacles preventing Mario Wolczko’s Smalltalk-to-Self translator from being a production-ready Smalltalk. (Sometimes I think about doing that for the current Self VM, just because I envy the size of the Smalltalk community – it’d be nice to be able to use Smalltalk libraries inside Self. Plus it might be kinda fun to work in a system with Smalltalk objects and Self objects living side-by-side – though the purist in me thinks it’d be a shame to complexify Self with all those unnecessary “class” things. ;) But that’s a topic for another blog post.)

Anyway, that’s what’s going on with Klein these days. I’m not sure whether it’s worth pursuing or not. Working on the GC and the inlining compiler has been kinda fun (turns out that being a VM guy isn’t so bad after all), but I’m more likely to keep going with it if I know that people are actually interested in it.

One of the first questions I ever asked David Ungar, when I first started to work for him on the Klein project, was, “So, what can we do to make Self more popular?”

He answered, “Why would we want to do that?”

It had never occurred to me before that it might not be a good idea to make myself crazy evangelizing my favourite programming language.

Nevertheless, I am hereby declaring my intention to actually spend some of my time writing about Self and Klein. (Maybe committing to it publicly will help me stick to it.) I’m doing this for two reasons:

  • I’ve spent the past two years working with much less interesting technologies, and if I don’t get back into the Smalltalk/Self world my soul is going to implode.
  • I need to find some articulate defenders of the class-based way of life, because there are some conversations I need to have with them.

I’ll post again soon, but for now I’m wondering: who’s reading this? Mostly Smalltalkers, or do we have visitors from far-away cultures too?


Get every new post delivered to your Inbox.