Understanding Slang for building a compiler to C++

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Understanding Slang for building a compiler to C++

kilon.alios
Hey pharo experts I am interesting into studying how Slang works for making a compiler that takes pharo code and turns it to C++ code, my questions is where to find Slang , how to extract the code that compiles Slang to C and study it as an example for making my own compiler for C++.
Reply | Threaded
Open this post in threaded view
|

Re: Understanding Slang for building a compiler to C++

Clément Béra
Hello Dimitris,

In the VMMaker packages you will find Slang. See the generator.image that you can build here: https://github.com/pharo-project/pharo-vm to get the package in Pharo. Slang is used exclusively for VM compilation and simulation.

The package named 'VMMaker-Translation to C' does what you are looking for. It translates Smalltalk source code to Smalltalk AST, then Smalltalk AST to Slang AST, then perform some optimizations on Slang AST, then writes the C code. The slang AST is all the class with names starting with T in this package. The CCodeGenerator is the class responsible for C code generation from slang AST. A closed world assumption allows the generator to do inlining efficiently and compile to direct function call, hence C code generation of a single method can be done only by parsing all the slang method needed for C compilation. The closed world is defined according to different settings such as which memory manager or which jit back end is used. There are no polymorphism in the generated c code, though there are polymorphism in Slang, for example all the memory managers are polymorphic with each other, but you have to pick one of them for C compilation. Super sends acts as macro replacements. There are no such things as mapping Smalltalk classes to C++ classes, it's more about compiling a huge pack of functions all together.

The other packages of VM Maker are related to the interpreter, the JIT, the Memory manager and the simulator. Code used exclusively for simulation or testing purpose can be Smalltalk, but most code here is in Slang.

There are documentation about the VM in general but I don't think there is documentation specifically about slang itself. Slang is meant to compile the VM and only the VM, hence both projects are tied together. It does not feel like it's a good idea to extract the C code generator as some things were hacked in the generator to make VM compilation work, while the VM code was hacked to work with the generator. In fact, each time we want to do something that is not possible in the VM, we hack either the VM code or the slang generator depending on which solution implies the least time consumption over the next few years.

You have to know that the Slang to C generation is, in my opinion, the worst code of the whole VM packages.

This is the kind of script I use in Squeak to generate the C code for a single smalltalk method and check if everything is correct, I guess it works in Pharo, as you can see, the compilation process is dependent on VM compilation, as you have to precise which JIT and memory manager to use to define correctly the closed world that will be generated and hence what function you can call or inline :

(Transcript show: [| sel vmm s cg |
sel := #writeImageFileIO.
vmm := (VMMaker forPlatform: 'Cross')
interpreterClass: CoInterpreter"MT";
options: {#Cogit. Cogit chooseCogitClass name}, #(ObjectMemory Spur32BitCoMemoryManager "NewspeakVM true MULTIPLEBYTECODESETS true" SistaVM false).
cg := [vmm buildCodeGeneratorForInterpreter]
on: Notification
do: [:ex|
ex tag == #getVMMaker
ifTrue: [ex resume: vmm]
ifFalse: [ex pass]].
"cg breakSrcInlineSelector: #readHeapFromImageFile:dataBytes:;
breakDestInlineSelector: sel;
breakOnInline: false""true".
cg vmClass preGenerationHook: cg.
cg inferTypesForImplicitlyTypedVariablesAndMethods.
cg retainMethods: { sel }.
cg prepareMethods.
((sel beginsWith: 'bytecode') or: [sel endsWith: 'Bytecode'])
ifTrue: [cg doBasicInlining: true]
ifFalse: [cg doInlining: true].
s := ReadWriteStream on: String new.
(cg methodNamed: sel)
removeUnusedTempsAndNilIfRequiredIn: cg;
halt;
emitCCodeOn: s generator: cg.
s contents] value)

I'd recommend you to ask question about this topic on the VM mailing list (http://lists.squeakfoundation.org/mailman/listinfo/vm-dev) because few people in the Pharo community have a good understanding of Slang, and the ones whodo are also on vm-dev.

Have fun :-)


2016-03-04 13:06 GMT+01:00 Dimitris Chloupis <[hidden email]>:
Hey pharo experts I am interesting into studying how Slang works for making a compiler that takes pharo code and turns it to C++ code, my questions is where to find Slang , how to extract the code that compiles Slang to C and study it as an example for making my own compiler for C++.

Reply | Threaded
Open this post in threaded view
|

Re: Understanding Slang for building a compiler to C++

vonbecmann
In reply to this post by kilon.alios
Hi Dimitris,
  Pavel has extracted the code generator in this project


On Fri, Mar 4, 2016 at 9:06 AM, Dimitris Chloupis <[hidden email]> wrote:
Hey pharo experts I am interesting into studying how Slang works for making a compiler that takes pharo code and turns it to C++ code, my questions is where to find Slang , how to extract the code that compiles Slang to C and study it as an example for making my own compiler for C++.



--
Bernardo E.C.

Sent from a cheap desktop computer in South America.
Reply | Threaded
Open this post in threaded view
|

Re: Understanding Slang for building a compiler to C++

kilon.alios
Thanks guys for the pointers, Clement I dont care about the VM my goal is not something like Slang but rather something that I will take a small piece of code in pharo lets say a few classes and convert it to C++ code that is human readable. But Esteban recommended to take a look at Slang anyway for what I want to create. My priority here is readable C++ code, rather than keeping the code as close to Pharo as possible. I know that compromises will have to be made, so no GC, static types etc. So I am looking into creating something that can take pharo code then the user adds annotations or extra syntax like types, pointers, manual memory managment, templates etc and then my compiler takes that and turns it to readable C++.

I think this approach would be much easier for me to do instead of doing the hard work of trying to map Pharo to C++ which most likely is not possible since C++ even lacks simple reflection and of course will have to implement my own VM and my own GC .

Essentially my goal is writing C++ with Pharo syntax or close to Pharo syntax. So people like me that want to take Pharo code and port it to C++ can do it easier than a manual rewrite.

On Fri, Mar 4, 2016 at 10:36 PM Bernardo Ezequiel Contreras <[hidden email]> wrote:
Hi Dimitris,
  Pavel has extracted the code generator in this project


On Fri, Mar 4, 2016 at 9:06 AM, Dimitris Chloupis <[hidden email]> wrote:
Hey pharo experts I am interesting into studying how Slang works for making a compiler that takes pharo code and turns it to C++ code, my questions is where to find Slang , how to extract the code that compiles Slang to C and study it as an example for making my own compiler for C++.



--
Bernardo E.C.

Sent from a cheap desktop computer in South America.