165 points · by ingve · 5y ago · 105 comments
He implemented the ANSI C compiler here in just 5 days.
Semi-related project, using libclang to import C++ directly (with C++ templates and all the other features, regular D has C++ linking and exception support built-in): https://github.com/Syniurge/Calypso
Some quotes from the forum https://forum.dlang.org/post/s7a37h$2bbv$1@digitalmars.com :
- Writing a C preprocessor is a nightmare. Fortunately, I already did that https://github.com/facebookarchive/warp and we can use it if we choose.
- The D code will be able to inline C code, and even CTFE it.
- There are a lot of wacky C extensions out there. We only need to implement currently used ones (not the 16 bit stuff), and only the stuff that appears in C headers.
- 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 good work but let's be realistic - it doesn't work yet on any real C code I've tried it with.
Yeah, it doesn't support C headers yet. But an excellent prototype nonetheless.
Lol, he is a paid hype man for the Dlang community, and you should ask him about Symmetry Investments and if they are hiring.
====
make
dmd -c -O -inline -release -ofwarp.o cmdline.d constexpr.d context.d directive.d expanded.d file.d id.d lexer.d loc.d macros.d main.d number.d outdeps.d ranges.d skip.d sources.d stringlit.d textbuf.d charclass.d
context.d(222): Error: Implicit string concatenation is error-prone and disallowed in D
context.d(222): Use the explicit syntax instead (concatenating literals is @nogc): "# 1 \"%2$s//\"\x0a" ~ "# 1 \"<command-line>\"\x0a"
context.d(223): Error: Implicit string concatenation is error-prone and disallowed in D
context.d(223): Use the explicit syntax instead (concatenating literals is @nogc): "# 1 \"<command-line>\"\x0a" ~ "# 1 \"%1$s\"\x0a"
macros.d(329): Error: Built-in hex string literals are obsolete, use std.conv.hexString!"FD 61 FD" instead.
macros.d(333): Error: Built-in hex string literals are obsolete, use std.conv.hexString!"FD" instead.
macros.d(338): Error: Built-in hex string literals are obsolete, use std.conv.hexString!"FD 61 62 FD FD" instead.
ranges.d(71): Error: Implicit string concatenation is error-prone and disallowed in D
ranges.d(71): Use the explicit syntax instead (concatenating literals is @nogc): "Buffer overflowed. Possibly caused by forgetting to " ~ "complete a git merge in your code."
Makefile:44: recipe for target 'warp.o' failed
make: ** [warp.o] Error 1
====
That isn't why Warp was discontinued. (My last change to Warp was 7 years ago.) Even with my C code, it's unsurprising to have to update a few things after 7 years.
My version of the repository is:
https://github.com/DigitalMars/dmpp
and compiles with DMD 2.067. I'll see about updating it later today. Stay tuned!
====
make
dmd -c -O -inline -release -ofwarp.o cmdline.d constexpr.d context.d directive.d expanded.d file.d id.d lexer.d loc.d macros.d main.d number.d outdeps.d ranges.d skip.d sources.d stringlit.d textbuf.d charclass.d
util.d(20): Error: function `loc.Loc.write(File* f)` is not callable using argument types `(File function() nothrow @nogc @property ref @system)`
util.d(20): cannot pass argument `& makeGlobal` of type `File function() nothrow @nogc @property ref @system` to parameter `File* f`
directive.d(470): Error: template instance `util.err_warning!(string, string)` error instantiating
context.d(264): instantiated from here: `parseDirective!(Lexer!(Context!(LockingTextWriter)))`
main.d(39): instantiated from here: `Context!(LockingTextWriter)`
Makefile:44: recipe for target 'warp.o' failed
make: *
[warp.o] Error 1====
I saw this addition in zig and thought that it was amazing, a lot of languages have this need to create definitions in their own language, C# with [DllImport](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.i...), [crystal with lib](https://crystal-lang.org/reference/syntax_and_semantics/c_bi...), I think since C is so ubiquitous and being able to just consume header files just removes so much boilerplate creation that is easy to get wrong.
zig also decided to just call an existing compiler (clang) and if you do zig cc --version then zig thinks it is clang, because the zig compiler just calls into clang main. 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.
> 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!
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.
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.
It's not bolted in the compiler so less nice than Zig but the jar produced by jextract is not ABI dependent so more portable. The ABI dependent code is generated by the JIT using the the Foreign Linker API (the equivalent of C# DllImport) at runtime.
https://docs.microsoft.com/en-us/previous-versions/visualstu...
Unfortunately there's a lot of disagreement between the bigger players as to how this should be handled:
> > 1) The authors appear to be deadlocked
> No need to soften it with an "appear", we are intractably deadlocked.
to bystanders, odd autocorrect: it's marshalling. marshalling data. although fight and war are indeed also appropriate associations for binary data exchange (struct conversions) between languages.
Marshal = mariscalcus = "horse servant", compare mare and seneschal. Martial = from Mars, god of war.
Not mocking the spelling of the previous post; it's a strange term anyway and in this instance either spelling works.
An added benefit is you get ffi-reflect -- https://github.com/corsix/ffi-reflect -- over the resulting types / functions...
Despite being told numerous compile-time options to use by different manuals and documents and people on IRC, nothing appeared to change the outcome. It might be due to some undefined behaviour in bzflag, of course, but it made zig cc a non-starter for C.
https://github.com/ziglang/zig/issues/4830#issuecomment-6054...
Major props to Walter and the rest of the D developers for being so seemingly in tune with what people are looking for in a language.
That's a step in the right direction! I love D!
I a way I'd rather use C than lua. I wish I could easily integrate python as a scripting language, but python is slow and its runtime is heavy. TinyCC, however, is very light.
The standard library won't be a variant you can use without the GC but rather one that is allocator agnostic wherever reasonable
Which is more important?
- https://en.wikipedia.org/wiki/Modula-3
- http://joeduffyblog.com/2015/11/03/blogging-about-midori/
- http://www.projectoberon.com/ (see https://www.astrobe.com)
- Eric Bier Demonstrates Cedar (https://www.youtube.com/watch?v=z_dt7NG38V4)
There are a lot of people in 2021 who - for whatever reason - feel like they need to program personal computers without using GC. As you point history shows that's a lot less true than people think, but nevertheless - that is the sentiment.
Dlang tries spends a lot of effort trying to attract these people, by making more and more of the stdlib nogc, having @nogc annotations, experimenting with deterministic manual memory management etc etc. But I feel like they'll never convert most of them.
https://dlang.org/blog/2018/12/04/interview-liran-zvibel-of-...
For me currently D provides a good level of balance between those worlds, but the aforementioned GC isn't an issue for me at all so I have it easy.
I think D should latch and promote more on the idea of having on-demand performance when it's necessary with GC convenience for the rest of the application. Soon having borrow checker capability ala rust and cyclone will only strengthen the narrative.
The obvious examples are Ddoc, the builtin documentation generator, and unittest, the builtin unit tester.
Binding C++ libraries is quite risky in the sense that the libraries usually aren't meant to be consumed via that API. For that reason it's mainly used (publicly at least, there are things I know that I cannot say) for exposing D to C++ in a sane way (Mangling, vtables, templates, all work, exceptions even!).
ldc and gdc are a mix of D and C++ due to the respective backends.