Search code examples
compilationsmalltalkpharo

Compiling Pharo to C?


It is said that Pharo's VM (CogVM) is developed, tested, profiled and etc in Smalltalk, but then the Smalltalk code is transcompiled to C, which is then compiled along side with some OS abstraction C code using the default system C compiler.

Well, I'd like to do a similar thing, I wan't to develop, test and profile code using Pharo, but then compile it to C. How can I do it? How the compilation to C works? Does Pharo comes with a Smalltalk to C transcompiler? How can I invoke it? Does it compile the full Smalltalk, or I have to use some kind of a Smalltalk subset? Is there any good documentation about it?


Solution

  • The Pharo VM is hosted on github. Follow the steps to build it and you'll get a Smalltalk image called "generator.image" which you can run (it's a regular image). Inside of that image you'll find the VMMaker package which is responsible for generating the C code from the special Smalltalk dialect used for this (which is called Slang; it's a subset of Smalltalk). Look at the code in the generator image to get a feel for what it does. There's also some information contained in the workspaces that are open when you first open the image.

    As soon as you have the C sources it's basically straight forward C compilation (which we do with Cmake + gcc / clang).

    As for documentation: you should probably read the Blue Book.

    clarifiation

    As @Leandro Caniglia points out in the comment, the purpose of Slang is to generate C source code for the VM. It has been designed to ease translation to C. That does not mean that:

    • arbitrary Smalltalk code can be translated to C using the generating mechanism
    • arbitrary Smalltalk code can be rewritten in Slang (at least not "easily")