Fwd: [Pharo-users] Understanding Slang for building a compiler to C++

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

Fwd: [Pharo-users] Understanding Slang for building a compiler to C++

kilon.alios
 


---------- Forwarded message ---------
From: Dimitris Chloupis <[hidden email]>
Date: Sat, Mar 5, 2016 at 1:05 AM
Subject: Re: [Pharo-users] Understanding Slang for building a compiler to C++
To: Any question about pharo is welcome <[hidden email]>


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.
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: [Pharo-users] Understanding Slang for building a compiler to C++

Eliot Miranda-2
 
Hi Dimitris,

    could you say more about why?  What are you looking for from the C++, performance, interoperability, static binaries, ..?

_,,,^..^,,,_ (phone)

On Mar 4, 2016, at 3:06 PM, Dimitris Chloupis <[hidden email]> wrote:



---------- Forwarded message ---------
From: Dimitris Chloupis <[hidden email]>
Date: Sat, Mar 5, 2016 at 1:05 AM
Subject: Re: [Pharo-users] Understanding Slang for building a compiler to C++
To: Any question about pharo is welcome <[hidden email]>


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.
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: [Pharo-users] Understanding Slang for building a compiler to C++

kilon.alios
Sorry for the late reply, I have been down with the flu lately.

My goal is mostly curiosity , I am itching to try messing with programming languages but I also like keeping close with smalltalk syntax.

The why is that I am aiming for readable C++ code that is easy to mix with existing C++ code. In my particular case is using and extending the Unreal Engine.

I cannot do it via shared libraries because Unreal Engine is not made to be used as a library , it actually messes with a lot of things as you can imagine it has it own event loop, threading, GC and even adds reflection to C++. Also I need something that compiles statically for the iOS platform where shared libraries are not allowed, and I am not sure if Android has similar restrictions.

So even though its great to use pharo unlimited as a scripting language, I think there is also usefuleness in limiting pharo usage (static types, memory managment etc) when you want to generate code in another programming language in my case C++.

My crazy dream would be that pharo would be able to be used in any project inside a team, without any other team mates having to learn pharo. The advantage would be you will be able to use pharo not just for prototyping but even generate the source in another language and know would be able to guess you wrote the code in Pharo. Of course this idea is not really new or unique.

However that dream is crazy , my more realistic dream is to build an enviroment of a collection of tools that allow to annotate pharo code without changing it, for example you can annotate what type a variable is or what variable is actually a pointer what kind of pointer (raw or smart) which method would use C++ templates etc. Then a compiler will take the pharo code plus the annotations and convert it to C++ code that is readable as much as possible. That means of course there will be some sever limitations of how one codes in Pharo in order to translate with ease to C++.


I have been pointed here because you guys are doing something similar with Slang though I am not sure whether Slang produces readable C code since it looks like it does not impose any sever limitations to Smalltalk usage but then I can be wrong.

In any case I am open to advice and suggestions.
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: [Pharo-users] Understanding Slang for building a compiler to C++

philippeback
 
Kilon,

Check http://www.smalltalkhub.com/#!/~PavelKrivanek/CCodeGenerator as this is a standalone part for Slang support.

I did some changes in the past but I think Pavel integrated them back.


Basically this thing allows you to write Slang, emit C code.

There is sample class to demonstrate:

* C code generation from Slang
* Using OSProcess to invoke C compiler to make a shared lib
* Loading it using NativeBoost

Here is the doc comment:

Generate the C code into generated.c

| cg |
cg := CCodeGenerator new initialize.
cg vmClass: CCGExample.
cg addClass: CCGExample.
CCGExample exportHeaders.
cg storeCodeOnFile: 'generated.c' doInlining: false.  

Compile it

gcc -c -m32 generated.c
gcc -shared -m32 -o generated.dll generated.o

| c | 
c := PipeableOSProcess command: 'gcc -c -m32 generated.c'.
(c output; succeeded)
ifFalse: [ self error: 'Compilation error: ', c errorPipelineContents printString].
c := PipeableOSProcess command: 'gcc -shared -m32 -o generated.dll generated.o'.
(c output; succeeded)
ifFalse: [ self error: 'Compilation error', c errorPipelineContents printString].
Try it

NativeBoost clearNativeCode.

CCGExample new nbFib4: 30.
CCGExample new nbFactorial: 5.


Slang code looking like this:

factorial: anArgument

"<var: #anArgument type: #'double'>"

| count factorial |
self export: true.
count := anArgument.
factorial := 1.
[ count > 0 ] whileTrue: 
[ factorial := factorial * count.
count := count - 1 ].
^ factorial

Here is the PDF going into how to extend the VM with plugins and there is some Slang in there too.
Sure Spur may have changed things and this may require adjustments.

HTH
Phil


On Tue, Mar 8, 2016 at 11:15 AM, kilon.alios <[hidden email]> wrote:

Sorry for the late reply, I have been down with the flu lately.

My goal is mostly curiosity , I am itching to try messing with programming
languages but I also like keeping close with smalltalk syntax.

The why is that I am aiming for readable C++ code that is easy to mix with
existing C++ code. In my particular case is using and extending the Unreal
Engine.

I cannot do it via shared libraries because Unreal Engine is not made to be
used as a library , it actually messes with a lot of things as you can
imagine it has it own event loop, threading, GC and even adds reflection to
C++. Also I need something that compiles statically for the iOS platform
where shared libraries are not allowed, and I am not sure if Android has
similar restrictions.

So even though its great to use pharo unlimited as a scripting language, I
think there is also usefuleness in limiting pharo usage (static types,
memory managment etc) when you want to generate code in another programming
language in my case C++.

My crazy dream would be that pharo would be able to be used in any project
inside a team, without any other team mates having to learn pharo. The
advantage would be you will be able to use pharo not just for prototyping
but even generate the source in another language and know would be able to
guess you wrote the code in Pharo. Of course this idea is not really new or
unique.

However that dream is crazy , my more realistic dream is to build an
enviroment of a collection of tools that allow to annotate pharo code
without changing it, for example you can annotate what type a variable is or
what variable is actually a pointer what kind of pointer (raw or smart)
which method would use C++ templates etc. Then a compiler will take the
pharo code plus the annotations and convert it to C++ code that is readable
as much as possible. That means of course there will be some sever
limitations of how one codes in Pharo in order to translate with ease to
C++.


I have been pointed here because you guys are doing something similar with
Slang though I am not sure whether Slang produces readable C code since it
looks like it does not impose any sever limitations to Smalltalk usage but
then I can be wrong.

In any case I am open to advice and suggestions.




--
View this message in context: http://forum.world.st/Fwd-Pharo-users-Understanding-Slang-for-building-a-compiler-to-C-tp4882613p4883161.html
Sent from the Squeak VM mailing list archive at Nabble.com.



greenberg.pdf (115K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: [Pharo-users] Understanding Slang for building a compiler to C++

kilon.alios
 
Thanks Phil I was already informed about CCodeGenerator , it definitely helps but your PDF helps even more , I see now why Esteban recommended Slang. It looks like there is a lot I can use from both for my compiler. I am studying them and will be back with questions. Is this the right place to ask questions about Slang and CCodeGenerator ? I really like the idea of Slang of two modes one interpreter mode for pure smalltalk code and one for generating the c code. This was the direction I was going too. Prototype in Pharo and worry about generating the C++ code only when your code reaches production state.
On Tue, 8 Mar 2016 at 15:25, [hidden email] <[hidden email]> wrote:
 
Kilon,

Check http://www.smalltalkhub.com/#!/~PavelKrivanek/CCodeGenerator as this is a standalone part for Slang support.

I did some changes in the past but I think Pavel integrated them back.


Basically this thing allows you to write Slang, emit C code.

There is sample class to demonstrate:

* C code generation from Slang
* Using OSProcess to invoke C compiler to make a shared lib
* Loading it using NativeBoost

Here is the doc comment:

Generate the C code into generated.c

| cg |
cg := CCodeGenerator new initialize.
cg vmClass: CCGExample.
cg addClass: CCGExample.
CCGExample exportHeaders.
cg storeCodeOnFile: 'generated.c' doInlining: false.  

Compile it

gcc -c -m32 generated.c
gcc -shared -m32 -o generated.dll generated.o

| c | 
c := PipeableOSProcess command: 'gcc -c -m32 generated.c'.
(c output; succeeded)
ifFalse: [ self error: 'Compilation error: ', c errorPipelineContents printString].
c := PipeableOSProcess command: 'gcc -shared -m32 -o generated.dll generated.o'.
(c output; succeeded)
ifFalse: [ self error: 'Compilation error', c errorPipelineContents printString].
Try it

NativeBoost clearNativeCode.

CCGExample new nbFib4: 30.
CCGExample new nbFactorial: 5.


Slang code looking like this:

factorial: anArgument

"<var: #anArgument type: #'double'>"

| count factorial |
self export: true.
count := anArgument.
factorial := 1.
[ count > 0 ] whileTrue: 
[ factorial := factorial * count.
count := count - 1 ].
^ factorial

Here is the PDF going into how to extend the VM with plugins and there is some Slang in there too.
Sure Spur may have changed things and this may require adjustments.

HTH
Phil


On Tue, Mar 8, 2016 at 11:15 AM, kilon.alios <[hidden email]> wrote:

Sorry for the late reply, I have been down with the flu lately.

My goal is mostly curiosity , I am itching to try messing with programming
languages but I also like keeping close with smalltalk syntax.

The why is that I am aiming for readable C++ code that is easy to mix with
existing C++ code. In my particular case is using and extending the Unreal
Engine.

I cannot do it via shared libraries because Unreal Engine is not made to be
used as a library , it actually messes with a lot of things as you can
imagine it has it own event loop, threading, GC and even adds reflection to
C++. Also I need something that compiles statically for the iOS platform
where shared libraries are not allowed, and I am not sure if Android has
similar restrictions.

So even though its great to use pharo unlimited as a scripting language, I
think there is also usefuleness in limiting pharo usage (static types,
memory managment etc) when you want to generate code in another programming
language in my case C++.

My crazy dream would be that pharo would be able to be used in any project
inside a team, without any other team mates having to learn pharo. The
advantage would be you will be able to use pharo not just for prototyping
but even generate the source in another language and know would be able to
guess you wrote the code in Pharo. Of course this idea is not really new or
unique.

However that dream is crazy , my more realistic dream is to build an
enviroment of a collection of tools that allow to annotate pharo code
without changing it, for example you can annotate what type a variable is or
what variable is actually a pointer what kind of pointer (raw or smart)
which method would use C++ templates etc. Then a compiler will take the
pharo code plus the annotations and convert it to C++ code that is readable
as much as possible. That means of course there will be some sever
limitations of how one codes in Pharo in order to translate with ease to
C++.


I have been pointed here because you guys are doing something similar with
Slang though I am not sure whether Slang produces readable C code since it
looks like it does not impose any sever limitations to Smalltalk usage but
then I can be wrong.

In any case I am open to advice and suggestions.




--
View this message in context: http://forum.world.st/Fwd-Pharo-users-Understanding-Slang-for-building-a-compiler-to-C-tp4882613p4883161.html
Sent from the Squeak VM mailing list archive at Nabble.com.