3 ms·
For the reference Rust did consider a similar style of C++ FFI back then [1] [2] [3], but to my knowledge it went nowhere due to the sheer amount of work requir
by lifthrasiir 5y ago
For the reference Rust did consider a similar style of C++ FFI back then [1] [2] [3], but to my knowledge it went nowhere due to the sheer amount of work required and the inability to future-proof. Inline assembly is another similar feature that D is substantially different from Rust, and the rationale for Rust [4] was also similar: D's inline assembly DSL only works in x86 and x86-64.
[1] https://github.com/rust-lang/rust/issues/5853 https://github.com/rust-lang/rust/issues/5853
[2] https://github.com/rust-lang/rfcs/issues/602 https://github.com/rust-lang/rfcs/issues/602
[3] https://internals.rust-lang.org/t/better-c-interoperability/2650 https://internals.rust-lang.org/t/better-c-interoperability/...
[4] https://rust-lang.github.io/rfcs/2873-inline-asm.html#implement-an-embedded-dsl https://rust-lang.github.io/rfcs/2873-inline-asm.html#implem...
- nicwilson 5y ago> D's inline assembly DSL only works in x86 and x86-64. You're conflating DMD (the reference compiler) with the language here. LDC and GDC (the LLVM and GCC based D compilers) both support GCC style "extended" inline assembly[0] and LDC even allows you to write inline LLVM IR. [0]: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
- WalterBright 5y agoYou're quite right, the D language leaves the inline assembler as implementation defined. DMD has a quite nice assembler that was lifted and updated from the older Digital Mars C++ inline assembler. Each of the D compilers supports inline assembler for each of the targets the particular compiler supports.
- lifthrasiir 5y agoI might not have been clear (sorry!) but the DSL here refers to the integrated assembly syntax as opposed to LLVM/GCC's textual approaches. Both syntaxes are officially documented in the D Language Reference [1], which is arguably the specification for D the language, but since DMD came much earlier than other implementations, I think it's fair to consider the DSL to be designed alongside with D. Anyway LDC and GCD's inability to support the DSL strengthens my original point. [1] https://dlang.org/spec/iasm.html https://dlang.org/spec/iasm.html
- mhh__ 5y agoLDC also supports most of the extended inline asm. GDC used to support it (long story but that code is now the LDC impl), but it was removed a long time ago. GDC isn't that much younger than dmd.
- WalterBright 5y agoAs for the amount of work required, we just rolled up our sleeves and did it :-) To improve interactivity with C, we even went so far as to build a C compiler into the D compiler. It was officially released this week.
- lifthrasiir 5y agoIt seems that the C reimplementation is becoming a preferred approach for C FFI in recent languages---at least ones that can afford the cost. Not just D's ImportC but there are other examples like Python cffi. C++ FFI is another story though.
- melony 5y agoZig has C++ FFI
- lifthrasiir 5y agoZig's C and C++ FFI is basically based on embedding clang, which seems the only way for its level of FFI in case of C++. For C it is much more doable to make a new C compiler specifically for FFI.
- WalterBright 5y agoD's ImportC also allows the C code to be extended with D-ish features, like forward references, compile time function execution, and modules.