The Martian Chronicles

Generating the PharoVM with Pharo3

Cogs

As probably all of you know, the PharoVM (a flavour of the CogVM) is made in a Smalltalk subset called SLANG. In fact, SLANG can be seen as a C with a Smalltalk syntax, but is a bit more complicated than that: Using SLANG, we are able to simulate the VM inside Pharo and use all the advantages our platform gives us (like no need to compilation/restart, refactoring tools, introspection, etc.). Once we are ready for compilation, SLANG is translated into C files, and processed though a set of CMake files (also generated in Pharo).

Because of this, is really important for us to keep aligned the VM source generation to the stable release of Pharo: then we can use our improved tools to improve VM in an iterative loop.

The generation

Besides the usual API changes, deprecations and differences (not many, usually a 5' work), this time we face one important problem: The old Compiler (needed to parse the sources and translate them to C) has been replaced in Pharo 3 for the new OpalCompiler (who provides a modern, good designed compiler).

But... the good part is that old Compiler is still in the image, even if not used (and it will be a loadable package in Pharo4). One possible solution is to port the source generation to OpalCompiler, but that has some other important implications. In concrete, the CogVM is developed Squeak, for Newspeak and it should work also for other Smalltalk flavours (like Cuis), so if we port the compiler we will need to keep it updated and aligned to other package, and that will be an increasing amount or work.

The easieast (and better) solution is to use the deprecated compiler to generate the sources. Fortunatelly this should be as easy as execute something like this:

[
	oldCompilerClass := SmalltalkImage compilerClass.
	SmalltalkImage compilerClass: Compiler.
	PharoVMBuilder build ]
ensure: [ 
	SmalltalkImage compilerClass: oldCompilerClass ].

... but we have to find the appropriate place to do the conversion. Easiest place is to modify PharoVMBuilder and siblings to use correct compiler, but that will be prone to error in the future so the right place is CPlatformConfig>>#generateSources.

This can introduce some problems with other platforms using the CMake infraestructure, but I hope they can be solved easily.

Small problem

Well... this was working almost out of the box. But I suddenly reach another problem: Pharo3 also changes class protocol organisation. It replaces old string based protocols for a reified Protocol hierarchy.

While this is a really good change, it generates a bug: ClassOrganization>>#categories method (meant to be backward compatible with old string categories) answers also '- - all - -' protocol, with all methods. And C generator uses class protocols to filter some methods... then I was having an infinite loop somewhere and an image crash after memory heap surpased.

Solution is simple: to add '- - all - -' to the filtered protocols. But I wanted to point this problem because I spent like three days debugging and understanding until I realised the source of the bug.

Conclusion

Port into Pharo3 something as complicated as the VMMaker was possible, and problems aside, not so hard :)

Posted by Esteban at 28 May 2014, 6:22 pm link