www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compile to C?

reply Nestor <nestorperez2016 yopmail.com> writes:
Hi friends,

Is there a way to "compile" d code to C, similar to what nim does?

That would be cool for greater portability.
Jan 21 2017
next sibling parent Joakim <dlang joakim.fea.st> writes:
On Saturday, 21 January 2017 at 18:38:22 UTC, Nestor wrote:
 Hi friends,

 Is there a way to "compile" d code to C, similar to what nim 
 does?

 That would be cool for greater portability.
The wiki says there was a dmd fork that attempted this 5 years ago, don't know how far he got: https://wiki.dlang.org/Compilers https://github.com/yebblies/dmd/tree/microd
Jan 21 2017
prev sibling next sibling parent reply Jack Stouffer <jack jackstouffer.com> writes:
On Saturday, 21 January 2017 at 18:38:22 UTC, Nestor wrote:
 Hi friends,

 Is there a way to "compile" d code to C, similar to what nim 
 does?

 That would be cool for greater portability.
No, and this is actually a terrible idea. See https://forum.dlang.org/post/n1vbos$11ov$1 digitalmars.com
Jan 21 2017
parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Saturday, 21 January 2017 at 19:30:31 UTC, Jack Stouffer wrote:
 On Saturday, 21 January 2017 at 18:38:22 UTC, Nestor wrote:
 Hi friends,

 Is there a way to "compile" d code to C, similar to what nim 
 does?

 That would be cool for greater portability.
No, and this is actually a terrible idea. See https://forum.dlang.org/post/n1vbos$11ov$1 digitalmars.com
No valid arguments for it being a terrible idea in that thread. Being able to translate code to other languages is a very useful feature and makes a language much more interesting in production.
Jan 23 2017
parent reply aberba <karabutaworld gmail.com> writes:
On Monday, 23 January 2017 at 14:40:18 UTC, Ola Fosheim Grøstad 
wrote:
 On Saturday, 21 January 2017 at 19:30:31 UTC, Jack Stouffer 
 wrote:
 On Saturday, 21 January 2017 at 18:38:22 UTC, Nestor wrote:
 Hi friends,

 Is there a way to "compile" d code to C, similar to what nim 
 does?

 That would be cool for greater portability.
No, and this is actually a terrible idea. See https://forum.dlang.org/post/n1vbos$11ov$1 digitalmars.com
No valid arguments for it being a terrible idea in that thread. Being able to translate code to other languages is a very useful feature and makes a language much more interesting in production.
Unless you will be limited by tge limitations of C. Vala programming language has that issue even though they utilize GObject
Jan 23 2017
parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Monday, 23 January 2017 at 15:24:09 UTC, aberba wrote:
 Unless you will be limited by tge limitations of C. Vala 
 programming language has that issue even though they utilize 
 GObject
What limitations? C/C++ programs go around "limitations" by using compiler extensions and runtime libraries. Vala is trying to keep code simple, like Go. Apples and oranges.
Jan 23 2017
prev sibling next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 21 January 2017 at 18:38:22 UTC, Nestor wrote:
 That would be cool for greater portability.
The hard part in porting to a new platform is rarely the code generation - gdc and ldc have diverse backends already (indeed, they tend to work for D as well as C there). But you still have to port runtime library requirements where compiling to C wouldn't help at all.
Jan 21 2017
parent reply Nestor <nestorperez2016 yopmail.com> writes:
On Saturday, 21 January 2017 at 19:33:27 UTC, Adam D. Ruppe wrote:
 On Saturday, 21 January 2017 at 18:38:22 UTC, Nestor wrote:
 That would be cool for greater portability.
The hard part in porting to a new platform is rarely the code generation - gdc and ldc have diverse backends already (indeed, they tend to work for D as well as C there). But you still have to port runtime library requirements where compiling to C wouldn't help at all.
You mean phobos, or system libraries?
Jan 22 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 23 January 2017 at 01:12:21 UTC, Nestor wrote:
 You mean phobos, or system libraries?
Phobos but mostly the druntime that interfaces with the system.
Jan 22 2017
parent reply Nestor <nestorperez2016 yopmail.com> writes:
On Monday, 23 January 2017 at 01:17:20 UTC, Adam D. Ruppe wrote:
 On Monday, 23 January 2017 at 01:12:21 UTC, Nestor wrote:
 You mean phobos, or system libraries?
Phobos but mostly the druntime that interfaces with the system.
I see, I was mostly thinking in Android and/or other platforms, but it does seem like heavy work, though curiously this approach seems to be working for Nim (at least so far), is it that the language is better suited for that, or simply that more work has been put into it? (not bashing D, honest curiosity)
Jan 22 2017
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 23/01/2017 3:20 PM, Nestor wrote:
 On Monday, 23 January 2017 at 01:17:20 UTC, Adam D. Ruppe wrote:
 On Monday, 23 January 2017 at 01:12:21 UTC, Nestor wrote:
 You mean phobos, or system libraries?
Phobos but mostly the druntime that interfaces with the system.
I see, I was mostly thinking in Android and/or other platforms, but it does seem like heavy work, though curiously this approach seems to be working for Nim (at least so far), is it that the language is better suited for that, or simply that more work has been put into it? (not bashing D, honest curiosity)
Nim probably doesn't care too much about the platform its running on like C. We support things like TLS and shared libraries with GC, that all requires druntime and having system support to some extent.
Jan 22 2017
prev sibling parent Joakim <dlang joakim.fea.st> writes:
On Monday, 23 January 2017 at 02:20:02 UTC, Nestor wrote:
 On Monday, 23 January 2017 at 01:17:20 UTC, Adam D. Ruppe wrote:
 On Monday, 23 January 2017 at 01:12:21 UTC, Nestor wrote:
 You mean phobos, or system libraries?
Phobos but mostly the druntime that interfaces with the system.
I see, I was mostly thinking in Android and/or other platforms, but it does seem like heavy work, though curiously this approach seems to be working for Nim (at least so far), is it that the language is better suited for that, or simply that more work has been put into it? (not bashing D, honest curiosity)
Btw, Ldc has good support for a lot of platforms already, including Android and iPhone: http://wiki.dlang.org/LDC Porting druntime to a new OS is not heavy work, mostly translating some C headers: https://github.com/dlang/druntime/pulls?utf8=✓&q=is%3Apr%20is%3Aopen%20netbsd
Jan 22 2017
prev sibling parent reply Bauss <jj_1337 live.dk> writes:
On Saturday, 21 January 2017 at 18:38:22 UTC, Nestor wrote:
 Hi friends,

 Is there a way to "compile" d code to C, similar to what nim 
 does?

 That would be cool for greater portability.
Nim is able to, because Nim doesn't really compile. The Nim compiler just translates Nim code to C code and then compiles the C code. It's much harder to do properly in D, because D doesn't use C as a backend, but compiles directly to native. I'd guess the code generation you'd get from doing so with D would be absolute horrific to read, because you'll get rid of CTFE, templates, proper class structure, globals properly stored, since everything in D is TLS and C doesn't then you'll see weird constructs everywhere. If you ask me, it's probably not something you want to do and there really isn't a reason to do it with D either. D has an almost 100% compatibility with C already.
Jan 23 2017
parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Monday, 23 January 2017 at 14:53:54 UTC, Bauss wrote:
 I'd guess the code generation you'd get from doing so with D 
 would be absolute horrific to read, because you'll get rid of 
 CTFE, templates, proper class structure, globals properly 
 stored, since everything in D is TLS and C doesn't then you'll 
 see weird constructs everywhere.
TLS is a nonissue. You can wrap the TLS declarations in macros.
Jan 23 2017