Adding ANSI C11 C compiler to D so it can import and compile C files directly

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

Our great sponsors
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • SaaSHub - Software Alternatives and Reviews
  • dmd

    dmd D Programming Language compiler

  • 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.

  • cgame

    some ideas involving games, C and Lua

  • LuaJIT was "good enough" at this that it worked pretty well for me -- mostly for automatically binding to headers from my own code where I was careful to avoid some of the potential gotchas. With a macro the header was stringified and sent to LuaJIT; could've also had a build step. eg: https://github.com/nikki93/cgame/blob/master/src/sprite.h#L9

    An added benefit is you get ffi-reflect -- https://github.com/corsix/ffi-reflect -- over the resulting types / functions...

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
  • ffi-reflect

    LuaJIT FFI reflection library

  • LuaJIT was "good enough" at this that it worked pretty well for me -- mostly for automatically binding to headers from my own code where I was careful to avoid some of the potential gotchas. With a macro the header was stringified and sent to LuaJIT; could've also had a build step. eg: https://github.com/nikki93/cgame/blob/master/src/sprite.h#L9

    An added benefit is you get ffi-reflect -- https://github.com/corsix/ffi-reflect -- over the resulting types / functions...

  • bzflag

    3D multi-player tank battle game

  • zig

    General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.

  • This is due to zig enabling clangs UBSAN by default in unoptimized builds. This can be disabled with `-fno-sanitize=undefined` or by enabling optimizations with `-O2` or `-O3`.

    https://github.com/ziglang/zig/issues/4830#issuecomment-6054...

  • warp

    Discontinued A fast preprocessor for C and C++ (by facebookarchive)

  • But dlang has a long problem of change constantly without backward compatibility and a lot of projects just die because of it, for example the mentioned https://github.com/facebookarchive/warp :

    ====

    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 \"\"\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 \"\"\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

    ====

  • arocc

    A C compiler written in Zig.

  • > 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!

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • dmpp

    C preprocessor

  • > die because of it

    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.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts