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!)
Your understanding of the Squeak-VM is correct, but some of the work I am doing on SiliconSqueak might be of interest. While the VM is actually structured as a class Interpreter which implements the bytecode engine and most of the primitives, and which inherits from ObjectMemory (unlike the “blue book” VM), I will explain it as if it were three roughly equal parts: B-BC (the bytecode interpreter in Slang (bytecodes), B-OM (the object memory in Slang) and B-PR (primitives in Slang). These get translated into C and compiled into machine code, becoming C-BC, C-OM and C-PR which run the tinyBenchmarks about 1000 times faster than the B versions.
step 1) short circuit B-BC so that whenever it would have been invoked we have C-BC actually interpret the bytecodes.
step 2) have special hardware speed up C-BC so that bytecodes run at roughly the same speed as machine language.
step 3) now C-OM isn’t really faster than B-OM nor C-PR than B-PR, so we add a detour so the Slang versions are used instead of the native code ones. The InterpreterSimulator is no longer dealing with a second and separate image, but in a Klein bottle style twist it is now controlling the image in which it is running.
Many of the complications you mentioned apply to this scheme as well: how do you send messages without more sending messages, or allocate objects without allocating more objects. And while the system I described is an interpreter, adding Cog native compilation would make it even nicer.
About the danger, one possible solution is to go back to having two images. A system image operates on itself (with all the advantages and risks) and it also operates on a user image running the actual application.
Jecel, did you mean B-BC in ‘step 2)’? I have to assume so given the mention of bytecodes. If not then I think this could use some further explanation.
Ken, I eliminated B-BC (the bytecode interpreter written in bytecodes) in step 1, so the hardware in step 2 is supposed to help out C-BC (the bytecode interpreter written in machine language).
These odd abbreviations don’t help much – a drawing or animation would be best.
OK, then I guess I would really like to hear some details regarding that, what can help the bytecode interpreter written in machine language. But perhaps this is not the place.
Yeah, email is better for a detailed discussion. If you look at the Jazelle technology that ARM uses to speed up Java you can get a good idea of what I am talking about.
http://en.wikipedia.org/wiki/Jazelle
Hi Adam,
Great to see you at the Toronto Smalltalk Users Group last night. This blog and the Self homepage are going to be very useful. I’m going to try to figure out how some of this stuff works over this summer.
Chris
Feel free to ask questions here, or on the Self mailing list.
I’ve never been quite sure which of Self’s ideas are already well-understood by the rest of the Smalltalk community. Maybe it’d be fun to have a series of posts here on the ways in which Self differs from Smalltalk.
W8kzJe Stands back from the keyboard in aazmmenet! Thanks!
[…] Klein and Squeak VM architectures | Self https://blog.selflanguage.org/2009/07/04/klein-and-squeak-vm/ […]