www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - CTFE C compiler

reply Johan Engelen <j j.nl> writes:
In LLVMweekly [1], I read the following:
"You may be be interested, amazed, and/or horrified to learn of 
constexpr-8cc [2]. It provides a compile-time C compiler 
implemented as C++14 constant expressions."

The constexpr compiler is generated using ELVM [3], a C compiler 
that targets a bunch of other languages (instead of targeting 
machine code). The constexpr compiler was built by adding a 
C++14-constexpr target to ELVM. [4]

A fun project: add a D CTFE backend? ;)

cheers,
   Johan


[1] http://llvmweekly.org/issue/151
[2] https://github.com/kw-udon/constexpr-8cc
[3] https://github.com/shinh/elvm
[4] 
https://github.com/shinh/elvm/commit/b6e2fed3326d57d05f1a354938bd3b9545ab701b
Nov 21 2016
next sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
On Monday, 21 November 2016 at 10:41:27 UTC, Johan Engelen wrote:
 In LLVMweekly [1], I read the following:
 "You may be be interested, amazed, and/or horrified to learn of 
 constexpr-8cc [2]. It provides a compile-time C compiler 
 implemented as C++14 constant expressions."

 The constexpr compiler is generated using ELVM [3], a C 
 compiler that targets a bunch of other languages (instead of 
 targeting machine code). The constexpr compiler was built by 
 adding a C++14-constexpr target to ELVM. [4]

 A fun project: add a D CTFE backend? ;)

 cheers,
   Johan


 [1] http://llvmweekly.org/issue/151
 [2] https://github.com/kw-udon/constexpr-8cc
 [3] https://github.com/shinh/elvm
 [4] 
 https://github.com/shinh/elvm/commit/b6e2fed3326d57d05f1a354938bd3b9545ab701b
I have been working on similar things for quite a while now. Compiling C at ctfe is perfectly doable. (If your input source is relatively small because CTFE implementation issues.) A compiling The full body of D at compiletime will eventually become possible with the mainstream D-Frontend..
Nov 21 2016
prev sibling parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Monday, 21 November 2016 at 10:41:27 UTC, Johan Engelen wrote:
 In LLVMweekly [1], I read the following:
 "You may be be interested, amazed, and/or horrified to learn of 
 constexpr-8cc [2]. It provides a compile-time C compiler 
 implemented as C++14 constant expressions."

 The constexpr compiler is generated using ELVM [3], a C 
 compiler that targets a bunch of other languages (instead of 
 targeting machine code). The constexpr compiler was built by 
 adding a C++14-constexpr target to ELVM. [4]

 A fun project: add a D CTFE backend? ;)

 cheers,
   Johan


 [1] http://llvmweekly.org/issue/151
 [2] https://github.com/kw-udon/constexpr-8cc
 [3] https://github.com/shinh/elvm
 [4] 
 https://github.com/shinh/elvm/commit/b6e2fed3326d57d05f1a354938bd3b9545ab701b
mixin(import("myCode.d")); Even better because it doesn't have to output the program at runtime, the generated executable *is* the compiled code ;). I guess that's just the same as #include though.... If you want to compile to other languages, just use the above with ldc --output-ll or --output-bc, then get an llvm decompiler to get that to your target language :) A C compiler using ctfe, now that would take some more work.
Nov 21 2016
parent reply Johan Engelen <j j.nl> writes:
On Monday, 21 November 2016 at 12:59:26 UTC, John Colvin wrote:
 mixin(import("myCode.d"));

 Even better because it doesn't have to output the program at 
 runtime, the generated executable *is* the compiled code ;). I 
 guess that's just the same as #include though....

 If you want to compile to other languages, just use the above 
 with ldc --output-ll or --output-bc, then get an llvm 
 decompiler to get that to your target language :)

 A C compiler using ctfe, now that would take some more work.
I think there is some confusion: a D-CTFE C-compiler is exactly what my OP was about ;) -Johan
Nov 21 2016
parent Johan Engelen <j j.nl> writes:
On Monday, 21 November 2016 at 16:37:41 UTC, Johan Engelen wrote:
 
 I think there is some confusion: a D-CTFE C-compiler is exactly 
 what my OP was about ;)
Not really true, my OP was about adding a backend to a compiler that compiles C to another language. The new backend should output D code that is completely CTFE-able.
Nov 21 2016