News for hackers. Stuff that matters.
Re:Adding ANSI C11 C compiler to D so it can import and comp... by WalterBright on Monday May 10, 2021 @05:10AM (#27104100)
> D has gone with the route of calling gcc.

Not exactly. D is going the route of actually implementing a C compiler in the D compiler. Here's the C parser:

https://github.com/dlang/dmd/pull/12507/files#diff-2fc171ca9...

You could argue that writing a C compiler instead of using libclang is an indicator of brain-damage, but I lay out my reasons here:

https://digitalmars.com/d/archives/digitalmars/D/Add_ImportC...

And besides, C is such a simple language it's kinda fun.

Re:Adding ANSI C11 C compiler to D so it can import and comp... by AndyKelley on Monday May 10, 2021 @01:50PM (#27108768)
> 9. Without a C compiler, we're stuck with, wedded to, and beholden to libclang.

> I wouldn't be surprised that the eventual cost of adapting ourselves to

> libclang will exceed the cost of doing our own C compiler.

This is a really insightful point. I had to learn this the hard way :)

We might follow your lead on this, as we have done with so many other great ideas implemented in D.

Ironically, Vexu started from the other side as you, with the preprocessor mostly done, but the backend left to-do: https://github.com/Vexu/arocc

One thing that might make libclang worth the cost, however, is its ability to compile C++ code as well. On Zig's end of things, all we have to do is provide libcxx, libcxxabi, libunwind, compiler-rt, and linking, and then libclang is really pulling a lot of weight by compiling C++ code into object files. Sadly this ability is just too useful in practice to ignore. For example, LLVM itself is C++ so if Zig wants to be able to bootstrap itself, it needs this capability.

Still, I think your maneuver here is the best long-term approach to tackle this problem, and I imagine as time goes on we'll start to migrate towards D's solution here. Maybe someday the Zig distribution that does not have LLVM extensions enabled will be the more popular one.

I'll be watching the evolution of this new feature in D with great interest!

Re:Adding ANSI C11 C compiler to D so it can import and comp... by WalterBright on Monday May 10, 2021 @02:57PM (#27109502)
I agree that this technique of writing my own C compiler isn't going to work so well with C++:

1. Even though I have written a C++ compiler, it's a C++98 compiler and C++, like the Winchester House, accretes new features constantly, meaning keeping it up to date is impractical.

2. C++ semantics are different enough from D that it is impractical to mechanically map C++ to D. You can do bits and pieces, and dpp does that, but seamlessly doing the bulk of it isn't going to work. (C can be mapped nearly 100% to D.)

dpp uses libclang to try and map what it can to D.

Re:Adding ANSI C11 C compiler to D so it can import and comp... by beeforpork on Monday May 10, 2021 @05:30AM (#27104220)
So how extremely could I exploit this? Could I write a program entirely in C, compile it with the D compiler, and use D's unit test framework? That would be insane (very good insane, obviously).
Re:Adding ANSI C11 C compiler to D so it can import and comp... by WalterBright on Monday May 10, 2021 @05:40AM (#27104284)
That is a great question. The point of ImportC is to import the declarations from a C file. But it turns out you can simply give the C file on the command line, and it'll compile it and make an executable:

    echo hello.c
    int printf(const char*,...);
    int main() { printf("hello world\n"); }

    dmd hello.c

    ./hello
    hello world
But then there's the question "should D features be allowed in the C code?" This is a very subversive question. Is it really C if that is allowed? Already, the C code allows for Compile Time Function Execution, which comes for free by passing the C code through the D front end. You can see it in action in the test cases in this file:

https://github.com/dlang/dmd/pull/12507/files#diff-b94776a02...

Scroll down a bit and you'll see it executes C functions at compile time. I decided to flirt with heresy and leave this in, because it sure makes adding tests convenient!

I'm less sure about adding unittests to the C compiler. Certainly, you can always add the unittests to the D code that is importing the C code. This will probably hold the charges of abomination to a minimum.

Re:Adding ANSI C11 C compiler to D so it can import and comp... by crazypython on Monday May 10, 2021 @07:46AM (#27104877)
> Already, the C code allows for Compile Time Function Execution, which comes for free by passing the C code through the D front end.

Oh my good gosh. D and C could be seamless together. This is great.

> I decided to flirt with heresy and leave this in

I'd love to see more C language extensions in ImportC.

Re:Adding ANSI C11 C compiler to D so it can import and comp... by agumonkey on Monday May 10, 2021 @12:17PM (#27107684)
Do you produce a D ~object from c source which is then processed by dmd ?
Re:Adding ANSI C11 C compiler to D so it can import and comp... by mhh__ on Monday May 10, 2021 @12:54PM (#27108104)
It produces a D AST from C syntax.
Re:Adding ANSI C11 C compiler to D so it can import and comp... by mistrial9 on Monday May 10, 2021 @09:33AM (#27105801)
congrats! C is simple sure, in some ways.. but it is the complete standard library that makes it behave like "C" for writing programs. This was by design of course
Re:Adding ANSI C11 C compiler to D so it can import and comp... by tomcam on Wednesday May 12, 2021 @03:08AM (#27127576)
You are a beast