digitalmars.D - D as an extension language for C
- Walter Bright (3/3) May 11 2023 https://twitter.com/WalterBright/status/1656574787874095104
- Coco (5/8) May 11 2023 Interesting, yes... or at least.. perhaps.
- user (9/18) May 12 2023 Couldn't help but observe that
- zjh (3/12) May 12 2023 If D can be a good glue language, it is also good. D can
- zjh (5/7) May 12 2023 I have a `weak` ecosystem, but I can borrow that I can
- Sergey (2/19) May 13 2023 C is already in that place. FFI C has almost every language..
- Walter Bright (23/24) May 13 2023 It already is. D has a better assembler than Gnu, and VC doesn't even ha...
- Dave P. (4/14) May 13 2023 Unfortunately for that example, dmd’s implementation of c varargs
- IGotD- (16/34) May 14 2023 You keep mention on this forum that you keep on hitting corner
- Walter Bright (8/23) May 14 2023 We've had 3 converters so far.
- IGotD- (2/4) May 15 2023 What are the C features that cannot be expressed in D?
- Patrick Schluter (2/7) May 16 2023 VLA's and flexible arrays.
- IGotD- (9/13) May 16 2023 A C flexible array can be translated to a slice. I have done this
- Andrew (6/9) May 16 2023 Bit fields are already something that's provided in Phobos:
- Adam D Ruppe (5/8) May 16 2023 ImportC can do C bitfields, but since they're layout is undefined
- Walter Bright (20/24) May 16 2023 Pedantically, the C layout is not undefined, it is implementation define...
- Daniel N (4/9) May 17 2023 I agree 100%. Usually unhappy users are more vokal than happy
- max haughton (10/16) May 17 2023 This is what makes them a major footgun. Other than being
- Walter Bright (6/8) May 17 2023 That's also why people implementing ELF use typedefs instead of ints, et...
- Johan (5/11) May 17 2023 What defines the "associated C compiler"?
- Walter Bright (4/6) May 17 2023 It's referred to in the D spec.
- Quirin Schroll (14/22) May 24 2023 Yes, the syntax is awful. It should have been `__vector(bool[n])`:
- Adam D Ruppe (4/6) May 24 2023 How would that interact with things like `sign.sizeof`?
- Walter Bright (13/22) May 16 2023 Some more things:
- jmh530 (3/16) May 14 2023 Is ImportC the final destination? Or is ImportC a step along the
- Walter Bright (2/4) May 14 2023 Not sure what you mean. It is an end in itself.
- jmh530 (15/19) May 15 2023 You said before: "But D itself cannot read C .h files nor .c
- Walter Bright (3/7) May 15 2023 The .h file limitation is due to a disagreement between myself and Iain ...
- jmh530 (21/29) May 16 2023 I hope some agreement is reached.
https://twitter.com/WalterBright/status/1656574787874095104 I love making discoveries of emergent behavior like this! It's certainly saved me a lot of work.
May 11 2023
On Thursday, 11 May 2023 at 18:11:32 UTC, Walter Bright wrote:https://twitter.com/WalterBright/status/1656574787874095104 I love making discoveries of emergent behavior like this! It's certainly saved me a lot of work.Interesting, yes... or at least.. perhaps. But personally, I think the D language's obsession with the C language.. has already gone too far. Rust and Swift are showing where languages should be heading.
May 11 2023
On Thursday, 11 May 2023 at 22:08:30 UTC, Coco wrote:On Thursday, 11 May 2023 at 18:11:32 UTC, Walter Bright wrote:Couldn't help but observe that D started as better Java by aot compiling similar code Then D wanted to become better C++ with ranges, structs instead of classes, nogc, etc Now D wants to be betterC, C extension, etc Future: D as better assembly /s :-)https://twitter.com/WalterBright/status/1656574787874095104 I love making discoveries of emergent behavior like this! It's certainly saved me a lot of work.Interesting, yes... or at least.. perhaps. But personally, I think the D language's obsession with the C language.. has already gone too far. Rust and Swift are showing where languages should be heading.
May 12 2023
On Friday, 12 May 2023 at 22:07:43 UTC, user wrote:On Thursday, 11 May 2023 at 22:08:30 UTC, Coco wrote:Couldn't help but observe that D started as better Java by aot compiling similar code Then D wanted to become better C++ with ranges, structs instead of classes, nogc, etc Now D wants to be betterC, C extension, etc Future: D as better assembly /s :-)If D can be a good glue language, it is also good. D can interface with `C++, rust, C, and asm` etc.
May 12 2023
On Saturday, 13 May 2023 at 00:35:04 UTC, zjh wrote:If D can be a good glue language, it is also good. D can interface with `C++, rust, C, and asm` etc.I have a `weak` ecosystem, but I can borrow that I can `interface` with, so there's nothing wrong with it. Looking at the new language, which one does not support interfacing with `C`?
May 12 2023
On Saturday, 13 May 2023 at 00:35:04 UTC, zjh wrote:On Friday, 12 May 2023 at 22:07:43 UTC, user wrote:C is already in that place. FFI C has almost every language..On Thursday, 11 May 2023 at 22:08:30 UTC, Coco wrote:Couldn't help but observe that D started as better Java by aot compiling similar code Then D wanted to become better C++ with ranges, structs instead of classes, nogc, etc Now D wants to be betterC, C extension, etc Future: D as better assembly /s :-)If D can be a good glue language, it is also good. D can interface with `C++, rust, C, and asm` etc.
May 13 2023
On 5/12/2023 3:07 PM, user wrote:Future: D as better assemblyIt already is. D has a better assembler than Gnu, and VC doesn't even have an assembler. Perhaps I haven't clarified the purpose of ImportC. D was designed from the beginning to leverage off of existing C code. D's foreign function interface with C is excellent. But D itself cannot read C .h files nor .c files (a capability that C++ has and has used to great advantage). .h files must be laboriously converted to D. We've done a lot of the usual system .h files conversions, such as core.stdc.stdio, they are in druntime. The Deimos project is to collect crowd-sourced conversions of C .h files. The fundamental problem with hand conversions is they are tedious, boring work that nobody wants to do. They also need redoing whenever the vendor of the .h file changes them. As the D community grows, this simply does not scale. Then there were three attempts at programmatic conversion of .h to .d files, one written by me, one by Jacob, and one by Atila. Those brought us to 3rd base. To do a home run, the compiler needs to understand C code directly - this is ImportC. Using D as an extension language for ImportC enabled me to easily get working a number of C features (such as variadic arguments) that would otherwise have required a fair chunk of explicit code to implement. Instead, I was able have it use D's existing implementation code (which is non-trivial) in core.stdc.stdarg. I.e. ImportC is an enabler of D use.
May 13 2023
On Saturday, 13 May 2023 at 18:09:33 UTC, Walter Bright wrote:On 5/12/2023 3:07 PM, user wrote:Unfortunately for that example, dmd’s implementation of c varargs is buggy. https://issues.dlang.org/show_bug.cgi?id=23409Future: D as better assembly[…] Using D as an extension language for ImportC enabled me to easily get working a number of C features (such as variadic arguments) that would otherwise have required a fair chunk of explicit code to implement. Instead, I was able have it use D's existing implementation code (which is non-trivial) in core.stdc.stdarg. I.e. ImportC is an enabler of D use.
May 13 2023
On Saturday, 13 May 2023 at 18:09:33 UTC, Walter Bright wrote:D was designed from the beginning to leverage off of existing C code. D's foreign function interface with C is excellent.I thought it was C++ you wanted to leverage.But D itself cannot read C .h files nor .c files (a capability that C++ has and has used to great advantage). .h files must be laboriously converted to D. We've done a lot of the usual system .h files conversions, such as core.stdc.stdio, they are in druntime. The Deimos project is to collect crowd-sourced conversions of C .h files. The fundamental problem with hand conversions is they are tedious, boring work that nobody wants to do. They also need redoing whenever the vendor of the .h file changes them. As the D community grows, this simply does not scale. Then there were three attempts at programmatic conversion of .h to .d files, one written by me, one by Jacob, and one by Atila. Those brought us to 3rd base. To do a home run, the compiler needs to understand C code directly - this is ImportC.You keep mention on this forum that you keep on hitting corner cases with importC which s expected. C is old and there many quirks, non-standard and compiler specific code out there. I would still gone the converter route as it would instead translate the .h file to native D which you then can use natively within D. For example you haven't enabled C macros to be used within D, with a converter you could have converted the macro to D code (mixin?). I think that would have scaled better than trying to retrofit a C compiler into D compiler. Also why should C be a first class citizen and not C++, Rust etc. Since there are plenty of languages out there a converter project for each language is more convenient. I'm a bit afraid that importC will be a feature that needs constant patching and consume time.
May 14 2023
On 5/14/2023 1:21 AM, IGotD- wrote:I thought it was C++ you wanted to leverage.I knew back then the impracticality of importing C++ code.You keep mention on this forum that you keep on hitting corner cases with importC which s expected. C is old and there many quirks, non-standard and compiler specific code out there. I would still gone the converter route as it would instead translate the .h file to native D which you then can use natively within D.We've had 3 converters so far.For example you haven't enabled C macros to be used within D, with a converter you could have converted the macro to D code (mixin?). I think that would have scaled better than trying to retrofit a C compiler into D compiler.There are a number of C features which do not translate into D. But with ImportC I can make them work seamlessly.Also why should C be a first class citizen and not C++, Rust etc.C is the lingua franka, not C++ or Rust.Since there are plenty of languages out there a converter project for each language is more convenient.I can only do so much.I'm a bit afraid that importC will be a feature that needs constant patching and consume time.C changes a lot slower than other languages.
May 14 2023
On Sunday, 14 May 2023 at 23:56:55 UTC, Walter Bright wrote:There are a number of C features which do not translate into D. But with ImportC I can make them work seamlessly.What are the C features that cannot be expressed in D?
May 15 2023
On Monday, 15 May 2023 at 20:19:05 UTC, IGotD- wrote:On Sunday, 14 May 2023 at 23:56:55 UTC, Walter Bright wrote:VLA's and flexible arrays.There are a number of C features which do not translate into D. But with ImportC I can make them work seamlessly.What are the C features that cannot be expressed in D?
May 16 2023
On Tuesday, 16 May 2023 at 07:48:42 UTC, Patrick Schluter wrote:On Monday, 15 May 2023 at 20:19:05 UTC, IGotD- wrote:A C flexible array can be translated to a slice. I have done this myself and was amused how good fit it was. In 99% of the cases there is some upper limit for the flexible array and a slice is an extra guard against out of bounds accesses that C doesn't have. VLA should be a part of the FFI and not importC. The only thing I can think of that isn't supported are bitfields. The question is if this could have been solved with meta programming.What are the C features that cannot be expressed in D?VLA's and flexible arrays.
May 16 2023
On Tuesday, 16 May 2023 at 11:04:53 UTC, IGotD- wrote:The only thing I can think of that isn't supported are bitfields. The question is if this could have been solved with meta programming.Bit fields are already something that's provided in Phobos: https://dlang.org/phobos/std_bitmanip.html#bitfields I can't attest to how good they are, but I think it's the right approach; rather choose to implement something in the std lib than the language itself to avoid unnecessary complexity.
May 16 2023
On Tuesday, 16 May 2023 at 11:04:53 UTC, IGotD- wrote:The only thing I can think of that isn't supported are bitfields. The question is if this could have been solved with meta programming.ImportC can do C bitfields, but since they're layout is undefined it is useless anyway. (Walter also copied importC's awful bitfields into D, but I hope that mistake is reverted.)
May 16 2023
On 5/16/2023 6:36 AM, Adam D Ruppe wrote:ImportC can do C bitfields, but since they're layout is undefined it is useless anyway.Pedantically, the C layout is not undefined, it is implementation defined. D's bitfields, however, are defined to match the layout of the associated C compiler. But as a practical matter, if you stick with `int` and `unsigned` bit field types, the layout is consistent across compilers with 32 bit ints. Any other differences would only be a problem if data is moved between systems. The C code will still work. It's analogous to the size of `int` being implementation defined, along with the size of `char`, `long`, `long long`, the signedness of char being implementation defined, whether it's ones or twos complement, and so on. But C remained useful. mportC also implements a number of half-specified associated C compiler extensions, because (sadly) nearly all of the C header files unnecessarily rely on these extensions. Ironically, by making D bitfields match the associated C compiler's, it arguably makes D bitfields *more* portable.(Walter also copied importC's awful bitfields into D, but I hope that mistake is reverted.)There's always https://dlang.org/phobos/std_bitmanip.html#bitfields which do have a portable defined format. Note that nobody has implemented in Phobos a bitfield setup that matches the associated C compiler's bit fields, making such C constructs difficult to access from D. They're easy now!
May 16 2023
On Wednesday, 17 May 2023 at 03:52:33 UTC, Walter Bright wrote:On 5/16/2023 6:36 AM, Adam D Ruppe wrote:I agree 100%. Usually unhappy users are more vokal than happy users, so I just wanted to say thanks Walter, I for one love this feature!ImportC can do C bitfields, but since they're layout is undefined it is useless anyway.Ironically, by making D bitfields match the associated C compiler's, it arguably makes D bitfields *more* portable.
May 17 2023
On Wednesday, 17 May 2023 at 03:52:33 UTC, Walter Bright wrote:On 5/16/2023 6:36 AM, Adam D Ruppe wrote:This is what makes them a major footgun. Other than being underpowered for the complexity they add (I want the compiler to help me do discontinuous bitpacking not just easy stuff), you can't rely on the layout for basically anything other than space-efficiency, so you can't guarantee something will actually round-trip (say) over the network or even a filesystem/buffer on the same computer. This, for example, is why one of the ELF specifications explicitly recommends not using bitfields to implement it.ImportC can do C bitfields, but since they're layout is undefined it is useless anyway.Pedantically, the C layout is not undefined, it is implementation defined. D's bitfields, however, are defined to match the layout of the associated C compiler.
May 17 2023
On 5/17/2023 2:53 AM, max haughton wrote:This, for example, is why one of the ELF specifications explicitly recommends not using bitfields to implement it.That's also why people implementing ELF use typedefs instead of ints, etc., and have special code to deal with endianness, which is also implementation defined. Despite all this implementation defined behavior in C, there are some things that while pedantically implementation defined, are in practice consistently defined across diverse compilers. The int bitfields, for example.
May 17 2023
On Wednesday, 17 May 2023 at 03:52:33 UTC, Walter Bright wrote:On 5/16/2023 6:36 AM, Adam D Ruppe wrote:What defines the "associated C compiler"? It's the first time I hear it for D. thanks, JohanImportC can do C bitfields, but since they're layout is undefined it is useless anyway.Pedantically, the C layout is not undefined, it is implementation defined. D's bitfields, however, are defined to match the layout of the associated C compiler.
May 17 2023
On 5/17/2023 4:25 AM, Johan wrote:What defines the "associated C compiler"?The D implementation.It's the first time I hear it for D.It's referred to in the D spec. https://dlang.org/glossary.html#acc
May 17 2023
On Tuesday, 16 May 2023 at 13:36:46 UTC, Adam D Ruppe wrote:On Tuesday, 16 May 2023 at 11:04:53 UTC, IGotD- wrote:Yes, the syntax is awful. It should have been `__vector(bool[n])`: ```d struct minifloat { __vector(bool[3]) significand; __vector(bool[4]) exponent; __vector(bool[1]) sign; } ``` C++ leads the way with `std::vector<bool>` and its packed bits. 😁 No, in all seriousness, `__vector(bool[n])` wouldn’t be nearly as bad a syntax as C’s bit-fields. And giving `__vector(bool[n])` special semantics is nowhere close to daring.The only thing I can think of that isn't supported are bitfields. The question is if this could have been solved with meta programming.ImportC can do C bitfields, but since they're layout is undefined it is useless anyway. (Walter also copied importC's awful bitfields into D, but I hope that mistake is reverted.)
May 24 2023
On Wednesday, 24 May 2023 at 14:28:52 UTC, Quirin Schroll wrote:Yes, the syntax is awful. It should have been `__vector(bool[n])`:How would that interact with things like `sign.sizeof`? I wrote about some of these things in more detail here btw: http://dpldocs.info/this-week-in-d/Blog.Posted_2022_09_12.html#bitfields
May 24 2023
On 5/16/2023 12:48 AM, Patrick Schluter wrote:On Monday, 15 May 2023 at 20:19:05 UTC, IGotD- wrote:Some more things: 1. struct tags and non-tag names co-existing in the same scope 2. enum member names get "promoted" to the surrounding scope, losing their enum-ness 3. bit fields (since fixed by adding bitfields to D!) 4. C's everything-in-one-global-namespace issue 5. default initialization isn't always zero in D 6. different rules for `const` (ImportC admittedly fakes it) If you look at the frontend source code, the semantic routines have lots of "if it's C do this instead". Of course, "cannot" is an absolute word. There are always ways to make something work, but the result may be just too ugly or otherwise unpalatable. With ImportC, we just let C be C instead of trying to bash it into D code.On Sunday, 14 May 2023 at 23:56:55 UTC, Walter Bright wrote:VLA's and flexible arrays.There are a number of C features which do not translate into D. But with ImportC I can make them work seamlessly.What are the C features that cannot be expressed in D?
May 16 2023
On Saturday, 13 May 2023 at 18:09:33 UTC, Walter Bright wrote:[snip] Perhaps I haven't clarified the purpose of ImportC. D was designed from the beginning to leverage off of existing C code. D's foreign function interface with C is excellent. But D itself cannot read C .h files nor .c files (a capability that C++ has and has used to great advantage). .h files must be laboriously converted to D. We've done a lot of the usual system .h files conversions, such as core.stdc.stdio, they are in druntime. [snip] To do a home run, the compiler needs to understand C code directly - this is ImportC. [snip]Is ImportC the final destination? Or is ImportC a step along the way to a final destination?
May 14 2023
On 5/14/2023 4:39 PM, jmh530 wrote:Is ImportC the final destination? Or is ImportC a step along the way to a final destination?Not sure what you mean. It is an end in itself.
May 14 2023
On Sunday, 14 May 2023 at 23:57:32 UTC, Walter Bright wrote:On 5/14/2023 4:39 PM, jmh530 wrote:You said before: "But D itself cannot read C .h files nor .c files (a capability that C++ has and has used to great advantage)" importC lets you import .c file in D, but not a .h file. To import a .h file in D, I have to create a .c file and include only the .h file header and then import the .c file. Would you be able to streamline that further so that the .h file can be imported? Maybe behind the scenes, it would look for a .d file, then a .c file, then try to create a blank .c file and include a .h? I don't know what makes the most sense, but think about it this way: for me to say some random C library works for D, I need to create a separate project for it with a .c file that includes the .h. If you can import the .h file directly, then it would just work (TM).Is ImportC the final destination? Or is ImportC a step along the way to a final destination?Not sure what you mean. It is an end in itself.
May 15 2023
On 5/15/2023 5:20 AM, jmh530 wrote:importC lets you import .c file in D, but not a .h file. To import a .h file in D, I have to create a .c file and include only the .h file header and then import the .c file. Would you be able to streamline that further so that the .h file can be imported?The .h file limitation is due to a disagreement between myself and Iain about the best way forward for it.
May 15 2023
On Monday, 15 May 2023 at 19:13:49 UTC, Walter Bright wrote:On 5/15/2023 5:20 AM, jmh530 wrote:I hope some agreement is reached. Looking over the (multiple) github discussions, I was partial to the solution of `import "foo.h"` combined with a custom search path for `.h` files [1]. The one to use `import("foo.h")` looks a bit too much like an import expression. You made a comment about the import syntax getting kludged [2], but it would be a little less kludge-y if you could import any file like `import "foo.d"` or `import "foo.c"`, but then only allow .d/.c/.i files to be imported like `import foo`. Further, I recall you previously commenting on how sometimes you have to make the compiler more complicated in order to make things work how users expect them to. This might be a situation like that. Though I can't say what the best solution is, I think most would agree that it would be good to have something better than creating a .c file to import a .h file. Not a lot of code has been written yet that uses importC so there is still time to get it right. [1] https://github.com/dlang/dmd/pull/14700 [2] https://github.com/dlang/dmd/pull/14864#issuecomment-1432256522importC lets you import .c file in D, but not a .h file. To import a .h file in D, I have to create a .c file and include only the .h file header and then import the .c file. Would you be able to streamline that further so that the .h file can be imported?The .h file limitation is due to a disagreement between myself and Iain about the best way forward for it.
May 16 2023